]> git.lizzy.rs Git - rust.git/blob - build_system/build_sysroot.rs
Rewrite cargo.sh in rust
[rust.git] / build_system / build_sysroot.rs
1 use std::env;
2 use std::fs;
3 use std::path::Path;
4 use std::process::{self, Command};
5
6 use crate::rustc_info::get_file_name;
7 use crate::utils::{spawn_and_wait, try_hard_link};
8 use crate::SysrootKind;
9
10 pub(crate) fn build_sysroot(
11     channel: &str,
12     sysroot_kind: SysrootKind,
13     target_dir: &Path,
14     cg_clif_dylib: String,
15     host_triple: &str,
16     target_triple: &str,
17 ) {
18     if target_dir.exists() {
19         fs::remove_dir_all(target_dir).unwrap();
20     }
21     fs::create_dir_all(target_dir.join("bin")).unwrap();
22     fs::create_dir_all(target_dir.join("lib")).unwrap();
23
24     // Copy the backend
25     for file in ["cg_clif", "cg_clif_build_sysroot"] {
26         try_hard_link(
27             Path::new("target").join(channel).join(get_file_name(file, "bin")),
28             target_dir.join("bin").join(get_file_name(file, "bin")),
29         );
30     }
31
32     if cfg!(windows) {
33         // Windows doesn't have rpath support, so the cg_clif dylib needs to be next to the
34         // binaries.
35         try_hard_link(
36             Path::new("target").join(channel).join(&cg_clif_dylib),
37             target_dir.join("bin").join(cg_clif_dylib),
38         );
39     } else {
40         try_hard_link(
41             Path::new("target").join(channel).join(&cg_clif_dylib),
42             target_dir.join("lib").join(cg_clif_dylib),
43         );
44     }
45
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.rs")
50         .arg("-o")
51         .arg(target_dir.join("cargo"))
52         .arg("-g");
53     spawn_and_wait(build_cargo_wrapper_cmd);
54
55     let default_sysroot = crate::rustc_info::get_default_sysroot();
56
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();
62
63     if target_triple == "x86_64-pc-windows-gnu" {
64         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
65             eprintln!(
66                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
67                 to compile a sysroot for it.",
68             );
69             process::exit(1);
70         }
71         for file in fs::read_dir(
72             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
73         )
74         .unwrap()
75         {
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
79             }
80             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
81         }
82     }
83
84     match sysroot_kind {
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"),
89             )
90             .unwrap()
91             {
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("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(channel, target_dir, target_triple);
119
120             if host_triple != target_triple {
121                 build_clif_sysroot_for_triple(channel, target_dir, host_triple);
122             }
123
124             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
125             // libstd.
126             for file in fs::read_dir(host_rustlib_lib).unwrap() {
127                 let file = file.unwrap().path();
128                 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
129                     try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
130                 }
131             }
132         }
133     }
134 }
135
136 fn build_clif_sysroot_for_triple(channel: &str, target_dir: &Path, triple: &str) {
137     let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
138
139     let keep_sysroot =
140         fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
141     if !keep_sysroot {
142         // Cleanup the target dir with the exception of build scripts and the incremental cache
143         for dir in ["build", "deps", "examples", "native"] {
144             if build_dir.join(dir).exists() {
145                 fs::remove_dir_all(build_dir.join(dir)).unwrap();
146             }
147         }
148     }
149
150     // Build sysroot
151     let mut build_cmd = Command::new("cargo");
152     build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
153     let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
154     if channel == "release" {
155         build_cmd.arg("--release");
156         rustflags.push_str(" -Zmir-opt-level=3");
157     }
158     build_cmd.env("RUSTFLAGS", rustflags);
159     build_cmd.env(
160         "RUSTC",
161         env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
162     );
163     // FIXME Enable incremental again once rust-lang/rust#74946 is fixed
164     build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
165     spawn_and_wait(build_cmd);
166
167     // Copy all relevant files to the sysroot
168     for entry in
169         fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
170             .unwrap()
171     {
172         let entry = entry.unwrap();
173         if let Some(ext) = entry.path().extension() {
174             if ext == "rmeta" || ext == "d" || ext == "dSYM" {
175                 continue;
176             }
177         } else {
178             continue;
179         };
180         try_hard_link(
181             entry.path(),
182             target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
183         );
184     }
185 }