Skip to content

Commit

Permalink
extend override_find_program with native keyword
Browse files Browse the repository at this point in the history
For the most part, what we had before would work correctly for cross
building. There is, however a case where this might not work correctly,
which is a project for the host machine that builds a build machine
target and uses that as an override (think some kind of transpiler), to
make this work correctly we need to add a native keyword argument to the
`override_find_program` method.
  • Loading branch information
dcbaker committed Sep 22, 2023
1 parent 14e4324 commit 5eb9df4
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 9 deletions.
13 changes: 13 additions & 0 deletions docs/markdown/snippets/subproject_and_override_program_native.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## subproject() and meson.override_find_program() now support the native keyword argument

Subprojects may now be built for the host or build machine (or both). This means
that build time dependencies can be built for the matching running the build in a
cross compile setup. When a subproject is run for the build machine it will act
just like a normal build == host setup, except that no targets will be installed.

This necessarily means that `meson.override_find_program()` must differentiate
between programs for the host and those for the build machine, as you may need
two versions of the same program, which have different outputs based on the
machine they are for. For backwards compatibility reasons the default is for the
host machine. Inside a native subproject the host and build machine will both be
the build machine.
11 changes: 10 additions & 1 deletion docs/yaml/builtins/meson.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ methods:
to install only a subset of the files.
By default the script has no install tag which means it is not being run when
`meson install --tags` argument is specified.
dry_run:
type: bool
since: 1.1.0
Expand Down Expand Up @@ -381,6 +381,15 @@ methods:
type: exe | file | external_program
description: The program to set as the override for `progname`.

native:
type: bool
since: 1.3.0
default: false
description : |
If set to `true`, the override is for the build machine, and will be returned
by `find_program(..., native : true)`, otherwise for the host machine. This
is an important distinction when cross compiling.
- name: override_dependency
returns: void
since: 0.54.0
Expand Down
7 changes: 7 additions & 0 deletions docs/yaml/functions/subproject.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,10 @@ kwargs:
default: true
description: |
Works just the same as in [[dependency]].
native:
type: bool
since: 1.3.0
default: false
description : |
Whether to build this subproject for the host or build machine.
5 changes: 2 additions & 3 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1628,9 +1628,8 @@ def store_name_lookups(self, command_names: T.List[mesonlib.FileOrString], for_m
if isinstance(name, str):
self.build.searched_programs[for_machine].add(name)

def add_find_program_override(self, name: str, exe: T.Union[build.Executable, ExternalProgram, 'OverrideProgram']) -> None:
# XXX: What to do here? We probably need to add an explicit native here...
for_machine = MachineChoice.HOST
def add_find_program_override(self, name: str, exe: T.Union[build.Executable, ExternalProgram, 'OverrideProgram'],
for_machine: MachineChoice = MachineChoice.HOST) -> None:
if name in self.build.searched_programs[for_machine]:
raise InterpreterException(f'Tried to override finding of executable "{name}" which has already been found.')
if name in self.build.find_overrides[for_machine]:
Expand Down
6 changes: 3 additions & 3 deletions mesonbuild/interpreter/mesonmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,16 @@ def install_dependency_manifest_method(self, args: T.Tuple[str], kwargs: 'TYPE_k

@FeatureNew('meson.override_find_program', '0.46.0')
@typed_pos_args('meson.override_find_program', str, (mesonlib.File, ExternalProgram, build.Executable))
@noKwargs
def override_find_program_method(self, args: T.Tuple[str, T.Union[mesonlib.File, ExternalProgram, build.Executable]], kwargs: 'TYPE_kwargs') -> None:
@typed_kwargs('meson.override_find_program', NATIVE_KW.evolve(since='1.3.0'))
def override_find_program_method(self, args: T.Tuple[str, T.Union[mesonlib.File, ExternalProgram, build.Executable]], kwargs: NativeKW) -> None:
name, exe = args
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(f'Tried to override {name} with a file that does not exist.')
exe = OverrideProgram(name, [abspath])
self.interpreter.add_find_program_override(name, exe)
self.interpreter.add_find_program_override(name, exe, kwargs['native'])

@typed_kwargs(
'meson.override_dependency',
Expand Down
8 changes: 8 additions & 0 deletions test cases/native/10 native subproject/maingen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>

int main(void) {
printf("const char * gen_main(void) {\n");
printf(" return \"int main() \";\n");
printf("}\n");
return 0;
}
2 changes: 2 additions & 0 deletions test cases/native/10 native subproject/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ project('native subproject', 'c', meson_version : '>= 1.3.0')
subproject('both', native : true)
subproject('both', native : false)

maingen = executable('maingen', 'maingen.c', native : true)
meson.override_find_program('maingen', maingen, native : true)
gen = subproject('buildtool', native : true).get_variable('e')

ct = custom_target(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <stdio.h>

const char * gen_main(void);

int main() {
printf("int main() { return 0; }\n");
printf("%s", gen_main());
printf("{ return 0; }\n");
return 0;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
project('buildtool', 'c', meson_version : '>= 1.3.0')

e = executable('buildtool', 'buildtool.c')
maingen = find_program('maingen')

ct = custom_target(
'gen',
command : [maingen],
output : 'gen.c',
capture : true,
)

e = executable('buildtool', 'buildtool.c', ct)

meson.override_find_program('buildtool', e)

Expand Down

0 comments on commit 5eb9df4

Please sign in to comment.