1
Fork 0

Rewrite prepare.sh in rust

This commit is contained in:
bjorn3 2021-06-11 18:50:01 +02:00
parent 2db4e50618
commit fe6a2892a6
12 changed files with 114 additions and 56 deletions

View file

@ -14,7 +14,7 @@ task:
- . $HOME/.cargo/env
- git config --global user.email "user@example.com"
- git config --global user.name "User"
- ./prepare.sh
- ./y.rs prepare
test_script:
- . $HOME/.cargo/env
- # Enable backtraces for easier debugging

View file

@ -53,7 +53,7 @@ jobs:
run: |
git config --global user.email "user@example.com"
git config --global user.name "User"
./prepare.sh
./y.rs prepare
- name: Test
env:

View file

@ -34,7 +34,7 @@ jobs:
run: |
git config --global user.email "user@example.com"
git config --global user.name "User"
./prepare.sh
./y.rs prepare
- name: Test
run: |
@ -72,7 +72,7 @@ jobs:
run: |
git config --global user.email "user@example.com"
git config --global user.name "User"
./prepare.sh
./y.rs prepare
- name: Test
run: |

View file

@ -10,8 +10,8 @@ If not please open an issue.
```bash
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
$ cd rustc_codegen_cranelift
$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
$ ./build.sh
$ ./y.rs prepare # download and patch sysroot src and install hyperfine for benchmarking
$ ./y.rs build
```
To run the test suite replace the last command with:
@ -20,7 +20,7 @@ To run the test suite replace the last command with:
$ ./test.sh
```
This will implicitly build cg_clif too. Both `build.sh` and `test.sh` accept a `--debug` argument to
This will implicitly build cg_clif too. Both `y.rs build` and `test.sh` accept a `--debug` argument to
build in debug mode.
Alternatively you can download a pre built version from [GHA]. It is listed in the artifacts section
@ -32,7 +32,7 @@ of workflow runs. Unfortunately due to GHA restrictions you need to be logged in
rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`prepare.sh` and `build.sh` or `test.sh`).
Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.rs prepare` and `y.rs build` or `test.sh`).
In the directory with your project (where you can do the usual `cargo build`), run:

View file

@ -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")
}

View file

@ -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;

74
build_system/prepare.rs Normal file
View file

@ -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
}

18
build_system/utils.rs Normal file
View file

@ -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);
}
}

View file

@ -2,7 +2,7 @@
rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`prepare.sh` and `build.sh` or `test.sh`).
Assuming `$cg_clif_dir` is the directory you cloned this repo into and you followed the instructions (`y.rs prepare` and `y.rs build` or `test.sh`).
## Cargo

View file

@ -1,29 +0,0 @@
#!/usr/bin/env bash
set -e
./build_sysroot/prepare_sysroot_src.sh
cargo install hyperfine || echo "Skipping hyperfine install"
git clone https://github.com/rust-random/rand.git || echo "rust-random/rand has already been cloned"
pushd rand
git checkout -- .
git checkout 0f933f9c7176e53b2a3c7952ded484e1783f0bf1
git am ../crate_patches/*-rand-*.patch
popd
git clone https://github.com/rust-lang/regex.git || echo "rust-lang/regex has already been cloned"
pushd regex
git checkout -- .
git checkout 341f207c1071f7290e3f228c710817c280c8dca1
popd
git clone https://github.com/ebobby/simple-raytracer || echo "ebobby/simple-raytracer has already been cloned"
pushd simple-raytracer
git checkout -- .
git checkout 804a7a21b9e673a482797aa289a18ed480e4d813
# build with cg_llvm for perf comparison
unset CARGO_TARGET_DIR
cargo build
mv target/debug/main raytracer_cg_llvm
popd

View file

@ -17,7 +17,7 @@ case $1 in
done
./clean_all.sh
./prepare.sh
./y.rs prepare
(cd build_sysroot && cargo update)

20
y.rs
View file

@ -24,19 +24,23 @@ exec ${0/.rs/.bin} $@
//! The name `y.rs` was chosen to not conflict with rustc's `x.py`.
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::process;
#[path = "build_system/build_backend.rs"]
mod build_backend;
#[path = "build_system/build_sysroot.rs"]
mod build_sysroot;
#[path = "build_system/prepare.rs"]
mod prepare;
#[path = "build_system/rustc_info.rs"]
mod rustc_info;
#[path = "build_system/utils.rs"]
mod utils;
fn usage() {
eprintln!("Usage:");
eprintln!(" ./y.rs prepare");
eprintln!(" ./y.rs build [--debug] [--sysroot none|clif|llvm] [--target-dir DIR]");
}
@ -69,7 +73,8 @@ fn main() {
if args.next().is_some() {
arg_error!("./x.rs prepare doesn't expect arguments");
}
todo!();
prepare::prepare();
process::exit(0);
}
Some("build") => Command::Build,
Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
@ -130,12 +135,3 @@ fn main() {
&target_triple,
);
}
#[track_caller]
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
}
}