Skip to content

Commit c6447be

Browse files
authored
Merge pull request #345 from GuillaumeGomez/build-sysroot
Rustify build.sh script
2 parents 71febd9 + 33e1daa commit c6447be

File tree

13 files changed

+576
-136
lines changed

13 files changed

+576
-136
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119
- name: Build
120120
run: |
121121
./y.sh prepare --only-libcore
122-
${{ matrix.libgccjit_version.env_extra }} ./build.sh ${{ matrix.libgccjit_version.extra }}
122+
${{ matrix.libgccjit_version.env_extra }} ./y.sh build ${{ matrix.libgccjit_version.extra }}
123123
${{ matrix.libgccjit_version.env_extra }} cargo test ${{ matrix.libgccjit_version.extra }}
124124
./clean_all.sh
125125

.github/workflows/release.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ jobs:
8686
- name: Build
8787
run: |
8888
./y.sh prepare --only-libcore
89-
EMBED_LTO_BITCODE=1 ./build.sh --release --release-sysroot
89+
EMBED_LTO_BITCODE=1 ./y.sh build --release --release-sysroot
9090
cargo test
9191
./clean_all.sh
9292

.github/workflows/stdarch.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ jobs:
100100
- name: Build
101101
run: |
102102
./y.sh prepare --only-libcore
103-
./build.sh --release --release-sysroot
103+
./y.sh build --release --release-sysroot
104104
cargo test
105105
106106
- name: Clean

Readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Then you can run commands like this:
6666

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

7272
To run the tests:

build.sh

-67
This file was deleted.

build_sysroot/build_sysroot.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
set -e
66
cd $(dirname "$0")
77

8-
pushd ../ >/dev/null
8+
pushd ../
99
source ./config.sh
10-
popd >/dev/null
10+
popd
1111

1212
# Cleanup for previous run
1313
# v Clean target dir except for build scripts and incremental cache

build_system/src/build.rs

