Change artifact name or output directory based on feature(s)

I'm using the [features] option of Cargo to conditionally compile certain things in my project.

Is it possible to change the output directory (or target name) of my Rust library based on the features that were selected at compile time?


The exact place where this is causing me problems is with users who are compiling my Rust library as part of a larger C/C++ project. They have different Make targets which compile the my library with different features but since the output from each feature configuration is the same artifact they end up clobbering one another.

There is the --target-dir command line argument for cargo build you could use to build each version (with the different features) in a different directory. See here: cargo build - The Cargo Book. I'd imagine you'd have Makefile targets for building your crate looking something like:

build-foo-feature-x:
    cd foo && cargo build --release --features=x  --target-dir=target-x

build-foo-feature-y:
    cd foo && cargo build --release --features=y  --target-dir=target-y
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.