Skip to content

Commit

Permalink
interpreter: Add native keyword argument to `meosn.override_find_pr…
Browse files Browse the repository at this point in the history
…ogram`

This is necissary in some cases, though generally it's not.
  • Loading branch information
dcbaker committed Apr 15, 2021
1 parent 46ce0b0 commit e9e62fe
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
1 change: 1 addition & 0 deletions docs/markdown/Reference-manual.md
Expand Up @@ -2092,6 +2092,7 @@ the following methods.

*(changed in 0.58.0)* Correctly tracks and handles overrides for the build
and host system separately.
*(since 0.58.0)* accepts the `native` keyword argument

- `override_dependency(name, dep_object)` *(since 0.54.0)*:
specifies that whenever `dependency(name, ...)` is used, Meson should not
Expand Down
5 changes: 5 additions & 0 deletions docs/markdown/snippets/override_find_program-native.md
Expand Up @@ -10,3 +10,8 @@ meson.override_find_program('exe', exe_for_build)
```
Meson would raise an error because `'exe'` had already been overwritten. This
has been fixed.

Additionally, a `native` keyword argument has been added. For built
dependencies, or those found with `find_program`, it's generally unnecessary
to use this argument, as Meson already know what those binaries are for. But
for scripts it is necessary.
13 changes: 7 additions & 6 deletions mesonbuild/interpreter/interpreter.py
Expand Up @@ -1278,14 +1278,15 @@ def store_name_lookups(self, command_names: T.List[T.Union[str, T.Any]], for_mac
if isinstance(name, str):
self.build.searched_programs[for_machine].add(name)

def add_find_program_override(self, name: str, exe: T.Union[ExternalProgram, build.Executable, mesonlib.File]) -> None:
if name in self.build.searched_programs[exe.for_machine]:
def add_find_program_override(self, name: str, exe: T.Union[ExternalProgram, build.Executable, mesonlib.File],
for_machine: MachineChoice) -> None:
if name in self.build.searched_programs[for_machine]:
raise InterpreterException('Tried to override finding of executable "%s" which has already been found.'
% name)
if name in self.build.find_overrides[exe.for_machine]:
if name in self.build.find_overrides[for_machine]:
raise InterpreterException('Tried to override executable "%s" which has already been overridden.'
% name)
self.build.find_overrides[exe.for_machine][name] = exe
self.build.find_overrides[for_machine][name] = exe

def notfound_program(self, args):
return ExternalProgramHolder(NonExistingExternalProgram(' '.join(args)), self.subproject)
Expand Down Expand Up @@ -2451,7 +2452,7 @@ def build_incdir_object(self, incdir_strings, is_system=False):
This makes it impossible to compile the project in any other directory layout and also
prevents the subproject from changing its own directory layout.
Instead of poking directly at the internals the subproject should be executed and
Instead of poking directly at the internals the subproject should be executed and
it should set a variable that the caller can then use. Something like:
# In subproject
Expand All @@ -2461,7 +2462,7 @@ def build_incdir_object(self, incdir_strings, is_system=False):
some_dep = depencency('some')
executable(..., dependencies: [some_dep])
This warning will become a hard error in a future Meson release.
This warning will become a hard error in a future Meson release.
''')
absdir_src = os.path.join(absbase_src, a)
absdir_build = os.path.join(absbase_build, a)
Expand Down
28 changes: 17 additions & 11 deletions mesonbuild/interpreter/mesonmain.py
Expand Up @@ -280,24 +280,30 @@ def install_dependency_manifest_method(self, args, kwargs):
self.build.dep_manifest_name = args[0]

@FeatureNew('meson.override_find_program', '0.46.0')
@permittedKwargs({})
def override_find_program_method(self, args, kwargs):
if len(args) != 2:
raise InterpreterException('Override needs two arguments')
name, exe = args
if not isinstance(name, str):
raise InterpreterException('First argument must be a string')
exe = unholder(exe)
@FeatureNewKwargs('meson.override_find_programs', '0.58.0', ['native'])
@permittedKwargs({'native'})
@typed_pos_args('meson.override_find_programs', str, (ExecutableHolder, mesonlib.File, ExternalProgramHolder))
def override_find_program_method(self, args: T.Tuple[str, T.Union[ExecutableHolder, mesonlib.File, ExternalProgramHolder]], kwargs: T.Dict[str, T.Any]):
name = args[0]
exe: T.Union[build.Executable, mesonlib.File, ExternalProgram] = unholder(args[1])

# We need native when using a file, and someone might want to override
# something for a specific machine, otherwise use the for_machien from
# the program itself.
if isinstance(exe, mesonlib.File) or 'native' in kwargs:
for_machine = self.interpreter.machine_from_native_kwarg(kwargs)
else:
for_machine = exe.for_machine

if isinstance(exe, mesonlib.File):
abspath = exe.absolute_path(self.interpreter.environment.source_dir,
self.interpreter.environment.build_dir)
if not os.path.exists(abspath):
raise InterpreterException('Tried to override %s with a file that does not exist.' % name)
for for_machine in MachineChoice:
self.interpreter.add_find_program_override(name, OverrideProgram(name, abspath, for_machine=for_machine))
exe = OverrideProgram(name, abspath, for_machine=for_machine)
if not isinstance(exe, (ExternalProgram, build.Executable)):
raise InterpreterException('Second argument must be an external program or executable.')
self.interpreter.add_find_program_override(name, exe)
self.interpreter.add_find_program_override(name, exe, for_machine)

@FeatureNew('meson.override_dependency', '0.54.0')
@permittedKwargs({'native'})
Expand Down
3 changes: 3 additions & 0 deletions test cases/native/9 override with exe/meson.build
Expand Up @@ -9,6 +9,9 @@ prog = find_program('foobar', version : '>= 1.0', native : true)
if meson.is_cross_build()
prog_for_host = find_program('foobar', version : '>= 1.0', native : false, required : false)
assert(not prog_for_host.found(), 'got build machine binary for host machine!')

prog_forced = find_program('foobar2', native : false)
assert(prog_forced.found())
else
prog_for_host = find_program('foobar', version : '>= 1.0', native : false, required : false)
assert(prog_for_host.found(), 'host and build are the same, should have been found')
Expand Down
@@ -1,3 +1,6 @@
project('sub', 'c', version : '1.0')
foobar = executable('foobar', 'foobar.c', native : true)
meson.override_find_program('foobar', foobar)

# Force this to be used for the host machine.
meson.override_find_program('foobar2', foobar, native : false)

0 comments on commit e9e62fe

Please sign in to comment.