Skip to content

Make profile name and for_host target available as environment variables #10387

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

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
2 changes: 2 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.)
Expand Down
5 changes: 5 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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);
}
"#,
)
Expand Down