| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2022 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from .base import RSPFileSyntax |
| 7 |
from .. import mlog |
| 8 |
from ..mesonlib import ( |
| 9 |
EnvironmentException, |
| 10 |
Popen_safe, Popen_safe_logged, join_args, search_version |
| 11 |
) |
| 12 |
|
| 13 |
import re |
| 14 |
import shlex |
| 15 |
import typing as T |
| 16 |
|
| 17 |
if T.TYPE_CHECKING: |
| 18 |
from .linkers import DynamicLinker, GnuDynamicLinker |
| 19 |
from ..environment import Environment |
| 20 |
from ..compilers import Compiler |
| 21 |
from ..mesonlib import MachineChoice |
| 22 |
|
| 23 |
defaults: T.Dict[str, T.List[str]] = {} |
| 24 |
defaults['static_linker'] = ['ar', 'gar'] |
| 25 |
defaults['vs_static_linker'] = ['lib'] |
| 26 |
defaults['clang_cl_static_linker'] = ['llvm-lib'] |
| 27 |
defaults['cuda_static_linker'] = ['nvlink'] |
| 28 |
defaults['gcc_static_linker'] = ['gcc-ar'] |
| 29 |
defaults['clang_static_linker'] = ['llvm-ar'] |
| 30 |
defaults['emxomf_static_linker'] = ['emxomfar'] |
| 31 |
|
| 32 |
def __failed_to_detect_linker(compiler: T.List[str], args: T.List[str], stdout: str, stderr: str) -> 'T.NoReturn': |
| 33 |
msg = 'Unable to detect linker for compiler `{}`\nstdout: {}\nstderr: {}'.format( |
| 34 |
join_args(compiler + args), stdout, stderr) |
| 35 |
raise EnvironmentException(msg) |
| 36 |
|
| 37 |
|
| 38 |
def guess_win_linker(env: 'Environment', compiler: T.List[str], comp_class: T.Type['Compiler'], |
| 39 |
comp_version: str, for_machine: MachineChoice, *, |
| 40 |
wrap_linker_args: bool = True, invoked_directly: bool = True, |
| 41 |
extra_args: T.Optional[T.List[str]] = None) -> 'DynamicLinker': |
| 42 |
from . import linkers |
| 43 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 44 |
|
| 45 |
if invoked_directly or comp_class.get_argument_syntax() == 'msvc': |
| 46 |
rsp_syntax = RSPFileSyntax.MSVC |
| 47 |
else: |
| 48 |
rsp_syntax = RSPFileSyntax.GCC |
| 49 |
|
| 50 |
# Explicitly pass logo here so that we can get the version of link.exe |
| 51 |
if not wrap_linker_args or comp_class.LINKER_OPTION_STYLE is None: |
| 52 |
check_args = ['/logo', '--version'] |
| 53 |
else: |
| 54 |
check_args = comp_class.LINKER_OPTION_STYLE.wrap(['/logo', '--version']) |
| 55 |
|
| 56 |
check_args += env.coredata.get_external_link_args(for_machine, comp_class.language) |
| 57 |
|
| 58 |
override: T.List[str] = [] |
| 59 |
value = env.lookup_binary_entry(for_machine, comp_class.language + '_ld') |
| 60 |
if value is not None: |
| 61 |
override = comp_class.use_linker_args(value[0], comp_version) |
| 62 |
check_args += override |
| 63 |
elif 'lld-link' in compiler: |
| 64 |
override = comp_class.use_linker_args('lld-link', comp_version) |
| 65 |
check_args += override |
| 66 |
|
| 67 |
if extra_args is not None: |
| 68 |
check_args.extend(extra_args) |
| 69 |
|
| 70 |
if value is not None and invoked_directly: |
| 71 |
compiler = value |
| 72 |
|
| 73 |
p, o, e = Popen_safe(compiler + check_args) |
| 74 |
if 'LLD' in o.split('\n', maxsplit=1)[0]: |
| 75 |
if 'compatible with GNU linkers' in o: |
| 76 |
return linkers.LLVMDynamicLinker( |
| 77 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, |
| 78 |
override, version=search_version(o)) |
| 79 |
if not invoked_directly: |
| 80 |
return linkers.ClangClDynamicLinker( |
| 81 |
env, for_machine, override, exelist=compiler, prefix=comp_class.LINKER_OPTION_STYLE, |
| 82 |
version=search_version(o), direct=False, machine=None, |
| 83 |
rsp_syntax=rsp_syntax) |
| 84 |
return linkers.ClangClDynamicLinker( |
| 85 |
env, for_machine, [], |
| 86 |
prefix=comp_class.LINKER_OPTION_STYLE if wrap_linker_args else None, |
| 87 |
exelist=compiler, version=search_version(o), direct=invoked_directly, |
| 88 |
rsp_syntax=rsp_syntax) |
| 89 |
elif 'OPTLINK' in o: |
| 90 |
# Optlink's stdout *may* begin with a \r character. |
| 91 |
return linkers.OptlinkDynamicLinker(compiler, env, for_machine, version=search_version(o)) |
| 92 |
elif o.startswith('Microsoft') or e.startswith('Microsoft'): |
| 93 |
out = o or e |
| 94 |
match = re.search(r'.*(X86|X64|ARM|ARM64).*', out) |
| 95 |
if match: |
| 96 |
target = str(match.group(1)) |
| 97 |
else: |
| 98 |
target = 'x86' |
| 99 |
|
| 100 |
return linkers.MSVCDynamicLinker( |
| 101 |
env, for_machine, [], machine=target, exelist=compiler, |
| 102 |
prefix=comp_class.LINKER_OPTION_STYLE if wrap_linker_args else None, |
| 103 |
version=search_version(out), direct=invoked_directly, |
| 104 |
rsp_syntax=rsp_syntax) |
| 105 |
elif 'GNU coreutils' in o: |
| 106 |
import shutil |
| 107 |
fullpath = shutil.which(compiler[0]) |
| 108 |
raise EnvironmentException( |
| 109 |
f"Found GNU link.exe instead of MSVC link.exe in {fullpath}.\n" |
| 110 |
"This link.exe is not a linker.\n" |
| 111 |
"You may need to reorder entries to your %PATH% variable to resolve this.") |
| 112 |
__failed_to_detect_linker(compiler, check_args, o, e) |
| 113 |
|
| 114 |
def guess_nix_linker(env: 'Environment', compiler: T.List[str], comp_class: T.Type['Compiler'], |
| 115 |
comp_version: str, for_machine: MachineChoice, *, |
| 116 |
extra_args: T.Optional[T.List[str]] = None) -> 'DynamicLinker': |
| 117 |
"""Helper for guessing what linker to use on Unix-Like OSes. |
| 118 |
|
| 119 |
:compiler: Invocation to use to get linker |
| 120 |
:comp_class: The Compiler Type (uninstantiated) |
| 121 |
:comp_version: The compiler version string |
| 122 |
:for_machine: which machine this linker targets |
| 123 |
:extra_args: Any additional arguments required (such as a source file) |
| 124 |
""" |
| 125 |
from . import linkers |
| 126 |
from ..options import OptionKey |
| 127 |
env.add_lang_args(comp_class.language, comp_class, for_machine) |
| 128 |
extra_args = extra_args or [] |
| 129 |
|
| 130 |
system = env.machines[for_machine].system |
| 131 |
ldflags = env.coredata.get_external_link_args(for_machine, comp_class.language) |
| 132 |
extra_args += comp_class._unix_args_to_native(ldflags, env.machines[for_machine]) |
| 133 |
check_args = comp_class.LINKER_OPTION_STYLE.wrap(['--version']) + extra_args |
| 134 |
|
| 135 |
override: T.List[str] = [] |
| 136 |
value = env.lookup_binary_entry(for_machine, comp_class.language + '_ld') |
| 137 |
if value is not None: |
| 138 |
override = comp_class.use_linker_args(value[0], comp_version) |
| 139 |
check_args += override |
| 140 |
|
| 141 |
if env.machines[for_machine].is_os2() and env.coredata.optstore.get_value_for(OptionKey('os2_emxomf')): |
| 142 |
check_args += ['-Zomf'] |
| 143 |
|
| 144 |
mlog.debug('-----') |
| 145 |
p, o, e = Popen_safe_logged(compiler + check_args, msg='Detecting linker via') |
| 146 |
|
| 147 |
v = search_version(o + e) |
| 148 |
linker: DynamicLinker |
| 149 |
if 'LLD' in o.split('\n', maxsplit=1)[0] or 'tiarmlnk' in e: |
| 150 |
cmd = compiler + override + comp_class.LINKER_OPTION_STYLE.wrap(['-v']) + extra_args |
| 151 |
_, newo, newerr = Popen_safe_logged(cmd, msg='Detecting LLD linker via') |
| 152 |
|
| 153 |
lld_cls: T.Type[DynamicLinker] |
| 154 |
if 'ld64.lld' in newerr: |
| 155 |
lld_cls = linkers.LLVMLD64DynamicLinker |
| 156 |
else: |
| 157 |
lld_cls = linkers.LLVMDynamicLinker |
| 158 |
|
| 159 |
linker = lld_cls( |
| 160 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, system=system, version=v) |
| 161 |
elif o.startswith("eld"): |
| 162 |
linker = linkers.ELDDynamicLinker( |
| 163 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, version=v) |
| 164 |
elif 'Snapdragon' in e and 'LLVM' in e: |
| 165 |
linker = linkers.QualcommLLVMDynamicLinker( |
| 166 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, version=v) |
| 167 |
elif e.startswith('lld-link: '): |
| 168 |
# The LLD MinGW frontend didn't respond to --version before version 9.0.0, |
| 169 |
# and produced an error message about failing to link (when no object |
| 170 |
# files were specified), instead of printing the version number. |
| 171 |
# Let's try to extract the linker invocation command to grab the version. |
| 172 |
|
| 173 |
_, o, e = Popen_safe(compiler + check_args + ['-v']) |
| 174 |
|
| 175 |
try: |
| 176 |
linker_cmd = re.match(r'.*\n(.*?)\nlld-link: ', e, re.DOTALL).group(1) |
| 177 |
linker_cmd = shlex.split(linker_cmd)[0] |
| 178 |
except (AttributeError, IndexError, ValueError): |
| 179 |
pass |
| 180 |
else: |
| 181 |
_, o, e = Popen_safe([linker_cmd, '--version']) |
| 182 |
v = search_version(o) |
| 183 |
|
| 184 |
linker = linkers.LLVMDynamicLinker(compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, version=v) |
| 185 |
elif 'GNU' in o or 'GNU' in e: |
| 186 |
gnu_cls: T.Type[GnuDynamicLinker] |
| 187 |
# this is always the only thing on stdout, except for swift |
| 188 |
# which may or may not redirect the linker stdout to stderr |
| 189 |
if o.startswith('GNU gold') or e.startswith('GNU gold'): |
| 190 |
gnu_cls = linkers.GnuGoldDynamicLinker |
| 191 |
elif o.startswith('mold') or e.startswith('mold'): |
| 192 |
gnu_cls = linkers.MoldDynamicLinker |
| 193 |
else: |
| 194 |
gnu_cls = linkers.GnuBFDDynamicLinker |
| 195 |
linker = gnu_cls(compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, version=v) |
| 196 |
elif 'Solaris' in e or 'Solaris' in o: |
| 197 |
for line in (o+e).split('\n'): |
| 198 |
if 'ld: Software Generation Utilities' in line: |
| 199 |
v = line.split(':')[2].lstrip() |
| 200 |
break |
| 201 |
else: |
| 202 |
v = 'unknown version' |
| 203 |
linker = linkers.SolarisDynamicLinker( |
| 204 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, |
| 205 |
version=v) |
| 206 |
elif 'ld: 0706-012 The -- flag is not recognized' in e: |
| 207 |
_, _, e = Popen_safe(compiler + comp_class.LINKER_OPTION_STYLE.wrap(['-V']) + extra_args) |
| 208 |
linker = linkers.AIXDynamicLinker( |
| 209 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, |
| 210 |
version=search_version(e)) |
| 211 |
elif o.startswith('zig ld'): |
| 212 |
linker = linkers.ZigCCDynamicLinker( |
| 213 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, version=v) |
| 214 |
# detect xtools first, bug #10805 |
| 215 |
elif 'xtools-' in o.split('\n', maxsplit=1)[0]: |
| 216 |
# See https://github.com/iains/darwin-xtools/blob/darwin-xtools-3-3-0/ld64/src/ld/ld_vers.c.in |
| 217 |
# for the format |
| 218 |
for line in o.split('\n'): |
| 219 |
if line.startswith('Based on Apple Inc. ld64-'): |
| 220 |
v = line.split()[4][5:] |
| 221 |
break |
| 222 |
else: |
| 223 |
__failed_to_detect_linker(compiler, check_args, o, e) |
| 224 |
linker = linkers.AppleDynamicLinker( |
| 225 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, |
| 226 |
system=system, version=v |
| 227 |
) |
| 228 |
# detect linker on MacOS - must be after other platforms because the |
| 229 |
# "(use -v to see invocation)" will match clang on other platforms, |
| 230 |
# but the rest of the checks will fail and call __failed_to_detect_linker. |
| 231 |
# First might be apple clang, second is for real gcc, the third is icc. |
| 232 |
# Note that "ld: unknown option: " sometimes instead is "ld: unknown options:". |
| 233 |
elif e.endswith('(use -v to see invocation)\n') or 'macosx_version' in e or 'ld: unknown option' in e: |
| 234 |
cmd = compiler + comp_class.LINKER_OPTION_STYLE.wrap(['-v']) + extra_args |
| 235 |
_, newo, newerr = Popen_safe_logged(cmd, msg='Detecting Apple linker via') |
| 236 |
|
| 237 |
for line in newerr.split('\n'): |
| 238 |
if 'PROJECT:ld' in line or 'PROJECT:dyld' in line: |
| 239 |
v = line.split('-')[1] |
| 240 |
break |
| 241 |
else: |
| 242 |
__failed_to_detect_linker(compiler, check_args, o, e) |
| 243 |
linker = linkers.AppleDynamicLinker( |
| 244 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, |
| 245 |
system=system, version=v |
| 246 |
) |
| 247 |
elif 'ld.exe: unrecognized option' in e or 'ld: unrecognized option' in e: |
| 248 |
linker = linkers.OS2AoutDynamicLinker( |
| 249 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, |
| 250 |
version='none') |
| 251 |
elif 'emxomfld: invalid option' in e: |
| 252 |
linker = linkers.OS2OmfDynamicLinker( |
| 253 |
compiler, env, for_machine, comp_class.LINKER_OPTION_STYLE, override, |
| 254 |
version='none') |
| 255 |
else: |
| 256 |
__failed_to_detect_linker(compiler, check_args, o, e) |
| 257 |
return linker |