2021-07-07 04:14:20 -05:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::fs;
|
2022-10-23 09:22:55 -05:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-07-07 04:14:20 -05:00
|
|
|
use std::process::Command;
|
|
|
|
|
2022-12-01 07:30:03 -06:00
|
|
|
use super::build_sysroot::{SYSROOT_RUSTC_VERSION, SYSROOT_SRC};
|
2022-12-01 08:54:37 -06:00
|
|
|
use super::path::{Dirs, RelPath};
|
2022-02-23 04:49:34 -06:00
|
|
|
use super::rustc_info::{get_file_name, get_rustc_path, get_rustc_version};
|
2022-10-26 09:51:03 -05:00
|
|
|
use super::utils::{copy_dir_recursively, spawn_and_wait, Compiler};
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
pub(crate) fn prepare(dirs: &Dirs) {
|
|
|
|
if RelPath::DOWNLOAD.to_path(dirs).exists() {
|
|
|
|
std::fs::remove_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap();
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
2022-12-01 08:54:37 -06:00
|
|
|
std::fs::create_dir_all(RelPath::DOWNLOAD.to_path(dirs)).unwrap();
|
2022-10-23 09:22:55 -05:00
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
prepare_sysroot(dirs);
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
// FIXME maybe install this only locally?
|
2021-07-07 04:14:20 -05:00
|
|
|
eprintln!("[INSTALL] hyperfine");
|
2022-12-01 09:24:45 -06:00
|
|
|
Command::new("cargo")
|
|
|
|
.arg("install")
|
|
|
|
.arg("hyperfine")
|
|
|
|
.env_remove("CARGO_TARGET_DIR")
|
|
|
|
.spawn()
|
|
|
|
.unwrap()
|
|
|
|
.wait()
|
|
|
|
.unwrap();
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
super::abi_cafe::ABI_CAFE_REPO.fetch(dirs);
|
|
|
|
super::tests::RAND_REPO.fetch(dirs);
|
|
|
|
super::tests::REGEX_REPO.fetch(dirs);
|
|
|
|
super::tests::PORTABLE_SIMD_REPO.fetch(dirs);
|
|
|
|
super::tests::SIMPLE_RAYTRACER_REPO.fetch(dirs);
|
2021-07-07 04:14:20 -05:00
|
|
|
|
|
|
|
eprintln!("[LLVM BUILD] simple-raytracer");
|
2022-10-26 09:51:03 -05:00
|
|
|
let host_compiler = Compiler::host();
|
2022-12-01 08:54:37 -06:00
|
|
|
let build_cmd = super::tests::SIMPLE_RAYTRACER.build(&host_compiler, dirs);
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(build_cmd);
|
|
|
|
fs::copy(
|
2022-10-26 09:51:03 -05:00
|
|
|
super::tests::SIMPLE_RAYTRACER
|
2022-12-01 08:54:37 -06:00
|
|
|
.target_dir(dirs)
|
2022-10-26 09:51:03 -05:00
|
|
|
.join(&host_compiler.triple)
|
2022-10-23 09:22:55 -05:00
|
|
|
.join("debug")
|
|
|
|
.join(get_file_name("main", "bin")),
|
2022-12-01 08:54:37 -06:00
|
|
|
RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")),
|
2021-07-07 04:14:20 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn prepare_sysroot(dirs: &Dirs) {
|
2021-07-07 04:14:20 -05:00
|
|
|
let rustc_path = get_rustc_path();
|
|
|
|
let sysroot_src_orig = rustc_path.parent().unwrap().join("../lib/rustlib/src/rust");
|
2022-12-01 07:30:03 -06:00
|
|
|
let sysroot_src = SYSROOT_SRC;
|
2021-07-07 04:14:20 -05:00
|
|
|
|
|
|
|
assert!(sysroot_src_orig.exists());
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
sysroot_src.ensure_fresh(dirs);
|
|
|
|
fs::create_dir_all(sysroot_src.to_path(dirs).join("library")).unwrap();
|
2021-07-07 04:14:20 -05:00
|
|
|
eprintln!("[COPY] sysroot src");
|
2022-12-01 08:54:37 -06:00
|
|
|
copy_dir_recursively(
|
|
|
|
&sysroot_src_orig.join("library"),
|
|
|
|
&sysroot_src.to_path(dirs).join("library"),
|
|
|
|
);
|
2021-07-07 04:14:20 -05:00
|
|
|
|
|
|
|
let rustc_version = get_rustc_version();
|
2022-12-01 08:54:37 -06:00
|
|
|
fs::write(SYSROOT_RUSTC_VERSION.to_path(dirs), &rustc_version).unwrap();
|
2021-07-07 04:14:20 -05:00
|
|
|
|
|
|
|
eprintln!("[GIT] init");
|
2022-12-01 08:54:37 -06:00
|
|
|
init_git_repo(&sysroot_src.to_path(dirs));
|
2021-07-07 04:14:20 -05:00
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
apply_patches(dirs, "sysroot", &sysroot_src.to_path(dirs));
|
2021-07-07 04:14:20 -05:00
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
pub(crate) struct GitRepo {
|
|
|
|
url: GitRepoUrl,
|
|
|
|
rev: &'static str,
|
|
|
|
patch_name: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
enum GitRepoUrl {
|
|
|
|
Github { user: &'static str, repo: &'static str },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GitRepo {
|
2022-10-26 09:51:03 -05:00
|
|
|
pub(crate) const fn github(
|
2022-10-23 09:22:55 -05:00
|
|
|
user: &'static str,
|
|
|
|
repo: &'static str,
|
|
|
|
rev: &'static str,
|
|
|
|
patch_name: &'static str,
|
|
|
|
) -> GitRepo {
|
|
|
|
GitRepo { url: GitRepoUrl::Github { user, repo }, rev, patch_name }
|
|
|
|
}
|
|
|
|
|
2022-12-01 07:30:03 -06:00
|
|
|
pub(crate) const fn source_dir(&self) -> RelPath {
|
2022-10-23 09:22:55 -05:00
|
|
|
match self.url {
|
2022-12-01 07:30:03 -06:00
|
|
|
GitRepoUrl::Github { user: _, repo } => RelPath::DOWNLOAD.join(repo),
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn fetch(&self, dirs: &Dirs) {
|
2022-10-23 09:22:55 -05:00
|
|
|
match self.url {
|
|
|
|
GitRepoUrl::Github { user, repo } => {
|
2022-12-01 08:54:37 -06:00
|
|
|
clone_repo_shallow_github(
|
|
|
|
dirs,
|
|
|
|
&self.source_dir().to_path(dirs),
|
|
|
|
user,
|
|
|
|
repo,
|
|
|
|
self.rev,
|
|
|
|
);
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
}
|
2022-12-01 08:54:37 -06:00
|
|
|
apply_patches(dirs, self.patch_name, &self.source_dir().to_path(dirs));
|
2022-10-23 09:22:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 10:55:21 -05:00
|
|
|
#[allow(dead_code)]
|
2022-10-23 09:22:55 -05:00
|
|
|
fn clone_repo(download_dir: &Path, repo: &str, rev: &str) {
|
2021-07-07 04:14:20 -05:00
|
|
|
eprintln!("[CLONE] {}", repo);
|
|
|
|
// Ignore exit code as the repo may already have been checked out
|
2022-10-23 09:22:55 -05:00
|
|
|
Command::new("git").arg("clone").arg(repo).arg(&download_dir).spawn().unwrap().wait().unwrap();
|
2021-07-07 04:14:20 -05:00
|
|
|
|
|
|
|
let mut clean_cmd = Command::new("git");
|
2022-10-23 09:22:55 -05:00
|
|
|
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(&download_dir);
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(clean_cmd);
|
|
|
|
|
|
|
|
let mut checkout_cmd = Command::new("git");
|
2022-10-23 09:22:55 -05:00
|
|
|
checkout_cmd.arg("checkout").arg("-q").arg(rev).current_dir(download_dir);
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(checkout_cmd);
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn clone_repo_shallow_github(dirs: &Dirs, download_dir: &Path, user: &str, repo: &str, rev: &str) {
|
2022-03-20 10:55:21 -05:00
|
|
|
if cfg!(windows) {
|
|
|
|
// Older windows doesn't have tar or curl by default. Fall back to using git.
|
2022-10-23 09:22:55 -05:00
|
|
|
clone_repo(download_dir, &format!("https://github.com/{}/{}.git", user, repo), rev);
|
2022-03-20 10:55:21 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
let archive_url = format!("https://github.com/{}/{}/archive/{}.tar.gz", user, repo, rev);
|
2022-12-01 08:54:37 -06:00
|
|
|
let archive_file = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}.tar.gz", rev));
|
|
|
|
let archive_dir = RelPath::DOWNLOAD.to_path(dirs).join(format!("{}-{}", repo, rev));
|
2022-10-23 09:22:55 -05:00
|
|
|
|
|
|
|
eprintln!("[DOWNLOAD] {}/{} from {}", user, repo, archive_url);
|
2022-03-20 10:55:21 -05:00
|
|
|
|
|
|
|
// Remove previous results if they exists
|
|
|
|
let _ = std::fs::remove_file(&archive_file);
|
|
|
|
let _ = std::fs::remove_dir_all(&archive_dir);
|
2022-10-23 09:22:55 -05:00
|
|
|
let _ = std::fs::remove_dir_all(&download_dir);
|
2022-03-20 10:55:21 -05:00
|
|
|
|
|
|
|
// Download zip archive
|
|
|
|
let mut download_cmd = Command::new("curl");
|
|
|
|
download_cmd.arg("--location").arg("--output").arg(&archive_file).arg(archive_url);
|
|
|
|
spawn_and_wait(download_cmd);
|
|
|
|
|
|
|
|
// Unpack tar archive
|
|
|
|
let mut unpack_cmd = Command::new("tar");
|
2022-12-01 08:54:37 -06:00
|
|
|
unpack_cmd.arg("xf").arg(&archive_file).current_dir(RelPath::DOWNLOAD.to_path(dirs));
|
2022-03-20 10:55:21 -05:00
|
|
|
spawn_and_wait(unpack_cmd);
|
|
|
|
|
|
|
|
// Rename unpacked dir to the expected name
|
2022-10-23 09:22:55 -05:00
|
|
|
std::fs::rename(archive_dir, &download_dir).unwrap();
|
2022-03-20 10:55:21 -05:00
|
|
|
|
2022-10-23 09:22:55 -05:00
|
|
|
init_git_repo(&download_dir);
|
2022-03-20 10:55:21 -05:00
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
std::fs::remove_file(archive_file).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_git_repo(repo_dir: &Path) {
|
|
|
|
let mut git_init_cmd = Command::new("git");
|
|
|
|
git_init_cmd.arg("init").arg("-q").current_dir(repo_dir);
|
|
|
|
spawn_and_wait(git_init_cmd);
|
|
|
|
|
|
|
|
let mut git_add_cmd = Command::new("git");
|
|
|
|
git_add_cmd.arg("add").arg(".").current_dir(repo_dir);
|
|
|
|
spawn_and_wait(git_add_cmd);
|
|
|
|
|
|
|
|
let mut git_commit_cmd = Command::new("git");
|
2022-12-14 09:21:18 -06:00
|
|
|
git_commit_cmd
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.name=Dummy")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.email=dummy@example.com")
|
|
|
|
.arg("commit")
|
|
|
|
.arg("-m")
|
|
|
|
.arg("Initial commit")
|
|
|
|
.arg("-q")
|
|
|
|
.current_dir(repo_dir);
|
2022-03-20 10:55:21 -05:00
|
|
|
spawn_and_wait(git_commit_cmd);
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn get_patches(dirs: &Dirs, crate_name: &str) -> Vec<PathBuf> {
|
|
|
|
let mut patches: Vec<_> = fs::read_dir(RelPath::PATCHES.to_path(dirs))
|
2021-07-07 04:14:20 -05:00
|
|
|
.unwrap()
|
|
|
|
.map(|entry| entry.unwrap().path())
|
|
|
|
.filter(|path| path.extension() == Some(OsStr::new("patch")))
|
2022-10-23 09:22:55 -05:00
|
|
|
.filter(|path| {
|
|
|
|
path.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
.split_once("-")
|
|
|
|
.unwrap()
|
|
|
|
.1
|
|
|
|
.starts_with(crate_name)
|
2021-07-07 04:14:20 -05:00
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
patches.sort();
|
|
|
|
patches
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
fn apply_patches(dirs: &Dirs, crate_name: &str, target_dir: &Path) {
|
2022-10-23 09:22:55 -05:00
|
|
|
if crate_name == "<none>" {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:54:37 -06:00
|
|
|
for patch in get_patches(dirs, crate_name) {
|
2022-10-23 09:22:55 -05:00
|
|
|
eprintln!(
|
|
|
|
"[PATCH] {:?} <- {:?}",
|
|
|
|
target_dir.file_name().unwrap(),
|
|
|
|
patch.file_name().unwrap()
|
|
|
|
);
|
2021-07-07 04:14:20 -05:00
|
|
|
let mut apply_patch_cmd = Command::new("git");
|
2022-12-14 09:21:18 -06:00
|
|
|
apply_patch_cmd
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.name=Dummy")
|
|
|
|
.arg("-c")
|
|
|
|
.arg("user.email=dummy@example.com")
|
|
|
|
.arg("am")
|
|
|
|
.arg(patch)
|
|
|
|
.arg("-q")
|
|
|
|
.current_dir(target_dir);
|
2021-07-07 04:14:20 -05:00
|
|
|
spawn_and_wait(apply_patch_cmd);
|
|
|
|
}
|
|
|
|
}
|