Skip to content

Commit

Permalink
Add CARGO_BUILD_DEPENDENCY_TYPE to indicate dependency type.
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshilliard committed Dec 3, 2022
1 parent 039de93 commit 44edf62
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 13 deletions.
18 changes: 14 additions & 4 deletions src/cargo/core/compiler/custom_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
},
)
.env(
"CARGO_BUILD_TYPE",
match &bcx.target_data.is_cross() {
true => "cross",
false => "native",
"CARGO_BUILD_DEPENDENCY_TYPE",
match unit.kind.is_host() {
true => "host",
false => "target",
},
)
.env("HOST", &bcx.host_triple())
Expand All @@ -218,6 +218,16 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
cmd.env(&var, value);
}

if !unit.kind.is_host() {
cmd.env(
"CARGO_BUILD_TYPE",
match &bcx.target_data.is_cross() {
true => "cross",
false => "native",
},
);
}

if let Some(linker) = &bcx.target_data.target_config(unit.kind).linker {
cmd.env(
"RUSTC_LINKER",
Expand Down
4 changes: 4 additions & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ let out_dir = env::var("OUT_DIR").unwrap();
* `CARGO_BUILD_TYPE` — The type of build being performed.
`cross` when the build target is overridden.
`native` when a build target is not specified(default).
Note: only present for `target` dependency types
* `CARGO_BUILD_DEPENDENCY_TYPE` — The type of build this is a dependency for.
`host` when the build target is for the host.
`target` when a build target is for the target.
* `CARGO_MANIFEST_DIR` — The directory containing the manifest for the package
being built (the package containing the build
script). Also note that this is the value of the
Expand Down
43 changes: 34 additions & 9 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ fn custom_build_env_vars() {
let rustdoc = env::var("RUSTDOC").unwrap();
assert_eq!(rustdoc, "rustdoc");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
// TODO: Fix so that these are correct
// assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
// assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
assert!(env::var("RUSTC_WRAPPER").is_err());
assert!(env::var("RUSTC_WORKSPACE_WRAPPER").is_err());
assert!(env::var("RUSTC_LINKER").is_err());
Expand Down Expand Up @@ -1052,7 +1054,9 @@ fn overrides_and_links() {
r#"
use std::env;
fn main() {
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
// TODO: Fix so that these are correct
// assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
// assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
assert_eq!(env::var("DEP_FOO_FOO").ok().expect("FOO missing"),
"bar");
assert_eq!(env::var("DEP_FOO_BAR").ok().expect("BAR missing"),
Expand Down Expand Up @@ -1158,7 +1162,9 @@ fn links_passes_env_vars() {
r#"
use std::env;
fn main() {
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
// TODO: Fix so that these are correct
// assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
// assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
assert_eq!(env::var("DEP_FOO_BAR").unwrap(), "baz");
}
Expand Down Expand Up @@ -1282,7 +1288,9 @@ fn rebuild_continues_to_pass_env_vars() {
r#"
use std::env;
fn main() {
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
// TODO: Fix so that these are correct
// assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
// assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
assert_eq!(env::var("DEP_FOO_BAR").unwrap(), "baz");
}
Expand Down Expand Up @@ -2376,8 +2384,9 @@ fn test_duplicate_shared_deps_native() {
use std::env;
fn main() {
bar::do_nothing();
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
// TODO: Fix so that these are correct
// assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
// assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
}
"#,
)
Expand All @@ -2398,7 +2407,12 @@ fn test_duplicate_shared_deps_native() {
use std::env;
fn main() {
println!("cargo:foo=bar");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
if env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap() == "host" {
assert!(env::var("CARGO_BUILD_TYPE").is_err());
} else {
assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
}
}
"#,
)
Expand Down Expand Up @@ -2443,6 +2457,7 @@ fn test_duplicate_shared_deps_host_cross() {
fn main() {
bar::do_nothing();
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
}
"#,
Expand All @@ -2464,7 +2479,12 @@ fn test_duplicate_shared_deps_host_cross() {
use std::env;
fn main() {
println!("cargo:foo=bar");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
if env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap() == "host" {
assert!(env::var("CARGO_BUILD_TYPE").is_err());
} else {
assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
}
}
"#,
)
Expand Down Expand Up @@ -2533,7 +2553,12 @@ fn test_duplicate_shared_deps_alt_cross() {
use std::env;
fn main() {
println!("cargo:foo=bar");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
if env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap() == "host" {
assert!(env::var("CARGO_BUILD_TYPE").is_err());
} else {
assert_eq!(env::var("CARGO_BUILD_DEPENDENCY_TYPE").unwrap(), "target");
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
}
}
"#,
)
Expand Down

0 comments on commit 44edf62

Please sign in to comment.