@@ -19,22 +19,16 @@ pub struct CfgInfo {
19
19
20
20
/// Cargo's version.
21
21
pub struct VersionInfo {
22
- pub major : u8 ,
23
- pub minor : u8 ,
24
- pub patch : u8 ,
22
+ /// Cargo's version, such as "1.57.0", "1.58.0-beta.1", "1.59.0-nightly", etc.
23
+ pub version : String ,
25
24
/// Information that's only available when we were built with
26
25
/// rustbuild, rather than Cargo itself.
27
26
pub cfg_info : Option < CfgInfo > ,
28
27
}
29
28
30
29
impl fmt:: Display for VersionInfo {
31
30
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
32
- write ! ( f, "{}.{}.{}" , self . major, self . minor, self . patch) ?;
33
- if let Some ( channel) = self . cfg_info . as_ref ( ) . map ( |ci| & ci. release_channel ) {
34
- if channel != "stable" {
35
- write ! ( f, "-{}" , channel) ?;
36
- }
37
- } ;
31
+ write ! ( f, "{}" , self . version) ?;
38
32
39
33
if let Some ( ref cfg) = self . cfg_info {
40
34
if let Some ( ref ci) = cfg. commit_info {
@@ -53,23 +47,28 @@ pub fn version() -> VersionInfo {
53
47
} ;
54
48
}
55
49
56
- // So this is pretty horrible...
57
- // There are two versions at play here:
58
- // - version of cargo-the-binary, which you see when you type `cargo --version`
59
- // - version of cargo-the-library, which you download from crates.io for use
60
- // in your packages.
61
- //
62
- // We want to make the `binary` version the same as the corresponding Rust/rustc release.
63
- // At the same time, we want to keep the library version at `0.x`, because Cargo as
64
- // a library is (and probably will always be) unstable.
65
- //
66
- // Historically, Cargo used the same version number for both the binary and the library.
67
- // Specifically, rustc 1.x.z was paired with cargo 0.x+1.w.
68
- // We continue to use this scheme for the library, but transform it to 1.x.w for the purposes
69
- // of `cargo --version`.
70
- let major = 1 ;
71
- let minor = env ! ( "CARGO_PKG_VERSION_MINOR" ) . parse :: < u8 > ( ) . unwrap ( ) - 1 ;
72
- let patch = env ! ( "CARGO_PKG_VERSION_PATCH" ) . parse :: < u8 > ( ) . unwrap ( ) ;
50
+ // This is the version set in rustbuild, which we use to match rustc.
51
+ let version = option_env_str ! ( "CFG_RELEASE" ) . unwrap_or_else ( || {
52
+ // If cargo is not being built by rustbuild, then we just use the
53
+ // version from cargo's own `Cargo.toml`.
54
+ //
55
+ // There are two versions at play here:
56
+ // - version of cargo-the-binary, which you see when you type `cargo --version`
57
+ // - version of cargo-the-library, which you download from crates.io for use
58
+ // in your packages.
59
+ //
60
+ // The library is permanently unstable, so it always has a 0 major
61
+ // version. However, the CLI now reports a stable 1.x version
62
+ // (starting in 1.26) which stays in sync with rustc's version.
63
+ //
64
+ // Coincidentally, the minor version for cargo-the-library is always
65
+ // +1 of rustc's minor version (that is, `rustc 1.11.0` corresponds to
66
+ // `cargo `0.12.0`). The versions always get bumped in lockstep, so
67
+ // this should continue to hold.
68
+ let minor = env ! ( "CARGO_PKG_VERSION_MINOR" ) . parse :: < u8 > ( ) . unwrap ( ) - 1 ;
69
+ let patch = env ! ( "CARGO_PKG_VERSION_PATCH" ) . parse :: < u8 > ( ) . unwrap ( ) ;
70
+ format ! ( "1.{}.{}" , minor, patch)
71
+ } ) ;
73
72
74
73
match option_env ! ( "CFG_RELEASE_CHANNEL" ) {
75
74
// We have environment variables set up from configure/make.
@@ -80,9 +79,7 @@ pub fn version() -> VersionInfo {
80
79
commit_date : option_env_str ! ( "CFG_COMMIT_DATE" ) . unwrap ( ) ,
81
80
} ) ;
82
81
VersionInfo {
83
- major,
84
- minor,
85
- patch,
82
+ version,
86
83
cfg_info : Some ( CfgInfo {
87
84
release_channel : option_env_str ! ( "CFG_RELEASE_CHANNEL" ) . unwrap ( ) ,
88
85
commit_info,
@@ -91,9 +88,7 @@ pub fn version() -> VersionInfo {
91
88
}
92
89
// We are being compiled by Cargo itself.
93
90
None => VersionInfo {
94
- major,
95
- minor,
96
- patch,
91
+ version,
97
92
cfg_info : None ,
98
93
} ,
99
94
}
0 commit comments