Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get assembly output from building with Cargo?

While I've seen docs on using rustc directly to output assembly, having to manually extract commands used by Cargo and edit them to write assembly is tedious.

Is there a way to run Cargo that writes out assembly files?

like image 539
ideasman42 Avatar asked Aug 30 '16 06:08

ideasman42


2 Answers

In addition to kennytm's answer, you can also use the RUSTFLAGS environment variable and use the standard cargo commands:

RUSTFLAGS="--emit asm" cargo build cat target/debug/deps/project_name-hash.s 

Or in release mode (with optimizations):

RUSTFLAGS="--emit asm" cargo build --release cat target/release/deps/project_name-hash.s 

You can pass different values to the --emit parameter, including (but not limited to):

  • mir (Rust intermediate representation)
  • llvm-ir (LLVM intermediate representation)
  • llvm-bc (LLVM byte code)
  • asm (assembly)
like image 38
Lukas Kalbertodt Avatar answered Sep 17 '22 13:09

Lukas Kalbertodt


You can use Cargo's cargo rustc command to send arguments to rustc directly:

cargo rustc -- --emit asm ls target/debug/deps/<crate_name>-<hash>.s 

For optimized assembly:

cargo rustc --release -- --emit asm ls target/release/deps/<crate_name>-<hash>.s 

If you see multiple <crate_name>-<hash>-<hash>.rcgu.s files instead of a <crate_name>-<hash>.s file, disable incremental compilation by setting the environment variable CARGO_INCREMENTAL=0.

like image 127
kennytm Avatar answered Sep 17 '22 13:09

kennytm