diff options
| author | Dylan Baker <dylan@pnwbakers.com> | 2025-06-30 13:45:57 -0700 |
|---|---|---|
| committer | Dylan Baker <dylan@pnwbakers.com> | 2025-12-05 08:13:08 -0800 |
| commit | f6918fe65c8b9526bc359bea8a6e8517b39b4ec5 (patch) | |
| tree | c8be5e054ecfa81dbcbe0350cab03e49328233b5 | |
| parent | 79854e4b8d09744acbe2f2d2c0d43a88b9ab1dcc (diff) | |
| download | meson-f6918fe65c8b9526bc359bea8a6e8517b39b4ec5.tar.gz | |
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.
| -rw-r--r-- | docs/markdown/snippets/getting-language-from-other-project.md | 6 | ||||
| -rw-r--r-- | mesonbuild/interpreter/mesonmain.py | 16 |
2 files changed, 17 insertions, 5 deletions
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 |
