]> git.proxmox.com Git - rustc.git/blob - src/bootstrap/compile.rs
New upstream version 1.28.0~beta.14+dfsg1
[rustc.git] / src / bootstrap / compile.rs
1 // Copyright 2015 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 compiling various phases of the compiler and standard
12 //! library.
13 //!
14 //! This module contains some of the real meat in the rustbuild build system
15 //! which is where Cargo is used to compiler the standard library, libtest, and
16 //! compiler. This module is also responsible for assembling the sysroot as it
17 //! goes along from the output of the previous stage.
18
19 use std::borrow::Cow;
20 use std::env;
21 use std::fs::{self, File};
22 use std::io::BufReader;
23 use std::io::prelude::*;
24 use std::path::{Path, PathBuf};
25 use std::process::{Command, Stdio};
26 use std::str;
27 use std::cmp::min;
28
29 use build_helper::{output, mtime, up_to_date};
30 use filetime::FileTime;
31 use serde_json;
32
33 use util::{exe, libdir, is_dylib, CiEnv};
34 use {Compiler, Mode};
35 use native;
36 use tool;
37
38 use cache::{INTERNER, Interned};
39 use builder::{Step, RunConfig, ShouldRun, Builder};
40
41 #[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)]
42 pub struct Std {
43 pub target: Interned<String>,
44 pub compiler: Compiler,
45 }
46
47 impl Step for Std {
48 type Output = ();
49 const DEFAULT: bool = true;
50
51 fn should_run(run: ShouldRun) -> ShouldRun {
52 run.all_krates("std")
53 }
54
55 fn make_run(run: RunConfig) {
56 run.builder.ensure(Std {
57 compiler: run.builder.compiler(run.builder.top_stage, run.host),
58 target: run.target,
59 });
60 }
61
62 /// Build the standard library.
63 ///
64 /// This will build the standard library for a particular stage of the build
65 /// using the `compiler` targeting the `target` architecture. The artifacts
66 /// created will also be linked into the sysroot directory.
67 fn run(self, builder: &Builder) {
68 let target = self.target;
69 let compiler = self.compiler;
70
71 builder.ensure(StartupObjects { compiler, target });
72
73 if builder.force_use_stage1(compiler, target) {
74 let from = builder.compiler(1, builder.config.build);
75 builder.ensure(Std {
76 compiler: from,
77 target,
78 });
79 builder.info(&format!("Uplifting stage1 std ({} -> {})", from.host, target));
80
81 // Even if we're not building std this stage, the new sysroot must
82 // still contain the musl startup objects.
83 if target.contains("musl") {
84 let libdir = builder.sysroot_libdir(compiler, target);
85 copy_musl_third_party_objects(builder, target, &libdir);
86 }
87
88 builder.ensure(StdLink {
89 compiler: from,
90 target_compiler: compiler,
91 target,
92 });
93 return;
94 }
95
96 if target.contains("musl") {
97 let libdir = builder.sysroot_libdir(compiler, target);
98 copy_musl_third_party_objects(builder, target, &libdir);
99 }
100
101 let out_dir = builder.cargo_out(compiler, Mode::Std, target);
102 builder.clear_if_dirty(&out_dir, &builder.rustc(compiler));
103 let mut cargo = builder.cargo(compiler, Mode::Std, target, "build");
104 std_cargo(builder, &compiler, target, &mut cargo);
105
106 let _folder = builder.fold_output(|| format!("stage{}-std", compiler.stage));
107 builder.info(&format!("Building stage{} std artifacts ({} -> {})", compiler.stage,
108 &compiler.host, target));
109 run_cargo(builder,
110 &mut cargo,
111 &libstd_stamp(builder, compiler, target),
112 false);
113
114 builder.ensure(StdLink {
115 compiler: builder.compiler(compiler.stage, builder.config.build),
116 target_compiler: compiler,
117 target,
118 });
119 }
120 }
121
122 /// Copies the crt(1,i,n).o startup objects
123 ///
124 /// Since musl supports fully static linking, we can cross link for it even
125 /// with a glibc-targeting toolchain, given we have the appropriate startup
126 /// files. As those shipped with glibc won't work, copy the ones provided by
127 /// musl so we have them on linux-gnu hosts.
128 fn copy_musl_third_party_objects(builder: &Builder,
129 target: Interned<String>,
130 into: &Path) {
131 for &obj in &["crt1.o", "crti.o", "crtn.o"] {
132 builder.copy(&builder.musl_root(target).unwrap().join("lib").join(obj), &into.join(obj));
133 }
134 }
135
136 /// Configure cargo to compile the standard library, adding appropriate env vars
137 /// and such.
138 pub fn std_cargo(builder: &Builder,
139 compiler: &Compiler,
140 target: Interned<String>,
141 cargo: &mut Command) {
142 if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
143 cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
144 }
145
146 if builder.no_std(target) == Some(true) {
147 // for no-std targets we only compile a few no_std crates
148 cargo.arg("--features").arg("c mem")
149 .args(&["-p", "alloc"])
150 .args(&["-p", "compiler_builtins"])
151 .args(&["-p", "std_unicode"])
152 .arg("--manifest-path")
153 .arg(builder.src.join("src/rustc/compiler_builtins_shim/Cargo.toml"));
154 } else {
155 let mut features = builder.std_features();
156
157 // When doing a local rebuild we tell cargo that we're stage1 rather than
158 // stage0. This works fine if the local rust and being-built rust have the
159 // same view of what the default allocator is, but fails otherwise. Since
160 // we don't have a way to express an allocator preference yet, work
161 // around the issue in the case of a local rebuild with jemalloc disabled.
162 if compiler.stage == 0 && builder.local_rebuild && !builder.config.use_jemalloc {
163 features.push_str(" force_alloc_system");
164 }
165
166 if compiler.stage != 0 && builder.config.sanitizers {
167 // This variable is used by the sanitizer runtime crates, e.g.
168 // rustc_lsan, to build the sanitizer runtime from C code
169 // When this variable is missing, those crates won't compile the C code,
170 // so we don't set this variable during stage0 where llvm-config is
171 // missing
172 // We also only build the runtimes when --enable-sanitizers (or its
173 // config.toml equivalent) is used
174 let llvm_config = builder.ensure(native::Llvm {
175 target: builder.config.build,
176 emscripten: false,
177 });
178 cargo.env("LLVM_CONFIG", llvm_config);
179 }
180
181 cargo.arg("--features").arg(features)
182 .arg("--manifest-path")
183 .arg(builder.src.join("src/libstd/Cargo.toml"));
184
185 if let Some(target) = builder.config.target_config.get(&target) {
186 if let Some(ref jemalloc) = target.jemalloc {
187 cargo.env("JEMALLOC_OVERRIDE", jemalloc);
188 }
189 }
190 if target.contains("musl") {
191 if let Some(p) = builder.musl_root(target) {
192 cargo.env("MUSL_ROOT", p);
193 }
194 }
195 }
196 }
197
198 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
199 struct StdLink {
200 pub compiler: Compiler,
201 pub target_compiler: Compiler,
202 pub target: Interned<String>,
203 }
204
205 impl Step for StdLink {
206 type Output = ();
207
208 fn should_run(run: ShouldRun) -> ShouldRun {
209 run.never()
210 }
211
212 /// Link all libstd rlibs/dylibs into the sysroot location.
213 ///
214 /// Links those artifacts generated by `compiler` to a the `stage` compiler's
215 /// sysroot for the specified `host` and `target`.
216 ///
217 /// Note that this assumes that `compiler` has already generated the libstd
218 /// libraries for `target`, and this method will find them in the relevant
219 /// output directory.
220 fn run(self, builder: &Builder) {
221 let compiler = self.compiler;
222 let target_compiler = self.target_compiler;
223 let target = self.target;
224 builder.info(&format!("Copying stage{} std from stage{} ({} -> {} / {})",
225 target_compiler.stage,
226 compiler.stage,
227 &compiler.host,
228 target_compiler.host,
229 target));
230 let libdir = builder.sysroot_libdir(target_compiler, target);
231 add_to_sysroot(builder, &libdir, &libstd_stamp(builder, compiler, target));
232
233 if builder.config.sanitizers && compiler.stage != 0 && target == "x86_64-apple-darwin" {
234 // The sanitizers are only built in stage1 or above, so the dylibs will
235 // be missing in stage0 and causes panic. See the `std()` function above
236 // for reason why the sanitizers are not built in stage0.
237 copy_apple_sanitizer_dylibs(builder, &builder.native_dir(target), "osx", &libdir);
238 }
239
240 builder.ensure(tool::CleanTools {
241 compiler: target_compiler,
242 target,
243 cause: Mode::Std,
244 });
245 }
246 }
247
248 fn copy_apple_sanitizer_dylibs(builder: &Builder, native_dir: &Path, platform: &str, into: &Path) {
249 for &sanitizer in &["asan", "tsan"] {
250 let filename = format!("libclang_rt.{}_{}_dynamic.dylib", sanitizer, platform);
251 let mut src_path = native_dir.join(sanitizer);
252 src_path.push("build");
253 src_path.push("lib");
254 src_path.push("darwin");
255 src_path.push(&filename);
256 builder.copy(&src_path, &into.join(filename));
257 }
258 }
259
260 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
261 pub struct StartupObjects {
262 pub compiler: Compiler,
263 pub target: Interned<String>,
264 }
265
266 impl Step for StartupObjects {
267 type Output = ();
268
269 fn should_run(run: ShouldRun) -> ShouldRun {
270 run.path("src/rtstartup")
271 }
272
273 fn make_run(run: RunConfig) {
274 run.builder.ensure(StartupObjects {
275 compiler: run.builder.compiler(run.builder.top_stage, run.host),
276 target: run.target,
277 });
278 }
279
280 /// Build and prepare startup objects like rsbegin.o and rsend.o
281 ///
282 /// These are primarily used on Windows right now for linking executables/dlls.
283 /// They don't require any library support as they're just plain old object
284 /// files, so we just use the nightly snapshot compiler to always build them (as
285 /// no other compilers are guaranteed to be available).
286 fn run(self, builder: &Builder) {
287 let for_compiler = self.compiler;
288 let target = self.target;
289 if !target.contains("pc-windows-gnu") {
290 return
291 }
292
293 let src_dir = &builder.src.join("src/rtstartup");
294 let dst_dir = &builder.native_dir(target).join("rtstartup");
295 let sysroot_dir = &builder.sysroot_libdir(for_compiler, target);
296 t!(fs::create_dir_all(dst_dir));
297
298 for file in &["rsbegin", "rsend"] {
299 let src_file = &src_dir.join(file.to_string() + ".rs");
300 let dst_file = &dst_dir.join(file.to_string() + ".o");
301 if !up_to_date(src_file, dst_file) {
302 let mut cmd = Command::new(&builder.initial_rustc);
303 builder.run(cmd.env("RUSTC_BOOTSTRAP", "1")
304 .arg("--cfg").arg("stage0")
305 .arg("--target").arg(target)
306 .arg("--emit=obj")
307 .arg("-o").arg(dst_file)
308 .arg(src_file));
309 }
310
311 builder.copy(dst_file, &sysroot_dir.join(file.to_string() + ".o"));
312 }
313
314 for obj in ["crt2.o", "dllcrt2.o"].iter() {
315 let src = compiler_file(builder,
316 builder.cc(target),
317 target,
318 obj);
319 builder.copy(&src, &sysroot_dir.join(obj));
320 }
321 }
322 }
323
324 #[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)]
325 pub struct Test {
326 pub target: Interned<String>,
327 pub compiler: Compiler,
328 }
329
330 impl Step for Test {
331 type Output = ();
332 const DEFAULT: bool = true;
333
334 fn should_run(run: ShouldRun) -> ShouldRun {
335 run.all_krates("test")
336 }
337
338 fn make_run(run: RunConfig) {
339 run.builder.ensure(Test {
340 compiler: run.builder.compiler(run.builder.top_stage, run.host),
341 target: run.target,
342 });
343 }
344
345 /// Build libtest.
346 ///
347 /// This will build libtest and supporting libraries for a particular stage of
348 /// the build using the `compiler` targeting the `target` architecture. The
349 /// artifacts created will also be linked into the sysroot directory.
350 fn run(self, builder: &Builder) {
351 let target = self.target;
352 let compiler = self.compiler;
353
354 builder.ensure(Std { compiler, target });
355
356 if builder.force_use_stage1(compiler, target) {
357 builder.ensure(Test {
358 compiler: builder.compiler(1, builder.config.build),
359 target,
360 });
361 builder.info(
362 &format!("Uplifting stage1 test ({} -> {})", builder.config.build, target));
363 builder.ensure(TestLink {
364 compiler: builder.compiler(1, builder.config.build),
365 target_compiler: compiler,
366 target,
367 });
368 return;
369 }
370
371 let out_dir = builder.cargo_out(compiler, Mode::Test, target);
372 builder.clear_if_dirty(&out_dir, &libstd_stamp(builder, compiler, target));
373 let mut cargo = builder.cargo(compiler, Mode::Test, target, "build");
374 test_cargo(builder, &compiler, target, &mut cargo);
375
376 let _folder = builder.fold_output(|| format!("stage{}-test", compiler.stage));
377 builder.info(&format!("Building stage{} test artifacts ({} -> {})", compiler.stage,
378 &compiler.host, target));
379 run_cargo(builder,
380 &mut cargo,
381 &libtest_stamp(builder, compiler, target),
382 false);
383
384 builder.ensure(TestLink {
385 compiler: builder.compiler(compiler.stage, builder.config.build),
386 target_compiler: compiler,
387 target,
388 });
389 }
390 }
391
392 /// Same as `std_cargo`, but for libtest
393 pub fn test_cargo(builder: &Builder,
394 _compiler: &Compiler,
395 _target: Interned<String>,
396 cargo: &mut Command) {
397 if let Some(target) = env::var_os("MACOSX_STD_DEPLOYMENT_TARGET") {
398 cargo.env("MACOSX_DEPLOYMENT_TARGET", target);
399 }
400 cargo.arg("--manifest-path")
401 .arg(builder.src.join("src/libtest/Cargo.toml"));
402 }
403
404 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
405 pub struct TestLink {
406 pub compiler: Compiler,
407 pub target_compiler: Compiler,
408 pub target: Interned<String>,
409 }
410
411 impl Step for TestLink {
412 type Output = ();
413
414 fn should_run(run: ShouldRun) -> ShouldRun {
415 run.never()
416 }
417
418 /// Same as `std_link`, only for libtest
419 fn run(self, builder: &Builder) {
420 let compiler = self.compiler;
421 let target_compiler = self.target_compiler;
422 let target = self.target;
423 builder.info(&format!("Copying stage{} test from stage{} ({} -> {} / {})",
424 target_compiler.stage,
425 compiler.stage,
426 &compiler.host,
427 target_compiler.host,
428 target));
429 add_to_sysroot(builder, &builder.sysroot_libdir(target_compiler, target),
430 &libtest_stamp(builder, compiler, target));
431 builder.ensure(tool::CleanTools {
432 compiler: target_compiler,
433 target,
434 cause: Mode::Test,
435 });
436 }
437 }
438
439 #[derive(Debug, PartialOrd, Ord, Copy, Clone, PartialEq, Eq, Hash)]
440 pub struct Rustc {
441 pub target: Interned<String>,
442 pub compiler: Compiler,
443 }
444
445 impl Step for Rustc {
446 type Output = ();
447 const ONLY_HOSTS: bool = true;
448 const DEFAULT: bool = true;
449
450 fn should_run(run: ShouldRun) -> ShouldRun {
451 run.all_krates("rustc-main")
452 }
453
454 fn make_run(run: RunConfig) {
455 run.builder.ensure(Rustc {
456 compiler: run.builder.compiler(run.builder.top_stage, run.host),
457 target: run.target,
458 });
459 }
460
461 /// Build the compiler.
462 ///
463 /// This will build the compiler for a particular stage of the build using
464 /// the `compiler` targeting the `target` architecture. The artifacts
465 /// created will also be linked into the sysroot directory.
466 fn run(self, builder: &Builder) {
467 let compiler = self.compiler;
468 let target = self.target;
469
470 builder.ensure(Test { compiler, target });
471
472 if builder.force_use_stage1(compiler, target) {
473 builder.ensure(Rustc {
474 compiler: builder.compiler(1, builder.config.build),
475 target,
476 });
477 builder.info(&format!("Uplifting stage1 rustc ({} -> {})",
478 builder.config.build, target));
479 builder.ensure(RustcLink {
480 compiler: builder.compiler(1, builder.config.build),
481 target_compiler: compiler,
482 target,
483 });
484 return;
485 }
486
487 // Ensure that build scripts have a std to link against.
488 builder.ensure(Std {
489 compiler: builder.compiler(self.compiler.stage, builder.config.build),
490 target: builder.config.build,
491 });
492 let cargo_out = builder.cargo_out(compiler, Mode::Rustc, target);
493 builder.clear_if_dirty(&cargo_out, &libstd_stamp(builder, compiler, target));
494 builder.clear_if_dirty(&cargo_out, &libtest_stamp(builder, compiler, target));
495
496 let mut cargo = builder.cargo(compiler, Mode::Rustc, target, "build");
497 rustc_cargo(builder, &mut cargo);
498
499 let _folder = builder.fold_output(|| format!("stage{}-rustc", compiler.stage));
500 builder.info(&format!("Building stage{} compiler artifacts ({} -> {})",
501 compiler.stage, &compiler.host, target));
502 run_cargo(builder,
503 &mut cargo,
504 &librustc_stamp(builder, compiler, target),
505 false);
506
507 builder.ensure(RustcLink {
508 compiler: builder.compiler(compiler.stage, builder.config.build),
509 target_compiler: compiler,
510 target,
511 });
512 }
513 }
514
515 pub fn rustc_cargo(builder: &Builder, cargo: &mut Command) {
516 cargo.arg("--features").arg(builder.rustc_features())
517 .arg("--manifest-path")
518 .arg(builder.src.join("src/rustc/Cargo.toml"));
519 rustc_cargo_env(builder, cargo);
520 }
521
522 pub fn rustc_cargo_env(builder: &Builder, cargo: &mut Command) {
523 // Set some configuration variables picked up by build scripts and
524 // the compiler alike
525 cargo.env("CFG_RELEASE", builder.rust_release())
526 .env("CFG_RELEASE_CHANNEL", &builder.config.channel)
527 .env("CFG_VERSION", builder.rust_version())
528 .env("CFG_PREFIX", builder.config.prefix.clone().unwrap_or_default())
529 .env("CFG_CODEGEN_BACKENDS_DIR", &builder.config.rust_codegen_backends_dir);
530
531 let libdir_relative = builder.config.libdir_relative().unwrap_or(Path::new("lib"));
532 cargo.env("CFG_LIBDIR_RELATIVE", libdir_relative);
533
534 // If we're not building a compiler with debugging information then remove
535 // these two env vars which would be set otherwise.
536 if builder.config.rust_debuginfo_only_std {
537 cargo.env_remove("RUSTC_DEBUGINFO");
538 cargo.env_remove("RUSTC_DEBUGINFO_LINES");
539 }
540
541 if let Some(ref ver_date) = builder.rust_info.commit_date() {
542 cargo.env("CFG_VER_DATE", ver_date);
543 }
544 if let Some(ref ver_hash) = builder.rust_info.sha() {
545 cargo.env("CFG_VER_HASH", ver_hash);
546 }
547 if !builder.unstable_features() {
548 cargo.env("CFG_DISABLE_UNSTABLE_FEATURES", "1");
549 }
550 if let Some(ref s) = builder.config.rustc_default_linker {
551 cargo.env("CFG_DEFAULT_LINKER", s);
552 }
553 if builder.config.rustc_parallel_queries {
554 cargo.env("RUSTC_PARALLEL_QUERIES", "1");
555 }
556 }
557
558 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
559 struct RustcLink {
560 pub compiler: Compiler,
561 pub target_compiler: Compiler,
562 pub target: Interned<String>,
563 }
564
565 impl Step for RustcLink {
566 type Output = ();
567
568 fn should_run(run: ShouldRun) -> ShouldRun {
569 run.never()
570 }
571
572 /// Same as `std_link`, only for librustc
573 fn run(self, builder: &Builder) {
574 let compiler = self.compiler;
575 let target_compiler = self.target_compiler;
576 let target = self.target;
577 builder.info(&format!("Copying stage{} rustc from stage{} ({} -> {} / {})",
578 target_compiler.stage,
579 compiler.stage,
580 &compiler.host,
581 target_compiler.host,
582 target));
583 add_to_sysroot(builder, &builder.sysroot_libdir(target_compiler, target),
584 &librustc_stamp(builder, compiler, target));
585 builder.ensure(tool::CleanTools {
586 compiler: target_compiler,
587 target,
588 cause: Mode::Rustc,
589 });
590 }
591 }
592
593 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
594 pub struct CodegenBackend {
595 pub compiler: Compiler,
596 pub target: Interned<String>,
597 pub backend: Interned<String>,
598 }
599
600 impl Step for CodegenBackend {
601 type Output = ();
602 const ONLY_HOSTS: bool = true;
603 const DEFAULT: bool = true;
604
605 fn should_run(run: ShouldRun) -> ShouldRun {
606 run.all_krates("rustc_codegen_llvm")
607 }
608
609 fn make_run(run: RunConfig) {
610 let backend = run.builder.config.rust_codegen_backends.get(0);
611 let backend = backend.cloned().unwrap_or_else(|| {
612 INTERNER.intern_str("llvm")
613 });
614 run.builder.ensure(CodegenBackend {
615 compiler: run.builder.compiler(run.builder.top_stage, run.host),
616 target: run.target,
617 backend,
618 });
619 }
620
621 fn run(self, builder: &Builder) {
622 let compiler = self.compiler;
623 let target = self.target;
624 let backend = self.backend;
625
626 builder.ensure(Rustc { compiler, target });
627
628 if builder.force_use_stage1(compiler, target) {
629 builder.ensure(CodegenBackend {
630 compiler: builder.compiler(1, builder.config.build),
631 target,
632 backend,
633 });
634 return;
635 }
636
637 let mut cargo = builder.cargo(compiler, Mode::Codegen, target, "build");
638 let mut features = builder.rustc_features().to_string();
639 cargo.arg("--manifest-path")
640 .arg(builder.src.join("src/librustc_codegen_llvm/Cargo.toml"));
641 rustc_cargo_env(builder, &mut cargo);
642
643 features += &build_codegen_backend(&builder, &mut cargo, &compiler, target, backend);
644
645 let tmp_stamp = builder.cargo_out(compiler, Mode::Codegen, target)
646 .join(".tmp.stamp");
647
648 let _folder = builder.fold_output(|| format!("stage{}-rustc_codegen_llvm", compiler.stage));
649 let files = run_cargo(builder,
650 cargo.arg("--features").arg(features),
651 &tmp_stamp,
652 false);
653 if builder.config.dry_run {
654 return;
655 }
656 let mut files = files.into_iter()
657 .filter(|f| {
658 let filename = f.file_name().unwrap().to_str().unwrap();
659 is_dylib(filename) && filename.contains("rustc_codegen_llvm-")
660 });
661 let codegen_backend = match files.next() {
662 Some(f) => f,
663 None => panic!("no dylibs built for codegen backend?"),
664 };
665 if let Some(f) = files.next() {
666 panic!("codegen backend built two dylibs:\n{}\n{}",
667 codegen_backend.display(),
668 f.display());
669 }
670 let stamp = codegen_backend_stamp(builder, compiler, target, backend);
671 let codegen_backend = codegen_backend.to_str().unwrap();
672 t!(t!(File::create(&stamp)).write_all(codegen_backend.as_bytes()));
673 }
674 }
675
676 pub fn build_codegen_backend(builder: &Builder,
677 cargo: &mut Command,
678 compiler: &Compiler,
679 target: Interned<String>,
680 backend: Interned<String>) -> String {
681 let mut features = String::new();
682
683 match &*backend {
684 "llvm" | "emscripten" => {
685 // Build LLVM for our target. This will implicitly build the
686 // host LLVM if necessary.
687 let llvm_config = builder.ensure(native::Llvm {
688 target,
689 emscripten: backend == "emscripten",
690 });
691
692 if backend == "emscripten" {
693 features.push_str(" emscripten");
694 }
695
696 builder.info(&format!("Building stage{} codegen artifacts ({} -> {}, {})",
697 compiler.stage, &compiler.host, target, backend));
698
699 // Pass down configuration from the LLVM build into the build of
700 // librustc_llvm and librustc_codegen_llvm.
701 if builder.is_rust_llvm(target) {
702 cargo.env("LLVM_RUSTLLVM", "1");
703 }
704 cargo.env("LLVM_CONFIG", &llvm_config);
705 if backend != "emscripten" {
706 let target_config = builder.config.target_config.get(&target);
707 if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
708 cargo.env("CFG_LLVM_ROOT", s);
709 }
710 }
711 // Building with a static libstdc++ is only supported on linux right now,
712 // not for MSVC or macOS
713 if builder.config.llvm_static_stdcpp &&
714 !target.contains("freebsd") &&
715 !target.contains("windows") &&
716 !target.contains("apple") {
717 let file = compiler_file(builder,
718 builder.cxx(target).unwrap(),
719 target,
720 "libstdc++.a");
721 cargo.env("LLVM_STATIC_STDCPP", file);
722 }
723 if builder.config.llvm_link_shared {
724 cargo.env("LLVM_LINK_SHARED", "1");
725 }
726 }
727 _ => panic!("unknown backend: {}", backend),
728 }
729
730 features
731 }
732
733 /// Creates the `codegen-backends` folder for a compiler that's about to be
734 /// assembled as a complete compiler.
735 ///
736 /// This will take the codegen artifacts produced by `compiler` and link them
737 /// into an appropriate location for `target_compiler` to be a functional
738 /// compiler.
739 fn copy_codegen_backends_to_sysroot(builder: &Builder,
740 compiler: Compiler,
741 target_compiler: Compiler) {
742 let target = target_compiler.host;
743
744 // Note that this step is different than all the other `*Link` steps in
745 // that it's not assembling a bunch of libraries but rather is primarily
746 // moving the codegen backend into place. The codegen backend of rustc is
747 // not linked into the main compiler by default but is rather dynamically
748 // selected at runtime for inclusion.
749 //
750 // Here we're looking for the output dylib of the `CodegenBackend` step and
751 // we're copying that into the `codegen-backends` folder.
752 let dst = builder.sysroot_codegen_backends(target_compiler);
753 t!(fs::create_dir_all(&dst));
754
755 if builder.config.dry_run {
756 return;
757 }
758
759 for backend in builder.config.rust_codegen_backends.iter() {
760 let stamp = codegen_backend_stamp(builder, compiler, target, *backend);
761 let mut dylib = String::new();
762 t!(t!(File::open(&stamp)).read_to_string(&mut dylib));
763 let file = Path::new(&dylib);
764 let filename = file.file_name().unwrap().to_str().unwrap();
765 // change `librustc_codegen_llvm-xxxxxx.so` to `librustc_codegen_llvm-llvm.so`
766 let target_filename = {
767 let dash = filename.find("-").unwrap();
768 let dot = filename.find(".").unwrap();
769 format!("{}-{}{}",
770 &filename[..dash],
771 backend,
772 &filename[dot..])
773 };
774 builder.copy(&file, &dst.join(target_filename));
775 }
776 }
777
778 fn copy_lld_to_sysroot(builder: &Builder,
779 target_compiler: Compiler,
780 lld_install_root: &Path) {
781 let target = target_compiler.host;
782
783 let dst = builder.sysroot_libdir(target_compiler, target)
784 .parent()
785 .unwrap()
786 .join("bin");
787 t!(fs::create_dir_all(&dst));
788
789 let exe = exe("lld", &target);
790 builder.copy(&lld_install_root.join("bin").join(&exe), &dst.join(&exe));
791 }
792
793 /// Cargo's output path for the standard library in a given stage, compiled
794 /// by a particular compiler for the specified target.
795 pub fn libstd_stamp(builder: &Builder, compiler: Compiler, target: Interned<String>) -> PathBuf {
796 builder.cargo_out(compiler, Mode::Std, target).join(".libstd.stamp")
797 }
798
799 /// Cargo's output path for libtest in a given stage, compiled by a particular
800 /// compiler for the specified target.
801 pub fn libtest_stamp(builder: &Builder, compiler: Compiler, target: Interned<String>) -> PathBuf {
802 builder.cargo_out(compiler, Mode::Test, target).join(".libtest.stamp")
803 }
804
805 /// Cargo's output path for librustc in a given stage, compiled by a particular
806 /// compiler for the specified target.
807 pub fn librustc_stamp(builder: &Builder, compiler: Compiler, target: Interned<String>) -> PathBuf {
808 builder.cargo_out(compiler, Mode::Rustc, target).join(".librustc.stamp")
809 }
810
811 /// Cargo's output path for librustc_codegen_llvm in a given stage, compiled by a particular
812 /// compiler for the specified target and backend.
813 fn codegen_backend_stamp(builder: &Builder,
814 compiler: Compiler,
815 target: Interned<String>,
816 backend: Interned<String>) -> PathBuf {
817 builder.cargo_out(compiler, Mode::Codegen, target)
818 .join(format!(".librustc_codegen_llvm-{}.stamp", backend))
819 }
820
821 pub fn compiler_file(builder: &Builder,
822 compiler: &Path,
823 target: Interned<String>,
824 file: &str) -> PathBuf {
825 let mut cmd = Command::new(compiler);
826 cmd.args(builder.cflags(target));
827 cmd.arg(format!("-print-file-name={}", file));
828 let out = output(&mut cmd);
829 PathBuf::from(out.trim())
830 }
831
832 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
833 pub struct Sysroot {
834 pub compiler: Compiler,
835 }
836
837 impl Step for Sysroot {
838 type Output = Interned<PathBuf>;
839
840 fn should_run(run: ShouldRun) -> ShouldRun {
841 run.never()
842 }
843
844 /// Returns the sysroot for the `compiler` specified that *this build system
845 /// generates*.
846 ///
847 /// That is, the sysroot for the stage0 compiler is not what the compiler
848 /// thinks it is by default, but it's the same as the default for stages
849 /// 1-3.
850 fn run(self, builder: &Builder) -> Interned<PathBuf> {
851 let compiler = self.compiler;
852 let sysroot = if compiler.stage == 0 {
853 builder.out.join(&compiler.host).join("stage0-sysroot")
854 } else {
855 builder.out.join(&compiler.host).join(format!("stage{}", compiler.stage))
856 };
857 let _ = fs::remove_dir_all(&sysroot);
858 t!(fs::create_dir_all(&sysroot));
859 INTERNER.intern_path(sysroot)
860 }
861 }
862
863 #[derive(Debug, Copy, PartialOrd, Ord, Clone, PartialEq, Eq, Hash)]
864 pub struct Assemble {
865 /// The compiler which we will produce in this step. Assemble itself will
866 /// take care of ensuring that the necessary prerequisites to do so exist,
867 /// that is, this target can be a stage2 compiler and Assemble will build
868 /// previous stages for you.
869 pub target_compiler: Compiler,
870 }
871
872 impl Step for Assemble {
873 type Output = Compiler;
874
875 fn should_run(run: ShouldRun) -> ShouldRun {
876 run.all_krates("rustc-main")
877 }
878
879 /// Prepare a new compiler from the artifacts in `stage`
880 ///
881 /// This will assemble a compiler in `build/$host/stage$stage`. The compiler
882 /// must have been previously produced by the `stage - 1` builder.build
883 /// compiler.
884 fn run(self, builder: &Builder) -> Compiler {
885 let target_compiler = self.target_compiler;
886
887 if target_compiler.stage == 0 {
888 assert_eq!(builder.config.build, target_compiler.host,
889 "Cannot obtain compiler for non-native build triple at stage 0");
890 // The stage 0 compiler for the build triple is always pre-built.
891 return target_compiler;
892 }
893
894 // Get the compiler that we'll use to bootstrap ourselves.
895 //
896 // Note that this is where the recursive nature of the bootstrap
897 // happens, as this will request the previous stage's compiler on
898 // downwards to stage 0.
899 //
900 // Also note that we're building a compiler for the host platform. We
901 // only assume that we can run `build` artifacts, which means that to
902 // produce some other architecture compiler we need to start from
903 // `build` to get there.
904 //
905 // FIXME: Perhaps we should download those libraries?
906 // It would make builds faster...
907 //
908 // FIXME: It may be faster if we build just a stage 1 compiler and then
909 // use that to bootstrap this compiler forward.
910 let build_compiler =
911 builder.compiler(target_compiler.stage - 1, builder.config.build);
912
913 // Build the libraries for this compiler to link to (i.e., the libraries
914 // it uses at runtime). NOTE: Crates the target compiler compiles don't
915 // link to these. (FIXME: Is that correct? It seems to be correct most
916 // of the time but I think we do link to these for stage2/bin compilers
917 // when not performing a full bootstrap).
918 if builder.config.keep_stage.map_or(false, |s| target_compiler.stage <= s) {
919 builder.verbose("skipping compilation of compiler due to --keep-stage");
920 let compiler = build_compiler;
921 for stage in 0..min(target_compiler.stage, builder.config.keep_stage.unwrap()) {
922 let target_compiler = builder.compiler(stage, target_compiler.host);
923 let target = target_compiler.host;
924 builder.ensure(StdLink { compiler, target_compiler, target });
925 builder.ensure(TestLink { compiler, target_compiler, target });
926 builder.ensure(RustcLink { compiler, target_compiler, target });
927 }
928 } else {
929 builder.ensure(Rustc {
930 compiler: build_compiler,
931 target: target_compiler.host,
932 });
933 for &backend in builder.config.rust_codegen_backends.iter() {
934 builder.ensure(CodegenBackend {
935 compiler: build_compiler,
936 target: target_compiler.host,
937 backend,
938 });
939 }
940 }
941
942 let lld_install = if builder.config.lld_enabled {
943 Some(builder.ensure(native::Lld {
944 target: target_compiler.host,
945 }))
946 } else {
947 None
948 };
949
950 let stage = target_compiler.stage;
951 let host = target_compiler.host;
952 builder.info(&format!("Assembling stage{} compiler ({})", stage, host));
953
954 // Link in all dylibs to the libdir
955 let sysroot = builder.sysroot(target_compiler);
956 let sysroot_libdir = sysroot.join(libdir(&*host));
957 t!(fs::create_dir_all(&sysroot_libdir));
958 let src_libdir = builder.sysroot_libdir(build_compiler, host);
959 for f in builder.read_dir(&src_libdir) {
960 let filename = f.file_name().into_string().unwrap();
961 if is_dylib(&filename) {
962 builder.copy(&f.path(), &sysroot_libdir.join(&filename));
963 }
964 }
965
966 copy_codegen_backends_to_sysroot(builder,
967 build_compiler,
968 target_compiler);
969 if let Some(lld_install) = lld_install {
970 copy_lld_to_sysroot(builder, target_compiler, &lld_install);
971 }
972
973 // Link the compiler binary itself into place
974 let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
975 let rustc = out_dir.join(exe("rustc_binary", &*host));
976 let bindir = sysroot.join("bin");
977 t!(fs::create_dir_all(&bindir));
978 let compiler = builder.rustc(target_compiler);
979 let _ = fs::remove_file(&compiler);
980 builder.copy(&rustc, &compiler);
981
982 target_compiler
983 }
984 }
985
986 /// Link some files into a rustc sysroot.
987 ///
988 /// For a particular stage this will link the file listed in `stamp` into the
989 /// `sysroot_dst` provided.
990 pub fn add_to_sysroot(builder: &Builder, sysroot_dst: &Path, stamp: &Path) {
991 t!(fs::create_dir_all(&sysroot_dst));
992 for path in builder.read_stamp_file(stamp) {
993 builder.copy(&path, &sysroot_dst.join(path.file_name().unwrap()));
994 }
995 }
996
997 // Avoiding a dependency on winapi to keep compile times down
998 #[cfg(unix)]
999 fn stderr_isatty() -> bool {
1000 use libc;
1001 unsafe { libc::isatty(libc::STDERR_FILENO) != 0 }
1002 }
1003 #[cfg(windows)]
1004 fn stderr_isatty() -> bool {
1005 type DWORD = u32;
1006 type BOOL = i32;
1007 type HANDLE = *mut u8;
1008 const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;
1009 extern "system" {
1010 fn GetStdHandle(which: DWORD) -> HANDLE;
1011 fn GetConsoleMode(hConsoleHandle: HANDLE, lpMode: *mut DWORD) -> BOOL;
1012 }
1013 unsafe {
1014 let handle = GetStdHandle(STD_ERROR_HANDLE);
1015 let mut out = 0;
1016 GetConsoleMode(handle, &mut out) != 0
1017 }
1018 }
1019
1020 pub fn run_cargo(builder: &Builder, cargo: &mut Command, stamp: &Path, is_check: bool)
1021 -> Vec<PathBuf>
1022 {
1023 if builder.config.dry_run {
1024 return Vec::new();
1025 }
1026
1027 // `target_root_dir` looks like $dir/$target/release
1028 let target_root_dir = stamp.parent().unwrap();
1029 // `target_deps_dir` looks like $dir/$target/release/deps
1030 let target_deps_dir = target_root_dir.join("deps");
1031 // `host_root_dir` looks like $dir/release
1032 let host_root_dir = target_root_dir.parent().unwrap() // chop off `release`
1033 .parent().unwrap() // chop off `$target`
1034 .join(target_root_dir.file_name().unwrap());
1035
1036 // Spawn Cargo slurping up its JSON output. We'll start building up the
1037 // `deps` array of all files it generated along with a `toplevel` array of
1038 // files we need to probe for later.
1039 let mut deps = Vec::new();
1040 let mut toplevel = Vec::new();
1041 let ok = stream_cargo(builder, cargo, &mut |msg| {
1042 let filenames = match msg {
1043 CargoMessage::CompilerArtifact { filenames, .. } => filenames,
1044 _ => return,
1045 };
1046 for filename in filenames {
1047 // Skip files like executables
1048 if !filename.ends_with(".rlib") &&
1049 !filename.ends_with(".lib") &&
1050 !is_dylib(&filename) &&
1051 !(is_check && filename.ends_with(".rmeta")) {
1052 return;
1053 }
1054
1055 let filename = Path::new(&*filename);
1056
1057 // If this was an output file in the "host dir" we don't actually
1058 // worry about it, it's not relevant for us.
1059 if filename.starts_with(&host_root_dir) {
1060 return;
1061 }
1062
1063 // If this was output in the `deps` dir then this is a precise file
1064 // name (hash included) so we start tracking it.
1065 if filename.starts_with(&target_deps_dir) {
1066 deps.push(filename.to_path_buf());
1067 return;
1068 }
1069
1070 // Otherwise this was a "top level artifact" which right now doesn't
1071 // have a hash in the name, but there's a version of this file in
1072 // the `deps` folder which *does* have a hash in the name. That's
1073 // the one we'll want to we'll probe for it later.
1074 //
1075 // We do not use `Path::file_stem` or `Path::extension` here,
1076 // because some generated files may have multiple extensions e.g.
1077 // `std-<hash>.dll.lib` on Windows. The aforementioned methods only
1078 // split the file name by the last extension (`.lib`) while we need
1079 // to split by all extensions (`.dll.lib`).
1080 let expected_len = t!(filename.metadata()).len();
1081 let filename = filename.file_name().unwrap().to_str().unwrap();
1082 let mut parts = filename.splitn(2, '.');
1083 let file_stem = parts.next().unwrap().to_owned();
1084 let extension = parts.next().unwrap().to_owned();
1085
1086 toplevel.push((file_stem, extension, expected_len));
1087 }
1088 });
1089
1090 if !ok {
1091 panic!("cargo must succeed");
1092 }
1093
1094 // Ok now we need to actually find all the files listed in `toplevel`. We've
1095 // got a list of prefix/extensions and we basically just need to find the
1096 // most recent file in the `deps` folder corresponding to each one.
1097 let contents = t!(target_deps_dir.read_dir())
1098 .map(|e| t!(e))
1099 .map(|e| (e.path(), e.file_name().into_string().unwrap(), t!(e.metadata())))
1100 .collect::<Vec<_>>();
1101 for (prefix, extension, expected_len) in toplevel {
1102 let candidates = contents.iter().filter(|&&(_, ref filename, ref meta)| {
1103 filename.starts_with(&prefix[..]) &&
1104 filename[prefix.len()..].starts_with("-") &&
1105 filename.ends_with(&extension[..]) &&
1106 meta.len() == expected_len
1107 });
1108 let max = candidates.max_by_key(|&&(_, _, ref metadata)| {
1109 FileTime::from_last_modification_time(metadata)
1110 });
1111 let path_to_add = match max {
1112 Some(triple) => triple.0.to_str().unwrap(),
1113 None => panic!("no output generated for {:?} {:?}", prefix, extension),
1114 };
1115 if is_dylib(path_to_add) {
1116 let candidate = format!("{}.lib", path_to_add);
1117 let candidate = PathBuf::from(candidate);
1118 if candidate.exists() {
1119 deps.push(candidate);
1120 }
1121 }
1122 deps.push(path_to_add.into());
1123 }
1124
1125 // Now we want to update the contents of the stamp file, if necessary. First
1126 // we read off the previous contents along with its mtime. If our new
1127 // contents (the list of files to copy) is different or if any dep's mtime
1128 // is newer then we rewrite the stamp file.
1129 deps.sort();
1130 let mut stamp_contents = Vec::new();
1131 if let Ok(mut f) = File::open(stamp) {
1132 t!(f.read_to_end(&mut stamp_contents));
1133 }
1134 let stamp_mtime = mtime(&stamp);
1135 let mut new_contents = Vec::new();
1136 let mut max = None;
1137 let mut max_path = None;
1138 for dep in deps.iter() {
1139 let mtime = mtime(dep);
1140 if Some(mtime) > max {
1141 max = Some(mtime);
1142 max_path = Some(dep.clone());
1143 }
1144 new_contents.extend(dep.to_str().unwrap().as_bytes());
1145 new_contents.extend(b"\0");
1146 }
1147 let max = max.unwrap();
1148 let max_path = max_path.unwrap();
1149 if stamp_contents == new_contents && max <= stamp_mtime {
1150 builder.verbose(&format!("not updating {:?}; contents equal and {:?} <= {:?}",
1151 stamp, max, stamp_mtime));
1152 return deps
1153 }
1154 if max > stamp_mtime {
1155 builder.verbose(&format!("updating {:?} as {:?} changed", stamp, max_path));
1156 } else {
1157 builder.verbose(&format!("updating {:?} as deps changed", stamp));
1158 }
1159 t!(t!(File::create(stamp)).write_all(&new_contents));
1160 deps
1161 }
1162
1163 pub fn stream_cargo(
1164 builder: &Builder,
1165 cargo: &mut Command,
1166 cb: &mut FnMut(CargoMessage),
1167 ) -> bool {
1168 if builder.config.dry_run {
1169 return true;
1170 }
1171 // Instruct Cargo to give us json messages on stdout, critically leaving
1172 // stderr as piped so we can get those pretty colors.
1173 cargo.arg("--message-format").arg("json")
1174 .stdout(Stdio::piped());
1175
1176 if stderr_isatty() && builder.ci_env == CiEnv::None &&
1177 // if the terminal is reported as dumb, then we don't want to enable color for rustc
1178 env::var_os("TERM").map(|t| t != *"dumb").unwrap_or(true) {
1179 // since we pass message-format=json to cargo, we need to tell the rustc
1180 // wrapper to give us colored output if necessary. This is because we
1181 // only want Cargo's JSON output, not rustcs.
1182 cargo.env("RUSTC_COLOR", "1");
1183 }
1184
1185 builder.verbose(&format!("running: {:?}", cargo));
1186 let mut child = match cargo.spawn() {
1187 Ok(child) => child,
1188 Err(e) => panic!("failed to execute command: {:?}\nerror: {}", cargo, e),
1189 };
1190
1191 // Spawn Cargo slurping up its JSON output. We'll start building up the
1192 // `deps` array of all files it generated along with a `toplevel` array of
1193 // files we need to probe for later.
1194 let stdout = BufReader::new(child.stdout.take().unwrap());
1195 for line in stdout.lines() {
1196 let line = t!(line);
1197 match serde_json::from_str::<CargoMessage>(&line) {
1198 Ok(msg) => cb(msg),
1199 // If this was informational, just print it out and continue
1200 Err(_) => println!("{}", line)
1201 }
1202 }
1203
1204 // Make sure Cargo actually succeeded after we read all of its stdout.
1205 let status = t!(child.wait());
1206 if !status.success() {
1207 eprintln!("command did not execute successfully: {:?}\n\
1208 expected success, got: {}",
1209 cargo,
1210 status);
1211 }
1212 status.success()
1213 }
1214
1215 #[derive(Deserialize)]
1216 #[serde(tag = "reason", rename_all = "kebab-case")]
1217 pub enum CargoMessage<'a> {
1218 CompilerArtifact {
1219 package_id: Cow<'a, str>,
1220 features: Vec<Cow<'a, str>>,
1221 filenames: Vec<Cow<'a, str>>,
1222 },
1223 BuildScriptExecuted {
1224 package_id: Cow<'a, str>,
1225 }
1226 }