diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2017-12-03 23:03:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-03 23:03:13 +0200 |
commit | 112e6c1927760d33d9392b0aa3ed26596bbe68f7 (patch) | |
tree | 8fe66972926550208d62109b910f7dce072ef63e | |
parent | 018deb48feee88a1de73f8bcaea4d944dade7827 (diff) | |
parent | 80157a5ea3faa8e53a3806203fc76251540c9652 (diff) | |
download | meson-112e6c1927760d33d9392b0aa3ed26596bbe68f7.zip meson-112e6c1927760d33d9392b0aa3ed26596bbe68f7.tar.gz meson-112e6c1927760d33d9392b0aa3ed26596bbe68f7.tar.bz2 |
Merge pull request #2638 from jibsen/use-value-regex
Use regex to substitute template strings
4 files changed, 36 insertions, 5 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index a35345b..09b5d92 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -744,6 +744,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) @@ -770,12 +772,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..3f6a159 --- /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) |