1# Copyright 2019 The meson development team 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15"""Abstractions for the LLVM/Clang compiler family.""" 16 17import os 18import shutil 19import typing as T 20 21from ... import mesonlib 22from ...linkers import AppleDynamicLinker 23from .gnu import GnuLikeCompiler 24 25if T.TYPE_CHECKING: 26 from ...environment import Environment 27 from ...dependencies import Dependency # noqa: F401 28 29clang_color_args = { 30 'auto': ['-Xclang', '-fcolor-diagnostics'], 31 'always': ['-Xclang', '-fcolor-diagnostics'], 32 'never': ['-Xclang', '-fno-color-diagnostics'], 33} # type: T.Dict[str, T.List[str]] 34 35clang_optimization_args = { 36 '0': [], 37 'g': ['-Og'], 38 '1': ['-O1'], 39 '2': ['-O2'], 40 '3': ['-O3'], 41 's': ['-Os'], 42} # type: T.Dict[str, T.List[str]] 43 44class ClangCompiler(GnuLikeCompiler): 45 def __init__(self, defines: T.Optional[T.Dict[str, str]]): 46 super().__init__() 47 self.id = 'clang' 48 self.defines = defines or {} 49 self.base_options.append('b_colorout') 50 # TODO: this really should be part of the linker base_options, but 51 # linkers don't have base_options. 52 if isinstance(self.linker, AppleDynamicLinker): 53 self.base_options.append('b_bitcode') 54 # All Clang backends can also do LLVM IR 55 self.can_compile_suffixes.add('ll') 56 57 def get_colorout_args(self, colortype: str) -> T.List[str]: 58 return clang_color_args[colortype][:] 59 60 def has_builtin_define(self, define: str) -> bool: 61 return define in self.defines 62 63 def get_builtin_define(self, define: str) -> T.Optional[str]: 64 return self.defines.get(define) 65 66 def get_optimization_args(self, optimization_level: str) -> T.List[str]: 67 return clang_optimization_args[optimization_level] 68 69 def get_pch_suffix(self) -> str: 70 return 'pch' 71 72 def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: 73 # Workaround for Clang bug http://llvm.org/bugs/show_bug.cgi?id=15136 74 # This flag is internal to Clang (or at least not documented on the man page) 75 # so it might change semantics at any time. 76 return ['-include-pch', os.path.join(pch_dir, self.get_pch_name(header))] 77 78 def has_multi_arguments(self, args: T.List[str], env: 'Environment') -> T.List[str]: 79 myargs = ['-Werror=unknown-warning-option', '-Werror=unused-command-line-argument'] 80 if mesonlib.version_compare(self.version, '>=3.6.0'): 81 myargs.append('-Werror=ignored-optimization-argument') 82 return super().has_multi_arguments( 83 myargs + args, 84 env) 85 86 def has_function(self, funcname: str, prefix: str, env: 'Environment', *, 87 extra_args: T.Optional[T.List[str]] = None, 88 dependencies: T.Optional[T.List['Dependency']] = None) -> bool: 89 if extra_args is None: 90 extra_args = [] 91 # Starting with XCode 8, we need to pass this to force linker 92 # visibility to obey OS X/iOS/tvOS minimum version targets with 93 # -mmacosx-version-min, -miphoneos-version-min, -mtvos-version-min etc. 94 # https://github.com/Homebrew/homebrew-core/issues/3727 95 # TODO: this really should be communicated by the linker 96 if isinstance(self.linker, AppleDynamicLinker) and mesonlib.version_compare(self.version, '>=8.0'): 97 extra_args.append('-Wl,-no_weak_imports') 98 return super().has_function(funcname, prefix, env, extra_args=extra_args, 99 dependencies=dependencies) 100 101 def openmp_flags(self) -> T.List[str]: 102 if mesonlib.version_compare(self.version, '>=3.8.0'): 103 return ['-fopenmp'] 104 elif mesonlib.version_compare(self.version, '>=3.7.0'): 105 return ['-fopenmp=libomp'] 106 else: 107 # Shouldn't work, but it'll be checked explicitly in the OpenMP dependency. 108 return [] 109 110 @classmethod 111 def use_linker_args(cls, linker: str) -> T.List[str]: 112 # Clang additionally can use a linker specified as a path, which GCC 113 # (and other gcc-like compilers) cannot. This is becuse clang (being 114 # llvm based) is retargetable, while GCC is not. 115 # 116 if shutil.which(linker): 117 if not shutil.which(linker): 118 raise mesonlib.MesonException( 119 'Cannot find linker {}.'.format(linker)) 120 return ['-fuse-ld={}'.format(linker)] 121 return super().use_linker_args(linker) 122 123 def get_has_func_attribute_extra_args(self, name): 124 # Clang only warns about unknown or ignored attributes, so force an 125 # error. 126 return ['-Werror=attributes'] 127 128 def get_coverage_link_args(self) -> T.List[str]: 129 return ['--coverage'] 130