Skip to content

Commit ac06686

Browse files
bonzinieli-schwartz
authored andcommitted
options: commonize code to accept unknown options
The check for unknown options is duplicated in OptionStore and MesonApp. Place the better version of the two as a new method of OptionStore, and use it in OptionStore.validate_cmd_line_options. Signed-off-by: Paolo Bonzini <[email protected]> (cherry picked from commit cc382b2) # Conflicts: # mesonbuild/options.py
1 parent 1ee05a6 commit ac06686

File tree

3 files changed

+33
-17
lines changed

3 files changed

+33
-17
lines changed

mesonbuild/msetup.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from . import build, coredata, environment, interpreter, mesonlib, mintro, mlog
1313
from .mesonlib import MesonException
14-
from .options import COMPILER_BASE_OPTIONS, OptionKey
14+
from .options import OptionKey
1515

1616
if T.TYPE_CHECKING:
1717
from typing_extensions import Protocol
@@ -191,18 +191,14 @@ def generate(self, capture: bool = False, vslite_ctx: T.Optional[dict] = None) -
191191
with mesonlib.BuildDirLock(self.build_dir):
192192
return self._generate(env, capture, vslite_ctx)
193193

194-
def check_unused_options(self, coredata: 'coredata.CoreData', cmd_line_options: T.Any, all_subprojects: T.Any) -> None:
194+
def check_unused_options(self, coredata: 'coredata.CoreData', cmd_line_options: T.Any, all_subprojects: T.Mapping[str, object]) -> None:
195195
pending = coredata.optstore.pending_options
196196
errlist: T.List[str] = []
197+
known_subprojects = all_subprojects.keys()
197198
for opt in pending:
198-
# It is not an error to set wrong option for unknown subprojects or
199-
# language because we don't have control on which one will be selected.
200-
if opt.subproject and opt.subproject not in all_subprojects:
201-
continue
202-
if coredata.optstore.is_compiler_option(opt):
203-
continue
204-
if (coredata.optstore.is_base_option(opt) and
205-
opt.evolve(subproject=None, machine=mesonlib.MachineChoice.HOST) in COMPILER_BASE_OPTIONS):
199+
# It is not an error to set wrong option for unknown subprojects
200+
# because they might be used in future reconfigurations
201+
if coredata.optstore.accept_as_pending_option(opt, known_subprojects):
206202
continue
207203
keystr = str(opt)
208204
if keystr in cmd_line_options:

mesonbuild/options.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,20 +1382,29 @@ def initialize_from_top_level_project_call(self,
13821382
else:
13831383
self.pending_options[key] = valstr
13841384

1385+
def accept_as_pending_option(self, key: OptionKey, known_subprojects: T.Optional[T.Union[T.Set[str], T.KeysView[str]]] = None) -> bool:
1386+
# Fail on unknown options that we can know must exist at this point in time.
1387+
# Subproject and compiler options are resolved later.
1388+
#
1389+
# Some base options (sanitizers etc) might get added later.
1390+
# Permitting them all is not strictly correct.
1391+
if key.subproject:
1392+
if known_subprojects is None or key.subproject not in known_subprojects:
1393+
return True
1394+
if self.is_compiler_option(key):
1395+
return True
1396+
return (self.is_base_option(key) and
1397+
key.evolve(subproject=None, machine=MachineChoice.HOST) in COMPILER_BASE_OPTIONS)
1398+
13851399
def validate_cmd_line_options(self, cmd_line_options: OptionStringLikeDict) -> None:
13861400
unknown_options = []
13871401
for keystr, valstr in cmd_line_options.items():
13881402
if isinstance(keystr, str):
13891403
key = OptionKey.from_string(keystr)
13901404
else:
13911405
key = keystr
1392-
# Fail on unknown options that we can know must exist at this point in time.
1393-
# Subproject and compiler options are resolved later.
1394-
#
1395-
# Some base options (sanitizers etc) might get added later.
1396-
# Permitting them all is not strictly correct.
1397-
if key.subproject is None and not self.is_compiler_option(key) and not self.is_base_option(key) and \
1398-
key in self.pending_options:
1406+
1407+
if key in self.pending_options and not self.accept_as_pending_option(key):
13991408
unknown_options.append(f'"{key}"')
14001409

14011410
if unknown_options:

unittests/optiontests.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ def test_b_default(self):
203203
value = optstore.get_default_for_b_option(OptionKey('b_vscrt'))
204204
self.assertEqual(value, 'from_buildtype')
205205

206+
def test_b_nonexistent(self):
207+
optstore = OptionStore(False)
208+
assert optstore.accept_as_pending_option(OptionKey('b_ndebug'))
209+
assert not optstore.accept_as_pending_option(OptionKey('b_whatever'))
210+
211+
def test_subproject_nonexistent(self):
212+
optstore = OptionStore(False)
213+
subprojects = {'found'}
214+
assert not optstore.accept_as_pending_option(OptionKey('foo', subproject='found'), subprojects)
215+
assert optstore.accept_as_pending_option(OptionKey('foo', subproject='whatisthis'), subprojects)
216+
206217
def test_deprecated_nonstring_value(self):
207218
# TODO: add a lot more deprecated option tests
208219
optstore = OptionStore(False)

0 commit comments

Comments
 (0)