about summary refs log tree commit diff
path: root/build_system
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2021-06-11 18:50:01 +0200
committerbjorn3 <bjorn3@users.noreply.github.com>2021-06-19 13:54:25 +0200
commitfe6a2892a6dd285774c3b1c84a2de45209454ce6 (patch)
tree953d54435e5caa1c8bb560df9a61a30af5868f51 /build_system
parent2db4e50618faf277aa6e7b0b45b6fc1aa5389653 (diff)
downloadrust-fe6a2892a6dd285774c3b1c84a2de45209454ce6.tar.gz
rust-fe6a2892a6dd285774c3b1c84a2de45209454ce6.zip
Rewrite prepare.sh in rust
Diffstat (limited to 'build_system')
-rw-r--r--build_system/build_backend.rs6
-rw-r--r--build_system/build_sysroot.rs3
-rw-r--r--build_system/prepare.rs74
-rw-r--r--build_system/utils.rs18
4 files changed, 96 insertions, 5 deletions
diff --git a/build_system/build_backend.rs b/build_system/build_backend.rs
index cdddeae2a33..81c47fa8358 100644
--- a/build_system/build_backend.rs
+++ b/build_system/build_backend.rs
@@ -1,5 +1,5 @@
 use std::env;
-use std::process::{self, Command};
+use std::process::Command;
 
 pub(crate) fn build_backend(channel: &str) -> String {
     let mut cmd = Command::new("cargo");
@@ -33,9 +33,7 @@ pub(crate) fn build_backend(channel: &str) -> String {
     }
 
     eprintln!("[BUILD] rustc_codegen_cranelift");
-    if !cmd.spawn().unwrap().wait().unwrap().success() {
-        process::exit(1);
-    }
+    crate::utils::spawn_and_wait(cmd);
 
     crate::rustc_info::get_dylib_name("rustc_codegen_cranelift")
 }
diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs
index d6ac19dd1cc..60906a8f19a 100644
--- a/build_system/build_sysroot.rs
+++ b/build_system/build_sysroot.rs
@@ -1,4 +1,5 @@
-use crate::{try_hard_link, SysrootKind};
+use crate::utils::try_hard_link;
+use crate::SysrootKind;
 use std::env;
 use std::fs;
 use std::path::Path;
diff --git a/build_system/prepare.rs b/build_system/prepare.rs
new file mode 100644
index 00000000000..4aef9162594
--- /dev/null
+++ b/build_system/prepare.rs
@@ -0,0 +1,74 @@
+use std::ffi::OsStr;
+use std::ffi::OsString;
+use std::fs;
+use std::process::Command;
+
+use crate::utils::spawn_and_wait;
+
+pub(crate) fn prepare() {
+    // FIXME implement in rust
+    let prepare_sysroot_cmd = Command::new("./build_sysroot/prepare_sysroot_src.sh");
+    spawn_and_wait(prepare_sysroot_cmd);
+
+    eprintln!("[INSTALL] hyperfine");
+    Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
+
+    clone_repo(
+        "rand",
+        "https://github.com/rust-random/rand.git",
+        "0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
+    );
+
+    eprintln!("[PATCH] rand");
+    for patch in get_patches("crate_patches", "rand") {
+        let mut patch_arg = OsString::from("../crate_patches/");
+        patch_arg.push(patch);
+        let mut apply_patch_cmd = Command::new("git");
+        apply_patch_cmd.arg("am").arg(patch_arg).current_dir("rand");
+        spawn_and_wait(apply_patch_cmd);
+    }
+
+    clone_repo(
+        "regex",
+        "https://github.com/rust-lang/regex.git",
+        "341f207c1071f7290e3f228c710817c280c8dca1",
+    );
+
+    clone_repo(
+        "simple-raytracer",
+        "https://github.com/ebobby/simple-raytracer",
+        "804a7a21b9e673a482797aa289a18ed480e4d813",
+    );
+
+    eprintln!("[LLVM BUILD] simple-raytracer");
+    let mut build_cmd = Command::new("cargo");
+    build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
+    spawn_and_wait(build_cmd);
+    fs::copy("simple-raytracer/target/debug/main", "simple-raytracer/raytracer_cg_llvm").unwrap();
+}
+
+fn clone_repo(name: &str, repo: &str, commit: &str) {
+    eprintln!("[CLONE] {}", repo);
+    // Ignore exit code as the repo may already have been checked out
+    Command::new("git").arg("clone").arg(repo).spawn().unwrap().wait().unwrap();
+
+    let mut clean_cmd = Command::new("git");
+    clean_cmd.arg("checkout").arg("--").arg(".").current_dir(name);
+    spawn_and_wait(clean_cmd);
+
+    let mut checkout_cmd = Command::new("git");
+    checkout_cmd.arg("checkout").arg(commit).current_dir(name);
+    spawn_and_wait(checkout_cmd);
+}
+
+fn get_patches(patch_dir: &str, crate_name: &str) -> Vec<OsString> {
+    let mut patches: Vec<_> = fs::read_dir(patch_dir)
+        .unwrap()
+        .map(|entry| entry.unwrap().path())
+        .filter(|path| path.extension() == Some(OsStr::new("patch")))
+        .map(|path| path.file_name().unwrap().to_owned())
+        .filter(|file_name| file_name.to_str().unwrap().split("-").nth(1).unwrap() == crate_name)
+        .collect();
+    patches.sort();
+    patches
+}
diff --git a/build_system/utils.rs b/build_system/utils.rs
new file mode 100644
index 00000000000..3eba87ee8a4
--- /dev/null
+++ b/build_system/utils.rs
@@ -0,0 +1,18 @@
+use std::fs;
+use std::path::Path;
+use std::process::{self, Command};
+
+#[track_caller]
+pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
+    let src = src.as_ref();
+    let dst = dst.as_ref();
+    if let Err(_) = fs::hard_link(src, dst) {
+        fs::copy(src, dst).unwrap(); // Fallback to copying if hardlinking failed
+    }
+}
+
+pub(crate) fn spawn_and_wait(mut cmd: Command) {
+    if !cmd.spawn().unwrap().wait().unwrap().success() {
+        process::exit(1);
+    }
+}