+230
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,233 @@
1+
use crate::config::set_config;
2+
use crate::utils::{
3+
get_gcc_path, run_command, run_command_with_env, run_command_with_output_and_env, walk_dir,
4+
};
5+
use std::collections::HashMap;
6+
use std::ffi::OsStr;
7+
use std::fs;
8+
use std::path::Path;
9+
10+
#[derive(Default)]
11+
struct BuildArg {
12+
codegen_release_channel: bool,
13+
sysroot_release_channel: bool,
14+
features: Vec<String>,
15+
gcc_path: String,
16+
}
17+
18+
impl BuildArg {
19+
fn new() -> Result<Option<Self>, String> {
20+
let gcc_path = get_gcc_path()?;
21+
let mut build_arg = Self {
22+
gcc_path,
23+
..Default::default()
24+
};
25+
// We skip binary name and the `build` command.
26+
let mut args = std::env::args().skip(2);
27+
28+
while let Some(arg) = args.next() {
29+
match arg.as_str() {
30+
"--release" => build_arg.codegen_release_channel = true,
31+
"--release-sysroot" => build_arg.sysroot_release_channel = true,
32+
"--no-default-features" => {
33+
build_arg.features.push("--no-default-features".to_string());
34+
}
35+
"--features" => {
36+
if let Some(arg) = args.next() {
37+
build_arg.features.push("--features".to_string());
38+
build_arg.features.push(arg.as_str().into());
39+
} else {
40+
return Err(
41+
"Expected a value after `--features`, found nothing".to_string()
42+
);
43+
}
44+
}
45+
"--help" => {
46+
Self::usage();
47+
return Ok(None);
48+
}
49+
arg => return Err(format!("Unknown argument `{}`", arg)),
50+
}
51+
}
52+
Ok(Some(build_arg))
53+
}
54+
55+
fn usage() {
56+
println!(
57+
r#"
58+
`build` command help:
59+
60+
--release : Build codegen in release mode
61+
--release-sysroot : Build sysroot in release mode
62+
--no-default-features : Add `--no-default-features` flag
63+
--features [arg] : Add a new feature [arg]
64+
--help : Show this help
65+
"#
66+
)
67+
}
68+
}
69+
70+
fn build_sysroot(
71+
env: &mut HashMap<String, String>,
72+
release_mode: bool,
73+
target_triple: &str,
74+
) -> Result<(), String> {
75+
std::env::set_current_dir("build_sysroot")
76+
.map_err(|error| format!("Failed to go to `build_sysroot` directory: {:?}", error))?;
77+
// Cleanup for previous run
78+
// Clean target dir except for build scripts and incremental cache
79+
let _ = walk_dir(
80+
"target",
81+
|dir: &Path| {
82+
for top in &["debug", "release"] {
83+
let _ = fs::remove_dir_all(dir.join(top).join("build"));
84+
let _ = fs::remove_dir_all(dir.join(top).join("deps"));
85+
let _ = fs::remove_dir_all(dir.join(top).join("examples"));
86+
let _ = fs::remove_dir_all(dir.join(top).join("native"));
87+
88+
let _ = walk_dir(
89+
dir.join(top),
90+
|sub_dir: &Path| {
91+
if sub_dir
92+
.file_name()
93+
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
94+
.unwrap_or(false)
95+
{
96+
let _ = fs::remove_dir_all(sub_dir);
97+
}
98+
Ok(())
99+
},
100+
|file: &Path| {
101+
if file
102+
.file_name()
103+
.map(|filename| filename.to_str().unwrap().starts_with("libsysroot"))
104+
.unwrap_or(false)
105+
{
106+
let _ = fs::remove_file(file);
107+
}
108+
Ok(())
109+
},
110+
);
111+
}
112+
Ok(())
113+
},
114+
|_| Ok(()),
115+
);
116+
117+
let _ = fs::remove_file("Cargo.lock");
118+
let _ = fs::remove_file("test_target/Cargo.lock");
119+
let _ = fs::remove_dir_all("sysroot");
120+
121+
// Builds libs
122+
let channel = if release_mode {
123+
let rustflags = env
124+
.get("RUSTFLAGS")
125+
.cloned()
126+
.unwrap_or_default();
127+
env.insert(
128+
"RUSTFLAGS".to_string(),
129+
format!("{} -Zmir-opt-level=3", rustflags),
130+
);
131+
run_command_with_output_and_env(
132+
&[
133+
&"cargo",
134+
&"build",
135+
&"--target",
136+
&target_triple,
137+
&"--release",
138+
],
139+
None,
140+
Some(&env),
141+
)?;
142+
"release"
143+
} else {
144+
run_command_with_output_and_env(
145+
&[
146+
&"cargo",
147+
&"build",
148+
&"--target",
149+
&target_triple,
150+
&"--features",
151+
&"compiler_builtins/c",
152+
],
153+
None,
154+
Some(env),
155+
)?;
156+
"debug"
157+
};
158+
159+
// Copy files to sysroot
160+
let sysroot_path = format!("sysroot/lib/rustlib/{}/lib/", target_triple);
161+
fs::create_dir_all(&sysroot_path)
162+
.map_err(|error| format!("Failed to create directory `{}`: {:?}", sysroot_path, error))?;
163+
let copier = |dir_to_copy: &Path| {
164+
run_command(&[&"cp", &"-r", &dir_to_copy, &sysroot_path], None).map(|_| ())
165+
};
166+
walk_dir(
167+
&format!("target/{}/{}/deps", target_triple, channel),
168+
copier,
169+
copier,
170+
)?;
171+
172+
Ok(())
173+
}
174+
175+
fn build_codegen(args: &BuildArg) -> Result<(), String> {
176+
let mut env = HashMap::new();
177+
178+
let current_dir =
179+
std::env::current_dir().map_err(|error| format!("`current_dir` failed: {:?}", error))?;
180+
if let Ok(rt_root) = std::env::var("RUST_COMPILER_RT_ROOT") {
181+
env.insert("RUST_COMPILER_RT_ROOT".to_string(), rt_root);
182+
} else {
183+
env.insert(
184+
"RUST_COMPILER_RT_ROOT".to_string(),
185+
format!("{}", current_dir.join("llvm/compiler-rt").display()),
186+
);
187+
}
188+
env.insert("LD_LIBRARY_PATH".to_string(), args.gcc_path.clone());
189+
env.insert("LIBRARY_PATH".to_string(), args.gcc_path.clone());
190+
191+
let mut command: Vec<&dyn AsRef<OsStr>> = vec![&"cargo", &"rustc"];
192+
if args.codegen_release_channel {
193+
command.push(&"--release");
194+
env.insert("CHANNEL".to_string(), "release".to_string());
195+
env.insert("CARGO_INCREMENTAL".to_string(), "1".to_string());
196+
} else {
197+
env.insert("CHANNEL".to_string(), "debug".to_string());
198+
}
199+
let ref_features = args.features.iter().map(|s| s.as_str()).collect::<Vec<_>>();
200+
for feature in &ref_features {
201+
command.push(feature);
202+
}
203+
run_command_with_env(&command, None, Some(&env))?;
204+
205+
let config = set_config(&mut env, &[], Some(&args.gcc_path))?;
206+
207+
// We voluntarily ignore the error.
208+
let _ = fs::remove_dir_all("target/out");
209+
let gccjit_target = "target/out/gccjit";
210+
fs::create_dir_all(gccjit_target).map_err(|error| {
211+
format!(
212+
"Failed to create directory `{}`: {:?}",
213+
gccjit_target, error
214+
)
215+
})?;
216+
217+
println!("[BUILD] sysroot");
218+
build_sysroot(
219+
&mut env,
220+
args.sysroot_release_channel,
221+
&config.target_triple,
222+
)?;
223+
Ok(())
224+
}
225+
1226
pub fn run() -> Result<(), String> {
227+
let args = match BuildArg::new()? {
228+
Some(args) => args,
229+
None => return Ok(()),
230+
};
231+
build_codegen(&args)?;
2232
Ok(())
3233
}

0 commit comments

Comments
 (0)