Skip to content

Commit b78bedc

Browse files
bonzinijpakkane
authored andcommitted
options: reuse set_option_maybe_root
There is common logic hiding between project() and "meson configure": the complication that the comment mentions for the "default_options" case actually applies to "meson configure", to machine files, to command line options and to project options. Reuse the same function in all four cases. Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 8394979 commit b78bedc

File tree

2 files changed

+22
-41
lines changed

2 files changed

+22
-41
lines changed

mesonbuild/options.py

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,19 +1060,30 @@ def replace(v: str) -> str:
10601060

10611061
return changed
10621062

1063-
def set_option_maybe_root(self, o: OptionKey, new_value: str) -> bool:
1063+
def set_option_maybe_root(self, o: OptionKey, new_value: ElementaryOptionValues, first_invocation: bool = False) -> bool:
10641064
if not self.is_cross and o.is_for_build():
10651065
return False
10661066

1067+
# This is complicated by the fact that a string can have two meanings:
1068+
#
1069+
# default_options: 'foo=bar'
1070+
#
1071+
# can be either
1072+
#
1073+
# A) a system option in which case the subproject is None
1074+
# B) a project option, in which case the subproject is '' (this method is only called from top level)
1075+
#
1076+
# The key parsing function can not handle the difference between the two
1077+
# and defaults to A.
10671078
if o in self.options:
1068-
return self.set_option(o, new_value)
1069-
if self.accept_as_pending_option(o):
1079+
return self.set_option(o, new_value, first_invocation)
1080+
if self.accept_as_pending_option(o, first_invocation=first_invocation):
10701081
old_value = self.pending_options.get(o, None)
10711082
self.pending_options[o] = new_value
10721083
return old_value is None or str(old_value) == new_value
10731084
else:
10741085
o = o.as_root()
1075-
return self.set_option(o, new_value)
1086+
return self.set_option(o, new_value, first_invocation)
10761087

10771088
def set_from_configure_command(self, D_args: T.List[str], U_args: T.List[str]) -> bool:
10781089
dirty = False
@@ -1289,22 +1300,10 @@ def initialize_from_top_level_project_call(self,
12891300
project_default_options_in: OptionDict,
12901301
cmd_line_options_in: OptionDict,
12911302
machine_file_options_in: T.Mapping[OptionKey, ElementaryOptionValues]) -> None:
1292-
first_invocation = True
12931303
(project_default_options, cmd_line_options, machine_file_options) = self.first_handle_prefix(project_default_options_in,
12941304
cmd_line_options_in,
12951305
machine_file_options_in)
12961306
for keystr, valstr in project_default_options.items():
1297-
# Ths is complicated by the fact that a string can have two meanings:
1298-
#
1299-
# default_options: 'foo=bar'
1300-
#
1301-
# can be either
1302-
#
1303-
# A) a system option in which case the subproject is None
1304-
# B) a project option, in which case the subproject is '' (this method is only called from top level)
1305-
#
1306-
# The key parsing function can not handle the difference between the two
1307-
# and defaults to A.
13081307
if isinstance(keystr, str):
13091308
key = OptionKey.from_string(keystr)
13101309
else:
@@ -1315,33 +1314,21 @@ def initialize_from_top_level_project_call(self,
13151314
continue
13161315
if key.subproject:
13171316
self.augments[key] = valstr
1318-
elif key in self.options:
1319-
self.set_option(key, valstr, first_invocation)
13201317
else:
1321-
# Setting a project option with default_options.
1322-
# Argubly this should be a hard error, the default
1318+
# Setting a project option with default_options
1319+
# should arguably be a hard error; the default
13231320
# value of project option should be set in the option
13241321
# file, not in the project call.
1325-
proj_key = key.as_root()
1326-
if self.is_project_option(proj_key):
1327-
self.set_option(proj_key, valstr)
1328-
else:
1329-
self.pending_options[key] = valstr
1322+
self.set_option_maybe_root(key, valstr, True)
13301323
for key, valstr in machine_file_options.items():
13311324
# Due to backwards compatibility we ignore all build-machine options
13321325
# when building natively.
13331326
if not self.is_cross and key.is_for_build():
13341327
continue
13351328
if key.subproject:
13361329
self.augments[key] = valstr
1337-
elif key in self.options:
1338-
self.set_option(key, valstr, first_invocation)
13391330
else:
1340-
proj_key = key.as_root()
1341-
if proj_key in self.options:
1342-
self.set_option(proj_key, valstr, first_invocation)
1343-
else:
1344-
self.pending_options[key] = valstr
1331+
self.set_option_maybe_root(key, valstr, True)
13451332
for keystr, valstr in cmd_line_options.items():
13461333
if isinstance(keystr, str):
13471334
key = OptionKey.from_string(keystr)
@@ -1353,14 +1340,8 @@ def initialize_from_top_level_project_call(self,
13531340
continue
13541341
if key.subproject:
13551342
self.augments[key] = valstr
1356-
elif key in self.options:
1357-
self.set_option(key, valstr, True)
13581343
else:
1359-
proj_key = key.as_root()
1360-
if proj_key in self.options:
1361-
self.set_option(proj_key, valstr, True)
1362-
else:
1363-
self.pending_options[key] = valstr
1344+
self.set_option_maybe_root(key, valstr, True)
13641345

13651346
def accept_as_pending_option(self, key: OptionKey, known_subprojects: T.Optional[T.Container[str]] = None,
13661347
first_invocation: bool = False) -> bool:

unittests/platformagnostictests.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,12 +421,12 @@ def test_setup_with_unknown_option(self):
421421

422422
with self.subTest('unknown user option'):
423423
out = self.init(testdir, extra_args=['-Dnot_an_option=1'], allow_fail=True)
424-
self.assertIn('ERROR: Unknown options: "not_an_option"', out)
424+
self.assertIn('ERROR: Unknown option: "not_an_option"', out)
425425

426426
with self.subTest('unknown builtin option'):
427427
self.new_builddir()
428428
out = self.init(testdir, extra_args=['-Db_not_an_option=1'], allow_fail=True)
429-
self.assertIn('ERROR: Unknown options: "b_not_an_option"', out)
429+
self.assertIn('ERROR: Unknown option: "b_not_an_option"', out)
430430

431431

432432
def test_configure_new_option(self) -> None:

0 commit comments

Comments
 (0)