結論
build.rs で print デバッグができなくて困っている方は cargo build -vv
を実行すると幸せになれると思います。
こんちには、matsu7874です。
Cargo は build.rs というファイルを作成しておくと、 cargo build
時に src/
以下のコンパイルの前に build.rs
を実行するという機能があります。
FFI(foreign function interface) のために他の言語で書かれたライブラリをビルドしたり、環境に応じた設定を行うといった用途が想定されています。
build.rs については Build Scripts - The Cargo Book が詳しいです。
この際、 cargo:
で始まるテキストを出力することで、Cargo に特定の動作をさせることができます。
build.rs を実行する際、標準出力は Cargo との通信のために利用され、 cargo:
で始まらないメッセージは無視されます。
Build scripts communicate with Cargo by printing to stdout. Cargo will interpret each line that starts with cargo: as an instruction that will influence compilation of the package. All other lines are ignored.
build.rs で出力した内容は、ターミナルに表示されません。
build.rs を print デバッグしたいなら cargo build -vv
cargo build -vv
として実行することで、build.rs の出力内容を確認することができます。
実際に動かしてみる
cargo new build_sample
とプロジェクトを作成し、build.rs と main.rs を下記のように修正します。
. ├── Cargo.toml ├── build.rs └── src └── main.rs
// build.rs fn main(){ println!("build.rs println"); eprintln!("build.rs eprintln"); println!("cargo:rustc-env=FROM_BUILDRS_STDOUT=TRUE"); eprintln!("cargo:rustc-env=FROM_BUILDRS_STDEER=FALSE"); }
// main.rs fn main() { println!("FROM_BUILDRS_STDOUT: {:?}",option_env!("FROM_BUILDRS_STDOUT")); println!("FROM_BUILDRS_STDEER: {:?}",option_env!("FROM_BUILDRS_STDEER")); }
cargo run
を実行すると、build.rs の標準出力でセットした FROM_BUILDRS_STDOUT=TRUE
が環境変数として設定されていることが確認できます。
$ cargo run Compiling build_sample v0.1.0 (/home/matsumoto/work/build_sample) Finished dev [unoptimized + debuginfo] target(s) in 0.36s Running `target/debug/build_sample` FROM_BUILDRS_STDOUT: Some("TRUE") FROM_BUILDRS_STDEER: None
build.rs を再度実行するため、cargo clean
を実行したあと cargo run -vv
を実行します。
[build_sample 0.1.0]
で始まる行に出力内容を確認できます。
$ cargo run -vv Compiling build_sample v0.1.0 (/home/matsumoto/work/build_sample) Running `CARGO=/home/matsumoto/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=build_script_build CARGO_MANIFEST_DIR=/home/matsumoto/work/build_sample CARGO_PKG_AUTHORS='matsu7874 <mtsmtkmt@gmail.com>' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=build_sample CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/matsumoto/work/build_sample/target/debug/deps:/home/matsumoto/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/matsumoto/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' rustc --crate-name build_script_build --edition=2018 build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=14f1f24d9212660a -C extra-filename=-14f1f24d9212660a --out-dir /home/matsumoto/work/build_sample/target/debug/build/build_sample-14f1f24d9212660a -C incremental=/home/matsumoto/work/build_sample/target/debug/incremental -L dependency=/home/matsumoto/work/build_sample/target/debug/deps` Running `/home/matsumoto/work/build_sample/target/debug/build/build_sample-14f1f24d9212660a/build-script-build` [build_sample 0.1.0] build.rs eprintln [build_sample 0.1.0] cargo:rustc-env=FROM_BUILDRS_STDEER=FALSE [build_sample 0.1.0] build.rs println [build_sample 0.1.0] cargo:rustc-env=FROM_BUILDRS_STDOUT=TRUE Running `CARGO=/home/matsumoto/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=build_sample CARGO_CRATE_NAME=build_sample CARGO_MANIFEST_DIR=/home/matsumoto/work/build_sample CARGO_PKG_AUTHORS='matsu7874 <mtsmtkmt@gmail.com>' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=build_sample CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 FROM_BUILDRS_STDOUT=TRUE LD_LIBRARY_PATH='/home/matsumoto/work/build_sample/target/debug/deps:/home/matsumoto/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib:/home/matsumoto/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/matsumoto/work/build_sample/target/debug/build/build_sample-d7ed5a0da2e8f1b8/out rustc --crate-name build_sample --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=6180fdd52899616c -C extra-filename=-6180fdd52899616c --out-dir /home/matsumoto/work/build_sample/target/debug/deps -C incremental=/home/matsumoto/work/build_sample/target/debug/incremental -L dependency=/home/matsumoto/work/build_sample/target/debug/deps` Finished dev [unoptimized + debuginfo] target(s) in 0.94s Running `target/debug/build_sample` FROM_BUILDRS_STDOUT: Some("TRUE") FROM_BUILDRS_STDEER: None
宣伝
『実践Rustプログラミング入門』のKindle版が発売されました。いろいろなユースケースで遊べる内容になっています。ぜひお買い求めください。