]> git.proxmox.com Git - rustc.git/blob - vendor/byteorder/build.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / vendor / byteorder / build.rs
1 // For the 'try!' macro, until we bump MSRV past 1.12.
2 #![allow(deprecated)]
3
4 use std::env;
5 use std::ffi::OsString;
6 use std::io::{self, Write};
7 use std::process::Command;
8
9 fn main() {
10 let version = match Version::read() {
11 Ok(version) => version,
12 Err(err) => {
13 writeln!(
14 &mut io::stderr(),
15 "failed to parse `rustc --version`: {}",
16 err
17 ).unwrap();
18 return;
19 }
20 };
21 enable_i128(version);
22 }
23
24 fn enable_i128(version: Version) {
25 if version < (Version { major: 1, minor: 26, patch: 0 }) {
26 return;
27 }
28
29 println!("cargo:rustc-cfg=byteorder_i128");
30 }
31
32 #[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
33 struct Version {
34 major: u32,
35 minor: u32,
36 patch: u32,
37 }
38
39 impl Version {
40 fn read() -> Result<Version, String> {
41 let rustc = env::var_os("RUSTC").unwrap_or(OsString::from("rustc"));
42 let output = Command::new(&rustc)
43 .arg("--version")
44 .output()
45 .unwrap()
46 .stdout;
47 Version::parse(&String::from_utf8(output).unwrap())
48 }
49
50 fn parse(mut s: &str) -> Result<Version, String> {
51 if !s.starts_with("rustc ") {
52 return Err(format!("unrecognized version string: {}", s));
53 }
54 s = &s["rustc ".len()..];
55
56 let parts: Vec<&str> = s.split(".").collect();
57 if parts.len() < 3 {
58 return Err(format!("not enough version parts: {:?}", parts));
59 }
60
61 let mut num = String::new();
62 for c in parts[0].chars() {
63 if !c.is_digit(10) {
64 break;
65 }
66 num.push(c);
67 }
68 let major = try!(num.parse::<u32>().map_err(|e| e.to_string()));
69
70 num.clear();
71 for c in parts[1].chars() {
72 if !c.is_digit(10) {
73 break;
74 }
75 num.push(c);
76 }
77 let minor = try!(num.parse::<u32>().map_err(|e| e.to_string()));
78
79 num.clear();
80 for c in parts[2].chars() {
81 if !c.is_digit(10) {
82 break;
83 }
84 num.push(c);
85 }
86 let patch = try!(num.parse::<u32>().map_err(|e| e.to_string()));
87
88 Ok(Version { major: major, minor: minor, patch: patch })
89 }
90 }