Skip to content

Commit

Permalink
interpreter: extend add_*_arguments to support native : 'both'
Browse files Browse the repository at this point in the history
This allows for arguments that are needed for both machines to be
passed to both at the same time. This can be helpful if when cross
compiling, when something like a `-D` option needs to be the same for
the host and the build targets.
  • Loading branch information
dcbaker committed Nov 30, 2023
1 parent 54bb41e commit ee83941
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 34 deletions.
4 changes: 4 additions & 0 deletions docs/markdown/snippets/add_native_both.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ This has been added to the `add_language()` function. This allows explicitly
setting the default behavior of getting a language for both the host and build
machines. The default has been changed to `'both'`, and Meson will no longer
warn about getting no value for `native` in this function as a result.

This as also been added to `add_global_args()`, `add_global_link_args()`,
`add_project_args()`, and `add_project_link_args()`. The default for these
remains the same, but `'both'` is now allowed
14 changes: 7 additions & 7 deletions docs/yaml/functions/add_global_arguments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ kwargs:
it in per-target flags.
native:
type: bool
type: bool | str
default: false
since: 0.48.0
description: |
A boolean specifying whether the arguments should be
applied to the native or cross compilation. If `true` the arguments
will only be used for native compilations. If `false` the arguments
will only be used in cross compilations. If omitted, the flags are
added to native compilations if compiling natively and cross
compilations (only) when cross compiling.
A boolean or `'both'` specifying whether the arguments should be
applied to targets for the host machine, the build machine, or both. If
`true` the arguments will only be used for build targets. If `false`
the arguments will only be used for host targets. *(since 1.4.0)* If
`'both'` then arguments will be used for both host and build targets.
When host == build, all options are equivalent.
38 changes: 29 additions & 9 deletions mesonbuild/interpreter/interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2867,29 +2867,49 @@ def func_add_test_setup(self, node: mparser.BaseNode, args: T.Tuple[str], kwargs
kwargs['exclude_suites'])

@typed_pos_args('add_global_arguments', varargs=str)
@typed_kwargs('add_global_arguments', NATIVE_KW, LANGUAGE_KW)
@typed_kwargs('add_global_arguments', NATIVE_BOTH_KW, LANGUAGE_KW)
def func_add_global_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwtypes.FuncAddProjectArgs') -> None:
self._add_global_arguments(node, self.build.global_args[kwargs['native']], args[0], kwargs)
native = kwargs['native']
if native is InterpreterMachineChoice.BOTH:
self._add_global_arguments(node, self.build.global_args[MachineChoice.HOST], args[0], kwargs)
self._add_global_arguments(node, self.build.global_args[MachineChoice.BUILD], args[0], kwargs)
else:
self._add_global_arguments(node, self.build.global_args[native.as_machinechoice()], args[0], kwargs)

@typed_pos_args('add_global_link_arguments', varargs=str)
@typed_kwargs('add_global_arguments', NATIVE_KW, LANGUAGE_KW)
@typed_kwargs('add_global_arguments', NATIVE_BOTH_KW, LANGUAGE_KW)
def func_add_global_link_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwtypes.FuncAddProjectArgs') -> None:
self._add_global_arguments(node, self.build.global_link_args[kwargs['native']], args[0], kwargs)
native = kwargs['native']
if native is InterpreterMachineChoice.BOTH:
self._add_global_arguments(node, self.build.global_link_args[MachineChoice.HOST], args[0], kwargs)
self._add_global_arguments(node, self.build.global_link_args[MachineChoice.BUILD], args[0], kwargs)
else:
self._add_global_arguments(node, self.build.global_link_args[native.as_machinechoice()], args[0], kwargs)

@typed_pos_args('add_project_arguments', varargs=str)
@typed_kwargs('add_project_arguments', NATIVE_KW, LANGUAGE_KW)
@typed_kwargs('add_project_arguments', NATIVE_BOTH_KW, LANGUAGE_KW)
def func_add_project_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwtypes.FuncAddProjectArgs') -> None:
self._add_project_arguments(node, self.build.projects_args[kwargs['native']], args[0], kwargs)
native = kwargs['native']
if native is InterpreterMachineChoice.BOTH:
self._add_project_arguments(node, self.build.projects_args[MachineChoice.BUILD], args[0], kwargs)
self._add_project_arguments(node, self.build.projects_args[MachineChoice.HOST], args[0], kwargs)
else:
self._add_project_arguments(node, self.build.projects_args[native.as_machinechoice()], args[0], kwargs)

@typed_pos_args('add_project_link_arguments', varargs=str)
@typed_kwargs('add_global_arguments', NATIVE_KW, LANGUAGE_KW)
@typed_kwargs('add_global_arguments', NATIVE_BOTH_KW, LANGUAGE_KW)
def func_add_project_link_arguments(self, node: mparser.FunctionNode, args: T.Tuple[T.List[str]], kwargs: 'kwtypes.FuncAddProjectArgs') -> None:
self._add_project_arguments(node, self.build.projects_link_args[kwargs['native']], args[0], kwargs)
native = kwargs['native']
if native is InterpreterMachineChoice.BOTH:
self._add_project_arguments(node, self.build.projects_link_args[MachineChoice.BUILD], args[0], kwargs)
self._add_project_arguments(node, self.build.projects_link_args[MachineChoice.HOST], args[0], kwargs)
else:
self._add_project_arguments(node, self.build.projects_link_args[native.as_machinechoice()], args[0], kwargs)

