1
votes

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.

1
Why not just create a build.sh that runs both commands? - eggyal
I could do that, but that doesn't feel like good practice. Especially since build targets are usually specified in the Cargo.toml - Valentin Metz
Not quite what you asked, but the arch module has docu for dynamic CPU-feature detection to avoid creating multiple binaries. - maxy
@maxy interesting. But for now I'll trust the compiler to optimize based on the target arch. - Valentin Metz

1 Answers

2
votes

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.