-
Notifications
You must be signed in to change notification settings - Fork 81
/
build.rs
272 lines (232 loc) · 7.35 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
extern crate target_build_utils;
extern crate walkdir;
use std::env;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::Command;
use target_build_utils::TargetInfo;
use walkdir::{DirEntry, WalkDir};
fn build_hermit(src_dir: &Path, target_dir_opt: Option<&Path>) {
assert!(
src_dir.exists(),
"rusty_hermit source folder does not exist"
);
let target = TargetInfo::new().expect("Could not get target info");
let profile = env::var("PROFILE").expect("PROFILE was not set");
let mut cmd = Command::new("cargo");
cmd.env_remove("RUSTUP_TOOLCHAIN");
if option_env!("RUSTC_WORKSPACE_WRAPPER")
.unwrap_or_default()
.ends_with("clippy-driver")
{
cmd.env("RUSTC_WORKSPACE_WRAPPER", "clippy-driver");
}
cmd.env("CARGO_TERM_COLOR", "always");
if target.target_arch() == "x86_64" {
cmd.current_dir(src_dir)
.arg("build")
.arg("-Z")
.arg("build-std=core,alloc")
.arg("--target")
.arg("x86_64-unknown-none-hermitkernel");
} else if target.target_arch() == "aarch64" {
cmd.current_dir(src_dir)
.arg("build")
.arg("-Z")
.arg("build-std=core,alloc")
.arg("--target")
.arg("aarch64-unknown-hermit");
} else {
panic!("Try to build for an unsupported platform");
}
if let Some(target_dir) = target_dir_opt {
cmd.arg("--target-dir").arg(target_dir);
}
let target_dir = match target_dir_opt {
Some(target_dir) => target_dir.to_path_buf(),
None => src_dir.join("target"),
};
if profile == "release" {
cmd.arg("--release");
}
// disable all default features
cmd.arg("--no-default-features");
if target.target_arch() == "aarch64" {
cmd.arg("--features");
cmd.arg("aarch64-qemu-stdout");
}
// do we have to enable PCI support?
#[cfg(feature = "pci")]
{
cmd.arg("--features");
cmd.arg("pci");
}
// do we have to enable acpi support?
#[cfg(feature = "acpi")]
{
cmd.arg("--features");
cmd.arg("acpi");
}
// do we have to enable FSGSBASE support?
#[cfg(feature = "fsgs_base")]
{
cmd.arg("--features");
cmd.arg("fsgs_base");
}
// do we support multi-processor systems?
#[cfg(feature = "smp")]
{
cmd.arg("--features");
cmd.arg("smp");
}
// do we have to enable VGA support
#[cfg(feature = "vga")]
{
cmd.arg("--features");
cmd.arg("vga");
}
let mut rustflags = vec!["-Zmutable-noalias=no".to_string()];
let outer_rustflags = env::var("CARGO_ENCODED_RUSTFLAGS").unwrap();
#[cfg(feature = "instrument")]
{
rustflags.push("-Zinstrument-mcount".to_string());
// Add outer rustflags to command
rustflags.push(outer_rustflags);
}
#[cfg(not(feature = "instrument"))]
{
// If the `instrument` feature feature is not enabled,
// filter it from outer rustflags before adding them to the command.
if !outer_rustflags.is_empty() {
let flags = outer_rustflags
.split('\x1f')
.filter(|&flag| !flag.contains("instrument-mcount"))
.map(String::from);
rustflags.extend(flags);
}
}
cmd.env("CARGO_ENCODED_RUSTFLAGS", rustflags.join("\x1f"));
let status = cmd.status().expect("failed to start kernel build");
assert!(status.success());
let lib_location = if target.target_arch() == "x86_64" {
target_dir
.join("x86_64-unknown-none-hermitkernel")
.join(&profile)
.canonicalize()
.unwrap() // Must exist after building
} else if target.target_arch() == "aarch64" {
target_dir
.join("aarch64-unknown-hermit")
.join(&profile)
.canonicalize()
.unwrap() // Must exist after building
} else {
panic!("Try to build for an unsupported platform");
};
println!("Lib location: {}", lib_location.display());
let lib = lib_location.join("libhermit.a");
rename_symbol("rust_begin_unwind", &lib);
rename_symbol("rust_oom", &lib);
println!("cargo:rustc-link-search=native={}", lib_location.display());
println!("cargo:rustc-link-lib=static=hermit");
// HERMIT_LOG_LEVEL_FILTER sets the log level filter at compile time
println!("cargo:rerun-if-env-changed=HERMIT_LOG_LEVEL_FILTER");
}
/// Kernel and user space has its own versions of panic handler, oom handler, memcpy, memset, etc,
/// Consequently, we rename the functions in the libos to avoid collisions.
fn rename_symbol(symbol: impl AsRef<OsStr>, lib: impl AsRef<Path>) {
let arg = IntoIterator::into_iter([symbol.as_ref(), "=kernel-".as_ref(), symbol.as_ref()])
.collect::<OsString>();
let status = Command::new("rust-objcopy")
.arg("--redefine-sym")
.arg(arg)
.arg(lib.as_ref())
.status()
.expect("Failed to execute rust-objcopy. Is cargo-binutils installed?");
assert!(status.success(), "rust-objcopy was not successful");
}
#[cfg(all(not(feature = "rustc-dep-of-std"), not(feature = "with_submodule")))]
fn build() {
#[cfg(windows)]
let out_dir = env::temp_dir();
#[cfg(not(windows))]
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let src_dir = out_dir.join("rusty-hermit");
if !src_dir.as_path().exists() {
let status = Command::new(env!("CARGO"))
.current_dir(out_dir)
.arg("download")
.arg("--output")
.arg(src_dir.clone().into_os_string())
.arg("--extract")
.arg("rusty-hermit")
.status()
.expect("failed to start kernel download");
assert!(
status.success(),
"Unable to download rusty-hermit. Is cargo-download installed?"
);
}
build_hermit(src_dir.as_ref(), None);
}
#[cfg(all(not(feature = "rustc-dep-of-std"), feature = "with_submodule"))]
fn build() {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let target_dir = out_dir.join("target");
let src_dir = env::current_dir()
.unwrap()
.parent()
.unwrap()
.join("libhermit-rs");
build_hermit(src_dir.as_ref(), Some(target_dir.as_ref()));
configure_cargo_rerun_if_changed(src_dir.as_ref());
}
#[cfg(all(not(feature = "rustc-dep-of-std"), feature = "with_submodule"))]
fn configure_cargo_rerun_if_changed(src_dir: &Path) {
fn is_not_ignored(entry: &DirEntry) -> bool {
// Ignore .git .vscode and target directories, but not .cargo or .github
if entry.depth() == 1
&& entry.path().is_dir()
&& (entry.path().ends_with("target")
|| entry.path().ends_with(".git")
|| entry.path().ends_with(".vscode"))
{
return false;
}
true
}
WalkDir::new(src_dir)
.into_iter()
.filter_entry(is_not_ignored)
.filter_map(|v| v.ok())
.filter_map(|v| v.path().canonicalize().ok())
.for_each(|x| println!("cargo:rerun-if-changed={}", x.display()));
}
#[cfg(not(feature = "rustc-dep-of-std"))]
fn create_constants() {
let out_dir = env::var("OUT_DIR").expect("No out dir");
let dest_path = Path::new(&out_dir).join("constants.rs");
let mut f = File::create(&dest_path).expect("Could not create file");
let ip = option_env!("HERMIT_IP");
let ip = ip.map_or("10.0.5.3", |v| v);
let gateway = option_env!("HERMIT_GATEWAY");
let gateway = gateway.map_or("10.0.5.1", |v| v);
let mask = option_env!("HERMIT_MASK");
let mask = mask.map_or("255.255.255.0", |v| v);
writeln!(&mut f, "const HERMIT_IP: &str = \"{}\";", ip).expect("Could not write file");
println!("cargo:rerun-if-env-changed=HERMIT_IP");
writeln!(&mut f, "const HERMIT_GATEWAY: &str = \"{}\";", gateway)
.expect("Could not write file");
println!("cargo:rerun-if-env-changed=HERMIT_GATEWAY");
writeln!(&mut f, "const HERMIT_MASK: &str = \"{}\";", mask).expect("Could not write file");
println!("cargo:rerun-if-env-changed=HERMIT_MASK");
}
fn main() {
#[cfg(not(feature = "rustc-dep-of-std"))]
create_constants();
#[cfg(not(feature = "rustc-dep-of-std"))]
build();
}