1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![warn(missing_docs)]

//! Simple library for getting the version information of a `rustc`
//! compiler.
//!
//! This calls `$RUSTC --version` and parses the output, falling
//! back to `rustc` if `$RUSTC` is not set.
//!
//! # Example
//!
//! ```rust
//! // This could be a cargo build script
//!
//! extern crate rustc_version;
//! use rustc_version::{version, version_matches, version_meta, Channel};
//!
//! fn main() {
//!     // Assert we haven't travelled back in time
//!     assert!(version().major >= 1);
//!
//!     // Set cfg flags depending on release channel
//!     match version_meta().channel {
//!         Channel::Stable => {
//!             println!("cargo:rustc-cfg=RUSTC_IS_STABLE");
//!         }
//!         Channel::Beta => {
//!             println!("cargo:rustc-cfg=RUSTC_IS_BETA");
//!         }
//!         Channel::Nightly => {
//!             println!("cargo:rustc-cfg=RUSTC_IS_NIGHTLY");
//!         }
//!         Channel::Dev => {
//!             println!("cargo:rustc-cfg=RUSTC_IS_DEV");
//!         }
//!     }
//!
//!     // Directly check a semver version requirment
//!     if version_matches(">= 1.4.0") {
//!         println!("cargo:rustc-cfg=compiler_has_important_bugfix");
//!     }
//! }
//! ```

extern crate semver;
use semver::{Version, VersionReq, Identifier};
use std::process::Command;
use std::env;
use std::ffi::OsString;

/// Release channel of the compiler.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum Channel {
    /// Development release channel
    Dev,
    /// Nightly release channel
    Nightly,
    /// Beta release channel
    Beta,
    /// Stable release channel
    Stable,
}

/// Rustc version plus metada like git short hash and build date.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct VersionMeta {
    /// Version of the compiler
    pub semver: Version,

    /// Git short hash of the build of the compiler
    pub git_short_hash: String,

    /// Build date of the compiler
    pub date: String,

    /// Release channel of the compiler
    pub channel: Channel
}

/// Returns the `rustc` SemVer version.
pub fn version() -> Version {
    version_meta().semver
}

/// Returns the `rustc` SemVer version and additional metadata
/// like the git short hash and build date.
pub fn version_meta() -> VersionMeta {
    let cmd = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));

    let out = Command::new(&cmd)
        .arg("--version")
        .output()
        .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });

    let version_string = String::from_utf8(out.stdout).unwrap();
    let mut parts_iter = version_string.split_whitespace();

    let rustc_string = parts_iter.next().unwrap();
    assert!(rustc_string == "rustc");

    let version_string = parts_iter.next().unwrap();
    let version = Version::parse(version_string).unwrap();

    let hash_string = parts_iter.next().unwrap();
    assert!(hash_string.starts_with('('));
    let hash_string = &hash_string[1..];

    let date_string = parts_iter.next().unwrap();
    assert!(date_string.ends_with(')'));
    let date_string = &date_string[..date_string.len() - 1];

    let channel = if version.pre.is_empty() {
        Channel::Stable
    } else {
        match version.pre[0] {
            Identifier::AlphaNumeric(ref s)
                if s == "dev" => Channel::Dev,
            Identifier::AlphaNumeric(ref s)
                if s == "beta" => Channel::Beta,
            Identifier::AlphaNumeric(ref s)
                if s == "nightly" => Channel::Nightly,
            _ => panic!(),
        }
    };

    VersionMeta {
        semver: version,
        git_short_hash: hash_string.to_string(),
        date: date_string.to_string(),
        channel: channel,
    }
}

/// Check wether the `rustc` version matches the given SemVer
/// version requirement.
pub fn version_matches(req: &str) -> bool {
    VersionReq::parse(req).unwrap().matches(&version())
}

#[test]
fn smoketest() {
    let v = version();
    assert!(v.major >= 1);
    assert!(v.minor >= 2);

    let v = version_meta();
    assert!(v.semver.major >= 1);
    assert!(v.semver.minor >= 2);

    assert!(version_matches(">= 1.2.0"));
}