From 6fb65e3af2aa2adcd88d52efb5c74bee8a813053 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 11 Dec 2023 12:06:31 -0800 Subject: [PATCH] mconf|msetup: use Protocol for argparse options These are much easier to handle as one commit since msetup calls mconf internally. This has found one case where things are being carefully crafted to work in mconf even though msetup has slightly different options --- mesonbuild/mconf.py | 12 ++++++++++-- mesonbuild/msetup.py | 30 ++++++++++++++++++++++++------ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/mesonbuild/mconf.py b/mesonbuild/mconf.py index b9547f0b251d..8c18eab3102d 100644 --- a/mesonbuild/mconf.py +++ b/mesonbuild/mconf.py @@ -1,5 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright 2014-2016 The Meson development team +# Copyright © 2023 Intel Corporation from __future__ import annotations @@ -20,8 +21,15 @@ from .mesonlib import MachineChoice, OptionKey if T.TYPE_CHECKING: + from typing_extensions import Protocol import argparse + class CMDOptions(coredata.SharedCMDOptions, Protocol): + + builddir: str + clearcache: bool + pager: bool + # cannot be TV_Loggable, because non-ansidecorators do direct string concat LOGLINE = T.Union[str, mlog.AnsiDecorator] @@ -293,7 +301,7 @@ def print_nondefault_buildtype_options(self) -> None: for m in mismatching: mlog.log(f'{m[0]:21}{m[1]:10}{m[2]:10}') -def run_impl(options: argparse.Namespace, builddir: str) -> int: +def run_impl(options: CMDOptions, builddir: str) -> int: print_only = not options.cmd_line_options and not options.clearcache c = None try: @@ -325,7 +333,7 @@ def run_impl(options: argparse.Namespace, builddir: str) -> int: pass return 0 -def run(options: argparse.Namespace) -> int: +def run(options: CMDOptions) -> int: coredata.parse_cmd_line_options(options) builddir = os.path.abspath(os.path.realpath(options.builddir)) return run_impl(options, builddir) diff --git a/mesonbuild/msetup.py b/mesonbuild/msetup.py index 2c663ec6c8e9..d4e03ccd98d6 100644 --- a/mesonbuild/msetup.py +++ b/mesonbuild/msetup.py @@ -1,5 +1,6 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright 2016-2018 The Meson development team +# Copyright © 2023 Intel Corporation from __future__ import annotations @@ -11,6 +12,21 @@ from . import build, coredata, environment, interpreter, mesonlib, mintro, mlog from .mesonlib import MesonException +if T.TYPE_CHECKING: + from typing_extensions import Protocol + from .coredata import SharedCMDOptions + + class CMDOptions(SharedCMDOptions, Protocol): + + profile: bool + fatal_warnings: bool + reconfigure: bool + wipe: bool + clearcache: bool + builddir: str + sourcedir: str + pager: bool # this is needed for mconf compatibility + git_ignore_file = '''# This file is autogenerated by Meson. If you change or delete it, it won't be recreated. * ''' @@ -53,7 +69,7 @@ def add_arguments(parser: argparse.ArgumentParser) -> None: parser.add_argument('sourcedir', nargs='?', default=None) class MesonApp: - def __init__(self, options: argparse.Namespace) -> None: + def __init__(self, options: CMDOptions) -> None: self.options = options (self.source_dir, self.build_dir) = self.validate_dirs() if options.wipe: @@ -173,7 +189,7 @@ def generate(self, capture: bool = False, vslite_ctx: T.Optional[dict] = None) - def _generate(self, env: environment.Environment, capture: bool, vslite_ctx: T.Optional[dict]) -> T.Optional[dict]: # Get all user defined options, including options that have been defined # during a previous invocation or using meson configure. - user_defined_options = argparse.Namespace(**vars(self.options)) + user_defined_options = T.cast('CMDOptions', argparse.Namespace(**vars(self.options))) coredata.read_cmd_line_file(self.build_dir, user_defined_options) mlog.debug('Build started at', datetime.datetime.now().isoformat()) @@ -300,7 +316,7 @@ def finalize_postconf_hooks(self, b: build.Build, intr: interpreter.Interpreter) for mod in intr.modules.values(): mod.postconf_hook(b) -def run_genvslite_setup(options: argparse.Namespace) -> None: +def run_genvslite_setup(options: CMDOptions) -> None: # With --genvslite, we essentially want to invoke multiple 'setup' iterations. I.e. - # meson setup ... builddirprefix_debug # meson setup ... builddirprefix_debugoptimized @@ -334,12 +350,14 @@ def run_genvslite_setup(options: argparse.Namespace) -> None: app = MesonApp(options) app.generate(capture=False, vslite_ctx=vslite_ctx) -def run(options: T.Union[argparse.Namespace, T.List[str]]) -> int: - if not isinstance(options, argparse.Namespace): +def run(options: T.Union[CMDOptions, T.List[str]]) -> int: + if isinstance(options, list): parser = argparse.ArgumentParser() add_arguments(parser) - options = parser.parse_args(options) + options = T.cast('CMDOptions', parser.parse_args(options)) coredata.parse_cmd_line_options(options) + # Because we need to pass this to mconf, but it won't actually be used + options.pager = False if mesonlib.OptionKey('genvslite') in options.cmd_line_options.keys(): run_genvslite_setup(options)