Skip to content

Instantly share code, notes, and snippets.

@cchandler
Created April 13, 2019 19:06
Show Gist options
  • Save cchandler/6db502b888045f9b43df01ae32928419 to your computer and use it in GitHub Desktop.
Save cchandler/6db502b888045f9b43df01ae32928419 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -x
OUTPUT_DIR=`pwd`
LLVM_HOME=/usr/local/Cellar/llvm/8.0.0/
RUSTUP_TOOLCHAIN_LIB=/Users/chris/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib
# Build main with temporary files preserved and emit LLVM-IR
rustc -C save-temps --emit=llvm-ir main.rs
# Remove the unoptimized bc or we'll get duplicate symbols at link time
rm *no-opt*
# Run all the bitcode through our phantom pass
find . -name '*.bc' | xargs -I{} -n 1 $LLVM_HOME/bin/opt -o {} {}
# Compile the bitcode to object files
find . -name '*.bc' | xargs -n 1 $LLVM_HOME/bin/llc -filetype=obj
# Complete the linking to default a.out file
# Extra flags for removing C++ default libs, but link System, resolv, libc, and math.
# Also strip dead code so we don't have tons of rust std library code that isn't referenced.
$LLVM_HOME/bin/clang -m64 *.o -Wl,-dead_strip -nodefaultlibs -lSystem -lresolv -lc -lm $(find /Users/chris/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib -name '*rlib')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment