Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active November 2, 2021 01:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan/1f78de3a4ebd78b6d2e145f70bbc75ed to your computer and use it in GitHub Desktop.
Save ivan/1f78de3a4ebd78b6d2e145f70bbc75ed to your computer and use it in GitHub Desktop.
cargo wrapper script to use lld via RUSTFLAGS="-C link-arg=-fuse-ld=lld" for all projects
#!/usr/bin/env bash
# cargo wrapper to use lld on all projects, which is 2-4x faster to link than bfd:
# https://github.com/rust-lang/rust/issues/39915#issuecomment-538049306
# Note that the advised `-C linker=clang` does not actually make it use lld.
#
# Place this into an executable ~/bin/cargo and edit the real cargo path at the
# end of the wrapper. Remove the nice and choom if not desired.
#
# You will also need to symlink a recent clang to ~/bin/cc so that -fuse-ld=lld works.
#
# If a project or any of its dependencies link to openssl, it will probably also need:
# openssl = { version = "0.10", features = [ "vendored" ] }
set -e
# Don't touch RUSTFLAGS when building wasm, because lld does not work.
# ("note: rust-lld: error: unknown argument: -fuse-ld=lld")
#
# Don't touch RUSTFLAGS if `-C link-arg=-fuse-ld=lld` is already in RUSTFLAGS, because
# otherwise `cargo clippy` builds with `-C link-arg=-fuse-ld=lld -C link-arg=-fuse-ld=lld`
# and spends a lot of time recompiling with "different" compiler options.
#
if [[
" $* " != *" --target wasm32-unknown-unknown "* &&
" $RUSTFLAGS " != *" -C link-arg=-fuse-ld=lld "*
]]; then
export RUSTFLAGS="-C link-arg=-fuse-ld=lld ${RUSTFLAGS:-}"
fi
# Make sure ~/bin is in PATH because it's the presumed location for the cc -> clang symlink.
# You can remove this if your cc is always clang by some other means.
if [[ :$PATH: != *:"$HOME/bin":* ]]; then
export PATH=$HOME/bin:$PATH
fi
exec nice -n 2 -- choom -n 750 -- /run/current-system/sw/bin/cargo "$@"
@vporton
Copy link

vporton commented Nov 16, 2020

It does not work for projects that build both native and WASM things.

@ivan
Copy link
Author

ivan commented Nov 16, 2020

@vporton let me know if you find a workaround. does global cargo configuration to set -C link-arg=-fuse-ld=lld work?

@vporton
Copy link

vporton commented Nov 16, 2020

@vporton let me know if you find a workaround. does global cargo configuration to set -C link-arg=-fuse-ld=lld work?

How to set it?

@ivan
Copy link
Author

ivan commented Nov 16, 2020

@vporton maybe something like

[build]
rustflags = ["-C", "linker=clang", "-C", "link-arg=-fuse-ld=lld"]

in ~/.cargo/config

via rust-lang/rust#39915 (comment)

@vporton
Copy link

vporton commented Nov 16, 2020

I've added the above mentioned config and it seems that cargo uses ld not lld.

@ivan
Copy link
Author

ivan commented Nov 2, 2021

This lld-using wrapper is now obsoleted by the mold-using wrapper https://gist.github.com/ivan/594026bee196cb290764ca7af861b138

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment