]>
Commit | Line | Data |
---|---|---|
c30ab7b3 SL |
1 | //! Implementation of the install aspects of the compiler. |
2 | //! | |
3 | //! This module is responsible for installing the standard library, | |
4 | //! compiler, and documentation. | |
5 | ||
32a655c1 | 6 | use std::env; |
c30ab7b3 | 7 | use std::fs; |
dc9dc135 | 8 | use std::path::{Component, Path, PathBuf}; |
c30ab7b3 SL |
9 | use std::process::Command; |
10 | ||
48663c56 XL |
11 | use build_helper::t; |
12 | ||
0731742a | 13 | use crate::dist::{self, pkgname, sanitize_sh, tmpdir}; |
dc9dc135 | 14 | use crate::Compiler; |
7cac9316 | 15 | |
0731742a XL |
16 | use crate::builder::{Builder, RunConfig, ShouldRun, Step}; |
17 | use crate::cache::Interned; | |
18 | use crate::config::Config; | |
7cac9316 | 19 | |
9fa01778 | 20 | pub fn install_docs(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
3b2f2976 XL |
21 | install_sh(builder, "docs", "rust-docs", stage, Some(host)); |
22 | } | |
cc61c64b | 23 | |
9fa01778 | 24 | pub fn install_std(builder: &Builder<'_>, stage: u32, target: Interned<String>) { |
abe05a73 | 25 | install_sh(builder, "std", "rust-std", stage, Some(target)); |
3b2f2976 | 26 | } |
c30ab7b3 | 27 | |
9fa01778 | 28 | pub fn install_cargo(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
3b2f2976 XL |
29 | install_sh(builder, "cargo", "cargo", stage, Some(host)); |
30 | } | |
7cac9316 | 31 | |
9fa01778 | 32 | pub fn install_rls(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
3b2f2976 XL |
33 | install_sh(builder, "rls", "rls", stage, Some(host)); |
34 | } | |
9fa01778 | 35 | pub fn install_clippy(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
8faf50e0 XL |
36 | install_sh(builder, "clippy", "clippy", stage, Some(host)); |
37 | } | |
9fa01778 | 38 | pub fn install_miri(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
0731742a XL |
39 | install_sh(builder, "miri", "miri", stage, Some(host)); |
40 | } | |
7cac9316 | 41 | |
9fa01778 | 42 | pub fn install_rustfmt(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
abe05a73 XL |
43 | install_sh(builder, "rustfmt", "rustfmt", stage, Some(host)); |
44 | } | |
45 | ||
9fa01778 | 46 | pub fn install_analysis(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
3b2f2976 XL |
47 | install_sh(builder, "analysis", "rust-analysis", stage, Some(host)); |
48 | } | |
7cac9316 | 49 | |
9fa01778 | 50 | pub fn install_src(builder: &Builder<'_>, stage: u32) { |
3b2f2976 XL |
51 | install_sh(builder, "src", "rust-src", stage, None); |
52 | } | |
9fa01778 | 53 | pub fn install_rustc(builder: &Builder<'_>, stage: u32, host: Interned<String>) { |
3b2f2976 XL |
54 | install_sh(builder, "rustc", "rustc", stage, Some(host)); |
55 | } | |
7cac9316 | 56 | |
3b2f2976 | 57 | fn install_sh( |
9fa01778 | 58 | builder: &Builder<'_>, |
3b2f2976 XL |
59 | package: &str, |
60 | name: &str, | |
61 | stage: u32, | |
dc9dc135 | 62 | host: Option<Interned<String>>, |
3b2f2976 | 63 | ) { |
83c7162d | 64 | builder.info(&format!("Install {} stage{} ({:?})", package, stage, host)); |
3b2f2976 XL |
65 | |
66 | let prefix_default = PathBuf::from("/usr/local"); | |
67 | let sysconfdir_default = PathBuf::from("/etc"); | |
2c00a5a8 XL |
68 | let datadir_default = PathBuf::from("share"); |
69 | let docdir_default = datadir_default.join("doc/rust"); | |
3b2f2976 | 70 | let libdir_default = PathBuf::from("lib"); |
2c00a5a8 | 71 | let mandir_default = datadir_default.join("man"); |
83c7162d | 72 | let prefix = builder.config.prefix.as_ref().map_or(prefix_default, |p| { |
8faf50e0 | 73 | fs::canonicalize(p).unwrap_or_else(|_| panic!("could not canonicalize {}", p.display())) |
83c7162d XL |
74 | }); |
75 | let sysconfdir = builder.config.sysconfdir.as_ref().unwrap_or(&sysconfdir_default); | |
76 | let datadir = builder.config.datadir.as_ref().unwrap_or(&datadir_default); | |
77 | let docdir = builder.config.docdir.as_ref().unwrap_or(&docdir_default); | |
e1599b0c | 78 | let bindir = &builder.config.bindir; |
83c7162d XL |
79 | let libdir = builder.config.libdir.as_ref().unwrap_or(&libdir_default); |
80 | let mandir = builder.config.mandir.as_ref().unwrap_or(&mandir_default); | |
3b2f2976 XL |
81 | |
82 | let sysconfdir = prefix.join(sysconfdir); | |
2c00a5a8 | 83 | let datadir = prefix.join(datadir); |
3b2f2976 XL |
84 | let docdir = prefix.join(docdir); |
85 | let bindir = prefix.join(bindir); | |
86 | let libdir = prefix.join(libdir); | |
87 | let mandir = prefix.join(mandir); | |
88 | ||
89 | let destdir = env::var_os("DESTDIR").map(PathBuf::from); | |
90 | ||
91 | let prefix = add_destdir(&prefix, &destdir); | |
92 | let sysconfdir = add_destdir(&sysconfdir, &destdir); | |
2c00a5a8 | 93 | let datadir = add_destdir(&datadir, &destdir); |
3b2f2976 XL |
94 | let docdir = add_destdir(&docdir, &destdir); |
95 | let bindir = add_destdir(&bindir, &destdir); | |
96 | let libdir = add_destdir(&libdir, &destdir); | |
97 | let mandir = add_destdir(&mandir, &destdir); | |
98 | ||
83c7162d | 99 | let empty_dir = builder.out.join("tmp/empty_dir"); |
3b2f2976 XL |
100 | |
101 | t!(fs::create_dir_all(&empty_dir)); | |
102 | let package_name = if let Some(host) = host { | |
83c7162d | 103 | format!("{}-{}", pkgname(builder, name), host) |
3b2f2976 | 104 | } else { |
83c7162d | 105 | pkgname(builder, name) |
3b2f2976 | 106 | }; |
7cac9316 | 107 | |
3b2f2976 XL |
108 | let mut cmd = Command::new("sh"); |
109 | cmd.current_dir(&empty_dir) | |
83c7162d | 110 | .arg(sanitize_sh(&tmpdir(builder).join(&package_name).join("install.sh"))) |
3b2f2976 XL |
111 | .arg(format!("--prefix={}", sanitize_sh(&prefix))) |
112 | .arg(format!("--sysconfdir={}", sanitize_sh(&sysconfdir))) | |
2c00a5a8 | 113 | .arg(format!("--datadir={}", sanitize_sh(&datadir))) |
3b2f2976 XL |
114 | .arg(format!("--docdir={}", sanitize_sh(&docdir))) |
115 | .arg(format!("--bindir={}", sanitize_sh(&bindir))) | |
116 | .arg(format!("--libdir={}", sanitize_sh(&libdir))) | |
117 | .arg(format!("--mandir={}", sanitize_sh(&mandir))) | |
118 | .arg("--disable-ldconfig"); | |
83c7162d | 119 | builder.run(&mut cmd); |
3b2f2976 | 120 | t!(fs::remove_dir_all(&empty_dir)); |
c30ab7b3 | 121 | } |
32a655c1 SL |
122 | |
123 | fn add_destdir(path: &Path, destdir: &Option<PathBuf>) -> PathBuf { | |
124 | let mut ret = match *destdir { | |
125 | Some(ref dest) => dest.clone(), | |
126 | None => return path.to_path_buf(), | |
127 | }; | |
128 | for part in path.components() { | |
129 | match part { | |
130 | Component::Normal(s) => ret.push(s), | |
131 | _ => {} | |
132 | } | |
133 | } | |
041b39d2 | 134 | ret |
32a655c1 | 135 | } |
3b2f2976 XL |
136 | |
137 | macro_rules! install { | |
138 | (($sel:ident, $builder:ident, $_config:ident), | |
139 | $($name:ident, | |
140 | $path:expr, | |
141 | $default_cond:expr, | |
142 | only_hosts: $only_hosts:expr, | |
143 | $run_item:block $(, $c:ident)*;)+) => { | |
144 | $( | |
145 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] | |
146 | pub struct $name { | |
dc9dc135 | 147 | pub compiler: Compiler, |
3b2f2976 | 148 | pub target: Interned<String>, |
3b2f2976 XL |
149 | } |
150 | ||
2c00a5a8 XL |
151 | impl $name { |
152 | #[allow(dead_code)] | |
153 | fn should_build(config: &Config) -> bool { | |
154 | config.extended && config.tools.as_ref() | |
155 | .map_or(true, |t| t.contains($path)) | |
156 | } | |
157 | ||
158 | #[allow(dead_code)] | |
9fa01778 | 159 | fn should_install(builder: &Builder<'_>) -> bool { |
2c00a5a8 XL |
160 | builder.config.tools.as_ref().map_or(false, |t| t.contains($path)) |
161 | } | |
162 | } | |
163 | ||
3b2f2976 XL |
164 | impl Step for $name { |
165 | type Output = (); | |
166 | const DEFAULT: bool = true; | |
3b2f2976 XL |
167 | const ONLY_HOSTS: bool = $only_hosts; |
168 | $(const $c: bool = true;)* | |
169 | ||
9fa01778 | 170 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
3b2f2976 XL |
171 | let $_config = &run.builder.config; |
172 | run.path($path).default_condition($default_cond) | |
173 | } | |
174 | ||
9fa01778 | 175 | fn make_run(run: RunConfig<'_>) { |
3b2f2976 | 176 | run.builder.ensure($name { |
dc9dc135 | 177 | compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build), |
3b2f2976 | 178 | target: run.target, |
3b2f2976 XL |
179 | }); |
180 | } | |
181 | ||
9fa01778 | 182 | fn run($sel, $builder: &Builder<'_>) { |
3b2f2976 XL |
183 | $run_item |
184 | } | |
185 | })+ | |
186 | } | |
187 | } | |
188 | ||
189 | install!((self, builder, _config), | |
190 | Docs, "src/doc", _config.docs, only_hosts: false, { | |
dc9dc135 XL |
191 | builder.ensure(dist::Docs { host: self.target }); |
192 | install_docs(builder, self.compiler.stage, self.target); | |
3b2f2976 XL |
193 | }; |
194 | Std, "src/libstd", true, only_hosts: true, { | |
83c7162d | 195 | for target in &builder.targets { |
abe05a73 | 196 | builder.ensure(dist::Std { |
dc9dc135 | 197 | compiler: self.compiler, |
abe05a73 XL |
198 | target: *target |
199 | }); | |
dc9dc135 | 200 | install_std(builder, self.compiler.stage, *target); |
abe05a73 | 201 | } |
3b2f2976 | 202 | }; |
2c00a5a8 | 203 | Cargo, "cargo", Self::should_build(_config), only_hosts: true, { |
dc9dc135 XL |
204 | builder.ensure(dist::Cargo { compiler: self.compiler, target: self.target }); |
205 | install_cargo(builder, self.compiler.stage, self.target); | |
3b2f2976 | 206 | }; |
2c00a5a8 | 207 | Rls, "rls", Self::should_build(_config), only_hosts: true, { |
dc9dc135 | 208 | if builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }).is_some() || |
2c00a5a8 | 209 | Self::should_install(builder) { |
dc9dc135 | 210 | install_rls(builder, self.compiler.stage, self.target); |
abe05a73 | 211 | } else { |
dc9dc135 XL |
212 | builder.info( |
213 | &format!("skipping Install RLS stage{} ({})", self.compiler.stage, self.target), | |
214 | ); | |
abe05a73 XL |
215 | } |
216 | }; | |
8faf50e0 | 217 | Clippy, "clippy", Self::should_build(_config), only_hosts: true, { |
dc9dc135 XL |
218 | if builder.ensure(dist::Clippy { |
219 | compiler: self.compiler, | |
220 | target: self.target, | |
221 | }).is_some() || Self::should_install(builder) { | |
222 | install_clippy(builder, self.compiler.stage, self.target); | |
8faf50e0 | 223 | } else { |
dc9dc135 XL |
224 | builder.info( |
225 | &format!("skipping Install clippy stage{} ({})", self.compiler.stage, self.target), | |
226 | ); | |
8faf50e0 XL |
227 | } |
228 | }; | |
0731742a | 229 | Miri, "miri", Self::should_build(_config), only_hosts: true, { |
dc9dc135 | 230 | if builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }).is_some() || |
0731742a | 231 | Self::should_install(builder) { |
dc9dc135 | 232 | install_miri(builder, self.compiler.stage, self.target); |
0731742a | 233 | } else { |
dc9dc135 XL |
234 | builder.info( |
235 | &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target), | |
236 | ); | |
0731742a XL |
237 | } |
238 | }; | |
2c00a5a8 | 239 | Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, { |
dc9dc135 XL |
240 | if builder.ensure(dist::Rustfmt { |
241 | compiler: self.compiler, | |
242 | target: self.target | |
243 | }).is_some() || Self::should_install(builder) { | |
244 | install_rustfmt(builder, self.compiler.stage, self.target); | |
abe05a73 | 245 | } else { |
83c7162d | 246 | builder.info( |
dc9dc135 XL |
247 | &format!("skipping Install Rustfmt stage{} ({})", self.compiler.stage, self.target), |
248 | ); | |
abe05a73 | 249 | } |
3b2f2976 | 250 | }; |
2c00a5a8 | 251 | Analysis, "analysis", Self::should_build(_config), only_hosts: false, { |
3b2f2976 | 252 | builder.ensure(dist::Analysis { |
dc9dc135 XL |
253 | // Find the actual compiler (handling the full bootstrap option) which |
254 | // produced the save-analysis data because that data isn't copied | |
255 | // through the sysroot uplifting. | |
256 | compiler: builder.compiler_for(builder.top_stage, builder.config.build, self.target), | |
3b2f2976 XL |
257 | target: self.target |
258 | }); | |
dc9dc135 | 259 | install_analysis(builder, self.compiler.stage, self.target); |
3b2f2976 | 260 | }; |
3b2f2976 XL |
261 | Rustc, "src/librustc", true, only_hosts: true, { |
262 | builder.ensure(dist::Rustc { | |
60c5eb7d | 263 | compiler: builder.compiler(builder.top_stage, self.target), |
3b2f2976 | 264 | }); |
dc9dc135 | 265 | install_rustc(builder, self.compiler.stage, self.target); |
3b2f2976 XL |
266 | }; |
267 | ); | |
0531ce1d XL |
268 | |
269 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] | |
270 | pub struct Src { | |
271 | pub stage: u32, | |
272 | } | |
273 | ||
274 | impl Step for Src { | |
275 | type Output = (); | |
276 | const DEFAULT: bool = true; | |
277 | const ONLY_HOSTS: bool = true; | |
278 | ||
9fa01778 | 279 | fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> { |
0531ce1d | 280 | let config = &run.builder.config; |
dc9dc135 | 281 | let cond = config.extended && config.tools.as_ref().map_or(true, |t| t.contains("src")); |
0531ce1d XL |
282 | run.path("src").default_condition(cond) |
283 | } | |
284 | ||
9fa01778 | 285 | fn make_run(run: RunConfig<'_>) { |
dc9dc135 | 286 | run.builder.ensure(Src { stage: run.builder.top_stage }); |
0531ce1d XL |
287 | } |
288 | ||
9fa01778 | 289 | fn run(self, builder: &Builder<'_>) { |
0531ce1d XL |
290 | builder.ensure(dist::Src); |
291 | install_src(builder, self.stage); | |
292 | } | |
293 | } |