From b6c28d282c99a0a7915f9924d98d620f70403814 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 17 Aug 2020 22:23:34 -0700 Subject: [PATCH 1/2] compilers: Add Apple subclasses for ObjC and ObjC++ These are needed because in some cases we need to be able to know if we're using vanilla clang or Apple's clang. --- mesonbuild/compilers/__init__.py | 4 ++++ mesonbuild/compilers/objc.py | 5 +++++ mesonbuild/compilers/objcpp.py | 6 ++++++ mesonbuild/environment.py | 9 +++++++-- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mesonbuild/compilers/__init__.py b/mesonbuild/compilers/__init__.py index fd47545d1..5b765a631 100644 --- a/mesonbuild/compilers/__init__.py +++ b/mesonbuild/compilers/__init__.py @@ -36,6 +36,8 @@ __all__ = [ 'AppleClangCCompiler', 'AppleClangCPPCompiler', + 'AppleClangObjCCompiler', + 'AppleClangObjCPPCompiler', 'ArmCCompiler', 'ArmCPPCompiler', 'ArmclangCCompiler', @@ -183,11 +185,13 @@ from .fortran import ( from .java import JavaCompiler from .objc import ( ObjCCompiler, + AppleClangObjCCompiler, ClangObjCCompiler, GnuObjCCompiler, ) from .objcpp import ( ObjCPPCompiler, + AppleClangObjCPPCompiler, ClangObjCPPCompiler, GnuObjCPPCompiler, ) diff --git a/mesonbuild/compilers/objc.py b/mesonbuild/compilers/objc.py index d351c8826..254a609bf 100644 --- a/mesonbuild/compilers/objc.py +++ b/mesonbuild/compilers/objc.py @@ -92,3 +92,8 @@ class ClangObjCCompiler(ClangCompiler, ObjCCompiler): '1': default_warn_args, '2': default_warn_args + ['-Wextra'], '3': default_warn_args + ['-Wextra', '-Wpedantic']} + + +class AppleClangObjCCompiler(ClangObjCCompiler): + + """Handle the differences between Apple's clang and vanilla clang.""" diff --git a/mesonbuild/compilers/objcpp.py b/mesonbuild/compilers/objcpp.py index 10555b455..3197abc8c 100644 --- a/mesonbuild/compilers/objcpp.py +++ b/mesonbuild/compilers/objcpp.py @@ -90,3 +90,9 @@ class ClangObjCPPCompiler(ClangCompiler, ObjCPPCompiler): '1': default_warn_args, '2': default_warn_args + ['-Wextra'], '3': default_warn_args + ['-Wextra', '-Wpedantic']} + + + +class AppleClangObjCPPCompiler(ClangObjCPPCompiler): + + """Handle the differences between Apple's clang and vanilla clang.""" diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index da2d513e6..5645c7179 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -74,6 +74,8 @@ from .compilers import ( ArmclangCPPCompiler, AppleClangCCompiler, AppleClangCPPCompiler, + AppleClangObjCCompiler, + AppleClangObjCPPCompiler, ClangCCompiler, ClangCPPCompiler, ClangObjCCompiler, @@ -1506,7 +1508,7 @@ class Environment: def detect_objcpp_compiler(self, for_machine: MachineInfo) -> 'Compiler': return self._detect_objc_or_objcpp_compiler(for_machine, False) - def _detect_objc_or_objcpp_compiler(self, for_machine: MachineInfo, objc: bool) -> 'Compiler': + def _detect_objc_or_objcpp_compiler(self, for_machine: MachineChoice, objc: bool) -> 'Compiler': popen_exceptions = {} compilers, ccache, exe_wrap = self._get_compilers('objc' if objc else 'objcpp', for_machine) is_cross = self.is_cross_build(for_machine) @@ -1535,7 +1537,10 @@ class Environment: exe_wrap, defines, linker=linker) if 'clang' in out: linker = None - comp = ClangObjCCompiler if objc else ClangObjCPPCompiler + if 'Apple' in out: + comp = AppleClangObjCCompiler if objc else AppleClangObjCPPCompiler + else: + comp = ClangObjCCompiler if objc else ClangObjCPPCompiler if 'windows' in out or self.machines[for_machine].is_windows(): # If we're in a MINGW context this actually will use a gnu style ld try: From 2845f5a2302de86e04efe7d9a153e05bde3caa65 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Mon, 17 Aug 2020 22:07:07 -0700 Subject: [PATCH 2/2] cmake: Fix detection of AppleClang It's not enough to detect that the linker is ld64: gcc, icc, and vanilla clang all use ld64 on macoOS. Instead we have to detect the class of the compiler, and determine if it's an Apple Compiler or a vanilla one. --- mesonbuild/cmake/executor.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/mesonbuild/cmake/executor.py b/mesonbuild/cmake/executor.py index e29e67a04..2226c0275 100644 --- a/mesonbuild/cmake/executor.py +++ b/mesonbuild/cmake/executor.py @@ -29,13 +29,18 @@ from .. import mlog, mesonlib from ..mesonlib import PerMachine, Popen_safe, version_compare, MachineChoice from ..environment import Environment from ..envconfig import get_env_var +from ..compilers import ( + AppleClangCCompiler, AppleClangCPPCompiler, AppleClangObjCCompiler, + AppleClangObjCPPCompiler +) if T.TYPE_CHECKING: from ..dependencies.base import ExternalProgram + from ..compilers import Compiler TYPE_result = T.Tuple[int, T.Optional[str], T.Optional[str]] -MESON_TO_CMAKE_MAPPING = { +_MESON_TO_CMAKE_MAPPING = { 'arm': 'ARMCC', 'armclang': 'ARMClang', 'clang': 'Clang', @@ -51,13 +56,21 @@ MESON_TO_CMAKE_MAPPING = { 'sun': 'SunPro', } -def meson_compiler_to_cmake_id(cobj): - # cland and apple clang both map to 'clang' in meson, so we need to look at - # the linker that's being used - if cobj.linker.get_id() == 'ld64': +def meson_compiler_to_cmake_id(cobj: 'Compiler') -> str: + """Translate meson compiler's into CMAKE compiler ID's. + + Most of these can be handled by a simple table lookup, with a few + exceptions. + + Clang and Apple's Clang are both identified as "clang" by meson. To make + things more complicated gcc and vanilla clang both use Apple's ld64 on + macOS. The only way to know for sure is to do an isinstance() check. + """ + if isinstance(cobj, (AppleClangCCompiler, AppleClangCPPCompiler, + AppleClangObjCCompiler, AppleClangObjCPPCompiler)): return 'AppleClang' # If no mapping, try GNU and hope that the build files don't care - return MESON_TO_CMAKE_MAPPING.get(cobj.get_id(), 'GNU') + return _MESON_TO_CMAKE_MAPPING.get(cobj.get_id(), 'GNU') class CMakeExecutor: