• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::io::Write;
7 use std::path::Path;
8 use std::process::Command;
9 use std::str::{self, FromStr};
10 
main()11 fn main() {
12     println!("cargo:rustc-cfg=build_script_ran");
13     let minor = match rustc_minor_version() {
14         Some(minor) => minor,
15         None => return,
16     };
17 
18     let target = env::var("TARGET").unwrap();
19 
20     if minor >= 34 {
21         println!("cargo:rustc-cfg=is_new_rustc");
22     } else {
23         println!("cargo:rustc-cfg=is_old_rustc");
24     }
25 
26     if target.contains("android") {
27         println!("cargo:rustc-cfg=is_android");
28     }
29     if target.contains("darwin") {
30         println!("cargo:rustc-cfg=is_mac");
31     }
32 
33     let feature_a_enabled = env::var_os("CARGO_FEATURE_MY_FEATURE_A").is_some();
34     if feature_a_enabled {
35         println!("cargo:rustc-cfg=has_feature_a");
36     }
37     let feature_b_enabled = env::var_os("CARGO_FEATURE_MY_FEATURE_B").is_some();
38     if feature_b_enabled {
39         println!("cargo:rustc-cfg=has_feature_b");
40     }
41 
42     // Some tests as to whether we're properly emulating various cargo features.
43     assert!(Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("build.rs").exists());
44     assert!(Path::new("build.rs").exists());
45     assert!(Path::new(&env::var_os("OUT_DIR").unwrap()).exists());
46     // Confirm the following env var is set, but do not attempt to validate content
47     // since the whole point is that it will differ on different platforms.
48     env::var_os("CARGO_CFG_TARGET_ARCH").unwrap();
49 
50     generate_some_code().unwrap();
51 }
52 
generate_some_code() -> std::io::Result<()>53 fn generate_some_code() -> std::io::Result<()> {
54     let output_dir = Path::new(&env::var_os("OUT_DIR").unwrap()).join("generated");
55     let _ = std::fs::create_dir_all(&output_dir);
56     // Test that environment variables from .gn files are passed to build scripts
57     let preferred_number = env::var("ENV_VAR_FOR_BUILD_SCRIPT").unwrap();
58     let mut file = std::fs::File::create(output_dir.join("generated.rs"))?;
59     write!(file, "fn run_some_generated_code() -> u32 {{ {} }}", preferred_number)?;
60     Ok(())
61 }
62 
rustc_minor_version() -> Option<u32>63 fn rustc_minor_version() -> Option<u32> {
64     let rustc = match env::var_os("RUSTC") {
65         Some(rustc) => rustc,
66         None => return None,
67     };
68 
69     let output = match Command::new(rustc).arg("--version").output() {
70         Ok(output) => output,
71         Err(_) => return None,
72     };
73 
74     let version = match str::from_utf8(&output.stdout) {
75         Ok(version) => version,
76         Err(_) => return None,
77     };
78 
79     let mut pieces = version.split('.');
80     if pieces.next() != Some("rustc 1") {
81         return None;
82     }
83 
84     let next = match pieces.next() {
85         Some(next) => next,
86         None => return None,
87     };
88 
89     u32::from_str(next).ok()
90 }
91