| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2017 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import functools |
| 7 |
import os.path |
| 8 |
import typing as T |
| 9 |
|
| 10 |
from .. import options |
| 11 |
from .. import mlog |
| 12 |
from ..mesonlib import MesonException, version_compare, lazy_property |
| 13 |
|
| 14 |
from .compilers import ( |
| 15 |
gnu_winlibs, |
| 16 |
msvc_winlibs, |
| 17 |
Compiler, |
| 18 |
CompileCheckMode, |
| 19 |
) |
| 20 |
from .c_function_attributes import CXX_FUNC_ATTRIBUTES, C_FUNC_ATTRIBUTES |
| 21 |
from .mixins.apple import AppleCompilerMixin, AppleCPPStdsMixin |
| 22 |
from .mixins.clike import CLikeCompiler |
| 23 |
from .mixins.ccrx import CcrxCompiler |
| 24 |
from .mixins.ti import TICompiler |
| 25 |
from .mixins.arm import ArmCompiler, ArmclangCompiler |
| 26 |
from .mixins.visualstudio import MSVCCompiler, ClangClCompiler |
| 27 |
from .mixins.gnu import GnuCompiler, GnuCPPStds, gnu_common_warning_args, gnu_cpp_warning_args |
| 28 |
from .mixins.intel import IntelGnuLikeCompiler, IntelLLVMLikeCompiler, IntelVisualStudioLikeCompiler |
| 29 |
from .mixins.clang import ClangCompiler, ClangCPPStds |
| 30 |
from .mixins.elbrus import ElbrusCompiler |
| 31 |
from .mixins.pgi import PGICompiler |
| 32 |
from .mixins.emscripten import EmscriptenMixin |
| 33 |
from .mixins.metrowerks import MetrowerksCompiler |
| 34 |
from .mixins.metrowerks import mwccarm_instruction_set_args, mwcceppc_instruction_set_args |
| 35 |
from .mixins.microchip import Xc32Compiler, Xc32CPPStds |
| 36 |
|
| 37 |
if T.TYPE_CHECKING: |
| 38 |
from ..options import MutableKeyedOptionDictType |
| 39 |
from ..dependencies import Dependency |
| 40 |
from ..environment import Environment |
| 41 |
from ..linkers.linkers import DynamicLinker |
| 42 |
from ..mesonlib import MachineChoice |
| 43 |
from ..build import BuildTarget |
| 44 |
CompilerMixinBase = CLikeCompiler |
| 45 |
else: |
| 46 |
CompilerMixinBase = object |
| 47 |
|
| 48 |
ALL_STDS = ['c++98', 'c++0x', 'c++03', 'c++1y', 'c++1z', 'c++11', 'c++14', 'c++17'] |
| 49 |
ALL_STDS += ['c++2a', 'c++2b', 'c++2c', 'c++20', 'c++23', 'c++26'] |
| 50 |
ALL_STDS += [f'gnu{std[1:]}' for std in ALL_STDS] |
| 51 |
ALL_STDS += ['vc++11', 'vc++14', 'vc++17', 'vc++20', 'vc++23', 'vc++latest', 'c++latest'] |
| 52 |
|
| 53 |
|
| 54 |
def non_msvc_eh_options(eh: str, args: T.List[str]) -> None: |
| 55 |
if eh == 'none': |
| 56 |
args.append('-fno-exceptions') |
| 57 |
elif eh in {'s', 'c'}: |
| 58 |
mlog.warning(f'non-MSVC compilers do not support {eh} exception handling. ' |
| 59 |
'You may want to set eh to \'default\'.', fatal=False) |
| 60 |
|
| 61 |
class CPPCompiler(CLikeCompiler, Compiler): |
| 62 |
def attribute_check_func(self, name: str) -> str: |
| 63 |
try: |
| 64 |
return CXX_FUNC_ATTRIBUTES.get(name, C_FUNC_ATTRIBUTES[name]) |
| 65 |
except KeyError: |
| 66 |
raise MesonException(f'Unknown function attribute "{name}"') |
| 67 |
|
| 68 |
language = 'cpp' |
| 69 |
|
| 70 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 71 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 72 |
full_version: T.Optional[str] = None): |
| 73 |
# If a child ObjCPP class has already set it, don't set it ourselves |
| 74 |
Compiler.__init__(self, ccache, exelist, version, for_machine, env, |
| 75 |
linker=linker, full_version=full_version) |
| 76 |
CLikeCompiler.__init__(self) |
| 77 |
|
| 78 |
@classmethod |
| 79 |
def get_display_language(cls) -> str: |
| 80 |
return 'C++' |
| 81 |
|
| 82 |
def get_no_stdinc_args(self) -> T.List[str]: |
| 83 |
return ['-nostdinc++'] |
| 84 |
|
| 85 |
def get_no_stdlib_link_args(self) -> T.List[str]: |
| 86 |
return ['-nostdlib++'] |
| 87 |
|
| 88 |
def get_cpp_modules_args(self) -> T.List[str]: |
| 89 |
return [] |
| 90 |
|
| 91 |
def _sanity_check_source_code(self) -> str: |
| 92 |
return '#include <stddef.h>\nclass breakCCompiler;int main(void) { return 0; }\n' |
| 93 |
|
| 94 |
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]: |
| 95 |
# -fpermissive allows non-conforming code to compile which is necessary |
| 96 |
# for many C++ checks. Particularly, the has_header_symbol check is |
| 97 |
# too strict without this and always fails. |
| 98 |
return super().get_compiler_check_args(mode) + ['-fpermissive'] |
| 99 |
|
| 100 |
def has_header_symbol(self, hname: str, symbol: str, prefix: str, *, |
| 101 |
extra_args: T.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]] = None, |
| 102 |
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: |
| 103 |
# Check if it's a C-like symbol |
| 104 |
found, cached = super().has_header_symbol(hname, symbol, prefix, |
| 105 |
extra_args=extra_args, |
| 106 |
dependencies=dependencies) |
| 107 |
if found: |
| 108 |
return True, cached |
| 109 |
# Check if it's a class or a template |
| 110 |
if extra_args is None: |
| 111 |
extra_args = [] |
| 112 |
t = f'''{prefix} |
| 113 |
#include <{hname}> |
| 114 |
using {symbol}; |
| 115 |
int main(void) {{ return 0; }}''' |
| 116 |
return self.compiles(t, extra_args=extra_args, |
| 117 |
dependencies=dependencies) |
| 118 |
|
| 119 |
def _test_cpp_std_arg(self, cpp_std_value: str) -> bool: |
| 120 |
# Test whether the compiler understands a -std=XY argument |
| 121 |
assert cpp_std_value.startswith('-std=') |
| 122 |
|
| 123 |
# This test does not use has_multi_arguments() for two reasons: |
| 124 |
# 1. has_multi_arguments() requires an env argument, which the compiler |
| 125 |
# object does not have at this point. |
| 126 |
# 2. even if it did have an env object, that might contain another more |
| 127 |
# recent -std= argument, which might lead to a cascaded failure. |
| 128 |
CPP_TEST = 'int i = static_cast<int>(0);' |
| 129 |
with self.compile(CPP_TEST, extra_args=[cpp_std_value], mode=CompileCheckMode.COMPILE) as p: |
| 130 |
if p.returncode == 0: |
| 131 |
mlog.debug(f'Compiler accepts {cpp_std_value}:', 'YES') |
| 132 |
return True |
| 133 |
else: |
| 134 |
mlog.debug(f'Compiler accepts {cpp_std_value}:', 'NO') |
| 135 |
return False |
| 136 |
|
| 137 |
@functools.lru_cache() |
| 138 |
def _find_best_cpp_std(self, cpp_std: str) -> str: |
| 139 |
# The initial version mapping approach to make falling back |
| 140 |
# from '-std=c++14' to '-std=c++1y' was too brittle. For instance, |
| 141 |
# Apple's Clang uses a different versioning scheme to upstream LLVM, |
| 142 |
# making the whole detection logic awfully brittle. Instead, let's |
| 143 |
# just see if feeding GCC or Clang our '-std=' setting works, and |
| 144 |
# if not, try the fallback argument. |
| 145 |
CPP_FALLBACKS = { |
| 146 |
'c++11': 'c++0x', |
| 147 |
'gnu++11': 'gnu++0x', |
| 148 |
'c++14': 'c++1y', |
| 149 |
'gnu++14': 'gnu++1y', |
| 150 |
'c++17': 'c++1z', |
| 151 |
'gnu++17': 'gnu++1z', |
| 152 |
'c++20': 'c++2a', |
| 153 |
'gnu++20': 'gnu++2a', |
| 154 |
'c++23': 'c++2b', |
| 155 |
'gnu++23': 'gnu++2b', |
| 156 |
'c++26': 'c++2c', |
| 157 |
'gnu++26': 'gnu++2c', |
| 158 |
} |
| 159 |
|
| 160 |
# Currently, remapping is only supported for Clang, Elbrus and GCC |
| 161 |
assert self.id in frozenset(['clang', 'lcc', 'gcc', 'emscripten', 'armltdclang', 'intel-llvm', 'nvidia_hpc', 'xc32-gcc']) |
| 162 |
|
| 163 |
if cpp_std not in CPP_FALLBACKS: |
| 164 |
# 'c++03' and 'c++98' don't have fallback types |
| 165 |
return '-std=' + cpp_std |
| 166 |
|
| 167 |
for i in (cpp_std, CPP_FALLBACKS[cpp_std]): |
| 168 |
cpp_std_value = '-std=' + i |
| 169 |
if self._test_cpp_std_arg(cpp_std_value): |
| 170 |
return cpp_std_value |
| 171 |
|
| 172 |
raise MesonException(f'C++ Compiler does not support -std={cpp_std}') |
| 173 |
|
| 174 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 175 |
opts = super().get_options() |
| 176 |
key = self.form_compileropt_key('std') |
| 177 |
opts.update({ |
| 178 |
key: options.UserStdOption('cpp', ALL_STDS), |
| 179 |
}) |
| 180 |
return opts |
| 181 |
|
| 182 |
|
| 183 |
class _StdCPPLibMixin(CompilerMixinBase): |
| 184 |
|
| 185 |
"""Detect whether to use libc++ or libstdc++.""" |
| 186 |
|
| 187 |
@lazy_property |
| 188 |
def language_stdlib_provider(self) -> str: |
| 189 |
# https://stackoverflow.com/a/31658120 |
| 190 |
header = 'version' if self.has_header('version', '')[0] else 'ciso646' |
| 191 |
is_libcxx = self.has_header_symbol(header, '_LIBCPP_VERSION', '')[0] |
| 192 |
lib = 'c++' if is_libcxx else 'stdc++' |
| 193 |
return lib |
| 194 |
|
| 195 |
@functools.lru_cache(None) |
| 196 |
def language_stdlib_only_link_flags(self) -> T.List[str]: |
| 197 |
"""Detect the C++ stdlib and default search dirs |
| 198 |
|
| 199 |
As an optimization, this method will cache the value, to avoid building the same values over and over |
| 200 |
|
| 201 |
:raises MesonException: If a stdlib cannot be determined |
| 202 |
""" |
| 203 |
|
| 204 |
# We need to apply the search prefix here, as these link arguments may |
| 205 |
# be passed to a different compiler with a different set of default |
| 206 |
# search paths, such as when using Clang for C/C++ and gfortran for |
| 207 |
# fortran. |
| 208 |
search_dirs = [f'-L{d}' for d in self.get_compiler_dirs('libraries')] |
| 209 |
|
| 210 |
lib = self.language_stdlib_provider |
| 211 |
if self.find_library(lib, []) is not None: |
| 212 |
return search_dirs + [f'-l{lib}'] |
| 213 |
|
| 214 |
# TODO: maybe a bug exception? |
| 215 |
raise MesonException('Could not detect either libc++ or libstdc++ as your C++ stdlib implementation.') |
| 216 |
|
| 217 |
|
| 218 |
class ClangCPPCompiler(_StdCPPLibMixin, ClangCPPStds, ClangCompiler, CPPCompiler): |
| 219 |
|
| 220 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 221 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 222 |
defines: T.Optional[T.Dict[str, str]] = None, |
| 223 |
full_version: T.Optional[str] = None): |
| 224 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 225 |
env, linker=linker, full_version=full_version) |
| 226 |
ClangCompiler.__init__(self, defines) |
| 227 |
default_warn_args = ['-Wall', '-Winvalid-pch'] |
| 228 |
self.warn_args = {'0': [], |
| 229 |
'1': default_warn_args, |
| 230 |
'2': default_warn_args + ['-Wextra'], |
| 231 |
'3': default_warn_args + ['-Wextra', '-Wpedantic'], |
| 232 |
'everything': ['-Weverything']} |
| 233 |
|
| 234 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 235 |
opts = super().get_options() |
| 236 |
|
| 237 |
key = self.form_compileropt_key('eh') |
| 238 |
opts[key] = options.UserComboOption( |
| 239 |
self.make_option_name(key), |
| 240 |
'C++ exception handling type.', |
| 241 |
'default', |
| 242 |
choices=['none', 'default', 'a', 's', 'sc']) |
| 243 |
|
| 244 |
key = self.form_compileropt_key('rtti') |
| 245 |
opts[key] = options.UserBooleanOption( |
| 246 |
self.make_option_name(key), |
| 247 |
'Enable RTTI', |
| 248 |
True) |
| 249 |
|
| 250 |
key = self.form_compileropt_key('debugstl') |
| 251 |
opts[key] = options.UserBooleanOption( |
| 252 |
self.make_option_name(key), |
| 253 |
'STL debug mode', |
| 254 |
False) |
| 255 |
|
| 256 |
if self.info.is_windows() or self.info.is_cygwin(): |
| 257 |
key = self.form_compileropt_key('winlibs') |
| 258 |
opts[key] = options.UserStringArrayOption( |
| 259 |
self.make_option_name(key), |
| 260 |
'Standard Win libraries to link against', |
| 261 |
gnu_winlibs) |
| 262 |
return opts |
| 263 |
|
| 264 |
def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 265 |
args: T.List[str] = [] |
| 266 |
|
| 267 |
rtti = self.get_compileropt_value('rtti', target, subproject) |
| 268 |
debugstl = self.get_compileropt_value('debugstl', target, subproject) |
| 269 |
eh = self.get_compileropt_value('eh', target, subproject) |
| 270 |
|
| 271 |
assert isinstance(rtti, bool) |
| 272 |
assert isinstance(eh, str) |
| 273 |
assert isinstance(debugstl, bool) |
| 274 |
|
| 275 |
non_msvc_eh_options(eh, args) |
| 276 |
|
| 277 |
if debugstl: |
| 278 |
args.append('-D_GLIBCXX_DEBUG=1') |
| 279 |
|
| 280 |
# We can't do _LIBCPP_DEBUG because it's unreliable unless libc++ was built with it too: |
| 281 |
# https://discourse.llvm.org/t/building-a-program-with-d-libcpp-debug-1-against-a-libc-that-is-not-itself-built-with-that-define/59176/3 |
| 282 |
# Note that unlike _GLIBCXX_DEBUG, _MODE_DEBUG doesn't break ABI. It's just slow. |
| 283 |
if version_compare(self.version, '>=18'): |
| 284 |
args.append('-U_LIBCPP_HARDENING_MODE') |
| 285 |
args.append('-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG') |
| 286 |
|
| 287 |
if not rtti: |
| 288 |
args.append('-fno-rtti') |
| 289 |
|
| 290 |
return args |
| 291 |
|
| 292 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 293 |
args: T.List[str] = [] |
| 294 |
std = self.get_compileropt_value('std', target, subproject) |
| 295 |
assert isinstance(std, str) |
| 296 |
if std != 'none': |
| 297 |
args.append(self._find_best_cpp_std(std)) |
| 298 |
return args |
| 299 |
|
| 300 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 301 |
if self.info.is_windows() or self.info.is_cygwin(): |
| 302 |
# without a typedict mypy can't understand this. |
| 303 |
retval = self.get_compileropt_value('winlibs', target, subproject) |
| 304 |
assert isinstance(retval, list) |
| 305 |
libs = retval[:] |
| 306 |
for l in libs: |
| 307 |
assert isinstance(l, str) |
| 308 |
return libs |
| 309 |
return [] |
| 310 |
|
| 311 |
def get_assert_args(self, disable: bool) -> T.List[str]: |
| 312 |
if disable: |
| 313 |
return ['-DNDEBUG'] |
| 314 |
|
| 315 |
# Don't inject the macro if the compiler already has it pre-defined. |
| 316 |
for macro in ['_GLIBCXX_ASSERTIONS', '_LIBCPP_HARDENING_MODE', '_LIBCPP_ENABLE_ASSERTIONS']: |
| 317 |
if self.defines.get(macro) is not None: |
| 318 |
return [] |
| 319 |
|
| 320 |
if self.language_stdlib_provider == 'stdc++': |
| 321 |
return ['-D_GLIBCXX_ASSERTIONS=1'] |
| 322 |
|
| 323 |
return ['-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST'] |
| 324 |
|
| 325 |
def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: |
| 326 |
args = super().get_pch_use_args(pch_dir, header) |
| 327 |
if version_compare(self.version, '>=11'): |
| 328 |
return ['-fpch-instantiate-templates'] + args |
| 329 |
return args |
| 330 |
|
| 331 |
def get_cpp_modules_args(self) -> T.List[str]: |
| 332 |
# Although -fmodules-ts is removed in LLVM 17, we keep this in for compatibility with old compilers. |
| 333 |
return ['-fmodules', '-fmodules-ts'] |
| 334 |
|
| 335 |
|
| 336 |
class ArmLtdClangCPPCompiler(ClangCPPCompiler): |
| 337 |
|
| 338 |
id = 'armltdclang' |
| 339 |
|
| 340 |
|
| 341 |
class AppleClangCPPCompiler(AppleCompilerMixin, AppleCPPStdsMixin, ClangCPPCompiler): |
| 342 |
pass |
| 343 |
|
| 344 |
|
| 345 |
class EmscriptenCPPCompiler(EmscriptenMixin, ClangCPPCompiler): |
| 346 |
|
| 347 |
id = 'emscripten' |
| 348 |
|
| 349 |
# Emscripten uses different version numbers than Clang; `emcc -v` will show |
| 350 |
# the Clang version number used as well (but `emcc --version` does not). |
| 351 |
# See https://github.com/pyodide/pyodide/discussions/4762 for more on |
| 352 |
# emcc <--> clang versions. Note, although earlier versions claim to be the |
| 353 |
# Clang versions 12.0.0 and 17.0.0 required for these C++ standards, they |
| 354 |
# only accept the flags in the later versions below. |
| 355 |
_CPP23_VERSION = '>=2.0.10' |
| 356 |
_CPP26_VERSION = '>=3.1.39' |
| 357 |
|
| 358 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 359 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 360 |
defines: T.Optional[T.Dict[str, str]] = None, |
| 361 |
full_version: T.Optional[str] = None): |
| 362 |
if not env.is_cross_build(for_machine): |
| 363 |
raise MesonException('Emscripten compiler can only be used for cross compilation.') |
| 364 |
if not version_compare(version, '>=1.39.19'): |
| 365 |
raise MesonException('Meson requires Emscripten >= 1.39.19') |
| 366 |
ClangCPPCompiler.__init__(self, ccache, exelist, version, for_machine, env, |
| 367 |
linker=linker, defines=defines, full_version=full_version) |
| 368 |
|
| 369 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 370 |
args: T.List[str] = [] |
| 371 |
std = self.get_compileropt_value('std', target, subproject) |
| 372 |
assert isinstance(std, str) |
| 373 |
if std != 'none': |
| 374 |
args.append(self._find_best_cpp_std(std)) |
| 375 |
return args |
| 376 |
|
| 377 |
|
| 378 |
class ArmclangCPPCompiler(ArmclangCompiler, CPPCompiler): |
| 379 |
''' |
| 380 |
Keil armclang |
| 381 |
''' |
| 382 |
|
| 383 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 384 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 385 |
full_version: T.Optional[str] = None): |
| 386 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 387 |
env, linker=linker, full_version=full_version) |
| 388 |
ArmclangCompiler.__init__(self) |
| 389 |
default_warn_args = ['-Wall', '-Winvalid-pch'] |
| 390 |
self.warn_args = {'0': [], |
| 391 |
'1': default_warn_args, |
| 392 |
'2': default_warn_args + ['-Wextra'], |
| 393 |
'3': default_warn_args + ['-Wextra', '-Wpedantic'], |
| 394 |
'everything': ['-Weverything']} |
| 395 |
|
| 396 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 397 |
opts = super().get_options() |
| 398 |
|
| 399 |
key = self.form_compileropt_key('eh') |
| 400 |
opts[key] = options.UserComboOption( |
| 401 |
self.make_option_name(key), |
| 402 |
'C++ exception handling type.', |
| 403 |
'default', |
| 404 |
choices=['none', 'default', 'a', 's', 'sc']) |
| 405 |
|
| 406 |
key = self.form_compileropt_key('std') |
| 407 |
std_opt = opts[key] |
| 408 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 409 |
std_opt.set_versions(['c++98', 'c++03', 'c++11', 'c++14', 'c++17'], gnu=True) |
| 410 |
return opts |
| 411 |
|
| 412 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 413 |
args: T.List[str] = [] |
| 414 |
std = self.get_compileropt_value('std', target, subproject) |
| 415 |
assert isinstance(std, str) |
| 416 |
if std != 'none': |
| 417 |
args.append('-std=' + std) |
| 418 |
|
| 419 |
eh = self.get_compileropt_value('eh', target, subproject) |
| 420 |
assert isinstance(eh, str) |
| 421 |
non_msvc_eh_options(eh, args) |
| 422 |
|
| 423 |
return args |
| 424 |
|
| 425 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 426 |
return [] |
| 427 |
|
| 428 |
|
| 429 |
class GnuCPPCompiler(_StdCPPLibMixin, GnuCPPStds, GnuCompiler, CPPCompiler): |
| 430 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 431 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 432 |
defines: T.Optional[T.Dict[str, str]] = None, |
| 433 |
full_version: T.Optional[str] = None): |
| 434 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 435 |
env, linker=linker, full_version=full_version) |
| 436 |
GnuCompiler.__init__(self, defines) |
| 437 |
default_warn_args = ['-Wall', '-Winvalid-pch'] |
| 438 |
self.warn_args = {'0': [], |
| 439 |
'1': default_warn_args, |
| 440 |
'2': default_warn_args + ['-Wextra'], |
| 441 |
'3': default_warn_args + ['-Wextra', '-Wpedantic'], |
| 442 |
'everything': (default_warn_args + ['-Wextra', '-Wpedantic'] + |
| 443 |
self.supported_warn_args(gnu_common_warning_args) + |
| 444 |
self.supported_warn_args(gnu_cpp_warning_args))} |
| 445 |
|
| 446 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 447 |
opts = super().get_options() |
| 448 |
|
| 449 |
key = self.form_compileropt_key('eh') |
| 450 |
opts[key] = options.UserComboOption( |
| 451 |
self.make_option_name(key), |
| 452 |
'C++ exception handling type.', |
| 453 |
'default', |
| 454 |
choices=['none', 'default', 'a', 's', 'sc']) |
| 455 |
|
| 456 |
key = self.form_compileropt_key('rtti') |
| 457 |
opts[key] = options.UserBooleanOption( |
| 458 |
self.make_option_name(key), |
| 459 |
'Enable RTTI', |
| 460 |
True) |
| 461 |
|
| 462 |
key = self.form_compileropt_key('debugstl') |
| 463 |
opts[key] = options.UserBooleanOption( |
| 464 |
self.make_option_name(key), |
| 465 |
'STL debug mode', |
| 466 |
False) |
| 467 |
|
| 468 |
if self.info.is_windows() or self.info.is_cygwin(): |
| 469 |
key = key.evolve(name='cpp_winlibs') |
| 470 |
opts[key] = options.UserStringArrayOption( |
| 471 |
self.make_option_name(key), |
| 472 |
'Standard Win libraries to link against', |
| 473 |
gnu_winlibs) |
| 474 |
|
| 475 |
if version_compare(self.version, '>=15.1'): |
| 476 |
key = key.evolve(name='cpp_importstd') |
| 477 |
opts[key] = options.UserComboOption(self.make_option_name(key), |
| 478 |
'Use #import std.', |
| 479 |
'false', |
| 480 |
choices=['false', 'true']) |
| 481 |
|
| 482 |
return opts |
| 483 |
|
| 484 |
def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 485 |
args: T.List[str] = [] |
| 486 |
|
| 487 |
rtti = self.get_compileropt_value('rtti', target, subproject) |
| 488 |
debugstl = self.get_compileropt_value('debugstl', target, subproject) |
| 489 |
eh = self.get_compileropt_value('eh', target, subproject) |
| 490 |
|
| 491 |
assert isinstance(rtti, bool) |
| 492 |
assert isinstance(eh, str) |
| 493 |
assert isinstance(debugstl, bool) |
| 494 |
|
| 495 |
non_msvc_eh_options(eh, args) |
| 496 |
|
| 497 |
if not rtti: |
| 498 |
args.append('-fno-rtti') |
| 499 |
|
| 500 |
# We may want to handle libc++'s debugstl mode here too |
| 501 |
if debugstl: |
| 502 |
args.append('-D_GLIBCXX_DEBUG=1') |
| 503 |
return args |
| 504 |
|
| 505 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 506 |
args: T.List[str] = [] |
| 507 |
std = self.get_compileropt_value('std', target, subproject) |
| 508 |
assert isinstance(std, str) |
| 509 |
if std != 'none': |
| 510 |
args.append(self._find_best_cpp_std(std)) |
| 511 |
return args |
| 512 |
|
| 513 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 514 |
if self.info.is_windows() or self.info.is_cygwin(): |
| 515 |
# without a typedict mypy can't understand this. |
| 516 |
retval = self.get_compileropt_value('winlibs', target, subproject) |
| 517 |
assert isinstance(retval, list) |
| 518 |
libs: T.List[str] = retval[:] |
| 519 |
for l in libs: |
| 520 |
assert isinstance(l, str) |
| 521 |
return libs |
| 522 |
return [] |
| 523 |
|
| 524 |
def get_assert_args(self, disable: bool) -> T.List[str]: |
| 525 |
if disable: |
| 526 |
return ['-DNDEBUG'] |
| 527 |
|
| 528 |
# Don't inject the macro if the compiler already has it pre-defined. |
| 529 |
for macro in ['_GLIBCXX_ASSERTIONS', '_LIBCPP_HARDENING_MODE', '_LIBCPP_ENABLE_ASSERTIONS']: |
| 530 |
if self.defines.get(macro) is not None: |
| 531 |
return [] |
| 532 |
|
| 533 |
# For GCC, we can assume that the libstdc++ version is the same as |
| 534 |
# the compiler itself. Anything else isn't supported. |
| 535 |
if self.language_stdlib_provider == 'stdc++': |
| 536 |
return ['-D_GLIBCXX_ASSERTIONS=1'] |
| 537 |
else: |
| 538 |
# One can use -stdlib=libc++ with GCC, it just (as of 2025) requires |
| 539 |
# an experimental configure arg to expose that. libc++ supports "multiple" |
| 540 |
# versions of GCC (only ever one version of GCC per libc++ version), but |
| 541 |
# that is "multiple" for our purposes as we can't assume a mapping. |
| 542 |
if version_compare(self.version, '>=18'): |
| 543 |
return ['-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST'] |
| 544 |
|
| 545 |
return [] |
| 546 |
|
| 547 |
def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: |
| 548 |
return ['-fpch-preprocess', '-include', os.path.basename(header)] |
| 549 |
|
| 550 |
def get_cpp_modules_args(self) -> T.List[str]: |
| 551 |
return ['-fmodules', '-fmodules-ts'] |
| 552 |
|
| 553 |
|
| 554 |
class PGICPPCompiler(PGICompiler, CPPCompiler): |
| 555 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 556 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 557 |
full_version: T.Optional[str] = None): |
| 558 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 559 |
env, linker=linker, full_version=full_version) |
| 560 |
PGICompiler.__init__(self) |
| 561 |
|
| 562 |
|
| 563 |
class NvidiaHPC_CPPCompiler(PGICompiler, CPPCompiler): |
| 564 |
|
| 565 |
id = 'nvidia_hpc' |
| 566 |
|
| 567 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 568 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 569 |
full_version: T.Optional[str] = None): |
| 570 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 571 |
env, linker=linker, full_version=full_version) |
| 572 |
PGICompiler.__init__(self) |
| 573 |
|
| 574 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 575 |
opts = super().get_options() |
| 576 |
cppstd_choices = [ |
| 577 |
'c++98', 'c++03', 'c++11', 'c++14', 'c++17', 'c++20', 'c++23', |
| 578 |
'gnu++98', 'gnu++03', 'gnu++11', 'gnu++14', 'gnu++17', 'gnu++20' |
| 579 |
] |
| 580 |
std_opt = opts[self.form_compileropt_key('std')] |
| 581 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 582 |
std_opt.set_versions(cppstd_choices) |
| 583 |
return opts |
| 584 |
|
| 585 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 586 |
args: T.List[str] = [] |
| 587 |
std = self.get_compileropt_value('std', target, subproject) |
| 588 |
assert isinstance(std, str) |
| 589 |
if std != 'none': |
| 590 |
args.append(self._find_best_cpp_std(std)) |
| 591 |
return args |
| 592 |
|
| 593 |
|
| 594 |
class ElbrusCPPCompiler(ElbrusCompiler, CPPCompiler): |
| 595 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 596 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 597 |
defines: T.Optional[T.Dict[str, str]] = None, |
| 598 |
full_version: T.Optional[str] = None): |
| 599 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 600 |
env, linker=linker, full_version=full_version) |
| 601 |
ElbrusCompiler.__init__(self) |
| 602 |
|
| 603 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 604 |
opts = super().get_options() |
| 605 |
|
| 606 |
key = self.form_compileropt_key('eh') |
| 607 |
opts[key] = options.UserComboOption( |
| 608 |
self.make_option_name(key), |
| 609 |
'C++ exception handling type.', |
| 610 |
'default', |
| 611 |
choices=['none', 'default', 'a', 's', 'sc']) |
| 612 |
|
| 613 |
key = self.form_compileropt_key('debugstl') |
| 614 |
opts[key] = options.UserBooleanOption( |
| 615 |
self.make_option_name(key), |
| 616 |
'STL debug mode', |
| 617 |
False) |
| 618 |
|
| 619 |
cpp_stds = ['c++98'] |
| 620 |
if version_compare(self.version, '>=1.20.00'): |
| 621 |
cpp_stds += ['c++03', 'c++0x', 'c++11'] |
| 622 |
if version_compare(self.version, '>=1.21.00') and version_compare(self.version, '<1.22.00'): |
| 623 |
cpp_stds += ['c++14', 'c++1y'] |
| 624 |
if version_compare(self.version, '>=1.22.00'): |
| 625 |
cpp_stds += ['c++14'] |
| 626 |
if version_compare(self.version, '>=1.23.00'): |
| 627 |
cpp_stds += ['c++1y'] |
| 628 |
if version_compare(self.version, '>=1.24.00'): |
| 629 |
cpp_stds += ['c++1z', 'c++17'] |
| 630 |
if version_compare(self.version, '>=1.25.00'): |
| 631 |
cpp_stds += ['c++2a'] |
| 632 |
if version_compare(self.version, '>=1.26.00'): |
| 633 |
cpp_stds += ['c++20'] |
| 634 |
if version_compare(self.version, '>=1.28.00'): |
| 635 |
cpp_stds += ['c++2b', 'c++23'] |
| 636 |
|
| 637 |
key = self.form_compileropt_key('std') |
| 638 |
std_opt = opts[key] |
| 639 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 640 |
std_opt.set_versions(cpp_stds, gnu=True) |
| 641 |
return opts |
| 642 |
|
| 643 |
# Elbrus C++ compiler does not have lchmod, but there is only linker warning, not compiler error. |
| 644 |
# So we should explicitly fail at this case. |
| 645 |
def has_function(self, funcname: str, prefix: str, *, |
| 646 |
extra_args: T.Optional[T.List[str]] = None, |
| 647 |
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[bool, bool]: |
| 648 |
if funcname == 'lchmod': |
| 649 |
return False, False |
| 650 |
return super().has_function(funcname, prefix, extra_args=extra_args, dependencies=dependencies) |
| 651 |
|
| 652 |
# Elbrus C++ compiler does not support RTTI, so don't check for it. |
| 653 |
def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 654 |
args: T.List[str] = [] |
| 655 |
eh = self.get_compileropt_value('eh', target, subproject) |
| 656 |
assert isinstance(eh, str) |
| 657 |
|
| 658 |
non_msvc_eh_options(eh, args) |
| 659 |
|
| 660 |
debugstl = self.get_compileropt_value('debugstl', target, subproject) |
| 661 |
assert isinstance(debugstl, bool) |
| 662 |
if debugstl: |
| 663 |
args.append('-D_GLIBCXX_DEBUG=1') |
| 664 |
return args |
| 665 |
|
| 666 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 667 |
args: T.List[str] = [] |
| 668 |
std = self.get_compileropt_value('std', target, subproject) |
| 669 |
assert isinstance(std, str) |
| 670 |
if std != 'none': |
| 671 |
args.append(self._find_best_cpp_std(std)) |
| 672 |
return args |
| 673 |
|
| 674 |
|
| 675 |
class IntelCPPCompiler(IntelGnuLikeCompiler, CPPCompiler): |
| 676 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 677 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 678 |
full_version: T.Optional[str] = None): |
| 679 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 680 |
env, linker=linker, full_version=full_version) |
| 681 |
IntelGnuLikeCompiler.__init__(self) |
| 682 |
self.lang_header = 'c++-header' |
| 683 |
default_warn_args = ['-Wall', '-w3', '-Wpch-messages'] |
| 684 |
self.warn_args = {'0': [], |
| 685 |
'1': default_warn_args + ['-diag-disable:remark'], |
| 686 |
'2': default_warn_args + ['-Wextra', '-diag-disable:remark'], |
| 687 |
'3': default_warn_args + ['-Wextra', '-diag-disable:remark'], |
| 688 |
'everything': default_warn_args + ['-Wextra']} |
| 689 |
|
| 690 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 691 |
opts = super().get_options() |
| 692 |
|
| 693 |
key = self.form_compileropt_key('eh') |
| 694 |
opts[key] = options.UserComboOption( |
| 695 |
self.make_option_name(key), |
| 696 |
'C++ exception handling type.', |
| 697 |
'default', |
| 698 |
choices=['none', 'default', 'a', 's', 'sc']) |
| 699 |
|
| 700 |
key = self.form_compileropt_key('rtti') |
| 701 |
opts[key] = options.UserBooleanOption( |
| 702 |
self.make_option_name(key), |
| 703 |
'Enable RTTI', |
| 704 |
True) |
| 705 |
|
| 706 |
key = self.form_compileropt_key('debugstl') |
| 707 |
opts[key] = options.UserBooleanOption( |
| 708 |
self.make_option_name(key), |
| 709 |
'STL debug mode', |
| 710 |
False) |
| 711 |
|
| 712 |
# Every Unix compiler under the sun seems to accept -std=c++03, |
| 713 |
# with the exception of ICC. Instead of preventing the user from |
| 714 |
# globally requesting C++03, we transparently remap it to C++98 |
| 715 |
c_stds = ['c++98', 'c++03'] |
| 716 |
g_stds = ['gnu++98', 'gnu++03'] |
| 717 |
if version_compare(self.version, '>=15.0.0'): |
| 718 |
c_stds += ['c++11', 'c++14'] |
| 719 |
g_stds += ['gnu++11'] |
| 720 |
if version_compare(self.version, '>=16.0.0'): |
| 721 |
c_stds += ['c++17'] |
| 722 |
if version_compare(self.version, '>=17.0.0'): |
| 723 |
g_stds += ['gnu++14'] |
| 724 |
if version_compare(self.version, '>=19.1.0'): |
| 725 |
c_stds += ['c++2a'] |
| 726 |
g_stds += ['gnu++2a'] |
| 727 |
|
| 728 |
self._update_language_stds(opts, c_stds + g_stds) |
| 729 |
return opts |
| 730 |
|
| 731 |
def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 732 |
args: T.List[str] = [] |
| 733 |
|
| 734 |
rtti = self.get_compileropt_value('rtti', target, subproject) |
| 735 |
debugstl = self.get_compileropt_value('debugstl', target, subproject) |
| 736 |
eh = self.get_compileropt_value('eh', target, subproject) |
| 737 |
|
| 738 |
assert isinstance(rtti, bool) |
| 739 |
assert isinstance(eh, str) |
| 740 |
assert isinstance(debugstl, bool) |
| 741 |
|
| 742 |
if eh == 'none': |
| 743 |
args.append('-fno-exceptions') |
| 744 |
if not rtti: |
| 745 |
args.append('-fno-rtti') |
| 746 |
if debugstl: |
| 747 |
args.append('-D_GLIBCXX_DEBUG=1') |
| 748 |
return args |
| 749 |
|
| 750 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 751 |
args: T.List[str] = [] |
| 752 |
std = self.get_compileropt_value('std', target, subproject) |
| 753 |
assert isinstance(std, str) |
| 754 |
if std != 'none': |
| 755 |
remap_cpp03 = { |
| 756 |
'c++03': 'c++98', |
| 757 |
'gnu++03': 'gnu++98' |
| 758 |
} |
| 759 |
args.append('-std=' + remap_cpp03.get(std, std)) |
| 760 |
|
| 761 |
return args |
| 762 |
|
| 763 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 764 |
return [] |
| 765 |
|
| 766 |
|
| 767 |
class IntelLLVMCPPCompiler(IntelLLVMLikeCompiler, ClangCPPCompiler): |
| 768 |
|
| 769 |
id = 'intel-llvm' |
| 770 |
|
| 771 |
|
| 772 |
class VisualStudioLikeCPPCompilerMixin(CompilerMixinBase): |
| 773 |
|
| 774 |
"""Mixin for C++ specific method overrides in MSVC-like compilers.""" |
| 775 |
|
| 776 |
VC_VERSION_MAP = { |
| 777 |
'none': (True, None), |
| 778 |
'vc++11': (True, 11), |
| 779 |
'vc++14': (True, 14), |
| 780 |
'vc++17': (True, 17), |
| 781 |
'vc++20': (True, 20), |
| 782 |
'vc++latest': (True, "latest"), |
| 783 |
'c++11': (False, 11), |
| 784 |
'c++14': (False, 14), |
| 785 |
'c++17': (False, 17), |
| 786 |
'c++20': (False, 20), |
| 787 |
'c++latest': (False, "latest"), |
| 788 |
} |
| 789 |
|
| 790 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 791 |
# need a typeddict for this |
| 792 |
key = self.form_compileropt_key('winlibs').evolve(subproject=subproject) |
| 793 |
if target: |
| 794 |
value = self.environment.coredata.get_option_for_target(target, key) |
| 795 |
else: |
| 796 |
value = self.environment.coredata.optstore.get_value_for(key) |
| 797 |
return T.cast('T.List[str]', value)[:] |
| 798 |
|
| 799 |
def _get_options_impl(self, opts: 'MutableKeyedOptionDictType', cpp_stds: T.List[str]) -> 'MutableKeyedOptionDictType': |
| 800 |
opts = super().get_options() |
| 801 |
|
| 802 |
key = self.form_compileropt_key('eh') |
| 803 |
opts[key] = options.UserComboOption( |
| 804 |
self.make_option_name(key), |
| 805 |
'C++ exception handling type.', |
| 806 |
'default', |
| 807 |
choices=['none', 'default', 'a', 's', 'sc']) |
| 808 |
|
| 809 |
key = self.form_compileropt_key('rtti') |
| 810 |
opts[key] = options.UserBooleanOption( |
| 811 |
self.make_option_name(key), |
| 812 |
'Enable RTTI', |
| 813 |
True) |
| 814 |
|
| 815 |
key = self.form_compileropt_key('winlibs') |
| 816 |
opts[key] = options.UserStringArrayOption( |
| 817 |
self.make_option_name(key), |
| 818 |
'Standard Win libraries to link against', |
| 819 |
msvc_winlibs) |
| 820 |
|
| 821 |
std_opt = opts[self.form_compileropt_key('std')] |
| 822 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 823 |
std_opt.set_versions(cpp_stds) |
| 824 |
|
| 825 |
if version_compare(self.version, '>=19.44.35219'): |
| 826 |
key = self.form_compileropt_key('importstd') |
| 827 |
opts[key] = options.UserComboOption(self.make_option_name(key), |
| 828 |
'Use #import std.', |
| 829 |
'false', |
| 830 |
choices=['false', 'true']) |
| 831 |
return opts |
| 832 |
|
| 833 |
def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 834 |
args: T.List[str] = [] |
| 835 |
|
| 836 |
eh = self.get_compileropt_value('eh', target, subproject) |
| 837 |
rtti = self.get_compileropt_value('rtti', target, subproject) |
| 838 |
|
| 839 |
assert isinstance(rtti, bool) |
| 840 |
assert isinstance(eh, str) |
| 841 |
|
| 842 |
if eh == 'default': |
| 843 |
args.append('/EHsc') |
| 844 |
elif eh == 'none': |
| 845 |
args.append('/EHs-c-') |
| 846 |
else: |
| 847 |
args.append('/EH' + eh) |
| 848 |
|
| 849 |
if not rtti: |
| 850 |
args.append('/GR-') |
| 851 |
|
| 852 |
return args |
| 853 |
|
| 854 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 855 |
args: T.List[str] = [] |
| 856 |
std = self.get_compileropt_value('std', target, subproject) |
| 857 |
assert isinstance(std, str) |
| 858 |
|
| 859 |
permissive, ver = self.VC_VERSION_MAP[std] |
| 860 |
if ver is not None: |
| 861 |
args.append(f'/std:c++{ver}') |
| 862 |
if not permissive: |
| 863 |
args.append('/permissive-') |
| 864 |
return args |
| 865 |
|
| 866 |
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]: |
| 867 |
# XXX: this is a hack because so much GnuLike stuff is in the base CPPCompiler class. |
| 868 |
return Compiler.get_compiler_check_args(self, mode) |
| 869 |
|
| 870 |
class CPP11AsCPP14Mixin(CompilerMixinBase): |
| 871 |
|
| 872 |
"""Mixin class for VisualStudio and ClangCl to replace C++11 std with C++14. |
| 873 |
|
| 874 |
This is a limitation of Clang and MSVC that ICL doesn't share. |
| 875 |
""" |
| 876 |
|
| 877 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 878 |
# Note: there is no explicit flag for supporting C++11; we attempt to do the best we can |
| 879 |
# which means setting the C++ standard version to C++14, in compilers that support it |
| 880 |
# (i.e., after VS2015U3) |
| 881 |
# if one is using anything before that point, one cannot set the standard. |
| 882 |
stdkey = self.form_compileropt_key('std').evolve(subproject=subproject) |
| 883 |
if target is not None: |
| 884 |
std = self.environment.coredata.get_option_for_target(target, stdkey) |
| 885 |
else: |
| 886 |
std = self.environment.coredata.optstore.get_value_for(stdkey) |
| 887 |
if std in {'vc++11', 'c++11'}: |
| 888 |
mlog.warning(self.id, 'does not support C++11;', |
| 889 |
'attempting best effort; setting the standard to C++14', |
| 890 |
once=True, fatal=False) |
| 891 |
original_args = super().get_option_std_args(target, subproject) |
| 892 |
std_mapping = {'/std:c++11': '/std:c++14'} |
| 893 |
processed_args = [std_mapping.get(x, x) for x in original_args] |
| 894 |
return processed_args |
| 895 |
|
| 896 |
|
| 897 |
class VisualStudioCPPCompiler(CPP11AsCPP14Mixin, VisualStudioLikeCPPCompilerMixin, MSVCCompiler, CPPCompiler): |
| 898 |
|
| 899 |
id = 'msvc' |
| 900 |
|
| 901 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 902 |
env: Environment, target: str, |
| 903 |
linker: T.Optional['DynamicLinker'] = None, |
| 904 |
full_version: T.Optional[str] = None): |
| 905 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 906 |
env, linker=linker, full_version=full_version) |
| 907 |
MSVCCompiler.__init__(self, target) |
| 908 |
|
| 909 |
# By default, MSVC has a broken __cplusplus define that pretends to be c++98: |
| 910 |
# https://docs.microsoft.com/en-us/cpp/build/reference/zc-cplusplus?view=msvc-160 |
| 911 |
# Pass the flag to enable a truthful define, if possible. |
| 912 |
if version_compare(self.version, '>= 19.14.26428'): |
| 913 |
self.always_args = self.always_args + ['/Zc:__cplusplus'] |
| 914 |
|
| 915 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 916 |
cpp_stds = ['none', 'c++11', 'vc++11'] |
| 917 |
# Visual Studio 2015 and later |
| 918 |
if version_compare(self.version, '>=19'): |
| 919 |
cpp_stds.extend(['c++14', 'c++latest', 'vc++latest']) |
| 920 |
# Visual Studio 2017 and later |
| 921 |
if version_compare(self.version, '>=19.11'): |
| 922 |
cpp_stds.extend(['vc++14', 'c++17', 'vc++17']) |
| 923 |
if version_compare(self.version, '>=19.29'): |
| 924 |
cpp_stds.extend(['c++20', 'vc++20']) |
| 925 |
return self._get_options_impl(super().get_options(), cpp_stds) |
| 926 |
|
| 927 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 928 |
std = self.get_compileropt_value('std', target, subproject) |
| 929 |
if std != 'none' and version_compare(self.version, '<19.00.24210'): |
| 930 |
mlog.warning('This version of MSVC does not support cpp_std arguments', fatal=False) |
| 931 |
|
| 932 |
args = super().get_option_std_args(target, subproject) |
| 933 |
|
| 934 |
if version_compare(self.version, '<19.11'): |
| 935 |
try: |
| 936 |
i = args.index('/permissive-') |
| 937 |
except ValueError: |
| 938 |
return args |
| 939 |
del args[i] |
| 940 |
return args |
| 941 |
|
| 942 |
def get_cpp_modules_args(self) -> T.List[str]: |
| 943 |
return ['/interface'] |
| 944 |
|
| 945 |
class ClangClCPPCompiler(VisualStudioLikeCPPCompilerMixin, ClangClCompiler, CPPCompiler): |
| 946 |
|
| 947 |
id = 'clang-cl' |
| 948 |
|
| 949 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 950 |
env: Environment, target: str, |
| 951 |
linker: T.Optional['DynamicLinker'] = None, |
| 952 |
full_version: T.Optional[str] = None): |
| 953 |
CPPCompiler.__init__(self, [], exelist, version, for_machine, |
| 954 |
env, linker=linker, full_version=full_version) |
| 955 |
ClangClCompiler.__init__(self, target) |
| 956 |
|
| 957 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 958 |
cpp_stds = list(self.VC_VERSION_MAP) + ['c++23', 'vc++23'] |
| 959 |
return self._get_options_impl(super().get_options(), cpp_stds) |
| 960 |
|
| 961 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 962 |
std = self.get_compileropt_value('std', target, subproject) |
| 963 |
assert isinstance(std, str) |
| 964 |
if std == 'none': |
| 965 |
return [] |
| 966 |
# c++latest and vc++latest have no /clang:-std= equivalent |
| 967 |
if std in {'c++latest', 'vc++latest'}: |
| 968 |
args = ['/std:c++latest'] |
| 969 |
if std == 'c++latest': |
| 970 |
args.append('/permissive-') |
| 971 |
return args |
| 972 |
# vc++ variants: permissive mode, strip 'vc++' prefix to get clang std name |
| 973 |
if std.startswith('vc++'): |
| 974 |
return [f'/clang:-std=c++{std[4:]}'] |
| 975 |
# c++ variants: strict conformance mode |
| 976 |
return [f'/clang:-std={std}', '/permissive-'] |
| 977 |
|
| 978 |
def get_cpp_modules_args(self) -> T.List[str]: |
| 979 |
# clang-cl does not support /interface. |
| 980 |
return ['-fmodules', '-fmodules-ts'] |
| 981 |
|
| 982 |
|
| 983 |
class IntelClCPPCompiler(VisualStudioLikeCPPCompilerMixin, IntelVisualStudioLikeCompiler, CPPCompiler): |
| 984 |
|
| 985 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 986 |
env: Environment, target: str, |
| 987 |
linker: T.Optional['DynamicLinker'] = None, |
| 988 |
full_version: T.Optional[str] = None): |
| 989 |
CPPCompiler.__init__(self, [], exelist, version, for_machine, |
| 990 |
env, linker=linker, full_version=full_version) |
| 991 |
IntelVisualStudioLikeCompiler.__init__(self, target) |
| 992 |
|
| 993 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 994 |
# This has only been tested with version 19.0, 2021.2.1, 2024.4.2 and 2025.0.1 |
| 995 |
if version_compare(self.version, '<2021.1.0'): |
| 996 |
cpp_stds = ['none', 'c++11', 'vc++11', 'c++14', 'vc++14', 'c++17', 'vc++17', 'c++latest'] |
| 997 |
else: |
| 998 |
cpp_stds = ['none', 'c++14', 'c++17', 'c++latest'] |
| 999 |
if version_compare(self.version, '>=2024.1.0'): |
| 1000 |
cpp_stds += ['c++20'] |
| 1001 |
return self._get_options_impl(super().get_options(), cpp_stds) |
| 1002 |
|
| 1003 |
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]: |
| 1004 |
# XXX: this is a hack because so much GnuLike stuff is in the base CPPCompiler class. |
| 1005 |
return IntelVisualStudioLikeCompiler.get_compiler_check_args(self, mode) |
| 1006 |
|
| 1007 |
|
| 1008 |
class IntelLLVMClCPPCompiler(IntelClCPPCompiler): |
| 1009 |
|
| 1010 |
id = 'intel-llvm-cl' |
| 1011 |
|
| 1012 |
|
| 1013 |
class ArmCPPCompiler(ArmCompiler, CPPCompiler): |
| 1014 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 1015 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 1016 |
full_version: T.Optional[str] = None): |
| 1017 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 1018 |
env, linker=linker, full_version=full_version) |
| 1019 |
ArmCompiler.__init__(self) |
| 1020 |
|
| 1021 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 1022 |
opts = super().get_options() |
| 1023 |
std_opt = self.form_compileropt_key('std') |
| 1024 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 1025 |
std_opt.set_versions(['c++03', 'c++11']) |
| 1026 |
return opts |
| 1027 |
|
| 1028 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 1029 |
args: T.List[str] = [] |
| 1030 |
std = self.get_compileropt_value('std', target, subproject) |
| 1031 |
assert isinstance(std, str) |
| 1032 |
if std == 'c++11': |
| 1033 |
args.append('--cpp11') |
| 1034 |
elif std == 'c++03': |
| 1035 |
args.append('--cpp') |
| 1036 |
return args |
| 1037 |
|
| 1038 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 1039 |
return [] |
| 1040 |
|
| 1041 |
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]: |
| 1042 |
return [] |
| 1043 |
|
| 1044 |
|
| 1045 |
class CcrxCPPCompiler(CcrxCompiler, CPPCompiler): |
| 1046 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 1047 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 1048 |
full_version: T.Optional[str] = None): |
| 1049 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 1050 |
env, linker=linker, full_version=full_version) |
| 1051 |
CcrxCompiler.__init__(self) |
| 1052 |
|
| 1053 |
# Override CCompiler.get_always_args |
| 1054 |
def get_always_args(self) -> T.List[str]: |
| 1055 |
return ['-nologo', '-lang=cpp'] |
| 1056 |
|
| 1057 |
def get_compile_only_args(self) -> T.List[str]: |
| 1058 |
return [] |
| 1059 |
|
| 1060 |
def get_output_args(self, outputname: str) -> T.List[str]: |
| 1061 |
return [f'-output=obj={outputname}'] |
| 1062 |
|
| 1063 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 1064 |
return [] |
| 1065 |
|
| 1066 |
def get_compiler_check_args(self, mode: CompileCheckMode) -> T.List[str]: |
| 1067 |
return [] |
| 1068 |
|
| 1069 |
class TICPPCompiler(TICompiler, CPPCompiler): |
| 1070 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 1071 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 1072 |
full_version: T.Optional[str] = None): |
| 1073 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 1074 |
env, linker=linker, full_version=full_version) |
| 1075 |
TICompiler.__init__(self) |
| 1076 |
|
| 1077 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 1078 |
opts = super().get_options() |
| 1079 |
key = self.form_compileropt_key('std') |
| 1080 |
std_opt = opts[key] |
| 1081 |
assert isinstance(std_opt, options.UserStdOption), 'for mypy' |
| 1082 |
std_opt.set_versions(['c++03']) |
| 1083 |
return opts |
| 1084 |
|
| 1085 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 1086 |
args: T.List[str] = [] |
| 1087 |
std = self.get_compileropt_value('std', target, subproject) |
| 1088 |
assert isinstance(std, str) |
| 1089 |
if std != 'none': |
| 1090 |
args.append('--' + std) |
| 1091 |
return args |
| 1092 |
|
| 1093 |
def get_always_args(self) -> T.List[str]: |
| 1094 |
return [] |
| 1095 |
|
| 1096 |
def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 1097 |
return [] |
| 1098 |
|
| 1099 |
class C2000CPPCompiler(TICPPCompiler): |
| 1100 |
# Required for backwards compat with projects created before ti-cgt support existed |
| 1101 |
id = 'c2000' |
| 1102 |
|
| 1103 |
class C6000CPPCompiler(TICPPCompiler): |
| 1104 |
id = 'c6000' |
| 1105 |
|
| 1106 |
class MetrowerksCPPCompilerARM(MetrowerksCompiler, CPPCompiler): |
| 1107 |
id = 'mwccarm' |
| 1108 |
|
| 1109 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 1110 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 1111 |
full_version: T.Optional[str] = None): |
| 1112 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 1113 |
env, linker=linker, full_version=full_version) |
| 1114 |
MetrowerksCompiler.__init__(self) |
| 1115 |
|
| 1116 |
def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[str]]: |
| 1117 |
return mwccarm_instruction_set_args.get(instruction_set, None) |
| 1118 |
|
| 1119 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 1120 |
opts = super().get_options() |
| 1121 |
self._update_language_stds(opts, []) |
| 1122 |
return opts |
| 1123 |
|
| 1124 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 1125 |
args: T.List[str] = [] |
| 1126 |
std = self.get_compileropt_value('std', target, subproject) |
| 1127 |
assert isinstance(std, str) |
| 1128 |
if std != 'none': |
| 1129 |
args.append('-lang') |
| 1130 |
args.append(std) |
| 1131 |
return args |
| 1132 |
|
| 1133 |
class MetrowerksCPPCompilerEmbeddedPowerPC(MetrowerksCompiler, CPPCompiler): |
| 1134 |
id = 'mwcceppc' |
| 1135 |
|
| 1136 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 1137 |
env: Environment, linker: T.Optional['DynamicLinker'] = None, |
| 1138 |
full_version: T.Optional[str] = None): |
| 1139 |
CPPCompiler.__init__(self, ccache, exelist, version, for_machine, |
| 1140 |
env, linker=linker, full_version=full_version) |
| 1141 |
MetrowerksCompiler.__init__(self) |
| 1142 |
|
| 1143 |
def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[str]]: |
| 1144 |
return mwcceppc_instruction_set_args.get(instruction_set, None) |
| 1145 |
|
| 1146 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 1147 |
opts = super().get_options() |
| 1148 |
self._update_language_stds(opts, []) |
| 1149 |
return opts |
| 1150 |
|
| 1151 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 1152 |
args: T.List[str] = [] |
| 1153 |
std = self.get_compileropt_value('std', target, subproject) |
| 1154 |
assert isinstance(std, str) |
| 1155 |
if std != 'none': |
| 1156 |
args.append('-lang ' + std) |
| 1157 |
return args |
| 1158 |
|
| 1159 |
|
| 1160 |
class Xc32CPPCompiler(Xc32CPPStds, Xc32Compiler, GnuCPPCompiler): |
| 1161 |
|
| 1162 |
"""Microchip XC32 C++ compiler.""" |
| 1163 |
|
| 1164 |
def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 1165 |
env: Environment, linker: T.Optional[DynamicLinker] = None, |
| 1166 |
defines: T.Optional[T.Dict[str, str]] = None, |
| 1167 |
full_version: T.Optional[str] = None): |
| 1168 |
GnuCPPCompiler.__init__(self, ccache, exelist, version, for_machine, env, |
| 1169 |
linker=linker, full_version=full_version, defines=defines) |
| 1170 |
Xc32Compiler.__init__(self) |