Skip to content

Commit 0d5b7b0

Browse files
committed
Add test for multiple OUT_DIR
1 parent a6c58d4 commit 0d5b7b0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/cargo/core/compiler/custom_build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ fn build_work(build_runner: &mut BuildRunner<'_, '_>, unit: &Unit) -> CargoResul
338338
.map(|d| &d.unit)
339339
.expect("running a script not depending on an actual script");
340340
let script_dir = build_runner.files().build_script_dir(build_script_unit);
341+
// println!("{:?}", script_dir);
341342
let script_out_dir = build_runner.files().build_script_out_dir(unit);
342343
let script_run_dir = build_runner.files().build_script_run_dir(unit);
343344
let build_plan = bcx.build_config.build_plan;

tests/testsuite/build_scripts_multiple.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,71 @@ fn bar() {
764764
"#]])
765765
.run();
766766
}
767+
768+
#[cargo_test]
769+
fn multiple_out_dirs() {
770+
// Test for accessing the OUT_DIR of build scripts
771+
let p = project()
772+
.file(
773+
"Cargo.toml",
774+
r#"
775+
cargo-features = ["multiple-build-scripts"]
776+
777+
[package]
778+
name = "foo"
779+
version = "0.1.0"
780+
edition = "2024"
781+
build = ["build1.rs", "build2.rs"]
782+
"#,
783+
)
784+
.file(
785+
"src/main.rs",
786+
r#"
787+
const OUT_DIR: &'static str = env!("OUT_DIR");
788+
const BUILD1_OUT_DIR: &'static str = env!("BUILD1_OUT_DIR");
789+
const BUILD2_OUT_DIR: &'static str = env!("BUILD2_OUT_DIR");
790+
791+
fn main() {
792+
println!("{}", OUT_DIR);
793+
println!("{}", BUILD1_OUT_DIR);
794+
println!("{}", BUILD2_OUT_DIR);
795+
}
796+
"#,
797+
)
798+
.file("build1.rs", "fn main() {}")
799+
.file("build2.rs", "fn main() {}")
800+
.build();
801+
p.cargo("run -v")
802+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
803+
.with_status(101)
804+
.with_stderr_data(str![[r#"
805+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
806+
[RUNNING] `rustc --crate-name build_script_build[..]`
807+
[RUNNING] `rustc --crate-name build_script_build[..]`
808+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build[..]`
809+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build[..]`
810+
[RUNNING] `rustc --crate-name foo --edition=2024 src/main.rs[..]`
811+
[ERROR] environment variable `BUILD1_OUT_DIR` not defined at compile time
812+
--> src/main.rs:3:54
813+
|
814+
3 | const BUILD1_OUT_DIR: &'static str = env!("BUILD1_OUT_DIR");
815+
| ^^^^^^^^^^^^^^^^^^^^^^
816+
|
817+
= [HELP] use `std::env::var("BUILD1_OUT_DIR")` to read the variable at run time
818+
819+
[ERROR] environment variable `BUILD2_OUT_DIR` not defined at compile time
820+
--> src/main.rs:4:54
821+
|
822+
4 | const BUILD2_OUT_DIR: &'static str = env!("BUILD2_OUT_DIR");
823+
| ^^^^^^^^^^^^^^^^^^^^^^
824+
|
825+
= [HELP] use `std::env::var("BUILD2_OUT_DIR")` to read the variable at run time
826+
827+
[ERROR] could not compile `foo` (bin "foo") due to 2 previous errors
828+
829+
Caused by:
830+
process didn't exit successfully: `rustc --crate-name foo --edition=2024 src/main.rs [..]` ([EXIT_STATUS]: 1)
831+
832+
"#]])
833+
.run();
834+
}

0 commit comments

Comments
 (0)