From 0fda7acf487993999531286784711730e9e2b627 Mon Sep 17 00:00:00 2001 From: Tobias Rapp Date: Mon, 13 Feb 2023 14:25:35 +0100 Subject: [PATCH] Set CARGO_BIN_NAME environment variable also for binary examples Extend the existing CARGO_BIN_NAME environment variable to be set when building binary example targets, additional to "normal" binary targets. Closes #11689. --- src/cargo/core/compiler/compilation.rs | 2 +- .../src/reference/environment-variables.md | 6 +++++- tests/testsuite/build.rs | 20 +++++++++++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 6fafb46fa63..1347fb862a2 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -372,7 +372,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 { - if unit.target.is_bin() { + if unit.target.is_executable() { let name = unit .target .binary_filename() diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 1675b3770c4..f8110a0cfaa 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -232,7 +232,9 @@ corresponding environment variable is set to the empty string, `""`. Note that this is the minimum Rust version supported by the package, not the current Rust version. * `CARGO_CRATE_NAME` — The name of the crate that is currently being compiled. It is the name of the [Cargo target] with `-` converted to `_`, such as the name of the library, binary, example, integration test, or benchmark. -* `CARGO_BIN_NAME` — The name of the binary that is currently being compiled (if it is a binary). This name does not include any file extension, such as `.exe`. +* `CARGO_BIN_NAME` — The name of the binary that is currently being compiled. + Only set for [binaries] or binary [examples]. This name does not include any + file extension, such as `.exe`. * `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.) @@ -256,6 +258,8 @@ corresponding environment variable is set to the empty string, `""`. manage its content in any way, this is the responsibility of the test code. [Cargo target]: cargo-targets.md +[binaries]: cargo-targets.md#binaries +[examples]: cargo-targets.md#examples [integration test]: cargo-targets.md#integration-tests [`env` macro]: ../../std/macro.env.html diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 1b68ab87c72..ca7f669b2e1 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1466,6 +1466,23 @@ fn crate_env_vars() { } "#, ) + .file( + "examples/ex-env-vars.rs", + r#" + static PKG_NAME: &'static str = env!("CARGO_PKG_NAME"); + static BIN_NAME: &'static str = env!("CARGO_BIN_NAME"); + static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME"); + + fn main() { + assert_eq!("foo", PKG_NAME); + assert_eq!("ex-env-vars", BIN_NAME); + assert_eq!("ex_env_vars", CRATE_NAME); + + // Verify CARGO_TARGET_TMPDIR isn't set for examples + assert!(option_env!("CARGO_TARGET_TMPDIR").is_none()); + } + "#, + ) .file( "tests/env.rs", r#" @@ -1503,6 +1520,9 @@ fn crate_env_vars() { .with_stdout("0-5-1 @ alpha.1 in [CWD]") .run(); + println!("example"); + p.cargo("run --example ex-env-vars -v").run(); + println!("test"); p.cargo("test -v").run();