Add default_options argument to find_program()

pull/11814/head
Nomura 1 year ago committed by Xavier Claessens
parent 183e4b8e90
commit 50baf3c626
  1. 4
      docs/markdown/snippets/find_program_default_options.md
  2. 10
      docs/yaml/functions/find_program.yaml
  3. 15
      mesonbuild/interpreter/interpreter.py
  4. 1
      mesonbuild/interpreter/kwargs.py
  5. 5
      test cases/common/265 default_options in find_program/meson.build
  6. 5
      test cases/common/265 default_options in find_program/subprojects/dummy.wrap
  7. 3
      test cases/common/265 default_options in find_program/subprojects/dummy/dummy.c
  8. 6
      test cases/common/265 default_options in find_program/subprojects/dummy/meson.build
  9. 1
      test cases/common/265 default_options in find_program/subprojects/dummy/meson_options.txt

@ -0,0 +1,4 @@
## find_program() now supports the 'default_options' argument
In a similar fashion as dependency(), find_program() now also allows you to set default
options for the subproject that gets built in case of a fallback.

@ -113,3 +113,13 @@ kwargs:
type: list[str]
since: 0.53.0
description: extra list of absolute paths where to look for program names.
default_options:
type: list[str] | dict[str | bool | int | list[str]]
since: 1.3.0
description: |
An array of default option values
that override those set in the subproject's `meson.options`
(like `default_options` in [[project]], they only have
effect when Meson is run for the first time, and command line
arguments override any default options in build files)

@ -1619,6 +1619,7 @@ class Interpreter(InterpreterBase, HoldableObject):
# the host machine.
def find_program_impl(self, args: T.List[mesonlib.FileOrString],
for_machine: MachineChoice = MachineChoice.HOST,
default_options: T.Optional[T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]] = None,
required: bool = True, silent: bool = True,
wanted: T.Union[str, T.List[str]] = '',
search_dirs: T.Optional[T.List[str]] = None,
@ -1627,7 +1628,7 @@ class Interpreter(InterpreterBase, HoldableObject):
args = mesonlib.listify(args)
extra_info: T.List[mlog.TV_Loggable] = []
progobj = self.program_lookup(args, for_machine, required, search_dirs, extra_info)
progobj = self.program_lookup(args, for_machine, default_options, required, search_dirs, extra_info)
if progobj is None:
progobj = self.notfound_program(args)
@ -1671,6 +1672,7 @@ class Interpreter(InterpreterBase, HoldableObject):
return progobj
def program_lookup(self, args: T.List[mesonlib.FileOrString], for_machine: MachineChoice,
default_options: T.Optional[T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]],
required: bool, search_dirs: T.List[str], extra_info: T.List[mlog.TV_Loggable]
) -> T.Optional[T.Union[ExternalProgram, build.Executable, OverrideProgram]]:
progobj = self.program_from_overrides(args, extra_info)
@ -1686,7 +1688,7 @@ class Interpreter(InterpreterBase, HoldableObject):
if wrap_mode != WrapMode.nofallback and self.environment.wrap_resolver:
fallback = self.environment.wrap_resolver.find_program_provider(args)
if fallback and wrap_mode == WrapMode.forcefallback:
return self.find_program_fallback(fallback, args, required, extra_info)
return self.find_program_fallback(fallback, args, default_options, required, extra_info)
progobj = self.program_from_file_for(for_machine, args)
if progobj is None:
@ -1695,18 +1697,19 @@ class Interpreter(InterpreterBase, HoldableObject):
prog = ExternalProgram('python3', mesonlib.python_command, silent=True)
progobj = prog if prog.found() else None
if progobj is None and fallback and required:
progobj = self.find_program_fallback(fallback, args, required, extra_info)
progobj = self.find_program_fallback(fallback, args, default_options, required, extra_info)
return progobj
def find_program_fallback(self, fallback: str, args: T.List[mesonlib.FileOrString],
default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]],
required: bool, extra_info: T.List[mlog.TV_Loggable]
) -> T.Optional[T.Union[ExternalProgram, build.Executable, OverrideProgram]]:
mlog.log('Fallback to subproject', mlog.bold(fallback), 'which provides program',
mlog.bold(' '.join(args)))
sp_kwargs: kwtypes.DoSubproject = {
'required': required,
'default_options': {},
'default_options': default_options or {},
'version': [],
'cmake_options': [],
'options': None,
@ -1722,6 +1725,7 @@ class Interpreter(InterpreterBase, HoldableObject):
REQUIRED_KW,
KwargInfo('dirs', ContainerTypeInfo(list, str), default=[], listify=True, since='0.53.0'),
KwargInfo('version', ContainerTypeInfo(list, str), default=[], listify=True, since='0.52.0'),
DEFAULT_OPTIONS.evolve(since='1.3.0')
)
@disablerIfNotFound
def func_find_program(self, node: mparser.BaseNode, args: T.Tuple[T.List[mesonlib.FileOrString]],
@ -1733,7 +1737,8 @@ class Interpreter(InterpreterBase, HoldableObject):
return self.notfound_program(args[0])
search_dirs = extract_search_dirs(kwargs)
return self.find_program_impl(args[0], kwargs['native'], required=required,
default_options = kwargs['default_options']
return self.find_program_impl(args[0], kwargs['native'], default_options=default_options, required=required,
silent=False, wanted=kwargs['version'],
search_dirs=search_dirs)

@ -232,6 +232,7 @@ class Summary(TypedDict):
class FindProgram(ExtractRequired, ExtractSearchDirs):
default_options: T.Dict[OptionKey, T.Union[str, int, bool, T.List[str]]]
native: MachineChoice
version: T.List[str]

@ -0,0 +1,5 @@
project('test default_options in find_program')
dummy_exe = find_program('dummy', default_options: ['subproject_option=true'])
test('test_dummy', dummy_exe)

@ -0,0 +1,5 @@
[wrap-file]
directory = dummy
[provide]
program_names = dummy

@ -0,0 +1,6 @@
project('dummy', 'c')
if get_option('subproject_option')
dummy_exe = executable('dummy', 'dummy.c')
meson.override_find_program('dummy', dummy_exe)
endif

@ -0,0 +1 @@
option('subproject_option', type: 'boolean', value: false)
Loading…
Cancel
Save