]>
Commit | Line | Data |
---|---|---|
c30ab7b3 SL |
1 | // Copyright 2016 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | //! Implementation of the install aspects of the compiler. | |
12 | //! | |
13 | //! This module is responsible for installing the standard library, | |
14 | //! compiler, and documentation. | |
15 | ||
32a655c1 | 16 | use std::env; |
c30ab7b3 | 17 | use std::fs; |
32a655c1 | 18 | use std::path::{Path, PathBuf, Component}; |
c30ab7b3 SL |
19 | use std::process::Command; |
20 | ||
3b2f2976 | 21 | use dist::{self, pkgname, sanitize_sh, tmpdir}; |
7cac9316 | 22 | |
3b2f2976 XL |
23 | use builder::{Builder, RunConfig, ShouldRun, Step}; |
24 | use cache::Interned; | |
2c00a5a8 | 25 | use config::Config; |
7cac9316 | 26 | |
3b2f2976 XL |
27 | pub fn install_docs(builder: &Builder, stage: u32, host: Interned<String>) { |
28 | install_sh(builder, "docs", "rust-docs", stage, Some(host)); | |
29 | } | |
cc61c64b | 30 | |
abe05a73 XL |
31 | pub fn install_std(builder: &Builder, stage: u32, target: Interned<String>) { |
32 | install_sh(builder, "std", "rust-std", stage, Some(target)); | |
3b2f2976 | 33 | } |
c30ab7b3 | 34 | |
3b2f2976 XL |
35 | pub fn install_cargo(builder: &Builder, stage: u32, host: Interned<String>) { |
36 | install_sh(builder, "cargo", "cargo", stage, Some(host)); | |
37 | } | |
7cac9316 | 38 | |
3b2f2976 XL |
39 | pub fn install_rls(builder: &Builder, stage: u32, host: Interned<String>) { |
40 | install_sh(builder, "rls", "rls", stage, Some(host)); | |
41 | } | |
7cac9316 | 42 | |
abe05a73 XL |
43 | pub fn install_rustfmt(builder: &Builder, stage: u32, host: Interned<String>) { |
44 | install_sh(builder, "rustfmt", "rustfmt", stage, Some(host)); | |
45 | } | |
46 | ||
3b2f2976 XL |
47 | pub fn install_analysis(builder: &Builder, stage: u32, host: Interned<String>) { |
48 | install_sh(builder, "analysis", "rust-analysis", stage, Some(host)); | |
49 | } | |
7cac9316 | 50 | |
3b2f2976 XL |
51 | pub fn install_src(builder: &Builder, stage: u32) { |
52 | install_sh(builder, "src", "rust-src", stage, None); | |
53 | } | |
54 | pub fn install_rustc(builder: &Builder, stage: u32, host: Interned<String>) { | |
55 | install_sh(builder, "rustc", "rustc", stage, Some(host)); | |
56 | } | |
7cac9316 | 57 | |
3b2f2976 XL |
58 | fn install_sh( |
59 | builder: &Builder, | |
60 | package: &str, | |
61 | name: &str, | |
62 | stage: u32, | |
63 | host: Option<Interned<String>> | |
64 | ) { | |
65 | let build = builder.build; | |
66 | println!("Install {} stage{} ({:?})", package, stage, host); | |
67 | ||
68 | let prefix_default = PathBuf::from("/usr/local"); | |
69 | let sysconfdir_default = PathBuf::from("/etc"); | |
2c00a5a8 XL |
70 | let datadir_default = PathBuf::from("share"); |
71 | let docdir_default = datadir_default.join("doc/rust"); | |
3b2f2976 XL |
72 | let bindir_default = PathBuf::from("bin"); |
73 | let libdir_default = PathBuf::from("lib"); | |
2c00a5a8 | 74 | let mandir_default = datadir_default.join("man"); |
3b2f2976 XL |
75 | let prefix = build.config.prefix.as_ref().unwrap_or(&prefix_default); |
76 | let sysconfdir = build.config.sysconfdir.as_ref().unwrap_or(&sysconfdir_default); | |
2c00a5a8 | 77 | let datadir = build.config.datadir.as_ref().unwrap_or(&datadir_default); |
3b2f2976 XL |
78 | let docdir = build.config.docdir.as_ref().unwrap_or(&docdir_default); |
79 | let bindir = build.config.bindir.as_ref().unwrap_or(&bindir_default); | |
80 | let libdir = build.config.libdir.as_ref().unwrap_or(&libdir_default); | |
81 | let mandir = build.config.mandir.as_ref().unwrap_or(&mandir_default); | |
82 | ||
83 | let sysconfdir = prefix.join(sysconfdir); | |
2c00a5a8 | 84 | let datadir = prefix.join(datadir); |
3b2f2976 XL |
85 | let docdir = prefix.join(docdir); |
86 | let bindir = prefix.join(bindir); | |
87 | let libdir = prefix.join(libdir); | |
88 | let mandir = prefix.join(mandir); | |
89 | ||
90 | let destdir = env::var_os("DESTDIR").map(PathBuf::from); | |
91 | ||
92 | let prefix = add_destdir(&prefix, &destdir); | |
93 | let sysconfdir = add_destdir(&sysconfdir, &destdir); | |
2c00a5a8 | 94 | let datadir = add_destdir(&datadir, &destdir); |
3b2f2976 XL |
95 | let docdir = add_destdir(&docdir, &destdir); |
96 | let bindir = add_destdir(&bindir, &destdir); | |
97 | let libdir = add_destdir(&libdir, &destdir); | |
98 | let mandir = add_destdir(&mandir, &destdir); | |
99 | ||
100 | let empty_dir = build.out.join("tmp/empty_dir"); | |
101 | ||
102 | t!(fs::create_dir_all(&empty_dir)); | |
103 | let package_name = if let Some(host) = host { | |
104 | format!("{}-{}", pkgname(build, name), host) | |
105 | } else { | |
106 | pkgname(build, name) | |
107 | }; | |
7cac9316 | 108 | |
3b2f2976 XL |
109 | let mut cmd = Command::new("sh"); |
110 | cmd.current_dir(&empty_dir) | |
111 | .arg(sanitize_sh(&tmpdir(build).join(&package_name).join("install.sh"))) | |
112 | .arg(format!("--prefix={}", sanitize_sh(&prefix))) | |
113 | .arg(format!("--sysconfdir={}", sanitize_sh(&sysconfdir))) | |
2c00a5a8 | 114 | .arg(format!("--datadir={}", sanitize_sh(&datadir))) |
3b2f2976 XL |
115 | .arg(format!("--docdir={}", sanitize_sh(&docdir))) |
116 | .arg(format!("--bindir={}", sanitize_sh(&bindir))) | |
117 | .arg(format!("--libdir={}", sanitize_sh(&libdir))) | |
118 | .arg(format!("--mandir={}", sanitize_sh(&mandir))) | |
119 | .arg("--disable-ldconfig"); | |
120 | build.run(&mut cmd); | |
121 | t!(fs::remove_dir_all(&empty_dir)); | |
c30ab7b3 | 122 | } |
32a655c1 SL |
123 | |
124 | fn add_destdir(path: &Path, destdir: &Option<PathBuf>) -> PathBuf { | |
125 | let mut ret = match *destdir { | |
126 | Some(ref dest) => dest.clone(), | |
127 | None => return path.to_path_buf(), | |
128 | }; | |
129 | for part in path.components() { | |
130 | match part { | |
131 | Component::Normal(s) => ret.push(s), | |
132 | _ => {} | |
133 | } | |
134 | } | |
041b39d2 | 135 | ret |
32a655c1 | 136 | } |
3b2f2976 XL |
137 | |
138 | macro_rules! install { | |
139 | (($sel:ident, $builder:ident, $_config:ident), | |
140 | $($name:ident, | |
141 | $path:expr, | |
142 | $default_cond:expr, | |
143 | only_hosts: $only_hosts:expr, | |
144 | $run_item:block $(, $c:ident)*;)+) => { | |
145 | $( | |
146 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] | |
147 | pub struct $name { | |
148 | pub stage: u32, | |
149 | pub target: Interned<String>, | |
150 | pub host: Interned<String>, | |
151 | } | |
152 | ||
2c00a5a8 XL |
153 | impl $name { |
154 | #[allow(dead_code)] | |
155 | fn should_build(config: &Config) -> bool { | |
156 | config.extended && config.tools.as_ref() | |
157 | .map_or(true, |t| t.contains($path)) | |
158 | } | |
159 | ||
160 | #[allow(dead_code)] | |
161 | fn should_install(builder: &Builder) -> bool { | |
162 | builder.config.tools.as_ref().map_or(false, |t| t.contains($path)) | |
163 | } | |
164 | } | |
165 | ||
3b2f2976 XL |
166 | impl Step for $name { |
167 | type Output = (); | |
168 | const DEFAULT: bool = true; | |
3b2f2976 XL |
169 | const ONLY_HOSTS: bool = $only_hosts; |
170 | $(const $c: bool = true;)* | |
171 | ||
172 | fn should_run(run: ShouldRun) -> ShouldRun { | |
173 | let $_config = &run.builder.config; | |
174 | run.path($path).default_condition($default_cond) | |
175 | } | |
176 | ||
177 | fn make_run(run: RunConfig) { | |
178 | run.builder.ensure($name { | |
179 | stage: run.builder.top_stage, | |
180 | target: run.target, | |
0531ce1d | 181 | host: run.builder.build.build, |
3b2f2976 XL |
182 | }); |
183 | } | |
184 | ||
185 | fn run($sel, $builder: &Builder) { | |
186 | $run_item | |
187 | } | |
188 | })+ | |
189 | } | |
190 | } | |
191 | ||
192 | install!((self, builder, _config), | |
193 | Docs, "src/doc", _config.docs, only_hosts: false, { | |
194 | builder.ensure(dist::Docs { stage: self.stage, host: self.target }); | |
195 | install_docs(builder, self.stage, self.target); | |
196 | }; | |
197 | Std, "src/libstd", true, only_hosts: true, { | |
abe05a73 XL |
198 | for target in &builder.build.targets { |
199 | builder.ensure(dist::Std { | |
200 | compiler: builder.compiler(self.stage, self.host), | |
201 | target: *target | |
202 | }); | |
203 | install_std(builder, self.stage, *target); | |
204 | } | |
3b2f2976 | 205 | }; |
2c00a5a8 | 206 | Cargo, "cargo", Self::should_build(_config), only_hosts: true, { |
3b2f2976 XL |
207 | builder.ensure(dist::Cargo { stage: self.stage, target: self.target }); |
208 | install_cargo(builder, self.stage, self.target); | |
209 | }; | |
2c00a5a8 XL |
210 | Rls, "rls", Self::should_build(_config), only_hosts: true, { |
211 | if builder.ensure(dist::Rls { stage: self.stage, target: self.target }).is_some() || | |
212 | Self::should_install(builder) { | |
abe05a73 XL |
213 | install_rls(builder, self.stage, self.target); |
214 | } else { | |
215 | println!("skipping Install RLS stage{} ({})", self.stage, self.target); | |
216 | } | |
217 | }; | |
2c00a5a8 XL |
218 | Rustfmt, "rustfmt", Self::should_build(_config), only_hosts: true, { |
219 | if builder.ensure(dist::Rustfmt { stage: self.stage, target: self.target }).is_some() || | |
220 | Self::should_install(builder) { | |
abe05a73 XL |
221 | install_rustfmt(builder, self.stage, self.target); |
222 | } else { | |
223 | println!("skipping Install Rustfmt stage{} ({})", self.stage, self.target); | |
224 | } | |
3b2f2976 | 225 | }; |
2c00a5a8 | 226 | Analysis, "analysis", Self::should_build(_config), only_hosts: false, { |
3b2f2976 XL |
227 | builder.ensure(dist::Analysis { |
228 | compiler: builder.compiler(self.stage, self.host), | |
229 | target: self.target | |
230 | }); | |
231 | install_analysis(builder, self.stage, self.target); | |
232 | }; | |
3b2f2976 XL |
233 | Rustc, "src/librustc", true, only_hosts: true, { |
234 | builder.ensure(dist::Rustc { | |
235 | compiler: builder.compiler(self.stage, self.target), | |
236 | }); | |
237 | install_rustc(builder, self.stage, self.target); | |
238 | }; | |
239 | ); | |
0531ce1d XL |
240 | |
241 | #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] | |
242 | pub struct Src { | |
243 | pub stage: u32, | |
244 | } | |
245 | ||
246 | impl Step for Src { | |
247 | type Output = (); | |
248 | const DEFAULT: bool = true; | |
249 | const ONLY_HOSTS: bool = true; | |
250 | ||
251 | fn should_run(run: ShouldRun) -> ShouldRun { | |
252 | let config = &run.builder.config; | |
253 | let cond = config.extended && | |
254 | config.tools.as_ref().map_or(true, |t| t.contains("src")); | |
255 | run.path("src").default_condition(cond) | |
256 | } | |
257 | ||
258 | fn make_run(run: RunConfig) { | |
259 | run.builder.ensure(Src { | |
260 | stage: run.builder.top_stage, | |
261 | }); | |
262 | } | |
263 | ||
264 | fn run(self, builder: &Builder) { | |
265 | builder.ensure(dist::Src); | |
266 | install_src(builder, self.stage); | |
267 | } | |
268 | } |