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 use std::env;
17 use std::process::Command;
18 use std::str::{self, FromStr};
19 
main()20 fn main() {
21     println!("cargo:rustc-cfg=build_script_ran");
22     let my_minor = match rustc_minor_version() {
23         Some(my_minor) => my_minor,
24         None => return,
25     };
26 
27     if my_minor >= 34 {
28         println!("cargo:rustc-cfg=is_new_rustc");
29     } else {
30         println!("cargo:rustc-cfg=is_old_rustc");
31     }
32 
33     let target = env::var("TARGET").unwrap();
34 
35     if target.contains("ohos") {
36         println!("cargo:rustc-cfg=is_ohos");
37     }
38     if target.contains("darwin") {
39         println!("cargo:rustc-cfg=is_mac");
40     }
41 
42     // Check that we can get a `rustenv` variable from the build script.
43     let _ = env!("BUILD_SCRIPT_TEST_VARIABLE");
44 }
45 
rustc_minor_version() -> Option<u32>46 fn rustc_minor_version() -> Option<u32> {
47     let rustc_bin = match env::var_os("RUSTC") {
48         Some(rustc_bin) => rustc_bin,
49         None => return None,
50     };
51 
52     let output = match Command::new(rustc_bin).arg("--version").output() {
53         Ok(output) => output,
54         Err(_) => return None,
55     };
56 
57     let rustc_version = match str::from_utf8(&output.stdout) {
58         Ok(rustc_version) => rustc_version,
59         Err(_) => return None,
60     };
61 
62     let mut pieces = rustc_version.split('.');
63     if pieces.next() != Some("rustc 1") {
64         return None;
65     }
66 
67     let next_var = match pieces.next() {
68         Some(next_var) => next_var,
69         None => return None,
70     };
71 
72     u32::from_str(next_var).ok()
73 }
74