Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rust Cargo build and crosscompile for specific CPU

I want to compile two binaries for different target architectures (eg. Skylake and Sandy Bridge). These are usually two lengthy cargo commands:

RUSTFLAGS="-C target-cpu=skylake" cargo build --target x86_64-unknown-linux-gnu --release

How can I set up cargo to build both binaries (with different names) from the same main.rs automatically? Ideally in either the config.toml or the Cargo.toml so I can add it to a repository.

like image 828
Valentin Metz Avatar asked Oct 29 '25 15:10

Valentin Metz


1 Answers

You can add the following text to your config.toml:

[build]
target = x86_64-unknown-linux-gnu
rustflags = ["-C","target-cpu=skylake"]

[profile.dev]   #do not need to add `--release` now
opt-level = 3
debug = false
debug-assertions = false
overflow-checks = false
lto = false
panic = 'unwind'
incremental = false
codegen-units = 16
rpath = false

But it seems like it can't compile for two different target architectures with one config.toml, so you may have to create two config.toml and use cargo --manifest-path PATH/TO/CONFIG to compile two binaries separately.

like image 174
WinterCicada Avatar answered Nov 01 '25 13:11

WinterCicada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!