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 Jun 24, 2021
1 parent 9e9813a commit 5dbd534
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 @@ -199,17 +199,27 @@ 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())
.env("RUSTC", &bcx.rustc().path)
.env("RUSTDOC", &*bcx.config.rustdoc()?)
.inherit_jobserver(&cx.jobserver);

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 @@ -276,6 +276,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 @@ -115,7 +115,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_LINKER").is_err());
}}
"#,
Expand Down Expand Up @@ -928,7 +930,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 @@ -1034,7 +1038,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 @@ -1158,7 +1164,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 @@ -2249,8 +2257,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 @@ -2271,7 +2280,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 @@ -2316,6 +2330,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 @@ -2337,7 +2352,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 @@ -2406,7 +2426,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 5dbd534

Please sign in to comment.