| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2012-2022 The Meson development team |
| 3 |
# Copyright © 2023-2025 Intel Corporation |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import argparse |
| 8 |
import functools |
| 9 |
import os.path |
| 10 |
import textwrap |
| 11 |
import re |
| 12 |
import typing as T |
| 13 |
|
| 14 |
from .. import options |
| 15 |
from ..dependencies import InternalDependency |
| 16 |
from ..mesonlib import EnvironmentException, MesonException, Popen_safe, Popen_safe_logged, version_compare |
| 17 |
from ..linkers.linkers import VisualStudioLikeLinkerMixin |
| 18 |
from ..options import OptionKey |
| 19 |
from .compilers import Compiler, CompileCheckMode, clike_debug_args, is_library |
| 20 |
|
| 21 |
if T.TYPE_CHECKING: |
| 22 |
from .. import build |
| 23 |
from ..options import MutableKeyedOptionDictType |
| 24 |
from ..environment import Environment # noqa: F401 |
| 25 |
from ..linkers.linkers import DynamicLinker |
| 26 |
from ..mesonlib import MachineChoice |
| 27 |
from ..dependencies import Dependency |
| 28 |
from ..build import BuildTarget |
| 29 |
|
| 30 |
from typing_extensions import Protocol |
| 31 |
|
| 32 |
class TargetParse(Protocol): |
| 33 |
target: T.Optional[str] |
| 34 |
|
| 35 |
|
| 36 |
rust_optimization_args: T.Dict[str, T.List[str]] = { |
| 37 |
'plain': [], |
| 38 |
'0': [], |
| 39 |
'g': ['-C', 'opt-level=0'], |
| 40 |
'1': ['-C', 'opt-level=1'], |
| 41 |
'2': ['-C', 'opt-level=2'], |
| 42 |
'3': ['-C', 'opt-level=3'], |
| 43 |
's': ['-C', 'opt-level=s'], |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
class _TargetParser: |
| 48 |
|
| 49 |
"""Helper for bindgen to look for --target in various command line arguments. |
| 50 |
|
| 51 |
Storing this as a helper class avoids the need to set up the ArgumentParser |
| 52 |
multiple times, and simplifies it's use as well as the typing. |
| 53 |
""" |
| 54 |
|
| 55 |
def __init__(self) -> None: |
| 56 |
parser = argparse.ArgumentParser(add_help=False, allow_abbrev=False) |
| 57 |
parser.add_argument('--target', action='store', default=None) |
| 58 |
self._parser = parser |
| 59 |
|
| 60 |
def parse(self, args: T.List[str]) -> T.Optional[str]: |
| 61 |
"""Parse arguments looking for --target |
| 62 |
|
| 63 |
:param args: A list of arguments to search |
| 64 |
:return: the argument to --target if it exists, otherwise None |
| 65 |
""" |
| 66 |
parsed = T.cast('TargetParse', self._parser.parse_known_args(args)[0]) |
| 67 |
return parsed.target |
| 68 |
|
| 69 |
|
| 70 |
parse_target = _TargetParser().parse |
| 71 |
|
| 72 |
def get_rustup_run_and_args(exelist: T.List[str]) -> T.Optional[T.Tuple[T.List[str], T.List[str]]]: |
| 73 |
"""Given the command for a rustc executable, check if it is invoked via |
| 74 |
"rustup run" and if so separate the "rustup [OPTIONS] run TOOLCHAIN" |
| 75 |
part from the arguments to rustc. If the returned value is not None, |
| 76 |
other tools (for example clippy-driver or rustdoc) can be run by placing |
| 77 |
the name of the tool between the two elements of the tuple.""" |
| 78 |
e = iter(exelist) |
| 79 |
try: |
| 80 |
if os.path.basename(next(e)) != 'rustup': |
| 81 |
return None |
| 82 |
# minimum three strings: "rustup run TOOLCHAIN" |
| 83 |
n = 3 |
| 84 |
opt = next(e) |
| 85 |
|
| 86 |
# options come first |
| 87 |
while opt.startswith('-'): |
| 88 |
n += 1 |
| 89 |
opt = next(e) |
| 90 |
|
| 91 |
# then "run TOOLCHAIN" |
| 92 |
if opt != 'run': |
| 93 |
return None |
| 94 |
|
| 95 |
next(e) |
| 96 |
next(e) |
| 97 |
return exelist[:n], list(e) |
| 98 |
except StopIteration: |
| 99 |
return None |
| 100 |
|
| 101 |
def rustc_link_args(args: T.List[str]) -> T.List[str]: |
| 102 |
if not args: |
| 103 |
return args |
| 104 |
rustc_args: T.List[str] = [] |
| 105 |
for arg in args: |
| 106 |
rustc_args.append('-C') |
| 107 |
rustc_args.append(f'link-arg={arg}') |
| 108 |
return rustc_args |
| 109 |
|
| 110 |
|
| 111 |
class RustSystemDependency(InternalDependency): |
| 112 |
pass |
| 113 |
|
| 114 |
|
| 115 |
class RustCompiler(Compiler): |
| 116 |
|
| 117 |
# rustc doesn't invoke the compiler itself, it doesn't need a LINKER_OPTION_STYLE |
| 118 |
language = 'rust' |
| 119 |
id = 'rustc' |
| 120 |
|
| 121 |
USED_FOR_SEPARATE_LINKING_STEP = False |
| 122 |
|
| 123 |
_WARNING_LEVELS: T.Dict[str, T.List[str]] = { |
| 124 |
'0': ['--cap-lints', 'allow'], |
| 125 |
'1': [], |
| 126 |
'2': [], |
| 127 |
'3': ['-W', 'warnings'], |
| 128 |
'everything': ['-W', 'warnings'], |
| 129 |
} |
| 130 |
|
| 131 |
allow_nightly: bool |
| 132 |
|
| 133 |
# libcore can be compiled with either static or dynamic CRT, so disable |
| 134 |
# both of them just in case. |
| 135 |
MSVCRT_ARGS: T.Mapping[str, T.List[str]] = { |
| 136 |
'none': [], |
| 137 |
'md': ['-Clink-arg=/nodefaultlib:libcmt', '-Clink-arg=/defaultlib:msvcrt'], |
| 138 |
'mdd': ['-Clink-arg=/nodefaultlib:libcmt', '-Clink-arg=/nodefaultlib:msvcrt', '-Clink-arg=/defaultlib:msvcrtd'], |
| 139 |
'mt': ['-Clink-arg=/defaultlib:libcmt', '-Clink-arg=/nodefaultlib:msvcrt'], |
| 140 |
'mtd': ['-Clink-arg=/nodefaultlib:libcmt', '-Clink-arg=/nodefaultlib:msvcrt', '-Clink-arg=/defaultlib:libcmtd'], |
| 141 |
} |
| 142 |
|
| 143 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 144 |
env: Environment, full_version: T.Optional[str] = None, |
| 145 |
linker: T.Optional['DynamicLinker'] = None): |
| 146 |
super().__init__([], exelist, version, for_machine, env, |
| 147 |
full_version=full_version, linker=linker) |
| 148 |
self.rustup_run_and_args: T.Optional[T.Tuple[T.List[str], T.List[str]]] = get_rustup_run_and_args(exelist) |
| 149 |
self.base_options.update({OptionKey(o) for o in ['b_colorout', 'b_coverage', 'b_ndebug', 'b_pgo']}) |
| 150 |
if isinstance(self.linker, VisualStudioLikeLinkerMixin): |
| 151 |
self.base_options.add(OptionKey('b_vscrt')) |
| 152 |
self.native_static_libs: T.List[str] = [] |
| 153 |
self.is_beta = '-beta' in full_version |
| 154 |
self.is_nightly = '-nightly' in full_version |
| 155 |
self.has_check_cfg = version_compare(version, '>=1.80.0') |
| 156 |
|
| 157 |
def init_from_options(self) -> None: |
| 158 |
nightly_opt = self.get_compileropt_value('nightly', None) |
| 159 |
if nightly_opt == 'enabled' and not self.is_nightly: |
| 160 |
raise EnvironmentException(f'Rust compiler {self.name_string()} is not a nightly compiler as required by the "nightly" option.') |
| 161 |
self.allow_nightly = nightly_opt != 'disabled' and self.is_nightly |
| 162 |
|
| 163 |
def needs_static_linker(self) -> bool: |
| 164 |
return False |
| 165 |
|
| 166 |
def _sanity_check_compile_args(self, sourcename: str, binname: str |
| 167 |
) -> T.Tuple[T.List[str], T.List[str]]: |
| 168 |
cmdlist = self.exelist.copy() |
| 169 |
largs: T.List[str] = [] |
| 170 |
if self.info.kernel == 'none' and 'ld.' in self.get_linker_id(): |
| 171 |
largs.extend(rustc_link_args(['-nostartfiles'])) |
| 172 |
cmdlist.extend(self.get_output_args(binname)) |
| 173 |
cmdlist.append(sourcename) |
| 174 |
return cmdlist, largs |
| 175 |
|
| 176 |
def _sanity_check_source_code(self) -> str: |
| 177 |
if self.info.kernel != 'none': |
| 178 |
return textwrap.dedent( |
| 179 |
'''fn main() { |
| 180 |
} |
| 181 |
''') |
| 182 |
return textwrap.dedent( |
| 183 |
'''#![no_std] |
| 184 |
#![no_main] |
| 185 |
#[no_mangle] |
| 186 |
pub fn _start() { |
| 187 |
} |
| 188 |
#[panic_handler] |
| 189 |
fn panic(_info: &core::panic::PanicInfo) -> ! { |
| 190 |
loop {} |
| 191 |
} |
| 192 |
''') |
| 193 |
|
| 194 |
def sanity_check(self, work_dir: str) -> None: |
| 195 |
super().sanity_check(work_dir) |
| 196 |
source_name = self._sanity_check_filenames()[0] |
| 197 |
self._native_static_libs(work_dir, source_name) |
| 198 |
|
| 199 |
def _native_static_libs(self, work_dir: str, source_name: str) -> None: |
| 200 |
# Get libraries needed to link with a Rust staticlib |
| 201 |
if self.native_static_libs: |
| 202 |
return |
| 203 |
|
| 204 |
cmdlist = self.exelist + ['--crate-type', 'staticlib', '--print', 'native-static-libs', source_name] |
| 205 |
p, stdo, stde = Popen_safe_logged(cmdlist, cwd=work_dir) |
| 206 |
if p.returncode != 0: |
| 207 |
raise EnvironmentException('Rust compiler cannot compile staticlib.') |
| 208 |
match = re.search('native-static-libs: (.*)$', stde, re.MULTILINE) |
| 209 |
if not match: |
| 210 |
if self.info.kernel == 'none': |
| 211 |
# no match and kernel == none (i.e. baremetal) is a valid use case. |
| 212 |
# return and let native_static_libs list empty |
| 213 |
return |
| 214 |
if self.info.system == 'emscripten': |
| 215 |
# no match and emscripten is valid after rustc 1.84 |
| 216 |
return |
| 217 |
raise EnvironmentException('Failed to find native-static-libs in Rust compiler output.') |
| 218 |
# Exclude some well known libraries that we don't need because they |
| 219 |
# are always part of C/C++ linkers. Rustc probably should not print |
| 220 |
# them, pkg-config for example never specify them. |
| 221 |
# FIXME: https://github.com/rust-lang/rust/issues/55120 |
| 222 |
exclude = {'-lc', '-lgcc_s', '-lkernel32', '-ladvapi32', '/defaultlib:msvcrt'} |
| 223 |
self.native_static_libs = [i for i in match.group(1).split() if i not in exclude] |
| 224 |
|
| 225 |
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: |
| 226 |
return ['--emit', f'dep-info={outfile}'] |
| 227 |
|
| 228 |
def get_output_args(self, outputname: str) -> T.List[str]: |
| 229 |
return ['--emit', f'link={outputname}'] |
| 230 |
|
| 231 |
@functools.lru_cache(maxsize=None) |
| 232 |
def get_sysroot(self) -> str: |
| 233 |
cmd = self.get_exelist(ccache=False) + ['--print', 'sysroot'] |
| 234 |
p, stdo, stde = Popen_safe_logged(cmd) |
| 235 |
return stdo.split('\n', maxsplit=1)[0] |
| 236 |
|
| 237 |
@functools.lru_cache(maxsize=None) |
| 238 |
def get_target_libdir(self) -> str: |
| 239 |
cmd = self.get_exelist(ccache=False) + ['--print', 'target-libdir'] |
| 240 |
p, stdo, stde = Popen_safe_logged(cmd) |
| 241 |
return stdo.split('\n', maxsplit=1)[0] |
| 242 |
|
| 243 |
@functools.lru_cache(maxsize=None) |
| 244 |
def get_cfgs(self) -> T.List[str]: |
| 245 |
cmd = self.get_exelist(ccache=False) + ['--print', 'cfg'] |
| 246 |
p, stdo, stde = Popen_safe_logged(cmd) |
| 247 |
return stdo.splitlines() |
| 248 |
|
| 249 |
@functools.lru_cache(maxsize=None) |
| 250 |
def get_target_triple(self) -> str: |
| 251 |
# First check if --target is explicitly set in the compiler command |
| 252 |
target = parse_target(self.get_exe_args()) |
| 253 |
if target: |
| 254 |
return target |
| 255 |
# Fall back to parsing the host triple from `rustc -vV` |
| 256 |
cmd = self.get_exelist(ccache=False) + ['-vV'] |
| 257 |
p, stdo, stde = Popen_safe(cmd) |
| 258 |
for line in stdo.splitlines(): |
| 259 |
if line.startswith('host:'): |
| 260 |
return line.split(':', 1)[1].strip() |
| 261 |
raise EnvironmentException('Could not determine Rust target triple') |
| 262 |
|
| 263 |
@functools.lru_cache(maxsize=None) |
| 264 |
def get_crt_static(self) -> bool: |
| 265 |
return 'target_feature="crt-static"' in self.get_cfgs() |
| 266 |
|
| 267 |
def get_nightly(self, target: T.Optional[BuildTarget]) -> bool: |
| 268 |
if not target: |
| 269 |
return self.allow_nightly |
| 270 |
key = self.form_compileropt_key('nightly') |
| 271 |
nightly_opt = self.environment.coredata.get_option_for_target(target, key) |
| 272 |
if nightly_opt == 'enabled' and not self.is_nightly: |
| 273 |
raise EnvironmentException(f'Rust compiler {self.name_string()} is not a nightly compiler as required by the "nightly" option.') |
| 274 |
return nightly_opt != 'disabled' and self.is_nightly |
| 275 |
|
| 276 |
def sanitizer_link_args(self, target: T.Optional[BuildTarget], value: T.List[str]) -> T.List[str]: |
| 277 |
# Sanitizers are not supported yet for Rust code. Nightly supports that |
| 278 |
# with -Zsanitizer=, but procedural macros cannot use them. But even if |
| 279 |
# Rust code cannot be instrumented, we can link in the sanitizer libraries |
| 280 |
# for the sake of C/C++ code |
| 281 |
return rustc_link_args(super().sanitizer_link_args(target, value)) |
| 282 |
|
| 283 |
def get_soname_args(self, prefix: str, shlib_name: str, suffix: str, soversion: str, |
| 284 |
darwin_versions: T.Tuple[str, str]) -> T.List[str]: |
| 285 |
return rustc_link_args(super().get_soname_args(prefix, shlib_name, suffix, soversion, darwin_versions)) |
| 286 |
|
| 287 |
@functools.lru_cache(maxsize=None) |
| 288 |
def has_verbatim(self) -> bool: |
| 289 |
if version_compare(self.version, '< 1.67.0'): |
| 290 |
return False |
| 291 |
# GNU ld support '-l:PATH' |
| 292 |
if 'ld.' in self.linker.id and self.linker.id != 'ld.wasm': |
| 293 |
return True |
| 294 |
# -l:+verbatim does not work (yet?) with MSVC link or Apple ld64 |
| 295 |
# (https://github.com/rust-lang/rust/pull/138753). For ld64, it |
| 296 |
# works together with -l:+whole_archive because -force_load (the macOS |
| 297 |
# equivalent of --whole-archive), receives the full path to the library |
| 298 |
# being linked. However, Meson uses "bundle", not "whole_archive". |
| 299 |
return False |
| 300 |
|
| 301 |
def lib_file_to_l_arg(self, libname: str) -> T.Optional[str]: |
| 302 |
"""Undo the effects of -l on the filename, returning the |
| 303 |
argument that can be passed to -l, or None if the |
| 304 |
library name is not supported.""" |
| 305 |
if not is_library(libname): |
| 306 |
return None |
| 307 |
libname, ext = os.path.splitext(libname) |
| 308 |
|
| 309 |
# On Windows, rustc's -lfoo searches either foo.lib or libfoo.a. |
| 310 |
# Elsewhere, it searches both static and shared libraries and always with |
| 311 |
# the "lib" prefix; for simplicity just skip .lib on non-Windows. |
| 312 |
if self.info.is_windows(): |
| 313 |
if ext == '.lib': |
| 314 |
return libname |
| 315 |
if ext != '.a': |
| 316 |
return None |
| 317 |
else: |
| 318 |
if ext == '.lib': |
| 319 |
return None |
| 320 |
|
| 321 |
if not libname.startswith('lib'): |
| 322 |
return None |
| 323 |
libname = libname[3:] |
| 324 |
return libname |
| 325 |
|
| 326 |
def get_debug_args(self, is_debug: bool) -> T.List[str]: |
| 327 |
return clike_debug_args[is_debug] |
| 328 |
|
| 329 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 330 |
return rust_optimization_args[optimization_level] |
| 331 |
|
| 332 |
def build_rpath_args(self, build_dir: str, from_dir: str, target: BuildTarget, |
| 333 |
extra_paths: T.Optional[T.List[str]] = None |
| 334 |
) -> T.Tuple[T.List[str], T.Set[bytes]]: |
| 335 |
# add rustc's sysroot to account for rustup installations |
| 336 |
args, to_remove = super().build_rpath_args( |
| 337 |
build_dir, from_dir, target, [self.get_target_libdir()]) |
| 338 |
return rustc_link_args(args), to_remove |
| 339 |
|
| 340 |
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], |
| 341 |
build_dir: str) -> T.List[str]: |
| 342 |
for idx, i in enumerate(parameter_list): |
| 343 |
if i[:2] == '-L': |
| 344 |
for j in ['dependency', 'crate', 'native', 'framework', 'all']: |
| 345 |
combined_len = len(j) + 3 |
| 346 |
if i[:combined_len] == f'-L{j}=': |
| 347 |
parameter_list[idx] = i[:combined_len] + os.path.normpath(os.path.join(build_dir, i[combined_len:])) |
| 348 |
break |
| 349 |
|
| 350 |
return parameter_list |
| 351 |
|
| 352 |
@classmethod |
| 353 |
def use_linker_args(cls, linker: str, version: str) -> T.List[str]: |
| 354 |
return ['-C', f'linker={linker}'] |
| 355 |
|
| 356 |
def get_options(self) -> MutableKeyedOptionDictType: |
| 357 |
opts = super().get_options() |
| 358 |
|
| 359 |
key = self.form_compileropt_key('std') |
| 360 |
opts[key] = options.UserComboOption( |
| 361 |
self.make_option_name(key), |
| 362 |
'Rust edition to use', |
| 363 |
'none', |
| 364 |
choices=['none', '2015', '2018', '2021', '2024']) |
| 365 |
|
| 366 |
key = self.form_compileropt_key('dynamic_std') |
| 367 |
opts[key] = options.UserBooleanOption( |
| 368 |
self.make_option_name(key), |
| 369 |
'Whether to link Rust build targets to a dynamic libstd', |
| 370 |
False) |
| 371 |
|
| 372 |
key = self.form_compileropt_key('nightly') |
| 373 |
opts[key] = options.UserFeatureOption( |
| 374 |
self.make_option_name(key), |
| 375 |
"Nightly Rust compiler (enabled=required, disabled=don't use nightly feature, auto=use nightly feature if available)", |
| 376 |
'auto') |
| 377 |
|
| 378 |
return opts |
| 379 |
|
| 380 |
def get_dependency_compile_args(self, dep: 'Dependency') -> T.List[str]: |
| 381 |
if isinstance(dep, RustSystemDependency): |
| 382 |
return dep.get_compile_args() |
| 383 |
# Rust doesn't have dependency compile arguments so simply return |
| 384 |
# nothing here. Dependencies are linked and all required metadata is |
| 385 |
# provided by the linker flags. |
| 386 |
return [] |
| 387 |
|
| 388 |
def get_option_std_args(self, target: BuildTarget, subproject: T.Optional[str] = None) -> T.List[str]: |
| 389 |
args = [] |
| 390 |
std = self.get_compileropt_value('std', target, subproject) |
| 391 |
assert isinstance(std, str) |
| 392 |
if std != 'none': |
| 393 |
args.append('--edition=' + std) |
| 394 |
return args |
| 395 |
|
| 396 |
def get_crt_compile_args(self, crt_val: str) -> T.List[str]: |
| 397 |
# Rust handles this for us, we don't need to do anything |
| 398 |
return [] |
| 399 |
|
| 400 |
def get_crt_link_args(self, crt_val: str) -> T.List[str]: |
| 401 |
if not isinstance(self.linker, VisualStudioLikeLinkerMixin): |
| 402 |
return [] |
| 403 |
# Rustc always use non-debug Windows runtime. Inject the one selected |
| 404 |
# by Meson options instead. |
| 405 |
# https://github.com/rust-lang/rust/issues/39016 |
| 406 |
return self.MSVCRT_ARGS[self.get_crt_val(crt_val)] |
| 407 |
|
| 408 |
def get_colorout_args(self, colortype: str) -> T.List[str]: |
| 409 |
if colortype in {'always', 'never', 'auto'}: |
| 410 |
return [f'--color={colortype}'] |
| 411 |
raise MesonException(f'Invalid color type for rust {colortype}') |
| 412 |
|
| 413 |
@functools.lru_cache(maxsize=None) |
| 414 |
def get_linker_always_args(self) -> T.List[str]: |
| 415 |
return rustc_link_args(super().get_linker_always_args()) + ['-Cdefault-linker-libraries'] |
| 416 |
|
| 417 |
def get_embed_bitcode_args(self, bitcode: bool, lto: bool) -> T.List[str]: |
| 418 |
if bitcode: |
| 419 |
return ['-C', 'embed-bitcode=yes'] |
| 420 |
elif lto: |
| 421 |
return [] |
| 422 |
else: |
| 423 |
return ['-C', 'embed-bitcode=no'] |
| 424 |
|
| 425 |
def get_lto_compile_args(self, *, target: T.Optional[BuildTarget] = None, threads: int = 0, |
| 426 |
mode: str = 'default') -> T.List[str]: |
| 427 |
if target.rust_crate_type in {'dylib', 'proc-macro'}: |
| 428 |
return [] |
| 429 |
|
| 430 |
# TODO: what about -Clinker-plugin-lto? |
| 431 |
rustc_lto = 'lto=thin' if mode == 'thin' else 'lto' |
| 432 |
return ['-C', rustc_lto] |
| 433 |
|
| 434 |
def get_lto_link_args(self, *, target: T.Optional[BuildTarget] = None, threads: int = 0, |
| 435 |
mode: str = 'default', thinlto_cache_dir: T.Optional[str] = None) -> T.List[str]: |
| 436 |
# no need to specify anything because the rustc command line |
| 437 |
# includes the result of get_lto_compile_args() |
| 438 |
return [] |
| 439 |
|
| 440 |
def get_lto_obj_cache_path(self, path: str) -> T.List[str]: |
| 441 |
return rustc_link_args(super().get_lto_obj_cache_path(path)) |
| 442 |
|
| 443 |
def get_coverage_args(self) -> T.List[str]: |
| 444 |
return ['-C', 'instrument-coverage'] |
| 445 |
|
| 446 |
def get_coverage_link_args(self) -> T.List[str]: |
| 447 |
return rustc_link_args(super().get_coverage_link_args()) |
| 448 |
|
| 449 |
def gen_vs_module_defs_args(self, defsfile: str) -> T.List[str]: |
| 450 |
return rustc_link_args(super().gen_vs_module_defs_args(defsfile)) |
| 451 |
|
| 452 |
def gen_export_dynamic_link_args(self) -> T.List[str]: |
| 453 |
return rustc_link_args(self.linker.export_dynamic_args()) |
| 454 |
|
| 455 |
def get_profile_generate_args(self) -> T.List[str]: |
| 456 |
return ['-C', 'profile-generate'] |
| 457 |
|
| 458 |
def get_profile_use_args(self) -> T.List[str]: |
| 459 |
return ['-C', 'profile-use'] |
| 460 |
|
| 461 |
@functools.lru_cache(maxsize=None) |
| 462 |
def get_asneeded_args(self) -> T.List[str]: |
| 463 |
return rustc_link_args(super().get_asneeded_args()) |
| 464 |
|
| 465 |
def bitcode_args(self) -> T.List[str]: |
| 466 |
return ['-C', 'embed-bitcode=yes'] |
| 467 |
|
| 468 |
@functools.lru_cache(maxsize=None) |
| 469 |
def headerpad_args(self) -> T.List[str]: |
| 470 |
return rustc_link_args(super().headerpad_args()) |
| 471 |
|
| 472 |
@functools.lru_cache(maxsize=None) |
| 473 |
def get_linker_fatal_warnings(self) -> T.List[str]: |
| 474 |
return rustc_link_args(super().get_linker_fatal_warnings()) |
| 475 |
|
| 476 |
@functools.lru_cache(maxsize=None) |
| 477 |
def get_allow_undefined_link_args(self) -> T.List[str]: |
| 478 |
return rustc_link_args(super().get_allow_undefined_link_args()) |
| 479 |
|
| 480 |
def get_build_link_args(self, target: BuildTarget, build: build.Build) -> T.List[str]: |
| 481 |
return rustc_link_args(super().get_build_link_args(target, build)) |
| 482 |
|
| 483 |
def get_target_link_args(self, target: 'BuildTarget') -> T.List[str]: |
| 484 |
return rustc_link_args(super().get_target_link_args(target)) |
| 485 |
|
| 486 |
def get_win_subsystem_args(self, value: str) -> T.List[str]: |
| 487 |
return rustc_link_args(super().get_win_subsystem_args(value)) |
| 488 |
|
| 489 |
def get_werror_args(self) -> T.List[str]: |
| 490 |
# Use -D warnings, which makes every warning not explicitly allowed an |
| 491 |
# error |
| 492 |
return ['-D', 'warnings'] |
| 493 |
|
| 494 |
def get_warn_args(self, level: str) -> T.List[str]: |
| 495 |
# TODO: I'm not really sure what to put here, Rustc doesn't have warning |
| 496 |
return self._WARNING_LEVELS[level] |
| 497 |
|
| 498 |
def get_pic_args(self) -> T.List[str]: |
| 499 |
# relocation-model=pic is rustc's default already. |
| 500 |
return [] |
| 501 |
|
| 502 |
def get_std_link_args(self, env: Environment, is_thin: bool) -> T.List[str]: |
| 503 |
# Rust handles static library creation via --crate-type |
| 504 |
return [] |
| 505 |
|
| 506 |
def get_std_shared_lib_link_args(self) -> T.List[str]: |
| 507 |
# Rust handles shared library creation via --crate-type |
| 508 |
return [] |
| 509 |
|
| 510 |
def get_std_shared_module_link_args(self, target: BuildTarget) -> T.List[str]: |
| 511 |
# Rust handles shared module creation via --crate-type |
| 512 |
return [] |
| 513 |
|
| 514 |
def get_pie_args(self) -> T.List[str]: |
| 515 |
# Rustc currently has no way to toggle this, it's controlled by whether |
| 516 |
# pic is on by rustc |
| 517 |
return [] |
| 518 |
|
| 519 |
def get_compile_only_args(self) -> T.List[str]: |
| 520 |
return ['--crate-type', 'lib'] |
| 521 |
|
| 522 |
def get_pie_link_args(self) -> T.List[str]: |
| 523 |
# Rustc currently has no way to toggle this, it's controlled by whether |
| 524 |
# pic is on by rustc |
| 525 |
return [] |
| 526 |
|
| 527 |
def get_assert_args(self, disable: bool) -> T.List[str]: |
| 528 |
action = "no" if disable else "yes" |
| 529 |
return ['-C', f'debug-assertions={action}', '-C', 'overflow-checks=no'] |
| 530 |
|
| 531 |
def get_rust_tool(self, name: str) -> T.List[str]: |
| 532 |
if self.rustup_run_and_args: |
| 533 |
rustup_exelist, args = self.rustup_run_and_args |
| 534 |
# do not use extend so that exelist is copied |
| 535 |
exelist = rustup_exelist + [name] |
| 536 |
else: |
| 537 |
exelist = [name] |
| 538 |
args = self.get_exe_args() |
| 539 |
|
| 540 |
from ..programs import find_external_program |
| 541 |
for prog in find_external_program(self.environment, self.for_machine, exelist[0], exelist[0], |
| 542 |
[exelist[0]], allow_default_for_cross=False): |
| 543 |
exelist[0] = prog.path |
| 544 |
break |
| 545 |
else: |
| 546 |
return [] |
| 547 |
|
| 548 |
return exelist + args |
| 549 |
|
| 550 |
def has_multi_arguments(self, args: T.List[str]) -> T.Tuple[bool, bool]: |
| 551 |
return self.compiles('fn main() { std::process::exit(0) }\n', extra_args=args, mode=CompileCheckMode.COMPILE) |
| 552 |
|
| 553 |
def has_multi_link_arguments(self, args: T.List[str], to_host_args: bool = True) -> T.Tuple[bool, bool]: |
| 554 |
if to_host_args: |
| 555 |
args = rustc_link_args(args) |
| 556 |
args = rustc_link_args(self.linker.fatal_warnings()) + args |
| 557 |
return self.compiles('fn main() { std::process::exit(0) }\n', extra_args=args, mode=CompileCheckMode.LINK) |
| 558 |
|
| 559 |
@functools.lru_cache(maxsize=None) |
| 560 |
def get_rustdoc(self) -> T.Optional[RustdocTestCompiler]: |
| 561 |
exelist = self.get_rust_tool('rustdoc') |
| 562 |
if not exelist: |
| 563 |
return None |
| 564 |
|
| 565 |
return RustdocTestCompiler(exelist, self.version, self.for_machine, |
| 566 |
self.environment, |
| 567 |
full_version=self.full_version, |
| 568 |
linker=self.linker, rustc=self) |
| 569 |
|
| 570 |
def enable_env_set_args(self) -> T.Optional[T.List[str]]: |
| 571 |
'''Extra arguments to enable --env-set support in rustc. |
| 572 |
Returns None if not supported. |
| 573 |
''' |
| 574 |
if version_compare(self.version, '>= 1.76') and self.allow_nightly: |
| 575 |
return ['-Z', 'unstable-options'] |
| 576 |
return None |
| 577 |
|
| 578 |
|
| 579 |
class ClippyRustCompiler(RustCompiler): |
| 580 |
|
| 581 |
"""Clippy is a linter that wraps Rustc. |
| 582 |
|
| 583 |
This just provides us a different id |
| 584 |
""" |
| 585 |
|
| 586 |
id = 'clippy-driver rustc' |
| 587 |
|
| 588 |
|
| 589 |
class RustdocTestCompiler(RustCompiler): |
| 590 |
|
| 591 |
"""We invoke Rustdoc to run doctests. Some of the flags |
| 592 |
are different from rustc and some (e.g. --emit link) are |
| 593 |
ignored.""" |
| 594 |
|
| 595 |
id = 'rustdoc --test' |
| 596 |
|
| 597 |
def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice, |
| 598 |
env: Environment, full_version: T.Optional[str], |
| 599 |
linker: T.Optional['DynamicLinker'], rustc: RustCompiler): |
| 600 |
super().__init__(exelist, version, for_machine, |
| 601 |
env, full_version, linker) |
| 602 |
self.rustc = rustc |
| 603 |
|
| 604 |
@functools.lru_cache(maxsize=None) |
| 605 |
def get_sysroot(self) -> str: |
| 606 |
return self.rustc.get_sysroot() |
| 607 |
|
| 608 |
@functools.lru_cache(maxsize=None) |
| 609 |
def get_target_libdir(self) -> str: |
| 610 |
return self.rustc.get_target_libdir() |
| 611 |
|
| 612 |
@functools.lru_cache(maxsize=None) |
| 613 |
def get_cfgs(self) -> T.List[str]: |
| 614 |
return self.rustc.get_cfgs() |
| 615 |
|
| 616 |
def get_debug_args(self, is_debug: bool) -> T.List[str]: |
| 617 |
return [] |
| 618 |
|
| 619 |
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: |
| 620 |
return [] |
| 621 |
|
| 622 |
def get_output_args(self, outputname: str) -> T.List[str]: |
| 623 |
return [] |