Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get rustc version string at rustversion crate build time #11

Merged
merged 1 commit into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ description = "Conditional compilation according to rustc compiler version"
repository = "https://github.com/dtolnay/rustversion"
documentation = "https://docs.rs/rustversion"
readme = "README.md"
build = "build.rs"

[lib]
proc-macro = true
Expand Down
18 changes: 18 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use std::env;
use std::ffi::OsString;
use std::fs;
use std::path::Path;
use std::process::Command;

fn main() {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
let output = Command::new(rustc)
.arg("--version")
.output()
.expect("Failed to exec rustc");

let string = String::from_utf8(output.stdout).expect("rustc output not utf8");
let dir = env::var_os("OUT_DIR").expect("OUT_DIR not set");

fs::write(&Path::new(&dir).join("version.txt"), &string).expect("failed to write version.txt")
}
15 changes: 4 additions & 11 deletions src/rustc.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::env;
use std::ffi::OsString;
use std::fmt::{self, Display};
use std::io;
use std::process::Command;
use std::str::FromStr;
use std::string::FromUtf8Error;

use crate::date::Date;
use crate::version::{Channel::*, Version};
use proc_macro2::Span;

const RUSTC_VERSION: &str = include_str!(concat!(env!("OUT_DIR"), "/version.txt"));

#[derive(Debug)]
pub enum Error {
Exec(io::Error),
Expand Down Expand Up @@ -48,16 +48,9 @@ impl From<Error> for syn::Error {
}

pub fn version() -> Result<Version> {
let rustc = env::var_os("RUSTC").unwrap_or_else(|| OsString::from("rustc"));
let output = Command::new(rustc)
.arg("--version")
.output()
.map_err(Error::Exec)?;
let string = String::from_utf8(output.stdout)?;

match parse(&string) {
match parse(RUSTC_VERSION) {
Some(version) => Ok(version),
None => Err(Error::Parse(string)),
None => Err(Error::Parse(RUSTC_VERSION.to_string())),
}
}

Expand Down