2022-11-08 00:44:27 +00:00
|
|
|
#!/usr/bin/env python3
|
2017-04-28 18:06:41 +00:00
|
|
|
|
|
|
|
# If you want to use a custom linker with Cargo, Cargo requires that you
|
|
|
|
# specify it in Cargo.toml or via the matching environment variable.
|
|
|
|
# Passing extra options to the linker is possible with Cargo via
|
|
|
|
# RUSTFLAGS='-C link-args', but testing showed that doing this reliably
|
|
|
|
# was difficult.
|
|
|
|
#
|
|
|
|
# Our solution to these problems is to use this wrapper script. We pass
|
2022-11-08 00:44:27 +00:00
|
|
|
# in the LD and the LDFLAGS to use via environment variables.
|
2017-04-28 18:06:41 +00:00
|
|
|
#
|
|
|
|
# * MOZ_CARGO_WRAP_LD is equivalent to CC on Unix-y platforms, and CC
|
|
|
|
# frequently has additional arguments in addition to the compiler
|
|
|
|
# itself.
|
2022-11-08 00:44:27 +00:00
|
|
|
#
|
2017-04-28 18:06:41 +00:00
|
|
|
# * MOZ_CARGO_WRAP_LDFLAGS contains space-separated arguments to pass,
|
Bug 1524396 - Unify how target/host linker/flags are passed to rust. r=chmanchester
The current setup uses different ways for different platforms, with
different workarounds, even using extra configuration items for Windows.
Now that there can't be a difference between the host per the build
system and the host per rust, we can get rid of those configuration
items, and use a more common infrastructure.
We cannot, however, avoid using wrapper scripts, because per-target rust
link-arg flags don't work up great.
The downside is that multiplies the number of wrappers, as we now have
to have a different one for host and target, and then we have .bat files
and shell scripts for, respectively, Windows hosts, and other hosts.
Depends on D24321
Differential Revision: https://phabricator.services.mozilla.com/D24322
--HG--
extra : moz-landing-system : lando
2019-03-22 11:05:18 +00:00
|
|
|
# and not quoting it ensures that each of those arguments is passed
|
2017-04-28 18:06:41 +00:00
|
|
|
# as a separate argument to the actual LD.
|
2017-11-18 11:24:12 +00:00
|
|
|
#
|
2022-01-11 11:26:39 +00:00
|
|
|
# * In rare cases, we also need MOZ_CARGO_WRAP_LD_CXX, which is the
|
|
|
|
# equivalent of CXX, when linking C++ code. Usually, this should
|
|
|
|
# simply work by the use of CC and -lstdc++ (added by cc-rs).
|
|
|
|
# However, in the case of sanitizer runtimes, there is a separate
|
|
|
|
# runtime for C and C++ and linking C++ code with the C runtime can
|
|
|
|
# fail if the requested feature is in the C++ runtime only (bug 1747298).
|
2017-04-28 18:06:41 +00:00
|
|
|
|
2022-11-08 00:44:27 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
SANITIZERS = {
|
|
|
|
"asan": "address",
|
|
|
|
"hwasan": "hwaddress",
|
|
|
|
"lsan": "leak",
|
|
|
|
"msan": "memory",
|
|
|
|
"tsan": "thread",
|
|
|
|
}
|
|
|
|
|
|
|
|
use_clang_sanitizer = os.environ.get("MOZ_CLANG_NEWER_THAN_RUSTC_LLVM")
|
|
|
|
wrap_ld = os.environ["MOZ_CARGO_WRAP_LD"]
|
|
|
|
args = os.environ["MOZ_CARGO_WRAP_LDFLAGS"].split()
|
|
|
|
for arg in sys.argv[1:]:
|
|
|
|
if arg in ["-lc++", "-lstdc++"]:
|
|
|
|
wrap_ld = os.environ["MOZ_CARGO_WRAP_LD_CXX"]
|
|
|
|
elif use_clang_sanitizer and arg.endswith("san.a"):
|
|
|
|
# When clang is newer than rustc's LLVM, we replace rust's sanitizer
|
|
|
|
# runtimes with clang's.
|
|
|
|
filename = os.path.basename(arg)
|
|
|
|
prefix, dot, suffix = filename[:-2].rpartition(".")
|
|
|
|
if (
|
|
|
|
prefix.startswith("librustc-")
|
|
|
|
and prefix.endswith("_rt") and dot == "."
|
|
|
|
):
|
|
|
|
args.append(f"-fsanitize={SANITIZERS[suffix]}")
|
|
|
|
continue
|
|
|
|
args.append(arg)
|
|
|
|
|
|
|
|
wrap_ld = wrap_ld.split()
|
|
|
|
os.execvp(wrap_ld[0], wrap_ld + args)
|