#!/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)

    if b"--global-base cannot be less than stack size when --stack-first is used" in result.stderr:
        args += ["--no-stack-first"]
        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()
