Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set the TARGET env variable when running rustc #12752

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl<'cfg> Compilation<'cfg> {
self.rustc_process.clone()
};

let cmd = fill_rustc_tool_env(rustc, unit);
let cmd = fill_rustc_tool_env(rustc, unit, &self.host);
self.fill_env(cmd, &unit.pkg, None, unit.kind, true)
}

Expand All @@ -194,7 +194,7 @@ impl<'cfg> Compilation<'cfg> {
script_meta: Option<Metadata>,
) -> CargoResult<ProcessBuilder> {
let rustdoc = ProcessBuilder::new(&*self.config.rustdoc()?);
let cmd = fill_rustc_tool_env(rustdoc, unit);
let cmd = fill_rustc_tool_env(rustdoc, unit, &self.host);
let mut cmd = self.fill_env(cmd, &unit.pkg, script_meta, unit.kind, true)?;
cmd.retry_with_argfile(true);
unit.target.edition().cmd_edition_arg(&mut cmd);
Expand Down Expand Up @@ -373,7 +373,7 @@ impl<'cfg> Compilation<'cfg> {

/// Prepares a rustc_tool process with additional environment variables
/// that are only relevant in a context that has a unit
fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit, host_triple: &str) -> ProcessBuilder {
if unit.target.is_executable() {
let name = unit
.target
Expand All @@ -383,6 +383,11 @@ fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
cmd.env("CARGO_BIN_NAME", name);
}
cmd.env("CARGO_CRATE_NAME", unit.target.crate_name());
let target_triple = match &unit.kind {
CompileKind::Host => host_triple,
CompileKind::Target(t) => t.short_name(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use this instead so that local target JSON files are respected?

Suggested change
CompileKind::Target(t) => t.short_name(),
CompileKind::Target(t) => t.rustc_target(),

};
cmd.env("TARGET", target_triple);
cmd
}

Expand Down
3 changes: 3 additions & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,9 @@ corresponding environment variable is set to the empty string, `""`.
* `OUT_DIR` --- If the package has a build script, this is set to the folder where the build
script should place its output. See below for more information.
(Only set during compilation.)
* `TARGET` --- the target triple that is being compiled for. Native code should be
compiled for this triple. See the [Target Triple] description
for more information. (Only set during compilation.)
* `CARGO_BIN_EXE_<name>` --- The absolute path to a binary target's executable.
This is only set when building an [integration test] or benchmark. This may
be used with the [`env` macro] to find the executable to run for testing
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5101,6 +5101,7 @@ fn duplicate_script_with_extra_env() {
compile_error!{"expected mycfg set"}
// Compile-time assertion.
assert_eq!(env!("CRATE_TARGET"), "{target}");
assert_eq!(env!("TARGET"), "{target}");
// Run-time assertion.
assert_eq!(std::env::var("CRATE_TARGET").unwrap(), "{target}");
}
Expand Down
10 changes: 7 additions & 3 deletions tests/testsuite/cross_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn simple_cross() {
assert_eq!(std::env::var("TARGET").unwrap(), "{}");
}}
"#,
cross_compile::alternate()
cross_compile::alternate(),
),
)
.file(
Expand All @@ -40,9 +40,11 @@ fn simple_cross() {
use std::env;
fn main() {{
assert_eq!(env::consts::ARCH, "{}");
assert_eq!(env!("TARGET"), "{}");
}}
"#,
cross_compile::alternate_arch()
cross_compile::alternate_arch(),
cross_compile::alternate()
),
)
.build();
Expand Down Expand Up @@ -101,9 +103,11 @@ fn simple_cross_config() {
use std::env;
fn main() {{
assert_eq!(env::consts::ARCH, "{}");
assert_eq!(env!("TARGET"), "{}");
}}
"#,
cross_compile::alternate_arch()
cross_compile::alternate_arch(),
cross_compile::alternate()
),
)
.build();
Expand Down