| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2019-2022 The meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
"""Abstractions for the LLVM/Clang compiler family.""" |
| 7 |
|
| 8 |
import os |
| 9 |
import shutil |
| 10 |
import typing as T |
| 11 |
|
| 12 |
from ... import mesonlib |
| 13 |
from ... import options |
| 14 |
from ...linkers.linkers import AppleDynamicLinker, ClangClDynamicLinker, LLVMDynamicLinker, \ |
| 15 |
GnuBFDDynamicLinker, GnuGoldDynamicLinker, MoldDynamicLinker, VisualStudioLikeLinkerMixin |
| 16 |
from ...options import OptionKey |
| 17 |
from ..compilers import CompileCheckMode |
| 18 |
from .gnu import GnuLikeCompiler |
| 19 |
|
| 20 |
if T.TYPE_CHECKING: |
| 21 |
from ...options import MutableKeyedOptionDictType |
| 22 |
from ...dependencies import Dependency # noqa: F401 |
| 23 |
from ...build import BuildTarget |
| 24 |
from ..compilers import Compiler |
| 25 |
|
| 26 |
CompilerMixinBase = Compiler |
| 27 |
else: |
| 28 |
CompilerMixinBase = object |
| 29 |
|
| 30 |
clang_color_args: T.Dict[str, T.List[str]] = { |
| 31 |
'auto': ['-fdiagnostics-color=auto'], |
| 32 |
'always': ['-fdiagnostics-color=always'], |
| 33 |
'never': ['-fdiagnostics-color=never'], |
| 34 |
} |
| 35 |
|
| 36 |
clang_optimization_args: T.Dict[str, T.List[str]] = { |
| 37 |
'plain': [], |
| 38 |
'0': ['-O0'], |
| 39 |
'g': ['-Og'], |
| 40 |
'1': ['-O1'], |
| 41 |
'2': ['-O2'], |
| 42 |
'3': ['-O3'], |
| 43 |
's': ['-Oz'], |
| 44 |
} |
| 45 |
|
| 46 |
clang_lang_map = { |
| 47 |
'c': 'c', |
| 48 |
'cpp': 'c++', |
| 49 |
'objc': 'objective-c', |
| 50 |
'objcpp': 'objective-c++', |
| 51 |
} |
| 52 |
|
| 53 |
class ClangCompiler(GnuLikeCompiler): |
| 54 |
|
| 55 |
id = 'clang' |
| 56 |
|
| 57 |
# -fms-runtime-lib is a compilation option which sets up an automatic dependency |
| 58 |
# from the .o files to the final link product |
| 59 |
CRT_D_ARGS: T.Dict[str, T.List[str]] = { |
| 60 |
'none': [], |
| 61 |
'md': ['-fms-runtime-lib=dll'], |
| 62 |
'mdd': ['-fms-runtime-lib=dll_dbg'], |
| 63 |
'mt': ['-fms-runtime-lib=static'], |
| 64 |
'mtd': ['-fms-runtime-lib=static_dbg'], |
| 65 |
} |
| 66 |
|
| 67 |
# disable libcmt to avoid warnings, as that is the default and clang |
| 68 |
# adds it by default. |
| 69 |
CRT_ARGS: T.Dict[str, T.List[str]] = { |
| 70 |
'none': [], |
| 71 |
'md': ['-Wl,/nodefaultlib:libcmt'], |
| 72 |
'mdd': ['-Wl,/nodefaultlib:libcmt'], |
| 73 |
'mt': [], |
| 74 |
'mtd': ['-Wl,/nodefaultlib:libcmt'], |
| 75 |
} |
| 76 |
|
| 77 |
def __init__(self, defines: T.Optional[T.Dict[str, str]]): |
| 78 |
super().__init__() |
| 79 |
self.defines = defines or {} |
| 80 |
self.base_options.update( |
| 81 |
{OptionKey('b_colorout'), OptionKey('b_lto_threads'), OptionKey('b_lto_mode'), OptionKey('b_thinlto_cache'), |
| 82 |
OptionKey('b_thinlto_cache_dir')}) |
| 83 |
|
| 84 |
# TODO: this really should be part of the linker base_options, but |
| 85 |
# linkers don't have base_options. |
| 86 |
if isinstance(self.linker, AppleDynamicLinker): |
| 87 |
self.base_options.add(OptionKey('b_bitcode')) |
| 88 |
elif isinstance(self.linker, VisualStudioLikeLinkerMixin): |
| 89 |
self.base_options.add(OptionKey('b_vscrt')) |
| 90 |
# All Clang backends can also do LLVM IR |
| 91 |
self.can_compile_suffixes.add('ll') |
| 92 |
|
| 93 |
def get_crt_compile_args(self, crt_val: str) -> T.List[str]: |
| 94 |
if not isinstance(self.linker, VisualStudioLikeLinkerMixin): |
| 95 |
return [] |
| 96 |
crt_val = self.get_crt_val(crt_val) |
| 97 |
return self.CRT_D_ARGS[crt_val] |
| 98 |
|
| 99 |
def get_crt_link_args(self, crt_val: str) -> T.List[str]: |
| 100 |
if not isinstance(self.linker, VisualStudioLikeLinkerMixin): |
| 101 |
return [] |
| 102 |
crt_val = self.get_crt_val(crt_val) |
| 103 |
return self.CRT_ARGS[crt_val] |
| 104 |
|
| 105 |
def get_colorout_args(self, colortype: str) -> T.List[str]: |
| 106 |
return clang_color_args[colortype][:] |
| 107 |
|
| 108 |
def has_builtin_define(self, define: str) -> bool: |
| 109 |
return define in self.defines |
| 110 |
|
| 111 |
def get_builtin_define(self, define: str) -> T.Optional[str]: |
| 112 |
return self.defines.get(define) |
| 113 |
|
| 114 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 115 |
return clang_optimization_args[optimization_level] |
| 116 |
|
| 117 |
def get_pch_suffix(self) -> str: |
| 118 |
return 'pch' |
| 119 |
|
| 120 |
def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: |
| 121 |
# Workaround for Clang bug http://llvm.org/bugs/show_bug.cgi?id=15136 |
| 122 |
# This flag is internal to Clang (or at least not documented on the man page) |
| 123 |
# so it might change semantics at any time. |
| 124 |
return ['-include-pch', os.path.join(pch_dir, self.get_pch_name(header))] |
| 125 |
|
| 126 |
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]: |
| 127 |
# Clang is different than GCC, it will return True when a symbol isn't |
| 128 |
# defined in a header. Specifically this is caused by a functionality |
| 129 |
# both GCC and clang have: for some "well known" functions, arbitrarily |
| 130 |
# chosen, they provide fixit suggestions for the header you should try |
| 131 |
# including. |
| 132 |
# |
| 133 |
# - With GCC, this is a note appended to the prexisting diagnostic |
| 134 |
# "error: undeclared identifier" |
| 135 |
# |
| 136 |
# - With clang, the error is converted to a c89'ish implicit function |
| 137 |
# declaration instead, which can be disabled with -Wno-error and on |
| 138 |
# clang < 16, simply passes compilation by default. |
| 139 |
# |
| 140 |
# One example of a clang fixit suggestion is for `strlcat`, which |
| 141 |
# triggers this. |
| 142 |
# |
| 143 |
# This was reported in 2017 and promptly fixed. Just kidding! |
| 144 |
# https://github.com/llvm/llvm-project/issues/33905 |
| 145 |
myargs: T.List[str] = ['-Werror=implicit-function-declaration'] |
| 146 |
if mode is CompileCheckMode.COMPILE: |
| 147 |
myargs.extend(['-Werror=unknown-warning-option', '-Werror=unused-command-line-argument']) |
| 148 |
if mesonlib.version_compare(self.version, '>=3.6.0'): |
| 149 |
myargs.append('-Werror=ignored-optimization-argument') |
| 150 |
return super().get_compiler_check_args(mode) + myargs |
| 151 |
|
| 152 |
def has_function(self, funcname: str, prefix: str, *, |
| 153 |
extra_args: T.Optional[T.List[str]] = None, |
| 154 |
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: |
| 155 |
if extra_args is None: |
| 156 |
extra_args = [] |
| 157 |
# Starting with XCode 8, we need to pass this to force linker |
| 158 |
# visibility to obey OS X/iOS/tvOS minimum version targets with |
| 159 |
# -mmacosx-version-min, -miphoneos-version-min, -mtvos-version-min etc. |
| 160 |
# https://github.com/Homebrew/homebrew-core/issues/3727 |
| 161 |
# TODO: this really should be communicated by the linker |
| 162 |
if isinstance(self.linker, AppleDynamicLinker) and mesonlib.version_compare(self.version, '>=8.0'): |
| 163 |
extra_args.append('-Wl,-no_weak_imports') |
| 164 |
return super().has_function(funcname, prefix, extra_args=extra_args, |
| 165 |
dependencies=dependencies) |
| 166 |
|
| 167 |
def openmp_flags(self) -> T.List[str]: |
| 168 |
if mesonlib.version_compare(self.version, '>=3.8.0'): |
| 169 |
return ['-fopenmp'] |
| 170 |
elif mesonlib.version_compare(self.version, '>=3.7.0'): |
| 171 |
return ['-fopenmp=libomp'] |
| 172 |
else: |
| 173 |
# Shouldn't work, but it'll be checked explicitly in the OpenMP dependency. |
| 174 |
return [] |
| 175 |
|
| 176 |
@classmethod |
| 177 |
def use_linker_args(cls, linker: str, version: str) -> T.List[str]: |
| 178 |
# Clang additionally can use a linker specified as a path, which GCC |
| 179 |
# (and other gcc-like compilers) cannot. This is because clang (being |
| 180 |
# llvm based) is retargetable, while GCC is not. |
| 181 |
# |
| 182 |
|
| 183 |
# eld: Qualcomm's opensource embedded linker |
| 184 |
if linker == 'eld': |
| 185 |
return ['-fuse-ld=eld'] |
| 186 |
# qcld: Qualcomm's deprecated linker |
| 187 |
if linker == 'qcld': |
| 188 |
return ['-fuse-ld=qcld'] |
| 189 |
if linker == 'mold': |
| 190 |
return ['-fuse-ld=mold'] |
| 191 |
|
| 192 |
if shutil.which(linker): |
| 193 |
if not shutil.which(linker): |
| 194 |
raise mesonlib.MesonException( |
| 195 |
f'Cannot find linker {linker}.') |
| 196 |
return [f'-fuse-ld={linker}'] |
| 197 |
return super().use_linker_args(linker, version) |
| 198 |
|
| 199 |
def get_has_func_attribute_extra_args(self, name: str) -> T.List[str]: |
| 200 |
# Clang only warns about unknown or ignored attributes, so force an |
| 201 |
# error. |
| 202 |
return ['-Werror=attributes'] |
| 203 |
|
| 204 |
def get_prelink_args(self, prelink_name: str, obj_list: T.List[str]) -> T.Tuple[T.List[str], T.List[str]]: |
| 205 |
if not mesonlib.version_compare(self.version, '>=14'): |
| 206 |
raise mesonlib.MesonException('prelinking requires clang >=14') |
| 207 |
return [prelink_name], ['-r', '-o', prelink_name] + obj_list |
| 208 |
|
| 209 |
def get_coverage_link_args(self) -> T.List[str]: |
| 210 |
return ['--coverage'] |
| 211 |
|
| 212 |
def get_embed_bitcode_args(self, bitcode: bool, lto: bool) -> T.List[str]: |
| 213 |
return ['-fembed-bitcode'] if bitcode else [] |
| 214 |
|
| 215 |
def get_lto_compile_args(self, *, target: T.Optional[BuildTarget] = None, threads: int = 0, |
| 216 |
mode: str = 'default') -> T.List[str]: |
| 217 |
args: T.List[str] = [] |
| 218 |
if mode == 'thin': |
| 219 |
# ThinLTO requires the use of gold, lld, ld64, lld-link or mold 1.1+ |
| 220 |
if isinstance(self.linker, (MoldDynamicLinker)): |
| 221 |
# https://github.com/rui314/mold/commit/46995bcfc3e3113133620bf16445c5f13cd76a18 |
| 222 |
if not mesonlib.version_compare(self.linker.version, '>=1.1'): |
| 223 |
raise mesonlib.MesonException("LLVM's ThinLTO requires mold 1.1+") |
| 224 |
elif not isinstance(self.linker, (AppleDynamicLinker, ClangClDynamicLinker, LLVMDynamicLinker, GnuBFDDynamicLinker, GnuGoldDynamicLinker)): |
| 225 |
raise mesonlib.MesonException(f"LLVM's ThinLTO only works with bfd, gold, lld, lld-link, ld64, or mold, not {self.linker.id}") |
| 226 |
args.append(f'-flto={mode}') |
| 227 |
else: |
| 228 |
assert mode == 'default', 'someone forgot to wire something up' |
| 229 |
args.extend(super().get_lto_compile_args(target=target, threads=threads)) |
| 230 |
return args |
| 231 |
|
| 232 |
def linker_to_compiler_args(self, args: T.List[str]) -> T.List[str]: |
| 233 |
if isinstance(self.linker, VisualStudioLikeLinkerMixin): |
| 234 |
return [flag if flag.startswith('-Wl,') or flag.startswith('-fuse-ld=') else f'-Wl,{flag}' for flag in args] |
| 235 |
else: |
| 236 |
return args |
| 237 |
|
| 238 |
def get_lto_link_args(self, *, target: T.Optional[BuildTarget] = None, threads: int = 0, |
| 239 |
mode: str = 'default', thinlto_cache_dir: T.Optional[str] = None) -> T.List[str]: |
| 240 |
args = self.get_lto_compile_args(target=target, threads=threads, mode=mode) |
| 241 |
if mode == 'thin' and thinlto_cache_dir is not None: |
| 242 |
# We check for ThinLTO linker support above in get_lto_compile_args, and all of them support |
| 243 |
# get_thinlto_cache_args as well |
| 244 |
args.extend(self.linker.get_thinlto_cache_args(thinlto_cache_dir)) |
| 245 |
# In clang -flto-jobs=0 means auto, and is the default if unspecified, just like in meson |
| 246 |
if threads > 0: |
| 247 |
if not mesonlib.version_compare(self.version, '>=4.0.0'): |
| 248 |
raise mesonlib.MesonException('clang support for LTO threads requires clang >=4.0') |
| 249 |
args.append(f'-flto-jobs={threads}') |
| 250 |
return args |
| 251 |
|
| 252 |
|
| 253 |
class ClangCStds(CompilerMixinBase): |
| 254 |
|
| 255 |
"""Mixin class for clang based compilers for setting C standards. |
| 256 |
|
| 257 |
This is used by both ClangCCompiler and ClangClCompiler, as they share |
| 258 |
the same versions |
| 259 |
""" |
| 260 |
|
| 261 |
_C17_VERSION = '>=6.0.0' |
| 262 |
_C18_VERSION = '>=8.0.0' |
| 263 |
_C2X_VERSION = '>=9.0.0' |
| 264 |
_C23_VERSION = '>=18.0.0' |
| 265 |
_C2Y_VERSION = '>=19.0.0' |
| 266 |
|
| 267 |
def get_options(self) -> MutableKeyedOptionDictType: |
| 268 |
opts = super().get_options() |
| 269 |
stds = ['c89', 'c99', 'c11'] |
| 270 |
# https://releases.llvm.org/6.0.0/tools/clang/docs/ReleaseNotes.html |
| 271 |
# https://en.wikipedia.org/wiki/Xcode#Latest_versions |
| 272 |
if mesonlib.version_compare(self.version, self._C17_VERSION): |
| 273 |
stds += ['c17'] |
| 274 |
if mesonlib.version_compare(self.version, self._C18_VERSION): |
| 275 |
stds += ['c18'] |
| 276 |
if mesonlib.version_compare(self.version, self._C2X_VERSION): |
| 277 |
stds += ['c2x'] |
| 278 |
if mesonlib.version_compare(self.version, self._C23_VERSION): |
| 279 |
stds += ['c23'] |
| 280 |
if mesonlib.version_compare(self.version, self._C2Y_VERSION): |
| 281 |
stds += ['c2y'] |
| 282 |
key = self.form_compileropt_key('std') |
| 283 |
std_opt = opts[key] |
| 284 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 285 |
std_opt.set_versions(stds, gnu=True) |
| 286 |
return opts |
| 287 |
|
| 288 |
|
| 289 |
class ClangCPPStds(CompilerMixinBase): |
| 290 |
|
| 291 |
"""Mixin class for clang based compilers for setting C++ standards. |
| 292 |
|
| 293 |
This is used by the ClangCPPCompiler |
| 294 |
""" |
| 295 |
|
| 296 |
_CPP23_VERSION = '>=12.0.0' |
| 297 |
_CPP26_VERSION = '>=17.0.0' |
| 298 |
|
| 299 |
def get_options(self) -> MutableKeyedOptionDictType: |
| 300 |
opts = super().get_options() |
| 301 |
stds = [ |
| 302 |
'c++98', 'c++03', 'c++11', 'c++14', 'c++17', 'c++1z', 'c++2a', |
| 303 |
'c++20', |
| 304 |
] |
| 305 |
if mesonlib.version_compare(self.version, self._CPP23_VERSION): |
| 306 |
stds.append('c++23') |
| 307 |
if mesonlib.version_compare(self.version, self._CPP26_VERSION): |
| 308 |
stds.append('c++26') |
| 309 |
key = self.form_compileropt_key('std') |
| 310 |
std_opt = opts[key] |
| 311 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 312 |
std_opt.set_versions(stds, gnu=True) |
| 313 |
return opts |