3 use std::path::{Path, PathBuf};
4 use std::process::{self, Command};
6 use super::rustc_info::{get_file_name, get_rustc_version};
7 use super::utils::{spawn_and_wait, try_hard_link};
8 use super::SysrootKind;
10 pub(crate) fn build_sysroot(
12 sysroot_kind: SysrootKind,
14 cg_clif_build_dir: PathBuf,
18 if target_dir.exists() {
19 fs::remove_dir_all(target_dir).unwrap();
21 fs::create_dir_all(target_dir.join("bin")).unwrap();
22 fs::create_dir_all(target_dir.join("lib")).unwrap();
25 for file in ["cg_clif", "cg_clif_build_sysroot"] {
27 cg_clif_build_dir.join(get_file_name(file, "bin")),
28 target_dir.join("bin").join(get_file_name(file, "bin")),
32 let cg_clif_dylib = get_file_name("rustc_codegen_cranelift", "dylib");
34 cg_clif_build_dir.join(&cg_clif_dylib),
36 .join(if cfg!(windows) {
37 // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
46 // Build and copy cargo wrapper
47 let mut build_cargo_wrapper_cmd = Command::new("rustc");
48 build_cargo_wrapper_cmd
49 .arg("scripts/cargo-clif.rs")
51 .arg(target_dir.join("cargo-clif"))
53 spawn_and_wait(build_cargo_wrapper_cmd);
55 let default_sysroot = super::rustc_info::get_default_sysroot();
57 let rustlib = target_dir.join("lib").join("rustlib");
58 let host_rustlib_lib = rustlib.join(host_triple).join("lib");
59 let target_rustlib_lib = rustlib.join(target_triple).join("lib");
60 fs::create_dir_all(&host_rustlib_lib).unwrap();
61 fs::create_dir_all(&target_rustlib_lib).unwrap();
63 if target_triple == "x86_64-pc-windows-gnu" {
64 if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
66 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
67 to compile a sysroot for it.",
71 for file in fs::read_dir(
72 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
76 let file = file.unwrap().path();
77 if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
78 continue; // only copy object files
80 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
85 SysrootKind::None => {} // Nothing to do
86 SysrootKind::Llvm => {
87 for file in fs::read_dir(
88 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
92 let file = file.unwrap().path();
93 let file_name_str = file.file_name().unwrap().to_str().unwrap();
94 if (file_name_str.contains("rustc_")
95 && !file_name_str.contains("rustc_std_workspace_")
96 && !file_name_str.contains("rustc_demangle"))
97 || file_name_str.contains("chalk")
98 || file_name_str.contains("tracing")
99 || file_name_str.contains("regex")
101 // These are large crates that are part of the rustc-dev component and are not
102 // necessary to run regular programs.
105 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
108 if target_triple != host_triple {
109 for file in fs::read_dir(
110 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
114 let file = file.unwrap().path();
115 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
119 SysrootKind::Clif => {
120 build_clif_sysroot_for_triple(channel, target_dir, host_triple, None);
122 if host_triple != target_triple {
123 // When cross-compiling it is often necessary to manually pick the right linker
124 let linker = if target_triple == "aarch64-unknown-linux-gnu" {
125 Some("aarch64-linux-gnu-gcc")
129 build_clif_sysroot_for_triple(channel, target_dir, target_triple, linker);
132 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
134 for file in fs::read_dir(host_rustlib_lib).unwrap() {
135 let file = file.unwrap().path();
136 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
137 try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
144 fn build_clif_sysroot_for_triple(
148 linker: Option<&str>,
150 match fs::read_to_string(Path::new("build_sysroot").join("rustc_version")) {
152 eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
153 eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
156 Ok(source_version) => {
157 let rustc_version = get_rustc_version();
158 if source_version != rustc_version {
159 eprintln!("The patched sysroot source is outdated");
160 eprintln!("Source version: {}", source_version.trim());
161 eprintln!("Rustc version: {}", rustc_version.trim());
162 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
168 let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
170 if !super::config::get_bool("keep_sysroot") {
171 // Cleanup the target dir with the exception of build scripts and the incremental cache
172 for dir in ["build", "deps", "examples", "native"] {
173 if build_dir.join(dir).exists() {
174 fs::remove_dir_all(build_dir.join(dir)).unwrap();
180 let mut build_cmd = Command::new("cargo");
181 build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
182 let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
183 if channel == "release" {
184 build_cmd.arg("--release");
185 rustflags.push_str(" -Zmir-opt-level=3");
187 if let Some(linker) = linker {
189 write!(rustflags, " -Clinker={}", linker).unwrap();
191 build_cmd.env("RUSTFLAGS", rustflags);
194 env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
196 build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
197 spawn_and_wait(build_cmd);
199 // Copy all relevant files to the sysroot
201 fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
204 let entry = entry.unwrap();
205 if let Some(ext) = entry.path().extension() {
206 if ext == "rmeta" || ext == "d" || ext == "dSYM" {
214 target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),