]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/build_sysroot.rs
Rollup merge of #104402 - joboet:sync_remutex, r=m-ou-se
[rust.git] / compiler / rustc_codegen_cranelift / build_system / build_sysroot.rs
1 use std::fs;
2 use std::path::Path;
3 use std::process::{self, Command};
4
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;
9
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");
14
15 pub(crate) fn build_sysroot(
16     dirs: &Dirs,
17     channel: &str,
18     sysroot_kind: SysrootKind,
19     cg_clif_dylib_src: &Path,
20     host_triple: &str,
21     target_triple: &str,
22 ) {
23     eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
24
25     DIST_DIR.ensure_fresh(dirs);
26     BIN_DIR.ensure_exists(dirs);
27     LIB_DIR.ensure_exists(dirs);
28
29     // Copy the backend
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
32         // binaries.
33         BIN_DIR
34     } else {
35         LIB_DIR
36     }
37     .to_path(dirs)
38     .join(get_file_name("rustc_codegen_cranelift", "dylib"));
39     try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
40
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");
44
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")))
48             .arg("-o")
49             .arg(DIST_DIR.to_path(dirs).join(wrapper_name))
50             .arg("-g");
51         spawn_and_wait(build_cargo_wrapper_cmd);
52     }
53
54     let default_sysroot = super::rustc_info::get_default_sysroot();
55
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();
60
61     if target_triple == "x86_64-pc-windows-gnu" {
62         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
63             eprintln!(
64                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
65                 to compile a sysroot for it.",
66             );
67             process::exit(1);
68         }
69         for file in fs::read_dir(
70             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
71         )
72         .unwrap()
73         {
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
77             }
78             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
79         }
80     }
81
82     match sysroot_kind {
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"),
87             )
88             .unwrap()
89             {
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")
98                 {
99                     // These are large crates that are part of the rustc-dev component and are not
100                     // necessary to run regular programs.
101                     continue;
102                 }
103                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
104             }
105
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"),
109                 )
110                 .unwrap()
111                 {
112                     let file = file.unwrap().path();
113                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
114                 }
115             }
116         }
117         SysrootKind::Clif => {
118             build_clif_sysroot_for_triple(dirs, channel, host_triple, &cg_clif_dylib_path, None);
119
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"),
125                     _ => None,
126                 };
127                 build_clif_sysroot_for_triple(
128                     dirs,
129                     channel,
130                     target_triple,
131                     &cg_clif_dylib_path,
132                     linker,
133                 );
134             }
135
136             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
137             // libstd.
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()));
143                 }
144             }
145         }
146     }
147 }
148
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");
153
154 fn build_clif_sysroot_for_triple(
155     dirs: &Dirs,
156     channel: &str,
157     triple: &str,
158     cg_clif_dylib_path: &Path,
159     linker: Option<&str>,
160 ) {
161     match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
162         Err(e) => {
163             eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
164             eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
165             process::exit(1);
166         }
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");
174                 process::exit(1);
175             }
176         }
177     }
178
179     let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(triple).join(channel);
180
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();
186         }
187     }
188
189     // Build sysroot
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");
195     }
196     if let Some(linker) = linker {
197         use std::fmt::Write;
198         write!(rustflags, " -Clinker={}", linker).unwrap();
199     }
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");
205     }
206     build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
207     spawn_and_wait(build_cmd);
208
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" {
214                 continue;
215             }
216         } else {
217             continue;
218         };
219         try_hard_link(
220             entry.path(),
221             RUSTLIB_DIR.to_path(dirs).join(triple).join("lib").join(entry.file_name()),
222         );
223     }
224 }