From 50a537bf93b8f34240777eeed5964a1c9b478d87 Mon Sep 17 00:00:00 2001 From: ReggaeMuffin <644950+reggaemuffin@users.noreply.github.com> Date: Sat, 23 May 2020 16:23:49 +0100 Subject: [PATCH 1/3] Add CARGO_BIN_NAME and CARGO_CRATE_NAME to rustc/rustdoc env CARGO_BIN_NAME is added for binary compilation units, CARGO_CRATE_NAME is added for binary and library units. The `crate_env_vars` test was updated to test for this. The test is currently only checking behavior for the binary compile unit. --- src/cargo/core/compiler/compilation.rs | 22 +++++++++++++++------- tests/testsuite/build.rs | 11 ++++++++++- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/cargo/core/compiler/compilation.rs b/src/cargo/core/compiler/compilation.rs index 7324618ff45..b8fb670c299 100644 --- a/src/cargo/core/compiler/compilation.rs +++ b/src/cargo/core/compiler/compilation.rs @@ -151,17 +151,15 @@ impl<'cfg> Compilation<'cfg> { self.rustc_process.clone() }; - self.fill_env(rustc, &unit.pkg, unit.kind, true) + let cmd = fill_rustc_tool_env(rustc, unit); + self.fill_env(cmd, &unit.pkg, unit.kind, true) } /// See `process`. pub fn rustdoc_process(&self, unit: &Unit) -> CargoResult { - let mut p = self.fill_env( - process(&*self.config.rustdoc()?), - &unit.pkg, - unit.kind, - true, - )?; + let rustdoc = process(&*self.config.rustdoc()?); + let cmd = fill_rustc_tool_env(rustdoc, unit); + let mut p = self.fill_env(cmd, &unit.pkg, unit.kind, true)?; if unit.target.edition() != Edition::Edition2015 { p.arg(format!("--edition={}", unit.target.edition())); } @@ -295,6 +293,16 @@ 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() { + cmd.env("CARGO_BIN_NAME", unit.target.name()); + } + cmd.env("CARGO_CRATE_NAME", unit.target.crate_name()); + cmd +} + fn pre_version_component(v: &Version) -> String { if v.pre.is_empty() { return String::new(); diff --git a/tests/testsuite/build.rs b/tests/testsuite/build.rs index 6605e1f5366..f3b5e401d98 100644 --- a/tests/testsuite/build.rs +++ b/tests/testsuite/build.rs @@ -1230,6 +1230,10 @@ fn crate_env_vars() { homepage = "https://example.com" repository = "https://example.com/repo.git" authors = ["wycats@example.com"] + + [[bin]] + name = "foo-bar" + path = "src/main.rs" "#, ) .file( @@ -1248,6 +1252,9 @@ fn crate_env_vars() { static HOMEPAGE: &'static str = env!("CARGO_PKG_HOMEPAGE"); static REPOSITORY: &'static str = env!("CARGO_PKG_REPOSITORY"); 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"); + fn main() { let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR, @@ -1256,6 +1263,8 @@ fn crate_env_vars() { assert_eq!(s, foo::version()); println!("{}", s); assert_eq!("foo", PKG_NAME); + assert_eq!("foo-bar", BIN_NAME); + assert_eq!("foo_bar", CRATE_NAME); assert_eq!("https://example.com", HOMEPAGE); assert_eq!("https://example.com/repo.git", REPOSITORY); assert_eq!("This is foo", DESCRIPTION); @@ -1284,7 +1293,7 @@ fn crate_env_vars() { p.cargo("build -v").run(); println!("bin"); - p.process(&p.bin("foo")) + p.process(&p.bin("foo-bar")) .with_stdout("0-5-1 @ alpha.1 in [CWD]") .run(); From 7cc6d95e4103fbd954c9d36183e0fa0d79312326 Mon Sep 17 00:00:00 2001 From: ReggaeMuffin <644950+reggaemuffin@users.noreply.github.com> Date: Sat, 23 May 2020 16:45:37 +0100 Subject: [PATCH 2/3] Add documentation for both environment variables --- src/doc/src/reference/environment-variables.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index bb03e5ef454..217f3ef7c6b 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -184,6 +184,8 @@ let version = env!("CARGO_PKG_VERSION"); * `CARGO_PKG_DESCRIPTION` — The description from the manifest of your package. * `CARGO_PKG_HOMEPAGE` — The home page from the manifest of your package. * `CARGO_PKG_REPOSITORY` — The repository 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). * `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.) From 7ad7427ced99dadd76295963615dc9d903462802 Mon Sep 17 00:00:00 2001 From: Marvin Hofmann <644950+reggaemuffin@users.noreply.github.com> Date: Sun, 31 May 2020 21:01:03 +0100 Subject: [PATCH 3/3] Update documentation as per review --- src/doc/src/reference/environment-variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/doc/src/reference/environment-variables.md b/src/doc/src/reference/environment-variables.md index 217f3ef7c6b..f1c96f1ac39 100644 --- a/src/doc/src/reference/environment-variables.md +++ b/src/doc/src/reference/environment-variables.md @@ -185,7 +185,7 @@ let version = env!("CARGO_PKG_VERSION"); * `CARGO_PKG_HOMEPAGE` — The home page from the manifest of your package. * `CARGO_PKG_REPOSITORY` — The repository 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). +* `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`. * `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.)