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.
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.
11 //! Implementation of rustbuild, the Rust build system.
13 //! This module, and its descendants, are the implementation of the Rust build
14 //! system. Most of this build system is backed by Cargo but the outer layer
15 //! here serves as the ability to orchestrate calling Cargo, sequencing Cargo
16 //! builds, building artifacts like LLVM, etc. The goals of rustbuild are:
18 //! * To be an easily understandable, easily extensible, and maintainable build
20 //! * Leverage standard tools in the Rust ecosystem to build the compiler, aka
21 //! crates.io and Cargo.
22 //! * A standard interface to build across all platforms, including MSVC
26 //! Although this build system defers most of the complicated logic to Cargo
27 //! itself, it still needs to maintain a list of targets and dependencies which
28 //! it can itself perform. Rustbuild is made up of a list of rules with
29 //! dependencies amongst them (created in the `step` module) and then knows how
30 //! to execute each in sequence. Each time rustbuild is invoked, it will simply
31 //! iterate through this list of steps and execute each serially in turn. For
32 //! each step rustbuild relies on the step internally being incremental and
33 //! parallel. Note, though, that the `-j` parameter to rustbuild gets forwarded
34 //! to appropriate test harnesses and such.
36 //! Most of the "meaty" steps that matter are backed by Cargo, which does indeed
37 //! have its own parallelism and incremental management. Later steps, like
38 //! tests, aren't incremental and simply run the entire suite currently.
40 //! When you execute `x.py build`, the steps which are executed are:
42 //! * First, the python script is run. This will automatically download the
43 //! stage0 rustc and cargo according to `src/stage0.txt`, or using the cached
44 //! versions if they're available. These are then used to compile rustbuild
45 //! itself (using Cargo). Finally, control is then transferred to rustbuild.
47 //! * Rustbuild takes over, performs sanity checks, probes the environment,
48 //! reads configuration, builds up a list of steps, and then starts executing
51 //! * The stage0 libstd is compiled
52 //! * The stage0 libtest is compiled
53 //! * The stage0 librustc is compiled
54 //! * The stage1 compiler is assembled
55 //! * The stage1 libstd, libtest, librustc are compiled
56 //! * The stage2 compiler is assembled
57 //! * The stage2 libstd, libtest, librustc are compiled
59 //! Each step is driven by a separate Cargo project and rustbuild orchestrates
60 //! copying files between steps and otherwise preparing for Cargo to run.
62 //! ## Further information
64 //! More documentation can be found in each respective module below, and you can
65 //! also check out the `src/bootstrap/README.md` file for more information.
69 extern crate build_helper;
71 extern crate filetime;
74 extern crate num_cpus;
75 extern crate rustc_serialize;
78 use std::collections::HashMap;
81 use std::ffi::OsString;
82 use std::fs::{self, File};
83 use std::path::{Component, PathBuf, Path};
84 use std::process::Command;
86 use build_helper::{run_silent, output};
88 use util::{exe, mtime, libdir, add_lib_path};
90 /// A helper macro to `unwrap` a result except also print out details like:
92 /// * The file/line of the panic
93 /// * The expression that failed
94 /// * The error itself
96 /// This is currently used judiciously throughout the build system rather than
97 /// using a `Result` with `try!`, but this may change one day...
99 ($e:expr) => (match $e {
101 Err(e) => panic!("{} failed with {}", stringify!($e), e),
126 pub unsafe fn setup() {}
129 pub use config::Config;
130 pub use flags::{Flags, Subcommand};
132 /// A structure representing a Rust compiler.
134 /// Each compiler has a `stage` that it is associated with and a `host` that
135 /// corresponds to the platform the compiler runs on. This structure is used as
136 /// a parameter to many methods below.
137 #[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
138 pub struct Compiler<'a> {
143 /// Global configuration for the build system.
145 /// This structure transitively contains all configuration for the build system.
146 /// All filesystem-encoded configuration is in `config`, all flags are in
147 /// `flags`, and then parsed or probed information is listed in the keys below.
149 /// This structure is a parameter of almost all methods in the build system,
150 /// although most functions are implemented as free functions rather than
151 /// methods specifically on this structure itself (to make it easier to
154 // User-specified configuration via config.toml
157 // User-specified configuration via CLI flags
160 // Derived properties from the above two configurations
166 unstable_features: bool,
167 ver_hash: Option<String>,
168 short_ver_hash: Option<String>,
169 ver_date: Option<String>,
171 package_vers: String,
174 prerelease_version: String,
176 // Probed tools at runtime
177 lldb_version: Option<String>,
178 lldb_python_dir: Option<String>,
180 // Runtime state filled in later on
181 cc: HashMap<String, (gcc::Tool, Option<PathBuf>)>,
182 cxx: HashMap<String, gcc::Tool>,
183 crates: HashMap<String, Crate>,
198 /// The various "modes" of invoking Cargo.
200 /// These entries currently correspond to the various output directories of the
201 /// build system, with each mod generating output in a different directory.
202 #[derive(Clone, Copy)]
204 /// This cargo is going to build the standard library, placing output in the
205 /// "stageN-std" directory.
208 /// This cargo is going to build libtest, placing output in the
209 /// "stageN-test" directory.
212 /// This cargo is going to build librustc and compiler libraries, placing
213 /// output in the "stageN-rustc" directory.
216 /// This cargo is going to some build tool, placing output in the
217 /// "stageN-tools" directory.
222 /// Creates a new set of build configuration from the `flags` on the command
223 /// line and the filesystem `config`.
225 /// By default all build output will be placed in the current directory.
226 pub fn new(flags: Flags, config: Config) -> Build {
227 let cwd = t!(env::current_dir());
228 let src = flags.src.clone().or_else(|| {
229 env::var_os("SRC").map(|x| x.into())
230 }).unwrap_or(cwd.clone());
231 let out = cwd.join("build");
233 let stage0_root = out.join(&config.build).join("stage0/bin");
234 let rustc = match config.rustc {
235 Some(ref s) => PathBuf::from(s),
236 None => stage0_root.join(exe("rustc", &config.build)),
238 let cargo = match config.cargo {
239 Some(ref s) => PathBuf::from(s),
240 None => stage0_root.join(exe("cargo", &config.build)),
242 let local_rebuild = config.local_rebuild;
244 let is_sudo = match env::var_os("SUDO_USER") {
246 match env::var_os("USER") {
247 Some(user) => user != sudo_user,
262 release: String::new(),
263 unstable_features: false,
265 short_ver_hash: None,
267 version: String::new(),
268 local_rebuild: local_rebuild,
269 package_vers: String::new(),
272 crates: HashMap::new(),
274 lldb_python_dir: None,
276 release_num: String::new(),
277 prerelease_version: String::new(),
281 /// Executes the entire build, as configured by the flags and configuration.
282 pub fn build(&mut self) {
287 if let Subcommand::Clean = self.flags.cmd {
288 return clean::clean(self);
291 self.verbose("finding compilers");
293 self.verbose("running sanity check");
295 self.verbose("collecting channel variables");
296 channel::collect(self);
297 // If local-rust is the same major.minor as the current version, then force a local-rebuild
298 let local_version_verbose = output(
299 Command::new(&self.rustc).arg("--version").arg("--verbose"));
300 let local_release = local_version_verbose
301 .lines().filter(|x| x.starts_with("release:"))
302 .next().unwrap().trim_left_matches("release:").trim();
303 if local_release.split('.').take(2).eq(self.release.split('.').take(2)) {
304 self.verbose(&format!("auto-detected local-rebuild {}", local_release));
305 self.local_rebuild = true;
307 self.verbose("updating submodules");
308 self.update_submodules();
309 self.verbose("learning about cargo");
310 metadata::build(self);
315 /// Updates all git submodules that we have.
317 /// This will detect if any submodules are out of date an run the necessary
318 /// commands to sync them all with upstream.
319 fn update_submodules(&self) {
320 struct Submodule<'a> {
326 // The submodule may have staged/unstaged changes
328 // Or could be initialized but never updated
330 // The submodule, itself, has extra commits but those changes haven't been commited to
331 // the (outer) git repository
335 if !self.config.submodules {
338 if fs::metadata(self.src.join(".git")).is_err() {
342 let mut cmd = Command::new("git");
343 cmd.current_dir(&self.src);
346 let git_submodule = || {
347 let mut cmd = Command::new("git");
348 cmd.current_dir(&self.src).arg("submodule");
352 // FIXME: this takes a seriously long time to execute on Windows and a
353 // nontrivial amount of time on Unix, we should have a better way
354 // of detecting whether we need to run all the submodule commands
356 let out = output(git_submodule().arg("status"));
357 let mut submodules = vec![];
358 for line in out.lines() {
359 // NOTE `git submodule status` output looks like this:
361 // -5066b7dcab7e700844b0e2ba71b8af9dc627a59b src/liblibc
362 // +b37ef24aa82d2be3a3cc0fe89bf82292f4ca181c src/compiler-rt (remotes/origin/..)
363 // e058ca661692a8d01f8cf9d35939dfe3105ce968 src/jemalloc (3.6.0-533-ge058ca6)
365 // The first character can be '-', '+' or ' ' and denotes the `State` of the submodule
366 // Right next to this character is the SHA-1 of the submodule HEAD
367 // And after that comes the path to the submodule
368 let path = Path::new(line[1..].split(' ').skip(1).next().unwrap());
369 let state = if line.starts_with('-') {
370 State::NotInitialized
371 } else if line.starts_with('+') {
373 } else if line.starts_with(' ') {
376 panic!("unexpected git submodule state: {:?}", line.chars().next());
379 submodules.push(Submodule { path: path, state: state })
382 self.run(git_submodule().arg("sync"));
384 for submodule in submodules {
385 // If using llvm-root then don't touch the llvm submodule.
386 if submodule.path.components().any(|c| c == Component::Normal("llvm".as_ref())) &&
387 self.config.target_config.get(&self.config.build)
388 .and_then(|c| c.llvm_config.as_ref()).is_some()
393 if submodule.path.components().any(|c| c == Component::Normal("jemalloc".as_ref())) &&
394 !self.config.use_jemalloc
399 // `submodule.path` is the relative path to a submodule (from the repository root)
400 // `submodule_path` is the path to a submodule from the cwd
402 // use `submodule.path` when e.g. executing a submodule specific command from the
404 // use `submodule_path` when e.g. executing a normal git command for the submodule
405 // (set via `current_dir`)
406 let submodule_path = self.src.join(submodule.path);
408 match submodule.state {
409 State::MaybeDirty => {
410 // drop staged changes
411 self.run(git().current_dir(&submodule_path)
412 .args(&["reset", "--hard"]));
413 // drops unstaged changes
414 self.run(git().current_dir(&submodule_path)
415 .args(&["clean", "-fdx"]));
417 State::NotInitialized => {
418 self.run(git_submodule().arg("init").arg(submodule.path));
419 self.run(git_submodule().arg("update").arg(submodule.path));
421 State::OutOfSync => {
422 // drops submodule commits that weren't reported to the (outer) git repository
423 self.run(git_submodule().arg("update").arg(submodule.path));
424 self.run(git().current_dir(&submodule_path)
425 .args(&["reset", "--hard"]));
426 self.run(git().current_dir(&submodule_path)
427 .args(&["clean", "-fdx"]));
433 /// Clear out `dir` if `input` is newer.
435 /// After this executes, it will also ensure that `dir` exists.
436 fn clear_if_dirty(&self, dir: &Path, input: &Path) {
437 let stamp = dir.join(".stamp");
438 if mtime(&stamp) < mtime(input) {
439 self.verbose(&format!("Dirty - {}", dir.display()));
440 let _ = fs::remove_dir_all(dir);
441 } else if stamp.exists() {
444 t!(fs::create_dir_all(dir));
445 t!(File::create(stamp));
448 /// Prepares an invocation of `cargo` to be run.
450 /// This will create a `Command` that represents a pending execution of
451 /// Cargo. This cargo will be configured to use `compiler` as the actual
452 /// rustc compiler, its output will be scoped by `mode`'s output directory,
453 /// it will pass the `--target` flag for the specified `target`, and will be
454 /// executing the Cargo command `cmd`.
459 cmd: &str) -> Command {
460 let mut cargo = Command::new(&self.cargo);
461 let out_dir = self.stage_out(compiler, mode);
462 cargo.env("CARGO_TARGET_DIR", out_dir)
464 .arg("-j").arg(self.jobs().to_string())
465 .arg("--target").arg(target);
467 // FIXME: Temporary fix for https://github.com/rust-lang/cargo/issues/3005
468 // Force cargo to output binaries with disambiguating hashes in the name
469 cargo.env("__CARGO_DEFAULT_LIB_METADATA", "1");
472 if compiler.stage == 0 && self.local_rebuild {
473 // Assume the local-rebuild rustc already has stage1 features.
476 stage = compiler.stage;
479 // Customize the compiler we're running. Specify the compiler to cargo
480 // as our shim and then pass it some various options used to configure
481 // how the actual compiler itself is called.
483 // These variables are primarily all read by
484 // src/bootstrap/bin/{rustc.rs,rustdoc.rs}
485 cargo.env("RUSTC", self.out.join("bootstrap/debug/rustc"))
486 .env("RUSTC_REAL", self.compiler_path(compiler))
487 .env("RUSTC_STAGE", stage.to_string())
488 .env("RUSTC_DEBUGINFO", self.config.rust_debuginfo.to_string())
489 .env("RUSTC_DEBUGINFO_LINES", self.config.rust_debuginfo_lines.to_string())
490 .env("RUSTC_CODEGEN_UNITS",
491 self.config.rust_codegen_units.to_string())
492 .env("RUSTC_DEBUG_ASSERTIONS",
493 self.config.rust_debug_assertions.to_string())
494 .env("RUSTC_SNAPSHOT", &self.rustc)
495 .env("RUSTC_SYSROOT", self.sysroot(compiler))
496 .env("RUSTC_LIBDIR", self.rustc_libdir(compiler))
497 .env("RUSTC_SNAPSHOT_LIBDIR", self.rustc_snapshot_libdir())
498 .env("RUSTC_RPATH", self.config.rust_rpath.to_string())
499 .env("RUSTDOC", self.out.join("bootstrap/debug/rustdoc"))
500 .env("RUSTDOC_REAL", self.rustdoc(compiler))
501 .env("RUSTC_FLAGS", self.rustc_flags(target).join(" "));
503 // Enable usage of unstable features
504 cargo.env("RUSTC_BOOTSTRAP", "1");
505 self.add_rust_test_threads(&mut cargo);
507 // Ignore incremental modes except for stage0, since we're
508 // not guaranteeing correctness acros builds if the compiler
509 // is changing under your feet.`
510 if self.flags.incremental && compiler.stage == 0 {
511 let incr_dir = self.incremental_dir(compiler);
512 cargo.env("RUSTC_INCREMENTAL", incr_dir);
515 let verbose = cmp::max(self.config.verbose, self.flags.verbose);
516 cargo.env("RUSTC_VERBOSE", format!("{}", verbose));
518 // Specify some various options for build scripts used throughout
521 // FIXME: the guard against msvc shouldn't need to be here
522 if !target.contains("msvc") {
523 cargo.env(format!("CC_{}", target), self.cc(target))
524 .env(format!("AR_{}", target), self.ar(target).unwrap()) // only msvc is None
525 .env(format!("CFLAGS_{}", target), self.cflags(target).join(" "));
528 if self.config.channel == "nightly" && compiler.is_final_stage(self) {
529 cargo.env("RUSTC_SAVE_ANALYSIS", "api".to_string());
532 // Environment variables *required* needed throughout the build
534 // FIXME: should update code to not require this env var
535 cargo.env("CFG_COMPILER_HOST_TRIPLE", target);
537 if self.config.verbose() || self.flags.verbose() {
540 // FIXME: cargo bench does not accept `--release`
541 if self.config.rust_optimize && cmd != "bench" {
542 cargo.arg("--release");
544 if self.config.vendor || self.is_sudo {
545 cargo.arg("--frozen");
550 /// Get a path to the compiler specified.
551 fn compiler_path(&self, compiler: &Compiler) -> PathBuf {
552 if compiler.is_snapshot(self) {
555 self.sysroot(compiler).join("bin").join(exe("rustc", compiler.host))
559 /// Get the specified tool built by the specified compiler
560 fn tool(&self, compiler: &Compiler, tool: &str) -> PathBuf {
561 self.cargo_out(compiler, Mode::Tool, compiler.host)
562 .join(exe(tool, compiler.host))
565 /// Get the `rustdoc` executable next to the specified compiler
566 fn rustdoc(&self, compiler: &Compiler) -> PathBuf {
567 let mut rustdoc = self.compiler_path(compiler);
569 rustdoc.push(exe("rustdoc", compiler.host));
573 /// Get a `Command` which is ready to run `tool` in `stage` built for
575 fn tool_cmd(&self, compiler: &Compiler, tool: &str) -> Command {
576 let mut cmd = Command::new(self.tool(&compiler, tool));
577 self.prepare_tool_cmd(compiler, &mut cmd);
581 /// Prepares the `cmd` provided to be able to run the `compiler` provided.
583 /// Notably this munges the dynamic library lookup path to point to the
584 /// right location to run `compiler`.
585 fn prepare_tool_cmd(&self, compiler: &Compiler, cmd: &mut Command) {
586 let host = compiler.host;
587 let mut paths = vec![
588 self.sysroot_libdir(compiler, compiler.host),
589 self.cargo_out(compiler, Mode::Tool, host).join("deps"),
592 // On MSVC a tool may invoke a C compiler (e.g. compiletest in run-make
593 // mode) and that C compiler may need some extra PATH modification. Do
595 if compiler.host.contains("msvc") {
596 let curpaths = env::var_os("PATH").unwrap_or(OsString::new());
597 let curpaths = env::split_paths(&curpaths).collect::<Vec<_>>();
598 for &(ref k, ref v) in self.cc[compiler.host].0.env() {
602 for path in env::split_paths(v) {
603 if !curpaths.contains(&path) {
609 add_lib_path(paths, cmd);
612 /// Get the space-separated set of activated features for the standard
614 fn std_features(&self) -> String {
615 let mut features = "panic-unwind".to_string();
616 if self.config.debug_jemalloc {
617 features.push_str(" debug-jemalloc");
619 if self.config.use_jemalloc {
620 features.push_str(" jemalloc");
622 if self.config.backtrace {
623 features.push_str(" backtrace");
628 /// Get the space-separated set of activated features for the compiler.
629 fn rustc_features(&self) -> String {
630 let mut features = String::new();
631 if self.config.use_jemalloc {
632 features.push_str(" jemalloc");
637 /// Component directory that Cargo will produce output into (e.g.
639 fn cargo_dir(&self) -> &'static str {
640 if self.config.rust_optimize {"release"} else {"debug"}
643 /// Returns the sysroot for the `compiler` specified that *this build system
646 /// That is, the sysroot for the stage0 compiler is not what the compiler
647 /// thinks it is by default, but it's the same as the default for stages
649 fn sysroot(&self, compiler: &Compiler) -> PathBuf {
650 if compiler.stage == 0 {
651 self.out.join(compiler.host).join("stage0-sysroot")
653 self.out.join(compiler.host).join(format!("stage{}", compiler.stage))
657 /// Get the directory for incremental by-products when using the
659 fn incremental_dir(&self, compiler: &Compiler) -> PathBuf {
660 self.out.join(compiler.host).join(format!("stage{}-incremental", compiler.stage))
663 /// Returns the libdir where the standard library and other artifacts are
664 /// found for a compiler's sysroot.
665 fn sysroot_libdir(&self, compiler: &Compiler, target: &str) -> PathBuf {
666 self.sysroot(compiler).join("lib").join("rustlib")
667 .join(target).join("lib")
670 /// Returns the root directory for all output generated in a particular
671 /// stage when running with a particular host compiler.
673 /// The mode indicates what the root directory is for.
674 fn stage_out(&self, compiler: &Compiler, mode: Mode) -> PathBuf {
675 let suffix = match mode {
676 Mode::Libstd => "-std",
677 Mode::Libtest => "-test",
678 Mode::Tool => "-tools",
679 Mode::Librustc => "-rustc",
681 self.out.join(compiler.host)
682 .join(format!("stage{}{}", compiler.stage, suffix))
685 /// Returns the root output directory for all Cargo output in a given stage,
686 /// running a particular comipler, wehther or not we're building the
687 /// standard library, and targeting the specified architecture.
691 target: &str) -> PathBuf {
692 self.stage_out(compiler, mode).join(target).join(self.cargo_dir())
695 /// Root output directory for LLVM compiled for `target`
697 /// Note that if LLVM is configured externally then the directory returned
698 /// will likely be empty.
699 fn llvm_out(&self, target: &str) -> PathBuf {
700 self.out.join(target).join("llvm")
703 /// Output directory for all documentation for a target
704 fn doc_out(&self, target: &str) -> PathBuf {
705 self.out.join(target).join("doc")
708 /// Returns true if no custom `llvm-config` is set for the specified target.
710 /// If no custom `llvm-config` was specified then Rust's llvm will be used.
711 fn is_rust_llvm(&self, target: &str) -> bool {
712 match self.config.target_config.get(target) {
713 Some(ref c) => c.llvm_config.is_none(),
718 /// Returns the path to `llvm-config` for the specified target.
720 /// If a custom `llvm-config` was specified for target then that's returned
722 fn llvm_config(&self, target: &str) -> PathBuf {
723 let target_config = self.config.target_config.get(target);
724 if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
727 self.llvm_out(&self.config.build).join("bin")
728 .join(exe("llvm-config", target))
732 /// Returns the path to `FileCheck` binary for the specified target
733 fn llvm_filecheck(&self, target: &str) -> PathBuf {
734 let target_config = self.config.target_config.get(target);
735 if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
736 let llvm_bindir = output(Command::new(s).arg("--bindir"));
737 Path::new(llvm_bindir.trim()).join(exe("FileCheck", target))
739 let base = self.llvm_out(&self.config.build).join("build");
740 let exe = exe("FileCheck", target);
741 if self.config.build.contains("msvc") {
742 base.join("Release/bin").join(exe)
744 base.join("bin").join(exe)
749 /// Root output directory for rust_test_helpers library compiled for
751 fn test_helpers_out(&self, target: &str) -> PathBuf {
752 self.out.join(target).join("rust-test-helpers")
755 /// Adds the compiler's directory of dynamic libraries to `cmd`'s dynamic
756 /// library lookup path.
757 fn add_rustc_lib_path(&self, compiler: &Compiler, cmd: &mut Command) {
758 // Windows doesn't need dylib path munging because the dlls for the
759 // compiler live next to the compiler and the system will find them
765 add_lib_path(vec![self.rustc_libdir(compiler)], cmd);
768 /// Adds the `RUST_TEST_THREADS` env var if necessary
769 fn add_rust_test_threads(&self, cmd: &mut Command) {
770 if env::var_os("RUST_TEST_THREADS").is_none() {
771 cmd.env("RUST_TEST_THREADS", self.jobs().to_string());
775 /// Returns the compiler's libdir where it stores the dynamic libraries that
776 /// it itself links against.
778 /// For example this returns `<sysroot>/lib` on Unix and `<sysroot>/bin` on
780 fn rustc_libdir(&self, compiler: &Compiler) -> PathBuf {
781 if compiler.is_snapshot(self) {
782 self.rustc_snapshot_libdir()
784 self.sysroot(compiler).join(libdir(compiler.host))
788 /// Returns the libdir of the snapshot compiler.
789 fn rustc_snapshot_libdir(&self) -> PathBuf {
790 self.rustc.parent().unwrap().parent().unwrap()
791 .join(libdir(&self.config.build))
794 /// Runs a command, printing out nice contextual information if it fails.
795 fn run(&self, cmd: &mut Command) {
796 self.verbose(&format!("running: {:?}", cmd));
800 /// Prints a message if this build is configured in verbose mode.
801 fn verbose(&self, msg: &str) {
802 if self.flags.verbose() || self.config.verbose() {
807 /// Returns the number of parallel jobs that have been configured for this
809 fn jobs(&self) -> u32 {
810 self.flags.jobs.unwrap_or(num_cpus::get() as u32)
813 /// Returns the path to the C compiler for the target specified.
814 fn cc(&self, target: &str) -> &Path {
815 self.cc[target].0.path()
818 /// Returns a list of flags to pass to the C compiler for the target
820 fn cflags(&self, target: &str) -> Vec<String> {
821 // Filter out -O and /O (the optimization flags) that we picked up from
822 // gcc-rs because the build scripts will determine that for themselves.
823 let mut base = self.cc[target].0.args().iter()
824 .map(|s| s.to_string_lossy().into_owned())
825 .filter(|s| !s.starts_with("-O") && !s.starts_with("/O"))
826 .collect::<Vec<_>>();
828 // If we're compiling on OSX then we add a few unconditional flags
829 // indicating that we want libc++ (more filled out than libstdc++) and
830 // we want to compile for 10.7. This way we can ensure that
831 // LLVM/jemalloc/etc are all properly compiled.
832 if target.contains("apple-darwin") {
833 base.push("-stdlib=libc++".into());
835 // This is a hack, because newer binutils broke things on some vms/distros
836 // (i.e., linking against unknown relocs disabled by the following flag)
837 // See: https://github.com/rust-lang/rust/issues/34978
839 "i586-unknown-linux-gnu" |
840 "i686-unknown-linux-musl" |
841 "x86_64-unknown-linux-musl" => {
842 base.push("-Wa,-mrelax-relocations=no".into());
849 /// Returns the path to the `ar` archive utility for the target specified.
850 fn ar(&self, target: &str) -> Option<&Path> {
851 self.cc[target].1.as_ref().map(|p| &**p)
854 /// Returns the path to the C++ compiler for the target specified, may panic
855 /// if no C++ compiler was configured for the target.
856 fn cxx(&self, target: &str) -> &Path {
857 match self.cxx.get(target) {
859 None => panic!("\n\ntarget `{}` is not configured as a host,
860 only as a target\n\n", target),
864 /// Returns flags to pass to the compiler to generate code for `target`.
865 fn rustc_flags(&self, target: &str) -> Vec<String> {
866 // New flags should be added here with great caution!
868 // It's quite unfortunate to **require** flags to generate code for a
869 // target, so it should only be passed here if absolutely necessary!
870 // Most default configuration should be done through target specs rather
871 // than an entry here.
873 let mut base = Vec::new();
874 if target != self.config.build && !target.contains("msvc") &&
875 !target.contains("emscripten") {
876 base.push(format!("-Clinker={}", self.cc(target).display()));
881 /// Returns the "musl root" for this `target`, if defined
882 fn musl_root(&self, target: &str) -> Option<&Path> {
883 self.config.target_config.get(target)
884 .and_then(|t| t.musl_root.as_ref())
885 .or(self.config.musl_root.as_ref())
889 /// Path to the python interpreter to use
890 fn python(&self) -> &Path {
891 self.config.python.as_ref().unwrap()
894 /// Tests whether the `compiler` compiling for `target` should be forced to
895 /// use a stage1 compiler instead.
897 /// Currently, by default, the build system does not perform a "full
898 /// bootstrap" by default where we compile the compiler three times.
899 /// Instead, we compile the compiler two times. The final stage (stage2)
900 /// just copies the libraries from the previous stage, which is what this
903 /// Here we return `true` if:
905 /// * The build isn't performing a full bootstrap
906 /// * The `compiler` is in the final stage, 2
907 /// * We're not cross-compiling, so the artifacts are already available in
910 /// When all of these conditions are met the build will lift artifacts from
911 /// the previous stage forward.
912 fn force_use_stage1(&self, compiler: &Compiler, target: &str) -> bool {
913 !self.config.full_bootstrap &&
914 compiler.stage >= 2 &&
915 self.config.host.iter().any(|h| h == target)
919 impl<'a> Compiler<'a> {
920 /// Creates a new complier for the specified stage/host
921 fn new(stage: u32, host: &'a str) -> Compiler<'a> {
922 Compiler { stage: stage, host: host }
925 /// Returns whether this is a snapshot compiler for `build`'s configuration
926 fn is_snapshot(&self, build: &Build) -> bool {
927 self.stage == 0 && self.host == build.config.build
930 /// Returns if this compiler should be treated as a final stage one in the
931 /// current build session.
932 /// This takes into account whether we're performing a full bootstrap or
933 /// not; don't directly compare the stage with `2`!
934 fn is_final_stage(&self, build: &Build) -> bool {
935 let final_stage = if build.config.full_bootstrap { 2 } else { 1 };
936 self.stage >= final_stage