about summary refs log tree commit diff
path: root/compiler/rustc_codegen_gcc/tests/lang_tests_common.rs
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume.gomez@huawei.com>2024-03-05 19:58:36 +0100
committerGuillaume Gomez <guillaume.gomez@huawei.com>2024-03-05 19:58:36 +0100
commit0d359efbe6dfcd927d4fd6208cdaed0bbaf33bb6 (patch)
treeff9149569c02c470f2dabf55d66e105d1163ba8f /compiler/rustc_codegen_gcc/tests/lang_tests_common.rs
parent7606c13961ddc1174b70638e934df0439b7dc515 (diff)
parentb385428e3ddf330805241e7758e773f933357c4b (diff)
downloadrust-0d359efbe6dfcd927d4fd6208cdaed0bbaf33bb6.tar.gz
rust-0d359efbe6dfcd927d4fd6208cdaed0bbaf33bb6.zip
Merge commit 'b385428e3ddf330805241e7758e773f933357c4b' into subtree-update_cg_gcc_2024-03-05
Diffstat (limited to 'compiler/rustc_codegen_gcc/tests/lang_tests_common.rs')
-rw-r--r--compiler/rustc_codegen_gcc/tests/lang_tests_common.rs88
1 files changed, 47 insertions, 41 deletions
diff --git a/compiler/rustc_codegen_gcc/tests/lang_tests_common.rs b/compiler/rustc_codegen_gcc/tests/lang_tests_common.rs
index af0133aad46..d321ffc8ff5 100644
--- a/compiler/rustc_codegen_gcc/tests/lang_tests_common.rs
+++ b/compiler/rustc_codegen_gcc/tests/lang_tests_common.rs
@@ -5,6 +5,7 @@ use std::{
     process::Command,
 };
 
+use boml::Toml;
 use lang_tester::LangTester;
 use tempfile::TempDir;
 
@@ -20,20 +21,32 @@ pub fn main_inner(profile: Profile) {
     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();
-    let gcc_path = include_str!("../gcc_path");
-    let gcc_path = gcc_path.trim();
+    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();
     env::set_var("LD_LIBRARY_PATH", gcc_path);
 
-    fn rust_filter(filename: &Path) -> bool {
-        filename.extension().expect("extension").to_str().expect("to_str") == "rs"
+    fn rust_filter(path: &Path) -> bool {
+        path.is_file() && path.extension().expect("extension").to_str().expect("to_str") == "rs"
     }
 
-    #[cfg(feature="master")]
+    #[cfg(feature = "master")]
     fn filter(filename: &Path) -> bool {
         rust_filter(filename)
     }
 
-    #[cfg(not(feature="master"))]
+    #[cfg(not(feature = "master"))]
     fn filter(filename: &Path) -> bool {
         if let Some(filename) = filename.to_str() {
             if filename.ends_with("gep.rs") {
@@ -45,16 +58,17 @@ pub fn main_inner(profile: Profile) {
 
     LangTester::new()
         .test_dir("tests/run")
-        .test_file_filter(filter)
-        .test_extract(|source| {
-            let lines =
-                source.lines()
-                    .skip_while(|l| !l.starts_with("//"))
-                    .take_while(|l| l.starts_with("//"))
-                    .map(|l| &l[2..])
-                    .collect::<Vec<_>>()
-                    .join("\n");
-            Some(lines)
+        .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
         })
         .test_cmds(move |path| {
             // Test command 1: Compile `x.rs` into `tempdir/x`.
@@ -62,19 +76,22 @@ pub fn main_inner(profile: Profile) {
             exe.push(&tempdir);
             exe.push(path.file_stem().expect("file_stem"));
             let mut compiler = Command::new("rustc");
-            compiler.args(&[
+            compiler.args([
                 &format!("-Zcodegen-backend={}/target/debug/librustc_codegen_gcc.so", current_dir),
-                "--sysroot", &format!("{}/build_sysroot/sysroot/", current_dir),
+                "--sysroot",
+                &format!("{}/build_sysroot/sysroot/", current_dir),
                 "-Zno-parallel-llvm",
-                "-C", "link-arg=-lc",
-                "-o", exe.to_str().expect("to_str"),
+                "-C",
+                "link-arg=-lc",
+                "-o",
+                exe.to_str().expect("to_str"),
                 path.to_str().expect("to_str"),
             ]);
 
             // 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 {
-                compiler.args(&["--target", &target]);
+                compiler.args(["--target", target]);
                 let linker = format!("{}-gcc", target);
                 compiler.args(&[format!("-Clinker={}", linker)]);
                 let mut env_path = std::env::var("PATH").unwrap_or_default();
@@ -85,49 +102,38 @@ pub fn main_inner(profile: Profile) {
 
             if let Some(flags) = option_env!("TEST_FLAGS") {
                 for flag in flags.split_whitespace() {
-                    compiler.arg(&flag);
+                    compiler.arg(flag);
                 }
             }
             match profile {
                 Profile::Debug => {}
                 Profile::Release => {
-                    compiler.args(&[
-                        "-C", "opt-level=3",
-                        "-C", "lto=no",
-                    ]);
+                    compiler.args(["-C", "opt-level=3", "-C", "lto=no"]);
                 }
             }
             // Test command 2: run `tempdir/x`.
             if test_target.is_ok() {
                 let vm_parent_dir = std::env::var("CG_GCC_VM_DIR")
-                    .map(|dir| PathBuf::from(dir))
+                    .map(PathBuf::from)
                     .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.
-                let inside_vm_exe_path = PathBuf::from("/home").join(&exe_filename);
+                let inside_vm_exe_path = PathBuf::from("/home").join(exe_filename);
                 let mut copy = Command::new("sudo");
                 copy.arg("cp");
-                copy.args(&[&exe, &vm_exe_path]);
+                copy.args([&exe, &vm_exe_path]);
 
                 let mut runtime = Command::new("sudo");
-                runtime.args(&["chroot", vm_dir, "qemu-m68k-static"]);
+                runtime.args(["chroot", vm_dir, "qemu-m68k-static"]);
                 runtime.arg(inside_vm_exe_path);
                 runtime.current_dir(vm_parent_dir);
-                vec![
-                    ("Compiler", compiler),
-                    ("Copy", copy),
-                    ("Run-time", runtime),
-                ]
-            }
-            else {
+                vec![("Compiler", compiler), ("Copy", copy), ("Run-time", runtime)]
+            } else {
                 let runtime = Command::new(exe);
-                vec![
-                    ("Compiler", compiler),
-                    ("Run-time", runtime),
-                ]
+                vec![("Compiler", compiler), ("Run-time", runtime)]
             }
         })
         .run();