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, ClangClDynamicLinker, LLVMDynamicLinker, GnuGoldDynamicLinker
23from ...mesonlib import OptionKey
24from ..compilers import CompileCheckMode
25from .gnu import GnuLikeCompiler
26
27if T.TYPE_CHECKING:
28    from ...environment import Environment
29    from ...dependencies import Dependency  # noqa: F401
30
31clang_color_args = {
32    'auto': ['-fcolor-diagnostics'],
33    'always': ['-fcolor-diagnostics'],
34    'never': ['-fno-color-diagnostics'],
35}  # type: T.Dict[str, T.List[str]]
36
37clang_optimization_args = {
38    '0': ['-O0'],
39    'g': ['-Og'],
40    '1': ['-O1'],
41    '2': ['-O2'],
42    '3': ['-O3'],
43    's': ['-Oz'],
44}  # type: T.Dict[str, T.List[str]]
45
46class ClangCompiler(GnuLikeCompiler):
47
48    def __init__(self, defines: T.Optional[T.Dict[str, str]]):
49        super().__init__()
50        self.id = 'clang'
51        self.defines = defines or {}
52        self.base_options.update(
53            {OptionKey('b_colorout'), OptionKey('b_lto_threads'), OptionKey('b_lto_mode')})
54
55        # TODO: this really should be part of the linker base_options, but
56        # linkers don't have base_options.
57        if isinstance(self.linker, AppleDynamicLinker):
58            self.base_options.add(OptionKey('b_bitcode'))
59        # All Clang backends can also do LLVM IR
60        self.can_compile_suffixes.add('ll')
61
62    def get_colorout_args(self, colortype: str) -> T.List[str]:
63        return clang_color_args[colortype][:]
64
65    def has_builtin_define(self, define: str) -> bool:
66        return define in self.defines
67
68    def get_builtin_define(self, define: str) -> T.Optional[str]:
69        return self.defines.get(define)
70
71    def get_optimization_args(self, optimization_level: str) -> T.List[str]:
72        return clang_optimization_args[optimization_level]
73
74    def get_pch_suffix(self) -> str:
75        return 'pch'
76
77    def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
78        # Workaround for Clang bug http://llvm.org/bugs/show_bug.cgi?id=15136
79        # This flag is internal to Clang (or at least not documented on the man page)
80        # so it might change semantics at any time.
81        return ['-include-pch', os.path.join(pch_dir, self.get_pch_name(header))]
82
83    def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]:
84        # Clang is different than GCC, it will return True when a symbol isn't
85        # defined in a header. Specifically this seems ot have something to do
86        # with functions that may be in a header on some systems, but not all of
87        # them. `strlcat` specifically with can trigger this.
88        myargs: T.List[str] = ['-Werror=implicit-function-declaration']
89        if mode is CompileCheckMode.COMPILE:
90            myargs.extend(['-Werror=unknown-warning-option', '-Werror=unused-command-line-argument'])
91            if mesonlib.version_compare(self.version, '>=3.6.0'):
92                myargs.append('-Werror=ignored-optimization-argument')
93        return super().get_compiler_check_args(mode) + myargs
94
95    def has_function(self, funcname: str, prefix: str, env: 'Environment', *,
96                     extra_args: T.Optional[T.List[str]] = None,
97                     dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]:
98        if extra_args is None:
99            extra_args = []
100        # Starting with XCode 8, we need to pass this to force linker
101        # visibility to obey OS X/iOS/tvOS minimum version targets with
102        # -mmacosx-version-min, -miphoneos-version-min, -mtvos-version-min etc.
103        # https://github.com/Homebrew/homebrew-core/issues/3727
104        # TODO: this really should be communicated by the linker
105        if isinstance(self.linker, AppleDynamicLinker) and mesonlib.version_compare(self.version, '>=8.0'):
106            extra_args.append('-Wl,-no_weak_imports')
107        return super().has_function(funcname, prefix, env, extra_args=extra_args,
108                                   dependencies=dependencies)
109
110    def openmp_flags(self) -> T.List[str]:
111        if mesonlib.version_compare(self.version, '>=3.8.0'):
112            return ['-fopenmp']
113        elif mesonlib.version_compare(self.version, '>=3.7.0'):
114            return ['-fopenmp=libomp']
115        else:
116            # Shouldn't work, but it'll be checked explicitly in the OpenMP dependency.
117            return []
118
119    @classmethod
120    def use_linker_args(cls, linker: str) -> T.List[str]:
121        # Clang additionally can use a linker specified as a path, which GCC
122        # (and other gcc-like compilers) cannot. This is becuse clang (being
123        # llvm based) is retargetable, while GCC is not.
124        #
125
126        # qcld: Qualcomm Snapdragon linker, based on LLVM
127        if linker == 'qcld':
128            return  ['-fuse-ld=qcld']
129
130        if shutil.which(linker):
131            if not shutil.which(linker):
132                raise mesonlib.MesonException(
133                    f'Cannot find linker {linker}.')
134            return [f'-fuse-ld={linker}']
135        return super().use_linker_args(linker)
136
137    def get_has_func_attribute_extra_args(self, name: str) -> T.List[str]:
138        # Clang only warns about unknown or ignored attributes, so force an
139        # error.
140        return ['-Werror=attributes']
141
142    def get_coverage_link_args(self) -> T.List[str]:
143        return ['--coverage']
144
145    def get_lto_compile_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]:
146        args: T.List[str] = []
147        if mode == 'thin':
148            # Thin LTO requires the use of gold, lld, ld64, or lld-link
149            if not isinstance(self.linker, (AppleDynamicLinker, ClangClDynamicLinker, LLVMDynamicLinker, GnuGoldDynamicLinker)):
150                raise mesonlib.MesonException(f"LLVM's thinLTO only works with gnu gold, lld, lld-link, and ld64, not {self.linker.id}")
151            args.append(f'-flto={mode}')
152        else:
153            assert mode == 'default', 'someone forgot to wire something up'
154            args.extend(super().get_lto_compile_args(threads=threads))
155        return args
156
157    def get_lto_link_args(self, *, threads: int = 0, mode: str = 'default') -> T.List[str]:
158        args = self.get_lto_compile_args(threads=threads, mode=mode)
159        # In clang -flto=0 means auto
160        if threads >= 0:
161            args.append(f'-flto-jobs={threads}')
162        return args
163