Skip to content

Commit ba8b1ad

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 7bdb969 commit ba8b1ad

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
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
@@ -816,6 +816,9 @@ pub struct RustcTargetData<'cfg> {
816816
target_config: HashMap<CompileTarget, TargetConfig>,
817817
/// Information about the target platform that we're building for.
818818
target_info: HashMap<CompileTarget, TargetInfo>,
819+
820+
/// True if a `--target` flag is passed.
821+
is_cross: bool,
819822
}
820823

821824
impl<'cfg> RustcTargetData<'cfg> {
@@ -840,10 +843,13 @@ impl<'cfg> RustcTargetData<'cfg> {
840843
// `--target` flag is not specified. Since the unit_dependency code
841844
// needs access to the target config data, create a copy so that it
842845
// can be found. See `rebuild_unit_graph_shared` for why this is done.
843-
if requested_kinds.iter().any(CompileKind::is_host) {
846+
let is_cross = if requested_kinds.iter().any(CompileKind::is_host) {
844847
let ct = CompileTarget::new(&rustc.host)?;
845848
target_info.insert(ct, host_info.clone());
846849
target_config.insert(ct, config.target_cfg_triple(&rustc.host)?);
850+
false
851+
} else {
852+
true
847853
};
848854

849855
let mut res = RustcTargetData {
@@ -854,6 +860,7 @@ impl<'cfg> RustcTargetData<'cfg> {
854860
host_info,
855861
target_config,
856862
target_info,
863+
is_cross,
857864
};
858865

859866
// Get all kinds we currently know about.
@@ -954,6 +961,10 @@ impl<'cfg> RustcTargetData<'cfg> {
954961
pub fn script_override(&self, lib_name: &str, kind: CompileKind) -> Option<&BuildOutput> {
955962
self.target_config(kind).links_overrides.get(lib_name)
956963
}
964+
965+
pub fn is_cross(&self) -> bool {
966+
self.is_cross
967+
}
957968
}
958969

959970
/// 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
@@ -201,6 +201,13 @@ fn build_work(cx: &mut Context<'_, '_>, unit: &Unit) -> CargoResult<Job> {
201201
ProfileRoot::Debug => "debug",
202202
},
203203
)
204+
.env(
205+
"CARGO_BUILD_TYPE",
206+
match &bcx.target_data.is_cross() {
207+
true => "cross",
208+
false => "native",
209+
},
210+
)
204211
.env("HOST", &bcx.host_triple())
205212
.env("RUSTC", &bcx.rustc().path)
206213
.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
@@ -299,6 +299,9 @@ let out_dir = env::var("OUT_DIR").unwrap();
299299
`out_dir` will now contain the value of `OUT_DIR`.
300300

301301
* `CARGO` — Path to the `cargo` binary performing the build.
302+
* `CARGO_BUILD_TYPE` — The type of build being performed.
303+
`cross` when the build target is overridden.
304+
`native` when a build target is not specified(default).
302305
* `CARGO_MANIFEST_DIR` — The directory containing the manifest for the package
303306
being built (the package containing the build
304307
script). Also note that this is the value of the

tests/testsuite/build_script.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ fn custom_build_env_vars() {
129129
let rustdoc = env::var("RUSTDOC").unwrap();
130130
assert_eq!(rustdoc, "rustdoc");
131131
132+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
132133
assert!(env::var("RUSTC_WRAPPER").is_err());
133134
assert!(env::var("RUSTC_WORKSPACE_WRAPPER").is_err());
134-
135135
assert!(env::var("RUSTC_LINKER").is_err());
136136
137137
assert!(env::var("RUSTFLAGS").is_err());
@@ -348,6 +348,7 @@ fn custom_build_env_var_rustc_linker() {
348348
use std::env;
349349
350350
fn main() {
351+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
351352
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
352353
}
353354
"#,
@@ -412,6 +413,7 @@ fn custom_build_env_var_rustc_linker_host_target() {
412413
use std::env;
413414
414415
fn main() {
416+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
415417
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
416418
}
417419
"#,
@@ -447,6 +449,7 @@ fn custom_build_env_var_rustc_linker_host_target_env() {
447449
use std::env;
448450
449451
fn main() {
452+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
450453
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/linker"));
451454
}
452455
"#,
@@ -624,6 +627,7 @@ fn custom_build_env_var_rustc_linker_cross_arch_host() {
624627
use std::env;
625628
626629
fn main() {
630+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "cross");
627631
assert!(env::var("RUSTC_LINKER").unwrap().ends_with("/path/to/target/linker"));
628632
}
629633
"#,
@@ -1048,6 +1052,7 @@ fn overrides_and_links() {
10481052
r#"
10491053
use std::env;
10501054
fn main() {
1055+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
10511056
assert_eq!(env::var("DEP_FOO_FOO").ok().expect("FOO missing"),
10521057
"bar");
10531058
assert_eq!(env::var("DEP_FOO_BAR").ok().expect("BAR missing"),
@@ -1153,6 +1158,7 @@ fn links_passes_env_vars() {
11531158
r#"
11541159
use std::env;
11551160
fn main() {
1161+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
11561162
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
11571163
assert_eq!(env::var("DEP_FOO_BAR").unwrap(), "baz");
11581164
}
@@ -1276,6 +1282,7 @@ fn rebuild_continues_to_pass_env_vars() {
12761282
r#"
12771283
use std::env;
12781284
fn main() {
1285+
assert_eq!(env::var("CARGO_BUILD_TYPE").unwrap(), "native");
12791286
assert_eq!(env::var("DEP_FOO_FOO").unwrap(), "bar");
12801287
assert_eq!(env::var("DEP_FOO_BAR").unwrap(), "baz");
12811288
}

0 commit comments

Comments
 (0)