Skip to content

Commit ad971bb

Browse files
committed
Rewrite build_sysroot.sh in rust
1 parent d71b125 commit ad971bb

File tree

3 files changed

+53
-54
lines changed

3 files changed

+53
-54
lines changed

build_sysroot/build_sysroot.sh

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

build_system/build_sysroot.rs

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use crate::utils::spawn_and_wait;
12
use crate::utils::try_hard_link;
23
use crate::SysrootKind;
3-
use std::env;
44
use std::fs;
55
use std::path::Path;
66
use std::process::{self, Command};
@@ -100,24 +100,14 @@ pub(crate) fn build_sysroot(
100100
}
101101
}
102102
SysrootKind::Clif => {
103-
let cwd = env::current_dir().unwrap();
104-
105-
let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
106-
cmd.current_dir(target_dir).env("TARGET_TRIPLE", target_triple);
107-
eprintln!("[BUILD] sysroot");
108-
if !cmd.spawn().unwrap().wait().unwrap().success() {
109-
process::exit(1);
110-
}
103+
build_clif_sysroot_for_triple(channel, target_dir, target_triple);
111104

112105
if host_triple != target_triple {
113-
let mut cmd = Command::new(cwd.join("build_sysroot").join("build_sysroot.sh"));
114-
cmd.current_dir(target_dir).env("TARGET_TRIPLE", host_triple);
115-
eprintln!("[BUILD] sysroot");
116-
if !cmd.spawn().unwrap().wait().unwrap().success() {
117-
process::exit(1);
118-
}
106+
build_clif_sysroot_for_triple(channel, target_dir, host_triple);
119107
}
120108

109+
// Copy std for the host to the lib dir. This is necessary for the jit mode to find
110+
// libstd.
121111
for file in fs::read_dir(host_rustlib_lib).unwrap() {
122112
let file = file.unwrap().path();
123113
if file.file_name().unwrap().to_str().unwrap().contains("std-") {
@@ -127,3 +117,49 @@ pub(crate) fn build_sysroot(
127117
}
128118
}
129119
}
120+
121+
fn build_clif_sysroot_for_triple(channel: &str, target_dir: &Path, triple: &str) {
122+
let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
123+
124+
// FIXME add option to skip this
125+
// Cleanup the target dir with the exception of build scripts and the incremental cache
126+
for dir in ["build", "deps", "examples", "native"] {
127+
if build_dir.join(dir).exists() {
128+
fs::remove_dir_all(build_dir.join(dir)).unwrap();
129+
}
130+
}
131+
132+
// Build sysroot
133+
let mut build_cmd = Command::new("cargo");
134+
build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
135+
let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
136+
if channel == "release" {
137+
build_cmd.arg("--release");
138+
rustflags.push_str(" -Zmir-opt-level=3");
139+
}
140+
build_cmd.env("RUSTFLAGS", rustflags);
141+
build_cmd
142+
.env("RUSTC", target_dir.join("bin").join("cg_clif_build_sysroot").canonicalize().unwrap());
143+
// FIXME Enable incremental again once rust-lang/rust#74946 is fixed
144+
build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
145+
spawn_and_wait(build_cmd);
146+
147+
// Copy all relevant files to the sysroot
148+
for entry in
149+
fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
150+
.unwrap()
151+
{
152+
let entry = entry.unwrap();
153+
if let Some(ext) = entry.path().extension() {
154+
if ext == "rmeta" || ext == "d" || ext == "dSYM" {
155+
continue;
156+
}
157+
} else {
158+
continue;
159+
};
160+
try_hard_link(
161+
entry.path(),
162+
target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
163+
);
164+
}
165+
}

y.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ enum SysrootKind {
6666
fn main() {
6767
env::set_var("CG_CLIF_DISPLAY_CG_TIME", "1");
6868
env::set_var("CG_CLIF_DISABLE_INCR_CACHE", "1");
69+
// The target dir is expected in the default location. Guard against the user changing it.
70+
env::set_var("CARGO_TARGET_DIR", "target");
6971

7072
let mut args = env::args().skip(1);
7173
let command = match args.next().as_deref() {

0 commit comments

Comments
 (0)