From 63ddb8aafa8b26e44115abd1e9d1856348fd6461 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Thu, 22 Jul 2021 12:38:00 +0200 Subject: compilers: fix flake8 issues --- mesonbuild/compilers/compilers.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index ed77df685..630274df0 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -32,13 +32,13 @@ from ..arglist import CompilerArgs if T.TYPE_CHECKING: from ..build import BuildTarget - from ..coredata import OptionDictType, KeyedOptionDictType + from ..coredata import KeyedOptionDictType from ..envconfig import MachineInfo from ..environment import Environment from ..linkers import DynamicLinker, RSPFileSyntax from ..dependencies import Dependency - CompilerType = T.TypeVar('CompilerType', bound=Compiler) + CompilerType = T.TypeVar('CompilerType', bound='Compiler') _T = T.TypeVar('_T') """This file contains the data files of all compilers Meson knows @@ -276,7 +276,7 @@ base_options: 'KeyedOptionDictType' = { OptionKey('b_pch'): coredata.UserBooleanOption('Use precompiled headers', True), OptionKey('b_lto'): coredata.UserBooleanOption('Use link time optimization', False), OptionKey('b_lto'): coredata.UserBooleanOption('Use link time optimization', False), - OptionKey('b_lto_threads'): coredata.UserIntegerOption('Use multiple threads for Link Time Optimization', (None, None,0)), + OptionKey('b_lto_threads'): coredata.UserIntegerOption('Use multiple threads for Link Time Optimization', (None, None, 0)), OptionKey('b_lto_mode'): coredata.UserComboOption('Select between different LTO modes.', ['default', 'thin'], 'default'), @@ -680,8 +680,8 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): raise EnvironmentException('Language %s does not support sizeof checks.' % self.get_display_language()) def alignment(self, typename: str, prefix: str, env: 'Environment', *, - extra_args: T.Optional[T.List[str]] = None, - dependencies: T.Optional[T.List['Dependency']] = None) -> int: + extra_args: T.Optional[T.List[str]] = None, + dependencies: T.Optional[T.List['Dependency']] = None) -> int: raise EnvironmentException('Language %s does not support alignment checks.' % self.get_display_language()) def has_function(self, funcname: str, prefix: str, env: 'Environment', *, @@ -767,7 +767,7 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): no_ccache = False if isinstance(code, str): srcname = os.path.join(tmpdirname, - 'testfile.' + self.default_suffix) + 'testfile.' + self.default_suffix) with open(srcname, 'w', encoding='utf-8') as ofile: ofile.write(code) # ccache would result in a cache miss @@ -1232,7 +1232,6 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): with self._build_wrapper(code, env, extra_args, dependencies, mode, disable_cache=disable_cache) as p: return p.returncode == 0, p.cached - def links(self, code: 'mesonlib.FileOrString', env: 'Environment', *, extra_args: T.Union[None, T.List[str], CompilerArgs, T.Callable[[CompileCheckMode], T.List[str]]] = None, dependencies: T.Optional[T.List['Dependency']] = None, -- cgit v1.2.3 From 2997480ee6eb2e603b092913d50c6de7a5bc813f Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Thu, 22 Jul 2021 12:14:59 +0200 Subject: compilers: do accept None in Compiler.compile extra_args The type information allows it, but it is not actually handled. --- mesonbuild/compilers/compilers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 630274df0..157d2a8c9 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -791,7 +791,8 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): # extra_args must be last because it could contain '/link' to # pass args to VisualStudio's linker. In that case everything # in the command line after '/link' is given to the linker. - commands += extra_args + if extra_args: + commands += extra_args # Generate full command-line with the exelist command_list = self.get_exelist() + commands.to_native() mlog.debug('Running compile:') -- cgit v1.2.3 From 90ee43463f4844b667d26230ef7773f63fcf60dd Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Thu, 22 Jul 2021 12:29:22 +0200 Subject: compilers: allow link tests to use objects from a different compiler In some cases, link tests would like to use objects provided by a compiler for a different language, for example linking a C object file with a C++ compiler. This kind of scenario is what link_language is for, but it is impossible to test that it works with a linker test. This patch implements the required support in the Compiler class. The source code compiler is passed to the Compiler.links method as an argument. --- mesonbuild/compilers/compilers.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 157d2a8c9..3d3e57ec2 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -775,8 +775,11 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): contents = code else: srcname = code.fname - with open(code.fname, encoding='utf-8') as f: - contents = f.read() + if not is_object(code.fname): + with open(code.fname, encoding='utf-8') as f: + contents = f.read() + else: + contents = '' # Construct the compiler command-line commands = self.compiler_args() @@ -1234,10 +1237,17 @@ class Compiler(HoldableObject, metaclass=abc.ABCMeta): return p.returncode == 0, p.cached def links(self, code: 'mesonlib.FileOrString', env: 'Environment', *, + compiler: T.Optional['Compiler'] = None, extra_args: T.Union[None, T.List[str], CompilerArgs, T.Callable[[CompileCheckMode], T.List[str]]] = None, dependencies: T.Optional[T.List['Dependency']] = None, mode: str = 'compile', disable_cache: bool = False) -> T.Tuple[bool, bool]: + if compiler: + with compiler._build_wrapper(code, env, dependencies=dependencies, want_output=True) as r: + objfile = mesonlib.File.from_absolute_file(r.output_name) + return self.compiles(objfile, env, extra_args=extra_args, + dependencies=dependencies, mode='link', disable_cache=True) + return self.compiles(code, env, extra_args=extra_args, dependencies=dependencies, mode='link', disable_cache=disable_cache) -- cgit v1.2.3 From 8596b3bcd12371ad16e5ffbd3e34953603cd1484 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Thu, 22 Jul 2021 12:01:01 +0200 Subject: interpreter: detect and pass compiler to be used for linker tests Allow using the links method to test that the C++ driver (e.g. g++) can be used to link C objects. One usecase is that the C compiler's libsanitizer might not be compatible with the one included by the C++ driver. This is theoretically backwards-incompatible, but it should be treated as a bugfix in my opinion. There is no way in Meson to compile a .c file with the C++ driver as part of a build target, therefore there would be no reason to do something like meson.get_compiler(meson.get_compiler('cpp').links(files('main.c')). Fixes: #7703 --- mesonbuild/compilers/__init__.py | 2 ++ mesonbuild/compilers/compilers.py | 2 ++ mesonbuild/interpreter/compiler.py | 15 +++++++++++++++ mesonbuild/mesonlib/universal.py | 4 ++++ 4 files changed, 23 insertions(+) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/__init__.py b/mesonbuild/compilers/__init__.py index 3d39c9b2f..895f98d6d 100644 --- a/mesonbuild/compilers/__init__.py +++ b/mesonbuild/compilers/__init__.py @@ -34,6 +34,7 @@ __all__ = [ 'is_known_suffix', 'lang_suffixes', 'sort_clink', + 'SUFFIX_TO_LANG', 'compiler_from_language', 'detect_compiler_for', @@ -148,6 +149,7 @@ from .compilers import ( lang_suffixes, LANGUAGES_USING_LDFLAGS, sort_clink, + SUFFIX_TO_LANG, ) from .detect import ( compiler_from_language, diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py index 3d3e57ec2..ea498331c 100644 --- a/mesonbuild/compilers/compilers.py +++ b/mesonbuild/compilers/compilers.py @@ -84,6 +84,8 @@ for _l in clink_langs + ('vala',): clink_suffixes += lang_suffixes[_l] clink_suffixes += ('h', 'll', 's') all_suffixes = set(itertools.chain(*lang_suffixes.values(), clink_suffixes)) # type: T.Set[str] +SUFFIX_TO_LANG = dict(itertools.chain(*( + [(suffix, lang) for suffix in v] for lang, v in lang_suffixes.items()))) # type: T.Dict[str, str] # Languages that should use LDFLAGS arguments when linking. LANGUAGES_USING_LDFLAGS = {'objcpp', 'cpp', 'objc', 'c', 'fortran', 'd', 'cuda'} # type: T.Set[str] diff --git a/mesonbuild/interpreter/compiler.py b/mesonbuild/interpreter/compiler.py index 54f4beef2..b76e7f87d 100644 --- a/mesonbuild/interpreter/compiler.py +++ b/mesonbuild/interpreter/compiler.py @@ -11,6 +11,7 @@ from .. import coredata from .. import dependencies from .. import mesonlib from .. import mlog +from ..compilers import SUFFIX_TO_LANG from ..compilers.compilers import CompileCheckMode from ..interpreterbase import (ObjectHolder, noPosargs, noKwargs, FeatureNew, disablerIfNotFound, @@ -454,13 +455,27 @@ class CompilerHolder(ObjectHolder['Compiler']): @typed_kwargs('compiler.links', *_COMPILES_KWS) def links_method(self, args: T.Tuple['mesonlib.FileOrString'], kwargs: 'CompileKW') -> bool: code = args[0] + compiler = None if isinstance(code, mesonlib.File): code = mesonlib.File.from_absolute_file( code.rel_to_builddir(self.environment.source_dir)) + suffix = code.suffix + if suffix not in self.compiler.file_suffixes: + for_machine = self.compiler.for_machine + clist = self.interpreter.coredata.compilers[for_machine] + if suffix not in SUFFIX_TO_LANG: + # just pass it to the compiler driver + mlog.warning(f'Unknown suffix for test file {code}') + elif SUFFIX_TO_LANG[suffix] not in clist: + mlog.warning(f'Passed {SUFFIX_TO_LANG[suffix]} source to links method, not specified for {for_machine.get_lower_case_name()} machine.') + else: + compiler = clist[SUFFIX_TO_LANG[suffix]] + testname = kwargs['name'] extra_args = functools.partial(self._determine_args, kwargs['no_builtin_args'], kwargs['include_directories'], kwargs['args']) deps, msg = self._determine_dependencies(kwargs['dependencies']) result, cached = self.compiler.links(code, self.environment, + compiler=compiler, extra_args=extra_args, dependencies=deps) cached_msg = mlog.blue('(cached)') if cached else '' diff --git a/mesonbuild/mesonlib/universal.py b/mesonbuild/mesonlib/universal.py index 1680541b3..f81c01a25 100644 --- a/mesonbuild/mesonlib/universal.py +++ b/mesonbuild/mesonlib/universal.py @@ -421,6 +421,10 @@ class File(HoldableObject): absdir = builddir return os.path.join(absdir, self.relative_name()) + @property + def suffix(self) -> str: + return os.path.splitext(self.fname)[1][1:].lower() + def endswith(self, ending: str) -> bool: return self.fname.endswith(ending) -- cgit v1.2.3