Skip to content

Commit c9e3081

Browse files
committed
Add environment variables for accessing the OUT_DIR
1 parent 6c8bc08 commit c9e3081

File tree

2 files changed

+114
-10
lines changed

2 files changed

+114
-10
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,10 +1706,15 @@ fn build_deps_args(
17061706

17071707
for dep in deps {
17081708
if dep.unit.mode.is_run_custom_build() {
1709-
cmd.env(
1710-
"OUT_DIR",
1711-
&build_runner.files().build_script_out_dir(&dep.unit),
1712-
);
1709+
let out_dir = &build_runner.files().build_script_out_dir(&dep.unit);
1710+
let mut out_dir_name = dep.unit.target.name().to_owned();
1711+
out_dir_name = out_dir_name
1712+
.strip_prefix("build-script-")
1713+
.unwrap_or(&out_dir_name)
1714+
.to_string();
1715+
out_dir_name.push_str("_OUT_DIR");
1716+
cmd.env("OUT_DIR", &out_dir);
1717+
cmd.env(&out_dir_name, &out_dir);
17131718
}
17141719
}
17151720

tests/testsuite/build_scripts_multiple.rs

Lines changed: 105 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ fn build_script_with_conflicting_out_dirs() {
549549
build = ["build1.rs", "build2.rs"]
550550
"#,
551551
)
552-
// OUT_DIR is set to the lexicographically largest build script's OUT_DIR by default
552+
// By default, OUT_DIR is set to that of the last build script in the array
553553
.file(
554554
"src/main.rs",
555555
r#"
@@ -628,7 +628,7 @@ fn build_script_with_conflicts_reverse_sorted() {
628628
build = ["build2.rs", "build1.rs"]
629629
"#,
630630
)
631-
// OUT_DIR is set to the lexicographically largest build script's OUT_DIR by default
631+
// By default, OUT_DIR is set to that of the last build script in the array
632632
.file(
633633
"src/main.rs",
634634
r#"
@@ -785,9 +785,11 @@ fn multiple_out_dirs() {
785785
.file(
786786
"src/main.rs",
787787
r#"
788-
include!(concat!(env!("OUT_DIR"), "/foo.rs"));
788+
include!(concat!(env!("build1_OUT_DIR"), "/foo.rs"));
789+
include!(concat!(env!("build2_OUT_DIR"), "/foo.rs"));
789790
fn main() {
790-
println!("{}", message());
791+
println!("{}", message1());
792+
println!("{}", message2());
791793
}
792794
"#,
793795
)
@@ -803,7 +805,7 @@ fn multiple_out_dirs() {
803805
let dest_path = Path::new(&out_dir).join("foo.rs");
804806
fs::write(
805807
&dest_path,
806-
"pub fn message() -> &'static str {
808+
"pub fn message1() -> &'static str {
807809
\"Hello, from Build Script 1!\"
808810
}
809811
"
@@ -822,7 +824,7 @@ fn multiple_out_dirs() {
822824
let dest_path = Path::new(&out_dir).join("foo.rs");
823825
fs::write(
824826
&dest_path,
825-
"pub fn message() -> &'static str {
827+
"pub fn message2() -> &'static str {
826828
\"Hello, from Build Script 2!\"
827829
}
828830
"
@@ -835,8 +837,105 @@ fn multiple_out_dirs() {
835837
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
836838
.with_status(0)
837839
.with_stdout_data(str![[r#"
840+
Hello, from Build Script 1!
838841
Hello, from Build Script 2!
839842
840843
"#]])
841844
.run();
842845
}
846+
847+
#[cargo_test]
848+
fn build_script_with_fn_conflicts() {
849+
// In this, multiple scripts have duplicated/conflicting declarations of same function
850+
851+
let p = project()
852+
.file(
853+
"Cargo.toml",
854+
r#"
855+
cargo-features = ["multiple-build-scripts"]
856+
857+
[package]
858+
name = "foo"
859+
version = "0.1.0"
860+
edition = "2024"
861+
862+
build = ["build1.rs", "build2.rs"]
863+
"#,
864+
)
865+
.file(
866+
"src/main.rs",
867+
r#"
868+
include!(concat!(env!("build1_OUT_DIR"), "/foo.rs"));
869+
include!(concat!(env!("build2_OUT_DIR"), "/foo.rs"));
870+
fn main() {
871+
println!("{}", message());
872+
}
873+
"#,
874+
)
875+
.file(
876+
"build1.rs",
877+
r#"
878+
use std::env;
879+
use std::fs;
880+
use std::path::Path;
881+
882+
fn main() {
883+
let out_dir = env::var_os("OUT_DIR").unwrap();
884+
let dest_path = Path::new(&out_dir).join("foo.rs");
885+
fs::write(
886+
&dest_path,
887+
"pub fn message() -> &'static str {
888+
\"Hello, from Build Script 1!\"
889+
}
890+
"
891+
).unwrap();
892+
}"#,
893+
)
894+
.file(
895+
"build2.rs",
896+
r#"
897+
use std::env;
898+
use std::fs;
899+
use std::path::Path;
900+
901+
fn main() {
902+
let out_dir = env::var_os("OUT_DIR").unwrap();
903+
let dest_path = Path::new(&out_dir).join("foo.rs");
904+
fs::write(
905+
&dest_path,
906+
"pub fn message() -> &'static str {
907+
\"Hello, from Build Script 2!\"
908+
}
909+
"
910+
).unwrap();
911+
}"#,
912+
)
913+
.build();
914+
915+
p.cargo("check -v")
916+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
917+
.with_status(101)
918+
.with_stderr_data(str![[r#"
919+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
920+
...
921+
--> [ROOT]/foo/target/debug/build/foo-[HASH]/out/foo.rs:1:1
922+
|
923+
1 | pub fn message() -> &'static str {
924+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `message` redefined here
925+
|
926+
::: [ROOT]/foo/target/debug/build/foo-[HASH]/out/foo.rs:1:1
927+
|
928+
1 | pub fn message() -> &'static str {
929+
| -------------------------------- previous definition of the value `message` here
930+
|
931+
= [NOTE] `message` must be defined only once in the value namespace of this module
932+
933+
For more information about this error, try `rustc --explain E0428`.
934+
[ERROR] could not compile `foo` (bin "foo") due to 1 previous error
935+
936+
Caused by:
937+
process didn't exit successfully: `rustc --crate-name foo --edition=2024 src/main.rs [..] ([EXIT_STATUS]: 1)
938+
939+
"#]])
940+
.run();
941+
}

0 commit comments

Comments
 (0)