rust-lld-wrapper 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # A wrapper for rust-lld that removes certain arguments inserted by rustc that
  3. # we'd like to override
  4. import sys
  5. import subprocess
  6. import re
  7. from os import path
  8. def main():
  9. args = sys.argv[1:]
  10. strip_debug = "--v86-strip-debug" in args
  11. # filter out args inserted by rustc
  12. TO_REMOVE = {
  13. "--export-table",
  14. "--stack-first",
  15. "--strip-debug",
  16. "--v86-strip-debug",
  17. }
  18. args = list(filter(lambda arg: arg not in TO_REMOVE, args))
  19. if strip_debug:
  20. args += ["--strip-debug"]
  21. lld = find_rust_lld()
  22. result = subprocess.run([lld] + args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  23. print(result.stderr, file=sys.stderr)
  24. print(result.stdout)
  25. result.check_returncode()
  26. def find_host_triplet():
  27. rustc = subprocess.run(["rustc", "--version", "--verbose"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  28. rustc.check_returncode()
  29. rustc_details = rustc.stdout.decode("utf8")
  30. host = re.search(r"host: (.*)", rustc_details)
  31. if host is None:
  32. raise ValueError("unexpected rustc output")
  33. return host.group(1)
  34. def find_rust_lld():
  35. try:
  36. which = subprocess.run(["rustup", "which", "rustc"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  37. except FileNotFoundError:
  38. return "lld"
  39. which.check_returncode()
  40. rustc_path = which.stdout.decode("utf8").strip()
  41. assert path.basename(rustc_path) == "rustc"
  42. bin_path = path.dirname(rustc_path)
  43. triplet = find_host_triplet()
  44. rust_lld_path = path.join(bin_path, "../lib/rustlib", triplet, "bin/rust-lld")
  45. assert path.isfile(rust_lld_path)
  46. return rust_lld_path
  47. main()