#cargo-build #cargo-check #cargo-metadata #env-var #cargo #cargo-subcommand #out-dir

app cargo-outdir

A cargo subcommand to get the $OUT_DIR environment variable from build scripts

3 unstable releases

0.2.1 Aug 29, 2024
0.2.0 Aug 28, 2024
0.1.0 Jan 7, 2022

#166 in Cargo plugins

MIT/Apache

37KB
686 lines

cargo-outdir crates.io

A cargo subcommand to fetch the $OUT_DIR variable from build scripts.

This is extremely useful to inspect the output of automatically generated code, like bindgen or parol.

This can be seen as an extension to cargo metadata, except it requires that cargo check succeeds.

If cargo check succeeds, then this command succeeds too (and give the same output).

Due to recent changes in cargo check, this will not invalidate cached outputs, so the build scripts wont be re-run unless they need to :)

It is effectively a workaround to rust-lang/cargo#7546

Examples

$ cargo out

Assuming your current crate is named current-crate, this wil output something like:

current-crate /Users/techcable/git/current-crate/target/debug/build/current-crate-82e5bb1cb82b68a7/out

Alternatively, to omit the name you can use --no-names. This is useful in shell scripts as $(cargo out --no-names)

This command works with any crate name (as long as it has a build.rs file).

$ cargo out syn indexmap

syn /Users/techcable/git/current-crate/target/debug/build/syn-2bbc24a01fc81726/out
indexmap /Users/techcable/git/current-crate/target/debug/build/indexmap-376e9f234cf30ee8/out

These are output in the order specified on the command line, seperated by newlines.

If the package doesn't have an out dir, the output will be "<MISSING OUT_DIR>" exactly and the exit code will be 2.

You might want to consider json output as well.

$ cargo out --json --all

{
    "syn": "/Users/techcable/git/current-crate/target/debug/build/syn-2bbc24a01fc81726/out",
    "indexmap": "/Users/techcable/git/current-crate/target/debug/build/indexmap-376e9f234cf30ee8/out",
    "libc:0.1.12": null,
    "libc:0.2.109": "/Users/techcable/git/current-crate/target/debug/build/libc-1c95e0902b980b08/out",
    // other_crates here
}

If packages don't have an $OUT_DIR (because they don't have a build script), then the value for the specified key will be null.

This can be used along with jq for easy processing in scripts :)

If multiple packages have the same name, then version will be added. More precicely, the json keys will be the minimal cargo pkgid needed to disambiguate them. This is actually somewhat difficult to do :)

How it works

This runs cargo check --message-format=json and extracts only the nessicarry information.

Historically this has been an issue for IDEs, both intellij-rust and rust-analsyer have struggled to support this.

Do to recent compiler changes, this will always output the proper $OUT_DIR variables for all packages that have one.

However recent changes to cargo check have made this possible. It sill :)

Dependencies

~5–7MB
~125K SLoC