Skip to content

Commit

Permalink
Introduce CARGO_PKG_EDITION env var
Browse files Browse the repository at this point in the history
Solves #14872
  • Loading branch information
nyurik committed Nov 29, 2024
1 parent 3908f64 commit 93f08b6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/build-rs/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,12 @@ pub fn cargo_pkg_license_file() -> Option<PathBuf> {
to_opt(var_or_panic("CARGO_PKG_LICENSE_FILE")).map(to_path)
}

/// The Rust language edition from the manifest of your package.
#[track_caller]
pub fn cargo_pkg_edition() -> Option<String> {
to_opt(var_or_panic("CARGO_PKG_EDITION")).map(to_string)
}

/// The Rust version from the manifest of your package. Note that this is the
/// minimum Rust version supported by the package, not the current Rust version.
#[track_caller]
Expand Down
2 changes: 2 additions & 0 deletions src/cargo/core/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ impl<'gctx> Compilation<'gctx> {
// consider adding the corresponding properties to the hash
// in BuildContext::target_metadata()
let rust_version = pkg.rust_version().as_ref().map(ToString::to_string);
let edition = pkg.edition();
cmd.env("CARGO_MANIFEST_DIR", pkg.root())
.env("CARGO_MANIFEST_PATH", pkg.manifest_path())
.env("CARGO_PKG_VERSION_MAJOR", &pkg.version().major.to_string())
Expand Down Expand Up @@ -390,6 +391,7 @@ impl<'gctx> Compilation<'gctx> {
metadata.license_file.as_ref().unwrap_or(&String::new()),
)
.env("CARGO_PKG_AUTHORS", &pkg.authors().join(":"))
.env("CARGO_PKG_EDITION", edition.to_string())
.env(
"CARGO_PKG_RUST_VERSION",
&rust_version.as_deref().unwrap_or_default(),
Expand Down
8 changes: 6 additions & 2 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::core::dependency::DepKind;
use crate::core::resolver::features::ForceAllTargets;
use crate::core::resolver::{HasDevUnits, Resolve};
use crate::core::{
CliUnstable, Dependency, Features, Manifest, PackageId, PackageIdSpec, SerializedDependency,
SourceId, Target,
CliUnstable, Dependency, Edition, Features, Manifest, PackageId, PackageIdSpec,
SerializedDependency, SourceId, Target,
};
use crate::core::{Summary, Workspace};
use crate::sources::source::{MaybePackage, SourceMap};
Expand Down Expand Up @@ -167,6 +167,10 @@ impl Package {
pub fn proc_macro(&self) -> bool {
self.targets().iter().any(|target| target.proc_macro())
}
/// Gets the package's language edition
pub fn edition(&self) -> Edition {
self.manifest().edition()
}
/// Gets the package's minimum Rust version.
pub fn rust_version(&self) -> Option<&RustVersion> {
self.manifest().rust_version()
Expand Down
1 change: 1 addition & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ corresponding environment variable is set to the empty string, `""`.
* `CARGO_PKG_REPOSITORY` --- The repository from the manifest of your package.
* `CARGO_PKG_LICENSE` --- The license from the manifest of your package.
* `CARGO_PKG_LICENSE_FILE` --- The license file from the manifest of your package.
* `CARGO_PKG_EDITION` --- The Rust language edition from the manifest of your package.
* `CARGO_PKG_RUST_VERSION` --- The Rust version from the manifest of your package.
Note that this is the minimum Rust version supported by the package, not the
current Rust version.
Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,7 @@ fn crate_env_vars() {
static LICENSE: &'static str = env!("CARGO_PKG_LICENSE");
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
static EDITION: &'static str = env!("CARGO_PKG_EDITION");
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
static README: &'static str = env!("CARGO_PKG_README");
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
Expand Down

0 comments on commit 93f08b6

Please sign in to comment.