rustc_version_runtime/lib.rs
1#![warn(missing_docs)]
2
3//! Simple library for getting the version information of a `rustc`
4//! compiler in runtime.
5//!
6//! The goal of this crate is to provide debug information to developers,
7//! not to branch application logic based on compiler version. Please, don't
8//! do that.
9//!
10//! # Example
11//!
12//! ```rust
13//! extern crate rustc_version_runtime;
14//!
15//! println!("This was compiled using {:?}", rustc_version_runtime::version());
16//! ```
17
18extern crate rustc_version;
19extern crate semver;
20use rustc_version::LlvmVersion;
21use semver::{BuildMetadata, Prerelease, VersionReq};
22
23pub use rustc_version::{Channel, Version, VersionMeta};
24mod version {
25 use super::*;
26 include!(concat!(env!("OUT_DIR"), "/version.rs"));
27}
28pub use version::version_meta;
29
30/// Returns the `rustc` SemVer version.
31pub fn version() -> Version {
32 version_meta().semver
33}
34
35/// Check wether the `rustc` version matches the given SemVer
36/// version requirement.
37pub fn version_matches(req: &str) -> bool {
38 // There is some issue checking requirements for pre-releases
39 // https://github.com/steveklabnik/semver/issues/172
40 // I believe users of this crate would expect 1.31.0-nightly to be greater than 1.30 and
41 // equal to 1.31.0. This might not be the case, but I cannot see why.
42 let mut v = version();
43 v.pre = Prerelease::new("").unwrap();
44 VersionReq::parse(req).unwrap().matches(&v)
45}
46
47#[test]
48fn smoketest() {
49 let v = version();
50 assert!(v.major >= 1);
51 assert!(v.minor >= 2);
52
53 let v = version_meta();
54 assert!(v.semver.major >= 1);
55 assert!(v.semver.minor >= 2);
56
57 assert!(version_matches(">= 1.2.0"));
58}