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