build/cargo-linker
author Serban Stanca <sstanca@mozilla.com>
Thu, 17 Jul 2025 20:21:32 +0300 (11 hours ago)
changeset 797003 7ec5a911287f51bc177058928bb102163a3b656e
parent 754795 f6cff71bfc6cba2b1d37d79fce2cb6b5d0b00852
permissions -rwxr-xr-x
Revert "Bug 1977690 - Remove unused AppRequestInterceptor in androidTests r=aaronmt" for causing fenix-debug failures. This reverts commit bc9dc5f4296482e17560627acaacd2797e462211.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
     1
#!/usr/bin/env python3
355703
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     2
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     3
# If you want to use a custom linker with Cargo, Cargo requires that you
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     4
# specify it in Cargo.toml or via the matching environment variable.
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     5
# Passing extra options to the linker is possible with Cargo via
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     6
# RUSTFLAGS='-C link-args', but testing showed that doing this reliably
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     7
# was difficult.
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     8
#
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
     9
# Our solution to these problems is to use this wrapper script.  We pass
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    10
# in the LD and the LDFLAGS to use via environment variables.
355703
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    11
#
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    12
# * MOZ_CARGO_WRAP_LD is equivalent to CC on Unix-y platforms, and CC
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    13
#   frequently has additional arguments in addition to the compiler
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    14
#   itself.
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    15
#
355703
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    16
# * MOZ_CARGO_WRAP_LDFLAGS contains space-separated arguments to pass,
465628
fc6f661f4113fa38c6e44663c404edc7e3ceadec Bug 1524396 - Unify how target/host linker/flags are passed to rust. r=chmanchester
Mike Hommey <mh+mozilla@glandium.org>
parents: 465533
diff changeset
    17
#   and not quoting it ensures that each of those arguments is passed
355703
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    18
#   as a separate argument to the actual LD.
392763
69465fa861e14da6b903ec4f154abf355e5d6d79 Bug 1418598 - Make cargo-linker properly handle quoted strings in MOZ_CARGO_WRAP_LD and MOZ_CARGO_WRAP_LDFLAGS. r=ted
Mike Hommey <mh+mozilla@glandium.org>
parents: 355703
diff changeset
    19
#
604132
8c18f6ce84755773d4231ccd164bb7e456044dbd Bug 1747298 - Use CXX for Cargo linking when necessary. r=glandium
Christian Holler <choller@mozilla.com>
parents: 604130
diff changeset
    20
# * In rare cases, we also need MOZ_CARGO_WRAP_LD_CXX, which is the
8c18f6ce84755773d4231ccd164bb7e456044dbd Bug 1747298 - Use CXX for Cargo linking when necessary. r=glandium
Christian Holler <choller@mozilla.com>
parents: 604130
diff changeset
    21
#   equivalent of CXX, when linking C++ code. Usually, this should
8c18f6ce84755773d4231ccd164bb7e456044dbd Bug 1747298 - Use CXX for Cargo linking when necessary. r=glandium
Christian Holler <choller@mozilla.com>
parents: 604130
diff changeset
    22
#   simply work by the use of CC and -lstdc++ (added by cc-rs).
8c18f6ce84755773d4231ccd164bb7e456044dbd Bug 1747298 - Use CXX for Cargo linking when necessary. r=glandium
Christian Holler <choller@mozilla.com>
parents: 604130
diff changeset
    23
#   However, in the case of sanitizer runtimes, there is a separate
8c18f6ce84755773d4231ccd164bb7e456044dbd Bug 1747298 - Use CXX for Cargo linking when necessary. r=glandium
Christian Holler <choller@mozilla.com>
parents: 604130
diff changeset
    24
#   runtime for C and C++ and linking C++ code with the C runtime can
8c18f6ce84755773d4231ccd164bb7e456044dbd Bug 1747298 - Use CXX for Cargo linking when necessary. r=glandium
Christian Holler <choller@mozilla.com>
parents: 604130
diff changeset
    25
#   fail if the requested feature is in the C++ runtime only (bug 1747298).
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    26
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    27
import os
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    28
import sys
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    29
672369
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    30
# This is not necessarily run with a virtualenv python, so add
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    31
# the necessary directory for the shellutil module.
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    32
base_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    33
sys.path.insert(0, os.path.join(base_dir, "python", "mozbuild"))
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    34
from mozbuild.shellutil import split
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    35
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    36
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    37
SANITIZERS = {
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    38
    "asan": "address",
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    39
    "hwasan": "hwaddress",
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    40
    "lsan": "leak",
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    41
    "msan": "memory",
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    42
    "tsan": "thread",
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    43
}
355703
fc34b2a31c4beb821ab40c8eca744c78212c14f5 Bug 1329737 - part 3 - use an alternate linker for Cargo invocations; r=rillian
Nathan Froyd <froydnj@mozilla.com>
parents:
diff changeset
    44
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    45
use_clang_sanitizer = os.environ.get("MOZ_CLANG_NEWER_THAN_RUSTC_LLVM")
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    46
wrap_ld = os.environ["MOZ_CARGO_WRAP_LD"]
754795
f6cff71bfc6cba2b1d37d79fce2cb6b5d0b00852 Bug 1916426 - Pass our LDFLAGS last to the linker command line cargo invokes r=firefox-build-system-reviewers,sergesanspaille
Mike Hommey <mh+mozilla@glandium.org>
parents: 672369
diff changeset
    47
args = []
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    48
for arg in sys.argv[1:]:
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    49
    if arg in ["-lc++", "-lstdc++"]:
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    50
        wrap_ld = os.environ["MOZ_CARGO_WRAP_LD_CXX"]
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    51
    elif use_clang_sanitizer and arg.endswith("san.a"):
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    52
        # When clang is newer than rustc's LLVM, we replace rust's sanitizer
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    53
        # runtimes with clang's.
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    54
        filename = os.path.basename(arg)
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    55
        prefix, dot, suffix = filename[:-2].rpartition(".")
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    56
        if (
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    57
            prefix.startswith("librustc-")
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    58
            and prefix.endswith("_rt") and dot == "."
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    59
        ):
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    60
            args.append(f"-fsanitize={SANITIZERS[suffix]}")
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    61
            continue
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    62
    args.append(arg)
754795
f6cff71bfc6cba2b1d37d79fce2cb6b5d0b00852 Bug 1916426 - Pass our LDFLAGS last to the linker command line cargo invokes r=firefox-build-system-reviewers,sergesanspaille
Mike Hommey <mh+mozilla@glandium.org>
parents: 672369
diff changeset
    63
args.extend(split(os.environ["MOZ_CARGO_WRAP_LDFLAGS"]))
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    64
672369
087853f0aeda29981c71db5b19bc7703bd4f4be2 Bug 1844538 - Properly split environment variables in cargo-linker. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 643525
diff changeset
    65
wrap_ld = split(wrap_ld)
640590
6b9175bbaba8995fc54eb3da9837cc83b14741fc Bug 1791476 - When clang is newer than rust's llvm, use clang sanitizer runtimes. r=firefox-build-system-reviewers,ahochheiden
Mike Hommey <mh+mozilla@glandium.org>
parents: 604132
diff changeset
    66
os.execvp(wrap_ld[0], wrap_ld + args)