diff options
4 files changed, 34 insertions, 4 deletions
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index b889aa0..77145b1 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -1139,7 +1139,8 @@ class MesonMain(InterpreterObject): class Interpreter(InterpreterBase): - def __init__(self, build, backend, subproject='', subdir='', subproject_dir='subprojects'): + def __init__(self, build, backend, subproject='', subdir='', subproject_dir='subprojects', + default_project_options=[]): super().__init__(build.environment.get_source_dir(), subdir) self.build = build self.environment = build.environment @@ -1156,6 +1157,7 @@ class Interpreter(InterpreterBase): self.args_frozen = False self.subprojects = {} self.subproject_stack = [] + self.default_project_options = default_project_options[:] # Passed from the outside, only used in subprojects. self.build_func_dict() self.parse_project() self.builtin['build_machine'] = BuildMachine(self.coredata.compilers) @@ -1413,7 +1415,8 @@ class Interpreter(InterpreterBase): os.makedirs(os.path.join(self.build.environment.get_build_dir(), subdir), exist_ok=True) self.args_frozen = True mlog.log('\nExecuting subproject ', mlog.bold(dirname), '.\n', sep='') - subi = Interpreter(self.build, self.backend, dirname, subdir, self.subproject_dir) + subi = Interpreter(self.build, self.backend, dirname, subdir, self.subproject_dir, + mesonlib.stringlistify(kwargs.get('default_options', []))) subi.subprojects = self.subprojects subi.subproject_stack = self.subproject_stack + [dirname] @@ -1489,16 +1492,38 @@ class Interpreter(InterpreterBase): self.coredata.set_builtin_option(key, value) # If this was set on the command line, do not override. else: + # Option values set with subproject() default_options override those + # set in project() default_options. + pref = key + '=' + for i in self.default_project_options: + if i.startswith(pref): + option = i + break # If we are in a subproject, add the subproject prefix to option # name. if self.subproject != '': option = self.subproject + ':' + option newoptions = [option] + self.environment.cmd_line_options.projectoptions self.environment.cmd_line_options.projectoptions = newoptions + # Add options that are only in default_options. + for defopt in self.default_project_options: + key, value = defopt.split('=') + pref = key + '=' + was_found = False + for i in default_options: + if i.startswith(pref): + was_found = True + break + if was_found: + break + defopt = self.subproject + ':' + defopt + newoptions = [defopt] + self.environment.cmd_line_options.projectoptions + self.environment.cmd_line_options.projectoptions = newoptions @stringArgs def func_project(self, node, args, kwargs): - if self.environment.first_invocation and 'default_options' in kwargs: + if self.environment.first_invocation and ('default_options' in kwargs or \ + len(self.default_project_options) > 0): self.parse_default_options(kwargs['default_options']) if not self.is_subproject(): self.build.project_name = args[0] diff --git a/test cases/unit/3 subproject defaults/meson.build b/test cases/unit/3 subproject defaults/meson.build index 0f64c9b..3bf05d0 100644 --- a/test cases/unit/3 subproject defaults/meson.build +++ b/test cases/unit/3 subproject defaults/meson.build @@ -3,7 +3,7 @@ project('subproject defaults', 'c', 'fromcmdline=defopt'] # This should get the value set in command line. ) -subproject('foob') +subproject('foob', default_options : ['fromspfunc=spfunc', 'fromspfunconly=spfunc']) assert(get_option('fromcmdline') == 'cmdline', 'Default option defined in cmd line is incorrect: ' + get_option('fromcmdline')) assert(get_option('defopoverride') == 'defopt', 'Default option without cmd line override is incorrect: ' + get_option('defopoverride')) diff --git a/test cases/unit/3 subproject defaults/subprojects/foob/meson.build b/test cases/unit/3 subproject defaults/subprojects/foob/meson.build index 48d0210..69f01d1 100644 --- a/test cases/unit/3 subproject defaults/subprojects/foob/meson.build +++ b/test cases/unit/3 subproject defaults/subprojects/foob/meson.build @@ -1,9 +1,12 @@ project('foob', 'c', default_options : ['defopoverride=s_defopt', # This should be overridden. + 'fromspfunc=s_defopt', # This is specified with a default_options kwarg to subproject() 'fromcmdline=s_defopt'] # This should get the value set in command line. ) assert(get_option('fromcmdline') == 's_cmdline', 'Default option defined in cmd line is incorrect: ' + get_option('fromcmdline')) +assert(get_option('fromspfunc') == 'spfunc', 'Default option set with subproject() incorrect: ' + get_option('fromspfunc')) +assert(get_option('fromspfunconly') == 'spfunc', 'Default option set with subproject() incorrect: ' + get_option('fromspfunc')) assert(get_option('defopoverride') == 's_defopt', 'Default option without cmd line override is incorrect: ' + get_option('defopoverride')) assert(get_option('fromoptfile') == 's_optfile', 'Default value from option file is incorrect: ' + get_option('fromoptfile')) diff --git a/test cases/unit/3 subproject defaults/subprojects/foob/meson_options.txt b/test cases/unit/3 subproject defaults/subprojects/foob/meson_options.txt index 7a1f6c0..a9a615e 100644 --- a/test cases/unit/3 subproject defaults/subprojects/foob/meson_options.txt +++ b/test cases/unit/3 subproject defaults/subprojects/foob/meson_options.txt @@ -1,3 +1,5 @@ option('defopoverride', type : 'string', value : 's_optfile', description : 'A value for overriding.') option('fromcmdline', type : 'string', value : 's_optfile', description : 'A value for overriding.') +option('fromspfunc', type : 'string', value : 's_optfile', description : 'A value for overriding.') +option('fromspfunconly', type : 'string', value : 's_optfile', description : 'A value for overriding.') option('fromoptfile', type : 'string', value : 's_optfile', description : 'A value for not overriding.') |