Can I prevent cargo from rebuilding libraries with every new project?

自古美人都是妖i 提交于 2019-12-01 03:49:34

Several Cargo projects might share the libraries by using the same target dir.

.cargo/config

Place a ".cargo" folder in a project and create a "config" file there containing:

[build]
target-dir = "/path/to/your/shared/target/dir"

On Unix this might look like:

mkdir ~/shared_rust_target
mkdir .cargo
echo "[build]" > .cargo/config
echo "target-dir = \"$HOME/shared_rust_target\"" >> .cargo/config

CARGO_TARGET_DIR

Set the CARGO_TARGET_DIR environment variable.

On Unix this might look like:

export CARGO_TARGET_DIR = "$HOME/shared_rust_target"

See this commit for some extra target-dir documentation.

In particular, prior to Cargo 1.9 you shouldn't build several projects into the same target dir concurrently. (Here's more on how Cargo 1.9 supports concurrent builds).

target-dir is also mentioned in the Cargo docs.

Note that personally I'm only using the target-dir feature to redirect the builds into a different place, so I never tried to do the shared builds. But it should work, according to this issue.


P.S. It is now also possible to achieve crate reuse with workspaces.

Even if there is a way to do it, you probably don't want to. Just because you happen to be using the same libraries doesn't mean that they were compiled the same. For example, Cargo supports the concept of features, compilation time configuration that changes how the crate was compiled.

Likewise, you may need to support multiple versions of Rust, such as nightly and stable. Or perhaps you need to cross-compile for a different architecture. Each of those will produce different code.

Cargo will cache the build products of a single project, so I've found the overhead not really noticeable, and I compile a lot of projects from people asking questions on Stack Overflow! :-)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!