• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 //! test_rlib_cargo_crate
17 use std::env;
18 use std::path::Path;
19 use std::io::Write;
20 use std::process::Command;
21 use std::str::{self, FromStr};
22 
main()23 fn main() {
24     println!("cargo:rustc-cfg=build_script_ran");
25     let my_minor = match rustc_minor_version() {
26         Some(my_minor) => my_minor,
27         None => return,
28     };
29 
30     if my_minor >= 34 {
31         println!("cargo:rustc-cfg=is_new_rustc");
32     } else {
33         println!("cargo:rustc-cfg=is_old_rustc");
34     }
35 
36     let target = env::var("TARGET").unwrap();
37 
38     if target.contains("ohos") {
39         println!("cargo:rustc-cfg=is_ohos");
40     }
41     if target.contains("darwin") {
42         println!("cargo:rustc-cfg=is_mac");
43     }
44 
45     let feature_a = env::var_os("CARGO_FEATURE_MY_FEATURE_A").is_some();
46     if feature_a {
47         println!("cargo:rustc-cfg=has_feature_a");
48     }
49     let feature_b = env::var_os("CARGO_FEATURE_MY_FEATURE_B").is_some();
50     if feature_b {
51         println!("cargo:rustc-cfg=has_feature_b");
52     }
53 
54     // Some tests as to whether we're properly emulating various cargo features.
55     assert!(Path::new("build.rs").exists());
56     assert!(Path::new(&env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("build.rs").exists());
57     assert!(Path::new(&env::var_os("OUT_DIR").unwrap()).exists());
58 
59     // Confirm the following env var is set
60     env::var_os("CARGO_CFG_TARGET_ARCH").unwrap();
61 
62     generate_some_code().unwrap();
63 }
64 
generate_some_code() -> std::io::Result<()>65 fn generate_some_code() -> std::io::Result<()> {
66     let test_output_dir = Path::new(&env::var_os("OUT_DIR").unwrap()).join("generated");
67     let _ = std::fs::create_dir_all(&test_output_dir);
68     // Test that environment variables from .gn files are passed to build scripts
69     let preferred_number = env::var("ENV_VAR_FOR_BUILD_SCRIPT").unwrap();
70     let mut file = std::fs::File::create(test_output_dir.join("generated.rs"))?;
71     write!(file, "fn run_some_generated_code() -> u32 {{ {} }}", preferred_number)?;
72     Ok(())
73 }
74 
rustc_minor_version() -> Option<u32>75 fn rustc_minor_version() -> Option<u32> {
76     let rustc_bin = match env::var_os("RUSTC") {
77         Some(rustc_bin) => rustc_bin,
78         None => return None,
79     };
80 
81     let output = match Command::new(rustc_bin).arg("--version").output() {
82         Ok(output) => output,
83         Err(_) => return None,
84     };
85 
86     let rustc_version = match str::from_utf8(&output.stdout) {
87         Ok(rustc_version) => rustc_version,
88         Err(_) => return None,
89     };
90 
91     let mut pieces = rustc_version.split('.');
92     if pieces.next() != Some("rustc 1") {
93         return None;
94     }
95 
96     let next_var = match pieces.next() {
97         Some(next_var) => next_var,
98         None => return None,
99     };
100 
101     u32::from_str(next_var).ok()
102 }
103