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