Skip to content
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

Add environment variables to identify the binary and crate name #8270

Merged
merged 4 commits into from
Jun 8, 2020

Conversation

marvin-bitterlich
Copy link
Contributor

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.

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.
@rust-highfive
Copy link

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ehuss (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 23, 2020
…-env-var

This fixes the nightly breakages of CI
@@ -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).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should clarify that on Windows the name does not contain the .exe extension?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding "This name does not include any file extension, such as .exe." would work.

@ehuss ehuss added the T-cargo Team: Cargo label May 26, 2020
@ehuss ehuss changed the title Add environment variables to identify the binary nd crate name Add environment variables to identify the binary and crate name May 26, 2020
@ehuss
Copy link
Contributor

ehuss commented May 26, 2020

Thanks! This looks good to me. Just want to get final OK from the team:

@rfcbot fcp merge

@rfcbot
Copy link
Collaborator

rfcbot commented May 26, 2020

Team member @ehuss has proposed to merge this. The next step is review by the rest of the tagged team members:

No concerns currently listed.

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period An FCP proposal has started, but not yet signed off. disposition-merge FCP with intent to merge labels May 26, 2020
@joshtriplett
Copy link
Member

Looks great, thank you!

@rfcbot rfcbot added final-comment-period FCP — a period for last comments before action is taken and removed proposed-final-comment-period An FCP proposal has started, but not yet signed off. labels May 26, 2020
@rfcbot
Copy link
Collaborator

rfcbot commented May 26, 2020

🔔 This is now entering its final comment period, as per the review above. 🔔

@alexcrichton
Copy link
Member

One possible alternative to BIN_NAME might be BIN_STEM since "filestem" is typically already known to not include file extensions.

@marvin-bitterlich
Copy link
Contributor Author

One possible alternative to BIN_NAME might be BIN_STEM since "filestem" is typically already known to not include file extensions.

Happy to amend the pr if we have a consensus on the name to go forward with. I personally find filestem a rather unknown word that has a lot in common with filesystem, so while technically being more precise it might be more confusing for the average user, so I would prefer clarifying in the docs and leave it as bin.

@ehuss
Copy link
Contributor

ehuss commented May 29, 2020

I don't have a strong opinion one way or the other. Unless @alexcrichton wants it to change, I would just leave it.

@alexcrichton
Copy link
Member

Nah yeah that's a good point, I think just clarifying that the name doesn't include the extension should do the trick!

@joshtriplett
Copy link
Member

Agreed. Please update that one line in the documentation; I supplied some text that should work.

@joshtriplett
Copy link
Member

👍

@rfcbot rfcbot added finished-final-comment-period FCP complete and removed final-comment-period FCP — a period for last comments before action is taken labels Jun 5, 2020
@rfcbot
Copy link
Collaborator

rfcbot commented Jun 5, 2020

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@alexcrichton
Copy link
Member

@bors: r=ehuss

@bors
Copy link
Contributor

bors commented Jun 8, 2020

📌 Commit 7ad7427 has been approved by ehuss

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 8, 2020
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jun 8, 2020
@bors
Copy link
Contributor

bors commented Jun 8, 2020

⌛ Testing commit 7ad7427 with merge 6f9d808...

@bors
Copy link
Contributor

bors commented Jun 8, 2020

☀️ Test successful - checks-azure
Approved by: ehuss
Pushing 6f9d808 to master...

@bors bors merged commit 6f9d808 into rust-lang:master Jun 8, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 11, 2020
Update cargo

