third_party_rust_version_check/README.md

81 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2017-01-15 08:49:18 +00:00
# version\_check
[![Build Status](https://github.com/SergioBenitez/version_check/workflows/CI/badge.svg)](https://github.com/SergioBenitez/version_check/actions)
2019-05-24 01:56:26 +00:00
[![Current Crates.io Version](https://img.shields.io/crates/v/version_check.svg)](https://crates.io/crates/version_check)
[![rustdocs on docs.rs](https://docs.rs/version_check/badge.svg)](https://docs.rs/version_check)
2017-01-15 08:49:18 +00:00
This tiny crate checks that the running or installed `rustc` meets some version
requirements. The version is queried by calling the Rust compiler with
`--version`. The path to the compiler is determined first via the `RUSTC`
environment variable. If it is not set, then `rustc` is used. If that fails, no
determination is made, and calls return `None`.
## Usage
2017-07-10 22:23:15 +00:00
Add to your `Cargo.toml` file, typically as a build dependency:
2017-01-15 08:49:18 +00:00
```toml
2017-07-10 22:23:15 +00:00
[build-dependencies]
2019-05-24 01:56:26 +00:00
version_check = "0.9"
2017-01-15 08:49:18 +00:00
```
`version_check` is compatible and compiles with Rust 1.0.0 and beyond.
2017-01-15 08:49:18 +00:00
## Examples
2019-05-24 01:56:26 +00:00
Set a `cfg` flag in `build.rs` if the running compiler was determined to be
at least version `1.13.0`:
2017-01-15 08:49:18 +00:00
```rust
2019-05-24 01:56:26 +00:00
extern crate version_check as rustc;
2017-01-15 08:49:18 +00:00
2019-05-24 01:56:26 +00:00
if rustc::is_min_version("1.13.0").unwrap_or(false) {
println!("cargo:rustc-cfg=question_mark_operator");
}
2017-01-15 08:49:18 +00:00
```
2019-05-24 01:56:26 +00:00
Check that the running compiler was released on or after `2018-12-18`:
2017-01-15 08:49:18 +00:00
```rust
2019-05-24 01:56:26 +00:00
extern crate version_check as rustc;
2017-01-15 08:49:18 +00:00
2019-05-24 01:56:26 +00:00
match rustc::is_min_date("2018-12-18") {
Some(true) => "Yep! It's recent!",
Some(false) => "No, it's older.",
None => "Couldn't determine the rustc version."
2017-01-15 08:49:18 +00:00
};
```
2019-05-24 01:56:26 +00:00
Check that the running compiler supports feature flags:
2017-01-15 08:49:18 +00:00
```rust
2019-05-24 01:56:26 +00:00
extern crate version_check as rustc;
2017-01-15 08:49:18 +00:00
2019-05-24 01:56:26 +00:00
match rustc::is_feature_flaggable() {
Some(true) => "Yes! It's a dev or nightly release!",
Some(false) => "No, it's stable or beta.",
None => "Couldn't determine the rustc version."
2017-01-15 08:49:18 +00:00
};
```
2019-05-24 01:56:26 +00:00
See the [rustdocs](https://docs.rs/version_check) for more examples and complete
documentation.
2017-01-15 08:49:18 +00:00
## Alternatives
This crate is dead simple with no dependencies. If you need something more and
don't care about panicking if the version cannot be obtained, or if you don't
mind adding dependencies, see [rustc_version]. If you'd instead prefer a feature
detection library that works by dynamically invoking `rustc` with a
representative code sample, see [autocfg].
[rustc_version]: https://crates.io/crates/rustc_version
[autocfg]: https://crates.io/crates/autocfg
2017-01-15 08:50:25 +00:00
## License
`version_check` is licensed under either of the following, at your option:
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)