2 use std::path::{Path, PathBuf};
3 use std::process::{self, Command};
5 use super::rustc_info::{get_file_name, get_rustc_version};
6 use super::utils::{spawn_and_wait, try_hard_link};
7 use super::SysrootKind;
9 pub(crate) fn build_sysroot(
11 sysroot_kind: SysrootKind,
13 cg_clif_build_dir: PathBuf,
17 if target_dir.exists() {
18 fs::remove_dir_all(target_dir).unwrap();
20 fs::create_dir_all(target_dir.join("bin")).unwrap();
21 fs::create_dir_all(target_dir.join("lib")).unwrap();
24 let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
25 let cg_clif_dylib_path = target_dir
26 .join(if cfg!(windows) {
27 // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
33 .join(&cg_clif_dylib);
34 try_hard_link(cg_clif_build_dir.join(cg_clif_dylib), &cg_clif_dylib_path);
36 // Build and copy rustc and cargo wrappers
37 for wrapper in ["rustc-clif", "cargo-clif"] {
38 let mut build_cargo_wrapper_cmd = Command::new("rustc");
39 build_cargo_wrapper_cmd
40 .arg(PathBuf::from("scripts").join(format!("{wrapper}.rs")))
42 .arg(target_dir.join(wrapper))
44 spawn_and_wait(build_cargo_wrapper_cmd);
47 let default_sysroot = super::rustc_info::get_default_sysroot();
49 let rustlib = target_dir.join("lib").join("rustlib");
50 let host_rustlib_lib = rustlib.join(host_triple).join("lib");
51 let target_rustlib_lib = rustlib.join(target_triple).join("lib");
52 fs::create_dir_all(&host_rustlib_lib).unwrap();
53 fs::create_dir_all(&target_rustlib_lib).unwrap();
55 if target_triple == "x86_64-pc-windows-gnu" {
56 if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
58 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
59 to compile a sysroot for it.",
63 for file in fs::read_dir(
64 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
68 let file = file.unwrap().path();
69 if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
70 continue; // only copy object files
72 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
77 SysrootKind::None => {} // Nothing to do
78 SysrootKind::Llvm => {
79 for file in fs::read_dir(
80 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
84 let file = file.unwrap().path();
85 let file_name_str = file.file_name().unwrap().to_str().unwrap();
86 if (file_name_str.contains("rustc_")
87 && !file_name_str.contains("rustc_std_workspace_")
88 && !file_name_str.contains("rustc_demangle"))
89 || file_name_str.contains("chalk")
90 || file_name_str.contains("tracing")
91 || file_name_str.contains("regex")
93 // These are large crates that are part of the rustc-dev component and are not
94 // necessary to run regular programs.
97 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
100 if target_triple != host_triple {
101 for file in fs::read_dir(
102 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
106 let file = file.unwrap().path();
107 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
111 SysrootKind::Clif => {
112 build_clif_sysroot_for_triple(
120 if host_triple != target_triple {
121 // When cross-compiling it is often necessary to manually pick the right linker
122 let linker = if target_triple == "aarch64-unknown-linux-gnu" {
123 Some("aarch64-linux-gnu-gcc")
127 build_clif_sysroot_for_triple(
136 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
138 for file in fs::read_dir(host_rustlib_lib).unwrap() {
139 let file = file.unwrap().path();
140 let filename = file.file_name().unwrap().to_str().unwrap();
141 if filename.contains("std-") && !filename.contains(".rlib") {
142 try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
149 fn build_clif_sysroot_for_triple(
153 cg_clif_dylib_path: &Path,
154 linker: Option<&str>,
156 match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
158 eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
159 eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
162 Ok(source_version) => {
163 let rustc_version = get_rustc_version();
164 if source_version != rustc_version {
165 eprintln!("The patched sysroot source is outdated");
166 eprintln!("Source version: {}", source_version.trim());
167 eprintln!("Rustc version: {}", rustc_version.trim());
168 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
174 let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
176 if !super::config::get_bool("keep_sysroot") {
177 // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
178 // recompilation as they are not affected by changes in cg_clif.
179 if build_dir.join("deps").exists() {
180 fs::remove_dir_all(build_dir.join("deps")).unwrap();
185 let mut build_cmd = Command::new("cargo");
186 build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
187 let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
188 rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
189 if channel == "release" {
190 build_cmd.arg("--release");
191 rustflags.push_str(" -Zmir-opt-level=3");
193 if let Some(linker) = linker {
195 write!(rustflags, " -Clinker={}", linker).unwrap();
197 build_cmd.env("RUSTFLAGS", rustflags);
198 build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
199 spawn_and_wait(build_cmd);
201 // Copy all relevant files to the sysroot
203 fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
206 let entry = entry.unwrap();
207 if let Some(ext) = entry.path().extension() {
208 if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
216 target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),