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