diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 3b21e4f43fd..526d9768a76 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -373,6 +373,8 @@ 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()); + cmd.env("CARGO_PROFILE_NAME", unit.profile.name); + cmd.env("CARGO_FOR_HOST", (unit.target.for_host()).to_string()); cmd } diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index bb200136ac1..9333cde6c4a 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -214,6 +214,8 @@ corresponding environment variable is set to the empty string, `""`. * `CARGO_PKG_LICENSE_FILE` — The license file from the manifest of your package. * `CARGO_CRATE_NAME` — The name of the crate that is currently being compiled. * `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_PROFILE_NAME` - The name of the [cargo profile](profiles.md) used. Can be one of the built-in profiles `dev`, `release`, `test`, `bench` or a custom profile's name. Is not set for `proc-macro` crates. +* `CARGO_FOR_HOST` - Is `true` when building `build.rs` files, `false` for normal compilations and not set for `proc-macro` crates. * `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.) diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index f3b8b16a69b..17eb8135663 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1340,6 +1340,8 @@ fn crate_env_vars() { static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION"); static BIN_NAME: &'static str = env!("CARGO_BIN_NAME"); static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME"); + static PROFILE_NAME: &'static str = env!("CARGO_PROFILE_NAME"); + static FOR_HOST: &'static str = env!("CARGO_FOR_HOST"); fn main() { @@ -1362,6 +1364,9 @@ fn crate_env_vars() { // Verify CARGO_TARGET_TMPDIR isn't set for bins assert!(option_env!("CARGO_TARGET_TMPDIR").is_none()); + + assert_eq!("dev", PROFILE_NAME); + assert_eq!("false", FOR_HOST); } "#, )