From c7aa4c8861eb6c1d3534b3b0e94e901b7b2d0206 Mon Sep 17 00:00:00 2001 From: Nirbheek Chauhan Date: Sat, 7 Mar 2020 00:34:05 +0530 Subject: [PATCH] coredata: Set default options as cmdline args that override each other The previous code was assuming that options do not depend on each other, and that you can set defaults using `dict.setdefault()`. This is not true for `buildtype` + `optimization`/`debug`, so we add defaults + overrides in the right order and use the options parsing code later to compute the values. Includes a test. Closes https://github.com/mesonbuild/meson/issues/6752 --- mesonbuild/coredata.py | 21 +++++++++++++-------- run_unittests.py | 18 ++++++++++++++++++ test cases/common/1 trivial/meson.build | 2 +- 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index fdd8cab28..a0df24ded 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -718,13 +718,8 @@ class CoreData: self.copy_build_options_from_regular_ones() def set_default_options(self, default_options, subproject, env): - # Set defaults first from conf files (cross or native), then - # override them as nec as necessary. - for k, v in env.paths.host: - if v is not None: - env.cmd_line_options.setdefault(k, v) - - # Set default options as if they were passed to the command line. + cmd_line_options = OrderedDict() + # Set project default_options as if they were passed to the cmdline. # Subprojects can only define default for user options and not yielding # builtin option. from . import optinterpreter @@ -734,7 +729,17 @@ class CoreData: and optinterpreter.is_invalid_name(k, log=False): continue k = subproject + ':' + k - env.cmd_line_options.setdefault(k, v) + cmd_line_options[k] = v + + # Override project default_options using conf files (cross or native) + for k, v in env.paths.host: + if v is not None: + cmd_line_options[k] = v + + # Override all the above defaults using the command-line arguments + # actually passed to us + cmd_line_options.update(env.cmd_line_options) + env.cmd_line_options = cmd_line_options # Create a subset of cmd_line_options, keeping only options for this # subproject. Also take builtin options if it's the main project. diff --git a/run_unittests.py b/run_unittests.py index baea70eb1..5b25b6c8c 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -3825,6 +3825,24 @@ recommended as it is not supported on some platforms''') self.assertEqual(opts['debug'], True) self.assertEqual(opts['optimization'], '0') + # Command-line parsing of buildtype settings should be the same as + # setting with `meson configure`. + # + # Setting buildtype should set optimization/debug + self.new_builddir() + self.init(testdir, extra_args=['-Dbuildtype=debugoptimized']) + opts = self.get_opts_as_dict() + self.assertEqual(opts['debug'], True) + self.assertEqual(opts['optimization'], '2') + self.assertEqual(opts['buildtype'], 'debugoptimized') + # Setting optimization/debug should set buildtype + self.new_builddir() + self.init(testdir, extra_args=['-Doptimization=2', '-Ddebug=true']) + opts = self.get_opts_as_dict() + self.assertEqual(opts['debug'], True) + self.assertEqual(opts['optimization'], '2') + self.assertEqual(opts['buildtype'], 'debugoptimized') + @skipIfNoPkgconfig @unittest.skipIf(is_windows(), 'Help needed with fixing this test on windows') def test_native_dep_pkgconfig(self): diff --git a/test cases/common/1 trivial/meson.build b/test cases/common/1 trivial/meson.build index c476c37a0..fb1142cb9 100644 --- a/test cases/common/1 trivial/meson.build +++ b/test cases/common/1 trivial/meson.build @@ -1,7 +1,7 @@ # Comment on the first line project('trivial test', # Comment inside a function call + array for language list - ['c'], + ['c'], default_options: ['buildtype=debug'], meson_version : '>=0.52.0') #this is a comment sources = 'trivial.c'