-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
138 lines (115 loc) · 3.68 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::ffi::OsString;
use std::fs::{copy, read_dir, DirEntry};
use std::path::PathBuf;
use std::process::Command;
fn cargo() -> Command {
Command::new(std::env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")))
}
fn main() {
if std::env::var("SKIP_BUILD").is_err() {
build_library("rustyrts-dynamic-rlib");
build_library("rustyrts-dynamic-runner");
install_rlib("rustyrts_dynamic_rlib", "rustyrts-dynamic-rlib");
install_staticlib("rustyrts_dynamic_runner", "rustyrts-dynamic-runner");
}
}
fn build_library(dir_name: &str) {
println!("cargo:warning=Building {dir_name}");
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let mut cmd = cargo();
let mut path = PathBuf::new();
path.push(dir);
path.push(dir_name);
cmd.current_dir(path);
cmd.arg("build");
cmd.arg("--release");
match cmd.status() {
Ok(exit) => {
if !exit.success() {
std::process::exit(exit.code().unwrap_or(42));
}
}
Err(ref e) => panic!("error while building {dir_name}: {e:?}"),
}
println!("cargo:rerun-if-changed={dir_name}");
}
fn install_rlib(name: &str, dir_name: &str) {
println!("cargo:warning=Installing {name}");
let dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let mut path = PathBuf::new();
path.push(dir);
path.push(dir_name);
path.push("target");
path.push("release");
path.push("deps");
let files: Vec<DirEntry> = read_dir(path)
.unwrap()
.filter_map(std::result::Result::ok)
.collect();
let rlib_file = find_file(&format!("lib{name}"), ".rlib", &files);
let cargo_home = get_cargo_home();
rlib_file
.ok_or(std::io::Error::new(
std::io::ErrorKind::NotFound,
"rlib file",
))
.and_then(|entry| {
let src = entry.path();
let mut dst = cargo_home.clone();
dst.push(format!("lib{name}.rlib"));
copy(src, dst)
})
.unwrap();
}
fn install_staticlib(name: &str, dir_name: &str) {
println!("cargo:warning=Installing {name}");
let mut dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
dir.push(dir_name);
dir.push("target");
dir.push("release");
let files: Vec<DirEntry> = read_dir(dir)
.unwrap()
.filter_map(std::result::Result::ok)
.collect();
let a_file = find_file(&format!("lib{name}"), ".a", &files);
let cargo_home = get_cargo_home();
a_file
.ok_or(std::io::Error::new(std::io::ErrorKind::NotFound, "a file"))
.and_then(|entry| {
let src = entry.path();
let mut dst = cargo_home.clone();
dst.push(format!("lib{name}.a"));
copy(src, dst)
})
.unwrap();
}
fn get_cargo_home() -> PathBuf {
let mut cargo_home = {
let maybe_cargo_home = std::env::var("CARGO_HOME");
if let Ok(cargo_home) = maybe_cargo_home {
PathBuf::from(cargo_home)
} else {
let home = std::env::var("HOME").expect("Unable to find HOME environment variable");
let mut path = PathBuf::new();
path.push(home);
path.push(".cargo");
path
}
};
cargo_home.push("bin");
cargo_home
}
fn find_file<'a>(
starts_with: &str,
ends_with: &str,
files: &'a [DirEntry],
) -> Option<&'a DirEntry> {
files.iter().find(|file| {
let file_name_os = file.file_name();
let file_name = file_name_os.to_str().unwrap();
if file_name.starts_with(starts_with) && file_name.ends_with(ends_with) {
return true;
}
false
})
}