1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 use std::env;
6 use std::process::Command;
7 use std::str::{self, FromStr};
8
main()9 fn main() {
10 println!("cargo:rustc-cfg=build_script_ran");
11 let minor = match rustc_minor_version() {
12 Some(minor) => minor,
13 None => return,
14 };
15
16 let target = env::var("TARGET").unwrap();
17
18 if minor >= 34 {
19 println!("cargo:rustc-cfg=is_new_rustc");
20 } else {
21 println!("cargo:rustc-cfg=is_old_rustc");
22 }
23
24 if target.contains("android") {
25 println!("cargo:rustc-cfg=is_android");
26 }
27 if target.contains("darwin") {
28 println!("cargo:rustc-cfg=is_mac");
29 }
30
31 // Check that we can get a `rustenv` variable from the build script.
32 let _ = env!("BUILD_SCRIPT_TEST_VARIABLE");
33 }
34
rustc_minor_version() -> Option<u32>35 fn rustc_minor_version() -> Option<u32> {
36 let rustc = match env::var_os("RUSTC") {
37 Some(rustc) => rustc,
38 None => return None,
39 };
40
41 let output = match Command::new(rustc).arg("--version").output() {
42 Ok(output) => output,
43 Err(_) => return None,
44 };
45
46 let version = match str::from_utf8(&output.stdout) {
47 Ok(version) => version,
48 Err(_) => return None,
49 };
50
51 let mut pieces = version.split('.');
52 if pieces.next() != Some("rustc 1") {
53 return None;
54 }
55
56 let next = match pieces.next() {
57 Some(next) => next,
58 None => return None,
59 };
60
61 u32::from_str(next).ok()
62 }
63