-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #10178 - ehuss:version-from-rustc, r=alexcrichton
Sync cargo-the-cli version with rustc. People occasionally get confused when cargo's version does not match the version of rustc. This happens in a variety of scenarios: * Point releases. * Beta releases (cargo is missing the .1 .2, etc.) * Nightly releases when cargo's version has not yet been bumped. This changes it so that cargo-the-cli will always report the same version as rustc (assuming they were built with rustbuild). The git information remains the same (reports cargo's last commit sha). Closes #10122
- Loading branch information
Showing
3 changed files
with
98 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
//! Code for representing cargo's release version number. | ||
use std::fmt; | ||
|
||
/// Information about the git repository where cargo was built from. | ||
pub struct CommitInfo { | ||
pub short_commit_hash: String, | ||
pub commit_hash: String, | ||
pub commit_date: String, | ||
} | ||
|
||
/// Information provided by the outer build system (rustbuild aka bootstrap). | ||
pub struct CfgInfo { | ||
/// Information about the Git repository we may have been built from. | ||
pub commit_info: Option<CommitInfo>, | ||
/// The release channel we were built for (stable/beta/nightly/dev). | ||
pub release_channel: String, | ||
} | ||
|
||
/// Cargo's version. | ||
pub struct VersionInfo { | ||
/// Cargo's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc. | ||
pub version: String, | ||
/// Information that's only available when we were built with | ||
/// rustbuild, rather than Cargo itself. | ||
pub cfg_info: Option<CfgInfo>, | ||
} | ||
|
||
impl fmt::Display for VersionInfo { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.version)?; | ||
|
||
if let Some(ref cfg) = self.cfg_info { | ||
if let Some(ref ci) = cfg.commit_info { | ||
write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?; | ||
} | ||
}; | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Returns information about cargo's version. | ||
pub fn version() -> VersionInfo { | ||
macro_rules! option_env_str { | ||
($name:expr) => { | ||
option_env!($name).map(|s| s.to_string()) | ||
}; | ||
} | ||
|
||
// This is the version set in rustbuild, which we use to match rustc. | ||
let version = option_env_str!("CFG_RELEASE").unwrap_or_else(|| { | ||
// If cargo is not being built by rustbuild, then we just use the | ||
// version from cargo's own `Cargo.toml`. | ||
// | ||
// There are two versions at play here: | ||
// - version of cargo-the-binary, which you see when you type `cargo --version` | ||
// - version of cargo-the-library, which you download from crates.io for use | ||
// in your packages. | ||
// | ||
// The library is permanently unstable, so it always has a 0 major | ||
// version. However, the CLI now reports a stable 1.x version | ||
// (starting in 1.26) which stays in sync with rustc's version. | ||
// | ||
// Coincidentally, the minor version for cargo-the-library is always | ||
// +1 of rustc's minor version (that is, `rustc 1.11.0` corresponds to | ||
// `cargo `0.12.0`). The versions always get bumped in lockstep, so | ||
// this should continue to hold. | ||
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap() - 1; | ||
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap(); | ||
format!("1.{}.{}", minor, patch) | ||
}); | ||
|
||
match option_env!("CFG_RELEASE_CHANNEL") { | ||
// We have environment variables set up from configure/make. | ||
Some(_) => { | ||
let commit_info = option_env!("CFG_COMMIT_HASH").map(|s| CommitInfo { | ||
commit_hash: s.to_string(), | ||
short_commit_hash: option_env_str!("CFG_SHORT_COMMIT_HASH").unwrap(), | ||
commit_date: option_env_str!("CFG_COMMIT_DATE").unwrap(), | ||
}); | ||
VersionInfo { | ||
version, | ||
cfg_info: Some(CfgInfo { | ||
release_channel: option_env_str!("CFG_RELEASE_CHANNEL").unwrap(), | ||
commit_info, | ||
}), | ||
} | ||
} | ||
// We are being compiled by Cargo itself. | ||
None => VersionInfo { | ||
version, | ||
cfg_info: None, | ||
}, | ||
} | ||
} |