Skip to content

Commit 2a87b26

Browse files
Rustify prepare.sh command
1 parent 4748fdc commit 2a87b26

File tree

17 files changed

+343
-86
lines changed

17 files changed

+343
-86
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119

120120
- name: Build
121121
run: |
122-
./prepare_build.sh
122+
./y.sh prepare --only-libcore
123123
${{ matrix.libgccjit_version.env_extra }} ./build.sh ${{ matrix.libgccjit_version.extra }}
124124
${{ matrix.libgccjit_version.env_extra }} cargo test ${{ matrix.libgccjit_version.extra }}
125125
./clean_all.sh
@@ -128,7 +128,7 @@ jobs:
128128
run: |
129129
git config --global user.email "[email protected]"
130130
git config --global user.name "User"
131-
./prepare.sh
131+
./y.sh prepare
132132
133133
# Compile is a separate step, as the actions-rs/cargo action supports error annotations
134134
- name: Compile

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ jobs:
8888

8989
- name: Build
9090
run: |
91-
./prepare_build.sh
91+
./y.sh prepare --only-libcore
9292
./build.sh --release --release-sysroot
9393
cargo test
9494
./clean_all.sh
@@ -97,7 +97,7 @@ jobs:
9797
run: |
9898
git config --global user.email "[email protected]"
9999
git config --global user.name "User"
100-
./prepare.sh
100+
./y.sh prepare
101101
102102
# Compile is a separate step, as the actions-rs/cargo action supports error annotations
103103
- name: Compile

.github/workflows/stdarch.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ jobs:
102102

103103
- name: Build
104104
run: |
105-
./prepare_build.sh
105+
./y.sh prepare --only-libcore
106106
./build.sh --release --release-sysroot
107107
cargo test
108108
@@ -115,7 +115,7 @@ jobs:
115115
run: |
116116
git config --global user.email "[email protected]"
117117
git config --global user.name "User"
118-
./prepare.sh
118+
./y.sh prepare
119119
120120
# Compile is a separate step, as the actions-rs/cargo action supports error annotations
121121
- name: Compile

.gitignore

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ perf.data
66
perf.data.old
77
*.events
88
*.string*
9-
/build_sysroot/sysroot
10-
/build_sysroot/sysroot_src
11-
/build_sysroot/Cargo.lock
12-
/build_sysroot/test_target/Cargo.lock
9+
build_sysroot
1310
/rust
1411
/simple-raytracer
1512
/regex
@@ -25,3 +22,4 @@ tools/llvmint
2522
tools/llvmint-2
2623
# The `llvm` folder is generated by the `tools/generate_intrinsics.py` script to update intrinsics.
2724
llvm
25+
build_system/target

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $ export RUST_COMPILER_RT_ROOT="$PWD/llvm/compiler-rt"
6565
Then you can run commands like this:
6666

6767
```bash
68-
$ ./prepare.sh # download and patch sysroot src and install hyperfine for benchmarking
68+
$ ./y.sh prepare # download and patch sysroot src and install hyperfine for benchmarking
6969
$ LIBRARY_PATH=$(cat gcc_path) LD_LIBRARY_PATH=$(cat gcc_path) ./build.sh --release
7070
```
7171

build_sysroot/prepare_sysroot_src.sh

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

build_system/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build_system/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "y"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[[bin]]
7+
name = "y"
8+
path = "src/main.rs"
9+
10+
[features]
11+
unstable-features = [] # for rust-analyzer

build_system/src/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub fn run() -> Result<(), String> {
2+
Ok(())
3+
}

