| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2022 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import os.path |
| 7 |
import re |
| 8 |
import typing as T |
| 9 |
|
| 10 |
from .. import mesonlib |
| 11 |
from ..arglist import CompilerArgs |
| 12 |
from ..linkers import RSPFileSyntax |
| 13 |
from ..mesonlib import ( |
| 14 |
EnvironmentException, MesonBugException, version_compare, is_windows |
| 15 |
) |
| 16 |
from ..options import OptionKey |
| 17 |
|
| 18 |
from .compilers import ( |
| 19 |
clike_debug_args, |
| 20 |
Compiler, |
| 21 |
CompileCheckMode, |
| 22 |
SimplePrefixLinkerOptionStyle, |
| 23 |
) |
| 24 |
from .mixins.gnu import GnuCompiler |
| 25 |
from .mixins.gnu import gnu_common_warning_args |
| 26 |
|
| 27 |
if T.TYPE_CHECKING: |
| 28 |
from . import compilers |
| 29 |
from ..build import DFeatures |
| 30 |
from ..dependencies import Dependency |
| 31 |
from ..envconfig import MachineInfo |
| 32 |
from ..environment import Environment |
| 33 |
from ..linkers.linkers import DynamicLinker |
| 34 |
from ..mesonlib import MachineChoice |
| 35 |
|
| 36 |
CompilerMixinBase = Compiler |
| 37 |
else: |
| 38 |
CompilerMixinBase = object |
| 39 |
|
| 40 |
d_feature_args: T.Dict[str, T.Dict[str, str]] = { |
| 41 |
'gcc': { |
| 42 |
'unittest': '-funittest', |
| 43 |
'debug': '-fdebug', |
| 44 |
'version': '-fversion', |
| 45 |
'import_dir': '-J' |
| 46 |
}, |
| 47 |
'llvm': { |
| 48 |
'unittest': '-unittest', |
| 49 |
'debug': '-d-debug', |
| 50 |
'version': '-d-version', |
| 51 |
'import_dir': '-J' |
| 52 |
}, |
| 53 |
'dmd': { |
| 54 |
'unittest': '-unittest', |
| 55 |
'debug': '-debug', |
| 56 |
'version': '-version', |
| 57 |
'import_dir': '-J' |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
ldc_optimization_args: T.Dict[str, T.List[str]] = { |
| 62 |
'plain': [], |
| 63 |
'0': [], |
| 64 |
'g': [], |
| 65 |
'1': ['-O1'], |
| 66 |
'2': ['-O2', '-enable-inlining', '-Hkeep-all-bodies'], |
| 67 |
'3': ['-O3', '-enable-inlining', '-Hkeep-all-bodies'], |
| 68 |
's': ['-Oz'], |
| 69 |
} |
| 70 |
|
| 71 |
dmd_optimization_args: T.Dict[str, T.List[str]] = { |
| 72 |
'plain': [], |
| 73 |
'0': [], |
| 74 |
'g': [], |
| 75 |
'1': ['-O'], |
| 76 |
'2': ['-O', '-inline'], |
| 77 |
'3': ['-O', '-inline'], |
| 78 |
's': ['-O'], |
| 79 |
} |
| 80 |
|
| 81 |
gdc_optimization_args: T.Dict[str, T.List[str]] = { |
| 82 |
'plain': [], |
| 83 |
'0': ['-O0'], |
| 84 |
'g': ['-Og'], |
| 85 |
'1': ['-O1'], |
| 86 |
'2': ['-O2', '-finline-functions'], |
| 87 |
'3': ['-O3', '-finline-functions'], |
| 88 |
's': ['-Os'], |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
class DmdLikeCompilerMixin(CompilerMixinBase): |
| 93 |
|
| 94 |
"""Mixin class for DMD and LDC. |
| 95 |
|
| 96 |
LDC has a number of DMD like arguments, and this class allows for code |
| 97 |
sharing between them as makes sense. |
| 98 |
""" |
| 99 |
|
| 100 |
def __init__(self, dmd_frontend_version: T.Optional[str]): |
| 101 |
if dmd_frontend_version is None: |
| 102 |
self._dmd_has_depfile = False |
| 103 |
else: |
| 104 |
# -makedeps switch introduced in 2.095 frontend |
| 105 |
self._dmd_has_depfile = version_compare(dmd_frontend_version, ">=2.095.0") |
| 106 |
|
| 107 |
if T.TYPE_CHECKING: |
| 108 |
mscrt_args: T.Dict[str, T.List[str]] = {} |
| 109 |
|
| 110 |
def _get_target_arch_args(self) -> T.List[str]: ... |
| 111 |
|
| 112 |
LINKER_OPTION_STYLE = SimplePrefixLinkerOptionStyle('-L=') |
| 113 |
|
| 114 |
def get_output_args(self, outputname: str) -> T.List[str]: |
| 115 |
return ['-of=' + outputname] |
| 116 |
|
| 117 |
def get_linker_output_args(self, outputname: str) -> T.List[str]: |
| 118 |
return ['-of=' + outputname] |
| 119 |
|
| 120 |
def get_include_args(self, path: str, is_system: bool) -> T.List[str]: |
| 121 |
if path == "": |
| 122 |
path = "." |
| 123 |
return ['-I=' + path] |
| 124 |
|
| 125 |
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], |
| 126 |
build_dir: str) -> T.List[str]: |
| 127 |
for idx, i in enumerate(parameter_list): |
| 128 |
if i[:3] == '-I=': |
| 129 |
parameter_list[idx] = i[:3] + os.path.normpath(os.path.join(build_dir, i[3:])) |
| 130 |
if i[:4] == '-L-L': |
| 131 |
parameter_list[idx] = i[:4] + os.path.normpath(os.path.join(build_dir, i[4:])) |
| 132 |
if i[:5] == '-L=-L': |
| 133 |
parameter_list[idx] = i[:5] + os.path.normpath(os.path.join(build_dir, i[5:])) |
| 134 |
if i[:6] == '-Wl,-L': |
| 135 |
parameter_list[idx] = i[:6] + os.path.normpath(os.path.join(build_dir, i[6:])) |
| 136 |
|
| 137 |
return parameter_list |
| 138 |
|
| 139 |
def get_warn_args(self, level: str) -> T.List[str]: |
| 140 |
return ['-wi'] |
| 141 |
|
| 142 |
def get_werror_args(self) -> T.List[str]: |
| 143 |
return ['-w'] |
| 144 |
|
| 145 |
def get_coverage_args(self) -> T.List[str]: |
| 146 |
return ['-cov'] |
| 147 |
|
| 148 |
def get_coverage_link_args(self) -> T.List[str]: |
| 149 |
return [] |
| 150 |
|
| 151 |
def get_preprocess_only_args(self) -> T.List[str]: |
| 152 |
return ['-E'] |
| 153 |
|
| 154 |
def get_compile_only_args(self) -> T.List[str]: |
| 155 |
return ['-c'] |
| 156 |
|
| 157 |
def get_depfile_suffix(self) -> str: |
| 158 |
return 'deps' |
| 159 |
|
| 160 |
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: |
| 161 |
if self._dmd_has_depfile: |
| 162 |
return [f'-makedeps={outfile}'] |
| 163 |
return [] |
| 164 |
|
| 165 |
def get_pic_args(self) -> T.List[str]: |
| 166 |
if self.info.is_windows(): |
| 167 |
return [] |
| 168 |
return ['-fPIC'] |
| 169 |
|
| 170 |
def get_optimization_link_args(self, optimization_level: str) -> T.List[str]: |
| 171 |
if optimization_level != 'plain': |
| 172 |
return self._get_target_arch_args() |
| 173 |
return [] |
| 174 |
|
| 175 |
def gen_import_library_args(self, implibname: str) -> T.List[str]: |
| 176 |
return self.linker.import_library_args(implibname) |
| 177 |
|
| 178 |
@classmethod |
| 179 |
def _translate_args_to_nongnu(cls, args: T.List[str], info: MachineInfo, link_id: str) -> T.List[str]: |
| 180 |
# Translate common arguments to flags the LDC/DMD compilers |
| 181 |
# can understand. |
| 182 |
# The flags might have been added by pkg-config files, |
| 183 |
# and are therefore out of the user's control. |
| 184 |
dcargs: T.List[str] = [] |
| 185 |
# whether we hit a linker argument that expect another arg |
| 186 |
# see the comment in the "-L" section |
| 187 |
link_expect_arg = False |
| 188 |
link_flags_with_arg = [ |
| 189 |
'-rpath', '-rpath-link', '-soname', '-compatibility_version', '-current_version', |
| 190 |
] |
| 191 |
for arg in args: |
| 192 |
# Translate OS specific arguments first. |
| 193 |
osargs: T.List[str] = [] |
| 194 |
if info.is_windows(): |
| 195 |
osargs = cls.translate_arg_to_windows(arg) |
| 196 |
elif info.is_darwin(): |
| 197 |
osargs = cls._translate_arg_to_osx(arg) |
| 198 |
if osargs: |
| 199 |
dcargs.extend(osargs) |
| 200 |
continue |
| 201 |
|
| 202 |
# Translate common D arguments here. |
| 203 |
if arg == '-pthread': |
| 204 |
continue |
| 205 |
if arg.startswith('-fstack-protector'): |
| 206 |
continue |
| 207 |
if arg.startswith('-D') and not (arg == '-D' or arg.startswith(('-Dd', '-Df'))): |
| 208 |
# ignore all '-D*' flags (like '-D_THREAD_SAFE') |
| 209 |
# unless they are related to documentation |
| 210 |
continue |
| 211 |
if arg.startswith('-Wl,'): |
| 212 |
# Translate linker arguments here. |
| 213 |
linkargs = arg[arg.index(',') + 1:].split(',') |
| 214 |
for la in linkargs: |
| 215 |
dcargs.append('-L=' + la.strip()) |
| 216 |
continue |
| 217 |
elif arg.startswith(('-link-defaultlib', '-linker', '-link-internally', '-linkonce-templates', '-lib')): |
| 218 |
# these are special arguments to the LDC linker call, |
| 219 |
# arguments like "-link-defaultlib-shared" do *not* |
| 220 |
# denote a library to be linked, but change the default |
| 221 |
# Phobos/DRuntime linking behavior, while "-linker" sets the |
| 222 |
# default linker. |
| 223 |
dcargs.append(arg) |
| 224 |
continue |
| 225 |
elif arg.startswith('-l'): |
| 226 |
# translate library link flag |
| 227 |
dcargs.append('-L=' + arg) |
| 228 |
continue |
| 229 |
elif arg.startswith('-isystem'): |
| 230 |
# translate -isystem system include path |
| 231 |
# this flag might sometimes be added by C library Cflags via |
| 232 |
# pkg-config. |
| 233 |
# NOTE: -isystem and -I are not 100% equivalent, so this is just |
| 234 |
# a workaround for the most common cases. |
| 235 |
if arg.startswith('-isystem='): |
| 236 |
dcargs.append('-I=' + arg[9:]) |
| 237 |
else: |
| 238 |
dcargs.append('-I' + arg[8:]) |
| 239 |
continue |
| 240 |
elif arg.startswith('-idirafter'): |
| 241 |
# same as -isystem, but appends the path instead |
| 242 |
if arg.startswith('-idirafter='): |
| 243 |
dcargs.append('-I=' + arg[11:]) |
| 244 |
else: |
| 245 |
dcargs.append('-I' + arg[10:]) |
| 246 |
continue |
| 247 |
elif arg.startswith('-L'): |
| 248 |
# The D linker expect library search paths in the form of -L=-L/path (the '=' is optional). |
| 249 |
# |
| 250 |
# This function receives a mix of arguments already prepended |
| 251 |
# with -L for the D linker driver and other linker arguments. |
| 252 |
# The arguments starting with -L can be: |
| 253 |
# - library search path (with or without a second -L) |
| 254 |
# - it can come from pkg-config (a single -L) |
| 255 |
# - or from the user passing linker flags (-L-L would be expected) |
| 256 |
# - arguments like "-L=-rpath" that expect a second argument (also prepended with -L) |
| 257 |
# - arguments like "-L=@rpath/xxx" without a second argument (on Apple platform) |
| 258 |
# - arguments like "-L=/SUBSYSTEM:CONSOLE (for Windows linker) |
| 259 |
# |
| 260 |
# The logic that follows tries to detect all these cases (some may be missing) |
| 261 |
# in order to prepend a -L only for the library search paths with a single -L |
| 262 |
|
| 263 |
if arg.startswith('-L='): |
| 264 |
suffix = arg[3:] |
| 265 |
else: |
| 266 |
suffix = arg[2:] |
| 267 |
|
| 268 |
if link_expect_arg: |
| 269 |
# flags like rpath and soname expect a path or filename respectively, |
| 270 |
# we must not alter it (i.e. prefixing with -L for a lib search path) |
| 271 |
dcargs.append(arg) |
| 272 |
link_expect_arg = False |
| 273 |
continue |
| 274 |
|
| 275 |
if suffix in link_flags_with_arg: |
| 276 |
link_expect_arg = True |
| 277 |
|
| 278 |
if suffix.startswith('-') or suffix.startswith('@'): |
| 279 |
# this is not search path |
| 280 |
dcargs.append(arg) |
| 281 |
continue |
| 282 |
|
| 283 |
# linker flag such as -L=/DEBUG must pass through |
| 284 |
if info.is_windows() and link_id == 'link' and suffix.startswith('/'): |
| 285 |
dcargs.append(arg) |
| 286 |
continue |
| 287 |
|
| 288 |
# Make sure static library files are passed properly to the linker. |
| 289 |
if arg.endswith('.a') or arg.endswith('.lib'): |
| 290 |
if len(suffix) > 0 and not suffix.startswith('-'): |
| 291 |
dcargs.append('-L=' + suffix) |
| 292 |
continue |
| 293 |
|
| 294 |
dcargs.append('-L=' + arg) |
| 295 |
continue |
| 296 |
elif not arg.startswith('-') and arg.endswith(('.a', '.lib')): |
| 297 |
# ensure static libraries are passed through to the linker |
| 298 |
dcargs.append('-L=' + arg) |
| 299 |
continue |
| 300 |
else: |
| 301 |
dcargs.append(arg) |
| 302 |
|
| 303 |
return dcargs |
| 304 |
|
| 305 |
@classmethod |
| 306 |
def translate_arg_to_windows(cls, arg: str) -> T.List[str]: |
| 307 |
args: T.List[str] = [] |
| 308 |
if arg.startswith('-Wl,'): |
| 309 |
# Translate linker arguments here. |
| 310 |
linkargs = arg[arg.index(',') + 1:].split(',') |
| 311 |
for la in linkargs: |
| 312 |
if la.startswith('--out-implib='): |
| 313 |
# Import library name |
| 314 |
args.append('-L=/IMPLIB:' + la[13:].strip()) |
| 315 |
elif arg.startswith('-mscrtlib='): |
| 316 |
args.append(arg) |
| 317 |
mscrtlib = arg[10:].lower() |
| 318 |
if cls is LLVMDCompiler: |
| 319 |
# Default crt libraries for LDC2 must be excluded for other |
| 320 |
# selected crt options. |
| 321 |
if mscrtlib != 'libcmt': |
| 322 |
args.append('-L=/NODEFAULTLIB:libcmt') |
| 323 |
args.append('-L=/NODEFAULTLIB:libvcruntime') |
| 324 |
|
| 325 |
# Fixes missing definitions for printf-functions in VS2017 |
| 326 |
if mscrtlib.startswith('msvcrt'): |
| 327 |
args.append('-L=/DEFAULTLIB:legacy_stdio_definitions.lib') |
| 328 |
|
| 329 |
return args |
| 330 |
|
| 331 |
@classmethod |
| 332 |
def _translate_arg_to_osx(cls, arg: str) -> T.List[str]: |
| 333 |
args: T.List[str] = [] |
| 334 |
if arg.startswith('-install_name'): |
| 335 |
args.append('-L=' + arg) |
| 336 |
return args |
| 337 |
|
| 338 |
@classmethod |
| 339 |
def _unix_args_to_native(cls, args: T.List[str], info: MachineInfo, link_id: str = '') -> T.List[str]: |
| 340 |
return cls._translate_args_to_nongnu(args, info, link_id) |
| 341 |
|
| 342 |
def get_debug_args(self, is_debug: bool) -> T.List[str]: |
| 343 |
ddebug_args = [] |
| 344 |
if is_debug: |
| 345 |
ddebug_args = [d_feature_args[self.id]['debug']] |
| 346 |
|
| 347 |
return clike_debug_args[is_debug] + ddebug_args |
| 348 |
|
| 349 |
def _get_crt_args(self, crt_val: str) -> T.List[str]: |
| 350 |
if not self.info.is_windows(): |
| 351 |
return [] |
| 352 |
return self.mscrt_args[self.get_crt_val(crt_val)] |
| 353 |
|
| 354 |
def get_soname_args(self, prefix: str, shlib_name: str, suffix: str, soversion: str, |
| 355 |
darwin_versions: T.Tuple[str, str]) -> T.List[str]: |
| 356 |
sargs = super().get_soname_args(prefix, shlib_name, suffix, soversion, darwin_versions) |
| 357 |
|
| 358 |
if not all(arg.startswith(self.LINKER_OPTION_STYLE.prefix) for arg in sargs): |
| 359 |
raise MesonBugException(f'Not all soname arguments for the D compiler start with {repr(self.LINKER_OPTION_STYLE.prefix)}: {sargs}') |
| 360 |
|
| 361 |
return sargs |
| 362 |
|
| 363 |
def get_allow_undefined_link_args(self) -> T.List[str]: |
| 364 |
args = self.linker.get_allow_undefined_args() |
| 365 |
if self.info.is_darwin(): |
| 366 |
# On macOS we're passing these options to the C compiler, but |
| 367 |
# they're linker options and need -Wl, so clang/gcc knows what to |
| 368 |
# do with them. I'm assuming, but don't know for certain, that |
| 369 |
# ldc/dmd do some kind of mapping internally for arguments they |
| 370 |
# understand, but pass arguments they don't understand directly. |
| 371 |
args = [a.replace('-L=', '-Xcc=-Wl,') for a in args] |
| 372 |
return args |
| 373 |
|
| 374 |
|
| 375 |
class DCompilerArgs(CompilerArgs): |
| 376 |
prepend_prefixes = ('-I', '-L') |
| 377 |
dedup2_prefixes = ('-I', ) |
| 378 |
|
| 379 |
|
| 380 |
class DCompiler(Compiler): |
| 381 |
mscrt_args = { |
| 382 |
'none': ['-mscrtlib='], |
| 383 |
'md': ['-mscrtlib=msvcrt'], |
| 384 |
'mdd': ['-mscrtlib=msvcrtd'], |
| 385 |
'mt': ['-mscrtlib=libcmt'], |
| 386 |
'mtd': ['-mscrtlib=libcmtd'], |
| 387 |
} |
| 388 |
|
| 389 |
language = 'd' |
| 390 |
|
| 391 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 392 |
env: Environment, arch: str, *, |
| 393 |
linker: T.Optional['DynamicLinker'] = None, |
| 394 |
full_version: T.Optional[str] = None): |
| 395 |
super().__init__([], exelist, version, for_machine, env, linker=linker, |
| 396 |
full_version=full_version) |
| 397 |
self.arch = arch |
| 398 |
|
| 399 |
def _sanity_check_source_code(self) -> str: |
| 400 |
return 'void main() { }' |
| 401 |
|
| 402 |
def _sanity_check_compile_args(self, sourcename: str, binname: str |
| 403 |
) -> T.Tuple[T.List[str], T.List[str]]: |
| 404 |
args, largs = super()._sanity_check_compile_args(sourcename, binname) |
| •405 |
largs = self.unix_args_to_native(largs) |
| 406 |
largs.extend(self._get_target_arch_args()) |
| 407 |
return args, largs |
| 408 |
|
| 409 |
def needs_static_linker(self) -> bool: |
| 410 |
return True |
| 411 |
|
| 412 |
def get_depfile_suffix(self) -> str: |
| 413 |
return 'deps' |
| 414 |
|
| 415 |
def get_pic_args(self) -> T.List[str]: |
| 416 |
if self.info.is_windows(): |
| 417 |
return [] |
| 418 |
return ['-fPIC'] |
| 419 |
|
| 420 |
def get_feature_args(self, kwargs: DFeatures, build_to_src: str) -> T.List[str]: |
| 421 |
res: T.List[str] = [] |
| 422 |
unittest_arg = d_feature_args[self.id]['unittest'] |
| 423 |
if not unittest_arg: |
| 424 |
raise EnvironmentException('D compiler %s does not support the "unittest" feature.' % self.name_string()) |
| 425 |
if kwargs['unittest']: |
| 426 |
res.append(unittest_arg) |
| 427 |
|
| 428 |
debug_level = -1 |
| 429 |
debug_arg = d_feature_args[self.id]['debug'] |
| 430 |
if not debug_arg: |
| 431 |
raise EnvironmentException('D compiler %s does not support conditional debug identifiers.' % self.name_string()) |
| 432 |
|
| 433 |
# Parse all debug identifiers and the largest debug level identifier |
| 434 |
for d in kwargs['debug']: |
| 435 |
if isinstance(d, int): |
| 436 |
debug_level = max(debug_level, d) |
| 437 |
elif isinstance(d, str) and d.isdigit(): |
| 438 |
debug_level = max(debug_level, int(d)) |
| 439 |
else: |
| 440 |
res.append(f'{debug_arg}={d}') |
| 441 |
|
| 442 |
if debug_level >= 0: |
| 443 |
res.append(f'{debug_arg}={debug_level}') |
| 444 |
|
| 445 |
version_level = -1 |
| 446 |
version_arg = d_feature_args[self.id]['version'] |
| 447 |
if not version_arg: |
| 448 |
raise EnvironmentException('D compiler %s does not support conditional version identifiers.' % self.name_string()) |
| 449 |
|
| 450 |
# Parse all version identifiers and the largest version level identifier |
| 451 |
for v in kwargs['versions']: |
| 452 |
if isinstance(v, int): |
| 453 |
version_level = max(version_level, v) |
| 454 |
elif isinstance(v, str) and v.isdigit(): |
| 455 |
version_level = max(version_level, int(v)) |
| 456 |
else: |
| 457 |
res.append(f'{version_arg}={v}') |
| 458 |
|
| 459 |
if version_level >= 0: |
| 460 |
res.append(f'{version_arg}={version_level}') |
| 461 |
|
| 462 |
import_dir_arg = d_feature_args[self.id]['import_dir'] |
| 463 |
if not import_dir_arg: |
| 464 |
raise EnvironmentException('D compiler %s does not support the "string import directories" feature.' % self.name_string()) |
| 465 |
for idir_obj in kwargs['import_dirs']: |
| 466 |
res.extend(f'{import_dir_arg}{i}' for i in idir_obj.rel_string_list(build_to_src)) |
| 467 |
|
| 468 |
return res |
| 469 |
|
| 470 |
def get_optimization_link_args(self, optimization_level: str) -> T.List[str]: |
| 471 |
if optimization_level != 'plain': |
| 472 |
return self._get_target_arch_args() |
| 473 |
return [] |
| 474 |
|
| 475 |
def compiler_args(self, args: T.Optional[T.Iterable[str]] = None) -> DCompilerArgs: |
| 476 |
return DCompilerArgs(self, args) |
| 477 |
|
| 478 |
def has_multi_arguments(self, args: T.List[str]) -> T.Tuple[bool, bool]: |
| 479 |
return self.compiles('int i;\n', extra_args=args) |
| 480 |
|
| 481 |
def _get_target_arch_args(self) -> T.List[str]: |
| 482 |
# LDC2 on Windows targets to current OS architecture, but |
| 483 |
# it should follow the target specified by the MSVC toolchain. |
| 484 |
if self.info.is_windows(): |
| 485 |
if self.is_cross: |
| 486 |
return [f'-mtriple={self.arch}-windows-msvc'] |
| 487 |
elif self.arch == 'x86_64': |
| 488 |
return ['-m64'] |
| 489 |
return ['-m32'] |
| 490 |
return [] |
| 491 |
|
| 492 |
def get_crt_compile_args(self, crt_val: str) -> T.List[str]: |
| 493 |
return [] |
| 494 |
|
| 495 |
def get_crt_link_args(self, crt_val: str) -> T.List[str]: |
| 496 |
return [] |
| 497 |
|
| 498 |
def _get_compile_extra_args(self, extra_args: T.Union[T.List[str], T.Callable[[CompileCheckMode], T.List[str]], None] = None) -> T.List[str]: |
| 499 |
args = self._get_target_arch_args() |
| 500 |
if extra_args: |
| 501 |
if callable(extra_args): |
| 502 |
extra_args = extra_args(CompileCheckMode.COMPILE) |
| 503 |
if isinstance(extra_args, list): |
| 504 |
args.extend(extra_args) |
| 505 |
elif isinstance(extra_args, str): |
| 506 |
args.append(extra_args) |
| 507 |
return args |
| 508 |
|
| 509 |
def run(self, code: 'mesonlib.FileOrString', |
| 510 |
extra_args: T.Union[T.List[str], T.Callable[[CompileCheckMode], T.List[str]], None] = None, |
| 511 |
dependencies: T.Optional[T.List['Dependency']] = None, |
| 512 |
run_env: T.Optional[T.Dict[str, str]] = None, |
| 513 |
run_cwd: T.Optional[str] = None) -> compilers.RunResult: |
| 514 |
extra_args = self._get_compile_extra_args(extra_args) |
| 515 |
return super().run(code, extra_args, dependencies, run_env, run_cwd) |
| 516 |
|
| 517 |
def sizeof(self, typename: str, prefix: str, *, |
| 518 |
extra_args: T.Union[None, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]] = None, |
| 519 |
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]: |
| 520 |
if extra_args is None: |
| 521 |
extra_args = [] |
| 522 |
t = f''' |
| 523 |
import std.stdio : writeln; |
| 524 |
{prefix} |
| 525 |
void main() {{ |
| 526 |
writeln(({typename}).sizeof); |
| 527 |
}} |
| 528 |
''' |
| 529 |
res = self.cached_run(t, extra_args=extra_args, |
| 530 |
dependencies=dependencies) |
| 531 |
if not res.compiled: |
| 532 |
return -1, False |
| 533 |
if res.returncode != 0: |
| 534 |
raise mesonlib.EnvironmentException('Could not run sizeof test binary.') |
| 535 |
return int(res.stdout), res.cached |
| 536 |
|
| 537 |
def alignment(self, typename: str, prefix: str, *, |
| 538 |
extra_args: T.Optional[T.List[str]] = None, |
| 539 |
dependencies: T.Optional[T.List['Dependency']] = None) -> T.Tuple[int, bool]: |
| 540 |
if extra_args is None: |
| 541 |
extra_args = [] |
| 542 |
t = f''' |
| 543 |
import std.stdio : writeln; |
| 544 |
{prefix} |
| 545 |
void main() {{ |
| 546 |
writeln(({typename}).alignof); |
| 547 |
}} |
| 548 |
''' |
| 549 |
res = self.run(t, extra_args=extra_args, dependencies=dependencies) |
| 550 |
if not res.compiled: |
| 551 |
raise mesonlib.EnvironmentException('Could not compile alignment test.') |
| 552 |
if res.returncode != 0: |
| 553 |
raise mesonlib.EnvironmentException('Could not run alignment test binary.') |
| 554 |
align = int(res.stdout) |
| 555 |
if align == 0: |
| 556 |
raise mesonlib.EnvironmentException(f'Could not determine alignment of {typename}. Sorry. You might want to file a bug.') |
| 557 |
return align, res.cached |
| 558 |
|
| 559 |
def has_header(self, hname: str, prefix: str, *, |
| 560 |
extra_args: T.Union[None, T.List[str], T.Callable[['CompileCheckMode'], T.List[str]]] = None, |
| 561 |
dependencies: T.Optional[T.List['Dependency']] = None, |
| 562 |
disable_cache: bool = False) -> T.Tuple[bool, bool]: |
| 563 |
|
| 564 |
extra_args = self._get_compile_extra_args(extra_args) |
| 565 |
code = f'''{prefix} |
| 566 |
import {hname}; |
| 567 |
''' |
| 568 |
return self.compiles(code, extra_args=extra_args, |
| 569 |
dependencies=dependencies, mode=CompileCheckMode.COMPILE, disable_cache=disable_cache) |
| 570 |
|
| 571 |
class GnuDCompiler(GnuCompiler, DCompiler): |
| 572 |
|
| 573 |
# we mostly want DCompiler, but that gives us the Compiler.LINKER_OPTION_STYLE instead |
| 574 |
LINKER_OPTION_STYLE = GnuCompiler.LINKER_OPTION_STYLE |
| 575 |
id = 'gcc' |
| 576 |
|
| 577 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 578 |
env: Environment, arch: str, *, |
| 579 |
linker: T.Optional['DynamicLinker'] = None, |
| 580 |
full_version: T.Optional[str] = None): |
| 581 |
DCompiler.__init__(self, exelist, version, for_machine, env, arch, |
| 582 |
linker=linker, full_version=full_version) |
| 583 |
GnuCompiler.__init__(self, {}) |
| 584 |
default_warn_args = ['-Wall', '-Wdeprecated'] |
| 585 |
self.warn_args = {'0': [], |
| 586 |
'1': default_warn_args, |
| 587 |
'2': default_warn_args + ['-Wextra'], |
| 588 |
'3': default_warn_args + ['-Wextra', '-Wpedantic'], |
| 589 |
'everything': (default_warn_args + ['-Wextra', '-Wpedantic'] + |
| 590 |
self.supported_warn_args(gnu_common_warning_args))} |
| 591 |
|
| 592 |
self.base_options = { |
| 593 |
OptionKey(o) for o in [ |
| 594 |
'b_colorout', 'b_sanitize', 'b_staticpic', 'b_vscrt', |
| 595 |
'b_coverage', 'b_pgo', 'b_ndebug']} |
| 596 |
|
| 597 |
self._has_color_support = version_compare(self.version, '>=4.9') |
| 598 |
# dependencies were implemented before, but broken - support was fixed in GCC 7.1+ |
| 599 |
# (and some backported versions) |
| 600 |
self._has_deps_support = version_compare(self.version, '>=7.1') |
| 601 |
|
| 602 |
def get_colorout_args(self, colortype: str) -> T.List[str]: |
| 603 |
if self._has_color_support: |
| 604 |
super().get_colorout_args(colortype) |
| 605 |
return [] |
| 606 |
|
| 607 |
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: |
| 608 |
if self._has_deps_support: |
| 609 |
return super().get_dependency_gen_args(outtarget, outfile) |
| 610 |
return [] |
| 611 |
|
| 612 |
def get_warn_args(self, level: str) -> T.List[str]: |
| 613 |
return self.warn_args[level] |
| 614 |
|
| 615 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 616 |
return gdc_optimization_args[optimization_level] |
| 617 |
|
| 618 |
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], |
| 619 |
build_dir: str) -> T.List[str]: |
| 620 |
for idx, i in enumerate(parameter_list): |
| 621 |
if i[:2] == '-I' or i[:2] == '-L': |
| 622 |
parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:])) |
| 623 |
|
| 624 |
return parameter_list |
| 625 |
|
| 626 |
def get_allow_undefined_link_args(self) -> T.List[str]: |
| 627 |
return self.linker.get_allow_undefined_args() |
| 628 |
|
| 629 |
def get_linker_always_args(self) -> T.List[str]: |
| 630 |
args = super().get_linker_always_args() |
| 631 |
if self.info.is_windows(): |
| 632 |
return args |
| 633 |
return args + ['-shared-libphobos'] |
| 634 |
|
| 635 |
def get_assert_args(self, disable: bool) -> T.List[str]: |
| 636 |
if disable: |
| 637 |
return ['-frelease'] |
| 638 |
return [] |
| 639 |
|
| 640 |
# LDC uses the DMD frontend code to parse and analyse the code. |
| 641 |
# It then uses LLVM for the binary code generation and optimizations. |
| 642 |
# This function retrieves the dmd frontend version, which determines |
| 643 |
# the common features between LDC and DMD. |
| 644 |
# We need the complete version text because the match is not on first line |
| 645 |
# of version_output |
| 646 |
def find_ldc_dmd_frontend_version(version_output: T.Optional[str]) -> T.Optional[str]: |
| 647 |
if version_output is None: |
| 648 |
return None |
| 649 |
version_regex = re.search(r'DMD v(\d+\.\d+\.\d+)', version_output) |
| 650 |
if version_regex: |
| 651 |
return version_regex.group(1) |
| 652 |
return None |
| 653 |
|
| 654 |
class LLVMDCompiler(DmdLikeCompilerMixin, DCompiler): |
| 655 |
|
| 656 |
id = 'llvm' |
| 657 |
|
| 658 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 659 |
env: Environment, arch: str, *, |
| 660 |
linker: T.Optional['DynamicLinker'] = None, |
| 661 |
full_version: T.Optional[str] = None, |
| 662 |
version_output: T.Optional[str] = None): |
| 663 |
DCompiler.__init__(self, exelist, version, for_machine, env, arch, |
| 664 |
linker=linker, full_version=full_version) |
| 665 |
DmdLikeCompilerMixin.__init__(self, dmd_frontend_version=find_ldc_dmd_frontend_version(version_output)) |
| 666 |
self.base_options = {OptionKey(o) for o in ['b_coverage', 'b_colorout', 'b_vscrt', 'b_ndebug']} |
| 667 |
|
| 668 |
def get_colorout_args(self, colortype: str) -> T.List[str]: |
| 669 |
if colortype == 'always': |
| 670 |
return ['-enable-color'] |
| 671 |
return [] |
| 672 |
|
| 673 |
def get_warn_args(self, level: str) -> T.List[str]: |
| 674 |
if level in {'2', '3'}: |
| 675 |
return ['-wi', '-dw'] |
| 676 |
elif level == '1': |
| 677 |
return ['-wi'] |
| 678 |
return [] |
| 679 |
|
| 680 |
def get_pic_args(self) -> T.List[str]: |
| 681 |
return ['-relocation-model=pic'] |
| 682 |
|
| 683 |
def get_crt_link_args(self, crt_val: str) -> T.List[str]: |
| 684 |
return self._get_crt_args(crt_val) |
| 685 |
|
| 686 |
def unix_args_to_native(self, args: T.List[str]) -> T.List[str]: |
| 687 |
return self._unix_args_to_native(args, self.info, self.linker.id) |
| 688 |
|
| 689 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 690 |
if optimization_level != 'plain': |
| 691 |
return self._get_target_arch_args() + ldc_optimization_args[optimization_level] |
| 692 |
return ldc_optimization_args[optimization_level] |
| 693 |
|
| 694 |
@classmethod |
| 695 |
def use_linker_args(cls, linker: str, version: str) -> T.List[str]: |
| 696 |
return [f'-linker={linker}'] |
| 697 |
|
| 698 |
def get_linker_always_args(self) -> T.List[str]: |
| 699 |
args = super().get_linker_always_args() |
| 700 |
if self.info.is_windows(): |
| 701 |
return args |
| 702 |
return args + ['-link-defaultlib-shared'] |
| 703 |
|
| 704 |
def get_assert_args(self, disable: bool) -> T.List[str]: |
| 705 |
if disable: |
| 706 |
return ['--release'] |
| 707 |
return [] |
| 708 |
|
| 709 |
def rsp_file_syntax(self) -> RSPFileSyntax: |
| 710 |
# We use `mesonlib.is_windows` here because we want to know what the |
| 711 |
# build machine is, not the host machine. This really means we would |
| 712 |
# have the Environment not the MachineInfo in the compiler. |
| 713 |
return RSPFileSyntax.MSVC if is_windows() else RSPFileSyntax.GCC |
| 714 |
|
| 715 |
|
| 716 |
class DmdDCompiler(DmdLikeCompilerMixin, DCompiler): |
| 717 |
|
| 718 |
id = 'dmd' |
| 719 |
|
| 720 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 721 |
env: Environment, arch: str, *, |
| 722 |
linker: T.Optional['DynamicLinker'] = None, |
| 723 |
full_version: T.Optional[str] = None): |
| 724 |
DCompiler.__init__(self, exelist, version, for_machine, env, arch, |
| 725 |
linker=linker, full_version=full_version) |
| 726 |
DmdLikeCompilerMixin.__init__(self, version) |
| 727 |
self.base_options = {OptionKey(o) for o in ['b_coverage', 'b_colorout', 'b_vscrt', 'b_ndebug']} |
| 728 |
|
| 729 |
def get_colorout_args(self, colortype: str) -> T.List[str]: |
| 730 |
if colortype == 'always': |
| 731 |
return ['-color=on'] |
| 732 |
return [] |
| 733 |
|
| 734 |
def get_std_exe_link_args(self) -> T.List[str]: |
| 735 |
if self.info.is_windows(): |
| 736 |
# DMD links against D runtime only when main symbol is found, |
| 737 |
# so these needs to be inserted when linking static D libraries. |
| 738 |
if self.arch == 'x86_64': |
| 739 |
return ['phobos64.lib'] |
| 740 |
elif self.arch == 'x86_mscoff': |
| 741 |
return ['phobos32mscoff.lib'] |
| 742 |
return ['phobos.lib'] |
| 743 |
return [] |
| 744 |
|
| 745 |
def get_std_shared_lib_link_args(self) -> T.List[str]: |
| 746 |
libname = 'libphobos2.so' |
| 747 |
if self.info.is_windows(): |
| 748 |
if self.arch == 'x86_64': |
| 749 |
libname = 'phobos64.lib' |
| 750 |
elif self.arch == 'x86_mscoff': |
| 751 |
libname = 'phobos32mscoff.lib' |
| 752 |
else: |
| 753 |
libname = 'phobos.lib' |
| 754 |
return ['-shared', '-defaultlib=' + libname] |
| 755 |
|
| 756 |
def _get_target_arch_args(self) -> T.List[str]: |
| 757 |
# DMD32 and DMD64 on 64-bit Windows defaults to 32-bit (OMF). |
| 758 |
# Force the target to 64-bit in order to stay consistent |
| 759 |
# across the different platforms. |
| 760 |
if self.info.is_windows(): |
| 761 |
if self.arch == 'x86_64': |
| 762 |
return ['-m64'] |
| 763 |
elif self.arch == 'x86_mscoff': |
| 764 |
return ['-m32mscoff'] |
| 765 |
return ['-m32'] |
| 766 |
return [] |
| 767 |
|
| 768 |
def get_crt_compile_args(self, crt_val: str) -> T.List[str]: |
| 769 |
return self._get_crt_args(crt_val) |
| 770 |
|
| 771 |
def unix_args_to_native(self, args: T.List[str]) -> T.List[str]: |
| 772 |
return self._unix_args_to_native(args, self.info, self.linker.id) |
| 773 |
|
| 774 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 775 |
if optimization_level != 'plain': |
| 776 |
return self._get_target_arch_args() + dmd_optimization_args[optimization_level] |
| 777 |
return dmd_optimization_args[optimization_level] |
| 778 |
|
| 779 |
def can_linker_accept_rsp(self) -> bool: |
| 780 |
return False |
| 781 |
|
| 782 |
def get_linker_always_args(self) -> T.List[str]: |
| 783 |
args = super().get_linker_always_args() |
| 784 |
if self.info.is_windows(): |
| 785 |
return args |
| 786 |
return args + ['-defaultlib=phobos2', '-debuglib=phobos2'] |
| 787 |
|
| 788 |
def get_assert_args(self, disable: bool) -> T.List[str]: |
| 789 |
if disable: |
| 790 |
return ['-release'] |
| 791 |
return [] |
| 792 |
|
| 793 |
def rsp_file_syntax(self) -> RSPFileSyntax: |
| 794 |
return RSPFileSyntax.MSVC |