@FeatureNew('add_project_dependencies', '0.63.0')
@typed_pos_args('add_project_dependencies', varargs=dependencies.Dependency)
@typed_kwargs('add_project_dependencies', NATIVE_KW, LANGUAGE_KW)
def func_add_project_dependencies(self, node: mparser.FunctionNode, args: T.Tuple[T.List[dependencies.Dependency]], kwargs: 'kwtypes.FuncAddProjectArgs') -> None:
def func_add_project_dependencies(self, node: mparser.FunctionNode, args: T.Tuple[T.List[dependencies.Dependency]], kwargs: kwtypes.FuncAddProjectDeps) -> None:
for_machine = kwargs['native']
for lang in kwargs['language']:
if lang not in self.compilers[for_machine]:
Expand Down
6 changes: 6 additions & 0 deletions mesonbuild/interpreter/kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class FuncAddProjectArgs(TypedDict):
a MachineChoice instance already.
"""

native: InterpreterMachineChoice
language: T.List[str]


class FuncAddProjectDeps(TypedDict):

native: MachineChoice
language: T.List[str]

Expand Down
46 changes: 38 additions & 8 deletions test cases/common/115 subproject project arguments/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,46 @@ project('project options tester', 'c', 'cpp',
version : '2.3.4',
license : 'mylicense')

add_global_arguments('-DGLOBAL_ARGUMENT', language: 'c')
add_project_arguments('-DPROJECT_OPTION', language: 'c')
add_project_arguments('-DPROJECT_OPTION_CPP', language: 'cpp')
add_project_arguments('-DPROJECT_OPTION_C_CPP', language: ['c', 'cpp'])
machine = get_option('machine')
assert(machine in ['host', 'build', 'both'])
if machine == 'host'
native = false
elif machine == 'build'
native = true
else
native = 'both'
endif

sub = subproject('subexe', version : '1.0.0')
if not add_languages('c', 'cpp', native : native, required : false)
error(f'MESON_SKIP_TEST this test requires compilers for @machine@ machine(s)')
endif

add_project_arguments('-DPROJECT_OPTION_1', language: 'c')
add_global_arguments('-DGLOBAL_ARGUMENT', language: 'c', native : native)
add_project_arguments('-DPROJECT_OPTION', language: 'c', native : native)
add_project_arguments('-DPROJECT_OPTION_CPP', language: 'cpp', native : native)
add_project_arguments('-DPROJECT_OPTION_C_CPP', language: ['c', 'cpp'], native : native)

e = executable('exe', 'exe.c')
e = executable('execpp', 'exe.cpp')
# XXX: when subprojects allow host/build
if machine != 'build'
sub = subproject('subexe', version : '1.0.0')
endif

add_project_arguments('-DPROJECT_OPTION_1', language: 'c', native : native)

if machine == 'both'
native = false
endif

e = executable('exe', 'exe.c', native : native)
e = executable('execpp', 'exe.cpp', native : native)
test('exetest', e)
test('execpptest', e)

if machine == 'both'
e = executable('exe_build', 'exe.c', native : true)
e = executable('execpp_build', 'exe.cpp', native : true)
if meson.can_run_host_binaries()
test('exetest_build', e)
test('execpptest_build', e)
endif
endif
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('machine', type : 'string', value : 'host')
11 changes: 11 additions & 0 deletions test cases/common/115 subproject project arguments/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"matrix": {
"options": {
"machine": [
{ "val": "host" },
{ "val": "build" },
{ "val": "both" }
]
}
}
}
3 changes: 0 additions & 3 deletions test cases/warning/2 languages missing native/meson.build

This file was deleted.

7 changes: 0 additions & 7 deletions test cases/warning/2 languages missing native/test.json

This file was deleted.

8 changes: 8 additions & 0 deletions unittests/allplatformstests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4931,3 +4931,11 @@ def test_c_cpp_stds(self):
# The first supported std should be selected
self.setconf('-Dcpp_std=c++11,gnu++11,vc++11')
self.assertEqual(self.getconf('cpp_std'), 'c++11')

def test_add_arguments_both_no_dups(self) -> None:
testdir = os.path.join(self.common_test_dir, '115 subproject project arguments')
self.init(testdir, extra_args=['-Dmachine=both'])
compdb = self.get_compdb()
# It really doesn't matter what target we look at
self.assertEqual(compdb[0]['command'].count('-DGLOBAL_ARGUMENT'), 1,
msg="`native : both` inserted arguments twice in the same target")

0 comments on commit ee83941

Please sign in to comment.