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, Path, PathBuf};
9 use std::process::Command;
13 use crate::dist::{self, pkgname, sanitize_sh, tmpdir};
16 use crate::builder::{Builder, RunConfig, ShouldRun, Step};
17 use crate::config::{Config, TargetSelection};
19 pub fn install_docs(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
20 install_sh(builder, "docs", "rust-docs", stage, Some(host));
23 pub fn install_std(builder: &Builder<'_>, stage: u32, target: TargetSelection) {
24 install_sh(builder, "std", "rust-std", stage, Some(target));
27 pub fn install_cargo(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
28 install_sh(builder, "cargo", "cargo", stage, Some(host));
31 pub fn install_rls(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
32 install_sh(builder, "rls", "rls", stage, Some(host));
35 pub fn install_rust_analyzer(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
36 install_sh(builder, "rust-analyzer", "rust-analyzer", stage, Some(host));
39 pub fn install_clippy(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
40 install_sh(builder, "clippy", "clippy", stage, Some(host));
42 pub fn install_miri(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
43 install_sh(builder, "miri", "miri", stage, Some(host));
46 pub fn install_rustfmt(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
47 install_sh(builder, "rustfmt", "rustfmt", stage, Some(host));
50 pub fn install_analysis(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
51 install_sh(builder, "analysis", "rust-analysis", stage, Some(host));
54 pub fn install_src(builder: &Builder<'_>, stage: u32) {
55 install_sh(builder, "src", "rust-src", stage, None);
57 pub fn install_rustc(builder: &Builder<'_>, stage: u32, host: TargetSelection) {
58 install_sh(builder, "rustc", "rustc", stage, Some(host));
62 builder: &Builder<'_>,
66 host: Option<TargetSelection>,
68 builder.info(&format!("Install {} stage{} ({:?})", package, stage, host));
70 let prefix_default = PathBuf::from("/usr/local");
71 let sysconfdir_default = PathBuf::from("/etc");
72 let datadir_default = PathBuf::from("share");
73 let docdir_default = datadir_default.join("doc/rust");
74 let libdir_default = PathBuf::from("lib");
75 let mandir_default = datadir_default.join("man");
76 let prefix = builder.config.prefix.as_ref().map_or(prefix_default, |p| {
78 .unwrap_or_else(|err| panic!("could not create {}: {}", p.display(), err));
80 .unwrap_or_else(|err| panic!("could not canonicalize {}: {}", p.display(), err))
82 let sysconfdir = builder.config.sysconfdir.as_ref().unwrap_or(&sysconfdir_default);
83 let datadir = builder.config.datadir.as_ref().unwrap_or(&datadir_default);
84 let docdir = builder.config.docdir.as_ref().unwrap_or(&docdir_default);
85 let bindir = &builder.config.bindir;
86 let libdir = builder.config.libdir.as_ref().unwrap_or(&libdir_default);
87 let mandir = builder.config.mandir.as_ref().unwrap_or(&mandir_default);
89 let sysconfdir = prefix.join(sysconfdir);
90 let datadir = prefix.join(datadir);
91 let docdir = prefix.join(docdir);
92 let bindir = prefix.join(bindir);
93 let libdir = prefix.join(libdir);
94 let mandir = prefix.join(mandir);
96 let destdir = env::var_os("DESTDIR").map(PathBuf::from);
98 let prefix = add_destdir(&prefix, &destdir);
99 let sysconfdir = add_destdir(&sysconfdir, &destdir);
100 let datadir = add_destdir(&datadir, &destdir);
101 let docdir = add_destdir(&docdir, &destdir);
102 let bindir = add_destdir(&bindir, &destdir);
103 let libdir = add_destdir(&libdir, &destdir);
104 let mandir = add_destdir(&mandir, &destdir);
106 let empty_dir = builder.out.join("tmp/empty_dir");
108 t!(fs::create_dir_all(&empty_dir));
109 let package_name = if let Some(host) = host {
110 format!("{}-{}", pkgname(builder, name), host)
112 pkgname(builder, name)
115 let mut cmd = Command::new("sh");
116 cmd.current_dir(&empty_dir)
117 .arg(sanitize_sh(&tmpdir(builder).join(&package_name).join("install.sh")))
118 .arg(format!("--prefix={}", sanitize_sh(&prefix)))
119 .arg(format!("--sysconfdir={}", sanitize_sh(&sysconfdir)))
120 .arg(format!("--datadir={}", sanitize_sh(&datadir)))
121 .arg(format!("--docdir={}", sanitize_sh(&docdir)))
122 .arg(format!("--bindir={}", sanitize_sh(&bindir)))
123 .arg(format!("--libdir={}", sanitize_sh(&libdir)))
124 .arg(format!("--mandir={}", sanitize_sh(&mandir)))
125 .arg("--disable-ldconfig");
126 builder.run(&mut cmd);
127 t!(fs::remove_dir_all(&empty_dir));
130 fn add_destdir(path: &Path, destdir: &Option<PathBuf>) -> PathBuf {
131 let mut ret = match *destdir {
132 Some(ref dest) => dest.clone(),
133 None => return path.to_path_buf(),
135 for part in path.components() {
136 if let Component::Normal(s) = part {
143 macro_rules! install {
144 (($sel:ident, $builder:ident, $_config:ident),
148 only_hosts: $only_hosts:expr,
149 $run_item:block $(, $c:ident)*;)+) => {
151 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
153 pub compiler: Compiler,
154 pub target: TargetSelection,
159 fn should_build(config: &Config) -> bool {
160 config.extended && config.tools.as_ref()
161 .map_or(true, |t| t.contains($path))
165 impl Step for $name {
167 const DEFAULT: bool = true;
168 const ONLY_HOSTS: bool = $only_hosts;
169 $(const $c: bool = true;)*
171 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
172 let $_config = &run.builder.config;
173 run.path($path).default_condition($default_cond)
176 fn make_run(run: RunConfig<'_>) {
177 run.builder.ensure($name {
178 compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
183 fn run($sel, $builder: &Builder<'_>) {
190 install!((self, builder, _config),
191 Docs, "src/doc", _config.docs, only_hosts: false, {
192 builder.ensure(dist::Docs { host: self.target });
193 install_docs(builder, self.compiler.stage, self.target);
195 Std, "src/libstd", true, only_hosts: true, {
196 for target in &builder.targets {
197 builder.ensure(dist::Std {
198 compiler: self.compiler,
201 install_std(builder, self.compiler.stage, *target);
204 Cargo, "cargo", Self::should_build(_config), only_hosts: true, {
205 builder.ensure(dist::Cargo { compiler: self.compiler, target: self.target });
206 install_cargo(builder, self.compiler.stage, self.target);
208 Rls, "rls", Self::should_build(_config), only_hosts: true, {
209 if builder.ensure(dist::Rls { compiler: self.compiler, target: self.target }).is_some() {
210 install_rls(builder, self.compiler.stage, self.target);
213 &format!("skipping Install RLS stage{} ({})", self.compiler.stage, self.target),
217 RustAnalyzer, "rust-analyzer", Self::should_build(_config), only_hosts: true, {
218 builder.ensure(dist::RustAnalyzer { compiler: self.compiler, target: self.target });
219 install_rust_analyzer(builder, self.compiler.stage, self.target);
221 Clippy, "clippy", Self::should_build(_config), only_hosts: true, {
222 builder.ensure(dist::Clippy { compiler: self.compiler, target: self.target });
223 install_clippy(builder, self.compiler.stage, self.target);
225 Miri, "miri", Self::should_build(_config), only_hosts: true, {
226 if builder.ensure(dist::Miri { compiler: self.compiler, target: self.target }).is_some() {
227 install_miri(builder, self.compiler.stage, self.target);
230 &format!("skipping Install miri stage{} ({})", self.compiler.stage, self.target),
234 Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, {
235 if builder.ensure(dist::Rustfmt {
236 compiler: self.compiler,
239 install_rustfmt(builder, self.compiler.stage, self.target);
242 &format!("skipping Install Rustfmt stage{} ({})", self.compiler.stage, self.target),
246 Analysis, "analysis", Self::should_build(_config), only_hosts: false, {
247 builder.ensure(dist::Analysis {
248 // Find the actual compiler (handling the full bootstrap option) which
249 // produced the save-analysis data because that data isn't copied
250 // through the sysroot uplifting.
251 compiler: builder.compiler_for(builder.top_stage, builder.config.build, self.target),
254 install_analysis(builder, self.compiler.stage, self.target);
256 Rustc, "src/librustc", true, only_hosts: true, {
257 builder.ensure(dist::Rustc {
258 compiler: builder.compiler(builder.top_stage, self.target),
260 install_rustc(builder, self.compiler.stage, self.target);
264 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
271 const DEFAULT: bool = true;
272 const ONLY_HOSTS: bool = true;
274 fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
275 let config = &run.builder.config;
276 let cond = config.extended && config.tools.as_ref().map_or(true, |t| t.contains("src"));
277 run.path("src").default_condition(cond)
280 fn make_run(run: RunConfig<'_>) {
281 run.builder.ensure(Src { stage: run.builder.top_stage });
284 fn run(self, builder: &Builder<'_>) {
285 builder.ensure(dist::Src);
286 install_src(builder, self.stage);