Skip to content

Commit

Permalink
mconf|msetup: use Protocol for argparse options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
dcbaker committed Dec 21, 2023
1 parent d26e59a commit 6fb65e3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
12 changes: 10 additions & 2 deletions mesonbuild/mconf.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
30 changes: 24 additions & 6 deletions mesonbuild/msetup.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.
*
'''
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 6fb65e3

Please sign in to comment.