2022-06-06 21:04:37 -05:00
|
|
|
//! The common code for `tests/lang_tests_*.rs`
|
2020-05-10 09:54:30 -05:00
|
|
|
use std::{
|
|
|
|
env::{self, current_dir},
|
2023-10-26 16:42:02 -05:00
|
|
|
path::{Path, PathBuf},
|
2020-05-10 09:54:30 -05:00
|
|
|
process::Command,
|
|
|
|
};
|
|
|
|
|
2024-03-05 12:58:36 -06:00
|
|
|
use boml::Toml;
|
2020-05-10 09:54:30 -05:00
|
|
|
use lang_tester::LangTester;
|
|
|
|
use tempfile::TempDir;
|
|
|
|
|
2022-06-06 21:04:37 -05:00
|
|
|
/// Controls the compile options (e.g., optimization level) used to compile
|
|
|
|
/// test code.
|
|
|
|
#[allow(dead_code)] // Each test crate picks one variant
|
|
|
|
pub enum Profile {
|
|
|
|
Debug,
|
|
|
|
Release,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main_inner(profile: Profile) {
|
2020-05-10 09:54:30 -05:00
|
|
|
let tempdir = TempDir::new().expect("temp dir");
|
|
|
|
let current_dir = current_dir().expect("current dir");
|
|
|
|
let current_dir = current_dir.to_str().expect("current dir").to_string();
|
2024-03-05 12:58:36 -06:00
|
|
|
let toml = Toml::parse(include_str!("../config.toml")).expect("Failed to parse `config.toml`");
|
|
|
|
let gcc_path = if let Ok(gcc_path) = toml.get_string("gcc-path") {
|
|
|
|
PathBuf::from(gcc_path.to_string())
|
|
|
|
} else {
|
|
|
|
// then we try to retrieve it from the `target` folder.
|
|
|
|
let commit = include_str!("../libgccjit.version").trim();
|
|
|
|
Path::new("build/libgccjit").join(commit)
|
|
|
|
};
|
|
|
|
|
|
|
|
let gcc_path = Path::new(&gcc_path)
|
|
|
|
.canonicalize()
|
|
|
|
.expect("failed to get absolute path of `gcc-path`")
|
|
|
|
.display()
|
|
|
|
.to_string();
|
2020-05-10 09:54:30 -05:00
|
|
|
env::set_var("LD_LIBRARY_PATH", gcc_path);
|
2023-10-26 16:42:02 -05:00
|
|
|
|
2024-03-05 12:58:36 -06:00
|
|
|
fn rust_filter(path: &Path) -> bool {
|
|
|
|
path.is_file() && path.extension().expect("extension").to_str().expect("to_str") == "rs"
|
2023-10-26 16:42:02 -05:00
|
|
|
}
|
|
|
|
|
2024-03-05 12:58:36 -06:00
|
|
|
#[cfg(feature = "master")]
|
2023-10-26 16:42:02 -05:00
|
|
|
fn filter(filename: &Path) -> bool {
|
|
|
|
rust_filter(filename)
|
|
|
|
}
|
|
|
|
|
2024-03-05 12:58:36 -06:00
|
|
|
#[cfg(not(feature = "master"))]
|
2023-10-26 16:42:02 -05:00
|
|
|
fn filter(filename: &Path) -> bool {
|
|
|
|
if let Some(filename) = filename.to_str() {
|
|
|
|
if filename.ends_with("gep.rs") {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rust_filter(filename)
|
|
|
|
}
|
|
|
|
|
2020-05-10 09:54:30 -05:00
|
|
|
LangTester::new()
|
|
|
|
.test_dir("tests/run")
|
2024-03-05 12:58:36 -06:00
|
|
|
.test_path_filter(filter)
|
|
|
|
.test_extract(|path| {
|
|
|
|
let lines = std::fs::read_to_string(path)
|
|
|
|
.expect("read file")
|
|
|
|
.lines()
|
|
|
|
.skip_while(|l| !l.starts_with("//"))
|
|
|
|
.take_while(|l| l.starts_with("//"))
|
|
|
|
.map(|l| &l[2..])
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.join("\n");
|
|
|
|
lines
|
2020-05-10 09:54:30 -05:00
|
|
|
})
|
|
|
|
.test_cmds(move |path| {
|
|
|
|
// Test command 1: Compile `x.rs` into `tempdir/x`.
|
|
|
|
let mut exe = PathBuf::new();
|
|
|
|
exe.push(&tempdir);
|
|
|
|
exe.push(path.file_stem().expect("file_stem"));
|
|
|
|
let mut compiler = Command::new("rustc");
|
2024-03-05 12:58:36 -06:00
|
|
|
compiler.args([
|
2020-05-10 09:54:30 -05:00
|
|
|
&format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir),
|
2024-03-05 12:58:36 -06:00
|
|
|
"--sysroot",
|
|
|
|
&format!("{}/build_sysroot/sysroot/", current_dir),
|
2020-05-10 09:54:30 -05:00
|
|
|
"-Zno-parallel-llvm",
|
2024-03-05 12:58:36 -06:00
|
|
|
"-C",
|
|
|
|
"link-arg=-lc",
|
|
|
|
"-o",
|
|
|
|
exe.to_str().expect("to_str"),
|
2020-05-10 09:54:30 -05:00
|
|
|
path.to_str().expect("to_str"),
|
|
|
|
]);
|
2023-10-26 16:42:02 -05:00
|
|
|
|
|
|
|
// TODO(antoyo): find a way to send this via a cli argument.
|
|
|
|
let test_target = std::env::var("CG_GCC_TEST_TARGET");
|
|
|
|
if let Ok(ref target) = test_target {
|
2024-03-05 12:58:36 -06:00
|
|
|
compiler.args(["--target", target]);
|
2023-10-26 16:42:02 -05:00
|
|
|
let linker = format!("{}-gcc", target);
|
|
|
|
compiler.args(&[format!("-Clinker={}", linker)]);
|
|
|
|
let mut env_path = std::env::var("PATH").unwrap_or_default();
|
|
|
|
// TODO(antoyo): find a better way to add the PATH necessary locally.
|
|
|
|
env_path = format!("/opt/m68k-unknown-linux-gnu/bin:{}", env_path);
|
|
|
|
compiler.env("PATH", env_path);
|
|
|
|
}
|
|
|
|
|
2023-03-05 11:03:19 -06:00
|
|
|
if let Some(flags) = option_env!("TEST_FLAGS") {
|
|
|
|
for flag in flags.split_whitespace() {
|
2024-03-05 12:58:36 -06:00
|
|
|
compiler.arg(flag);
|
2023-03-05 11:03:19 -06:00
|
|
|
}
|
|
|
|
}
|
2022-06-06 21:04:37 -05:00
|
|
|
match profile {
|
|
|
|
Profile::Debug => {}
|
|
|
|
Profile::Release => {
|
2024-03-05 12:58:36 -06:00
|
|
|
compiler.args(["-C", "opt-level=3", "-C", "lto=no"]);
|
2022-06-06 21:04:37 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-10 09:54:30 -05:00
|
|
|
// Test command 2: run `tempdir/x`.
|
2023-10-26 16:42:02 -05:00
|
|
|
if test_target.is_ok() {
|
|
|
|
let vm_parent_dir = std::env::var("CG_GCC_VM_DIR")
|
2024-03-05 12:58:36 -06:00
|
|
|
.map(PathBuf::from)
|
2023-10-26 16:42:02 -05:00
|
|
|
.unwrap_or_else(|_| std::env::current_dir().unwrap());
|
|
|
|
let vm_dir = "vm";
|
|
|
|
let exe_filename = exe.file_name().unwrap();
|
|
|
|
let vm_home_dir = vm_parent_dir.join(vm_dir).join("home");
|
|
|
|
let vm_exe_path = vm_home_dir.join(exe_filename);
|
|
|
|
// FIXME(antoyo): panicking here makes the test pass.
|
2024-03-05 12:58:36 -06:00
|
|
|
let inside_vm_exe_path = PathBuf::from("/home").join(exe_filename);
|
2023-10-26 16:42:02 -05:00
|
|
|
let mut copy = Command::new("sudo");
|
|
|
|
copy.arg("cp");
|
2024-03-05 12:58:36 -06:00
|
|
|
copy.args([&exe, &vm_exe_path]);
|
2023-10-26 16:42:02 -05:00
|
|
|
|
|
|
|
let mut runtime = Command::new("sudo");
|
2024-03-05 12:58:36 -06:00
|
|
|
runtime.args(["chroot", vm_dir, "qemu-m68k-static"]);
|
2023-10-26 16:42:02 -05:00
|
|
|
runtime.arg(inside_vm_exe_path);
|
|
|
|
runtime.current_dir(vm_parent_dir);
|
2024-03-05 12:58:36 -06:00
|
|
|
vec![("Compiler", compiler), ("Copy", copy), ("Run-time", runtime)]
|
|
|
|
} else {
|
2023-10-26 16:42:02 -05:00
|
|
|
let runtime = Command::new(exe);
|
2024-03-05 12:58:36 -06:00
|
|
|
vec![("Compiler", compiler), ("Run-time", runtime)]
|
2023-10-26 16:42:02 -05:00
|
|
|
}
|
2020-05-10 09:54:30 -05:00
|
|
|
})
|
|
|
|
.run();
|
|
|
|
}
|