]> git.lizzy.rs Git - rust.git/blob - build_system/build_sysroot.rs
Improve windows support
[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     // Copy supporting files
47     try_hard_link("rust-toolchain", target_dir.join("rust-toolchain"));
48     try_hard_link("scripts/config.sh", target_dir.join("config.sh"));
49     try_hard_link("scripts/cargo.sh", target_dir.join("cargo.sh"));
50
51     let default_sysroot = crate::rustc_info::get_default_sysroot();
52
53     let rustlib = target_dir.join("lib").join("rustlib");
54     let host_rustlib_lib = rustlib.join(host_triple).join("lib");
55     let target_rustlib_lib = rustlib.join(target_triple).join("lib");
56     fs::create_dir_all(&host_rustlib_lib).unwrap();
57     fs::create_dir_all(&target_rustlib_lib).unwrap();
58
59     if target_triple == "x86_64-pc-windows-gnu" {
60         if !default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib").exists() {
61             eprintln!(
62                 "The x86_64-pc-windows-gnu target needs to be installed first before it is possible \
63                 to compile a sysroot for it.",
64             );
65             process::exit(1);
66         }
67         for file in fs::read_dir(
68             default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
69         )
70         .unwrap()
71         {
72             let file = file.unwrap().path();
73             if file.extension().map_or(true, |ext| ext.to_str().unwrap() != "o") {
74                 continue; // only copy object files
75             }
76             try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
77         }
78     }
79
80     match sysroot_kind {
81         SysrootKind::None => {} // Nothing to do
82         SysrootKind::Llvm => {
83             for file in fs::read_dir(
84                 default_sysroot.join("lib").join("rustlib").join(host_triple).join("lib"),
85             )
86             .unwrap()
87             {
88                 let file = file.unwrap().path();
89                 let file_name_str = file.file_name().unwrap().to_str().unwrap();
90                 if file_name_str.contains("rustc_")
91                     || file_name_str.contains("chalk")
92                     || file_name_str.contains("tracing")
93                     || file_name_str.contains("regex")
94                 {
95                     // These are large crates that are part of the rustc-dev component and are not
96                     // necessary to run regular programs.
97                     continue;
98                 }
99                 try_hard_link(&file, host_rustlib_lib.join(file.file_name().unwrap()));
100             }
101
102             if target_triple != host_triple {
103                 for file in fs::read_dir(
104                     default_sysroot.join("lib").join("rustlib").join(target_triple).join("lib"),
105                 )
106                 .unwrap()
107                 {
108                     let file = file.unwrap().path();
109                     try_hard_link(&file, target_rustlib_lib.join(file.file_name().unwrap()));
110                 }
111             }
112         }
113         SysrootKind::Clif => {
114             build_clif_sysroot_for_triple(channel, target_dir, target_triple);
115
116             if host_triple != target_triple {
117                 build_clif_sysroot_for_triple(channel, target_dir, host_triple);
118             }
119
120             // Copy std for the host to the lib dir. This is necessary for the jit mode to find
121             // libstd.
122             for file in fs::read_dir(host_rustlib_lib).unwrap() {
123                 let file = file.unwrap().path();
124                 if file.file_name().unwrap().to_str().unwrap().contains("std-") {
125                     try_hard_link(&file, target_dir.join("lib").join(file.file_name().unwrap()));
126                 }
127             }
128         }
129     }
130 }
131
132 fn build_clif_sysroot_for_triple(channel: &str, target_dir: &Path, triple: &str) {
133     let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
134
135     let keep_sysroot =
136         fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
137     if !keep_sysroot {
138         // Cleanup the target dir with the exception of build scripts and the incremental cache
139         for dir in ["build", "deps", "examples", "native"] {
140             if build_dir.join(dir).exists() {
141                 fs::remove_dir_all(build_dir.join(dir)).unwrap();
142             }
143         }
144     }
145
146     // Build sysroot
147     let mut build_cmd = Command::new("cargo");
148     build_cmd.arg("build").arg("--target").arg(triple).current_dir("build_sysroot");
149     let mut rustflags = "--clif -Zforce-unstable-if-unmarked".to_string();
150     if channel == "release" {
151         build_cmd.arg("--release");
152         rustflags.push_str(" -Zmir-opt-level=3");
153     }
154     build_cmd.env("RUSTFLAGS", rustflags);
155     build_cmd.env(
156         "RUSTC",
157         env::current_dir().unwrap().join(target_dir).join("bin").join("cg_clif_build_sysroot"),
158     );
159     // FIXME Enable incremental again once rust-lang/rust#74946 is fixed
160     build_cmd.env("CARGO_INCREMENTAL", "0").env("__CARGO_DEFAULT_LIB_METADATA", "cg_clif");
161     spawn_and_wait(build_cmd);
162
163     // Copy all relevant files to the sysroot
164     for entry in
165         fs::read_dir(Path::new("build_sysroot/target").join(triple).join(channel).join("deps"))
166             .unwrap()
167     {
168         let entry = entry.unwrap();
169         if let Some(ext) = entry.path().extension() {
170             if ext == "rmeta" || ext == "d" || ext == "dSYM" {
171                 continue;
172             }
173         } else {
174             continue;
175         };
176         try_hard_link(
177             entry.path(),
178             target_dir.join("lib").join("rustlib").join(triple).join("lib").join(entry.file_name()),
179         );
180     }
181 }