Skip to content

Commit fe6a289

Browse files
committed
Rewrite prepare.sh in rust
1 parent 2db4e50 commit fe6a289

File tree

12 files changed

+114
-56
lines changed

12 files changed

+114
-56
lines changed

.cirrus.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ task:
1414
- . $HOME/.cargo/env
1515
- git config --global user.email "[email protected]"
1616
- git config --global user.name "User"
17-
- ./prepare.sh
17+
- ./y.rs prepare
1818
test_script:
1919
- . $HOME/.cargo/env
2020
- # Enable backtraces for easier debugging

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
run: |
5454
git config --global user.email "[email protected]"
5555
git config --global user.name "User"
56-
./prepare.sh
56+
./y.rs prepare
5757
5858
- name: Test
5959
env:

.github/workflows/rustc.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
run: |
3535
git config --global user.email "[email protected]"
3636
git config --global user.name "User"
37-
./prepare.sh
37+
./y.rs prepare
3838
3939
- name: Test
4040
run: |
@@ -72,7 +72,7 @@ jobs:
7272
run: |
7373
git config --global user.email "[email protected]"
7474
git config --global user.name "User"
75-
./prepare.sh
75+
./y.rs prepare
7676
7777
- name: Test
7878
run: |

Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ If not please open an issue.
1010
```bash
1111
$ git clone https://github.com/bjorn3/rustc_codegen_cranelift.git
1212
$ cd rustc_codegen_cranelift
13-
$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
14-
$ ./build.sh
13+
$ ./y.rs prepare # download and patch sysroot src and install hyperfine for benchmarking
14+
$ ./y.rs build
1515
```
1616

1717
To run the test suite replace the last command with:
@@ -20,7 +20,7 @@ To run the test suite replace the last command with:
2020
$ ./test.sh
2121
```
2222

23-
This will implicitly build cg_clif too. Both `build.sh` and `test.sh` accept a `--debug` argument to
23+
This will implicitly build cg_clif too. Both `y.rs build` and `test.sh` accept a `--debug` argument to
2424
build in debug mode.
2525

2626
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
3232

3333
rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
3434

35-
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`).
35+
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`).
3636

3737
In the directory with your project (where you can do the usual `cargo build`), run:
3838

build_system/build_backend.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::env;
2-
use std::process::{self, Command};
2+
use std::process::Command;
33

44
pub(crate) fn build_backend(channel: &str) -> String {
55
let mut cmd = Command::new("cargo");
@@ -33,9 +33,7 @@ pub(crate) fn build_backend(channel: &str) -> String {
3333
}
3434

3535
eprintln!("[BUILD] rustc_codegen_cranelift");
36-
if !cmd.spawn().unwrap().wait().unwrap().success() {
37-
process::exit(1);
38-
}
36+
crate::utils::spawn_and_wait(cmd);
3937

4038
crate::rustc_info::get_dylib_name("rustc_codegen_cranelift")
4139
}

build_system/build_sysroot.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use crate::{try_hard_link, SysrootKind};
1+
use crate::utils::try_hard_link;
2+
use crate::SysrootKind;
23
use std::env;
34
use std::fs;
45
use std::path::Path;

build_system/prepare.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use std::ffi::OsStr;
2+
use std::ffi::OsString;
3+
use std::fs;
4+
use std::process::Command;
5+
6+
use crate::utils::spawn_and_wait;
7+
8+
pub(crate) fn prepare() {
9+
// FIXME implement in rust
10+
let prepare_sysroot_cmd = Command::new("./build_sysroot/prepare_sysroot_src.sh");
11+
spawn_and_wait(prepare_sysroot_cmd);
12+
13+
eprintln!("[INSTALL] hyperfine");
14+
Command::new("cargo").arg("install").arg("hyperfine").spawn().unwrap().wait().unwrap();
15+
16+
clone_repo(
17+
"rand",
18+
"https://github.com/rust-random/rand.git",
19+
"0f933f9c7176e53b2a3c7952ded484e1783f0bf1",
20+
);
21+
22+
eprintln!("[PATCH] rand");
23+
for patch in get_patches("crate_patches", "rand") {
24+
let mut patch_arg = OsString::from("../crate_patches/");
25+
patch_arg.push(patch);
26+
let mut apply_patch_cmd = Command::new("git");
27+
apply_patch_cmd.arg("am").arg(patch_arg).current_dir("rand");
28+
spawn_and_wait(apply_patch_cmd);
29+
}
30+
31+
clone_repo(
32+
"regex",
33+
"https://github.com/rust-lang/regex.git",
34+
"341f207c1071f7290e3f228c710817c280c8dca1",
35+
);
36+
37+
clone_repo(
38+
"simple-raytracer",
39+
"https://github.com/ebobby/simple-raytracer",
40+
"804a7a21b9e673a482797aa289a18ed480e4d813",
41+
);
42+
43+
eprintln!("[LLVM BUILD] simple-raytracer");
44+
let mut build_cmd = Command::new("cargo");
45+
build_cmd.arg("build").env_remove("CARGO_TARGET_DIR").current_dir("simple-raytracer");
46+
spawn_and_wait(build_cmd);
47+
fs::copy("simple-raytracer/target/debug/main", "simple-raytracer/raytracer_cg_llvm").unwrap();
48+
}
49+
50+
fn clone_repo(name: &str, repo: &str, commit: &str) {
51+
eprintln!("[CLONE] {}", repo);
52+
// Ignore exit code as the repo may already have been checked out
53+
Command::new("git").arg("clone").arg(repo).spawn().unwrap().wait().unwrap();
54+
55+
let mut clean_cmd = Command::new("git");
56+
clean_cmd.arg("checkout").arg("--").arg(".").current_dir(name);
57+
spawn_and_wait(clean_cmd);
58+
59+
let mut checkout_cmd = Command::new("git");
60+
checkout_cmd.arg("checkout").arg(commit).current_dir(name);
61+
spawn_and_wait(checkout_cmd);
62+
}
63+
64+
fn get_patches(patch_dir: &str, crate_name: &str) -> Vec<OsString> {
65+
let mut patches: Vec<_> = fs::read_dir(patch_dir)
66+
.unwrap()
67+
.map(|entry| entry.unwrap().path())
68+
.filter(|path| path.extension() == Some(OsStr::new("patch")))
69+
.map(|path| path.file_name().unwrap().to_owned())
70+
.filter(|file_name| file_name.to_str().unwrap().split("-").nth(1).unwrap() == crate_name)
71+
.collect();
72+
patches.sort();
73+
patches
74+
}

build_system/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::fs;
2+
use std::path::Path;
3+
use std::process::{self, Command};
4+
5+
#[track_caller]
6+
pub(crate) fn try_hard_link(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
7+
let src = src.as_ref();
8+
let dst = dst.as_ref();
9+
if let Err(_) = fs::hard_link(src, dst) {
10+
fs::copy(src, dst).unwrap(); // Fallback to copying if hardlinking failed
11+
}
12+
}
13+
14+
pub(crate) fn spawn_and_wait(mut cmd: Command) {
15+
if !cmd.spawn().unwrap().wait().unwrap().success() {
16+
process::exit(1);
17+
}
18+
}

docs/usage.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
rustc_codegen_cranelift can be used as a near-drop-in replacement for `cargo build` or `cargo run` for existing projects.
44

5-
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`).
5+
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`).
66

77
## Cargo
88

prepare.sh

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)