build_system/src/main.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use std::env;
2+
use std::process;
3+
4+
mod build;
5+
mod prepare;
6+
mod rustc_info;
7+
mod utils;
8+
9+
macro_rules! arg_error {
10+
($($err:tt)*) => {{
11+
eprintln!($($err)*);
12+
usage();
13+
std::process::exit(1);
14+
}};
15+
}
16+
17+
fn usage() {
18+
// println!("{}", include_str!("usage.txt"));
19+
}
20+
21+
pub enum Command {
22+
Prepare,
23+
Build,
24+
}
25+
26+
fn main() {
27+
if env::var("RUST_BACKTRACE").is_err() {
28+
env::set_var("RUST_BACKTRACE", "1");
29+
}
30+
31+
let command = match env::args().nth(1).as_deref() {
32+
Some("prepare") => Command::Prepare,
33+
Some("build") => Command::Build,
34+
Some(flag) if flag.starts_with('-') => arg_error!("Expected command found flag {}", flag),
35+
Some(command) => arg_error!("Unknown command {}", command),
36+
None => {
37+
usage();
38+
process::exit(0);
39+
}
40+
};
41+
42+
if let Err(e) = match command {
43+
Command::Prepare => prepare::run(),
44+
Command::Build => build::run(),
45+
} {
46+
eprintln!("Command failed to run: {e:?}");
47+
process::exit(1);
48+
}
49+
}

