prepare.rs 4.5 KB
Newer Older
B
bjorn3 已提交
1
use std::env;
B
bjorn3 已提交
2 3 4
use std::ffi::OsStr;
use std::ffi::OsString;
use std::fs;
B
bjorn3 已提交
5
use std::path::Path;
B
bjorn3 已提交
6 7
use std::process::Command;

8
use crate::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
B
bjorn3 已提交
9
use crate::utils::{copy_dir_recursively, spawn_and_wait};
B
bjorn3 已提交
10 11

pub(crate) fn prepare() {
B
bjorn3 已提交
12
    prepare_sysroot();
B
bjorn3 已提交
13 14 15 16 17 18 19 20 21

    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",
    );
22
    apply_patches("rand", Path::new("rand"));
B
bjorn3 已提交
23 24 25 26 27 28 29

    clone_repo(
        "regex",
        "https://github.com/rust-lang/regex.git",
        "341f207c1071f7290e3f228c710817c280c8dca1",
    );

B
bjorn3 已提交
30 31 32 33 34 35 36
    clone_repo(
        "stdsimd",
        "https://github.com/rust-lang/stdsimd",
        "be96995d8ddec03fac9a0caf4d4c51c7fbc33507",
    );
    apply_patches("stdsimd", Path::new("stdsimd"));

B
bjorn3 已提交
37 38 39 40 41 42 43 44 45 46
    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);
B
bjorn3 已提交
47 48 49 50 51 52
    fs::copy(
        Path::new("simple-raytracer/target/debug").join(get_file_name("main", "bin")),
        // FIXME use get_file_name here too once testing is migrated to rust
        "simple-raytracer/raytracer_cg_llvm",
    )
    .unwrap();
B
bjorn3 已提交
53 54
}

B
bjorn3 已提交
55 56 57
fn prepare_sysroot() {
    let rustc_path = get_rustc_path();
    let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust");
B
bjorn3 已提交
58
    let sysroot_src = env::current_dir().unwrap().join("build_sysroot").join("sysroot_src");
B
bjorn3 已提交
59 60 61 62 63 64 65 66 67 68

    assert!(sysroot_src_orig.exists());

    if sysroot_src.exists() {
        fs::remove_dir_all(&sysroot_src).unwrap();
    }
    fs::create_dir_all(sysroot_src.join("library")).unwrap();
    eprintln!("[COPY] sysroot src");
    copy_dir_recursively(&sysroot_src_orig.join("library"), &sysroot_src.join("library"));

69
    let rustc_version = get_rustc_version();
B
bjorn3 已提交
70
    fs::write(Path::new("build_sysroot").join("rustc_version"), &rustc_version).unwrap();
71

B
bjorn3 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    eprintln!("[GIT] init");
    let mut git_init_cmd = Command::new("git");
    git_init_cmd.arg("init").arg("-q").current_dir(&sysroot_src);
    spawn_and_wait(git_init_cmd);

    let mut git_add_cmd = Command::new("git");
    git_add_cmd.arg("add").arg(".").current_dir(&sysroot_src);
    spawn_and_wait(git_add_cmd);

    let mut git_commit_cmd = Command::new("git");
    git_commit_cmd
        .arg("commit")
        .arg("-m")
        .arg("Initial commit")
        .arg("-q")
        .current_dir(&sysroot_src);
    spawn_and_wait(git_commit_cmd);

90
    apply_patches("sysroot", &sysroot_src);
B
bjorn3 已提交
91 92 93 94

    clone_repo(
        "build_sysroot/compiler-builtins",
        "https://github.com/rust-lang/compiler-builtins.git",
95
        "0.1.46",
B
bjorn3 已提交
96
    );
97
    apply_patches("compiler-builtins", Path::new("build_sysroot/compiler-builtins"));
B
bjorn3 已提交
98 99 100
}

fn clone_repo(target_dir: &str, repo: &str, rev: &str) {
B
bjorn3 已提交
101 102
    eprintln!("[CLONE] {}", repo);
    // Ignore exit code as the repo may already have been checked out
B
bjorn3 已提交
103
    Command::new("git").arg("clone").arg(repo).arg(target_dir).spawn().unwrap().wait().unwrap();
B
bjorn3 已提交
104 105

    let mut clean_cmd = Command::new("git");
B
bjorn3 已提交
106
    clean_cmd.arg("checkout").arg("--").arg(".").current_dir(target_dir);
B
bjorn3 已提交
107 108 109
    spawn_and_wait(clean_cmd);

    let mut checkout_cmd = Command::new("git");
B
bjorn3 已提交
110
    checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(target_dir);
B
bjorn3 已提交
111 112 113
    spawn_and_wait(checkout_cmd);
}

114 115
fn get_patches(crate_name: &str) -> Vec<OsString> {
    let mut patches: Vec<_> = fs::read_dir("patches")
B
bjorn3 已提交
116 117 118 119
        .unwrap()
        .map(|entry| entry.unwrap().path())
        .filter(|path| path.extension() == Some(OsStr::new("patch")))
        .map(|path| path.file_name().unwrap().to_owned())
B
bjorn3 已提交
120 121 122
        .filter(|file_name| {
            file_name.to_str().unwrap().split_once("-").unwrap().1.starts_with(crate_name)
        })
B
bjorn3 已提交
123 124 125 126
        .collect();
    patches.sort();
    patches
}
B
bjorn3 已提交
127

128 129
fn apply_patches(crate_name: &str, target_dir: &Path) {
    for patch in get_patches(crate_name) {
B
bjorn3 已提交
130
        eprintln!("[PATCH] {:?} <- {:?}", target_dir.file_name().unwrap(), patch);
B
bjorn3 已提交
131
        let patch_arg = env::current_dir().unwrap().join("patches").join(patch);
B
bjorn3 已提交
132 133 134 135 136
        let mut apply_patch_cmd = Command::new("git");
        apply_patch_cmd.arg("am").arg(patch_arg).arg("-q").current_dir(target_dir);
        spawn_and_wait(apply_patch_cmd);
    }
}