]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/install.rs
Rollup merge of #88706 - ThePuzzlemaker:issue-88609, r=jackh726
[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 }).expect("missing docs");
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
158             .ensure(dist::Cargo { compiler: self.compiler, target: self.target })
159             .expect("missing cargo");
160         install_sh(builder, "cargo", self.compiler.stage, Some(self.target), &tarball);
161     };
162     Rls, "rls", Self::should_build(_config), only_hosts: true, {
163         if let Some(tarball) = builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }) {
164             install_sh(builder, "rls", self.compiler.stage, Some(self.target), &tarball);
165         } else {
166             builder.info(
167                 &format!("skipping Install RLS stage{} ({})", self.compiler.stage, self.target),
168             );
169         }
170     };
171     RustAnalyzer, "rust-analyzer", Self::should_build(_config), only_hosts: true, {
172         if let Some(tarball) =
173             builder.ensure(dist::RustAnalyzer { compiler: self.compiler, target: self.target })
174         {
175             install_sh(builder, "rust-analyzer", self.compiler.stage, Some(self.target), &tarball);
176         } else {
177             builder.info(
178                 &format!("skipping Install rust-analyzer stage{} ({})", self.compiler.stage, self.target),
179             );
180         }
181     };
182     Clippy, "clippy", Self::should_build(_config), only_hosts: true, {
183         let tarball = builder
184             .ensure(dist::Clippy { compiler: self.compiler, target: self.target })
185             .expect("missing clippy");
186         install_sh(builder, "clippy", self.compiler.stage, Some(self.target), &tarball);
187     };
188     Miri, "miri", Self::should_build(_config), only_hosts: true, {
189         if let Some(tarball) = builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }) {
190             install_sh(builder, "miri", self.compiler.stage, Some(self.target), &tarball);
191         } else {
192             builder.info(
193                 &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target),
194             );
195         }
196     };
197     Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, {
198         if let Some(tarball) = builder.ensure(dist::Rustfmt {
199             compiler: self.compiler,
200             target: self.target
201         }) {
202             install_sh(builder, "rustfmt", self.compiler.stage, Some(self.target), &tarball);
203         } else {
204             builder.info(
205                 &format!("skipping Install Rustfmt stage{} ({})", self.compiler.stage, self.target),
206             );
207         }
208     };
209     RustDemangler, "rust-demangler", Self::should_build(_config), only_hosts: true, {
210         // Note: Even though `should_build` may return true for `extended` default tools,
211         // dist::RustDemangler may still return None, unless the target-dependent `profiler` config
212         // is also true, or the `tools` array explicitly includes "rust-demangler".
213         if let Some(tarball) = builder.ensure(dist::RustDemangler {
214             compiler: self.compiler,
215             target: self.target
216         }) {
217             install_sh(builder, "rust-demangler", self.compiler.stage, Some(self.target), &tarball);
218         } else {
219             builder.info(
220                 &format!("skipping Install RustDemangler stage{} ({})",
221                          self.compiler.stage, self.target),
222             );
223         }
224     };
225     Analysis, "analysis", Self::should_build(_config), only_hosts: false, {
226         // `expect` should be safe, only None with host != build, but this
227         // only uses the `build` compiler
228         let tarball = builder.ensure(dist::Analysis {
229             // Find the actual compiler (handling the full bootstrap option) which
230             // produced the save-analysis data because that data isn't copied
231             // through the sysroot uplifting.
232             compiler: builder.compiler_for(builder.top_stage, builder.config.build, self.target),
233             target: self.target
234         }).expect("missing analysis");
235         install_sh(builder, "analysis", self.compiler.stage, Some(self.target), &tarball);
236     };
237     Rustc, "src/librustc", true, only_hosts: true, {
238         let tarball = builder.ensure(dist::Rustc {
239             compiler: builder.compiler(builder.top_stage, self.target),
240         });
241         install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball);
242     };
243 );
244
245 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
246 pub struct Src {
247     pub stage: u32,
248 }
249
250 impl Step for Src {
251     type Output = ();
252     const DEFAULT: bool = true;
253     const ONLY_HOSTS: bool = true;
254
255     fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
256         let config = &run.builder.config;
257         let cond = config.extended && config.tools.as_ref().map_or(true, |t| t.contains("src"));
258         run.path("src").default_condition(cond)
259     }
260
261     fn make_run(run: RunConfig<'_>) {
262         run.builder.ensure(Src { stage: run.builder.top_stage });
263     }
264
265     fn run(self, builder: &Builder<'_>) {
266         let tarball = builder.ensure(dist::Src);
267         install_sh(builder, "src", self.stage, None, &tarball);
268     }
269 }