0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

共有targetでRust依存ビルドを再利用する設定ガイド

0
Last updated at Posted at 2025-12-14

大きなクレートで target が肥大化する問題

bebyなどの大きなクレートをつかって書いてると同じファイルを複数のプロジェクトで無駄にコンパイルして時間がかかり、容量も食ってしまう。

解決策:中間生成物だけ共通ディレクトリに集約する

バージョン要件
以下を確認する。1.91.0以降なら中間生成物を分けることが可能である。のでそれ未満ならばupdateをする。

cargo --version
# rustup update #必要なら

ただver.1.90.0において中間生成物と最終生成物を配置するtarget-dirの指定なら可能であるっぽかったが、ドキュメント読んでないので不明。

手順

以下の場所に以下のファイルを作成する

  • Windows: %USERPROFILE%.cargo\config.toml
  • Unix: $HOME/.cargo/config.toml
[build]
# target-dir = "target"         # 最終生成物のパス?中間もかも、今回は設定しない
build-dir = "target"          # 中間生成物のパス

これによって以下に中間生成物が入る

  • Windows: %USERPROFILE%\target
  • Unix: $HOME/target
    なお、最終生成物はプロジェクトのtarget入る

設定の上書き

特別に中間生成物をプロジェクトのtargetに置きたい、例えばcodexに依存先のソースコードを読ませたいときなどそういうときはプロジェクトの.cargo/config.tomlに設定すればbuild-dirの設定が上書きされる。

#path to project/.cargo/config.toml
[build]
build-dir = "target"

すると下記がに中間生成物及び最終生成物が生成される。

  • path to project/target

参考: 設定ファイルの優先順位

  • /projects/foo/bar/baz/.cargo/config.toml
  • /projects/foo/bar/.cargo/config.toml
  • /projects/foo/.cargo/config.toml
  • /projects/.cargo/config.toml
  • /.cargo/config.toml
  • $CARGO_HOME/config.toml which defaults to:
    • Windows: %USERPROFILE%.cargo\config.toml
    • Unix: $HOME/.cargo/config.toml

ハッシュによる衝突回避の仕組み

ライブラリーは次の要素のハッシュ値を持っていてそれを基に管理されていて競合のおそれはないと思われる。

  • コンパイラーのメタデータ
  • build方法
  • ライブラリーが依存するクレートすべて
  • ライブラリーのすべてのソース名

実際、cargo install cargo build --releaseではライブラリーが異なるハッシュを使っていてrustup update後はも異なるハッシュ値になった。また、異なるプロジェクトから同じバージョンのクレートを使う際は片方でコンパイルしていたらもう片方ではコンパイルせずに同じものを共有していた。もちろんライブラリーの依存関係が異なれば再びコンパイルすることになると思われるが。

evcxr が動かなくなった

EVCXR_TMPDIR=/tmp CARGO_TARGET_DIR=/tmp/target CARGO_BUILD_BUILD_DIR=/tmp/target evcxr

EVCXR_TMPDIR=/tmp CARGO_TARGET_DIR=/tmp/target CARGO_BUILD_BUILD_DIR=/tmp/target evcxr
次のように実行すればevcxr動いた

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?