Skip to content

Commit

Permalink
Make cargo-the-binary version the same as the Rust version
Browse files Browse the repository at this point in the history
Closes #4211
  • Loading branch information
matklad committed Feb 26, 2018
1 parent 62f05f4 commit cc971ee
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/cargo/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
// Information that's only available when we were built with
// configure/make, rather than cargo itself.
Expand Down Expand Up @@ -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::<u8>().unwrap() - 1;
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u8>().unwrap();

match option_env!("CFG_RELEASE_CHANNEL") {
// We have environment variables set up from configure/make.
Some(_) => {
Expand All @@ -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(),
Expand All @@ -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,
}
Expand Down

0 comments on commit cc971ee

Please sign in to comment.