3 use std::process::{self, Command};
5 use super::path::{Dirs, RelPath};
6 use super::rustc_info::{
7 get_file_name, get_rustc_version, get_toolchain_name, get_wrapper_file_name,
9 use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
10 use super::SysrootKind;
12 static DIST_DIR: RelPath = RelPath::DIST;
13 static BIN_DIR: RelPath = RelPath::DIST.join("bin");
14 static LIB_DIR: RelPath = RelPath::DIST.join("lib");
15 static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib");
17 pub(crate) fn build_sysroot(
20 sysroot_kind: SysrootKind,
21 cg_clif_dylib_src: &Path,
22 bootstrap_host_compiler: &Compiler,
23 target_triple: String,
25 eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
27 DIST_DIR.ensure_fresh(dirs);
28 BIN_DIR.ensure_exists(dirs);
29 LIB_DIR.ensure_exists(dirs);
31 let is_native = bootstrap_host_compiler.triple == target_triple;
34 let cg_clif_dylib_path = if cfg!(windows) {
35 // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
42 .join(get_file_name("rustc_codegen_cranelift", "dylib"));
43 try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
45 // Build and copy rustc and cargo wrappers
46 for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
47 let wrapper_name = get_wrapper_file_name(wrapper, "bin");
49 let mut build_cargo_wrapper_cmd = Command::new(&bootstrap_host_compiler.rustc);
50 build_cargo_wrapper_cmd
51 .env("TOOLCHAIN_NAME", get_toolchain_name())
52 .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
54 .arg(DIST_DIR.to_path(dirs).join(wrapper_name))
56 spawn_and_wait(build_cargo_wrapper_cmd);
59 let default_sysroot = super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc);
61 let host_rustlib_lib =
62 RUSTLIB_DIR.to_path(dirs).join(&bootstrap_host_compiler.triple).join("lib");
63 let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(&target_triple).join("lib");
64 fs::create_dir_all(&host_rustlib_lib).unwrap();
65 fs::create_dir_all(&target_rustlib_lib).unwrap();
67 if target_triple.ends_with("windows-gnu") {
68 eprintln!("[BUILD] rtstartup for {target_triple}");
70 let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup");
72 for file in ["rsbegin", "rsend"] {
73 let mut build_rtstartup_cmd = Command::new(&bootstrap_host_compiler.rustc);
79 .arg(target_rustlib_lib.join(format!("{file}.o")))
80 .arg(rtstartup_src.join(format!("{file}.rs")));
81 spawn_and_wait(build_rtstartup_cmd);
86 SysrootKind::None => {} // Nothing to do
87 SysrootKind::Llvm => {
88 for file in fs::read_dir(
92 .join(&bootstrap_host_compiler.triple)
97 let file = file.unwrap().path();
98 let file_name_str = file.file_name().unwrap().to_str().unwrap();
99 if (file_name_str.contains("rustc_")
100 && !file_name_str.contains("rustc_std_workspace_")
101 && !file_name_str.contains("rustc_demangle"))
102 || file_name_str.contains("chalk")
103 || file_name_str.contains("tracing")
104 || file_name_str.contains("regex")
106 // These are large crates that are part of the rustc-dev component and are not
107 // necessary to run regular programs.
110 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
114 for file in fs::read_dir(
115 default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"),
119 let file = file.unwrap().path();
120 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
124 SysrootKind::Clif => {
125 build_clif_sysroot_for_triple(
128 bootstrap_host_compiler.clone(),
133 build_clif_sysroot_for_triple(
137 let mut bootstrap_target_compiler = bootstrap_host_compiler.clone();
138 bootstrap_target_compiler.triple = target_triple.clone();
139 bootstrap_target_compiler.set_cross_linker_and_runner();
140 bootstrap_target_compiler
146 // Copy std for the host to the lib dir. This is necessary for the jit mode to find
148 for file in fs::read_dir(host_rustlib_lib).unwrap() {
149 let file = file.unwrap().path();
150 let filename = file.file_name().unwrap().to_str().unwrap();
151 if filename.contains("std-") && !filename.contains(".rlib") {
152 try_hard_link(&file, LIB_DIR.to_path(dirs).join(file.file_name().unwrap()));
158 let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
160 target_compiler.set_cross_linker_and_runner();
165 pub(crate) static ORIG_BUILD_SYSROOT: RelPath = RelPath::SOURCE.join("build_sysroot");
166 pub(crate) static BUILD_SYSROOT: RelPath = RelPath::DOWNLOAD.join("sysroot");
167 pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = BUILD_SYSROOT.join("rustc_version");
168 pub(crate) static SYSROOT_SRC: RelPath = BUILD_SYSROOT.join("sysroot_src");
169 pub(crate) static STANDARD_LIBRARY: CargoProject =
170 CargoProject::new(&BUILD_SYSROOT, "build_sysroot");
172 fn build_clif_sysroot_for_triple(
175 mut compiler: Compiler,
176 cg_clif_dylib_path: &Path,
178 match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
180 eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
181 eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
184 Ok(source_version) => {
185 let rustc_version = get_rustc_version(&compiler.rustc);
186 if source_version != rustc_version {
187 eprintln!("The patched sysroot source is outdated");
188 eprintln!("Source version: {}", source_version.trim());
189 eprintln!("Rustc version: {}", rustc_version.trim());
190 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
196 let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
198 if !super::config::get_bool("keep_sysroot") {
199 // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
200 // recompilation as they are not affected by changes in cg_clif.
201 if build_dir.join("deps").exists() {
202 fs::remove_dir_all(build_dir.join("deps")).unwrap();
207 let mut rustflags = " -Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
208 rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
209 // Necessary for MinGW to find rsbegin.o and rsend.o
210 rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
211 if channel == "release" {
212 rustflags.push_str(" -Zmir-opt-level=3");
214 compiler.rustflags += &rustflags;
215 let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
216 if channel == "release" {
217 build_cmd.arg("--release");
219 build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
220 spawn_and_wait(build_cmd);
222 // Copy all relevant files to the sysroot
223 for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
224 let entry = entry.unwrap();
225 if let Some(ext) = entry.path().extension() {
226 if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
234 RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()),