-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
281 lines (258 loc) · 9.08 KB
/
build.rs
File metadata and controls
281 lines (258 loc) · 9.08 KB
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
273
274
275
276
277
278
279
280
281
use std::{
fs::File,
io::Write,
path::{Path, PathBuf},
};
use serde::Deserialize;
fn status_msg(msg: String) {
std::io::stdout().write_all(msg.as_bytes()).unwrap();
}
fn build_jit_test_code(func_to_call: &str, indir: &Path, outfile_path: &Path) {
let mut outfile_handle = File::create(outfile_path).unwrap();
status_msg(format!(
"building tests from {} into {}\n",
indir.display(),
outfile_path.display()
));
let iter = std::fs::read_dir(indir);
if iter.is_err() {
return;
}
let iter = iter.unwrap();
for entry in iter {
let entry = entry.unwrap();
if !entry.file_type().unwrap().is_file() {
continue;
}
let entry_path = entry.path();
let is_cpm_file = entry_path.extension().map_or(false, |ext| ext == "cpm");
if !is_cpm_file {
panic!(
"unexpected !cpm file {} in JIT test tree",
entry_path.display()
);
}
let entry_path = entry_path.canonicalize().unwrap();
let test_name = entry_path.file_stem().unwrap().to_str().unwrap();
let content = format!(
"#[test]\nfn jit_{test_name}() {{ let pf = PathBuf::from(\"{}\"); {func_to_call}(pf); }}\n",
entry_path.as_os_str().to_str().unwrap()
);
write!(outfile_handle, "{content}").unwrap();
}
}
fn build_jit_tests(indir: &Path, outdir: &Path, pass: bool) {
let mut outfile_path = PathBuf::from(outdir);
let outfile_name = format!("test_jit{}.rs", if pass { "pass" } else { "fail" });
outfile_path.push(outfile_name);
let func_to_call = if pass {
"expect_jit_pass"
} else {
"expect_jit_fail"
};
build_jit_test_code(func_to_call, indir, &outfile_path);
}
#[derive(Deserialize)]
struct DriverTestConfig {
source_files: Vec<String>,
bom: bool,
args: Option<Vec<String>>,
diags_match: Option<Vec<String>>,
diags_no_match: Option<Vec<String>>,
stdout_match: Option<Vec<String>>,
stderr_match: Option<Vec<String>>,
}
fn opt_vec_to_opt_str(inp: &Option<Vec<String>>) -> Option<String> {
inp.as_ref().map(|x| {
x.iter()
.map(|x| format!("\"{x}\".to_string()"))
.collect::<Vec<String>>()
.join(",")
})
}
fn opt_vec_to_opt_vec(inp: &Option<Vec<String>>, name: &str) -> String {
opt_str_to_opt_vec(&opt_vec_to_opt_str(inp), name)
}
fn opt_str_to_opt_vec(os: &Option<String>, name: &str) -> String {
format!(
" let {name}: Option<Vec<String>> = {};\n",
if let Some(s) = os {
format!("Some(vec![{s}])")
} else {
String::from("None")
}
)
}
#[allow(clippy::format_in_format_args)]
fn build_driver_test_code(func_to_call: &str, indir: &Path, outfile_path: &Path) {
let mut outfile_handle = File::create(outfile_path).unwrap();
status_msg(format!(
"building tests from {} into {}\n",
indir.display(),
outfile_path.display()
));
let iter = std::fs::read_dir(indir);
if iter.is_err() {
return;
}
let iter = iter.unwrap();
for entry in iter {
let entry = entry.unwrap();
if !entry.file_type().unwrap().is_dir() {
continue;
}
let entry_path = entry.path().canonicalize().unwrap();
let test_name = entry_path.file_name().unwrap().to_str().unwrap();
let test_json_path = entry_path.join("test.json");
if !test_json_path.exists() {
panic!(
"directory {} does not contain a test configuration file",
entry_path.display()
);
}
let test_json_str = std::fs::read_to_string(test_json_path).unwrap();
let test_descriptor: DriverTestConfig = serde_json::from_str(&test_json_str).unwrap();
let args = opt_vec_to_opt_vec(&test_descriptor.args, "args");
let diags_match = opt_vec_to_opt_str(&test_descriptor.diags_match);
let diags_no_match = opt_vec_to_opt_str(&test_descriptor.diags_no_match);
let stdout_match = opt_vec_to_opt_str(&test_descriptor.stdout_match);
let stderr_match = opt_vec_to_opt_str(&test_descriptor.stderr_match);
let sources: Vec<PathBuf> = test_descriptor
.source_files
.iter()
.map(|sfn| entry_path.join(sfn).canonicalize().unwrap())
.collect();
let sources = sources
.iter()
.map(|x| format!("PathBuf::from(\"{}\")", x.display()))
.collect::<Vec<String>>()
.join(",");
let dest = entry_path.join("a.out").display().to_string();
write!(
outfile_handle,
"{}",
format!("#[test]\nfn driver_{}() {{\n", test_name)
)
.expect("<io error>");
write!(outfile_handle, "{}", args).expect("<io error>");
write!(
outfile_handle,
"{}",
format!(" let sources: Vec<PathBuf> = vec![{sources}];\n")
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
opt_str_to_opt_vec(&diags_match, "diags_match")
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
opt_str_to_opt_vec(&diags_no_match, "diags_no_match")
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
opt_str_to_opt_vec(&stdout_match, "stdout_match")
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
opt_str_to_opt_vec(&stderr_match, "stderr_match")
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
format!(" let dest: PathBuf = PathBuf::from(\"{dest}\");\n")
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
format!(" remove_stale_files(&dest);\n")
)
.expect("<io error>");
write!(outfile_handle, "{}", format!(" let opts = CompilerOptions{{ optimize: true, dump_bom:{}, ..Default::default() }};\n", test_descriptor.bom)).expect("<io error>");
write!(
outfile_handle,
"{}",
format!(
" {func_to_call}(&sources, &dest, &args, &opts, &diags_match, &diags_no_match, &stdout_match, &stderr_match);"
)
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
format!(" remove_stale_files(&dest);\n")
)
.expect("<io error>");
write!(outfile_handle, "{}", format!(" let opts = CompilerOptions{{ optimize: false, dump_bom:{}, ..Default::default() }};\n", test_descriptor.bom)).expect("<io error>");
write!(
outfile_handle,
"{}",
format!(
" {func_to_call}(&sources, &dest, &args, &opts, &diags_match, &diags_no_match, &stdout_match, &stderr_match);"
)
)
.expect("<io error>");
write!(
outfile_handle,
"{}",
format!(" remove_stale_files(&dest);\n")
)
.expect("<io error>");
write!(outfile_handle, "{}", format!("}}\n")).expect("<io error>");
}
}
fn build_driver_tests(indir: &Path, outdir: &Path, pass: bool) {
let mut outfile_path = PathBuf::from(outdir);
let outfile_name = format!("test_driver{}.rs", if pass { "pass" } else { "fail" });
outfile_path.push(outfile_name);
let func_to_call = if pass {
"expect_driver_pass"
} else {
"expect_driver_fail"
};
build_driver_test_code(func_to_call, indir, &outfile_path);
}
fn build_tests(indir: &mut Path, outdir: &Path) {
status_msg(format!(
"building C± tests from {} into {}\n",
indir.display(),
outdir.display()
));
let mut jit_pass_indir = indir.to_path_buf();
let mut jit_fail_indir = indir.to_path_buf();
jit_pass_indir.push("jit");
jit_pass_indir.push("pass");
jit_fail_indir.push("jit");
jit_fail_indir.push("fail");
build_jit_tests(&jit_pass_indir, outdir, true);
build_jit_tests(&jit_fail_indir, outdir, false);
let mut driver_pass_indir = indir.to_path_buf();
let mut driver_fail_indir = indir.to_path_buf();
driver_pass_indir.push("driver");
driver_pass_indir.push("pass");
driver_fail_indir.push("driver");
driver_fail_indir.push("fail");
build_driver_tests(&driver_pass_indir, outdir, true);
build_driver_tests(&driver_fail_indir, outdir, false);
}
fn main() {
// if these variables are missing, bail out quick
let env_manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
let env_out_dir = std::env::var("OUT_DIR").unwrap();
let path_manifest_dir = Path::new(&env_manifest_dir);
let path_out_dir = Path::new(&env_out_dir);
let mut path_manifest_dir = PathBuf::from(path_manifest_dir);
let path_out_dir = PathBuf::from(path_out_dir);
path_manifest_dir.push("tests");
build_tests(&mut path_manifest_dir, &path_out_dir);
println!("cargo:rerun-if-changed=tests");
}