Use regex to substitute template strings

pull/2638/head
Joergen Ibsen 7 years ago
parent d882b6fbd4
commit f31142de88
  1. 9
      mesonbuild/mesonlib.py
  2. 9
      test cases/common/166 custom target template substitution/checkcopy.py
  3. 6
      test cases/common/166 custom target template substitution/foo.c.in
  4. 17
      test cases/common/166 custom target template substitution/meson.build

@ -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

@ -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]))

@ -0,0 +1,6 @@
#include <stdio.h>
int main() {
printf("foo is working.\n");
return 0;
}

@ -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)
Loading…
Cancel
Save