From 9074c7f8a43e6a50ff255a9974adc20887a3001f Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 12 Mar 2020 10:55:58 -0700 Subject: envconfig: Make compiler and linker environment variables match --- docs/markdown/howtox.md | 6 +++ .../linker_environment_variables_match_docs.md | 7 ++++ mesonbuild/envconfig.py | 30 ++++++++++---- run_unittests.py | 46 ++++++++++++++-------- 4 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 docs/markdown/snippets/linker_environment_variables_match_docs.md diff --git a/docs/markdown/howtox.md b/docs/markdown/howtox.md index f70ff47..1a2add0 100644 --- a/docs/markdown/howtox.md +++ b/docs/markdown/howtox.md @@ -26,6 +26,9 @@ build. Because of this Meson needs to know both the native and the cross compiler. The former is set via the environment variables or native-files and the latter via the cross file only. +There is a table of all environment variables supported [Here](Reference-tables.md#compiler-and-linker-selection-variables) + + ## Set dynamic linker *New in 0.53.0* @@ -61,6 +64,9 @@ c = 'clang' c_ld = 'lld' ``` +There is a table of all environment variables supported [Here](Reference-tables.md#compiler-and-linker-selection-variables) + + ## Set default C/C++ language version ```meson diff --git a/docs/markdown/snippets/linker_environment_variables_match_docs.md b/docs/markdown/snippets/linker_environment_variables_match_docs.md new file mode 100644 index 0000000..233547c --- /dev/null +++ b/docs/markdown/snippets/linker_environment_variables_match_docs.md @@ -0,0 +1,7 @@ +## Dynamic Linker environment variables actually match docs + +The docs have always claimed that the Dynamic Linker environment variable +should be `${COMPILER_VAR}_LD`, but that's only the case for about half of +the variables. The other half are different. In 0.54.0 the variables match. +The old variables are still supported, but are deprecated and raise a +deprecation warning. diff --git a/mesonbuild/envconfig.py b/mesonbuild/envconfig.py index 3e5e44b..8587842 100644 --- a/mesonbuild/envconfig.py +++ b/mesonbuild/envconfig.py @@ -317,11 +317,11 @@ class BinaryTable(HasEnvVarFallback): # Linkers 'c_ld': 'CC_LD', 'cpp_ld': 'CXX_LD', - 'd_ld': 'D_LD', - 'fortran_ld': 'F_LD', + 'd_ld': 'DC_LD', + 'fortran_ld': 'FC_LD', 'objc_ld': 'OBJC_LD', - 'objcpp_ld': 'OBJCPP_LD', - 'rust_ld': 'RUST_LD', + 'objcpp_ld': 'OBJCXX_LD', + 'rust_ld': 'RUSTC_LD', # Binutils 'strip': 'STRIP', @@ -334,6 +334,15 @@ class BinaryTable(HasEnvVarFallback): 'pkgconfig': 'PKG_CONFIG', } # type: T.Dict[str, str] + # Deprecated environment variables mapped from the new variable to the old one + # Deprecated in 0.54.0 + DEPRECATION_MAP = { + 'DC_LD': 'D_LD', + 'FC_LD': 'F_LD', + 'RUSTC_LD': 'RUST_LD', + 'OBJCXX_LD': 'OBJCPP_LD', + } # type: T.Dict[str, str] + @staticmethod def detect_ccache() -> T.List[str]: try: @@ -362,12 +371,10 @@ This is probably wrong, it should always point to the native compiler.''' % evar return compiler, ccache def lookup_entry(self, name: str) -> T.Optional[T.List[str]]: - """Lookup binaryk + """Lookup binary in cross/native file and fallback to environment. Returns command with args as list if found, Returns `None` if nothing is found. - - First tries looking in explicit map, then tries environment variable. """ # Try explicit map, don't fall back on env var command = self.binaries.get(name) @@ -380,9 +387,18 @@ This is probably wrong, it should always point to the native compiler.''' % evar # Relies on there being no "" env var evar = self.evarMap.get(name, "") command = os.environ.get(evar) + if command is None: + deprecated = self.DEPRECATION_MAP.get(evar) + if deprecated: + command = os.environ.get(deprecated) + if command: + mlog.deprecation( + 'The', deprecated, 'environment variable is deprecated in favor of', + evar, once=True) if command is not None: command = split_args(command) + # Do not return empty or blank string entries if command is not None and (len(command) == 0 or len(command[0].strip()) == 0): return None diff --git a/run_unittests.py b/run_unittests.py index ee6ec7e..2f7ad43 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -4972,14 +4972,21 @@ class WindowsTests(BasePlatformTests): def _check_ld(self, name: str, lang: str, expected: str) -> None: if not shutil.which(name): raise unittest.SkipTest('Could not find {}.'.format(name)) - envvar = mesonbuild.envconfig.BinaryTable.evarMap['{}_ld'.format(lang)] - with mock.patch.dict(os.environ, {envvar: name}): - env = get_fake_env() - try: - comp = getattr(env, 'detect_{}_compiler'.format(lang))(MachineChoice.HOST) - except EnvironmentException: - raise unittest.SkipTest('Could not find a compiler for {}'.format(lang)) - self.assertEqual(comp.linker.id, expected) + envvars = [mesonbuild.envconfig.BinaryTable.evarMap['{}_ld'.format(lang)]] + + # Also test a deprecated variable if there is one. + if envvars[0] in mesonbuild.envconfig.BinaryTable.DEPRECATION_MAP: + envvars.append( + mesonbuild.envconfig.BinaryTable.DEPRECATION_MAP[envvars[0]]) + + for envvar in envvars: + with mock.patch.dict(os.environ, {envvar: name}): + env = get_fake_env() + try: + comp = getattr(env, 'detect_{}_compiler'.format(lang))(MachineChoice.HOST) + except EnvironmentException: + raise unittest.SkipTest('Could not find a compiler for {}'.format(lang)) + self.assertEqual(comp.linker.id, expected) def test_link_environment_variable_lld_link(self): self._check_ld('lld-link', 'c', 'lld-link') @@ -6333,14 +6340,21 @@ c = ['{0}'] raise unittest.SkipTest('Solaris currently cannot override the linker.') if not shutil.which(check): raise unittest.SkipTest('Could not find {}.'.format(check)) - envvar = mesonbuild.envconfig.BinaryTable.evarMap['{}_ld'.format(lang)] - with mock.patch.dict(os.environ, {envvar: name}): - env = get_fake_env() - comp = getattr(env, 'detect_{}_compiler'.format(lang))(MachineChoice.HOST) - if lang != 'rust' and comp.use_linker_args('foo') == []: - raise unittest.SkipTest( - 'Compiler {} does not support using alternative linkers'.format(comp.id)) - self.assertEqual(comp.linker.id, expected) + envvars = [mesonbuild.envconfig.BinaryTable.evarMap['{}_ld'.format(lang)]] + + # Also test a deprecated variable if there is one. + if envvars[0] in mesonbuild.envconfig.BinaryTable.DEPRECATION_MAP: + envvars.append( + mesonbuild.envconfig.BinaryTable.DEPRECATION_MAP[envvars[0]]) + + for envvar in envvars: + with mock.patch.dict(os.environ, {envvar: name}): + env = get_fake_env() + comp = getattr(env, 'detect_{}_compiler'.format(lang))(MachineChoice.HOST) + if lang != 'rust' and comp.use_linker_args('bfd') == []: + raise unittest.SkipTest( + 'Compiler {} does not support using alternative linkers'.format(comp.id)) + self.assertEqual(comp.linker.id, expected) def test_ld_environment_variable_bfd(self): self._check_ld('ld.bfd', 'bfd', 'c', 'ld.bfd') -- cgit v1.1 From 6a5fdbf995c46e17b9e1bbd0516772b35e2786ac Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 12 Mar 2020 11:13:17 -0700 Subject: docs: Add Environment variables for compiler/linker selection We really should be documenting these in an easy to find and reference place. --- docs/markdown/Reference-tables.md | 19 +++++++++++++++++++ test cases/java/1 basic/meson.build | 4 ++++ 2 files changed, 23 insertions(+) diff --git a/docs/markdown/Reference-tables.md b/docs/markdown/Reference-tables.md index 59e252f..8c55180 100644 --- a/docs/markdown/Reference-tables.md +++ b/docs/markdown/Reference-tables.md @@ -255,3 +255,22 @@ These are the values that can be passed to `dependency` function's | config-tool | Use a custom dep tool such as `cups-config` | | system | System provided (e.g. OpenGL) | | extraframework | A macOS/iOS framework | + + +## Compiler and Linker selection variables + +| Language | Compiler | Linker | Note | +|:-------------:|----------|-----------|---------------------------------------------| +| C | CC | CC_LD | | +| C++ | CXX | CXX_LD | | +| D | DC | DC_LD | Before 0.54 D_LD* | +| Fortran | FC | FC_LD | Before 0.54 F_LD* | +| Objective-C | OBJC | OBJC_LD | | +| Objective-C++ | OBJCXX | OBJCXX_LD | Before 0.54 OBJCPP_LD* | +| Rust | RUSTC | RUSTC_LD | Before 0.54 RUST_LD* | +| Vala | VALAC | | Use CC_LD. Vala transpiles to C | +| C# | CSC | CSC | The linker is the compiler | + +*The old environment variales are still supported, but are deprecated and will +be removed in a future version of meson. + diff --git a/test cases/java/1 basic/meson.build b/test cases/java/1 basic/meson.build index 201a609..ef1a4b7 100644 --- a/test cases/java/1 basic/meson.build +++ b/test cases/java/1 basic/meson.build @@ -5,3 +5,7 @@ javaprog = jar('myprog', 'com/mesonbuild/Simple.java', install : true, install_dir : get_option('bindir')) test('mytest', javaprog) + +jc = meson.get_compiler('java') +message(jc.get_id()) +message(jc.get_linker_id()) -- cgit v1.1 From b8294b4436f7a187cd8cfae02554ce0140428e78 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Thu, 12 Mar 2020 13:17:36 -0700 Subject: compilers: Error if invalid linker selected --- mesonbuild/compilers/mixins/gnu.py | 4 ++++ mesonbuild/compilers/mixins/visualstudio.py | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mesonbuild/compilers/mixins/gnu.py b/mesonbuild/compilers/mixins/gnu.py index ae4b7db..4c8ca0b 100644 --- a/mesonbuild/compilers/mixins/gnu.py +++ b/mesonbuild/compilers/mixins/gnu.py @@ -304,6 +304,10 @@ class GnuLikeCompiler(metaclass=abc.ABCMeta): @classmethod def use_linker_args(cls, linker: str) -> T.List[str]: + if linker not in {'gold', 'bfd', 'lld'}: + raise mesonlib.MesonException( + 'Unsupported linker, only bfd, gold, and lld are supported, ' + 'not {}.'.format(linker)) return ['-fuse-ld={}'.format(linker)] diff --git a/mesonbuild/compilers/mixins/visualstudio.py b/mesonbuild/compilers/mixins/visualstudio.py index 44aefc8..d0004ce 100644 --- a/mesonbuild/compilers/mixins/visualstudio.py +++ b/mesonbuild/compilers/mixins/visualstudio.py @@ -366,10 +366,6 @@ class VisualStudioLikeCompiler(metaclass=abc.ABCMeta): def get_argument_syntax(self) -> str: return 'msvc' - @classmethod - def use_linker_args(cls, linker: str) -> T.List[str]: - return [] - class MSVCCompiler(VisualStudioLikeCompiler): -- cgit v1.1