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