compilers: don't use instance checks to determine properties

In various situations we want to figure out what type of compiler we
have, because we want to know stuff like "is it the pgi one", or "does
it use msvc style". The compiler object has this property already, via
an API specifically designed to communicate this info, but instead we
performed isinstance checks on a compiler class.

This is confusing and indirect, and has the side effect of requiring
more imports everywhere. We should do away with it.
pull/10810/head
Eli Schwartz 2 years ago
parent 24ac3cdb9c
commit 0a9048e554
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 28
      mesonbuild/backend/ninjabackend.py
  2. 3
      mesonbuild/cmake/toolchain.py
  3. 4
      mesonbuild/environment.py
  4. 7
      unittests/windowstests.py

@ -36,13 +36,8 @@ from .. import build
from .. import mlog
from .. import compilers
from ..arglist import CompilerArgs
from ..compilers import (
Compiler, CCompiler,
FortranCompiler,
mixins,
PGICCompiler,
VisualStudioLikeCompiler,
)
from ..compilers import Compiler
from ..compilers.c import CCompiler
from ..linkers import ArLinker, AppleArLinker, RSPFileSyntax
from ..mesonlib import (
File, LibType, MachineChoice, MesonException, OrderedSet, PerMachine,
@ -60,6 +55,7 @@ if T.TYPE_CHECKING:
from ..interpreter import Interpreter
from ..linkers import DynamicLinker, StaticLinker
from ..compilers.cs import CsCompiler
from ..compilers.fortran import FortranCompiler
RUST_EDITIONS = Literal['2015', '2018', '2021']
@ -516,12 +512,12 @@ class NinjaBackend(backends.Backend):
# Have to detect the dependency format
# IFort on windows is MSVC like, but doesn't have /showincludes
if isinstance(compiler, FortranCompiler):
if compiler.language == 'fortran':
continue
if isinstance(compiler, PGICCompiler) and mesonlib.is_windows():
if compiler.id == 'pgi' and mesonlib.is_windows():
# for the purpose of this function, PGI doesn't act enough like MSVC
return open(tempfilename, 'a', encoding='utf-8')
if isinstance(compiler, VisualStudioLikeCompiler):
if compiler.get_argument_syntax() == 'msvc':
break
else:
# None of our compilers are MSVC, we're done.
@ -2325,7 +2321,7 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
command = compiler.get_exelist()
args = ['$ARGS'] + depargs + NinjaCommandArg.list(compiler.get_output_args('$out'), Quoting.none) + compiler.get_compile_only_args() + ['$in']
description = f'Compiling {compiler.get_display_language()} object $out'
if isinstance(compiler, VisualStudioLikeCompiler):
if compiler.get_argument_syntax() == 'msvc':
deps = 'msvc'
depfile = None
else:
@ -2341,13 +2337,13 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
rule = self.compiler_to_pch_rule_name(compiler)
depargs = compiler.get_dependency_gen_args('$out', '$DEPFILE')
if isinstance(compiler, VisualStudioLikeCompiler):
if compiler.get_argument_syntax() == 'msvc':
output = []
else:
output = NinjaCommandArg.list(compiler.get_output_args('$out'), Quoting.none)
command = compiler.get_exelist() + ['$ARGS'] + depargs + output + compiler.get_compile_only_args() + ['$in']
description = 'Precompiling header $in'
if isinstance(compiler, VisualStudioLikeCompiler):
if compiler.get_argument_syntax() == 'msvc':
deps = 'msvc'
depfile = None
else:
@ -2931,8 +2927,8 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
msg = f'Precompiled header of {target.get_basename()!r} must not be in the same ' \
'directory as source, please put it in a subdirectory.'
raise InvalidArguments(msg)
compiler = target.compilers[lang]
if isinstance(compiler, VisualStudioLikeCompiler):
compiler: Compiler = target.compilers[lang]
if compiler.get_argument_syntax() == 'msvc':
(commands, dep, dst, objs, src) = self.generate_msvc_pch_command(target, compiler, pch)
extradep = os.path.join(self.build_to_src, target.get_source_subdir(), pch[0])
elif compiler.id == 'intel':
@ -3026,7 +3022,7 @@ https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47485'''))
def get_link_whole_args(self, linker, target):
use_custom = False
if isinstance(linker, mixins.visualstudio.MSVCCompiler):
if linker.id == 'msvc':
# Expand our object lists manually if we are on pre-Visual Studio 2015 Update 2
# (incidentally, the "linker" here actually refers to cl.exe)
if mesonlib.version_compare(linker.version, '<19.00.23918'):

@ -16,7 +16,6 @@ from __future__ import annotations
from pathlib import Path
from .traceparser import CMakeTraceParser
from ..envconfig import CMakeSkipCompilerTest
from ..compilers import VisualStudioLikeCompiler
from .common import language_map, cmake_get_generator_args
from .. import mlog
@ -204,7 +203,7 @@ class CMakeToolchain:
@staticmethod
def is_cmdline_option(compiler: 'Compiler', arg: str) -> bool:
if isinstance(compiler, VisualStudioLikeCompiler):
if compiler.get_argument_syntax() == 'msvc':
return arg.startswith('/')
else:
return arg.startswith('-')

@ -845,10 +845,10 @@ class Environment:
def get_compiler_system_dirs(self, for_machine: MachineChoice):
for comp in self.coredata.compilers[for_machine].values():
if isinstance(comp, compilers.ClangCompiler):
if comp.id == 'clang':
index = 1
break
elif isinstance(comp, compilers.GnuCompiler):
elif comp.id == 'gcc':
index = 2
break
else:

@ -32,7 +32,6 @@ from mesonbuild.mesonlib import (
)
from mesonbuild.compilers import (
detect_c_compiler, detect_d_compiler, compiler_from_language,
GnuLikeCompiler
)
from mesonbuild.programs import ExternalProgram
import mesonbuild.dependencies.base
@ -222,21 +221,21 @@ class WindowsTests(BasePlatformTests):
def test_link_environment_variable_lld_link(self):
env = get_fake_env()
comp = detect_c_compiler(env, MachineChoice.HOST)
if isinstance(comp, GnuLikeCompiler):
if comp.get_argument_syntax() == 'gcc':
raise SkipTest('GCC cannot be used with link compatible linkers.')
self._check_ld('lld-link', 'c', 'lld-link')
def test_link_environment_variable_link(self):
env = get_fake_env()
comp = detect_c_compiler(env, MachineChoice.HOST)
if isinstance(comp, GnuLikeCompiler):
if comp.get_argument_syntax() == 'gcc':
raise SkipTest('GCC cannot be used with link compatible linkers.')
self._check_ld('link', 'c', 'link')
def test_link_environment_variable_optlink(self):
env = get_fake_env()
comp = detect_c_compiler(env, MachineChoice.HOST)
if isinstance(comp, GnuLikeCompiler):
if comp.get_argument_syntax() == 'gcc':
raise SkipTest('GCC cannot be used with link compatible linkers.')
self._check_ld('optlink', 'c', 'optlink')

Loading…
Cancel
Save