How to emit LLVM-IR from Cargo

2020-07-03 04:45发布

问题:

How can I get cargo to emit LLVM-IR instead of a binary for my project? I know that you can use the --emit=llvm-ir flag in rustc, but I've read some Github issues that show it's impossible to pass arbitrary compiler flags to cargo.

Is there any way I can get cargo to emit LLVM-IR directly?

回答1:

There is cargo rustc to pass arbitrary compiler flags through Cargo to rustc. So I think:

cargo rustc -- --emit=llvm-ir

is what you want!



回答2:

EDIT: You should use Jacob's answer instead; a lot easier and less hacky.

Build the project with cargo normally but add on the -v flag to show verbose output. The command will have a result like this:

casey@casey-ubuntu:~/Documents/project$ cargo build -v
   Fresh aster v0.22.1
   Fresh num-traits v0.1.34
   Fresh itoa v0.1.1
   ...
Compiling project v0.1.0 (file:///home/casey/Documents/project)
 Running `rustc src/main.rs --crate-name ...`
Finished debug [unoptimized + debuginfo] target(s) in 3.54 secs

If the command produces no output, make a change somewhere in your project code to trick the compiler into rebuilding it, since it will only rebuild if it detects a change in one of the files.

Copy the rustc command from inside the ` markers on the line starting with "Running `rustc..." and append --emit=llvm-ir to it.

This will produce a .ll file in your /target/debug folder.