3 use std::process::{self, Command};
5 use super::path::{Dirs, RelPath};
6 use super::rustc_info::{get_file_name, get_rustc_version, get_wrapper_file_name};
7 use super::utils::{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");
13 static RUSTLIB_DIR: RelPath = LIB_DIR.join("rustlib");
15 pub(crate) fn build_sysroot(
18 sysroot_kind: SysrootKind,
19 cg_clif_dylib_src: &Path,
23 eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
25 DIST_DIR.ensure_fresh(dirs);
26 BIN_DIR.ensure_exists(dirs);
27 LIB_DIR.ensure_exists(dirs);
30 let cg_clif_dylib_path = if cfg!(windows) {
31 // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
38 .join(get_file_name("rustc_codegen_cranelift", "dylib"));
39 try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
41 // Build and copy rustc and cargo wrappers
42 for wrapper in ["rustc-clif", "rustdoc-clif", "cargo-clif"] {
43 let wrapper_name = get_wrapper_file_name(wrapper, "bin");
45 let mut build_cargo_wrapper_cmd = Command::new("rustc");
46 build_cargo_wrapper_cmd
47 .arg(RelPath::SCRIPTS.to_path(dirs).join(&format!("{wrapper}.rs")))
49 .arg(DIST_DIR.to_path(dirs).join(wrapper_name))
51 spawn_and_wait(build_cargo_wrapper_cmd);
54 let default_sysroot = super::rustc_info::get_default_sysroot();
56 let host_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(host_triple).join("lib");
57 let target_rustlib_lib = RUSTLIB_DIR.to_path(dirs).join(target_triple).join("lib");
58 fs::create_dir_all(&host_rustlib_lib).unwrap();
59 fs::create_dir_all(&target_rustlib_lib).unwrap();
61 if target_triple == "x86_64-pc-windows-gnu" {
62 if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
64 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
65 to compile a sysroot for it.",
69 for file in fs::read_dir(
70 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
74 let file = file.unwrap().path();
75 if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
76 continue; // only copy object files
78 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
83 SysrootKind::None => {} // Nothing to do
84 SysrootKind::Llvm => {
85 for file in fs::read_dir(
86 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
90 let file = file.unwrap().path();
91 let file_name_str = file.file_name().unwrap().to_str().unwrap();
92 if (file_name_str.contains("rustc_")
93 && !file_name_str.contains("rustc_std_workspace_")
94 && !file_name_str.contains("rustc_demangle"))
95 || file_name_str.contains("chalk")
96 || file_name_str.contains("tracing")
97 || file_name_str.contains("regex")
99 // These are large crates that are part of the rustc-dev component and are not
100 // necessary to run regular programs.
103 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
106 if target_triple != host_triple {
107 for file in fs::read_dir(
108 default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
112 let file = file.unwrap().path();
113 try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
117 SysrootKind::Clif => {
118 build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None);
120 if host_triple != target_triple {
121 // When cross-compiling it is often necessary to manually pick the right linker
122 let linker = match target_triple {
123 "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu-gcc"),
124 "s390x-unknown-linux-gnu" => Some("s390x-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, LIB_DIR.to_path(dirs).join(file.file_name().unwrap()));
149 // FIXME move to download/ or dist/
150 pub(crate) static SYSROOT_RUSTC_VERSION: RelPath = RelPath::BUILD_SYSROOT.join("rustc_version");
151 pub(crate) static SYSROOT_SRC: RelPath = RelPath::BUILD_SYSROOT.join("sysroot_src");
152 static STANDARD_LIBRARY: CargoProject = CargoProject::new(&RelPath::BUILD_SYSROOT, "build_sysroot");
154 fn build_clif_sysroot_for_triple(
158 cg_clif_dylib_path: &Path,
159 linker: Option<&str>,
161 match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
163 eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
164 eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
167 Ok(source_version) => {
168 let rustc_version = get_rustc_version();
169 if source_version != rustc_version {
170 eprintln!("The patched sysroot source is outdated");
171 eprintln!("Source version: {}", source_version.trim());
172 eprintln!("Rustc version: {}", rustc_version.trim());
173 eprintln!("Hint: Try `./y.rs prepare` to update the patched sysroot source");
179 let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel);
181 if !super::config::get_bool("keep_sysroot") {
182 // Cleanup the deps dir, but keep build scripts and the incremental cache for faster
183 // recompilation as they are not affected by changes in cg_clif.
184 if build_dir.join("deps").exists() {
185 fs::remove_dir_all(build_dir.join("deps")).unwrap();
190 let mut rustflags = "-Zforce-unstable-if-unmarked -Cpanic=abort".to_string();
191 rustflags.push_str(&format!(" -Zcodegen-backend={}", cg_clif_dylib_path.to_str().unwrap()));
192 rustflags.push_str(&format!(" --sysroot={}", DIST_DIR.to_path(dirs).to_str().unwrap()));
193 if channel == "release" {
194 rustflags.push_str(" -Zmir-opt-level=3");
196 if let Some(linker) = linker {
198 write!(rustflags, " -Clinker={}", linker).unwrap();
200 let mut compiler = Compiler::with_triple(triple.to_owned());
201 compiler.rustflags = rustflags;
202 let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
203 if channel == "release" {
204 build_cmd.arg("--release");
206 build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
207 spawn_and_wait(build_cmd);
209 // Copy all relevant files to the sysroot
210 for entry in fs::read_dir(build_dir.join("deps")).unwrap() {
211 let entry = entry.unwrap();
212 if let Some(ext) = entry.path().extension() {
213 if ext == "rmeta" || ext == "d" || ext == "dSYM" || ext == "clif" {
221 RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()),