Skip to content

Commit 7e1777a

Browse files
Split compile from run
1 parent 9052259 commit 7e1777a

1 file changed

Lines changed: 114 additions & 75 deletions

File tree

tests/lang_tests.rs

Lines changed: 114 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ use std::process::Command;
99
use lang_tester::LangTester;
1010
use tempfile::TempDir;
1111

12-
fn compile_and_run_cmds(
12+
fn compile_cmd(
1313
compiler_args: Vec<String>,
1414
test_target: &Option<String>,
15-
exe: &Path,
16-
) -> Vec<(&'static str, Command)> {
15+
) -> (&'static str, Command) {
1716
let mut compiler = Command::new("rustc");
1817
compiler.args(compiler_args);
1918

@@ -23,7 +22,13 @@ fn compile_and_run_cmds(
2322
// TODO(antoyo): find a better way to add the PATH necessary locally.
2423
env_path = format!("/opt/m68k-unknown-linux-gnu/bin:{}", env_path);
2524
compiler.env("PATH", env_path);
25+
}
26+
("Compiler", compiler)
27+
}
2628

29+
fn run_cmd(exe: &Path, test_target: &Option<String>) -> (&'static str, Command) {
30+
// Test command 2: run `tempdir/x`.
31+
if test_target.is_some() {
2732
let vm_parent_dir = std::env::var("CG_GCC_VM_DIR")
2833
.map(PathBuf::from)
2934
.unwrap_or_else(|_| std::env::current_dir().unwrap());
@@ -41,15 +46,107 @@ fn compile_and_run_cmds(
4146
runtime.args(["chroot", vm_dir, "qemu-m68k-static"]);
4247
runtime.arg(inside_vm_exe_path);
4348
runtime.current_dir(vm_parent_dir);
44-
vec![("Compiler", compiler), ("Copy", copy), ("Run-time", runtime)]
49+
("Run-time", runtime)
4550
} else {
4651
let runtime = Command::new(exe);
47-
vec![("Compiler", compiler), ("Run-time", runtime)]
52+
("Run-time", runtime)
53+
}
54+
}
55+
56+
fn extract_test(path: &Path) -> String {
57+
std::fs::read_to_string(path)
58+
.expect("read file")
59+
.lines()
60+
.skip_while(|l| !l.starts_with("//"))
61+
.take_while(|l| l.starts_with("//"))
62+
.map(|l| &l[2..])
63+
.collect::<Vec<_>>()
64+
.join("\n")
65+
}
66+
67+
fn compile_and_run(
68+
path: &Path,
69+
tempdir: &TempDir,
70+
current_dir: &str,
71+
) -> Vec<(&'static str, Command)> {
72+
compile_and_or_run(path, tempdir, current_dir, true)
73+
}
74+
75+
fn compile(path: &Path, tempdir: &TempDir, current_dir: &str) -> Vec<(&'static str, Command)> {
76+
compile_and_or_run(path, tempdir, current_dir, false)
77+
}
78+
79+
fn compile_and_or_run(
80+
path: &Path,
81+
tempdir: &TempDir,
82+
current_dir: &str,
83+
run: bool,
84+
) -> Vec<(&'static str, Command)> {
85+
// TODO(antoyo): find a way to send this via a cli argument.
86+
let test_target = std::env::var("CG_GCC_TEST_TARGET").ok();
87+
88+
// Test command 1: Compile `x.rs` into `tempdir/x`.
89+
let mut exe = PathBuf::new();
90+
exe.push(tempdir);
91+
exe.push(path.file_stem().expect("file_stem"));
92+
let mut compiler_args = vec![
93+
format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir),
94+
"--sysroot".into(),
95+
format!("{}/build/build_sysroot/sysroot/", current_dir),
96+
"-C".into(),
97+
"link-arg=-lc".into(),
98+
"--extern".into(),
99+
"mini_core=target/out/libmini_core.rlib".into(),
100+
"-o".into(),
101+
exe.to_str().expect("to_str").into(),
102+
path.to_str().expect("to_str").into(),
103+
];
104+
105+
if let Some(ref target) = test_target {
106+
compiler_args.extend_from_slice(&["--target".into(), target.into()]);
107+
108+
let linker = format!("{}-gcc", target);
109+
compiler_args.extend_from_slice(&[format!("-Clinker={}", linker)]);
110+
}
111+
112+
if let Some(flags) = option_env!("TEST_FLAGS") {
113+
for flag in flags.split_whitespace() {
114+
compiler_args.push(flag.into());
115+
}
116+
}
117+
let mut debug_args = compiler_args.clone();
118+
if test_target.is_some() {
119+
// m68k doesn't have lubsan for now
120+
debug_args.extend_from_slice(&["-C".to_string(), "llvm-args=sanitize-undefined".into()]);
121+
} else {
122+
debug_args.extend_from_slice(&[
123+
"-C".to_string(),
124+
"llvm-args=sanitize-undefined".into(),
125+
"-C".into(),
126+
"link-args=-lubsan".into(),
127+
]);
128+
}
129+
130+
compiler_args.extend_from_slice(&[
131+
"-C".into(),
132+
"opt-level=3".into(),
133+
"-C".into(),
134+
"lto=no".into(),
135+
]);
136+
137+
let mut parts = Vec::new();
138+
parts.push(compile_cmd(debug_args, &test_target));
139+
if run {
140+
parts.push(run_cmd(&exe, &test_target));
48141
}
142+
parts.push(compile_cmd(compiler_args, &test_target));
143+
if run {
144+
parts.push(run_cmd(&exe, &test_target));
145+
}
146+
parts
49147
}
50148

51149
fn main() {
52-
let tempdir = TempDir::new().expect("temp dir");
53150
let current_dir = current_dir().expect("current dir");
54151
let current_dir = current_dir.to_str().expect("current dir").to_string();
55152

@@ -71,78 +168,20 @@ fn main() {
71168
}
72169
rust_filter(filename)
73170
}
74-
// TODO(antoyo): find a way to send this via a cli argument.
75-
let test_target = std::env::var("CG_GCC_TEST_TARGET").ok();
76171

172+
let tempdir = TempDir::new().expect("temp dir");
173+
let current_dir1 = current_dir.clone();
77174
LangTester::new()
78175
.test_dir("tests/run")
79176
.test_path_filter(filter)
80-
.test_extract(|path| {
81-
std::fs::read_to_string(path)
82-
.expect("read file")
83-
.lines()
84-
.skip_while(|l| !l.starts_with("//"))
85-
.take_while(|l| l.starts_with("//"))
86-
.map(|l| &l[2..])
87-
.collect::<Vec<_>>()
88-
.join("\n")
89-
})
90-
.test_cmds(move |path| {
91-
// Test command 1: Compile `x.rs` into `tempdir/x`.
92-
let mut exe = PathBuf::new();
93-
exe.push(&tempdir);
94-
exe.push(path.file_stem().expect("file_stem"));
95-
let mut compiler_args = vec![
96-
format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir),
97-
"--sysroot".into(),
98-
format!("{}/build/build_sysroot/sysroot/", current_dir),
99-
"-C".into(),
100-
"link-arg=-lc".into(),
101-
"--extern".into(),
102-
"mini_core=target/out/libmini_core.rlib".into(),
103-
"-o".into(),
104-
exe.to_str().expect("to_str").into(),
105-
path.to_str().expect("to_str").into(),
106-
];
107-
108-
if let Some(ref target) = test_target {
109-
compiler_args.extend_from_slice(&["--target".into(), target.into()]);
110-
111-
let linker = format!("{}-gcc", target);
112-
compiler_args.extend_from_slice(&[format!("-Clinker={}", linker)]);
113-
}
114-
115-
if let Some(flags) = option_env!("TEST_FLAGS") {
116-
for flag in flags.split_whitespace() {
117-
compiler_args.push(flag.into());
118-
}
119-
}
120-
let mut debug_args = compiler_args.clone();
121-
if test_target.is_some() {
122-
// m68k doesn't have lubsan for now
123-
debug_args
124-
.extend_from_slice(&["-C".to_string(), "llvm-args=sanitize-undefined".into()]);
125-
} else {
126-
debug_args.extend_from_slice(&[
127-
"-C".to_string(),
128-
"llvm-args=sanitize-undefined".into(),
129-
"-C".into(),
130-
"link-args=-lubsan".into(),
131-
]);
132-
}
133-
134-
compiler_args.extend_from_slice(&[
135-
"-C".into(),
136-
"opt-level=3".into(),
137-
"-C".into(),
138-
"lto=no".into(),
139-
]);
140-
141-
let mut parts = compile_and_run_cmds(debug_args, &test_target, &exe);
142-
for part in compile_and_run_cmds(compiler_args, &test_target, &exe) {
143-
parts.push(part);
144-
}
145-
parts
146-
})
177+
.test_extract(extract_test)
178+
.test_cmds(move |path| compile_and_run(path, &tempdir, &current_dir1))
179+
.run();
180+
let tempdir = TempDir::new().expect("temp dir");
181+
LangTester::new()
182+
.test_dir("tests/compile")
183+
.test_path_filter(filter)
184+
.test_extract(extract_test)
185+
.test_cmds(move |path| compile(path, &tempdir, &current_dir))
147186
.run();
148187
}

0 commit comments

Comments
 (0)