Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access current cargo profile (build, test, bench, doc, ....) from the build script (build.rs)

I want to write a custom build.rs script that generates some diagrams to accompany the documentation for a crate I'm working on. I want this script to run only when I run cargo doc, not the other profiles (cargo build, cargo test, ...). What would be the best way to do that?

I was hoping that cargo would pass this info to build.rs in the PROFILE env variable, but that seems to only contain "debug" or "release".

like image 439
adam Avatar asked Apr 06 '16 09:04

adam


People also ask

Where does cargo build output go?

Cargo stores the output of a build into the "target" directory. By default, this is the directory named target in the root of your workspace. To change the location, you can set the CARGO_TARGET_DIR environment variable, the build. target-dir config value, or the --target-dir command-line flag.

Where does build rs go?

Placing a file named build.rs in the root of a package will cause Cargo to compile that script and execute it just before building the package. Some example use cases of build scripts are: Building a bundled C library.

How does cargo bench work?

By default, cargo bench uses the bench profile, which enables optimizations and disables debugging information. If you need to debug a benchmark, you can use the --profile=dev command-line option to switch to the dev profile. You can then run the debug-enabled benchmark within a debugger.

What is cargo build -- release?

cargo build --release puts the resulting binary in target/release instead of target/debug . Compiling in debug mode is the default for development-- compilation time is shorter since the compiler doesn't do optimizations, but the code will run slower. Release mode takes longer to compile, but the code will run faster.


1 Answers

This is not possible as of Rust 1.47. Cargo issue #4001 tracks the possibility of supporting this in some fashion.

like image 91
Shepmaster Avatar answered Nov 02 '22 23:11

Shepmaster