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