• 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     let cargo_manifest_dir = &env::var_os("CARGO_MANIFEST_DIR").unwrap();
44     let manifest_dir_path = Path::new(cargo_manifest_dir);
45     assert!(
46         !manifest_dir_path.is_absolute(),
47         "CARGO_MANIFEST_DIR={} should be relative path for build cache sharing.",
48         manifest_dir_path.display()
49     );
50     assert!(manifest_dir_path.join("build.rs").exists());
51     assert!(Path::new("build.rs").exists());
52     assert!(Path::new(&env::var_os("OUT_DIR").unwrap()).exists());
53     // Confirm the following env var is set, but do not attempt to validate content
54     // since the whole point is that it will differ on different platforms.
55     env::var_os("CARGO_CFG_TARGET_ARCH").unwrap();
56 
57     generate_some_code().unwrap();
58 }
59 
generate_some_code() -> std::io::Result<()>60 fn generate_some_code() -> std::io::Result<()> {
61     let output_dir = Path::new(&env::var_os("OUT_DIR").unwrap()).join("generated");
62     let _ = std::fs::create_dir_all(&output_dir);
63     // Test that environment variables from .gn files are passed to build scripts
64     let preferred_number = env::var("ENV_VAR_FOR_BUILD_SCRIPT").unwrap();
65     let mut file = std::fs::File::create(output_dir.join("generated.rs"))?;
66     write!(file, "fn run_some_generated_code() -> u32 {{ {} }}", preferred_number)?;
67     Ok(())
68 }
69 
rustc_minor_version() -> Option<u32>70 fn rustc_minor_version() -> Option<u32> {
71     let rustc = match env::var_os("RUSTC") {
72         Some(rustc) => rustc,
73         None => return None,
74     };
75 
76     let output = match Command::new(rustc).arg("--version").output() {
77         Ok(output) => output,
78         Err(_) => return None,
79     };
80 
81     let version = match str::from_utf8(&output.stdout) {
82         Ok(version) => version,
83         Err(_) => return None,
84     };
85 
86     let mut pieces = version.split('.');
87     if pieces.next() != Some("rustc 1") {
88         return None;
89     }
90 
91     let next = match pieces.next() {
92         Some(next) => next,
93         None => return None,
94     };
95 
96     u32::from_str(next).ok()
97 }
98