]> git.lizzy.rs Git - rust.git/blob - build_system/build_sysroot.rs
Build rtstartup for MinGW from scratch
[rust.git] / 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::{
7     get_file_name, get_rustc_version, get_toolchain_name, get_wrapper_file_name,
8 };
9 use super::utils::{spawn_and_wait, try_hard_link, CargoProject, Compiler};
10 use super::SysrootKind;
11
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");
16
17 pub(crate) fn build_sysroot(
18     dirs: &Dirs,
19     channel: &str,
20     sysroot_kind: SysrootKind,
21     cg_clif_dylib_src: &Path,
22     bootstrap_host_compiler: &Compiler,
23     target_triple: String,
24 ) -> Compiler {
25     eprintln!("[BUILD] sysroot {:?}", sysroot_kind);
26
27     DIST_DIR.ensure_fresh(dirs);
28     BIN_DIR.ensure_exists(dirs);
29     LIB_DIR.ensure_exists(dirs);
30
31     let is_native = bootstrap_host_compiler.triple == target_triple;
32
33     // Copy the backend
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
36         // binaries.
37         BIN_DIR
38     } else {
39         LIB_DIR
40     }
41     .to_path(dirs)
42     .join(get_file_name("rustc_codegen_cranelift", "dylib"));
43     try_hard_link(cg_clif_dylib_src, &cg_clif_dylib_path);
44
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");
48
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")))
53             .arg("-o")
54             .arg(DIST_DIR.to_path(dirs).join(wrapper_name))
55             .arg("-g");
56         spawn_and_wait(build_cargo_wrapper_cmd);
57     }
58
59     let default_sysroot = super::rustc_info::get_default_sysroot(&bootstrap_host_compiler.rustc);
60
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();
66
67     if target_triple.ends_with("windows-gnu") {
68         eprintln!("[BUILD] rtstartup for {target_triple}");
69
70         let rtstartup_src = SYSROOT_SRC.to_path(dirs).join("library").join("rtstartup");
71
72         for file in ["rsbegin", "rsend"] {
73             let mut build_rtstartup_cmd = Command::new(&bootstrap_host_compiler.rustc);
74             build_rtstartup_cmd
75                 .arg("--target")
76                 .arg(&target_triple)
77                 .arg("--emit=obj")
78                 .arg("-o")
79                 .arg(target_rustlib_lib.join(format!("{file}.o")))
80                 .arg(rtstartup_src.join(format!("{file}.rs")));
81             spawn_and_wait(build_rtstartup_cmd);
82         }
83     }
84
85     match sysroot_kind {
86         SysrootKind::None => {} // Nothing to do
87         SysrootKind::Llvm => {
88             for file in fs::read_dir(
89                 default_sysroot
90                     .join("lib")
91                     .join("rustlib")
92                     .join(&bootstrap_host_compiler.triple)
93                     .join("lib"),
94             )
95             .unwrap()
96             {
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")
105                 {
106                     // These are large crates that are part of the rustc-dev component and are not
107                     // necessary to run regular programs.
108                     continue;
109                 }
110                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
111             }
112
113             if !is_native {
114                 for file in fs::read_dir(
115                     default_sysroot.join("lib").join("rustlib").join(&target_triple).join("lib"),
116                 )
117                 .unwrap()
118                 {
119                     let file = file.unwrap().path();
120                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
121                 }
122             }
123         }
124         SysrootKind::Clif => {
125             build_clif_sysroot_for_triple(
126                 dirs,
127                 channel,
128                 bootstrap_host_compiler.clone(),
129                 &cg_clif_dylib_path,
130             );
131
132             if !is_native {
133                 build_clif_sysroot_for_triple(
134                     dirs,
135                     channel,
136                     {
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
141                     },
142                     &cg_clif_dylib_path,
143                 );
144             }
145
146             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
147             // libstd.
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()));
153                 }
154             }
155         }
156     }
157
158     let mut target_compiler = Compiler::clif_with_triple(&dirs, target_triple);
159     if !is_native {
160         target_compiler.set_cross_linker_and_runner();
161     }
162     target_compiler
163 }
164
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");
171
172 fn build_clif_sysroot_for_triple(
173     dirs: &Dirs,
174     channel: &str,
175     mut compiler: Compiler,
176     cg_clif_dylib_path: &Path,
177 ) {
178     match fs::read_to_string(SYSROOT_RUSTC_VERSION.to_path(dirs)) {
179         Err(e) => {
180             eprintln!("Failed to get rustc version for patched sysroot source: {}", e);
181             eprintln!("Hint: Try `./y.rs prepare` to patch the sysroot source");
182             process::exit(1);
183         }
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");
191                 process::exit(1);
192             }
193         }
194     }
195
196     let build_dir = STANDARD_LIBRARY.target_dir(dirs).join(&compiler.triple).join(channel);
197
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();
203         }
204     }
205
206     // Build sysroot
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");
213     }
214     compiler.rustflags += &rustflags;
215     let mut build_cmd = STANDARD_LIBRARY.build(&compiler, dirs);
216     if channel == "release" {
217         build_cmd.arg("--release");
218     }
219     build_cmd.env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
220     spawn_and_wait(build_cmd);
221
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" {
227                 continue;
228             }
229         } else {
230             continue;
231         };
232         try_hard_link(
233             entry.path(),
234             RUSTLIB_DIR.to_path(dirs).join(&compiler.triple).join("lib").join(entry.file_name()),
235         );
236     }
237 }