15 commits in 40ebd52206e25c7a576ee42c137cc06a745a167a..1ec223effbbbf9fddd3453cdcae3a96a967608eb
2020-06-01 22:35:00 +0000 to 2020-06-09 20:03:14 +0000
- Default values for `readme` if not specified (rust-lang/cargo#8277)
- Fix tree completions. (rust-lang/cargo#8342)
- Support `{prefix}` and `{lowerprefix}` markers in `config.json` `dl` key (rust-lang/cargo#8267)
- Add environment variables to identify the binary and crate name (rust-lang/cargo#8270)
- Bump to 0.47.0, update changelog (rust-lang/cargo#8336)
- Nits: Remove unneeded mut and loop (rust-lang/cargo#8334)
- 1.45 beta backports (rust-lang/cargo#8331)
- Better error message when passing in relative path to Workspace::new (rust-lang/cargo#8321)
- Don't hash executable filenames on apple platforms. (rust-lang/cargo#8329)
- fix clippy warnings (rust-lang/cargo#8324)
- Require latest libgit2 to pull in bugfixes (rust-lang/cargo#8320)
- Fix an accidental raw access of field (rust-lang/cargo#8319)
- Use mem::take to replace with Default values (rust-lang/cargo#8314)
- Allow Windows dylibs without dll suffix. (rust-lang/cargo#8310)
- Show alias in help message (rust-lang/cargo#8307)
@joshtriplett joshtriplett added the relnotes Release-note worthy label Jun 20, 2020
@A1-Triard
Copy link

I've tried last nighly and it looks like env!("CARGO_CRATE_NAME") is always set to "build_script_build", env!("CARGO_BIN_NAME") does not compile, and env::var("CARGO_CRATE_NAME") as well as `env::var("CARGO_BIN_NAME") are not set at all.

@ehuss
Copy link
Contributor

ehuss commented Jun 23, 2020

@A1-Triard CARGO_BIN_NAME is only set while building the binary.

@A1-Triard
Copy link

A1-Triard commented Jun 23, 2020

@ehuss, I am building a binary. Two binaries in fact. They are not specified in Cargo.toml explicitly, but they lie in right place (src/bin/bin1.rs, src/bin/bin2.rs), thus they are automatically detected as binaries.

@ehuss
Copy link
Contributor

ehuss commented Jun 23, 2020

From your description, it sounds like you are checking from the build script, though? The following works for me when placed inside the binary:

// src/main.rs
fn main() {
    println!("Hello, {}", env!("CARGO_BIN_NAME"));
}

Are you checking it from the build script, or from the binary?

@A1-Triard
Copy link

@ehuss, I have understood what you mean. Thank you for explanation.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Aug 30, 2020
according to various people on tech-pkg@, there are no problems with
the Firefox build

Version 1.46.0 (2020-08-27)
==========================

Language
--------
- [`if`, `match`, and `loop` expressions can now be used in const functions.][72437]
- [Additionally you are now also able to coerce and cast to slices (`&[T]`) in
  const functions.][73862]
- [The `#[track_caller]` attribute can now be added to functions to use the
  function's caller's location information for panic messages.][72445]
- [Recursively indexing into tuples no longer needs parentheses.][71322] E.g.
  `x.0.0` over `(x.0).0`.
- [`mem::transmute` can now be used in static and constants.][72920] **Note**
  You currently can't use `mem::transmute` in constant functions.

Compiler
--------
- [You can now use the `cdylib` target on Apple iOS and tvOS platforms.][73516]
- [Enabled static "Position Independent Executables" by default
  for `x86_64-unknown-linux-musl`.][70740]

Libraries
---------
- [`mem::forget` is now a `const fn`.][73887]
- [`String` now implements `From<char>`.][73466]
- [The `leading_ones`, and `trailing_ones` methods have been stabilised for all
  integer types.][73032]
- [`vec::IntoIter<T>` now implements `AsRef<[T]>`.][72583]
- [All non-zero integer types (`NonZeroU8`) now implement `TryFrom` for their
  zero-able equivalent (e.g. `TryFrom<u8>`).][72717]
- [`&[T]` and `&mut [T]` now implement `PartialEq<Vec<T>>`.][71660]
- [`(String, u16)` now implements `ToSocketAddrs`.][73007]
- [`vec::Drain<'_, T>` now implements `AsRef<[T]>`.][72584]

Stabilized APIs
---------------
- [`Option::zip`]
- [`vec::Drain::as_slice`]

Cargo
-----
Added a number of new environment variables that are now available when
compiling your crate.

- [`CARGO_BIN_NAME` and `CARGO_CRATE_NAME`][cargo/8270] Providing the name of
  the specific binary being compiled and the name of the crate.
- [`CARGO_PKG_LICENSE`][cargo/8325] The license from the manifest of the package.
- [`CARGO_PKG_LICENSE_FILE`][cargo/8387] The path to the license file.

Compatibility Notes
-------------------
- [The target configuration option `abi_blacklist` has been renamed
  to `unsupported_abis`.][74150] The old name will still continue to work.
- [Rustc will now warn if you cast a C-like enum that implements `Drop`.][72331]
  This was previously accepted but will become a hard error in a future release.
- [Rustc will fail to compile if you have a struct with
  `#[repr(i128)]` or `#[repr(u128)]`.][74109] This representation is currently only
  allowed on `enum`s.
- [Tokens passed to `macro_rules!` are now always captured.][73293] This helps
  ensure that spans have the correct information, and may cause breakage if you
  were relying on receiving spans with dummy information.
- [The InnoSetup installer for Windows is no longer available.][72569] This was
  a legacy installer that was replaced by a MSI installer a few years ago but
  was still being built.
- [`{f32, f64}::asinh` now returns the correct values for negative numbers.][72486]
- [Rustc will no longer accept overlapping trait implementations that only
  differ in how the lifetime was bound.][72493]
- [Rustc now correctly relates the lifetime of an existential associated
  type.][71896] This fixes some edge cases where `rustc` would erroneously allow
  you to pass a shorter lifetime than expected.
- [Rustc now dynamically links to `libz` (also called `zlib`) on Linux.][74420]
  The library will need to be installed for `rustc` to work, even though we
  expect it to be already available on most systems.
- [Tests annotated with `#[should_panic]` are broken on ARMv7 while running
  under QEMU.][74820]
- [Pretty printing of some tokens in procedural macros changed.][75453] The
  exact output returned by rustc's pretty printing is an unstable
  implementation detail: we recommend any macro relying on it to switch to a
  more robust parsing system.

[75453]: rust-lang/rust#75453
[74820]: rust-lang/rust#74820
[74420]: rust-lang/rust#74420
[74109]: rust-lang/rust#74109
[74150]: rust-lang/rust#74150
[73862]: rust-lang/rust#73862
[73887]: rust-lang/rust#73887
[73466]: rust-lang/rust#73466
[73516]: rust-lang/rust#73516
[73293]: rust-lang/rust#73293
[73007]: rust-lang/rust#73007
[73032]: rust-lang/rust#73032
[72920]: rust-lang/rust#72920
[72569]: rust-lang/rust#72569
[72583]: rust-lang/rust#72583
[72584]: rust-lang/rust#72584
[72717]: rust-lang/rust#72717
[72437]: rust-lang/rust#72437
[72445]: rust-lang/rust#72445
[72486]: rust-lang/rust#72486
[72493]: rust-lang/rust#72493
[72331]: rust-lang/rust#72331
[71896]: rust-lang/rust#71896
[71660]: rust-lang/rust#71660
[71322]: rust-lang/rust#71322
[70740]: rust-lang/rust#70740
[cargo/8270]: rust-lang/cargo#8270
[cargo/8325]: rust-lang/cargo#8325
[cargo/8387]: rust-lang/cargo#8387
[`Option::zip`]: https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.zip
[`vec::Drain::as_slice`]: https://doc.rust-lang.org/stable/std/vec/struct.Drain.html#method.as_slice
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this pull request Oct 30, 2020
Pkgsrc changes:
 * Portability patches for Illumos have been intregrated upstream,
   so are no longer needed in pkgsrc.
 * Adjust one other patch, and update vendor/libc cargo checksum.

Upstream changes:

Version 1.46.0 (2020-08-27)
==========================

Language
--------
- [`if`, `match`, and `loop` expressions can now be used in const functions.]
  [72437]
- [Additionally you are now also able to coerce and cast to slices (`&[T]`) in
  const functions.][73862]
- [The `#[track_caller]` attribute can now be added to functions to use the
  function's caller's location information for panic messages.][72445]
- [Recursively indexing into tuples no longer needs parentheses.][71322] E.g.
  `x.0.0` over `(x.0).0`.
- [`mem::transmute` can now be used in static and constants.][72920] **Note**
  You currently can't use `mem::transmute` in constant functions.

Compiler
--------
- [You can now use the `cdylib` target on Apple iOS and tvOS platforms.][73516]
- [Enabled static "Position Independent Executables" by default
  for `x86_64-unknown-linux-musl`.][70740]

Libraries
---------
- [`mem::forget` is now a `const fn`.][73887]
- [`String` now implements `From<char>`.][73466]
- [The `leading_ones`, and `trailing_ones` methods have been stabilised for all
  integer types.][73032]
- [`vec::IntoIter<T>` now implements `AsRef<[T]>`.][72583]
- [All non-zero integer types (`NonZeroU8`) now implement `TryFrom` for their
  zero-able equivalent (e.g. `TryFrom<u8>`).][72717]
- [`&[T]` and `&mut [T]` now implement `PartialEq<Vec<T>>`.][71660]
- [`(String, u16)` now implements `ToSocketAddrs`.][73007]
- [`vec::Drain<'_, T>` now implements `AsRef<[T]>`.][72584]

Stabilized APIs
---------------
- [`Option::zip`]
- [`vec::Drain::as_slice`]

Cargo
-----
Added a number of new environment variables that are now available when
compiling your crate.

- [`CARGO_BIN_NAME` and `CARGO_CRATE_NAME`][cargo/8270] Providing the name of
  the specific binary being compiled and the name of the crate.
- [`CARGO_PKG_LICENSE`][cargo/8325] The license from the manifest of the
  package.
- [`CARGO_PKG_LICENSE_FILE`][cargo/8387] The path to the license file.

Compatibility Notes
-------------------
- [The target configuration option `abi_blacklist` has been renamed
  to `unsupported_abis`.][74150] The old name will still continue to work.
- [Rustc will now warn if you have a C-like enum that implements `Drop`.][72331]
  This was previously accepted but will become a hard error in a future release.
- [Rustc will fail to compile if you have a struct with
  `#[repr(i128)]` or `#[repr(u128)]`.][74109] This representation is currently
  only allowed on `enum`s.
- [Tokens passed to `macro_rules!` are now always captured.][73293] This helps
  ensure that spans have the correct information, and may cause breakage if you
  were relying on receiving spans with dummy information.
- [The InnoSetup installer for Windows is no longer available.][72569] This was
  a legacy installer that was replaced by a MSI installer a few years ago but
  was still being built.
- [`{f32, f64}::asinh` now returns the correct values for negative numbers.]
  [72486]
- [Rustc will no longer accept overlapping trait implementations that only
  differ in how the lifetime was bound.][72493]
- [Rustc now correctly relates the lifetime of an existential associated
  type.][71896] This fixes some edge cases where `rustc` would erroneously
  allow you to pass a shorter lifetime than expected.
- [Rustc now dynamically links to `libz` (also called `zlib`) on Linux.][74420]
  The library will need to be installed for `rustc` to work, even though we
  expect it to be already available on most systems.
- [Tests annotated with `#[should_panic]` are broken on ARMv7 while running
  under QEMU.][74820]
- [Pretty printing of some tokens in procedural macros changed.][75453] The
  exact output returned by rustc's pretty printing is an unstable
  implementation detail: we recommend any macro relying on it to switch to a
  more robust parsing system.

[75453]: rust-lang/rust#75453
[74820]: rust-lang/rust#74820
[74420]: rust-lang/rust#74420
[74109]: rust-lang/rust#74109
[74150]: rust-lang/rust#74150
[73862]: rust-lang/rust#73862
[73887]: rust-lang/rust#73887
[73466]: rust-lang/rust#73466
[73516]: rust-lang/rust#73516
[73293]: rust-lang/rust#73293
[73007]: rust-lang/rust#73007
[73032]: rust-lang/rust#73032
[72920]: rust-lang/rust#72920
[72569]: rust-lang/rust#72569
[72583]: rust-lang/rust#72583
[72584]: rust-lang/rust#72584
[72717]: rust-lang/rust#72717
[72437]: rust-lang/rust#72437
[72445]: rust-lang/rust#72445
[72486]: rust-lang/rust#72486
[72493]: rust-lang/rust#72493
[72331]: rust-lang/rust#72331
[71896]: rust-lang/rust#71896
[71660]: rust-lang/rust#71660
[71322]: rust-lang/rust#71322
[70740]: rust-lang/rust#70740
[cargo/8270]: rust-lang/cargo#8270
[cargo/8325]: rust-lang/cargo#8325
[cargo/8387]: rust-lang/cargo#8387
[`Option::zip`]: https://doc.rust-lang.org/stable/std/option/enum.Option.html#method.zip
[`vec::Drain::as_slice`]: https://doc.rust-lang.org/stable/std/vec/struct.Drain.html#method.as_slice
@ehuss ehuss added this to the 1.46.0 milestone Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge FCP with intent to merge finished-final-comment-period FCP complete relnotes Release-note worthy S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-cargo Team: Cargo
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Environment variable to identify the binary name
8 participants