-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
90 lines (77 loc) · 2.69 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2022 Huawei Technologies Co.,Ltd. All rights reserved.
//
// sysMaster is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan
// PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//! do prepared actions for build
// if use env out_dir need add build.rs
use std::{env, fs, path::Path};
const RELEASE: &str = "release";
macro_rules! warn {
($message:expr) => {
println!("cargo:warning={}", $message);
};
}
fn copy_directory(src: &Path, dst: &Path) -> std::io::Result<()> {
if src.is_dir() {
if !dst.exists() {
fs::create_dir(dst)?;
}
for entry in fs::read_dir(src)? {
let entry = entry?;
let entry_path = entry.path();
if let Some(entry_name) = entry_path.file_name() {
let dst_path = dst.join(entry_name);
if entry_path.is_dir() {
copy_directory(&entry_path, &dst_path)?;
} else {
fs::copy(&entry_path, &dst_path)?;
}
}
}
} else {
fs::copy(src, dst)?;
}
Ok(())
}
fn main() {
// pre install git hooks
let hooks = vec!["pre-commit", "commit-msg"];
for hook in hooks {
let source_file = format!("ci/{}", hook);
let target_path = Path::new(".git/hooks/");
if !target_path.exists() {
let _ = std::fs::create_dir_all(target_path);
}
let target_file = target_path.join(hook);
if !target_file.exists() {
let _ = std::fs::copy(&source_file, target_path.join(hook));
}
}
// copy test config
let args: Vec<&str> = vec!["tests/presets", "tests/test_units"];
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let out_dir = env::var("OUT_DIR").unwrap();
let t: Vec<_> = out_dir.split("build").collect();
let dst = t[0];
for arg in args {
let src = format!("{}/{}", manifest_dir, arg);
if let Err(e) = copy_directory(Path::new(&src), Path::new(dst)) {
warn!(format!("{:?}", e));
}
}
// set rerun
println!("cargo:rerun-if-changed=build.sh");
println!("cargo:rerun-if-changed=build.rs");
// turn on "debug" for non-release build
let profile = env::var("PROFILE").unwrap();
if profile != RELEASE {
println!("cargo:rustc-cfg=debug");
}
}