From f6918fe65c8b9526bc359bea8a6e8517b39b4ec5 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 30 Jun 2025 13:45:57 -0700 Subject: mesonmain: mark getting a language from another subproject as broken Currently, you can call `meson.get_compiler('c')`, if you haven't initialized 'c' for your project, but a super-project has initialized it. This happens because we check the wrong set of compilers (the global list vs the per-subproject one). Because of how fragile this is, we can mark it as broken an move on. --- .../snippets/getting-language-from-other-project.md | 6 ++++++ mesonbuild/interpreter/mesonmain.py | 16 +++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 docs/markdown/snippets/getting-language-from-other-project.md diff --git a/docs/markdown/snippets/getting-language-from-other-project.md b/docs/markdown/snippets/getting-language-from-other-project.md new file mode 100644 index 000000000..79f19baee --- /dev/null +++ b/docs/markdown/snippets/getting-language-from-other-project.md @@ -0,0 +1,6 @@ +## Using `meson.get_compiler()` to get a language from another project is marked broken + +Meson currently will return a compiler instance from the `meson.get_compiler()` +call, if that language has been initialized in any project. This can result in +situations where a project can only work as a subproject, or if a dependency is +provided by a subproject rather than by a pre-built dependency. diff --git a/mesonbuild/interpreter/mesonmain.py b/mesonbuild/interpreter/mesonmain.py index 067d5ffb3..7c3789f65 100644 --- a/mesonbuild/interpreter/mesonmain.py +++ b/mesonbuild/interpreter/mesonmain.py @@ -16,7 +16,7 @@ from ..mesonlib import MachineChoice from ..options import OptionKey from ..programs import OverrideProgram, ExternalProgram from ..interpreter.type_checking import ENV_KW, ENV_METHOD_KW, ENV_SEPARATOR_KW, env_convertor_with_method -from ..interpreterbase import (MesonInterpreterObject, FeatureNew, FeatureDeprecated, +from ..interpreterbase import (MesonInterpreterObject, FeatureNew, FeatureDeprecated, FeatureBroken, typed_pos_args, noArgsFlattening, noPosargs, noKwargs, typed_kwargs, KwargInfo, InterpreterException, InterpreterObject) from .primitives import MesonVersionString @@ -285,13 +285,19 @@ class MesonMain(MesonInterpreterObject): @typed_kwargs('meson.get_compiler', NATIVE_KW) @InterpreterObject.method('get_compiler') def get_compiler_method(self, args: T.Tuple[str], kwargs: 'NativeKW') -> 'Compiler': - cname = args[0] + lang = args[0] for_machine = kwargs['native'] - clist = self.interpreter.coredata.compilers[for_machine] try: - return clist[cname] + return self.interpreter.compilers[for_machine][lang] except KeyError: - raise InterpreterException(f'Tried to access compiler for language "{cname}", not specified for {for_machine.get_lower_case_name()} machine.') + try: + comp = self.interpreter.coredata.compilers[for_machine][lang] + except KeyError: + raise InterpreterException(f'Tried to access compiler for language "{lang}", not specified for {for_machine.get_lower_case_name()} machine.') + + FeatureBroken.single_use('Using `meson.get_compiler()` for languages only initialized in another subproject', '1.11.0', self.subproject, + 'This is extremely fragile, as your project likely cannot be used outside of your environment.') + return comp @noPosargs @noKwargs -- cgit v1.2.3