build_system/src/prepare.rs

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
use crate::rustc_info::get_rustc_path;
2+
use crate::utils::{cargo_install, git_clone, run_command, walk_dir};
3+
4+
use std::fs;
5+
use std::path::Path;
6+
7+
fn prepare_libcore() -> Result<(), String> {
8+
let rustc_path = match get_rustc_path() {
9+
Some(path) => path,
10+
None => return Err("`rustc` path not found".to_owned()),
11+
};
12+
13+
let parent = match rustc_path.parent() {
14+
Some(path) => path,
15+
None => return Err(format!("No parent for `{}`", rustc_path.display())),
16+
};
17+
18+
let rustlib_dir = parent.join("../lib/rustlib/src/rust");
19+
if !rustlib_dir.is_dir() {
20+
return Err("Please install `rust-src` component".to_owned());
21+
}
22+
23+
let sysroot_dir = Path::new("build_sysroot/sysroot_src");
24+
if sysroot_dir.is_dir() {
25+
if let Err(e) = fs::remove_dir_all(sysroot_dir) {
26+
return Err(format!("Failed to remove `{}`: {:?}", sysroot_dir.display(), e));
27+
}
28+
}
29+
30+
let sysroot_library_dir = sysroot_dir.join("library");
31+
fs::create_dir_all(&sysroot_library_dir)
32+
.map_err(|e| format!(
33+
"Failed to create folder `{}`: {e:?}",
34+
sysroot_library_dir.display(),
35+
))?;
36+
37+
run_command(&[&"cp", &"-r", &rustlib_dir, &sysroot_library_dir], None)?;
38+
39+
println!("[GIT] init (cwd): `{}`", sysroot_dir.display());
40+
run_command(&[&"git", &"init"], Some(&sysroot_dir))?;
41+
println!("[GIT] add (cwd): `{}`", sysroot_dir.display());
42+
run_command(&[&"git", &"add", &"."], Some(&sysroot_dir))?;
43+
println!("[GIT] commit (cwd): `{}`", sysroot_dir.display());
44+
45+
// This is needed on systems where nothing is configured.
46+
// git really needs something here, or it will fail.
47+
// Even using --author is not enough.
48+
run_command(&[&"git", &"config", &"user.email", &"[email protected]"], Some(&sysroot_dir))?;
49+
run_command(&[&"git", &"config", &"user.name", &"None"], Some(&sysroot_dir))?;
50+
run_command(&[&"git", &"config", &"core.autocrlf=false"], Some(&sysroot_dir))?;
51+
run_command(&[&"git", &"config", &"commit.gpgSign=false"], Some(&sysroot_dir))?;
52+
run_command(&[&"git", &"commit", &"-m", &"Initial commit", &"-q"], Some(&sysroot_dir))?;
53+
54+
walk_dir("patches", |_| Ok(()), |file_path: &Path| {
55+
println!("[GIT] apply `{}`", file_path.display());
56+
let path = Path::new("../..").join(file_path);
57+
run_command(&[&"git", &"apply", &path], Some(&sysroot_dir))?;
58+
run_command(&[&"git", &"add", &"-A"], Some(&sysroot_dir))?;
59+
run_command(
60+
&[&"git", &"commit", &"--no-gpg-sign", &"-m", &format!("Patch {}", path.display())],
61+
Some(&sysroot_dir),
62+
)?;
63+
Ok(())
64+
})?;
65+
println!("Successfully prepared libcore for building");
66+
Ok(())
67+
}
68+
69+
// build with cg_llvm for perf comparison
70+
fn build_raytracer(repo_dir: &Path) -> Result<(), String> {
71+
run_command(&[&"cargo", &"build"], Some(repo_dir))?;
72+
run_command(&[&"mv", &"target/debug/main", &"raytracer_cg_llvm"], Some(repo_dir))?;
73+
Ok(())
74+
}
75+
76+
fn clone_and_setup<F>(repo_url: &str, checkout_commit: &str, extra: Option<F>) -> Result<(), String>
77+
where
78+
F: Fn(&Path) -> Result<(), String>,
79+
{
80+
let clone_result = git_clone(repo_url, None)?;
81+
if !clone_result.ran_clone {
82+
println!("`{}` has already been cloned", clone_result.repo_name);
83+
}
84+
let repo_path = Path::new(&clone_result.repo_name);
85+
run_command(&[&"git", &"checkout", &"--", &"."], Some(repo_path))?;
86+
run_command(&[&"git", &"checkout", &checkout_commit], Some(repo_path))?;
87+
let filter = format!("-{}-", clone_result.repo_name);
88+
walk_dir("crate_patches", |_| Ok(()), |file_path| {
89+
let s = file_path.as_os_str().to_str().unwrap();
90+
if s.contains(&filter) && s.ends_with(".patch") {
91+
run_command(&[&"git", &"am", &s], Some(repo_path))?;
92+
}
93+
Ok(())
94+
})?;
95+
if let Some(extra) = extra {
96+
extra(repo_path)?;
97+
}
98+
Ok(())
99+
}
100+
101+
struct PrepareArg {
102+
only_libcore: bool,
103+
}
104+
105+
impl PrepareArg {
106+
fn new() -> Result<Option<Self>, String> {
107+
let mut only_libcore = false;
108+
109+
for arg in std::env::args().skip(2) {
110+
match arg.as_str() {
111+
"--only-libcore" => only_libcore = true,
112+
"--help" => {
113+
Self::usage();
114+
return Ok(None)
115+
}
116+
a => return Err(format!("Unknown argument `{a}`")),
117+
}
118+
}
119+
Ok(Some(Self {
120+
only_libcore,
121+
}))
122+
}
123+
124+
fn usage() {
125+
println!(r#"
126+
`prepare` command help:
127+
128+
--only-libcore : Only setup libcore and don't clone other repositories
129+
--help : Show this help
130+
"#)
131+
}
132+
}
133+
134+
pub fn run() -> Result<(), String> {
135+
let args = match PrepareArg::new()? {
136+
Some(a) => a,
137+
None => return Ok(()),
138+
};
139+
prepare_libcore()?;
140+
141+
if !args.only_libcore {
142+
cargo_install("hyperfine")?;
143+
144+
let to_clone = &[
145+
("https://github.com/rust-random/rand.git", "0f933f9c7176e53b2a3c7952ded484e1783f0bf1", None),
146+
("https://github.com/rust-lang/regex.git", "341f207c1071f7290e3f228c710817c280c8dca1", None),
147+
("https://github.com/ebobby/simple-raytracer", "804a7a21b9e673a482797aa289a18ed480e4d813", Some(build_raytracer)),
148+
];
149+
150+
for (repo_url, checkout_commit, cb) in to_clone {
151+
clone_and_setup(repo_url, checkout_commit, *cb)?;
152+
}
153+
}
154+
155+
println!("Successfully ran `prepare`");
156+
Ok(())
157+
}

build_system/src/rustc_info.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
use std::path::{Path, PathBuf};
2+
3+
use crate::utils::run_command;
4+
5+
pub fn get_rustc_path() -> Option<PathBuf> {
6+
if let Ok(rustc) = std::env::var("RUSTC") {
7+
return Some(PathBuf::from(rustc));
8+
}
9+
run_command(&[&"rustup", &"which", &"rustc"], None)
10+
.ok()
11+
.map(|out| Path::new(String::from_utf8(out.stdout).unwrap().trim()).to_owned())
12+
}

0 commit comments

Comments
 (0)