1 //! Implementation of the install aspects of the compiler.
3 //! This module is responsible for installing the standard library,
4 //! compiler, and documentation.
8 use std::path::{Component, PathBuf};
9 use std::process::Command;
13 use crate::dist::{self, sanitize_sh};
14 use crate::tarball::GeneratedTarball;
17 use crate::builder::{Builder, RunConfig, ShouldRun, Step};
18 use crate::config::{Config, TargetSelection};
20 #[cfg(target_os = "illumos")]
21 const SHELL: &str = "bash";
22 #[cfg(not(target_os = "illumos"))]
23 const SHELL: &str = "sh";
26 builder: &Builder<'_>,
29 host: Option<TargetSelection>,
30 tarball: &GeneratedTarball,
32 builder.info(&format!("Install {} stage{} ({:?})", package, stage, host));
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
42 let empty_dir = builder.out.join("tmp/empty_dir");
43 t!(fs::create_dir_all(&empty_dir));
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));
60 fn default_path(config: &Option<PathBuf>, default: &str) -> PathBuf {
61 config.as_ref().cloned().unwrap_or_else(|| PathBuf::from(default))
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.
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();
74 // Custom .join() which ignores disk roots.
75 for part in without_destdir.components() {
76 if let Component::Normal(s) = part {
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");
93 macro_rules! install {
94 (($sel:ident, $builder:ident, $_config:ident),
98 only_hosts: $only_hosts:expr,
99 $run_item:block $(, $c:ident)*;)+) => {
101 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
103 pub compiler: Compiler,
104 pub target: TargetSelection,
109 fn should_build(config: &Config) -> bool {
110 config.extended && config.tools.as_ref()
111 .map_or(true, |t| t.contains($path))
115 impl Step for $name {
117 const DEFAULT: bool = true;
118 const ONLY_HOSTS: bool = $only_hosts;
119 $(const $c: bool = true;)*
121 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
122 let $_config = &run.builder.config;
123 run.path($path).default_condition($default_cond)
126 fn make_run(run: RunConfig<'_>) {
127 run.builder.ensure($name {
128 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
133 fn run($sel, $builder: &Builder<'_>) {
140 install!((self, builder, _config),
141 Docs, "src/doc", _config.docs, only_hosts: false, {
142 if let Some(tarball) = builder.ensure(dist::Docs { host: self.target }) {
143 install_sh(builder, "docs", self.compiler.stage, Some(self.target), &tarball);
145 panic!("docs are not available to install, \
146 check that `build.docs` is true in `config.toml`");
149 Std, "library/std", true, only_hosts: false, {
150 for target in &builder.targets {
151 // `expect` should be safe, only None when host != build, but this
152 // only runs when host == build
153 let tarball = builder.ensure(dist::Std {
154 compiler: self.compiler,
156 }).expect("missing std");
157 install_sh(builder, "std", self.compiler.stage, Some(*target), &tarball);
160 Cargo, "cargo", Self::should_build(_config), only_hosts: true, {
161 let tarball = builder.ensure(dist::Cargo { compiler: self.compiler, target: self.target });
162 install_sh(builder, "cargo", self.compiler.stage, Some(self.target), &tarball);
164 Rls, "rls", Self::should_build(_config), only_hosts: true, {
165 if let Some(tarball) = builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }) {
166 install_sh(builder, "rls", self.compiler.stage, Some(self.target), &tarball);
169 &format!("skipping Install RLS stage{} ({})", self.compiler.stage, self.target),
173 RustAnalyzer, "rust-analyzer", Self::should_build(_config), only_hosts: true, {
174 if let Some(tarball) =
175 builder.ensure(dist::RustAnalyzer { compiler: self.compiler, target: self.target })
177 install_sh(builder, "rust-analyzer", self.compiler.stage, Some(self.target), &tarball);
180 &format!("skipping Install rust-analyzer stage{} ({})", self.compiler.stage, self.target),
184 Clippy, "clippy", Self::should_build(_config), only_hosts: true, {
185 let tarball = builder.ensure(dist::Clippy { compiler: self.compiler, target: self.target });
186 install_sh(builder, "clippy", self.compiler.stage, Some(self.target), &tarball);
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);
193 &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target),
197 Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, {
198 if let Some(tarball) = builder.ensure(dist::Rustfmt {
199 compiler: self.compiler,
202 install_sh(builder, "rustfmt", self.compiler.stage, Some(self.target), &tarball);
205 &format!("skipping Install Rustfmt stage{} ({})", self.compiler.stage, self.target),
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,
217 install_sh(builder, "rust-demangler", self.compiler.stage, Some(self.target), &tarball);
220 &format!("skipping Install RustDemangler stage{} ({})",
221 self.compiler.stage, self.target),
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),
234 }).expect("missing analysis");
235 install_sh(builder, "analysis", self.compiler.stage, Some(self.target), &tarball);
237 Rustc, "src/librustc", true, only_hosts: true, {
238 let tarball = builder.ensure(dist::Rustc {
239 compiler: builder.compiler(builder.top_stage, self.target),
241 install_sh(builder, "rustc", self.compiler.stage, Some(self.target), &tarball);
245 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
252 const DEFAULT: bool = true;
253 const ONLY_HOSTS: bool = true;
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)
261 fn make_run(run: RunConfig<'_>) {
262 run.builder.ensure(Src { stage: run.builder.top_stage });
265 fn run(self, builder: &Builder<'_>) {
266 let tarball = builder.ensure(dist::Src);
267 install_sh(builder, "src", self.stage, None, &tarball);