From f31142de88a282c2e526f56d2f2c2a0347442e77 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Fri, 17 Nov 2017 10:03:04 +0100 Subject: Use regex to substitute template strings --- mesonbuild/mesonlib.py | 9 ++++----- .../checkcopy.py | 9 +++++++++ .../166 custom target template substitution/foo.c.in | 6 ++++++ .../166 custom target template substitution/meson.build | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 test cases/common/166 custom target template substitution/checkcopy.py create mode 100644 test cases/common/166 custom target template substitution/foo.c.in create mode 100644 test cases/common/166 custom target template substitution/meson.build diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index e9cf6cf96..787fc4279 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 000000000..ab9f436ce --- /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 000000000..d53846f52 --- /dev/null +++ b/test cases/common/166 custom target template substitution/foo.c.in @@ -0,0 +1,6 @@ +#include + +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 000000000..00fe078f7 --- /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) -- cgit v1.2.3 From 80157a5ea3faa8e53a3806203fc76251540c9652 Mon Sep 17 00:00:00 2001 From: Joergen Ibsen Date: Sat, 25 Nov 2017 10:06:14 +0100 Subject: Make prefix substitution test uppercase --- test cases/common/166 custom target template substitution/meson.build | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test cases/common/166 custom target template substitution/meson.build b/test cases/common/166 custom target template substitution/meson.build index 00fe078f7..3f6a15951 100644 --- a/test cases/common/166 custom target template substitution/meson.build +++ b/test cases/common/166 custom target template substitution/meson.build @@ -6,12 +6,12 @@ config = configuration_data() in = configure_file(configuration : config, output : 'x@IN') -# Check that substitution does not find @foo@ and then misses @INPUT0@. +# 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@'] + command : [check, '-D@FOO@INPUT0@PUT1@', '@INPUT1@', '@OUTPUT@'] ) executable('foo', foo) -- cgit v1.2.3