diff options
author | Joergen Ibsen <ji@ibse.dk> | 2017-11-17 10:03:04 +0100 |
---|---|---|
committer | Joergen Ibsen <ji@ibse.dk> | 2017-11-22 10:55:35 +0100 |
commit | f31142de88a282c2e526f56d2f2c2a0347442e77 (patch) | |
tree | 07ebf9768bbe59020ddf7b19d9cc7e18d1c6a775 | |
parent | d882b6fbd466940d452cfaa890bd9270e10a93b4 (diff) | |
download | meson-f31142de88a282c2e526f56d2f2c2a0347442e77.zip meson-f31142de88a282c2e526f56d2f2c2a0347442e77.tar.gz meson-f31142de88a282c2e526f56d2f2c2a0347442e77.tar.bz2 |
Use regex to substitute template strings
4 files changed, 36 insertions, 5 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index e9cf6cf..787fc42 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -707,6 +707,8 @@ def substitute_values(command, values): _substitute_values_check_errors(command, values) # Substitution outcmd = [] + rx_keys = [re.escape(key) for key in values if key not in ('@INPUT@', '@OUTPUT@')] + value_rx = re.compile('|'.join(rx_keys)) if rx_keys else None for vv in command: if not isinstance(vv, str): outcmd.append(vv) @@ -733,12 +735,9 @@ def substitute_values(command, values): elif vv in values: outcmd.append(values[vv]) # Substitute everything else with replacement + elif value_rx: + outcmd.append(value_rx.sub(lambda m: values[m.group(0)], vv)) else: - for key, value in values.items(): - if key in ('@INPUT@', '@OUTPUT@'): - # Already done above - continue - vv = vv.replace(key, value) outcmd.append(vv) return outcmd diff --git a/test cases/common/166 custom target template substitution/checkcopy.py b/test cases/common/166 custom target template substitution/checkcopy.py new file mode 100644 index 0000000..ab9f436 --- /dev/null +++ b/test cases/common/166 custom target template substitution/checkcopy.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 + +import sys +import shutil + +if '@INPUT1@' in sys.argv[1]: + shutil.copyfile(sys.argv[2], sys.argv[3]) +else: + sys.exit('String @INPUT1@ not found in "{}"'.format(sys.argv[1])) diff --git a/test cases/common/166 custom target template substitution/foo.c.in b/test cases/common/166 custom target template substitution/foo.c.in new file mode 100644 index 0000000..d53846f --- /dev/null +++ b/test cases/common/166 custom target template substitution/foo.c.in @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main() { + printf("foo is working.\n"); + return 0; +} diff --git a/test cases/common/166 custom target template substitution/meson.build b/test cases/common/166 custom target template substitution/meson.build new file mode 100644 index 0000000..00fe078 --- /dev/null +++ b/test cases/common/166 custom target template substitution/meson.build @@ -0,0 +1,17 @@ +project('custom target template substitution', 'c') + +check = find_program('checkcopy.py') + +config = configuration_data() + +in = configure_file(configuration : config, output : 'x@IN') + +# Check that substitution does not find @foo@ and then misses @INPUT0@. +# Check the resulting x@INPUT1@ is not replaced. +foo = custom_target('runcheck', + input : [in, 'foo.c.in'], + output : 'foo.c', + command : [check, '-D@foo@INPUT0@PUT1@', '@INPUT1@', '@OUTPUT@'] +) + +executable('foo', foo) |