diff options
author | Nirbheek Chauhan <nirbheek@centricular.com> | 2018-07-03 22:27:44 +0530 |
---|---|---|
committer | Nirbheek Chauhan <nirbheek.chauhan@gmail.com> | 2018-07-04 00:25:01 +0000 |
commit | 602e58d398c1126b792b4d725d481c67a519f9c1 (patch) | |
tree | 243f3c0c77617348c9c9a4889d1a013fdb7919de | |
parent | 2ac6f6be3295db8bd13e1d7e158bd1a2f06a0cc0 (diff) | |
download | meson-602e58d398c1126b792b4d725d481c67a519f9c1.zip meson-602e58d398c1126b792b4d725d481c67a519f9c1.tar.gz meson-602e58d398c1126b792b4d725d481c67a519f9c1.tar.bz2 |
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
-rw-r--r-- | mesonbuild/interpreter.py | 28 | ||||
-rw-r--r-- | mesonbuild/mesonlib.py | 8 | ||||
-rwxr-xr-x | run_unittests.py | 13 | ||||
-rw-r--r-- | test cases/common/16 configure file/meson.build | 23 | ||||
-rw-r--r-- | test cases/common/16 configure file/nosubst-nocopy1.txt.in | 1 | ||||
-rw-r--r-- | test cases/common/16 configure file/nosubst-nocopy2.txt.in | 1 |
6 files changed, 52 insertions, 22 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 06d80ab..3a7716d 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -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() diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index 5f9b98a..6b1fb76 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -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. diff --git a/run_unittests.py b/run_unittests.py index b6ad20d..fe98058 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -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): ''' diff --git a/test cases/common/16 configure file/meson.build b/test cases/common/16 configure file/meson.build index 1850c2b..409765b 100644 --- a/test cases/common/16 configure file/meson.build +++ b/test cases/common/16 configure file/meson.build @@ -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', diff --git a/test cases/common/16 configure file/nosubst-nocopy1.txt.in b/test cases/common/16 configure file/nosubst-nocopy1.txt.in new file mode 100644 index 0000000..6e893a1 --- /dev/null +++ b/test cases/common/16 configure file/nosubst-nocopy1.txt.in @@ -0,0 +1 @@ +#mesondefine FOO_BAR diff --git a/test cases/common/16 configure file/nosubst-nocopy2.txt.in b/test cases/common/16 configure file/nosubst-nocopy2.txt.in new file mode 100644 index 0000000..a6a7cca --- /dev/null +++ b/test cases/common/16 configure file/nosubst-nocopy2.txt.in @@ -0,0 +1 @@ +@FOO_BAR@ |