Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

Suppose I execute cargo new one --bin and cargo new two --bin then add the same dependency to each project's Cargo.toml and build them.

Now there are two absolutely identical sets of libraries:

/one/target/debug/deps/ *.rlib

/two/target/debug/deps/ *.rlib

They are same files and waste storage space, but really the problem is that I have to compile these libraries again for every project. It takes a very much time. There is the same problem with cargo install.

Can I specify a place to store compiled libraries to avoid recompilation?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...