configure_file: Don't optimize away substitutions

It's possible that the configuration data object has components added
conditionally, and that sometimes an empty configuration data object
is passed on purpose.

Instead, we do the substitution and also warn if no tokens were found
that could've been substituted.

Closes https://github.com/mesonbuild/meson/issues/3826
pull/3833/head
Nirbheek Chauhan 7 years ago committed by Nirbheek Chauhan
parent 2ac6f6be32
commit 602e58d398
  1. 28
      mesonbuild/interpreter.py
  2. 8
      mesonbuild/mesonlib.py
  3. 13
      run_unittests.py
  4. 23
      test cases/common/16 configure file/meson.build
  5. 1
      test cases/common/16 configure file/nosubst-nocopy1.txt.in
  6. 1
      test cases/common/16 configure file/nosubst-nocopy2.txt.in

@ -3444,20 +3444,6 @@ root and issuing %s.
raise InterpreterException('Output file name must not contain a subdirectory.')
(ofile_path, ofile_fname) = os.path.split(os.path.join(self.subdir, output))
ofile_abs = os.path.join(self.environment.build_dir, ofile_path, ofile_fname)
# Optimize copies by not doing substitution if there's nothing to
# substitute, and warn about this legacy hack
if 'configuration' in kwargs:
conf = kwargs['configuration']
if not isinstance(conf, ConfigurationDataHolder):
raise InterpreterException('Argument "configuration" must be of type configuration_data')
if ifile_abs and not conf.keys():
del kwargs['configuration']
kwargs['copy'] = True
mlog.warning('Got an empty configuration_data() object: '
'optimizing copy automatically; if you want to '
'copy a file to the build dir, use the \'copy:\' '
'keyword argument added in 0.47.0', location=node)
conf.mark_used()
# Perform the appropriate action
if 'configuration' in kwargs:
conf = kwargs['configuration']
@ -3467,15 +3453,21 @@ root and issuing %s.
if inputfile is not None:
os.makedirs(os.path.join(self.environment.build_dir, self.subdir), exist_ok=True)
file_encoding = kwargs.setdefault('encoding', 'utf-8')
missing_variables = mesonlib.do_conf_file(ifile_abs, ofile_abs,
conf.held_object, fmt,
file_encoding)
missing_variables, confdata_useless = \
mesonlib.do_conf_file(ifile_abs, ofile_abs, conf.held_object,
fmt, file_encoding)
if missing_variables:
var_list = ", ".join(map(repr, sorted(missing_variables)))
mlog.warning(
"The variable(s) %s in the input file %s are not "
"The variable(s) %s in the input file '%s' are not "
"present in the given configuration data." % (
var_list, inputfile), location=node)
if confdata_useless:
ifbase = os.path.basename(ifile_abs)
mlog.warning('Got an empty configuration_data() object and found no '
'substitutions in the input file {!r}. If you want to '
'copy a file to the build dir, use the \'copy:\' keyword '
'argument added in 0.47.0'.format(ifbase), location=node)
else:
mesonlib.dump_conf_header(ofile_abs, conf.held_object, output_format)
conf.mark_used()

@ -653,12 +653,18 @@ def do_conf_file(src, dst, confdata, format, encoding='utf-8'):
result = []
missing_variables = set()
# Detect when the configuration data is empty and no tokens were found
# during substitution so we can warn the user to use the `copy:` kwarg.
confdata_useless = not confdata.keys()
for line in data:
if line.startswith(search_token):
confdata_useless = False
line = do_mesondefine(line, confdata)
else:
line, missing = do_replacement(regex, line, format, confdata)
missing_variables.update(missing)
if missing:
confdata_useless = False
result.append(line)
dst_tmp = dst + '~'
try:
@ -668,7 +674,7 @@ def do_conf_file(src, dst, confdata, format, encoding='utf-8'):
raise MesonException('Could not write output file %s: %s' % (dst, str(e)))
shutil.copymode(src, dst_tmp)
replace_if_different(dst, dst_tmp)
return missing_variables
return missing_variables, confdata_useless
CONF_C_PRELUDE = '''/*
* Autogenerated by the Meson build system.

@ -1864,7 +1864,7 @@ int main(int argc, char **argv) {
r'meson.build:6: WARNING: a warning of some sort',
r'sub' + os.path.sep + r'meson.build:4: WARNING: subdir warning',
r'meson.build:7: WARNING: Module unstable-simd has no backwards or forwards compatibility and might not exist in future releases.',
r"meson.build:11: WARNING: The variable(s) 'MISSING' in the input file conf.in are not present in the given configuration data.",
r"meson.build:11: WARNING: The variable(s) 'MISSING' in the input file 'conf.in' are not present in the given configuration data.",
r'meson.build:1: WARNING: Passed invalid keyword argument "invalid".',
]:
self.assertRegex(out, re.escape(expected))
@ -2286,6 +2286,17 @@ recommended as it is not supported on some platforms''')
self.assertRegex(out, "WARNING: Project specifies a minimum meson_version '>=0.45'")
self.assertRegex(out, " * 0.47.0: {'dict'}")
def test_configure_file_warnings(self):
testdir = os.path.join(self.common_test_dir, "16 configure file")
out = self.init(testdir)
self.assertRegex(out, "WARNING:.*'empty'.*config.h.in.*not present.*")
self.assertRegex(out, "WARNING:.*'FOO_BAR'.*nosubst-nocopy2.txt.in.*not present.*")
self.assertRegex(out, "WARNING:.*'empty'.*config.h.in.*not present.*")
self.assertRegex(out, "WARNING:.*empty configuration_data.*test.py.in")
# No warnings about empty configuration data objects passed to files with substitutions
self.assertNotRegex(out, "WARNING:.*empty configuration_data.*nosubst-nocopy1.txt.in")
self.assertNotRegex(out, "WARNING:.*empty configuration_data.*nosubst-nocopy2.txt.in")
class FailureTests(BasePlatformTests):
'''

@ -156,11 +156,11 @@ configure_file(
)
test('test7', executable('prog7', 'prog7.c'))
# Test empty configuration data object on invalid utf8 file
# Test copying of an empty configuration data object
inf = 'invalid-utf8.bin.in'
outf = configure_file(input : inf,
output : 'invalid-utf8.bin',
configuration : configuration_data())
copy: true)
ret = run_command(check_file, inf, outf)
if ret.returncode() != 0
error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
@ -185,6 +185,25 @@ configure_file(
configuration : conf8
)
# Test that passing an empty configuration_data() object to a file with
# #mesondefine substitutions does not print the warning.
configure_file(
input: 'nosubst-nocopy1.txt.in',
output: 'nosubst-nocopy1.txt',
configuration : configuration_data()
)
# Test that passing an empty configuration_data() object to a file with
# @FOO@ substitutions does not print the warning.
configure_file(
input: 'nosubst-nocopy2.txt.in',
output: 'nosubst-nocopy2.txt',
configuration : configuration_data()
)
# Test that passing a configured file object to test() works, and that passing
# an empty configuration_data() object to a file that leads to no substitutions
# prints a warning (see unit tests)
test_file = configure_file(
input: 'test.py.in',
output: 'test.py',

Loading…
Cancel
Save