Skip to content

Commit 6f9d808

Browse files
committed
Auto merge of #8270 - reggaemuffin:8251-binary-name-env-var, r=ehuss
Add environment variables to identify the binary and crate name Closes #8251 This adds `CARGO_BIN_NAME` and `CARGO_CRATE_NAME` to rustc/rustdoc process env. `CARGO_BIN_NAME` is added for binary compilation units, `CARGO_CRATE_NAME` is added for binary and library units. The `build::crate_env_vars` test was updated to test for this. The test is currently only checking behavior for the binary compile unit. Documentation was updated to reflect the added environment variables.
2 parents fb0e392 + 7ad7427 commit 6f9d808

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/cargo/core/compiler/compilation.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,15 @@ impl<'cfg> Compilation<'cfg> {
151151
self.rustc_process.clone()
152152
};
153153

154-
self.fill_env(rustc, &unit.pkg, unit.kind, true)
154+
let cmd = fill_rustc_tool_env(rustc, unit);
155+
self.fill_env(cmd, &unit.pkg, unit.kind, true)
155156
}
156157

157158
/// See `process`.
158159
pub fn rustdoc_process(&self, unit: &Unit) -> CargoResult<ProcessBuilder> {
159-
let mut p = self.fill_env(
160-
process(&*self.config.rustdoc()?),
161-
&unit.pkg,
162-
unit.kind,
163-
true,
164-
)?;
160+
let rustdoc = process(&*self.config.rustdoc()?);
161+
let cmd = fill_rustc_tool_env(rustdoc, unit);
162+
let mut p = self.fill_env(cmd, &unit.pkg, unit.kind, true)?;
165163
if unit.target.edition() != Edition::Edition2015 {
166164
p.arg(format!("--edition={}", unit.target.edition()));
167165
}
@@ -295,6 +293,16 @@ impl<'cfg> Compilation<'cfg> {
295293
}
296294
}
297295

296+
/// Prepares a rustc_tool process with additional environment variables
297+
/// that are only relevant in a context that has a unit
298+
fn fill_rustc_tool_env(mut cmd: ProcessBuilder, unit: &Unit) -> ProcessBuilder {
299+
if unit.target.is_bin() {
300+
cmd.env("CARGO_BIN_NAME", unit.target.name());
301+
}
302+
cmd.env("CARGO_CRATE_NAME", unit.target.crate_name());
303+
cmd
304+
}
305+
298306
fn pre_version_component(v: &Version) -> String {
299307
if v.pre.is_empty() {
300308
return String::new();

src/doc/src/reference/environment-variables.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ let version = env!("CARGO_PKG_VERSION");
184184
* `CARGO_PKG_DESCRIPTION` — The description from the manifest of your package.
185185
* `CARGO_PKG_HOMEPAGE` — The home page from the manifest of your package.
186186
* `CARGO_PKG_REPOSITORY` — The repository from the manifest of your package.
187+
* `CARGO_CRATE_NAME` — The name of the crate that is currently being compiled.
188+
* `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`.
187189
* `OUT_DIR` — If the package has a build script, this is set to the folder where the build
188190
script should place its output. See below for more information.
189191
(Only set during compilation.)

tests/testsuite/build.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,6 +1230,10 @@ fn crate_env_vars() {
12301230
homepage = "https://example.com"
12311231
repository = "https://example.com/repo.git"
12321232
authors = ["wycats@example.com"]
1233+
1234+
[[bin]]
1235+
name = "foo-bar"
1236+
path = "src/main.rs"
12331237
"#,
12341238
)
12351239
.file(
@@ -1248,6 +1252,9 @@ fn crate_env_vars() {
12481252
static HOMEPAGE: &'static str = env!("CARGO_PKG_HOMEPAGE");
12491253
static REPOSITORY: &'static str = env!("CARGO_PKG_REPOSITORY");
12501254
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
1255+
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
1256+
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
1257+
12511258
12521259
fn main() {
12531260
let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
@@ -1256,6 +1263,8 @@ fn crate_env_vars() {
12561263
assert_eq!(s, foo::version());
12571264
println!("{}", s);
12581265
assert_eq!("foo", PKG_NAME);
1266+
assert_eq!("foo-bar", BIN_NAME);
1267+
assert_eq!("foo_bar", CRATE_NAME);
12591268
assert_eq!("https://example.com", HOMEPAGE);
12601269
assert_eq!("https://example.com/repo.git", REPOSITORY);
12611270
assert_eq!("This is foo", DESCRIPTION);
@@ -1284,7 +1293,7 @@ fn crate_env_vars() {
12841293
p.cargo("build -v").run();
12851294

12861295
println!("bin");
1287-
p.process(&p.bin("foo"))
1296+
p.process(&p.bin("foo-bar"))
12881297
.with_stdout("0-5-1 @ alpha.1 in [CWD]")
12891298
.run();
12901299

0 commit comments

Comments
 (0)