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