Skip to content

Commit

Permalink
Set the TARGET env variable when running rustc
Browse files Browse the repository at this point in the history
The goal is to be able to do things differently in a proc macro
depending on the target platform. It is already defined for build script
and it would help to have it for macros as well.

Closes: #10714
  • Loading branch information
ogoffart committed Sep 29, 2023
1 parent c031b0c commit f474a5d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
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(),
};
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

0 comments on commit f474a5d

Please sign in to comment.