Is it possible to override the crate-type specified in Cargo.toml from the command line when calling cargo build?

Viewed 1082

I want to generate a static library from a Rust project that I do not maintain. The project allows building a dynamic library — the Cargo.toml specifies crate-type = ["cdylib"].

Modifying the crate type in the file works, but I want to keep the unmodified original project as git submodule in my project if possible.

Is there is any flag that can be passed to the cargo build command to override this setting?

2 Answers

You can't override it, but you can supplement it. Use cargo rustc and pass --crate-type=staticlib directly to the compiler:

% cargo build
   Compiling example v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.31s

% find target -name '*.a'

% cargo rustc -- --crate-type=staticlib
   Compiling example v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 0.29s

% find target -name '*.a'
target/debug/deps/libexample.a

You can provide the crate-type, but you cannot override the one specified in your Cargo.toml:

$ cargo rustc -- --crate-type=staticlib
Compiling example v0.1.0 (/dev/tmp)
    Finished dev [unoptimized + debuginfo] target(s) in 0.34s

There is an tracking issue to add a --crate-type override. In the meantime, a workaround is to use cargo-crate-type:

$ cargo install cargo-crate-type

$ cargo crate-type static
$ cargo build

Note that this command will alter your Cargo.toml

Related