]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/install.rs
refactor gating of dist docs
[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, PathBuf};
9 use std::process::Command;
10
11 use build_helper::t;
12
13 use crate::dist::{self, sanitize_sh};
14 use crate::tarball::GeneratedTarball;
15 use crate::Compiler;
16
17 use crate::builder::{Builder, RunConfig, ShouldRun, Step};
18 use crate::config::{Config, TargetSelection};
19
20 #[cfg(target_os = "illumos")]
21 const SHELL: &str = "bash";
22 #[cfg(not(target_os = "illumos"))]
23 const SHELL: &str = "sh";
24
25 fn install_sh(
26     builder: &Builder<'_>,
27     package: &str,
28     stage: u32,
29     host: Option<TargetSelection>,
30     tarball: &GeneratedTarball,
31 ) {
32     builder.info(&format!("Install {} stage{} ({:?})", package, stage, host));
33
34     let prefix = default_path(&builder.config.prefix, "/usr/local");
35     let sysconfdir = prefix.join(default_path(&builder.config.sysconfdir, "/etc"));
36     let datadir = prefix.join(default_path(&builder.config.datadir, "share"));
37     let docdir = prefix.join(default_path(&builder.config.docdir, "share/doc/rust"));
38     let mandir = prefix.join(default_path(&builder.config.mandir, "share/man"));
39     let libdir = prefix.join(default_path(&builder.config.libdir, "lib"));
40     let bindir = prefix.join(&builder.config.bindir); // Default in config.rs
41
42     let empty_dir = builder.out.join("tmp/empty_dir");
43     t!(fs::create_dir_all(&empty_dir));
44
45     let mut cmd = Command::new(SHELL);
46     cmd.current_dir(&empty_dir)
47         .arg(sanitize_sh(&tarball.decompressed_output().join("install.sh")))
48         .arg(format!("--prefix={}", prepare_dir(prefix)))
49         .arg(format!("--sysconfdir={}", prepare_dir(sysconfdir)))
50         .arg(format!("--datadir={}", prepare_dir(datadir)))
51         .arg(format!("--docdir={}", prepare_dir(docdir)))
52         .arg(format!("--bindir={}", prepare_dir(bindir)))
53         .arg(format!("--libdir={}", prepare_dir(libdir)))
54         .arg(format!("--mandir={}", prepare_dir(mandir)))
55         .arg("--disable-ldconfig");
56     builder.run(&mut cmd);
57     t!(fs::remove_dir_all(&empty_dir));
58 }
59
60 fn default_path(config: &Option<PathBuf>, default: &str) -> PathBuf {
61     config.as_ref().cloned().unwrap_or_else(|| PathBuf::from(default))
62 }
63
64 fn prepare_dir(mut path: PathBuf) -> String {
65     // The DESTDIR environment variable is a standard way to install software in a subdirectory
66     // while keeping the original directory structure, even if the prefix or other directories
67     // contain absolute paths.
68     //
69     // More information on the environment variable is available here:
70     // https://www.gnu.org/prep/standards/html_node/DESTDIR.html
71     if let Some(destdir) = env::var_os("DESTDIR").map(PathBuf::from) {
72         let without_destdir = path.clone();
73         path = destdir;
74         // Custom .join() which ignores disk roots.
75         for part in without_destdir.components() {
76             if let Component::Normal(s) = part {
77                 path.push(s)
78             }
79         }
80     }
81
82     // The installation command is not executed from the current directory, but from a temporary
83     // directory. To prevent relative paths from breaking this converts relative paths to absolute
84     // paths. std::fs::canonicalize is not used as that requires the path to actually be present.
85     if path.is_relative() {
86         path = std::env::current_dir().expect("failed to get the current directory").join(path);
87         assert!(path.is_absolute(), "could not make the path relative");
88     }
89
90     sanitize_sh(&path)
91 }
92
93 macro_rules! install {
94     (($sel:ident, $builder:ident, $_config:ident),
95        $($name:ident,
96        $path:expr,
97        $default_cond:expr,
98        only_hosts: $only_hosts:expr,
99        $run_item:block $(, $c:ident)*;)+) => {
100         $(
101             #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
102         pub struct $name {
103             pub compiler: Compiler,
104             pub target: TargetSelection,
105         }
106
107         impl $name {
108             #[allow(dead_code)]
109             fn should_build(config: &Config) -> bool {
110                 config.extended && config.tools.as_ref()
111                     .map_or(true, |t| t.contains($path))
112             }
113         }
114
115         impl Step for $name {
116             type Output = ();
117             const DEFAULT: bool = true;
118             const ONLY_HOSTS: bool = $only_hosts;
119             $(const $c: bool = true;)*
120
121             fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
122                 let $_config = &run.builder.config;
123                 run.path($path).default_condition($default_cond)
124             }
125
126             fn make_run(run: RunConfig<'_>) {
127                 run.builder.ensure($name {
128                     compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
129                     target: run.target,
130                 });
131             }
132
133             fn run($sel, $builder: &Builder<'_>) {
134                 $run_item
135             }
136         })+
137     }
138 }
139
140 install!((self, builder, _config),
141     Docs, "src/doc", _config.docs, only_hosts: false, {
142         let tarball = builder.ensure(dist::Docs { host: self.target });
143         install_sh(builder, "docs", self.compiler.stage, Some(self.target), &tarball);
144     };
145     Std, "library/std", true, only_hosts: false, {
146         for target in &builder.targets {
147             // `expect` should be safe, only None when host != build, but this
148             // only runs when host == build
149             let tarball = builder.ensure(dist::Std {
150                 compiler: self.compiler,
151                 target: *target
152             }).expect("missing std");
153             install_sh(builder, "std", self.compiler.stage, Some(*target), &tarball);
154         }
155     };
156     Cargo, "cargo", Self::should_build(_config), only_hosts: true, {
157         let tarball = builder.ensure(dist::Cargo { compiler: self.compiler, target: self.target });
158         install_sh(builder, "cargo", self.compiler.stage, Some(self.target), &tarball);
159     };
160     Rls, "rls", Self::should_build(_config), only_hosts: true, {
161         if let Some(tarball) = builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }) {
162             install_sh(builder, "rls", self.compiler.stage, Some(self.target), &tarball);
163         } else {
164             builder.info(
165                 &format!("skipping Install RLS stage{} ({})", self.compiler.stage, self.target),
166             );
167         }
168     };
169     RustAnalyzer, "rust-analyzer", Self::should_build(_config), only_hosts: true, {
170         if let Some(tarball) =
171             builder.ensure(dist::RustAnalyzer { compiler: self.compiler, target: self.target })
172         {
173             install_sh(builder, "rust-analyzer", self.compiler.stage, Some(self.target), &tarball);
174         } else {
175             builder.info(
176                 &format!("skipping Install rust-analyzer stage{} ({})", self.compiler.stage, self.target),
177             );
178         }
179     };
180     Clippy, "clippy", Self::should_build(_config), only_hosts: true, {
181         let tarball = builder.ensure(dist::Clippy { compiler: self.compiler, target: self.target });
182         install_sh(builder, "clippy", self.compiler.stage, Some(self.target), &tarball);
183     };
184     Miri, "miri", Self::should_build(_config), only_hosts: true, {
185         if let Some(tarball) = builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }) {
186             install_sh(builder, "miri", self.compiler.stage, Some(self.target), &tarball);
187         } else {
188             builder.info(
189                 &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target),
190             );
191         }
192     };
193     Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, {
194         if let Some(tarball) = builder.ensure(dist::Rustfmt {
195             compiler: self.compiler,
196             target: self.target
197         }) {
198             install_sh(builder, "rustfmt", self.compiler.stage, Some(self.target), &tarball);
199         } else {
200             builder.info(
201                 &format!("skipping Install Rustfmt stage{} ({})", self.compiler.stage, self.target),
202             );
203         }
204     };
205     RustDemangler, "rust-demangler", Self::should_build(_config), only_hosts: true, {
206         // Note: Even though `should_build` may return true for `extended` default tools,
207         // dist::RustDemangler may still return None, unless the target-dependent `profiler` config
208         // is also true, or the `tools` array explicitly includes "rust-demangler".
209         if let Some(tarball) = builder.ensure(dist::RustDemangler {
210             compiler: self.compiler,
211             target: self.target
212         }) {
213             install_sh(builder, "rust-demangler", self.compiler.stage, Some(self.target), &tarball);
214         } else {
215             builder.info(
216                 &format!("skipping Install RustDemangler stage{} ({})",
217                          self.compiler.stage, self.target),
218             );
219         }
220     };
221     Analysis, "analysis", Self::should_build(_config), only_hosts: false, {
222         // `expect` should be safe, only None with host != build, but this
223         // only uses the `build` compiler
224         let tarball = builder.ensure(dist::Analysis {
225             // Find the actual compiler (handling the full bootstrap option) which
226             // produced the save-analysis data because that data isn't copied
227             // through the sysroot uplifting.
228             compiler: builder.compiler_for(builder.top_stage, builder.config.build, self.target),
229             target: self.target
230         }).expect("missing analysis");
231         install_sh(builder, "analysis", self.compiler.stage, Some(self.target), &tarball);
232     };
233     Rustc, "src/librustc", true, only_hosts: true, {
234         let tarball = builder.ensure(dist::Rustc {
235             compiler: builder.compiler(builder.top_stage, self.target),
236         });
237         install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball);
238     };
239 );
240
241 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
242 pub struct Src {
243     pub stage: u32,
244 }
245
246 impl Step for Src {
247     type Output = ();
248     const DEFAULT: bool = true;
249     const ONLY_HOSTS: bool = true;
250
251     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
252         let config = &run.builder.config;
253         let cond = config.extended && config.tools.as_ref().map_or(true, |t| t.contains("src"));
254         run.path("src").default_condition(cond)
255     }
256
257     fn make_run(run: RunConfig<'_>) {
258         run.builder.ensure(Src { stage: run.builder.top_stage });
259     }
260
261     fn run(self, builder: &Builder<'_>) {
262         let tarball = builder.ensure(dist::Src);
263         install_sh(builder, "src", self.stage, None, &tarball);
264     }
265 }