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));
36 pub fn install_rustfmt(builder: &Builder, stage: u32, host: Interned<String>) {
37 install_sh(builder, "rustfmt", "rustfmt", stage, Some(host));
40 pub fn install_analysis(builder: &Builder, stage: u32, host: Interned<String>) {
41 install_sh(builder, "analysis", "rust-analysis", stage, Some(host));
44 pub fn install_src(builder: &Builder, stage: u32) {
45 install_sh(builder, "src", "rust-src", stage, None);
47 pub fn install_rustc(builder: &Builder, stage: u32, host: Interned<String>) {
48 install_sh(builder, "rustc", "rustc", stage, Some(host));
56 host: Option<Interned<String>>
58 builder.info(&format!("Install {} stage{} ({:?})", package, stage, host));
60 let prefix_default = PathBuf::from("/usr/local");
61 let sysconfdir_default = PathBuf::from("/etc");
62 let datadir_default = PathBuf::from("share");
63 let docdir_default = datadir_default.join("doc/rust");
64 let bindir_default = PathBuf::from("bin");
65 let libdir_default = PathBuf::from("lib");
66 let mandir_default = datadir_default.join("man");
67 let prefix = builder.config.prefix.as_ref().map_or(prefix_default, |p| {
68 fs::canonicalize(p).unwrap_or_else(|_| panic!("could not canonicalize {}", p.display()))
70 let sysconfdir = builder.config.sysconfdir.as_ref().unwrap_or(&sysconfdir_default);
71 let datadir = builder.config.datadir.as_ref().unwrap_or(&datadir_default);
72 let docdir = builder.config.docdir.as_ref().unwrap_or(&docdir_default);
73 let bindir = builder.config.bindir.as_ref().unwrap_or(&bindir_default);
74 let libdir = builder.config.libdir.as_ref().unwrap_or(&libdir_default);
75 let mandir = builder.config.mandir.as_ref().unwrap_or(&mandir_default);
77 let sysconfdir = prefix.join(sysconfdir);
78 let datadir = prefix.join(datadir);
79 let docdir = prefix.join(docdir);
80 let bindir = prefix.join(bindir);
81 let libdir = prefix.join(libdir);
82 let mandir = prefix.join(mandir);
84 let destdir = env::var_os("DESTDIR").map(PathBuf::from);
86 let prefix = add_destdir(&prefix, &destdir);
87 let sysconfdir = add_destdir(&sysconfdir, &destdir);
88 let datadir = add_destdir(&datadir, &destdir);
89 let docdir = add_destdir(&docdir, &destdir);
90 let bindir = add_destdir(&bindir, &destdir);
91 let libdir = add_destdir(&libdir, &destdir);
92 let mandir = add_destdir(&mandir, &destdir);
94 let empty_dir = builder.out.join("tmp/empty_dir");
96 t!(fs::create_dir_all(&empty_dir));
97 let package_name = if let Some(host) = host {
98 format!("{}-{}", pkgname(builder, name), host)
100 pkgname(builder, name)
103 let mut cmd = Command::new("sh");
104 cmd.current_dir(&empty_dir)
105 .arg(sanitize_sh(&tmpdir(builder).join(&package_name).join("install.sh")))
106 .arg(format!("--prefix={}", sanitize_sh(&prefix)))
107 .arg(format!("--sysconfdir={}", sanitize_sh(&sysconfdir)))
108 .arg(format!("--datadir={}", sanitize_sh(&datadir)))
109 .arg(format!("--docdir={}", sanitize_sh(&docdir)))
110 .arg(format!("--bindir={}", sanitize_sh(&bindir)))
111 .arg(format!("--libdir={}", sanitize_sh(&libdir)))
112 .arg(format!("--mandir={}", sanitize_sh(&mandir)))
113 .arg("--disable-ldconfig");
114 builder.run(&mut cmd);
115 t!(fs::remove_dir_all(&empty_dir));
118 fn add_destdir(path: &Path, destdir: &Option<PathBuf>) -> PathBuf {
119 let mut ret = match *destdir {
120 Some(ref dest) => dest.clone(),
121 None => return path.to_path_buf(),
123 for part in path.components() {
125 Component::Normal(s) => ret.push(s),
132 macro_rules! install {
133 (($sel:ident, $builder:ident, $_config:ident),
137 only_hosts: $only_hosts:expr,
138 $run_item:block $(, $c:ident)*;)+) => {
140 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
143 pub target: Interned<String>,
144 pub host: Interned<String>,
149 fn should_build(config: &Config) -> bool {
150 config.extended && config.tools.as_ref()
151 .map_or(true, |t| t.contains($path))
155 fn should_install(builder: &Builder) -> bool {
156 builder.config.tools.as_ref().map_or(false, |t| t.contains($path))
160 impl Step for $name {
162 const DEFAULT: bool = true;
163 const ONLY_HOSTS: bool = $only_hosts;
164 $(const $c: bool = true;)*
166 fn should_run(run: ShouldRun) -> ShouldRun {
167 let $_config = &run.builder.config;
168 run.path($path).default_condition($default_cond)
171 fn make_run(run: RunConfig) {
172 run.builder.ensure($name {
173 stage: run.builder.top_stage,
175 host: run.builder.config.build,
179 fn run($sel, $builder: &Builder) {
186 install!((self, builder, _config),
187 Docs, "src/doc", _config.docs, only_hosts: false, {
188 builder.ensure(dist::Docs { stage: self.stage, host: self.target });
189 install_docs(builder, self.stage, self.target);
191 Std, "src/libstd", true, only_hosts: true, {
192 for target in &builder.targets {
193 builder.ensure(dist::Std {
194 compiler: builder.compiler(self.stage, self.host),
197 install_std(builder, self.stage, *target);
200 Cargo, "cargo", Self::should_build(_config), only_hosts: true, {
201 builder.ensure(dist::Cargo { stage: self.stage, target: self.target });
202 install_cargo(builder, self.stage, self.target);
204 Rls, "rls", Self::should_build(_config), only_hosts: true, {
205 if builder.ensure(dist::Rls { stage: self.stage, target: self.target }).is_some() ||
206 Self::should_install(builder) {
207 install_rls(builder, self.stage, self.target);
209 builder.info(&format!("skipping Install RLS stage{} ({})", self.stage, self.target));
212 Clippy, "clippy", Self::should_build(_config), only_hosts: true, {
213 if builder.ensure(dist::Clippy { stage: self.stage, target: self.target }).is_some() ||
214 Self::should_install(builder) {
215 install_clippy(builder, self.stage, self.target);
217 builder.info(&format!("skipping Install clippy stage{} ({})", self.stage, self.target));
220 Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, {
221 if builder.ensure(dist::Rustfmt { stage: self.stage, target: self.target }).is_some() ||
222 Self::should_install(builder) {
223 install_rustfmt(builder, self.stage, self.target);
226 &format!("skipping Install Rustfmt stage{} ({})", self.stage, self.target));
229 Analysis, "analysis", Self::should_build(_config), only_hosts: false, {
230 builder.ensure(dist::Analysis {
231 compiler: builder.compiler(self.stage, self.host),
234 install_analysis(builder, self.stage, self.target);
236 Rustc, "src/librustc", true, only_hosts: true, {
237 builder.ensure(dist::Rustc {
238 compiler: builder.compiler(self.stage, self.target),
240 install_rustc(builder, self.stage, self.target);
244 #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
251 const DEFAULT: bool = true;
252 const ONLY_HOSTS: bool = true;
254 fn should_run(run: ShouldRun) -> ShouldRun {
255 let config = &run.builder.config;
256 let cond = config.extended &&
257 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 {
263 stage: run.builder.top_stage,
267 fn run(self, builder: &Builder) {
268 builder.ensure(dist::Src);
269 install_src(builder, self.stage);