File size: 1,695 Bytes
8df6da4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

# A wrapper for rust-lld that removes certain arguments inserted by rustc that
# we'd like to override

import sys
import subprocess
import re
from os import path

def main():
    args = sys.argv[1:]

    strip_debug = "--v86-strip-debug" in args

    # filter out args inserted by rustc
    TO_REMOVE = {
        "--export-table",
        "--stack-first",
        "--strip-debug",
        "--v86-strip-debug",
    }
    args = list(filter(lambda arg: arg not in TO_REMOVE, args))

    if strip_debug:
        args += ["--strip-debug"]

    lld = find_rust_lld()

    result = subprocess.run([lld] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print(result.stderr, file=sys.stderr)
    print(result.stdout)

    result.check_returncode()

def find_host_triplet():
    rustc = subprocess.run(["rustc", "--version", "--verbose"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    rustc.check_returncode()

    rustc_details = rustc.stdout.decode("utf8")
    host = re.search(r"host: (.*)", rustc_details)
    if host is None:
        raise ValueError("unexpected rustc output")
    return host.group(1)

def find_rust_lld():
    try:
        which = subprocess.run(["rustup", "which", "rustc"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    except FileNotFoundError:
        return "lld"
    which.check_returncode()

    rustc_path = which.stdout.decode("utf8").strip()
    assert path.basename(rustc_path) == "rustc"

    bin_path = path.dirname(rustc_path)
    triplet = find_host_triplet()

    rust_lld_path = path.join(bin_path, "../lib/rustlib", triplet, "bin/rust-lld")
    assert path.isfile(rust_lld_path)

    return rust_lld_path

main()