From 7a159ff1e1e947f20a017bbc8e89c1701a9d7098 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Wed, 8 May 2019 17:22:56 +0100 Subject: [PATCH] Add add_languages(native:) v2: Retain not using logical-and, to avoid short-circuiting side-effects of add_languages() --- docs/markdown/Reference-manual.md | 24 ++++++++++++------- .../snippets/native_compiler_not_required.md | 5 ++++ mesonbuild/interpreter.py | 21 +++++++++++----- test cases/common/85 add language/meson.build | 3 +++ 4 files changed, 38 insertions(+), 15 deletions(-) create mode 100644 docs/markdown/snippets/native_compiler_not_required.md diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md index 510d44323..3b07b5f77 100644 --- a/docs/markdown/Reference-manual.md +++ b/docs/markdown/Reference-manual.md @@ -54,9 +54,9 @@ Like `add_global_arguments` but the arguments are passed to the linker. bool add_languages(*langs*) ``` -Add support for new programming languages. Equivalent to having them -in the `project` declaration. This function is usually used to add -languages that are only used on some platforms like this: +Add programming languages used by the project. Equivalent to having them in the +`project` declaration. This function is usually used to add languages that are +only used under some conditions, like this: ```meson project('foobar', 'c') @@ -68,12 +68,18 @@ if add_languages('cpp', required : false) endif ``` -Takes one keyword argument, `required`. It defaults to `true`, which -means that if any of the languages specified is not found, Meson will -halt. Returns true if all languages specified were found and false -otherwise. Since *0.47.0* the value of a -[`feature`](Build-options.md#features) option can also be passed to -the `required` keyword argument. +Takes the following keyword arguments: + +- `required` defaults to `true`, which means that if any of the languages +specified is not found, Meson will halt. Since *0.47.0* the value of a +[`feature`](Build-options.md#features) option can also be passed. + +- `native` if set to `true`, the language will be used to compile for the build + machine, if `false`, for the host machine. If omitted, the language may be + used for either. Since *0.54.0*. The default may change to `false` in a future + meson version. + +Returns `true` if all languages specified were found and `false` otherwise. ### add_project_arguments() diff --git a/docs/markdown/snippets/native_compiler_not_required.md b/docs/markdown/snippets/native_compiler_not_required.md new file mode 100644 index 000000000..0847b2bd7 --- /dev/null +++ b/docs/markdown/snippets/native_compiler_not_required.md @@ -0,0 +1,5 @@ +## Native (build machine) compilers not always required + +`add_languages()` gained a `native:` keyword, indicating if a native or cross +compiler is to be used. Currently, for backwards compatibility, if the keyword +is absent, that means both are used. diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index d824e3c3a..0d8347282 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -2080,7 +2080,7 @@ _base_test_args = {'args', 'depends', 'env', 'should_fail', 'timeout', 'workdir' permitted_kwargs = {'add_global_arguments': {'language', 'native'}, 'add_global_link_arguments': {'language', 'native'}, - 'add_languages': {'required'}, + 'add_languages': {'required', 'native'}, 'add_project_link_arguments': {'language', 'native'}, 'add_project_arguments': {'language', 'native'}, 'add_test_setup': {'exe_wrapper', 'gdb', 'timeout_multiplier', 'env', 'is_default'}, @@ -2908,11 +2908,13 @@ external dependencies (including libraries) must go to "dependencies".''') self.build.projects[self.subproject] = proj_name mlog.log('Project name:', mlog.bold(proj_name)) mlog.log('Project version:', mlog.bold(self.project_version)) - self.add_languages(proj_langs, True) + self.add_languages(proj_langs, True, MachineChoice.BUILD) + self.add_languages(proj_langs, True, MachineChoice.HOST) self.set_backend() if not self.is_subproject(): self.check_stdlibs() + @FeatureNewKwargs('add_languages', '0.54.0', ['native']) @permittedKwargs(permitted_kwargs['add_languages']) @stringArgs def func_add_languages(self, node, args, kwargs): @@ -2921,7 +2923,15 @@ external dependencies (including libraries) must go to "dependencies".''') for lang in sorted(args, key=compilers.sort_clink): mlog.log('Compiler for language', mlog.bold(lang), 'skipped: feature', mlog.bold(feature), 'disabled') return False - return self.add_languages(args, required) + if 'native' in kwargs: + return self.add_languages(args, required, self.machine_from_native_kwarg(kwargs)) + else: + # absent 'native' means 'both' for backwards compatibility + mlog.warning('add_languages is missing native:, assuming languages are required for both host and build.', + location=self.current_node) + success = self.add_languages(args, required, MachineChoice.BUILD) + success &= self.add_languages(args, required, MachineChoice.HOST) + return success def get_message_string_arg(self, arg): if isinstance(arg, list): @@ -3009,9 +3019,8 @@ external dependencies (including libraries) must go to "dependencies".''') self.validate_arguments(args, 0, []) raise Exception() - def add_languages(self, args: T.Sequence[str], required: bool) -> bool: - success = self.add_languages_for(args, required, MachineChoice.BUILD) - success &= self.add_languages_for(args, required, MachineChoice.HOST) + def add_languages(self, args: T.Sequence[str], required: bool, for_machine: MachineChoice) -> bool: + success = self.add_languages_for(args, required, for_machine) if not self.coredata.is_cross_build(): self.coredata.copy_build_options_from_regular_ones() return success diff --git a/test cases/common/85 add language/meson.build b/test cases/common/85 add language/meson.build index d9bc0fa2b..aa4972abd 100644 --- a/test cases/common/85 add language/meson.build +++ b/test cases/common/85 add language/meson.build @@ -6,3 +6,6 @@ assert(add_languages('cpp'), 'Add_languages returned false on success') assert(not add_languages('klingon', required : false), 'Add_languages returned true on failure.') test('C++', executable('cppprog', 'prog.cc')) + +add_languages('c', native: true) +add_languages('c', native: false)