From 64edfd50691be21fae1e61b8864d6de6fcead1d4 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Thu, 20 Sep 2018 20:29:57 +0100 Subject: Detect clang-cl as msvc-like, not clang-like Handle clang's cl or clang-cl being in PATH, or set in CC/CXX Future work: checking the name of the executable here seems like a bad idea. These compilers will fail to be detected if they are renamed. v2: Update compiler.get_argument_type() test Fix comparisons of id inside CCompiler, backends and elsewhere v3: ClangClCPPCompiler should be a subclass of ClangClCCompier, as well Future work: mocking in test_find_library_patterns() is effected, as we now test for a subclass, rather than self.id in CCompiler.get_library_naming() --- mesonbuild/compilers/__init__.py | 4 ++++ mesonbuild/compilers/c.py | 10 +++++++--- mesonbuild/compilers/cpp.py | 6 +++++- 3 files changed, 16 insertions(+), 4 deletions(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/__init__.py b/mesonbuild/compilers/__init__.py index 677301e..7050b0c 100644 --- a/mesonbuild/compilers/__init__.py +++ b/mesonbuild/compilers/__init__.py @@ -45,6 +45,8 @@ __all__ = [ 'ClangCPPCompiler', 'ClangObjCCompiler', 'ClangObjCPPCompiler', + 'ClangClCCompiler', + 'ClangClCPPCompiler', 'CompilerArgs', 'CPPCompiler', 'DCompiler', @@ -114,6 +116,7 @@ from .c import ( ArmCCompiler, ArmclangCCompiler, ClangCCompiler, + ClangClCCompiler, GnuCCompiler, ElbrusCCompiler, IntelCCompiler, @@ -124,6 +127,7 @@ from .cpp import ( ArmCPPCompiler, ArmclangCPPCompiler, ClangCPPCompiler, + ClangClCPPCompiler, GnuCPPCompiler, ElbrusCPPCompiler, IntelCPPCompiler, diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index 9b24e85..d17dd3a 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -170,7 +170,7 @@ class CCompiler(Compiler): else: # GNU ld and LLVM lld return ['-Wl,--allow-shlib-undefined'] - elif self.id == 'msvc': + elif isinstance(self, VisualStudioCCompiler): # link.exe return ['/FORCE:UNRESOLVED'] # FIXME: implement other linkers @@ -890,7 +890,7 @@ class CCompiler(Compiler): stlibext = ['a'] # We've always allowed libname to be both `foo` and `libfoo`, # and now people depend on it - if strict and self.id != 'msvc': # lib prefix is not usually used with msvc + if strict and not isinstance(self, VisualStudioCCompiler): # lib prefix is not usually used with msvc prefixes = ['lib'] else: prefixes = ['lib', ''] @@ -900,7 +900,7 @@ class CCompiler(Compiler): elif for_windows(env.is_cross_build(), env): # FIXME: .lib files can be import or static so we should read the # file, figure out which one it is, and reject the wrong kind. - if self.id == 'msvc': + if isinstance(self, VisualStudioCCompiler): shlibext = ['lib'] else: shlibext = ['dll.a', 'lib', 'dll'] @@ -1546,6 +1546,10 @@ class VisualStudioCCompiler(CCompiler): def get_argument_syntax(self): return 'msvc' +class ClangClCCompiler(VisualStudioCCompiler): + def __init__(self, exelist, version, is_cross, exe_wrap, is_64): + super().__init__(exelist, version, is_cross, exe_wrap, is_64) + self.id = 'clang-cl' class ArmCCompiler(ArmCompiler, CCompiler): def __init__(self, exelist, version, compiler_type, is_cross, exe_wrapper=None, **kwargs): diff --git a/mesonbuild/compilers/cpp.py b/mesonbuild/compilers/cpp.py index 9dc2876..1045c7d 100644 --- a/mesonbuild/compilers/cpp.py +++ b/mesonbuild/compilers/cpp.py @@ -19,7 +19,7 @@ from .. import coredata from .. import mlog from ..mesonlib import MesonException, version_compare -from .c import CCompiler, VisualStudioCCompiler +from .c import CCompiler, VisualStudioCCompiler, ClangClCCompiler from .compilers import ( CompilerType, gnu_winlibs, @@ -378,6 +378,10 @@ class VisualStudioCPPCompiler(VisualStudioCCompiler, CPPCompiler): # so just use the plain C args. return VisualStudioCCompiler.get_compiler_check_args(self) +class ClangClCPPCompiler(VisualStudioCPPCompiler, ClangClCCompiler): + def __init__(self, exelist, version, is_cross, exe_wrap, is_64): + VisualStudioCPPCompiler.__init__(self, exelist, version, is_cross, exe_wrap, is_64) + self.id = 'clang-cl' class ArmCPPCompiler(ArmCompiler, CPPCompiler): def __init__(self, exelist, version, compiler_type, is_cross, exe_wrap=None, **kwargs): -- cgit v1.1 From bb31a8c1c752cff6f969e345f3a170f935f159cf Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Fri, 21 Sep 2018 17:38:46 +0100 Subject: Only add link arguments when needed in Compiler object methods Currently, ComplierHolder.determine_args() unconditionally adds the link arguments to the commmand, even if we aren't linking, because it doesn't have access to the mode (preprocess, compile, link) that _get_compiler_check_args() will use. This leads to command lines like: 'cl testfile.c /nologo /showIncludes /c /Fooutput.obj /Od kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib' which clang-cl considers invalid; MSVS cl accepts this, ignoring the unneeded libraries Change from passing extra_args down to _get_compiler_check_args(), to passing down a callback to CompilerHolder.determine_args() (with a bound kwargs argument), so it can consult mode and kwargs to determine the args to use. --- mesonbuild/compilers/c.py | 2 ++ mesonbuild/compilers/d.py | 2 ++ 2 files changed, 4 insertions(+) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index d17dd3a..b41625c 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -392,6 +392,8 @@ class CCompiler(Compiler): return self.compiles(t.format(**fargs), env, extra_args, dependencies) def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'): + if callable(extra_args): + extra_args = extra_args(mode) if extra_args is None: extra_args = [] elif isinstance(extra_args, str): diff --git a/mesonbuild/compilers/d.py b/mesonbuild/compilers/d.py index 099d907..0a59e7f 100644 --- a/mesonbuild/compilers/d.py +++ b/mesonbuild/compilers/d.py @@ -274,6 +274,8 @@ class DCompiler(Compiler): return ['-Wl,-rpath,{}'.format(paths)] def _get_compiler_check_args(self, env, extra_args, dependencies, mode='compile'): + if callable(extra_args): + extra_args = extra_args(mode) if extra_args is None: extra_args = [] elif isinstance(extra_args, str): -- cgit v1.1 From b5b7e5b94ae7aaacc04a9d15e60892c385e9388e Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Mon, 1 Oct 2018 21:57:39 +0100 Subject: Teach VisualStudioCCompiler.has_arguments() about clang-cl When invoked as clang-cl to compile, it doesn't emit cl-compatible D9002 warnings about unknown options, but fortunately also supports -Werror-unknown-argument instead. When invoked to link, and using LINK, it does emit cl-compatible LNK4044 warnings about unknown options. --- mesonbuild/compilers/c.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index b41625c..24f661f 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -1451,6 +1451,8 @@ class VisualStudioCCompiler(CCompiler): # http://stackoverflow.com/questions/15259720/how-can-i-make-the-microsoft-c-compiler-treat-unknown-flags-as-errors-rather-t def has_arguments(self, args, env, code, mode): warning_text = '4044' if mode == 'link' else '9002' + if self.id == 'clang-cl' and mode != 'link': + args = args + ['-Werror=unknown-argument'] with self._build_wrapper(code, env, extra_args=args, mode=mode) as p: if p.returncode != 0: return False -- cgit v1.1 From e820c66a4c367397111093bc7531868e2f0ef2e3 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Mon, 1 Oct 2018 23:06:29 +0100 Subject: Teach VisualStudioCCompiler.get_pch_use_args() to handle clang-cl It seems that clang-cl isn't quite compatible with cl in the way it handles pch, and when the precompiled header is used, the pathname of the header is needed, not just its filename. This fixes test\common\13 pch with clang-cl --- mesonbuild/compilers/c.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index 24f661f..b185dfb 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -1316,6 +1316,8 @@ class VisualStudioCCompiler(CCompiler): def get_pch_use_args(self, pch_dir, header): base = os.path.basename(header) + if self.id == 'clang-cl': + base = header pchname = self.get_pch_name(header) return ['/FI' + base, '/Yu' + base, '/Fp' + os.path.join(pch_dir, pchname)] -- cgit v1.1 From a025c98d3082806321f53c4dfae9c738d1dc1584 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Wed, 24 Oct 2018 14:47:02 +0100 Subject: Qualify checks of self.version by self.id in VisualStudioC/CPPCompiler --- mesonbuild/compilers/c.py | 10 +++++++--- mesonbuild/compilers/cpp.py | 17 ++++++++++------- 2 files changed, 17 insertions(+), 10 deletions(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index b185dfb..ab62afd 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -1298,7 +1298,7 @@ class VisualStudioCCompiler(CCompiler): def get_buildtype_args(self, buildtype): args = compilers.msvc_buildtype_args[buildtype] - if version_compare(self.version, '<18.0'): + if self.id == 'msvc' and version_compare(self.version, '<18.0'): args = [arg for arg in args if arg != '/Gw'] return args @@ -1470,7 +1470,7 @@ class VisualStudioCCompiler(CCompiler): # build obviously, which is why we only do this when PCH is on. # This was added in Visual Studio 2013 (MSVC 18.0). Before that it was # always on: https://msdn.microsoft.com/en-us/library/dn502518.aspx - if pch and version_compare(self.version, '>=18.0'): + if pch and self.id == 'msvc' and version_compare(self.version, '>=18.0'): args = ['/FS'] + args return args @@ -1487,7 +1487,7 @@ class VisualStudioCCompiler(CCompiler): def get_instruction_set_args(self, instruction_set): if self.is_64: return vs64_instruction_set_args.get(instruction_set, None) - if self.version.split('.')[0] == '16' and instruction_set == 'avx': + if self.id == 'msvc' and self.version.split('.')[0] == '16' and instruction_set == 'avx': # VS documentation says that this exists and should work, but # it does not. The headers do not contain AVX intrinsics # and the can not be called. @@ -1495,6 +1495,10 @@ class VisualStudioCCompiler(CCompiler): return vs32_instruction_set_args.get(instruction_set, None) def get_toolset_version(self): + if self.id == 'clang-cl': + # I have no idea + return '14.1' + # See boost/config/compiler/visualc.cpp for up to date mapping try: version = int(''.join(self.version.split('.')[0:2])) diff --git a/mesonbuild/compilers/cpp.py b/mesonbuild/compilers/cpp.py index 1045c7d..65a1033 100644 --- a/mesonbuild/compilers/cpp.py +++ b/mesonbuild/compilers/cpp.py @@ -310,12 +310,15 @@ class VisualStudioCPPCompiler(VisualStudioCCompiler, CPPCompiler): def get_options(self): cpp_stds = ['none', 'c++11', 'vc++11'] - # Visual Studio 2015 and later - if version_compare(self.version, '>=19'): - cpp_stds.extend(['c++14', 'vc++14', 'c++latest', 'vc++latest']) - # Visual Studio 2017 and later - if version_compare(self.version, '>=19.11'): - cpp_stds.extend(['c++17', 'vc++17']) + if self.id == 'clang-cl': + cpp_stds.extend(['c++14', 'vc++14', 'c++17', 'vc++17', 'c++latest']) + else: + # Visual Studio 2015 and later + if version_compare(self.version, '>=19'): + cpp_stds.extend(['c++14', 'vc++14', 'c++latest', 'vc++latest']) + # Visual Studio 2017 and later + if version_compare(self.version, '>=19.11'): + cpp_stds.extend(['c++17', 'vc++17']) opts = CPPCompiler.get_options(self) opts.update({'cpp_eh': coredata.UserComboOption('cpp_eh', @@ -356,7 +359,7 @@ class VisualStudioCPPCompiler(VisualStudioCCompiler, CPPCompiler): # which means setting the C++ standard version to C++14, in compilers that support it # (i.e., after VS2015U3) # if one is using anything before that point, one cannot set the standard. - if version_compare(self.version, '>=19.00.24210'): + if self.id == 'clang-cl' or version_compare(self.version, '>=19.00.24210'): mlog.warning('MSVC does not support C++11; ' 'attempting best effort; setting the standard to C++14') args.append('/std:c++14') -- cgit v1.1 From c789efb8c883a719b308e023b82fab4d808ab460 Mon Sep 17 00:00:00 2001 From: Jon Turney Date: Mon, 8 Oct 2018 19:39:41 +0100 Subject: Use lld-link with clang-cl Use lld-link dynamic linker with clang-cl Don't hardcode dynamic linker name in tests --- mesonbuild/compilers/c.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'mesonbuild/compilers') diff --git a/mesonbuild/compilers/c.py b/mesonbuild/compilers/c.py index ab62afd..35c71df 100644 --- a/mesonbuild/compilers/c.py +++ b/mesonbuild/compilers/c.py @@ -1345,7 +1345,12 @@ class VisualStudioCCompiler(CCompiler): return [] def get_linker_exelist(self): - return ['link'] # FIXME, should have same path as compiler. + # FIXME, should have same path as compiler. + # FIXME, should be controllable via cross-file. + if self.id == 'clang-cl': + return ['lld-link'] + else: + return ['link'] def get_linker_always_args(self): return ['/nologo'] -- cgit v1.1