Skip to content

Commit

Permalink
Use version from rustbuild.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Dec 7, 2021
1 parent f5cb79e commit a58725a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
5 changes: 1 addition & 4 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ pub fn get_version_string(is_verbose: bool) -> String {
let version = cargo::version();
let mut version_string = format!("cargo {}\n", version);
if is_verbose {
version_string.push_str(&format!(
"release: {}.{}.{}\n",
version.major, version.minor, version.patch
));
version_string.push_str(&format!("release: {}\n", version.version,));
if let Some(ref cfg) = version.cfg_info {
if let Some(ref ci) = cfg.commit_info {
version_string.push_str(&format!("commit-hash: {}\n", ci.commit_hash));
Expand Down
59 changes: 27 additions & 32 deletions src/cargo/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,16 @@ pub struct CfgInfo {

/// Cargo's version.
pub struct VersionInfo {
pub major: u8,
pub minor: u8,
pub patch: u8,
/// 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.major, self.minor, self.patch)?;
if let Some(channel) = self.cfg_info.as_ref().map(|ci| &ci.release_channel) {
if channel != "stable" {
write!(f, "-{}", channel)?;
}
};
write!(f, "{}", self.version)?;

if let Some(ref cfg) = self.cfg_info {
if let Some(ref ci) = cfg.commit_info {
Expand All @@ -53,23 +47,28 @@ pub fn version() -> VersionInfo {
};
}

// So this is pretty horrible...
// 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.
//
// We want to make the `binary` version the same as the corresponding Rust/rustc release.
// At the same time, we want to keep the library version at `0.x`, because Cargo as
// a library is (and probably will always be) unstable.
//
// Historically, Cargo used the same version number for both the binary and the library.
// Specifically, rustc 1.x.z was paired with cargo 0.x+1.w.
// We continue to use this scheme for the library, but transform it to 1.x.w for the purposes
// of `cargo --version`.
let major = 1;
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap() - 1;
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap();
// 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.
Expand All @@ -80,9 +79,7 @@ pub fn version() -> VersionInfo {
commit_date: option_env_str!("CFG_COMMIT_DATE").unwrap(),
});
VersionInfo {
major,
minor,
patch,
version,
cfg_info: Some(CfgInfo {
release_channel: option_env_str!("CFG_RELEASE_CHANNEL").unwrap(),
commit_info,
Expand All @@ -91,9 +88,7 @@ pub fn version() -> VersionInfo {
}
// We are being compiled by Cargo itself.
None => VersionInfo {
major,
minor,
patch,
version,
cfg_info: None,
},
}
Expand Down

0 comments on commit a58725a

Please sign in to comment.