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::{Path, PathBuf, Component};
9 use std::process::Command;
11 use crate::dist::{self, pkgname, sanitize_sh, tmpdir};
13 use crate::builder::{Builder, RunConfig, ShouldRun, Step};
14 use crate::cache::Interned;
15 use crate::config::Config;
17 pub fn install_docs(builder: &Builder, stage: u32, host: Interned<String>) {
18 install_sh(builder, "docs", "rust-docs", stage, Some(host));
21 pub fn install_std(builder: &Builder, stage: u32, target: Interned<String>) {
22 install_sh(builder, "std", "rust-std", stage, Some(target));
25 pub fn install_cargo(builder: &Builder, stage: u32, host: Interned<String>) {
26 install_sh(builder, "cargo", "cargo", stage, Some(host));
29 pub fn install_rls(builder: &Builder, stage: u32, host: Interned<String>) {
30 install_sh(builder, "rls", "rls", stage, Some(host));
32 pub fn install_clippy(builder: &Builder, stage: u32, host: Interned<String>) {
33 install_sh(builder, "clippy", "clippy", stage, Some(host));
35 pub fn install_miri(builder: &Builder, stage: u32, host: Interned<String>) {
36 install_sh(builder, "miri", "miri", stage, Some(host));
39 pub fn install_rustfmt(builder: &Builder, stage: u32, host: Interned<String>) {
40 install_sh(builder, "rustfmt", "rustfmt", stage, Some(host));
43 pub fn install_analysis(builder: &Builder, stage: u32, host: Interned<String>) {
44 install_sh(builder, "analysis", "rust-analysis", stage, Some(host));
47 pub fn install_src(builder: &Builder, stage: u32) {
48 install_sh(builder, "src", "rust-src", stage, None);
50 pub fn install_rustc(builder: &Builder, stage: u32, host: Interned<String>) {
51 install_sh(builder, "rustc", "rustc", stage, Some(host));
59 host: Option<Interned<String>>
61 builder.info(&format!("Install {} stage{} ({:?})", package, stage, host));
63 let prefix_default = PathBuf::from("/usr/local");
64 let sysconfdir_default = PathBuf::from("/etc");
65 let datadir_default = PathBuf::from("share");
66 let docdir_default = datadir_default.join("doc/rust");
67 let bindir_default = PathBuf::from("bin");
68 let libdir_default = PathBuf::from("lib");
69 let mandir_default = datadir_default.join("man");
70 let prefix = builder.config.prefix.as_ref().map_or(prefix_default, |p| {
71 fs::canonicalize(p).unwrap_or_else(|_| panic!("could not canonicalize {}", p.display()))
73 let sysconfdir = builder.config.sysconfdir.as_ref().unwrap_or(&sysconfdir_default);
74 let datadir = builder.config.datadir.as_ref().unwrap_or(&datadir_default);
75 let docdir = builder.config.docdir.as_ref().unwrap_or(&docdir_default);
76 let bindir = builder.config.bindir.as_ref().unwrap_or(&bindir_default);
77 let libdir = builder.config.libdir.as_ref().unwrap_or(&libdir_default);
78 let mandir = builder.config.mandir.as_ref().unwrap_or(&mandir_default);
80 let sysconfdir = prefix.join(sysconfdir);
81 let datadir = prefix.join(datadir);
82 let docdir = prefix.join(docdir);
83 let bindir = prefix.join(bindir);
84 let libdir = prefix.join(libdir);
85 let mandir = prefix.join(mandir);
87 let destdir = env::var_os("DESTDIR").map(PathBuf::from);
89 let prefix = add_destdir(&prefix, &destdir);
90 let sysconfdir = add_destdir(&sysconfdir, &destdir);
91 let datadir = add_destdir(&datadir, &destdir);
92 let docdir = add_destdir(&docdir, &destdir);
93 let bindir = add_destdir(&bindir, &destdir);
94 let libdir = add_destdir(&libdir, &destdir);
95 let mandir = add_destdir(&mandir, &destdir);
97 let empty_dir = builder.out.join("tmp/empty_dir");
99 t!(fs::create_dir_all(&empty_dir));
100 let package_name = if let Some(host) = host {
101 format!("{}-{}", pkgname(builder, name), host)
103 pkgname(builder, name)
106 let mut cmd = Command::new("sh");
107 cmd.current_dir(&empty_dir)
108 .arg(sanitize_sh(&tmpdir(builder).join(&package_name).join("install.sh")))
109 .arg(format!("--prefix={}", sanitize_sh(&prefix)))
110 .arg(format!("--sysconfdir={}", sanitize_sh(&sysconfdir)))
111 .arg(format!("--datadir={}", sanitize_sh(&datadir)))
112 .arg(format!("--docdir={}", sanitize_sh(&docdir)))
113 .arg(format!("--bindir={}", sanitize_sh(&bindir)))
114 .arg(format!("--libdir={}", sanitize_sh(&libdir)))
115 .arg(format!("--mandir={}", sanitize_sh(&mandir)))
116 .arg("--disable-ldconfig");
117 builder.run(&mut cmd);
118 t!(fs::remove_dir_all(&empty_dir));
121 fn add_destdir(path: &Path, destdir: &Option<PathBuf>) -> PathBuf {
122 let mut ret = match *destdir {
123 Some(ref dest) => dest.clone(),
124 None => return path.to_path_buf(),
126 for part in path.components() {
128 Component::Normal(s) => ret.push(s),
135 macro_rules! install {
136 (($sel:ident, $builder:ident, $_config:ident),
140 only_hosts: $only_hosts:expr,
141 $run_item:block $(, $c:ident)*;)+) => {
143 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
146 pub target: Interned<String>,
147 pub host: Interned<String>,
152 fn should_build(config: &Config) -> bool {
153 config.extended && config.tools.as_ref()
154 .map_or(true, |t| t.contains($path))
158 fn should_install(builder: &Builder) -> bool {
159 builder.config.tools.as_ref().map_or(false, |t| t.contains($path))
163 impl Step for $name {
165 const DEFAULT: bool = true;
166 const ONLY_HOSTS: bool = $only_hosts;
167 $(const $c: bool = true;)*
169 fn should_run(run: ShouldRun) -> ShouldRun {
170 let $_config = &run.builder.config;
171 run.path($path).default_condition($default_cond)
174 fn make_run(run: RunConfig) {
175 run.builder.ensure($name {
176 stage: run.builder.top_stage,
178 host: run.builder.config.build,
182 fn run($sel, $builder: &Builder) {
189 install!((self, builder, _config),
190 Docs, "src/doc", _config.docs, only_hosts: false, {
191 builder.ensure(dist::Docs { stage: self.stage, host: self.target });
192 install_docs(builder, self.stage, self.target);
194 Std, "src/libstd", true, only_hosts: true, {
195 for target in &builder.targets {
196 builder.ensure(dist::Std {
197 compiler: builder.compiler(self.stage, self.host),
200 install_std(builder, self.stage, *target);
203 Cargo, "cargo", Self::should_build(_config), only_hosts: true, {
204 builder.ensure(dist::Cargo { stage: self.stage, target: self.target });
205 install_cargo(builder, self.stage, self.target);
207 Rls, "rls", Self::should_build(_config), only_hosts: true, {
208 if builder.ensure(dist::Rls { stage: self.stage, target: self.target }).is_some() ||
209 Self::should_install(builder) {
210 install_rls(builder, self.stage, self.target);
212 builder.info(&format!("skipping Install RLS stage{} ({})", self.stage, self.target));
215 Clippy, "clippy", Self::should_build(_config), only_hosts: true, {
216 if builder.ensure(dist::Clippy { stage: self.stage, target: self.target }).is_some() ||
217 Self::should_install(builder) {
218 install_clippy(builder, self.stage, self.target);
220 builder.info(&format!("skipping Install clippy stage{} ({})", self.stage, self.target));
223 Miri, "miri", Self::should_build(_config), only_hosts: true, {
224 if builder.ensure(dist::Clippy { stage: self.stage, target: self.target }).is_some() ||
225 Self::should_install(builder) {
226 install_miri(builder, self.stage, self.target);
228 builder.info(&format!("skipping Install miri stage{} ({})", self.stage, self.target));
231 Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, {
232 if builder.ensure(dist::Rustfmt { stage: self.stage, target: self.target }).is_some() ||
233 Self::should_install(builder) {
234 install_rustfmt(builder, self.stage, self.target);
237 &format!("skipping Install Rustfmt stage{} ({})", self.stage, self.target));
240 Analysis, "analysis", Self::should_build(_config), only_hosts: false, {
241 builder.ensure(dist::Analysis {
242 compiler: builder.compiler(self.stage, self.host),
245 install_analysis(builder, self.stage, self.target);
247 Rustc, "src/librustc", true, only_hosts: true, {
248 builder.ensure(dist::Rustc {
249 compiler: builder.compiler(self.stage, self.target),
251 install_rustc(builder, self.stage, self.target);
255 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
262 const DEFAULT: bool = true;
263 const ONLY_HOSTS: bool = true;
265 fn should_run(run: ShouldRun) -> ShouldRun {
266 let config = &run.builder.config;
267 let cond = config.extended &&
268 config.tools.as_ref().map_or(true, |t| t.contains("src"));
269 run.path("src").default_condition(cond)
272 fn make_run(run: RunConfig) {
273 run.builder.ensure(Src {
274 stage: run.builder.top_stage,
278 fn run(self, builder: &Builder) {
279 builder.ensure(dist::Src);
280 install_src(builder, self.stage);