How difficult is it to allow parallel compilation

2020-04-02 08:58发布

The default file-tree created by Cargo allows parallel compilation of release and debug builds as they are located in their own directories; target/release and target/debug, respectively.

How difficult is it to also allow parallel compilation of stable/nightly-compiler. For example using the directories

  • target/debug/stable
  • target/debug/nightly

I am aware it can be done with jails/containers, but I was hoping for a somewhat more Cargo-ish solution.

1条回答
冷血范
2楼-- · 2020-04-02 09:35

Modern Rust

I believe that your main problem of rebuilding dependencies no longer exists:

$ cargo +nightly build
    Updating crates.io index
   Compiling either v1.5.0
   Compiling itertools v0.8.0
   Compiling example v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 5.87s
$ cargo +stable build
   Compiling either v1.5.0
   Compiling itertools v0.8.0
   Compiling example v0.1.0 (/private/tmp/example)
    Finished dev [unoptimized + debuginfo] target(s) in 2.67s
$ cargo +nightly build
    Finished dev [unoptimized + debuginfo] target(s) in 0.17s
$ cargo +stable build
    Finished dev [unoptimized + debuginfo] target(s) in 0.16s

I believe that this is a side-effect of the work done for incremental compilation: the compiler version (or something equivalent) is used as part of the hashing algorithm used for build artifacts. Thus, artifacts from multiple toolchains can coexist.

This does not cover the final artifact, which has a fixed name and will be overridden. Keep on reading if you really need to keep both in parallel.

Original answer

As explained in Is it possible to deactivate file locking in cargo?, you can set the environment variable CARGO_TARGET_DIR for each channel you are interested in:

$ CARGO_TARGET_DIR=$PWD/stable rustup run stable cargo build
   Compiling many v0.1.0 (file:///private/tmp/many)
    Finished debug [unoptimized + debuginfo] target(s) in 0.89 secs
$ CARGO_TARGET_DIR=$PWD/nightly rustup run nightly cargo build
   Compiling many v0.1.0 (file:///private/tmp/many)
    Finished debug [unoptimized + debuginfo] target(s) in 0.62 secs
$ ./stable/debug/many
Hello, world!
$ ./nightly/debug/many
Hello, world!
查看更多
登录 后发表回答