Skip to content

Commit afebe84

Browse files
committed
Add env variable to indicate the build type.
Build scripts in some cases need to know if the build is a cross build or a native build. Set the CARGO_BUILD_TYPE env variable for build scripts so that they can properly determine the build type being performed.
1 parent 4c27c96 commit afebe84

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ pub struct RustcTargetData<'cfg> {
673673
/// empty if the `--target` flag is not passed.
674674
target_config: HashMap<CompileTarget, TargetConfig>,
675675
target_info: HashMap<CompileTarget, TargetInfo>,
676+
677+
/// True if a `--target` flag is passed.
678+
is_cross: bool,
676679
}
677680

678681
impl<'cfg> RustcTargetData<'cfg> {
@@ -697,10 +700,13 @@ impl<'cfg> RustcTargetData<'cfg> {
697700
// `--target` flag is not specified. Since the unit_dependency code
698701
// needs access to the target config data, create a copy so that it
699702
// can be found. See `rebuild_unit_graph_shared` for why this is done.
700-
if requested_kinds.iter().any(CompileKind::is_host) {
703+
let is_cross = if requested_kinds.iter().any(CompileKind::is_host) {
701704
let ct = CompileTarget::new(&rustc.host)?;
702705
target_info.insert(ct, host_info.clone());
703706
target_config.insert(ct, config.target_cfg_triple(&rustc.host)?);
707+
false
708+
} else {
709+
true
704710
};
705711

706712
let mut res = RustcTargetData {
@@ -711,6 +717,7 @@ impl<'cfg> RustcTargetData<'cfg> {
711717
host_info,
712718
target_config,
713719
target_info,
720+
is_cross,
714721
};
715722

716723
// Get all kinds we currently know about.
@@ -798,6 +805,10 @@ impl<'cfg> RustcTargetData<'cfg> {
798805
pub fn script_override(&self, lib_name: &str, kind: CompileKind) -> Option<&BuildOutput> {
799806
self.target_config(kind).links_overrides.get(lib_name)
800807
}
808+
809+
pub fn is_cross(&self) -> bool {
810+
self.is_cross
811+
}
801812
}
802813

803814
/// Structure used to deal with Rustdoc fingerprinting

src/cargo/core/compiler/custom_build.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,13 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
198198
ProfileRoot::Debug => "debug",
199199
},
200200
)
201+
.env(
202+
"CARGO_BUILD_TYPE",
203+
match &bcx.target_data.is_cross() {
204+
true => "cross",
205+
false => "native",
206+
},
207+
)
201208
.env("HOST", &bcx.host_triple())
202209
.env("RUSTC", &bcx.rustc().path)
203210
.env("RUSTDOC", &*bcx.config.rustdoc()?)

src/doc/src/reference/environment-variables.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ let out_dir = env::var("OUT_DIR").unwrap();
273273
`out_dir` will now contain the value of `OUT_DIR`.
274274

275275
* `CARGO` — Path to the `cargo` binary performing the build.
276+
* `CARGO_BUILD_TYPE` — The type of build being performed.
277+
`cross` when the build target is overridden.
278+
`native` when a build target is not specified(default).
276279
* `CARGO_MANIFEST_DIR` — The directory containing the manifest for the package
277280
being built (the package containing the build
278281
script). Also note that this is the value of the

tests/testsuite/build_script.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn custom_build_env_vars() {
115115
let rustdoc = env::var("RUSTDOC").unwrap();
116116
assert_eq!(rustdoc, "rustdoc");
117117
118+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
118119
assert!(env::var("RUSTC_LINKER").is_err());
119120
}}
120121
"#,
@@ -153,6 +154,7 @@ fn custom_build_env_var_rustc_linker() {
153154
use std::env;
154155
155156
fn main() {
157+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
156158
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
157159
}
158160
"#,
@@ -226,6 +228,7 @@ fn custom_build_env_var_rustc_linker_host_target() {
226228
use std::env;
227229
228230
fn main() {
231+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
229232
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
230233
}
231234
"#,
@@ -263,6 +266,7 @@ fn custom_build_env_var_rustc_linker_host_target_env() {
263266
use std::env;
264267
265268
fn main() {
269+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
266270
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
267271
}
268272
"#,
@@ -487,6 +491,7 @@ fn custom_build_env_var_rustc_linker_cross_arch_host() {
487491
use std::env;
488492
489493
fn main() {
494+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
490495
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker"));
491496
}
492497
"#,
@@ -923,6 +928,7 @@ fn overrides_and_links() {
923928
r#"
924929
use std::env;
925930
fn main() {
931+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
926932
assert_eq!(env::var("DEP_FOO_FOO").ok().expect("FOO missing"),
927933
"bar");
928934
assert_eq!(env::var("DEP_FOO_BAR").ok().expect("BAR missing"),
@@ -1028,6 +1034,7 @@ fn links_passes_env_vars() {
10281034
r#"
10291035
use std::env;
10301036
fn main() {
1037+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
10311038
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
10321039
assert_eq!(env::var("DEP_FOO_BAR").unwrap(), "baz");
10331040
}
@@ -1151,6 +1158,7 @@ fn rebuild_continues_to_pass_env_vars() {
11511158
r#"
11521159
use std::env;
11531160
fn main() {
1161+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
11541162
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
11551163
assert_eq!(env::var("DEP_FOO_BAR").unwrap(), "baz");
11561164
}

0 commit comments

Comments
 (0)