Skip to content

Commit

Permalink
interpreter: use typed_pos_args for run_command
Browse files Browse the repository at this point in the history
  • Loading branch information
dcbaker committed Nov 30, 2021
1 parent 3c039f4 commit 3a16851
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
26 changes: 18 additions & 8 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,17 +687,25 @@ def validate_arguments(self, args, argcount, arg_types):
@FeatureNewKwargs('run_command', '0.50.0', ['env'])
@FeatureNewKwargs('run_command', '0.47.0', ['check', 'capture'])
@permittedKwargs({'check', 'capture', 'env'})
def func_run_command(self, node, args, kwargs):
# Executables aren't actually accepted, but we allow them here to allow for
# better error messages when overridden
@typed_pos_args(
'run_command',
(build.Executable, ExternalProgram, compilers.Compiler, mesonlib.File, str),
varargs=(build.Executable, ExternalProgram, compilers.Compiler, mesonlib.File, str))
def func_run_command(self, node: mparser.BaseNode,
args: T.Tuple[T.Union[build.Executable, ExternalProgram, compilers.Compiler, mesonlib.File, str],
T.List[T.Union[build.Executable, ExternalProgram, compilers.Compiler, mesonlib.File, str]]],
kwargs):
return self.run_command_impl(node, args, kwargs)

def run_command_impl(self,
node: mparser.BaseNode,
args: T.Sequence[TYPE_nvar],
args: T.Tuple[T.Union[build.Executable, ExternalProgram, compilers.Compiler, mesonlib.File, str],
T.List[T.Union[build.Executable, ExternalProgram, compilers.Compiler, mesonlib.File, str]]],
kwargs: TYPE_nkwargs,
in_builddir: bool = False) -> RunProcess:
if len(args) < 1:
raise InterpreterException('Not enough arguments')
cmd, *cargs = args
cmd, cargs = args
capture = kwargs.get('capture', True)
srcdir = self.environment.get_source_dir()
builddir = self.environment.get_build_dir()
Expand All @@ -714,7 +722,7 @@ def run_command_impl(self,

m = 'must be a string, or the output of find_program(), files() '\
'or configure_file(), or a compiler object; not {!r}'
expanded_args = []
expanded_args: T.List[str] = []
if isinstance(cmd, build.Executable):
progname = node.args.arguments[0].value
msg = 'Program {!r} was overridden with the compiled executable {!r}'\
Expand All @@ -734,6 +742,7 @@ def run_command_impl(self,
else:
if isinstance(cmd, mesonlib.File):
cmd = cmd.absolute_path(srcdir, builddir)
# FIXME: This case can only be reached through configure_file, delete once that is typesafe
elif not isinstance(cmd, str):
raise InterpreterException('First argument ' + m.format(cmd))
# Prefer scripts in the current source directory
Expand Down Expand Up @@ -2233,9 +2242,10 @@ def func_configure_file(self, node, args, kwargs):
depfile = os.path.join(self.environment.get_scratch_dir(), depfile)
values['@DEPFILE@'] = depfile
# Substitute @INPUT@, @OUTPUT@, etc here.
cmd = mesonlib.substitute_values(kwargs['command'], values)
_cmd = mesonlib.substitute_values(kwargs['command'], values)
mlog.log('Configuring', mlog.bold(output), 'with command')
res = self.run_command_impl(node, cmd, {}, True)
cmd, *args = mesonlib.listify(_cmd)
res = self.run_command_impl(node, (cmd, args), {}, True)
if res.returncode != 0:
raise InterpreterException(f'Running configure command failed.\n{res.stdout}\n{res.stderr}')
if 'capture' in kwargs and kwargs['capture']:
Expand Down
3 changes: 1 addition & 2 deletions mesonbuild/programs.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ def description(self) -> str:
def get_version(self, interpreter: 'Interpreter') -> str:
if not self.cached_version:
raw_cmd = self.get_command() + ['--version']
cmd: T.List[T.Union[str, ExternalProgram]] = [self, '--version']
res = interpreter.run_command_impl(interpreter.current_node, cmd, {}, True)
res = interpreter.run_command_impl(interpreter.current_node, (self, ['--version']), {}, True)
if res.returncode != 0:
raise mesonlib.MesonException(f'Running {raw_cmd!r} failed')
output = res.stdout.strip()
Expand Down

0 comments on commit 3a16851

Please sign in to comment.