| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2022 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from ..mesonlib import ( |
| 7 |
MesonException, EnvironmentException, MachineChoice, join_args, |
| 8 |
search_version, is_windows, Popen_safe, Popen_safe_logged, version_compare, windows_proof_rm, |
| 9 |
unwrap, |
| 10 |
) |
| 11 |
from ..programs import ExternalProgram |
| 12 |
from ..envconfig import BinaryTable, detect_cpu_family |
| 13 |
from .. import mlog |
| 14 |
|
| 15 |
from ..linkers import guess_win_linker, guess_nix_linker |
| 16 |
|
| 17 |
import subprocess |
| 18 |
import platform |
| 19 |
import re |
| 20 |
import shutil |
| 21 |
import tempfile |
| 22 |
import os |
| 23 |
import typing as T |
| 24 |
|
| 25 |
if T.TYPE_CHECKING: |
| 26 |
from .compilers import Language, Compiler, CompilerDict |
| 27 |
from .asm import ASMCompiler |
| 28 |
from .c import CCompiler |
| 29 |
from .cpp import CPPCompiler |
| 30 |
from .fortran import FortranCompiler |
| 31 |
from .rust import RustCompiler |
| 32 |
from ..linkers.linkers import StaticLinker, DynamicLinker |
| 33 |
from ..environment import Environment |
| 34 |
|
| 35 |
|
| 36 |
# Default compilers and linkers |
| 37 |
# ============================= |
| 38 |
|
| 39 |
defaults: T.Dict[str, T.List[str]] = {} |
| 40 |
|
| 41 |
# List of potential compilers. |
| 42 |
if is_windows(): |
| 43 |
# Intel C and C++ compiler is icl on Windows, but icc and icpc elsewhere. |
| 44 |
# Search for icl before cl, since Intel "helpfully" provides a |
| 45 |
# cl.exe that returns *exactly the same thing* that Microsoft's |
| 46 |
# cl.exe does, and if icl is present, it's almost certainly what |
| 47 |
# you want. |
| 48 |
defaults['c'] = ['icl', 'cl', 'cc', 'gcc', 'clang', 'clang-cl', 'pgcc'] |
| 49 |
# There is currently no pgc++ for Windows, only for Mac and Linux. |
| 50 |
defaults['cpp'] = ['icl', 'cl', 'c++', 'g++', 'clang++', 'clang-cl'] |
| 51 |
# the binary flang-new will be renamed to flang in the foreseeable future |
| 52 |
defaults['fortran'] = ['ifort', 'ifx', 'gfortran', 'flang-new', 'flang', 'pgfortran', 'g95'] |
| 53 |
defaults['objc'] = ['clang', 'clang-cl', 'gcc'] |
| 54 |
defaults['objcpp'] = ['clang++', 'clang-cl', 'g++'] |
| 55 |
defaults['cs'] = ['csc', 'mcs'] |
| 56 |
else: |
| 57 |
if platform.machine().lower() == 'e2k': |
| 58 |
defaults['c'] = ['cc', 'gcc', 'lcc', 'clang'] |
| 59 |
defaults['cpp'] = ['c++', 'g++', 'l++', 'clang++'] |
| 60 |
defaults['objc'] = ['clang'] |
| 61 |
defaults['objcpp'] = ['clang++'] |
| 62 |
else: |
| 63 |
defaults['c'] = ['cc', 'gcc', 'clang', 'nvc', 'pgcc', 'icc', 'icx'] |
| 64 |
defaults['cpp'] = ['c++', 'g++', 'clang++', 'nvc++', 'pgc++', 'icpc', 'icpx'] |
| 65 |
defaults['objc'] = ['clang', 'gcc'] |
| 66 |
defaults['objcpp'] = ['clang++', 'g++'] |
| 67 |
# the binary flang-new will be renamed to flang in the foreseeable future |
| 68 |
defaults['fortran'] = ['gfortran', 'flang-new', 'flang', 'nvfortran', 'pgfortran', 'ifort', 'ifx', 'g95'] |
| 69 |
defaults['cs'] = ['mcs', 'csc'] |
| 70 |
defaults['d'] = ['ldc2', 'ldc', 'gdc', 'dmd'] |
| 71 |
defaults['java'] = ['javac'] |
| 72 |
defaults['cuda'] = ['nvcc'] |
| 73 |
defaults['rust'] = ['rustc'] |
| 74 |
defaults['swift'] = ['swiftc'] |
| 75 |
defaults['vala'] = ['valac'] |
| 76 |
defaults['cython'] = ['cython', 'cython3'] # Official name is cython, but Debian renamed it to cython3. |
| 77 |
defaults['static_linker'] = ['ar', 'gar'] |
| 78 |
defaults['strip'] = ['strip'] |
| 79 |
defaults['vs_static_linker'] = ['lib'] |
| 80 |
defaults['clang_cl_static_linker'] = ['llvm-lib'] |
| 81 |
defaults['cuda_static_linker'] = ['nvlink'] |
| 82 |
defaults['gcc_static_linker'] = ['gcc-ar'] |
| 83 |
defaults['clang_static_linker'] = ['llvm-ar'] |
| 84 |
defaults['emxomf_static_linker'] = ['emxomfar'] |
| 85 |
defaults['nasm'] = ['nasm', 'yasm'] |
| 86 |
|
| 87 |
|
| 88 |
def compiler_from_language(env: 'Environment', lang: str, for_machine: MachineChoice) -> T.Optional[Compiler]: |
| 89 |
lang_map: T.Dict[str, T.Callable[['Environment', MachineChoice], Compiler]] = { |
| 90 |
'c': detect_c_compiler, |
| 91 |
'cpp': detect_cpp_compiler, |
| 92 |
'objc': detect_objc_compiler, |
| 93 |
'cuda': detect_cuda_compiler, |
| 94 |
'objcpp': detect_objcpp_compiler, |
| 95 |
'java': detect_java_compiler, |
| 96 |
'cs': detect_cs_compiler, |
| 97 |
'vala': detect_vala_compiler, |
| 98 |
'd': detect_d_compiler, |
| 99 |
'rust': detect_rust_compiler, |
| 100 |
'fortran': detect_fortran_compiler, |
| 101 |
'swift': detect_swift_compiler, |
| 102 |
'cython': detect_cython_compiler, |
| 103 |
'nasm': detect_nasm_compiler, |
| 104 |
'masm': detect_masm_compiler, |
| 105 |
'linearasm': detect_linearasm_compiler, |
| 106 |
} |
| 107 |
return lang_map[lang](env, for_machine) if lang in lang_map else None |
| 108 |
|
| 109 |
def detect_compiler_for(env: 'Environment', lang: Language, for_machine: MachineChoice, skip_sanity_check: bool, subproject: str) -> T.Optional[Compiler]: |
| 110 |
comp = compiler_from_language(env, lang, for_machine) |
| 111 |
if comp is None: |
| 112 |
return comp |
| 113 |
assert comp.for_machine == for_machine |
| 114 |
env.coredata.process_compiler_options(lang, comp, subproject) |
| 115 |
if not skip_sanity_check: |
| 116 |
comp.sanity_check(env.get_scratch_dir()) |
| 117 |
env.coredata.compilers[comp.for_machine][lang] = comp |
| 118 |
return comp |
| 119 |
|
| 120 |
|
| 121 |
# Helpers |
| 122 |
# ======= |
| 123 |
|
| 124 |
def _get_compilers(env: 'Environment', lang: str, for_machine: MachineChoice, |
| 125 |
allow_build_machine: bool = False) -> T.Tuple[T.List[T.List[str]], T.Union[None, ExternalProgram]]: |
| 126 |
''' |
| 127 |
The list of compilers is detected in the exact same way for |
| 128 |
C, C++, ObjC, ObjC++, Fortran, CS so consolidate it here. |
| 129 |
''' |
| 130 |
value = env.lookup_binary_entry(for_machine, lang) |
| 131 |
if value is not None: |
| 132 |
comp, ccache = BinaryTable.parse_entry(value) |
| 133 |
# Return value has to be a list of compiler 'choices' |
| 134 |
compilers = [comp] |
| 135 |
else: |
| 136 |
if not env.machines.matches_build_machine(for_machine): |
| 137 |
if allow_build_machine: |
| 138 |
return _get_compilers(env, lang, MachineChoice.BUILD) |
| 139 |
raise EnvironmentException(f'{lang!r} compiler binary not defined in cross file [binaries] section') |
| 140 |
compilers = [[x] for x in defaults[lang]] |
| 141 |
ccache = BinaryTable.detect_compiler_cache() |
| 142 |
|
| 143 |
return compilers, ccache |
| 144 |
|
| 145 |
def _handle_exceptions( |
| 146 |
exceptions: T.Mapping[str, T.Union[Exception, str]], |
| 147 |
binaries: T.List[T.List[str]], |
| 148 |
bintype: str = 'compiler') -> T.NoReturn: |
| 149 |
errmsg = f'Unknown {bintype}(s): {binaries}' |
| 150 |
if exceptions: |
| 151 |
errmsg += '\nThe following exception(s) were encountered:' |
| 152 |
for c, e in exceptions.items(): |
| 153 |
if isinstance(e, MesonException): |
| 154 |
errmsg += f'\nUsing `{c}` failed: {e}' |
| 155 |
else: |
| 156 |
errmsg += f'\nRunning `{c}` gave "{e}"' |
| 157 |
raise EnvironmentException(errmsg) |
| 158 |
|
| 159 |
|
| 160 |
# Linker specific |
| 161 |
# =============== |
| 162 |
|
| 163 |
def detect_static_linker(env: 'Environment', compiler: Compiler) -> StaticLinker: |
| 164 |
from . import d |
| 165 |
from ..linkers import linkers |
| 166 |
from ..options import OptionKey |
| 167 |
linker = env.lookup_binary_entry(compiler.for_machine, 'ar') |
| 168 |
if linker is not None: |
| 169 |
trials = [linker] |
| 170 |
else: |
| 171 |
default_linkers = [[l] for l in defaults['static_linker']] |
| 172 |
if compiler.language == 'cuda': |
| 173 |
trials = [defaults['cuda_static_linker']] + default_linkers |
| 174 |
elif compiler.get_argument_syntax() == 'msvc': |
| 175 |
trials = [defaults['vs_static_linker'], defaults['clang_cl_static_linker']] |
| 176 |
elif env.machines[compiler.for_machine].is_os2() and env.coredata.optstore.get_value_for(OptionKey('os2_emxomf')): |
| 177 |
trials = [defaults['emxomf_static_linker']] + default_linkers |
| 178 |
elif compiler.id == 'gcc': |
| 179 |
# Use gcc-ar if available; needed for LTO |
| 180 |
trials = [defaults['gcc_static_linker']] + default_linkers |
| 181 |
elif compiler.id == 'clang': |
| 182 |
# Use llvm-ar if available; needed for LTO |
| 183 |
llvm_ar = defaults['clang_static_linker'] |
| 184 |
# Extract the version major of the compiler to use as a suffix |
| 185 |
suffix = compiler.version.split('.')[0] |
| 186 |
# Prefer suffixed llvm-ar first, then unsuffixed then the defaults |
| 187 |
trials = [[f'{llvm_ar[0]}-{suffix}'], llvm_ar] + default_linkers |
| 188 |
elif compiler.language == 'd': |
| 189 |
# Prefer static linkers over linkers used by D compilers |
| 190 |
if is_windows(): |
| 191 |
trials = [defaults['vs_static_linker'], defaults['clang_cl_static_linker'], compiler.get_linker_exelist()] |
| 192 |
else: |
| 193 |
trials = default_linkers |
| 194 |
elif compiler.id == 'intel-cl' and compiler.language == 'c': # why not cpp? Is this a bug? |
| 195 |
# Intel has its own linker that acts like Microsoft's lib |
| 196 |
trials = [['xilib']] |
| 197 |
elif is_windows() and compiler.id == 'pgi': # this handles cpp / nvidia HPC, in addition to just c/fortran |
| 198 |
trials = [['ar']] # For PGI on Windows, "ar" is just a wrapper calling link/lib. |
| 199 |
elif is_windows() and compiler.id == 'nasm': |
| 200 |
# This may well be LINK.EXE if it's under a MSVC environment |
| 201 |
trials = [defaults['vs_static_linker'], defaults['clang_cl_static_linker']] + default_linkers |
| 202 |
else: |
| 203 |
trials = default_linkers |
| 204 |
popen_exceptions = {} |
| 205 |
for linker in trials: |
| 206 |
linker_name = os.path.basename(linker[0]) |
| 207 |
|
| 208 |
if any(os.path.basename(x) in {'lib', 'lib.exe', 'llvm-lib', 'llvm-lib.exe', 'xilib', 'xilib.exe'} for x in linker): |
| 209 |
arg = '/?' |
| 210 |
elif linker_name in {'ar2000', 'ar2000.exe', 'ar430', 'ar430.exe', 'ar6x', 'ar6x.exe'}: |
| 211 |
arg = '?' |
| 212 |
elif linker_name in {'armar', 'armar.exe'}: |
| 213 |
arg = '-h' |
| 214 |
else: |
| 215 |
arg = '--version' |
| 216 |
try: |
| 217 |
p, out, err = Popen_safe_logged(linker + [arg], msg='Detecting archiver via') |
| 218 |
except OSError as e: |
| 219 |
popen_exceptions[join_args(linker + [arg])] = e |
| 220 |
continue |
| 221 |
if "xilib: executing 'lib'" in err: |
| 222 |
return linkers.IntelVisualStudioLinker(linker, env, getattr(compiler, 'machine', None)) |
| 223 |
if '/OUT:' in out.upper() or '/OUT:' in err.upper(): |
| 224 |
return linkers.VisualStudioLinker(linker, env, getattr(compiler, 'machine', None)) |
| 225 |
if 'ar-Error-Unknown switch: --version' in err: |
| 226 |
return linkers.PGIStaticLinker(linker, env) |
| 227 |
if p.returncode == 0 and 'armar' in linker_name: |
| 228 |
return linkers.ArmarLinker(linker, env) |
| 229 |
if 'DMD32 D Compiler' in out or 'DMD64 D Compiler' in out: |
| 230 |
assert isinstance(compiler, d.DCompiler) |
| 231 |
return linkers.DLinker(linker, env, compiler.arch) |
| 232 |
if 'LDC - the LLVM D compiler' in out: |
| 233 |
assert isinstance(compiler, d.DCompiler) |
| 234 |
return linkers.DLinker(linker, env, compiler.arch, rsp_syntax=compiler.rsp_file_syntax()) |
| 235 |
if 'GDC' in out and ' based on D ' in out: |
| 236 |
assert isinstance(compiler, d.DCompiler) |
| 237 |
return linkers.DLinker(linker, env, compiler.arch) |
| 238 |
if err.startswith('Renesas') and 'rlink' in linker_name: |
| 239 |
return linkers.CcrxLinker(linker, env) |
| 240 |
if out.startswith('GNU ar'): |
| 241 |
if 'xc16-ar' in linker_name: |
| 242 |
return linkers.Xc16Linker(linker, env) |
| 243 |
elif 'xc32-ar' in linker_name: |
| 244 |
return linkers.Xc32ArLinker(compiler.for_machine, linker, env) |
| 245 |
if 'Texas Instruments Incorporated' in out: |
| 246 |
if 'ar2000' in linker_name: |
| 247 |
return linkers.C2000Linker(linker, env) |
| 248 |
elif 'ar6000' in linker_name: |
| 249 |
return linkers.C6000Linker(linker, env) |
| 250 |
else: |
| 251 |
return linkers.TILinker(linker, env) |
| 252 |
if out.startswith('The CompCert'): |
| 253 |
return linkers.CompCertLinker(linker, env) |
| 254 |
if out.strip().startswith('Metrowerks') or out.strip().startswith('Freescale'): |
| 255 |
if 'ARM' in out: |
| 256 |
return linkers.MetrowerksStaticLinkerARM(linker, env) |
| 257 |
else: |
| 258 |
return linkers.MetrowerksStaticLinkerEmbeddedPowerPC(linker, env) |
| 259 |
if 'TASKING VX-toolset' in err: |
| 260 |
return linkers.TaskingStaticLinker(linker, env) |
| 261 |
if p.returncode == 0: |
| 262 |
return linkers.ArLinker(compiler.for_machine, linker, env) |
| 263 |
if p.returncode == 1 and err.startswith('usage'): # OSX |
| 264 |
return linkers.AppleArLinker(compiler.for_machine, linker, env) |
| 265 |
if p.returncode == 1 and err.startswith('Usage'): # AIX |
| 266 |
return linkers.AIXArLinker(linker, env) |
| 267 |
if p.returncode == 1 and err.startswith('ar: bad option: --'): # Solaris |
| 268 |
return linkers.ArLinker(compiler.for_machine, linker, env) |
| 269 |
if p.returncode == 1 and err.startswith('emxomfar'): |
| 270 |
return linkers.EmxomfArLinker(compiler.for_machine, linker, env) |
| 271 |
_handle_exceptions(popen_exceptions, trials, 'linker') |
| 272 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 273 |
|
| 274 |
|
| 275 |
# Compilers |
| 276 |
# ========= |
| 277 |
|
| 278 |
|
| 279 |
def _detect_c_or_cpp_compiler(env: 'Environment', lang: str, for_machine: MachineChoice, *, override_compilers: T.Optional[T.List[T.List[str]]] = None) -> Compiler: |
| 280 |
"""Shared implementation for finding the C or C++ compiler to use. |
| 281 |
|
| 282 |
the override_compiler option is provided to allow compilers which use |
| 283 |
the compiler (GCC or Clang usually) as their shared linker, to find |
| 284 |
the linker they need. |
| 285 |
""" |
| 286 |
from . import c, cpp |
| 287 |
from ..linkers import linkers |
| 288 |
popen_exceptions: T.Dict[str, T.Union[Exception, str]] = {} |
| 289 |
compilers, ccache_exe = _get_compilers(env, lang, for_machine) |
| 290 |
ccache = ccache_exe.get_command() if (ccache_exe and ccache_exe.found()) else [] |
| 291 |
if override_compilers is not None: |
| 292 |
compilers = override_compilers |
| 293 |
cls: T.Union[T.Type[CCompiler], T.Type[CPPCompiler]] |
| 294 |
lnk: T.Union[T.Type[StaticLinker], T.Type[DynamicLinker]] |
| 295 |
|
| 296 |
for compiler in compilers: |
| 297 |
compiler_name = os.path.basename(compiler[0]) |
| 298 |
|
| 299 |
if any(os.path.basename(x) in {'cl', 'cl.exe', 'clang-cl', 'clang-cl.exe'} for x in compiler): |
| 300 |
# Watcom C provides its own cl.exe clone that mimics an older |
| 301 |
# version of Microsoft's compiler. Since Watcom's cl.exe is |
| 302 |
# just a wrapper, we skip using it if we detect its presence |
| 303 |
# so as not to confuse Meson when configuring for MSVC. |
| 304 |
# |
| 305 |
# Additionally the help text of Watcom's cl.exe is paged, and |
| 306 |
# the binary will not exit without human intervention. In |
| 307 |
# practice, Meson will block waiting for Watcom's cl.exe to |
| 308 |
# exit, which requires user input and thus will never exit. |
| 309 |
if 'WATCOM' in os.environ: |
| 310 |
def sanitize(p: T.Optional[str]) -> T.Optional[str]: |
| 311 |
return os.path.normcase(os.path.abspath(p)) if p else None |
| 312 |
|
| 313 |
watcom_cls = [sanitize(os.path.join(os.environ['WATCOM'], 'BINNT', 'cl')), |
| 314 |
sanitize(os.path.join(os.environ['WATCOM'], 'BINNT', 'cl.exe')), |
| 315 |
sanitize(os.path.join(os.environ['WATCOM'], 'BINNT64', 'cl')), |
| 316 |
sanitize(os.path.join(os.environ['WATCOM'], 'BINNT64', 'cl.exe'))] |
| 317 |
found_cl = sanitize(shutil.which('cl')) |
| 318 |
if found_cl in watcom_cls: |
| 319 |
mlog.debug('Skipping unsupported cl.exe clone at:', found_cl) |
| 320 |
continue |
| 321 |
arg = '/?' |
| 322 |
elif 'armcc' in compiler_name: |
| 323 |
arg = '--vsn' |
| 324 |
elif 'ccrx' in compiler_name: |
| 325 |
arg = '-v' |
| 326 |
elif 'xc16' in compiler_name: |
| 327 |
arg = '--version' |
| 328 |
elif 'ccomp' in compiler_name: |
| 329 |
arg = '-version' |
| 330 |
elif compiler_name in {'cl2000', 'cl2000.exe', 'cl430', 'cl430.exe', 'armcl', 'armcl.exe', 'cl6x', 'cl6x.exe'}: |
| 331 |
# TI compiler |
| 332 |
arg = '-version' |
| 333 |
elif compiler_name in {'icl', 'icl.exe'}: |
| 334 |
# if you pass anything to icl you get stuck in a pager |
| 335 |
arg = '' |
| 336 |
else: |
| 337 |
arg = '--version' |
| 338 |
|
| 339 |
cmd = compiler + [arg] |
| 340 |
try: |
| 341 |
p, out, err = Popen_safe_logged(cmd, msg='Detecting compiler via') |
| 342 |
except OSError as e: |
| 343 |
popen_exceptions[join_args(cmd)] = e |
| 344 |
continue |
| 345 |
|
| 346 |
if 'ccrx' in compiler_name: |
| 347 |
out = err |
| 348 |
|
| 349 |
full_version = out.split('\n', 1)[0] |
| 350 |
version = search_version(out) |
| 351 |
|
| 352 |
guess_gcc_or_lcc: T.Optional[str] = None |
| 353 |
if 'Free Software Foundation' in out or out.startswith('xt-'): |
| 354 |
guess_gcc_or_lcc = 'gcc' |
| 355 |
if 'e2k' in out and 'lcc' in out: |
| 356 |
guess_gcc_or_lcc = 'lcc' |
| 357 |
if 'Microchip' in out: |
| 358 |
# this output has "Free Software Foundation" in its version |
| 359 |
guess_gcc_or_lcc = None |
| 360 |
|
| 361 |
if guess_gcc_or_lcc: |
| 362 |
defines = _get_gnu_compiler_defines(compiler, lang) |
| 363 |
if not defines: |
| 364 |
popen_exceptions[join_args(compiler)] = 'no pre-processor defines' |
| 365 |
continue |
| 366 |
|
| 367 |
if guess_gcc_or_lcc == 'lcc': |
| 368 |
version = _get_lcc_version_from_defines(defines) |
| 369 |
cls = c.ElbrusCCompiler if lang == 'c' else cpp.ElbrusCPPCompiler |
| 370 |
else: |
| 371 |
version = _get_gnu_version_from_defines(defines) |
| 372 |
cls = c.GnuCCompiler if lang == 'c' else cpp.GnuCPPCompiler |
| 373 |
|
| 374 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 375 |
|
| 376 |
return cls( |
| 377 |
ccache, compiler, version, for_machine, |
| 378 |
env, defines=defines, full_version=full_version, |
| 379 |
linker=linker) |
| 380 |
|
| 381 |
if 'Emscripten' in out: |
| 382 |
cls = c.EmscriptenCCompiler if lang == 'c' else cpp.EmscriptenCPPCompiler |
| 383 |
env.add_lang_args(cls.language, cls, for_machine) |
| 384 |
|
| 385 |
# emcc requires a file input in order to pass arguments to the |
| 386 |
# linker. It'll exit with an error code, but still print the |
| 387 |
# linker version. |
| 388 |
with tempfile.NamedTemporaryFile(suffix='.c') as f: |
| 389 |
cmd = [*compiler, *cls.LINKER_OPTION_STYLE.wrap(['--version']), f.name] |
| 390 |
_, o, _ = Popen_safe(cmd) |
| 391 |
|
| 392 |
linker = linkers.WASMDynamicLinker( |
| 393 |
compiler, env, for_machine, cls.LINKER_OPTION_STYLE, |
| 394 |
[], version=search_version(o)) |
| 395 |
return cls( |
| 396 |
ccache, compiler, version, for_machine, env, |
| 397 |
linker=linker, full_version=full_version) |
| 398 |
|
| 399 |
if 'Arm C/C++/Fortran Compiler' in out: |
| 400 |
arm_ver_match = unwrap(re.search(r'version (\d+)\.(\d+)\.?(\d+)? \(build number (\d+)\)', out)) |
| 401 |
version = '.'.join([x for x in arm_ver_match.groups() if x is not None]) |
| 402 |
if lang == 'c': |
| 403 |
cls = c.ArmLtdClangCCompiler |
| 404 |
elif lang == 'cpp': |
| 405 |
cls = cpp.ArmLtdClangCPPCompiler |
| 406 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 407 |
return cls( |
| 408 |
ccache, compiler, version, for_machine, env, |
| 409 |
linker=linker) |
| 410 |
if 'armclang' in out: |
| 411 |
# The compiler version is not present in the first line of output, |
| 412 |
# instead it is present in second line, startswith 'Component:'. |
| 413 |
# So, searching for the 'Component' in out although we know it is |
| 414 |
# present in second line, as we are not sure about the |
| 415 |
# output format in future versions |
| 416 |
arm_ver_match = re.search('.*Component.*', out) |
| 417 |
if arm_ver_match is None: |
| 418 |
popen_exceptions[join_args(compiler)] = 'version string not found' |
| 419 |
continue |
| 420 |
arm_ver_str = arm_ver_match.group(0) |
| 421 |
# Override previous values |
| 422 |
version = search_version(arm_ver_str) |
| 423 |
full_version = arm_ver_str |
| 424 |
cls = c.ArmclangCCompiler if lang == 'c' else cpp.ArmclangCPPCompiler |
| 425 |
linker = linkers.ArmClangDynamicLinker(env, for_machine, version=version) |
| 426 |
env.add_lang_args(cls.language, cls, for_machine) |
| 427 |
return cls( |
| 428 |
ccache, compiler, version, for_machine, env, |
| 429 |
full_version=full_version, linker=linker) |
| 430 |
if 'CL.EXE COMPATIBILITY' in out: |
| 431 |
# if this is clang-cl masquerading as cl, detect it as cl, not |
| 432 |
# clang |
| 433 |
arg = '--version' |
| 434 |
try: |
| 435 |
p, out, err = Popen_safe(compiler + [arg]) |
| 436 |
except OSError as e: |
| 437 |
popen_exceptions[join_args(compiler + [arg])] = e |
| 438 |
version = search_version(out) |
| 439 |
match = re.search('^Target: (.*?)-', out, re.MULTILINE) |
| 440 |
if match: |
| 441 |
target = match.group(1) |
| 442 |
else: |
| 443 |
target = 'unknown target' |
| 444 |
cls = c.ClangClCCompiler if lang == 'c' else cpp.ClangClCPPCompiler |
| 445 |
linker = guess_win_linker(env, ['lld-link'], cls, version, for_machine) |
| 446 |
return cls( |
| 447 |
compiler, version, for_machine, env, target, |
| 448 |
linker=linker) |
| 449 |
|
| 450 |
# must be detected here before clang because TI compilers contain 'clang' in their output and so that they can be detected as 'clang' |
| 451 |
ti_compilers = { |
| 452 |
'TMS320C2000 C/C++': (c.C2000CCompiler, cpp.C2000CPPCompiler, linkers.C2000DynamicLinker), |
| 453 |
'TMS320C6x C/C++': (c.C6000CCompiler, cpp.C6000CPPCompiler, linkers.C6000DynamicLinker), |
| 454 |
'TI ARM C/C++ Compiler': (c.TICCompiler, cpp.TICPPCompiler, linkers.TIDynamicLinker), |
| 455 |
'MSP430 C/C++': (c.TICCompiler, cpp.TICPPCompiler, linkers.TIDynamicLinker) |
| 456 |
} |
| 457 |
for identifier, compiler_classes in ti_compilers.items(): |
| 458 |
if identifier in out: |
| 459 |
cls = compiler_classes[0] if lang == 'c' else compiler_classes[1] |
| 460 |
lnk = compiler_classes[2] |
| 461 |
env.add_lang_args(cls.language, cls, for_machine) |
| 462 |
linker = lnk(compiler, env, for_machine, version=version) |
| 463 |
return cls( |
| 464 |
ccache, compiler, version, for_machine, env, |
| 465 |
full_version=full_version, linker=linker) |
| 466 |
|
| 467 |
if 'clang' in out or 'Clang' in out: |
| 468 |
linker = None |
| 469 |
|
| 470 |
defines = _get_clang_compiler_defines(compiler, lang) |
| 471 |
|
| 472 |
# Even if the for_machine is darwin, we could be using vanilla |
| 473 |
# clang. |
| 474 |
if 'Apple' in out: |
| 475 |
cls = c.AppleClangCCompiler if lang == 'c' else cpp.AppleClangCPPCompiler |
| 476 |
else: |
| 477 |
cls = c.ClangCCompiler if lang == 'c' else cpp.ClangCPPCompiler |
| 478 |
|
| 479 |
if 'windows' in out or env.machines[for_machine].is_windows(): |
| 480 |
# If we're in a MINGW context this actually will use a gnu |
| 481 |
# style ld, but for clang on "real" windows we'll use |
| 482 |
# either link.exe or lld-link.exe |
| 483 |
try: |
| 484 |
linker = guess_win_linker(env, compiler, cls, version, for_machine, invoked_directly=False) |
| 485 |
except MesonException: |
| 486 |
pass |
| 487 |
if linker is None: |
| 488 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 489 |
|
| 490 |
return cls( |
| 491 |
ccache, compiler, version, for_machine, env, |
| 492 |
defines=defines, full_version=full_version, linker=linker) |
| 493 |
|
| 494 |
if 'Intel(R) C++ Intel(R)' in err: |
| 495 |
version = search_version(err) |
| 496 |
target = 'x86' if 'IA-32' in err else 'x86_64' |
| 497 |
cls = c.IntelClCCompiler if lang == 'c' else cpp.IntelClCPPCompiler |
| 498 |
env.add_lang_args(cls.language, cls, for_machine) |
| 499 |
linker = linkers.XilinkDynamicLinker(env, for_machine, [], version=version) |
| 500 |
return cls( |
| 501 |
compiler, version, for_machine, env, target, |
| 502 |
linker=linker) |
| 503 |
if 'Intel(R) oneAPI DPC++/C++ Compiler for applications' in err: |
| 504 |
version = search_version(err) |
| 505 |
target = 'x86' if 'IA-32' in err else 'x86_64' |
| 506 |
cls = c.IntelLLVMClCCompiler if lang == 'c' else cpp.IntelLLVMClCPPCompiler |
| 507 |
env.add_lang_args(cls.language, cls, for_machine) |
| 508 |
linker = linker = guess_win_linker( |
| 509 |
env, ['link'], cls, version, |
| 510 |
for_machine) |
| 511 |
return cls( |
| 512 |
compiler, version, for_machine, env, target, |
| 513 |
linker=linker) |
| 514 |
if 'Microsoft' in out or 'Microsoft' in err: |
| 515 |
# Latest versions of Visual Studio print version |
| 516 |
# number to stderr but earlier ones print version |
| 517 |
# on stdout. Why? Lord only knows. |
| 518 |
# Check both outputs to figure out version. |
| 519 |
for lookat in [err, out]: |
| 520 |
version = search_version(lookat) |
| 521 |
if version != 'unknown version': |
| 522 |
break |
| 523 |
else: |
| 524 |
raise EnvironmentException(f'Failed to detect MSVC compiler version: stderr was\n{err!r}') |
| 525 |
cl_signature = lookat.split('\n', maxsplit=1)[0] |
| 526 |
match = re.search(r'.*(x86|x64|ARM|ARM64)([^_A-Za-z0-9]|$)', cl_signature) |
| 527 |
if match: |
| 528 |
target = match.group(1) |
| 529 |
else: |
| 530 |
m = f'Failed to detect MSVC compiler target architecture: \'cl /?\' output is\n{cl_signature}' |
| 531 |
raise EnvironmentException(m) |
| 532 |
cls = c.VisualStudioCCompiler if lang == 'c' else cpp.VisualStudioCPPCompiler |
| 533 |
linker = guess_win_linker(env, ['link'], cls, version, for_machine) |
| 534 |
if ccache_exe and ccache_exe.found(): |
| 535 |
if ccache_exe.get_name() == 'ccache' and version_compare(ccache_exe.get_version(), '< 4.6'): |
| 536 |
mlog.warning('Visual Studio support requires ccache 4.6 or higher. You have ccache {}. '.format(ccache_exe.get_version()), once=True) |
| 537 |
ccache = [] |
| 538 |
return cls( |
| 539 |
ccache, compiler, version, for_machine, env, target, |
| 540 |
full_version=cl_signature, linker=linker) |
| 541 |
if 'PGI Compilers' in out: |
| 542 |
cls = c.PGICCompiler if lang == 'c' else cpp.PGICPPCompiler |
| 543 |
env.add_lang_args(cls.language, cls, for_machine) |
| 544 |
linker = linkers.PGIDynamicLinker(compiler, env, for_machine, cls.LINKER_OPTION_STYLE, [], version=version) |
| 545 |
return cls( |
| 546 |
ccache, compiler, version, for_machine, |
| 547 |
env, linker=linker) |
| 548 |
if 'NVIDIA Compilers and Tools' in out: |
| 549 |
cls = c.NvidiaHPC_CCompiler if lang == 'c' else cpp.NvidiaHPC_CPPCompiler |
| 550 |
env.add_lang_args(cls.language, cls, for_machine) |
| 551 |
linker = linkers.NvidiaHPC_DynamicLinker(compiler, env, for_machine, cls.LINKER_OPTION_STYLE, [], version=version) |
| 552 |
return cls( |
| 553 |
ccache, compiler, version, for_machine, |
| 554 |
env, linker=linker) |
| 555 |
if '(ICC)' in out: |
| 556 |
cls = c.IntelCCompiler if lang == 'c' else cpp.IntelCPPCompiler |
| 557 |
l = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 558 |
return cls( |
| 559 |
ccache, compiler, version, for_machine, env, |
| 560 |
full_version=full_version, linker=l) |
| 561 |
if 'Intel(R) oneAPI' in out: |
| 562 |
cls = c.IntelLLVMCCompiler if lang == 'c' else cpp.IntelLLVMCPPCompiler |
| 563 |
l = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 564 |
return cls( |
| 565 |
ccache, compiler, version, for_machine, env, |
| 566 |
full_version=full_version, linker=l) |
| 567 |
if 'ARM' in out and not ('Metrowerks' in out or 'Freescale' in out): |
| 568 |
cls = c.ArmCCompiler if lang == 'c' else cpp.ArmCPPCompiler |
| 569 |
env.add_lang_args(cls.language, cls, for_machine) |
| 570 |
linker = linkers.ArmDynamicLinker(env, for_machine, version=version) |
| 571 |
return cls( |
| 572 |
ccache, compiler, version, for_machine, |
| 573 |
env, full_version=full_version, linker=linker) |
| 574 |
if 'RX Family' in out: |
| 575 |
cls = c.CcrxCCompiler if lang == 'c' else cpp.CcrxCPPCompiler |
| 576 |
env.add_lang_args(cls.language, cls, for_machine) |
| 577 |
linker = linkers.CcrxDynamicLinker(env, for_machine, version=version) |
| 578 |
return cls( |
| 579 |
ccache, compiler, version, for_machine, env, |
| 580 |
full_version=full_version, linker=linker) |
| 581 |
|
| 582 |
if 'Microchip' in out: |
| 583 |
if 'XC32' in out: |
| 584 |
# XC32 versions always have the form 'vMAJOR.MINOR' |
| 585 |
match = re.search(r'XC32.*v(\d+\.\d+)', out) |
| 586 |
if match: |
| 587 |
version = match.group(1) |
| 588 |
else: |
| 589 |
raise EnvironmentException(f'Failed to detect XC32 compiler version: full version was\n{full_version}') |
| 590 |
|
| 591 |
cls = c.Xc32CCompiler if lang == 'c' else cpp.Xc32CPPCompiler |
| 592 |
defines = _get_gnu_compiler_defines(compiler, lang) |
| 593 |
cls.gcc_version = _get_gnu_version_from_defines(defines) |
| 594 |
|
| 595 |
env.add_lang_args(cls.language, cls, for_machine) |
| 596 |
linker = linkers.Xc32DynamicLinker(compiler, env, for_machine, cls.LINKER_OPTION_STYLE, [], version=version) |
| 597 |
|
| 598 |
return cls( |
| 599 |
ccache, compiler, version, for_machine, |
| 600 |
env, defines=defines, full_version=full_version, |
| 601 |
linker=linker) |
| 602 |
else: |
| 603 |
cls = c.Xc16CCompiler |
| 604 |
env.add_lang_args(cls.language, cls, for_machine) |
| 605 |
linker = linkers.Xc16DynamicLinker(env, for_machine, version=version) |
| 606 |
|
| 607 |
return cls( |
| 608 |
ccache, compiler, version, for_machine, env, |
| 609 |
full_version=full_version, linker=linker) |
| 610 |
|
| 611 |
if 'CompCert' in out: |
| 612 |
cls = c.CompCertCCompiler |
| 613 |
env.add_lang_args(cls.language, cls, for_machine) |
| 614 |
linker = linkers.CompCertDynamicLinker(env, for_machine, version=version) |
| 615 |
return cls( |
| 616 |
ccache, compiler, version, for_machine, env, |
| 617 |
full_version=full_version, linker=linker) |
| 618 |
|
| 619 |
if 'Metrowerks C/C++' in out or 'Freescale C/C++' in out: |
| 620 |
if 'ARM' in out: |
| 621 |
cls = c.MetrowerksCCompilerARM if lang == 'c' else cpp.MetrowerksCPPCompilerARM |
| 622 |
lnk = linkers.MetrowerksLinkerARM |
| 623 |
else: |
| 624 |
cls = c.MetrowerksCCompilerEmbeddedPowerPC if lang == 'c' else cpp.MetrowerksCPPCompilerEmbeddedPowerPC |
| 625 |
lnk = linkers.MetrowerksLinkerEmbeddedPowerPC |
| 626 |
|
| 627 |
mwcc_ver_match = unwrap(re.search(r'Version (\d+)\.(\d+)\.?(\d+)? build (\d+)', out)) |
| 628 |
compiler_version = '.'.join(x for x in mwcc_ver_match.groups() if x is not None) |
| 629 |
|
| 630 |
env.add_lang_args(cls.language, cls, for_machine) |
| 631 |
ld = env.lookup_binary_entry(for_machine, cls.language + '_ld') |
| 632 |
|
| 633 |
if ld is not None: |
| 634 |
_, o_ld, _ = Popen_safe(ld + ['--version']) |
| 635 |
|
| 636 |
mwld_ver_match = unwrap(re.search(r'Version (\d+)\.(\d+)\.?(\d+)? build (\d+)', o_ld)) |
| 637 |
linker_version = '.'.join(x for x in mwld_ver_match.groups() if x is not None) |
| 638 |
|
| 639 |
linker = lnk(ld, env, for_machine, version=linker_version) |
| 640 |
else: |
| 641 |
raise EnvironmentException(f'Failed to detect linker for {cls.id!r} compiler. Please update your cross file(s).') |
| 642 |
|
| 643 |
return cls( |
| 644 |
ccache, compiler, compiler_version, for_machine, env, |
| 645 |
full_version=full_version, linker=linker) |
| 646 |
if 'TASKING VX-toolset' in err: |
| 647 |
cls = c.TaskingCCompiler |
| 648 |
lnk = linkers.TaskingLinker |
| 649 |
|
| 650 |
tasking_ver_match = unwrap(re.search(r'v([0-9]+)\.([0-9]+)r([0-9]+) Build ([0-9]+)', err)) |
| 651 |
tasking_version = '.'.join(x for x in tasking_ver_match.groups() if x is not None) |
| 652 |
|
| 653 |
env.add_lang_args(cls.language, cls, for_machine) |
| 654 |
ld = env.lookup_binary_entry(for_machine, cls.language + '_ld') |
| 655 |
if ld is None: |
| 656 |
raise MesonException(f'{cls.language}_ld was not properly defined in your cross file') |
| 657 |
|
| 658 |
linker = lnk(ld, env, for_machine, version=tasking_version) |
| 659 |
return cls( |
| 660 |
ccache, compiler, tasking_version, for_machine, env, |
| 661 |
full_version=full_version, linker=linker) |
| 662 |
|
| 663 |
_handle_exceptions(popen_exceptions, compilers) |
| 664 |
raise EnvironmentException(f'Unknown compiler {compilers}') |
| 665 |
|
| 666 |
def detect_c_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 667 |
return _detect_c_or_cpp_compiler(env, 'c', for_machine) |
| 668 |
|
| 669 |
def detect_cpp_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 670 |
return _detect_c_or_cpp_compiler(env, 'cpp', for_machine) |
| 671 |
|
| 672 |
def detect_cuda_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 673 |
from .cuda import CudaCompiler, Phase |
| 674 |
from ..options import OptionKey |
| 675 |
from ..linkers.linkers import CudaLinker |
| 676 |
popen_exceptions = {} |
| 677 |
compilers, ccache_exe = _get_compilers(env, 'cuda', for_machine) |
| 678 |
ccache = ccache_exe.get_command() if (ccache_exe and ccache_exe.found()) else [] |
| 679 |
|
| 680 |
try: |
| 681 |
cpp_compiler = env.coredata.compilers[for_machine]['cpp'] |
| 682 |
except KeyError: |
| 683 |
raise MesonException('Cuda requires a working C++ compiler for the same machine, but one could not be found') |
| 684 |
|
| 685 |
for compiler in compilers: |
| 686 |
arg = '--version' |
| 687 |
try: |
| 688 |
p, out, err = Popen_safe_logged(compiler + [arg], msg='Detecting compiler via') |
| 689 |
except OSError as e: |
| 690 |
popen_exceptions[join_args(compiler + [arg])] = e |
| 691 |
continue |
| 692 |
# Example nvcc printout: |
| 693 |
# |
| 694 |
# nvcc: NVIDIA (R) Cuda compiler driver |
| 695 |
# Copyright (c) 2005-2018 NVIDIA Corporation |
| 696 |
# Built on Sat_Aug_25_21:08:01_CDT_2018 |
| 697 |
# Cuda compilation tools, release 10.0, V10.0.130 |
| 698 |
# |
| 699 |
# search_version() first finds the "10.0" after "release", |
| 700 |
# rather than the more precise "10.0.130" after "V". |
| 701 |
# The patch version number is occasionally important; For |
| 702 |
# instance, on Linux, |
| 703 |
# - CUDA Toolkit 8.0.44 requires NVIDIA Driver 367.48 |
| 704 |
# - CUDA Toolkit 8.0.61 requires NVIDIA Driver 375.26 |
| 705 |
# Split on the `V` to get the version, then strip additional lines after |
| 706 |
# that. |
| 707 |
version = out.strip().rsplit('V', maxsplit=1)[-1].split(maxsplit=1)[0] |
| 708 |
cls = CudaCompiler |
| 709 |
env.add_lang_args(cls.language, cls, for_machine) |
| 710 |
key = OptionKey('cuda_link_args', machine=for_machine) |
| 711 |
if not env.is_cross_build(for_machine): |
| 712 |
key = key.as_host() |
| 713 |
if key in env.options: |
| 714 |
# To fix LDFLAGS issue |
| 715 |
val = env.options[key] |
| 716 |
assert isinstance(val, list) |
| 717 |
env.coredata.optstore.set_option(key, cls.to_host_flags_base(val, Phase.LINKER)) |
| 718 |
linker = CudaLinker(compiler, env, for_machine, CudaCompiler.LINKER_OPTION_STYLE, [], version=CudaLinker.parse_version()) |
| 719 |
return cls(ccache, compiler, version, for_machine, cpp_compiler, env, linker=linker) |
| 720 |
|
| 721 |
_handle_exceptions(popen_exceptions, compilers) |
| 722 |
raise EnvironmentException(f'Unknown compiler {compilers}') |
| 723 |
|
| 724 |
def detect_fortran_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 725 |
from . import fortran |
| 726 |
from ..linkers import linkers |
| 727 |
popen_exceptions: T.Dict[str, T.Union[Exception, str]] = {} |
| 728 |
compilers, ccache = _get_compilers(env, 'fortran', for_machine) |
| 729 |
cls: T.Type[FortranCompiler] |
| 730 |
for compiler in compilers: |
| 731 |
# capture help text for possible fallback |
| 732 |
try: |
| 733 |
_, help_out, _ = Popen_safe_logged(compiler + ['--help'], msg='Detecting compiler via') |
| 734 |
except OSError as e: |
| 735 |
popen_exceptions[join_args(compiler + ['--help'])] = e |
| 736 |
help_out = '' |
| 737 |
|
| 738 |
for arg in ['--version', '-V']: |
| 739 |
try: |
| 740 |
p, out, err = Popen_safe_logged(compiler + [arg], msg='Detecting compiler via') |
| 741 |
except OSError as e: |
| 742 |
popen_exceptions[join_args(compiler + [arg])] = e |
| 743 |
continue |
| 744 |
|
| 745 |
version = search_version(out) |
| 746 |
full_version = out.split('\n', 1)[0] |
| 747 |
|
| 748 |
guess_gcc_or_lcc: T.Optional[str] = None |
| 749 |
if 'GNU Fortran' in out: |
| 750 |
guess_gcc_or_lcc = 'gcc' |
| 751 |
if 'e2k' in out and 'lcc' in out: |
| 752 |
guess_gcc_or_lcc = 'lcc' |
| 753 |
|
| 754 |
if guess_gcc_or_lcc: |
| 755 |
defines = _get_gnu_compiler_defines(compiler, 'fortran') |
| 756 |
if not defines: |
| 757 |
popen_exceptions[join_args(compiler)] = 'no pre-processor defines' |
| 758 |
continue |
| 759 |
if guess_gcc_or_lcc == 'lcc': |
| 760 |
version = _get_lcc_version_from_defines(defines) |
| 761 |
cls = fortran.ElbrusFortranCompiler |
| 762 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 763 |
return cls( |
| 764 |
compiler, version, for_machine, env, |
| 765 |
defines, full_version=full_version, linker=linker) |
| 766 |
else: |
| 767 |
version = _get_gnu_version_from_defines(defines) |
| 768 |
cls = fortran.GnuFortranCompiler |
| 769 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 770 |
return cls( |
| 771 |
compiler, version, for_machine, env, |
| 772 |
defines, full_version=full_version, linker=linker) |
| 773 |
|
| 774 |
if 'Arm C/C++/Fortran Compiler' in out: |
| 775 |
cls = fortran.ArmLtdFlangFortranCompiler |
| 776 |
arm_ver_match = unwrap(re.search(r'version (\d+)\.(\d+)\.?(\d+)? \(build number (\d+)\)', out)) |
| 777 |
version = '.'.join([x for x in arm_ver_match.groups() if x is not None]) |
| 778 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 779 |
return cls( |
| 780 |
compiler, version, for_machine, env, |
| 781 |
linker=linker) |
| 782 |
if 'G95' in out: |
| 783 |
cls = fortran.G95FortranCompiler |
| 784 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 785 |
return cls( |
| 786 |
compiler, version, for_machine, env, |
| 787 |
full_version=full_version, linker=linker) |
| 788 |
|
| 789 |
if 'Sun Fortran' in err: |
| 790 |
version = search_version(err) |
| 791 |
cls = fortran.SunFortranCompiler |
| 792 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 793 |
return cls( |
| 794 |
compiler, version, for_machine, env, |
| 795 |
full_version=full_version, linker=linker) |
| 796 |
|
| 797 |
if 'Intel(R) Fortran Compiler for applications' in err: |
| 798 |
version = search_version(err) |
| 799 |
target = 'x86' if 'IA-32' in err else 'x86_64' |
| 800 |
cls = fortran.IntelLLVMClFortranCompiler |
| 801 |
env.add_lang_args(cls.language, cls, for_machine) |
| 802 |
linker = guess_win_linker( |
| 803 |
env, ['link'], cls, version, |
| 804 |
for_machine) |
| 805 |
return cls( |
| 806 |
compiler, version, for_machine, env, |
| 807 |
target, linker=linker) |
| 808 |
|
| 809 |
if 'Intel(R) Visual Fortran' in err or 'Intel(R) Fortran' in err: |
| 810 |
version = search_version(err) |
| 811 |
target = 'x86' if 'IA-32' in err else 'x86_64' |
| 812 |
cls = fortran.IntelClFortranCompiler |
| 813 |
env.add_lang_args(cls.language, cls, for_machine) |
| 814 |
linker = linkers.XilinkDynamicLinker(env, for_machine, [], version=version) |
| 815 |
return cls( |
| 816 |
compiler, version, for_machine, env, |
| 817 |
target, linker=linker) |
| 818 |
|
| 819 |
if 'ifort (IFORT)' in out: |
| 820 |
cls = fortran.IntelFortranCompiler |
| 821 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 822 |
return cls( |
| 823 |
compiler, version, for_machine, env, |
| 824 |
full_version=full_version, linker=linker) |
| 825 |
|
| 826 |
if 'ifx (IFORT)' in out or 'ifx (IFX)' in out: |
| 827 |
cls = fortran.IntelLLVMFortranCompiler |
| 828 |
linker = guess_nix_linker(env, compiler, cls, version, for_machine) |
| 829 |
return cls( |
| 830 |
compiler, version, for_machine, env, |
| 831 |
full_version=full_version, linker=linker) |
| 832 |
|
| 833 |
if 'PathScale EKOPath(tm)' in err: |
| 834 |
return fortran.PathScaleFortranCompiler( |
| 835 |
compiler, version, for_machine, env, |
| 836 |
full_version=full_version) |
| 837 |
|
| 838 |
if 'PGI Compilers' in out: |
| 839 |
cls = fortran.PGIFortranCompiler |
| 840 |
env.add_lang_args(cls.language, cls, for_machine) |
| 841 |
linker = linkers.PGIDynamicLinker(compiler, env, for_machine, |
| 842 |
cls.LINKER_OPTION_STYLE, [], version=version) |
| 843 |
return cls( |
| 844 |
compiler, version, for_machine, env, |
| 845 |
full_version=full_version, linker=linker) |
| 846 |
|
| 847 |
if 'NVIDIA Compilers and Tools' in out: |
| 848 |
cls = fortran.NvidiaHPC_FortranCompiler |
| 849 |
env.add_lang_args(cls.language, cls, for_machine) |
| 850 |
linker = linkers.PGIDynamicLinker(compiler, env, for_machine, |
| 851 |
cls.LINKER_OPTION_STYLE, [], version=version) |
| 852 |
return cls( |
| 853 |
compiler, version, for_machine, env, |
| 854 |
full_version=full_version, linker=linker) |
| 855 |
|
| 856 |
def _get_linker_try_windows(cls: T.Type['Compiler']) -> T.Optional['DynamicLinker']: |
| 857 |
linker = None |
| 858 |
if 'windows' in out or env.machines[for_machine].is_windows(): |
| 859 |
# If we're in a MINGW context this actually will use a gnu |
| 860 |
# style ld, but for flang on "real" windows we'll use |
| 861 |
# either link.exe or lld-link.exe |
| 862 |
try: |
| 863 |
linker = guess_win_linker( |
| 864 |
env, compiler, cls, version, |
| 865 |
for_machine, invoked_directly=False |
| 866 |
) |
| 867 |
except MesonException: |
| 868 |
pass |
| 869 |
if linker is None: |
| 870 |
linker = guess_nix_linker(env, compiler, cls, |
| 871 |
version, for_machine) |
| 872 |
return linker |
| 873 |
|
| 874 |
if 'flang-new' in out or 'flang LLVM compiler' in help_out: |
| 875 |
cls = fortran.LlvmFlangFortranCompiler |
| 876 |
linker = _get_linker_try_windows(cls) |
| 877 |
return cls( |
| 878 |
compiler, version, for_machine, env, |
| 879 |
full_version=full_version, linker=linker) |
| 880 |
|
| 881 |
if 'flang' in out or 'clang' in out: |
| 882 |
cls = fortran.ClassicFlangFortranCompiler |
| 883 |
linker = _get_linker_try_windows(cls) |
| 884 |
return cls( |
| 885 |
compiler, version, for_machine, env, |
| 886 |
full_version=full_version, linker=linker) |
| 887 |
|
| 888 |
if 'Open64 Compiler Suite' in err: |
| 889 |
cls = fortran.Open64FortranCompiler |
| 890 |
linker = guess_nix_linker(env, |
| 891 |
compiler, cls, version, for_machine) |
| 892 |
return cls( |
| 893 |
compiler, version, for_machine, env, |
| 894 |
full_version=full_version, linker=linker) |
| 895 |
|
| 896 |
if 'NAG Fortran' in err: |
| 897 |
full_version = err.split('\n', 1)[0] |
| 898 |
version = full_version.split()[-1] |
| 899 |
cls = fortran.NAGFortranCompiler |
| 900 |
env.add_lang_args(cls.language, cls, for_machine) |
| 901 |
linker = linkers.NAGDynamicLinker( |
| 902 |
compiler, env, for_machine, cls.LINKER_OPTION_STYLE, [], |
| 903 |
version=version) |
| 904 |
return cls( |
| 905 |
compiler, version, for_machine, env, |
| 906 |
full_version=full_version, linker=linker) |
| 907 |
|
| 908 |
_handle_exceptions(popen_exceptions, compilers) |
| 909 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 910 |
|
| 911 |
def detect_objc_compiler(env: 'Environment', for_machine: MachineChoice) -> 'Compiler': |
| 912 |
return _detect_objc_or_objcpp_compiler(env, 'objc', for_machine) |
| 913 |
|
| 914 |
def detect_objcpp_compiler(env: 'Environment', for_machine: MachineChoice) -> 'Compiler': |
| 915 |
return _detect_objc_or_objcpp_compiler(env, 'objcpp', for_machine) |
| 916 |
|
| 917 |
def _detect_objc_or_objcpp_compiler(env: 'Environment', lang: str, for_machine: MachineChoice) -> 'Compiler': |
| 918 |
from . import objc, objcpp |
| 919 |
popen_exceptions: T.Dict[str, T.Union[Exception, str]] = {} |
| 920 |
compilers, ccache_exe = _get_compilers(env, lang, for_machine) |
| 921 |
ccache = ccache_exe.get_command() if (ccache_exe and ccache_exe.found()) else [] |
| 922 |
comp: T.Union[T.Type[objc.ObjCCompiler], T.Type[objcpp.ObjCPPCompiler]] |
| 923 |
|
| 924 |
for compiler in compilers: |
| 925 |
arg = ['--version'] |
| 926 |
try: |
| 927 |
p, out, err = Popen_safe_logged(compiler + arg, msg='Detecting compiler via') |
| 928 |
except OSError as e: |
| 929 |
popen_exceptions[join_args(compiler + arg)] = e |
| 930 |
continue |
| 931 |
version = search_version(out) |
| 932 |
if 'Free Software Foundation' in out: |
| 933 |
defines = _get_gnu_compiler_defines(compiler, lang) |
| 934 |
if not defines: |
| 935 |
popen_exceptions[join_args(compiler)] = 'no pre-processor defines' |
| 936 |
continue |
| 937 |
version = _get_gnu_version_from_defines(defines) |
| 938 |
comp = objc.GnuObjCCompiler if lang == 'objc' else objcpp.GnuObjCPPCompiler |
| 939 |
linker = guess_nix_linker(env, compiler, comp, version, for_machine) |
| 940 |
c = comp( |
| 941 |
ccache, compiler, version, for_machine, env, |
| 942 |
defines, linker=linker) |
| 943 |
if not c.compiles('int main(void) { return 0; }')[0]: |
| 944 |
popen_exceptions[join_args(compiler)] = f'GCC was not built with support for {"objective-c" if lang == "objc" else "objective-c++"}' |
| 945 |
continue |
| 946 |
return c |
| 947 |
if 'clang' in out: |
| 948 |
linker = None |
| 949 |
defines = _get_clang_compiler_defines(compiler, lang) |
| 950 |
if not defines: |
| 951 |
popen_exceptions[join_args(compiler)] = 'no pre-processor defines' |
| 952 |
continue |
| 953 |
if 'Apple' in out: |
| 954 |
comp = objc.AppleClangObjCCompiler if lang == 'objc' else objcpp.AppleClangObjCPPCompiler |
| 955 |
else: |
| 956 |
comp = objc.ClangObjCCompiler if lang == 'objc' else objcpp.ClangObjCPPCompiler |
| 957 |
if 'windows' in out or env.machines[for_machine].is_windows(): |
| 958 |
# If we're in a MINGW context this actually will use a gnu style ld |
| 959 |
try: |
| 960 |
linker = guess_win_linker(env, compiler, comp, version, for_machine) |
| 961 |
except MesonException: |
| 962 |
pass |
| 963 |
|
| 964 |
if not linker: |
| 965 |
linker = guess_nix_linker(env, compiler, comp, version, for_machine) |
| 966 |
return comp( |
| 967 |
ccache, compiler, version, for_machine, |
| 968 |
env, linker=linker, defines=defines) |
| 969 |
_handle_exceptions(popen_exceptions, compilers) |
| 970 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 971 |
|
| 972 |
def detect_java_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 973 |
from .java import JavaCompiler |
| 974 |
exelist = env.lookup_binary_entry(for_machine, 'java') |
| 975 |
if exelist is None: |
| 976 |
# TODO support fallback |
| 977 |
exelist = [defaults['java'][0]] |
| 978 |
|
| 979 |
try: |
| 980 |
p, out, err = Popen_safe_logged(exelist + ['-version'], msg='Detecting compiler via') |
| 981 |
except OSError: |
| 982 |
raise EnvironmentException('Could not execute Java compiler: {}'.format(join_args(exelist))) |
| 983 |
if 'javac' in out or 'javac' in err: |
| 984 |
version = search_version(err if 'javac' in err else out) |
| 985 |
if not version or version == 'unknown version': |
| 986 |
parts = (err if 'javac' in err else out).split() |
| 987 |
if len(parts) > 1: |
| 988 |
version = parts[1] |
| 989 |
comp_class = JavaCompiler |
| 990 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 991 |
return comp_class(exelist, version, for_machine, env) |
| 992 |
raise EnvironmentException('Unknown compiler: ' + join_args(exelist)) |
| 993 |
|
| 994 |
def detect_cs_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 995 |
from . import cs |
| 996 |
compilers, ccache = _get_compilers(env, 'cs', for_machine) |
| 997 |
popen_exceptions = {} |
| 998 |
for comp in compilers: |
| 999 |
try: |
| 1000 |
p, out, err = Popen_safe_logged(comp + ['--version'], msg='Detecting compiler via') |
| 1001 |
except OSError as e: |
| 1002 |
popen_exceptions[join_args(comp + ['--version'])] = e |
| 1003 |
continue |
| 1004 |
|
| 1005 |
version = search_version(out) |
| 1006 |
cls: T.Type[cs.CsCompiler] |
| 1007 |
if 'Mono' in out: |
| 1008 |
cls = cs.MonoCompiler |
| 1009 |
elif "Visual C#" in out: |
| 1010 |
cls = cs.VisualStudioCsCompiler |
| 1011 |
else: |
| 1012 |
continue |
| 1013 |
env.add_lang_args(cls.language, cls, for_machine) |
| 1014 |
return cls(comp, version, for_machine, env) |
| 1015 |
|
| 1016 |
_handle_exceptions(popen_exceptions, compilers) |
| 1017 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1018 |
|
| 1019 |
def detect_cython_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 1020 |
"""Search for a cython compiler.""" |
| 1021 |
from .cython import CythonCompiler |
| 1022 |
compilers, _ = _get_compilers(env, 'cython', MachineChoice.BUILD) |
| 1023 |
|
| 1024 |
popen_exceptions: T.Dict[str, Exception] = {} |
| 1025 |
for comp in compilers: |
| 1026 |
try: |
| 1027 |
_, out, err = Popen_safe_logged(comp + ['-V'], msg='Detecting compiler via') |
| 1028 |
except OSError as e: |
| 1029 |
popen_exceptions[join_args(comp + ['-V'])] = e |
| 1030 |
continue |
| 1031 |
|
| 1032 |
version: T.Optional[str] = None |
| 1033 |
# 3.0 |
| 1034 |
if 'Cython' in out: |
| 1035 |
version = search_version(out) |
| 1036 |
# older |
| 1037 |
elif 'Cython' in err: |
| 1038 |
version = search_version(err) |
| 1039 |
if version is not None: |
| 1040 |
comp_class = CythonCompiler |
| 1041 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 1042 |
return comp_class([], comp, version, for_machine, env) |
| 1043 |
_handle_exceptions(popen_exceptions, compilers) |
| 1044 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1045 |
|
| 1046 |
def detect_vala_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 1047 |
from .vala import ValaCompiler |
| 1048 |
exelist = env.lookup_binary_entry(for_machine, 'vala') |
| 1049 |
if exelist is None: |
| 1050 |
# TODO support fallback |
| 1051 |
exelist = [defaults['vala'][0]] |
| 1052 |
|
| 1053 |
try: |
| 1054 |
p, out = Popen_safe_logged(exelist + ['--version'], msg='Detecting compiler via')[0:2] |
| 1055 |
except OSError: |
| 1056 |
raise EnvironmentException('Could not execute Vala compiler: {}'.format(join_args(exelist))) |
| 1057 |
version = search_version(out) |
| 1058 |
if 'Vala' in out: |
| 1059 |
comp_class = ValaCompiler |
| 1060 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 1061 |
return comp_class(exelist, version, for_machine, env) |
| 1062 |
raise EnvironmentException('Unknown compiler: ' + join_args(exelist)) |
| 1063 |
|
| 1064 |
def detect_rust_compiler(env: 'Environment', for_machine: MachineChoice) -> RustCompiler: |
| 1065 |
from . import rust |
| 1066 |
from ..linkers import linkers |
| 1067 |
popen_exceptions: T.Dict[str, Exception] = {} |
| 1068 |
compilers, _ = _get_compilers(env, 'rust', for_machine) |
| 1069 |
override = env.lookup_binary_entry(for_machine, 'rust_ld') |
| 1070 |
|
| 1071 |
for compiler in compilers: |
| 1072 |
arg = ['--version'] |
| 1073 |
try: |
| 1074 |
out = Popen_safe_logged(compiler + arg, msg='Detecting compiler via')[1] |
| 1075 |
except OSError as e: |
| 1076 |
popen_exceptions[join_args(compiler + arg)] = e |
| 1077 |
continue |
| 1078 |
|
| 1079 |
# Full version contains the "-nightly" or "-beta" suffixes, but version |
| 1080 |
# should just be X.Y.Z |
| 1081 |
full_version = search_version(out) |
| 1082 |
version = full_version.split('-', 1)[0] |
| 1083 |
|
| 1084 |
cls: T.Type[RustCompiler] = rust.RustCompiler |
| 1085 |
|
| 1086 |
# Clippy is a wrapper around rustc, but it doesn't have rustc in its |
| 1087 |
# output. We can otherwise treat it as rustc. |
| 1088 |
if 'clippy' in out: |
| 1089 |
# clippy returns its own version and not the rustc version by |
| 1090 |
# default so try harder here to get the correct version. |
| 1091 |
# Also replace the whole output with the rustc output in |
| 1092 |
# case this is later used for other purposes. |
| 1093 |
arg = ['--rustc', '--version'] |
| 1094 |
try: |
| 1095 |
out = Popen_safe(compiler + arg)[1] |
| 1096 |
except OSError as e: |
| 1097 |
popen_exceptions[join_args(compiler + arg)] = e |
| 1098 |
continue |
| 1099 |
full_version = search_version(out) |
| 1100 |
version = full_version.split('-', 1)[0] |
| 1101 |
|
| 1102 |
cls = rust.ClippyRustCompiler |
| 1103 |
mlog.deprecation( |
| 1104 |
'clippy-driver is not intended as a general purpose compiler. ' |
| 1105 |
'You can use "ninja clippy" in order to run clippy on a ' |
| 1106 |
'meson project.') |
| 1107 |
|
| 1108 |
if 'rustc' in out: |
| 1109 |
# On Linux and mac rustc will invoke gcc (clang for mac |
| 1110 |
# presumably) and it can do this windows, for dynamic linking. |
| 1111 |
# this means the easiest way to figure out what linker to use |
| 1112 |
# is to just get the value of the C compiler and use that as |
| 1113 |
# the basis of the rust linker. |
| 1114 |
# |
| 1115 |
# However, there are two things we need to change, if CC is not |
| 1116 |
# the default use that, and second add the necessary arguments |
| 1117 |
# to rust to use -fuse-ld |
| 1118 |
# |
| 1119 |
# For MSVC targets, require an MSVC-compatible C compiler to get |
| 1120 |
# the corresponding linker |
| 1121 |
rust_target = rust.parse_target(compiler) |
| 1122 |
if rust_target and rust_target.endswith('-msvc'): |
| 1123 |
try: |
| 1124 |
cc = _detect_c_or_cpp_compiler(env, 'c', for_machine, |
| 1125 |
override_compilers=[['cl'], ['clang-cl']]) |
| 1126 |
except EnvironmentException: |
| 1127 |
popen_exceptions[join_args(compiler)] = \ |
| 1128 |
EnvironmentException('No MSVC-compatible C compiler found for MSVC Rust target') |
| 1129 |
continue |
| 1130 |
else: |
| 1131 |
try: |
| 1132 |
cc = detect_c_compiler(env, for_machine) |
| 1133 |
except EnvironmentException as e: |
| 1134 |
popen_exceptions[join_args(compiler)] = e |
| 1135 |
continue |
| 1136 |
|
| 1137 |
is_link_exe = isinstance(cc.linker, linkers.VisualStudioLikeLinkerMixin) |
| 1138 |
|
| 1139 |
if any(a.startswith('linker=') for a in compiler): |
| 1140 |
mlog.warning( |
| 1141 |
'Please do not put -C linker= in your compiler ' |
| 1142 |
'command, set rust_ld=command in your cross file ' |
| 1143 |
'or use the RUSTC_LD environment variable, otherwise meson ' |
| 1144 |
'will override your selection.') |
| 1145 |
|
| 1146 |
compiler = compiler.copy() # avoid mutating the original list |
| 1147 |
|
| 1148 |
if override is None: |
| 1149 |
assert cc.linker is not None, 'for mypy' |
| 1150 |
linker: DynamicLinker |
| 1151 |
exelist = cc.get_linker_exelist() |
| 1152 |
if is_link_exe: |
| 1153 |
# This trickery with type() gets us the class of the linker |
| 1154 |
# so we can initialize a new copy for the Rust Compiler |
| 1155 |
# Due to initializer mismatch we can't use the VisualStudioLikeMixin here |
| 1156 |
# But all of these have the same API so we can just pick one. |
| 1157 |
linker = T.cast('T.Type[linkers.MSVCDynamicLinker]', type(cc.linker))( |
| 1158 |
env, for_machine, always_args=[], |
| 1159 |
exelist=exelist, version=cc.linker.version, |
| 1160 |
direct=True, machine=cc.linker.machine) |
| 1161 |
else: |
| 1162 |
linker = type(cc.linker)(exelist, env, for_machine, cc.LINKER_OPTION_STYLE, |
| 1163 |
always_args=[], system=cc.linker.system, |
| 1164 |
version=cc.linker.version) |
| 1165 |
exelist += cc.linker.get_always_args() |
| 1166 |
elif 'link' in override[0]: |
| 1167 |
linker = guess_win_linker(env, |
| 1168 |
override, cls, version, for_machine, wrap_linker_args=False) |
| 1169 |
# rustc takes linker arguments without a prefix, and |
| 1170 |
# inserts the correct prefix itself. |
| 1171 |
assert isinstance(linker, linkers.VisualStudioLikeLinkerMixin) |
| 1172 |
linker.direct = True |
| 1173 |
exelist = linker.get_exelist() |
| 1174 |
else: |
| 1175 |
# On linux and macos rust will invoke the c compiler for |
| 1176 |
# linking, on windows it will use lld-link or link.exe. |
| 1177 |
# we will simply ask for the C compiler that corresponds to |
| 1178 |
# it, and use that. |
| 1179 |
cc = _detect_c_or_cpp_compiler(env, 'c', for_machine, override_compilers=[override]) |
| 1180 |
linker = cc.linker |
| 1181 |
exelist = cc.get_linker_exelist() |
| 1182 |
|
| 1183 |
c = exelist.pop(0) |
| 1184 |
compiler.extend(cls.use_linker_args(c, '')) |
| 1185 |
compiler.extend(rust.rustc_link_args(exelist)) |
| 1186 |
|
| 1187 |
env.add_lang_args(cls.language, cls, for_machine) |
| 1188 |
return cls( |
| 1189 |
compiler, version, for_machine, env, |
| 1190 |
linker=linker, full_version=full_version) |
| 1191 |
|
| 1192 |
_handle_exceptions(popen_exceptions, compilers) |
| 1193 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1194 |
|
| 1195 |
def detect_d_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 1196 |
from . import c, d |
| 1197 |
|
| 1198 |
# Detect the target architecture, required for proper architecture handling on Windows. |
| 1199 |
# MSVC compiler is required for correct platform detection. |
| 1200 |
c_compiler: CompilerDict = {'c': detect_c_compiler(env, for_machine)} |
| 1201 |
is_msvc = isinstance(c_compiler['c'], c.VisualStudioCCompiler) |
| 1202 |
if not is_msvc: |
| 1203 |
c_compiler = {} |
| 1204 |
|
| 1205 |
arch = detect_cpu_family(c_compiler) |
| 1206 |
if is_msvc and arch == 'x86': |
| 1207 |
arch = 'x86_mscoff' |
| 1208 |
|
| 1209 |
popen_exceptions = {} |
| 1210 |
info = env.machines[for_machine] |
| 1211 |
compilers, ccache = _get_compilers(env, 'd', for_machine) |
| 1212 |
cls: T.Type[d.DCompiler] |
| 1213 |
for exelist in compilers: |
| 1214 |
# Search for a D compiler. |
| 1215 |
# We prefer LDC over GDC unless overridden with the DC |
| 1216 |
# environment variable because LDC has a much more |
| 1217 |
# up to date language version at time (2016). |
| 1218 |
if os.path.basename(exelist[-1]).startswith(('ldmd', 'gdmd')): |
| 1219 |
raise EnvironmentException( |
| 1220 |
f'Meson does not support {exelist[-1]} as it is only a DMD frontend for another compiler.' |
| 1221 |
'Please provide a valid value for DC or unset it so that Meson can resolve the compiler by itself.') |
| 1222 |
try: |
| 1223 |
p, out = Popen_safe(exelist + ['--version'])[0:2] |
| 1224 |
except OSError as e: |
| 1225 |
popen_exceptions[join_args(exelist + ['--version'])] = e |
| 1226 |
continue |
| 1227 |
version = search_version(out) |
| 1228 |
full_version = out.split('\n', 1)[0] |
| 1229 |
|
| 1230 |
# The OpenD fork should stay close enough to upstream D (in |
| 1231 |
# the areas that interest us) to allow supporting them both |
| 1232 |
# without much hassle. |
| 1233 |
# See: https://github.com/orgs/opendlang/discussions/56 |
| 1234 |
if 'LLVM D compiler' in out or 'LLVM Open D compiler' in out: |
| 1235 |
cls = d.LLVMDCompiler |
| 1236 |
# LDC seems to require a file |
| 1237 |
# We cannot use NamedTemporaryFile on windows, its documented |
| 1238 |
# to not work for our uses. So, just use mkstemp and only have |
| 1239 |
# one path for simplicity. |
| 1240 |
o, f = tempfile.mkstemp('.d') |
| 1241 |
os.close(o) |
| 1242 |
|
| 1243 |
try: |
| 1244 |
if info.is_windows() or info.is_cygwin(): |
| 1245 |
objfile = os.path.basename(f)[:-1] + 'obj' |
| 1246 |
extra_args = [f] |
| 1247 |
if env.is_cross_build(for_machine): |
| 1248 |
extra_args.append(f'-mtriple={info.cpu}-windows') |
| 1249 |
|
| 1250 |
linker = guess_win_linker(env, |
| 1251 |
exelist, |
| 1252 |
cls, full_version, for_machine, |
| 1253 |
wrap_linker_args=True, invoked_directly=False, |
| 1254 |
extra_args=extra_args) |
| 1255 |
else: |
| 1256 |
# LDC writes an object file to the current working directory. |
| 1257 |
# Clean it up. |
| 1258 |
objfile = os.path.basename(f)[:-1] + 'o' |
| 1259 |
linker = guess_nix_linker(env, |
| 1260 |
exelist, cls, full_version, for_machine, |
| 1261 |
extra_args=[f]) |
| 1262 |
finally: |
| 1263 |
windows_proof_rm(f) |
| 1264 |
windows_proof_rm(objfile) |
| 1265 |
|
| 1266 |
return cls( |
| 1267 |
exelist, version, for_machine, env, arch, |
| 1268 |
full_version=full_version, linker=linker, |
| 1269 |
version_output=out) |
| 1270 |
elif 'gdc' in out: |
| 1271 |
cls = d.GnuDCompiler |
| 1272 |
linker = guess_nix_linker(env, exelist, cls, version, for_machine) |
| 1273 |
return cls( |
| 1274 |
exelist, version, for_machine, env, arch, |
| 1275 |
full_version=full_version, linker=linker) |
| 1276 |
elif 'The D Language Foundation' in out or 'Digital Mars' in out: |
| 1277 |
cls = d.DmdDCompiler |
| 1278 |
# DMD seems to require a file |
| 1279 |
# We cannot use NamedTemporaryFile on windows, its documented |
| 1280 |
# to not work for our uses. So, just use mkstemp and only have |
| 1281 |
# one path for simplicity. |
| 1282 |
o, f = tempfile.mkstemp('.d') |
| 1283 |
os.close(o) |
| 1284 |
|
| 1285 |
# DMD as different detection logic for x86 and x86_64 |
| 1286 |
arch_arg = '-m64' if arch == 'x86_64' else '-m32' |
| 1287 |
|
| 1288 |
try: |
| 1289 |
if info.is_windows() or info.is_cygwin(): |
| 1290 |
objfile = os.path.basename(f)[:-1] + 'obj' |
| 1291 |
linker = guess_win_linker(env, |
| 1292 |
exelist, cls, full_version, for_machine, |
| 1293 |
invoked_directly=False, extra_args=[f, arch_arg]) |
| 1294 |
else: |
| 1295 |
objfile = os.path.basename(f)[:-1] + 'o' |
| 1296 |
linker = guess_nix_linker(env, |
| 1297 |
exelist, cls, full_version, for_machine, |
| 1298 |
extra_args=[f, arch_arg]) |
| 1299 |
finally: |
| 1300 |
windows_proof_rm(f) |
| 1301 |
windows_proof_rm(objfile) |
| 1302 |
|
| 1303 |
return cls( |
| 1304 |
exelist, version, for_machine, env, arch, |
| 1305 |
full_version=full_version, linker=linker) |
| 1306 |
raise EnvironmentException('Unknown compiler: ' + join_args(exelist)) |
| 1307 |
|
| 1308 |
_handle_exceptions(popen_exceptions, compilers) |
| 1309 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1310 |
|
| 1311 |
def detect_swift_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 1312 |
from .swift import SwiftCompiler |
| 1313 |
exelist = env.lookup_binary_entry(for_machine, 'swift') |
| 1314 |
if exelist is None: |
| 1315 |
# TODO support fallback |
| 1316 |
exelist = [defaults['swift'][0]] |
| 1317 |
|
| 1318 |
try: |
| 1319 |
p, _, err = Popen_safe_logged(exelist + ['-v'], msg='Detecting compiler via') |
| 1320 |
except OSError: |
| 1321 |
raise EnvironmentException('Could not execute Swift compiler: {}'.format(join_args(exelist))) |
| 1322 |
version = search_version(err) |
| 1323 |
if 'Swift' in err: |
| 1324 |
# As for 5.0.1 swiftc *requires* a file to check the linker: |
| 1325 |
with tempfile.NamedTemporaryFile(suffix='.swift') as f: |
| 1326 |
cls = SwiftCompiler |
| 1327 |
linker = guess_nix_linker(env, |
| 1328 |
exelist, cls, version, for_machine, |
| 1329 |
extra_args=[f.name, '-o', '/dev/null']) |
| 1330 |
return cls( |
| 1331 |
exelist, version, for_machine, env, linker=linker) |
| 1332 |
|
| 1333 |
raise EnvironmentException('Unknown compiler: ' + join_args(exelist)) |
| 1334 |
|
| 1335 |
def detect_nasm_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 1336 |
from .asm import NasmCompiler, YasmCompiler, MetrowerksAsmCompilerARM, MetrowerksAsmCompilerEmbeddedPowerPC |
| 1337 |
|
| 1338 |
# When cross compiling and nasm is not defined in the cross file we can |
| 1339 |
# fallback to the build machine nasm. |
| 1340 |
compilers, _ = _get_compilers(env, 'nasm', for_machine, allow_build_machine=True) |
| 1341 |
|
| 1342 |
# We need a C compiler to properly detect the machine info and linker |
| 1343 |
cc = detect_c_compiler(env, for_machine) |
| 1344 |
if not env.is_cross_build(for_machine): |
| 1345 |
env.update_build_machine({'c': cc}) |
| 1346 |
|
| 1347 |
popen_exceptions: T.Dict[str, Exception] = {} |
| 1348 |
for comp in compilers: |
| 1349 |
if comp == ['nasm'] and is_windows() and not shutil.which(comp[0]): |
| 1350 |
# nasm is not in PATH on Windows by default |
| 1351 |
default_path = os.path.join(os.environ['ProgramFiles'], 'NASM') |
| 1352 |
comp[0] = shutil.which(comp[0], path=default_path) or comp[0] |
| 1353 |
try: |
| 1354 |
output = Popen_safe_logged(comp + ['--version'], msg='Detecting compiler via')[1] |
| 1355 |
except OSError as e: |
| 1356 |
popen_exceptions[' '.join(comp + ['--version'])] = e |
| 1357 |
continue |
| 1358 |
|
| 1359 |
version = search_version(output) |
| 1360 |
comp_class: T.Type[ASMCompiler] |
| 1361 |
if 'NASM' in output: |
| 1362 |
comp_class = NasmCompiler |
| 1363 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 1364 |
return comp_class([], comp, version, for_machine, env, cc.linker) |
| 1365 |
elif 'yasm' in output: |
| 1366 |
comp_class = YasmCompiler |
| 1367 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 1368 |
return comp_class([], comp, version, for_machine, env, cc.linker) |
| 1369 |
elif 'Metrowerks' in output or 'Freescale' in output: |
| 1370 |
if 'ARM' in output: |
| 1371 |
comp_class_mwasmarm = MetrowerksAsmCompilerARM |
| 1372 |
env.add_lang_args(comp_class_mwasmarm.language, comp_class_mwasmarm, for_machine) |
| 1373 |
return comp_class_mwasmarm([], comp, version, for_machine, env, cc.linker) |
| 1374 |
else: |
| 1375 |
comp_class_mwasmeppc = MetrowerksAsmCompilerEmbeddedPowerPC |
| 1376 |
env.add_lang_args(comp_class_mwasmeppc.language, comp_class_mwasmeppc, for_machine) |
| 1377 |
return comp_class_mwasmeppc([], comp, version, for_machine, env, cc.linker) |
| 1378 |
|
| 1379 |
_handle_exceptions(popen_exceptions, compilers) |
| 1380 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1381 |
|
| 1382 |
def detect_masm_compiler(env: 'Environment', for_machine: MachineChoice) -> Compiler: |
| 1383 |
# We need a C compiler to properly detect the machine info and linker |
| 1384 |
cc = detect_c_compiler(env, for_machine) |
| 1385 |
if not env.is_cross_build(for_machine): |
| 1386 |
env.update_build_machine({'c': cc}) |
| 1387 |
|
| 1388 |
info = env.machines[for_machine] |
| 1389 |
|
| 1390 |
from .asm import MasmCompiler, MasmARMCompiler |
| 1391 |
comp_class: T.Type[ASMCompiler] |
| 1392 |
if info.cpu_family == 'x86': |
| 1393 |
comp = ['ml'] |
| 1394 |
comp_class = MasmCompiler |
| 1395 |
arg = '/?' |
| 1396 |
elif info.cpu_family == 'x86_64': |
| 1397 |
comp = ['ml64'] |
| 1398 |
comp_class = MasmCompiler |
| 1399 |
arg = '/?' |
| 1400 |
elif info.cpu_family == 'arm': |
| 1401 |
comp = ['armasm'] |
| 1402 |
comp_class = MasmARMCompiler |
| 1403 |
arg = '-h' |
| 1404 |
elif info.cpu_family == 'aarch64': |
| 1405 |
comp = ['armasm64'] |
| 1406 |
comp_class = MasmARMCompiler |
| 1407 |
arg = '-h' |
| 1408 |
else: |
| 1409 |
raise EnvironmentException(f'Platform {info.cpu_family} not supported by MASM') |
| 1410 |
|
| 1411 |
popen_exceptions: T.Dict[str, Exception] = {} |
| 1412 |
try: |
| 1413 |
output = Popen_safe(comp + [arg])[2] |
| 1414 |
version = search_version(output) |
| 1415 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 1416 |
return comp_class([], comp, version, for_machine, env, cc.linker) |
| 1417 |
except OSError as e: |
| 1418 |
popen_exceptions[' '.join(comp + [arg])] = e |
| 1419 |
_handle_exceptions(popen_exceptions, [comp]) |
| 1420 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1421 |
|
| 1422 |
def detect_linearasm_compiler(env: Environment, for_machine: MachineChoice) -> Compiler: |
| 1423 |
from .asm import TILinearAsmCompiler |
| 1424 |
comp = ['cl6x'] |
| 1425 |
comp_class: T.Type[ASMCompiler] = TILinearAsmCompiler |
| 1426 |
arg = '-h' |
| 1427 |
cc = detect_c_compiler(env, for_machine) |
| 1428 |
if not env.is_cross_build(for_machine): |
| 1429 |
env.update_build_machine({'c': cc}) |
| 1430 |
|
| 1431 |
popen_exceptions: T.Dict[str, Exception] = {} |
| 1432 |
try: |
| 1433 |
output = Popen_safe(comp + [arg])[2] |
| 1434 |
version = search_version(output) |
| 1435 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 1436 |
return comp_class([], comp, version, for_machine, env, cc.linker) |
| 1437 |
except OSError as e: |
| 1438 |
popen_exceptions[' '.join(comp + [arg])] = e |
| 1439 |
_handle_exceptions(popen_exceptions, [comp]) |
| 1440 |
raise EnvironmentException('Unreachable code (exception to make mypy happy)') |
| 1441 |
|
| 1442 |
# GNU/Clang defines and version |
| 1443 |
# ============================= |
| 1444 |
|
| 1445 |
def _get_gnu_compiler_defines(compiler: T.List[str], lang: str) -> T.Dict[str, str]: |
| 1446 |
""" |
| 1447 |
Get the list of GCC pre-processor defines |
| 1448 |
""" |
| 1449 |
from .mixins.gnu import gnu_lang_map |
| 1450 |
|
| 1451 |
def _try_obtain_compiler_defines(args: T.List[str]) -> str: |
| 1452 |
mlog.debug(f'Running command: {join_args(args)}') |
| 1453 |
p, output, error = Popen_safe(compiler + args, write='', stdin=subprocess.PIPE) |
| 1454 |
if p.returncode != 0: |
| 1455 |
raise EnvironmentException('Unable to get gcc pre-processor defines:\n' |
| 1456 |
f'Compiler stdout:\n{output}\n-----\n' |
| 1457 |
f'Compiler stderr:\n{error}\n-----\n') |
| 1458 |
return output |
| 1459 |
|
| 1460 |
# Arguments to output compiler pre-processor defines to stdout |
| 1461 |
# gcc, g++, and gfortran all support these arguments |
| 1462 |
baseline_test_args = ['-E', '-dM', '-'] |
| 1463 |
try: |
| 1464 |
# We assume that when _get_gnu_compiler_defines is called, it's |
| 1465 |
# close enough to a GCCish compiler so we reuse the _LANG_MAP |
| 1466 |
# from the GCC mixin. This isn't a dangerous assumption because |
| 1467 |
# we fallback if the detection fails anyway. |
| 1468 |
|
| 1469 |
# We might not have a match for Fortran, so fallback to detection |
| 1470 |
# based on the driver. |
| 1471 |
lang = gnu_lang_map[lang] |
| 1472 |
|
| 1473 |
# The compiler may not infer the target language based on the driver name |
| 1474 |
# so first, try with '-cpp -x lang', then fallback without given it's less |
| 1475 |
# portable. We try with '-cpp' as GCC needs it for Fortran at least, and |
| 1476 |
# it seems to do no harm. |
| 1477 |
output = _try_obtain_compiler_defines(['-cpp', '-x', lang] + baseline_test_args) |
| 1478 |
except (EnvironmentException, KeyError): |
| 1479 |
mlog.debug(f'pre-processor extraction using -cpp -x {lang} failed, falling back w/o lang') |
| 1480 |
output = _try_obtain_compiler_defines(baseline_test_args) |
| 1481 |
|
| 1482 |
# Parse several lines of the type: |
| 1483 |
# `#define ___SOME_DEF some_value` |
| 1484 |
# and extract `___SOME_DEF` |
| 1485 |
defines: T.Dict[str, str] = {} |
| 1486 |
for line in output.split('\n'): |
| 1487 |
if not line: |
| 1488 |
continue |
| 1489 |
d, *rest = line.split(' ', 2) |
| 1490 |
if d != '#define': |
| 1491 |
continue |
| 1492 |
if len(rest) == 1: |
| 1493 |
defines[rest[0]] = '' |
| 1494 |
if len(rest) == 2: |
| 1495 |
defines[rest[0]] = rest[1] |
| 1496 |
return defines |
| 1497 |
|
| 1498 |
def _get_clang_compiler_defines(compiler: T.List[str], lang: str) -> T.Dict[str, str]: |
| 1499 |
""" |
| 1500 |
Get the list of Clang pre-processor defines |
| 1501 |
""" |
| 1502 |
from .mixins.clang import clang_lang_map |
| 1503 |
|
| 1504 |
def _try_obtain_compiler_defines(args: T.List[str]) -> str: |
| 1505 |
mlog.debug(f'Running command: {join_args(args)}') |
| 1506 |
p, output, error = Popen_safe(compiler + args, write='', stdin=subprocess.PIPE) |
| 1507 |
if p.returncode != 0: |
| 1508 |
raise EnvironmentException('Unable to get clang pre-processor defines:\n' |
| 1509 |
f'Compiler stdout:\n{output}\n-----\n' |
| 1510 |
f'Compiler stderr:\n{error}\n-----\n') |
| 1511 |
return output |
| 1512 |
|
| 1513 |
# Arguments to output compiler pre-processor defines to stdout |
| 1514 |
baseline_test_args = ['-E', '-dM', '-'] |
| 1515 |
try: |
| 1516 |
# We assume that when _get_clang_compiler_defines is called, it's |
| 1517 |
# close enough to a Clangish compiler so we reuse the _LANG_MAP |
| 1518 |
# from the Clang mixin. This isn't a dangerous assumption because |
| 1519 |
# we fallback if the detection fails anyway. |
| 1520 |
|
| 1521 |
# We might not have a match for Fortran, so fallback to detection |
| 1522 |
# based on the driver. |
| 1523 |
lang = clang_lang_map[lang] |
| 1524 |
|
| 1525 |
# The compiler may not infer the target language based on the driver name. |
| 1526 |
# Try first with '-x lang' to supported systemwide language level overrides, |
| 1527 |
# then fallback to without since it's a more recent option. |
| 1528 |
output = _try_obtain_compiler_defines(['-x', lang] + baseline_test_args) |
| 1529 |
except (EnvironmentException, KeyError): |
| 1530 |
mlog.debug(f'pre-processor extraction using -x {lang} failed, falling back w/o lang') |
| 1531 |
output = _try_obtain_compiler_defines(baseline_test_args) |
| 1532 |
|
| 1533 |
defines: T.Dict[str, str] = {} |
| 1534 |
for line in output.split('\n'): |
| 1535 |
if not line: |
| 1536 |
continue |
| 1537 |
d, *rest = line.split(' ', 2) |
| 1538 |
if d != '#define': |
| 1539 |
continue |
| 1540 |
if len(rest) == 1: |
| 1541 |
defines[rest[0]] = '' |
| 1542 |
if len(rest) == 2: |
| 1543 |
defines[rest[0]] = rest[1] |
| 1544 |
return defines |
| 1545 |
|
| 1546 |
def _get_gnu_version_from_defines(defines: T.Dict[str, str]) -> str: |
| 1547 |
dot = '.' |
| 1548 |
major = defines.get('__GNUC__', '0') |
| 1549 |
minor = defines.get('__GNUC_MINOR__', '0') |
| 1550 |
patch = defines.get('__GNUC_PATCHLEVEL__', '0') |
| 1551 |
return dot.join((major, minor, patch)) |
| 1552 |
|
| 1553 |
def _get_lcc_version_from_defines(defines: T.Dict[str, str]) -> str: |
| 1554 |
dot = '.' |
| 1555 |
generation_and_major = defines.get('__LCC__', '100') |
| 1556 |
generation = generation_and_major[:1] |
| 1557 |
major = generation_and_major[1:] |
| 1558 |
minor = defines.get('__LCC_MINOR__', '0') |
| 1559 |
return dot.join((generation, major, minor)) |