From cc971ee517353e33428f33ccf329a6a42c2d7e33 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 26 Feb 2018 22:48:31 +0300 Subject: [PATCH] Make cargo-the-binary version the same as the Rust version Closes #4211 --- src/cargo/lib.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 41615e3d87d..ac611b010bc 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -72,9 +72,9 @@ pub struct CfgInfo { } pub struct VersionInfo { - pub major: String, - pub minor: String, - pub patch: String, + pub major: u8, + pub minor: u8, + pub patch: u8, pub pre_release: Option, // Information that's only available when we were built with // configure/make, rather than cargo itself. @@ -192,12 +192,28 @@ fn handle_cause(cargo_err: &Error, shell: &mut Shell) -> bool { } pub fn version() -> VersionInfo { - macro_rules! env_str { - ($name:expr) => { env!($name).to_string() } - } macro_rules! option_env_str { ($name:expr) => { option_env!($name).map(|s| s.to_string()) } } + + // 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 projects. + // + // 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::().unwrap() - 1; + let patch = env!("CARGO_PKG_VERSION_PATCH").parse::().unwrap(); + match option_env!("CFG_RELEASE_CHANNEL") { // We have environment variables set up from configure/make. Some(_) => { @@ -210,9 +226,9 @@ pub fn version() -> VersionInfo { } }); VersionInfo { - major: env_str!("CARGO_PKG_VERSION_MAJOR"), - minor: env_str!("CARGO_PKG_VERSION_MINOR"), - patch: env_str!("CARGO_PKG_VERSION_PATCH"), + major, + minor, + patch, pre_release: option_env_str!("CARGO_PKG_VERSION_PRE"), cfg_info: Some(CfgInfo { release_channel: option_env_str!("CFG_RELEASE_CHANNEL").unwrap(), @@ -223,9 +239,9 @@ pub fn version() -> VersionInfo { // We are being compiled by Cargo itself. None => { VersionInfo { - major: env_str!("CARGO_PKG_VERSION_MAJOR"), - minor: env_str!("CARGO_PKG_VERSION_MINOR"), - patch: env_str!("CARGO_PKG_VERSION_PATCH"), + major, + minor, + patch, pre_release: option_env_str!("CARGO_PKG_VERSION_PRE"), cfg_info: None, }