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
pull/12263/head
Dylan Baker 12 months ago
parent 95b3b9f710
commit 5d1c3ae787
  1. 12
      mesonbuild/mconf.py
  2. 33
      mesonbuild/msetup.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 .ast import AstIDGenerator, IntrospectionInterpreter
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 @@ class Conf:
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)

@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2016-2018 The Meson development team
# Copyright © 2023-2024 Intel Corporation
from __future__ import annotations
@ -11,6 +12,21 @@ import typing as T
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
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 @@ class MesonApp:
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())
@ -294,7 +310,7 @@ class MesonApp:
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
@ -328,13 +344,18 @@ 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)
# Msetup doesn't actually use this option, but we pass msetup options to
# mconf, and it does. We won't actally hit the path that uses it, but don't
# lie
options.pager = False
if mesonlib.OptionKey('genvslite') in options.cmd_line_options.keys():
run_genvslite_setup(options)
else:

Loading…
Cancel
Save