Ayaz Agayev
Ayaz Agayev

Reputation: 23

How to compile rust packages with different targets

Lets say I have nested packages like /foo/bar

Now I want to compile these packages with different targets. So, How to compile /foo package with target wasm32-unknown-unknownand /foo/bar package with target x86_64-unknown-linux-gnu with one build command like cargo build --release?

Upvotes: 1

Views: 1554

Answers (1)

eggyal
eggyal

Reputation: 125995

You can specify the default target (that will be used absent an explicit --target option on the command line) in a configuration file. I therefore suggest that you create the following files:

  1. /foo/.cargo/config.toml

    [build]
    target = "wasm32-unknown-unknown"
    
  2. /foo/bar/.cargo/config.toml

    [build]
    target = "x86_64-unknown-linux-gnu"
    

To ensure that /foo/bar is built whenever /foo is built, you can use a build script:

/foo/build.rs

fn main() {
    println!("cargo:rerun-if-changed=bar");

    let profile = std::env::var("PROFILE").unwrap();
    let status = std::process::Command::new("cargo")
        .arg("build")
        .arg(format!("--{}", profile))
        .current_dir("bar")
        .status()
        .expect("failed to execute cargo");

    assert!(status.success(), "failed to build bar");
}

Upvotes: 2

Related Questions