2 use std::path::{Path, PathBuf};
3 use std::process::{self, Command};
5 use super::path::{Dirs, RelPath};
6 use super::rustc_info::{get_file_name, get_rustc_version, get_toolchain_name};
7 use super::utils::{remove_dir_if_exists, spawn_and_wait, try_hard_link, CargoProject, Compiler};
8 use super::SysrootKind;
10 static DIST_DIR: RelPath = RelPath::DIST;
11 static BIN_DIR: RelPath = RelPath::DIST.join("bin");
12 static LIB_DIR: RelPath = RelPath::DIST.join("lib");
14 pub(crate) fn build_sysroot(
17 sysroot_kind: SysrootKind,
18 cg_clif_dylib_src: &Path,
19 bootstrap_host_compiler: &Compiler,
20 target_triple: String,
22 eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
24 DIST_DIR.ensure_fresh(dirs);
25 BIN_DIR.ensure_exists(dirs);
26 LIB_DIR.ensure_exists(dirs);
28 let is_native = bootstrap_host_compiler.triple == target_triple;
31 let cg_clif_dylib_path = if cfg!(windows) {
32 // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
39 .join(cg_clif_dylib_src.file_name().unwrap());
40 try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
42 // Build and copy rustc and cargo wrappers
43 let wrapper_base_name = get_file_name("____", "bin");
44 let toolchain_name = get_toolchain_name();
45 for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
46 let wrapper_name = wrapper_base_name.replace("____", wrapper);
48 let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
49 build_cargo_wrapper_cmd
50 .env("TOOLCHAIN_NAME", toolchain_name.clone())
51 .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
53 .arg(DIST_DIR.to_path(dirs).join(wrapper_name))
54 .arg("-Cstrip=debuginfo");
55 spawn_and_wait(build_cargo_wrapper_cmd);
58 let host = build_sysroot_for_triple(
61 bootstrap_host_compiler.clone(),
65 host.install_into_sysroot(&DIST_DIR.to_path(dirs));
68 build_sysroot_for_triple(
72 let mut bootstrap_target_compiler = bootstrap_host_compiler.clone();
73 bootstrap_target_compiler.triple = target_triple.clone();
74 bootstrap_target_compiler.set_cross_linker_and_runner();
75 bootstrap_target_compiler
80 .install_into_sysroot(&DIST_DIR.to_path(dirs));
83 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
85 for lib in host.libs {
86 let filename = lib.file_name().unwrap().to_str().unwrap();
87 if filename.contains("std-") && !filename.contains(".rlib") {
88 try_hard_link(&lib, LIB_DIR.to_path(dirs).join(lib.file_name().unwrap()));
92 let mut target_compiler = {
93 let dirs: &Dirs = &dirs;
95 RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustc-clif"));
97 RelPath::DIST.to_path(&dirs).join(wrapper_base_name.replace("____", "rustdoc-clif"));
100 cargo: bootstrap_host_compiler.cargo.clone(),
101 rustc: rustc_clif.clone(),
102 rustdoc: rustdoc_clif.clone(),
103 rustflags: String::new(),
104 rustdocflags: String::new(),
105 triple: target_triple,
110 target_compiler.set_cross_linker_and_runner();
115 struct SysrootTarget {
121 fn install_into_sysroot(&self, sysroot: &Path) {
122 if self.libs.is_empty() {
126 let target_rustlib_lib = sysroot.join("lib").join("rustlib").join(&self.triple).join("lib");
127 fs::create_dir_all(&target_rustlib_lib).unwrap();
129 for lib in &self.libs {
130 try_hard_link(lib, target_rustlib_lib.join(lib.file_name().unwrap()));
135 pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
136 pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot");
137 pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version");
138 pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src");
139 pub(crate) static STANDARD_LIBRARY: CargoProject =
140 CargoProject::new(&BUILD_SYSROOT, "build_sysroot");
141 pub(crate) static RTSTARTUP_SYSROOT: RelPath = RelPath::BUILD.join("rtstartup");
144 fn build_sysroot_for_triple(
148 cg_clif_dylib_path: &Path,
149 sysroot_kind: SysrootKind,
152 SysrootKind::None => build_rtstartup(dirs, &compiler)
153 .unwrap_or(SysrootTarget { triple: compiler.triple, libs: vec![] }),
154 SysrootKind::Llvm => build_llvm_sysroot_for_triple(compiler),
155 SysrootKind::Clif => {
156 build_clif_sysroot_for_triple(dirs, channel, compiler, &cg_clif_dylib_path)
162 fn build_llvm_sysroot_for_triple(compiler: Compiler) -> SysrootTarget {
163 let default_sysroot = super::rustc_info::get_default_sysroot(&compiler.rustc);
165 let mut target_libs = SysrootTarget { triple: compiler.triple, libs: vec![] };
167 for entry in fs::read_dir(
168 default_sysroot.join("lib").join("rustlib").join(&target_libs.triple).join("lib"),
172 let entry = entry.unwrap();
173 if entry.file_type().unwrap().is_dir() {
176 let file = entry.path();
177 let file_name_str = file.file_name().unwrap().to_str().unwrap();
178 if (file_name_str.contains("rustc_")
179 && !file_name_str.contains("rustc_std_workspace_")
180 && !file_name_str.contains("rustc_demangle"))
181 || file_name_str.contains("chalk")
182 || file_name_str.contains("tracing")
183 || file_name_str.contains("regex")
185 // These are large crates that are part of the rustc-dev component and are not
186 // necessary to run regular programs.
189 target_libs.libs.push(file);
196 fn build_clif_sysroot_for_triple(
199 mut compiler: Compiler,
200 cg_clif_dylib_path: &Path,
202 match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
204 eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
205 eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
208 Ok(source_version) => {
209 let rustc_version = get_rustc_version(&compiler.rustc);
210 if source_version != rustc_version {
211 eprintln!("The patched sysroot source is outdated");
212 eprintln!("Source version: {}", source_version.trim());
213 eprintln!("Rustc version: {}", rustc_version.trim());
214 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
220 let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
222 if let Some(rtstartup_target_libs) = build_rtstartup(dirs, &compiler) {
223 rtstartup_target_libs.install_into_sysroot(&RTSTARTUP_SYSROOT.to_path(dirs));
225 target_libs.libs.extend(rtstartup_target_libs.libs);
228 let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
230 if !super::config::get_bool("keep_sysroot") {
231 // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
232 // recompilation as they are not affected by changes in cg_clif.
233 remove_dir_if_exists(&build_dir.join("deps"));
237 let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
238 rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
239 // Necessary for MinGW to find rsbegin.o and rsend.o
241 .push_str(&format!(" --sysroot={}", RTSTARTUP_SYSROOT.to_path(dirs).to_str().unwrap()));
242 if channel == "release" {
243 rustflags.push_str(" -Zmir-opt-level=3");
245 compiler.rustflags += &rustflags;
246 let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
247 if channel == "release" {
248 build_cmd.arg("--release");
250 build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
251 if compiler.triple.contains("apple") {
252 build_cmd.env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed");
254 spawn_and_wait(build_cmd);
256 for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
257 let entry = entry.unwrap();
258 if let Some(ext) = entry.path().extension() {
259 if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
265 target_libs.libs.push(entry.path());
271 fn build_rtstartup(dirs: &Dirs, compiler: &Compiler) -> Option<SysrootTarget> {
272 if !compiler.triple.ends_with("windows-gnu") {
276 RTSTARTUP_SYSROOT.ensure_fresh(dirs);
278 let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup");
279 let mut target_libs = SysrootTarget { triple: compiler.triple.clone(), libs: vec![] };
281 for file in ["rsbegin", "rsend"] {
282 let obj = RTSTARTUP_SYSROOT.to_path(dirs).join(format!("{file}.o"));
283 let mut build_rtstartup_cmd = Command::new(&compiler.rustc);
286 .arg(&compiler.triple)
290 .arg(rtstartup_src.join(format!("{file}.rs")));
291 spawn_and_wait(build_rtstartup_cmd);
292 target_libs.libs.push(obj.clone());