-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
144 lines (127 loc) · 4.01 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
use std::{path::PathBuf, process::Command};
fn main() {
generate_ona_bindings();
compile_and_link_ona();
}
fn generate_ona_bindings() {
let cwd = std::env::current_dir().unwrap();
let include_path = cwd.join("ona/src");
let bindings = bindgen::builder()
.header("ona/src/NAR.h")
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.clang_args(["-I", include_path.to_str().unwrap()])
.allowlist_file(cwd.join("ona/src/.*\\.h").to_str().unwrap())
.generate()
.expect("Unable to generate ONA bindings.");
bindings
.write_to_file(PathBuf::from("src").join("sys.rs"))
.expect("Unable to write bindings to file");
}
fn compile_and_link_ona() {
let out_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let root_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
std::env::set_current_dir(&out_dir).unwrap();
let main_c_path = root_dir.join("ona/src/main.c");
let nar_first_stage_exe_path = out_dir.join("NAR_first_stage");
let rule_table_path = root_dir.join("ona/src/RuleTable.c");
// Source list
let sources = std::fs::read_dir(root_dir.join("ona/src"))
.unwrap()
.chain(std::fs::read_dir(root_dir.join("ona/src/NetworkNAR")).unwrap())
.filter_map(|entry| {
let entry = entry.unwrap();
if entry.file_type().unwrap().is_file()
&& entry.file_name().to_str().unwrap().ends_with(".c")
&& entry.file_name().to_str().unwrap() != "main.c"
{
Some(entry.path())
} else {
None
}
})
.collect::<Vec<_>>();
// for source in sources
// .iter()
// .chain([&main_c_path, &rule_table_path].into_iter())
// {
// println!("cargo:rerun-if-changed={}", source.to_str().unwrap());
// }
// Common arguments
let base_args = [
// Base args
"-D_POSIX_C_SOURCE=199506L",
"-pedantic",
"-std=c99",
"-pthread",
"-lpthread",
"-lm",
// Ignore warnings
"-Wno-unknown-pragmas",
"-Wno-tautological-compare",
"-Wno-dollar-in-identifier-extension",
"-Wno-unused-parameter",
"-Wno-unused-variable",
];
//
// Stage 1
//
// TODO: detect if the SSE build fails, and then build without SSE.
Command::new("cc")
.args([
"-mfpmath=sse",
"-msse2",
"-DSTAGE=1",
"-Wall",
"-Wextra",
"-Wformat-security",
])
.args(&sources)
.arg(root_dir.join("ona/src/main.c"))
.args(base_args)
.arg(format!("-o{}", nar_first_stage_exe_path.to_str().unwrap()))
.run();
// Generate rule table
let rule_table = Command::new(nar_first_stage_exe_path)
.arg("NAL_GenerateRuleTable")
.output()
.unwrap()
.stdout;
std::fs::write(rule_table_path, rule_table).unwrap();
//
// Stage 2
//
Command::new("cc")
.args(["-mfpmath=sse", "-msse2", "-c", "-DSTAGE=2"])
.args(&sources)
.arg(root_dir.join("ona/src/RuleTable.c"))
.args(base_args)
.run();
let objects = std::fs::read_dir(&out_dir)
.unwrap()
.filter_map(|entry| {
let entry = entry.unwrap();
if entry.file_name().to_str().unwrap().ends_with(".o") {
Some(entry.path())
} else {
None
}
})
.collect::<Vec<_>>();
Command::new("ar")
.args(["rcs", "libONA.a"])
.args(objects)
.run();
println!("cargo:rustc-link-search={}", out_dir.to_str().unwrap());
println!("cargo:rustc-link-lib=static=ONA");
}
trait CommandExt {
fn run(&mut self);
}
impl CommandExt for Command {
fn run(&mut self) {
let cmd = format!("{self:?}");
if !self.spawn().unwrap().wait().unwrap().success() {
panic!("Command failed: {cmd}");
}
}
}