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 //! Serialized configuration of a build.
13 //! This module implements parsing `config.toml` configuration files to tweak
14 //! how the build runs.
16 use std::collections::{HashMap, HashSet};
19 use std::io::prelude::*;
20 use std::path::{Path, PathBuf};
27 use cache::{INTERNER, Interned};
29 pub use flags::Subcommand;
31 /// Global configuration for the entire build and/or bootstrap.
33 /// This structure is derived from a combination of both `config.toml` and
34 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
35 /// is used all that much, so this is primarily filled out by `config.mk` which
36 /// is generated from `./configure`.
38 /// Note that this structure is not decoded directly into, but rather it is
39 /// filled out from the decoded forms of the structs below. For documentation
40 /// each field, see the corresponding fields in
41 /// `config.toml.example`.
44 pub ccache: Option<String>,
48 pub compiler_docs: bool,
50 pub locked_deps: bool,
52 pub target_config: HashMap<Interned<String>, Target>,
53 pub full_bootstrap: bool,
55 pub tools: Option<HashSet<String>>,
59 pub exclude: Vec<PathBuf>,
60 pub rustc_error_format: Option<String>,
62 pub run_host_only: bool,
64 pub on_fail: Option<String>,
65 pub stage: Option<u32>,
66 pub keep_stage: Option<u32>,
68 pub jobs: Option<u32>,
70 pub incremental: bool,
72 // llvm codegen options
73 pub llvm_enabled: bool,
74 pub llvm_assertions: bool,
75 pub llvm_optimize: bool,
76 pub llvm_release_debuginfo: bool,
77 pub llvm_version_check: bool,
78 pub llvm_static_stdcpp: bool,
79 pub llvm_link_shared: bool,
80 pub llvm_targets: Option<String>,
81 pub llvm_experimental_targets: String,
82 pub llvm_link_jobs: Option<u32>,
84 pub lld_enabled: bool,
86 // rust codegen options
87 pub rust_optimize: bool,
88 pub rust_codegen_units: Option<u32>,
89 pub rust_thinlto: bool,
90 pub rust_debug_assertions: bool,
91 pub rust_debuginfo: bool,
92 pub rust_debuginfo_lines: bool,
93 pub rust_debuginfo_only_std: bool,
95 pub rustc_parallel_queries: bool,
96 pub rustc_default_linker: Option<String>,
97 pub rust_optimize_tests: bool,
98 pub rust_debuginfo_tests: bool,
99 pub rust_dist_src: bool,
100 pub rust_codegen_backends: Vec<Interned<String>>,
101 pub rust_codegen_backends_dir: String,
103 pub build: Interned<String>,
104 pub hosts: Vec<Interned<String>>,
105 pub targets: Vec<Interned<String>>,
106 pub local_rebuild: bool,
109 pub dist_sign_folder: Option<PathBuf>,
110 pub dist_upload_addr: Option<String>,
111 pub dist_gpg_password_file: Option<PathBuf>,
114 pub debug_jemalloc: bool,
115 pub use_jemalloc: bool,
116 pub backtrace: bool, // support for RUST_BACKTRACE
117 pub wasm_syscall: bool,
120 pub low_priority: bool,
122 pub quiet_tests: bool,
124 pub save_toolstates: Option<PathBuf>,
126 // Fallback musl-root for all targets
127 pub musl_root: Option<PathBuf>,
128 pub prefix: Option<PathBuf>,
129 pub sysconfdir: Option<PathBuf>,
130 pub docdir: Option<PathBuf>,
131 pub bindir: Option<PathBuf>,
132 pub libdir: Option<PathBuf>,
133 pub mandir: Option<PathBuf>,
134 pub codegen_tests: bool,
135 pub nodejs: Option<PathBuf>,
136 pub gdb: Option<PathBuf>,
137 pub python: Option<PathBuf>,
138 pub openssl_static: bool,
139 pub configure_args: Vec<String>,
141 // These are either the stage0 downloaded binaries or the locally installed ones.
142 pub initial_cargo: PathBuf,
143 pub initial_rustc: PathBuf,
146 /// Per-target configuration stored in the global configuration structure.
149 /// Some(path to llvm-config) if using an external LLVM.
150 pub llvm_config: Option<PathBuf>,
151 pub jemalloc: Option<PathBuf>,
152 pub cc: Option<PathBuf>,
153 pub cxx: Option<PathBuf>,
154 pub ar: Option<PathBuf>,
155 pub linker: Option<PathBuf>,
156 pub ndk: Option<PathBuf>,
157 pub crt_static: Option<bool>,
158 pub musl_root: Option<PathBuf>,
159 pub qemu_rootfs: Option<PathBuf>,
162 /// Structure of the `config.toml` file that configuration is read from.
164 /// This structure uses `Decodable` to automatically decode a TOML configuration
165 /// file into this format, and then this is traversed and written into the above
166 /// `Config` structure.
167 #[derive(Deserialize, Default)]
168 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
170 build: Option<Build>,
171 install: Option<Install>,
174 target: Option<HashMap<String, TomlTarget>>,
178 /// TOML representation of various global build decisions.
179 #[derive(Deserialize, Default, Clone)]
180 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
182 build: Option<String>,
187 cargo: Option<String>,
188 rustc: Option<String>,
189 low_priority: Option<bool>,
190 compiler_docs: Option<bool>,
192 submodules: Option<bool>,
194 locked_deps: Option<bool>,
195 vendor: Option<bool>,
196 nodejs: Option<String>,
197 python: Option<String>,
198 full_bootstrap: Option<bool>,
199 extended: Option<bool>,
200 tools: Option<HashSet<String>>,
201 verbose: Option<usize>,
202 sanitizers: Option<bool>,
203 profiler: Option<bool>,
204 openssl_static: Option<bool>,
205 configure_args: Option<Vec<String>>,
206 local_rebuild: Option<bool>,
209 /// TOML representation of various global install decisions.
210 #[derive(Deserialize, Default, Clone)]
211 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
213 prefix: Option<String>,
214 sysconfdir: Option<String>,
215 docdir: Option<String>,
216 bindir: Option<String>,
217 libdir: Option<String>,
218 mandir: Option<String>,
220 // standard paths, currently unused
221 datadir: Option<String>,
222 infodir: Option<String>,
223 localstatedir: Option<String>,
226 /// TOML representation of how the LLVM build is configured.
227 #[derive(Deserialize, Default)]
228 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
230 enabled: Option<bool>,
231 ccache: Option<StringOrBool>,
233 assertions: Option<bool>,
234 optimize: Option<bool>,
235 release_debuginfo: Option<bool>,
236 version_check: Option<bool>,
237 static_libstdcpp: Option<bool>,
238 targets: Option<String>,
239 experimental_targets: Option<String>,
240 link_jobs: Option<u32>,
241 link_shared: Option<bool>,
244 #[derive(Deserialize, Default, Clone)]
245 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
247 sign_folder: Option<String>,
248 gpg_password_file: Option<String>,
249 upload_addr: Option<String>,
250 src_tarball: Option<bool>,
253 #[derive(Deserialize)]
260 impl Default for StringOrBool {
261 fn default() -> StringOrBool {
262 StringOrBool::Bool(false)
266 /// TOML representation of how the Rust build is configured.
267 #[derive(Deserialize, Default)]
268 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
270 optimize: Option<bool>,
271 codegen_units: Option<u32>,
272 thinlto: Option<bool>,
273 debug_assertions: Option<bool>,
274 debuginfo: Option<bool>,
275 debuginfo_lines: Option<bool>,
276 debuginfo_only_std: Option<bool>,
277 experimental_parallel_queries: Option<bool>,
278 debug_jemalloc: Option<bool>,
279 use_jemalloc: Option<bool>,
280 backtrace: Option<bool>,
281 default_linker: Option<String>,
282 channel: Option<String>,
283 musl_root: Option<String>,
285 optimize_tests: Option<bool>,
286 debuginfo_tests: Option<bool>,
287 codegen_tests: Option<bool>,
288 ignore_git: Option<bool>,
290 dist_src: Option<bool>,
291 quiet_tests: Option<bool>,
292 test_miri: Option<bool>,
293 save_toolstates: Option<String>,
294 codegen_backends: Option<Vec<String>>,
295 codegen_backends_dir: Option<String>,
296 wasm_syscall: Option<bool>,
300 /// TOML representation of how each build target is configured.
301 #[derive(Deserialize, Default)]
302 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
304 llvm_config: Option<String>,
305 jemalloc: Option<String>,
309 linker: Option<String>,
310 android_ndk: Option<String>,
311 crt_static: Option<bool>,
312 musl_root: Option<String>,
313 qemu_rootfs: Option<String>,
317 pub fn parse(args: &[String]) -> Config {
318 let flags = Flags::parse(&args);
319 let file = flags.config.clone();
320 let mut config = Config::default();
321 config.exclude = flags.exclude;
322 config.llvm_enabled = true;
323 config.llvm_optimize = true;
324 config.llvm_version_check = true;
325 config.use_jemalloc = true;
326 config.backtrace = true;
327 config.rust_optimize = true;
328 config.rust_optimize_tests = true;
329 config.submodules = true;
331 config.rust_rpath = true;
332 config.channel = "dev".to_string();
333 config.codegen_tests = true;
334 config.ignore_git = false;
335 config.rust_dist_src = true;
336 config.test_miri = false;
337 config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
338 config.rust_codegen_backends_dir = "codegen-backends".to_owned();
340 config.rustc_error_format = flags.rustc_error_format;
341 config.on_fail = flags.on_fail;
342 config.stage = flags.stage;
343 config.src = flags.src;
344 config.jobs = flags.jobs;
345 config.cmd = flags.cmd;
346 config.incremental = flags.incremental;
347 config.keep_stage = flags.keep_stage;
349 // If --target was specified but --host wasn't specified, don't run any host-only tests.
350 config.run_host_only = flags.host.is_empty() && !flags.target.is_empty();
352 let toml = file.map(|file| {
353 let mut f = t!(File::open(&file));
354 let mut contents = String::new();
355 t!(f.read_to_string(&mut contents));
356 match toml::from_str(&contents) {
359 println!("failed to parse TOML configuration '{}': {}",
360 file.display(), err);
364 }).unwrap_or_else(|| TomlConfig::default());
366 let build = toml.build.clone().unwrap_or(Build::default());
367 set(&mut config.build, build.build.clone().map(|x| INTERNER.intern_string(x)));
368 set(&mut config.build, flags.build);
369 if config.build.is_empty() {
370 // set by bootstrap.py
371 config.build = INTERNER.intern_str(&env::var("BUILD").unwrap());
373 config.hosts.push(config.build.clone());
374 for host in build.host.iter() {
375 let host = INTERNER.intern_str(host);
376 if !config.hosts.contains(&host) {
377 config.hosts.push(host);
380 for target in config.hosts.iter().cloned()
381 .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
383 if !config.targets.contains(&target) {
384 config.targets.push(target);
387 config.hosts = if !flags.host.is_empty() {
392 config.targets = if !flags.target.is_empty() {
399 config.nodejs = build.nodejs.map(PathBuf::from);
400 config.gdb = build.gdb.map(PathBuf::from);
401 config.python = build.python.map(PathBuf::from);
402 set(&mut config.low_priority, build.low_priority);
403 set(&mut config.compiler_docs, build.compiler_docs);
404 set(&mut config.docs, build.docs);
405 set(&mut config.submodules, build.submodules);
406 set(&mut config.locked_deps, build.locked_deps);
407 set(&mut config.vendor, build.vendor);
408 set(&mut config.full_bootstrap, build.full_bootstrap);
409 set(&mut config.extended, build.extended);
410 config.tools = build.tools;
411 set(&mut config.verbose, build.verbose);
412 set(&mut config.sanitizers, build.sanitizers);
413 set(&mut config.profiler, build.profiler);
414 set(&mut config.openssl_static, build.openssl_static);
415 set(&mut config.configure_args, build.configure_args);
416 set(&mut config.local_rebuild, build.local_rebuild);
417 config.verbose = cmp::max(config.verbose, flags.verbose);
419 if let Some(ref install) = toml.install {
420 config.prefix = install.prefix.clone().map(PathBuf::from);
421 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
422 config.docdir = install.docdir.clone().map(PathBuf::from);
423 config.bindir = install.bindir.clone().map(PathBuf::from);
424 config.libdir = install.libdir.clone().map(PathBuf::from);
425 config.mandir = install.mandir.clone().map(PathBuf::from);
428 // Store off these values as options because if they're not provided
429 // we'll infer default values for them later
430 let mut thinlto = None;
431 let mut llvm_assertions = None;
432 let mut debuginfo_lines = None;
433 let mut debuginfo_only_std = None;
434 let mut debug = None;
435 let mut debug_jemalloc = None;
436 let mut debuginfo = None;
437 let mut debug_assertions = None;
438 let mut optimize = None;
439 let mut ignore_git = None;
441 if let Some(ref llvm) = toml.llvm {
443 Some(StringOrBool::String(ref s)) => {
444 config.ccache = Some(s.to_string())
446 Some(StringOrBool::Bool(true)) => {
447 config.ccache = Some("ccache".to_string());
449 Some(StringOrBool::Bool(false)) | None => {}
451 set(&mut config.ninja, llvm.ninja);
452 set(&mut config.llvm_enabled, llvm.enabled);
453 llvm_assertions = llvm.assertions;
454 set(&mut config.llvm_optimize, llvm.optimize);
455 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
456 set(&mut config.llvm_version_check, llvm.version_check);
457 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
458 set(&mut config.llvm_link_shared, llvm.link_shared);
459 config.llvm_targets = llvm.targets.clone();
460 config.llvm_experimental_targets = llvm.experimental_targets.clone()
461 .unwrap_or("WebAssembly".to_string());
462 config.llvm_link_jobs = llvm.link_jobs;
465 if let Some(ref rust) = toml.rust {
467 debug_assertions = rust.debug_assertions;
468 debuginfo = rust.debuginfo;
469 debuginfo_lines = rust.debuginfo_lines;
470 debuginfo_only_std = rust.debuginfo_only_std;
471 optimize = rust.optimize;
472 ignore_git = rust.ignore_git;
473 debug_jemalloc = rust.debug_jemalloc;
474 thinlto = rust.thinlto;
475 set(&mut config.rust_optimize_tests, rust.optimize_tests);
476 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
477 set(&mut config.codegen_tests, rust.codegen_tests);
478 set(&mut config.rust_rpath, rust.rpath);
479 set(&mut config.use_jemalloc, rust.use_jemalloc);
480 set(&mut config.backtrace, rust.backtrace);
481 set(&mut config.channel, rust.channel.clone());
482 set(&mut config.rust_dist_src, rust.dist_src);
483 set(&mut config.quiet_tests, rust.quiet_tests);
484 set(&mut config.test_miri, rust.test_miri);
485 set(&mut config.wasm_syscall, rust.wasm_syscall);
486 set(&mut config.lld_enabled, rust.lld);
487 config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
488 config.rustc_default_linker = rust.default_linker.clone();
489 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
490 config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
492 if let Some(ref backends) = rust.codegen_backends {
493 config.rust_codegen_backends = backends.iter()
494 .map(|s| INTERNER.intern_str(s))
498 set(&mut config.rust_codegen_backends_dir, rust.codegen_backends_dir.clone());
500 match rust.codegen_units {
501 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
502 Some(n) => config.rust_codegen_units = Some(n),
507 if let Some(ref t) = toml.target {
508 for (triple, cfg) in t {
509 let mut target = Target::default();
511 if let Some(ref s) = cfg.llvm_config {
512 target.llvm_config = Some(env::current_dir().unwrap().join(s));
514 if let Some(ref s) = cfg.jemalloc {
515 target.jemalloc = Some(env::current_dir().unwrap().join(s));
517 if let Some(ref s) = cfg.android_ndk {
518 target.ndk = Some(env::current_dir().unwrap().join(s));
520 target.cc = cfg.cc.clone().map(PathBuf::from);
521 target.cxx = cfg.cxx.clone().map(PathBuf::from);
522 target.ar = cfg.ar.clone().map(PathBuf::from);
523 target.linker = cfg.linker.clone().map(PathBuf::from);
524 target.crt_static = cfg.crt_static.clone();
525 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
526 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
528 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
532 if let Some(ref t) = toml.dist {
533 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
534 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
535 config.dist_upload_addr = t.upload_addr.clone();
536 set(&mut config.rust_dist_src, t.src_tarball);
539 let cwd = t!(env::current_dir());
540 let out = cwd.join("build");
542 let stage0_root = out.join(&config.build).join("stage0/bin");
543 config.initial_rustc = match build.rustc {
544 Some(s) => PathBuf::from(s),
545 None => stage0_root.join(exe("rustc", &config.build)),
547 config.initial_cargo = match build.cargo {
548 Some(s) => PathBuf::from(s),
549 None => stage0_root.join(exe("cargo", &config.build)),
552 // Now that we've reached the end of our configuration, infer the
553 // default values for all options that we haven't otherwise stored yet.
556 config.llvm_assertions = llvm_assertions.unwrap_or(default);
558 let default = match &config.channel[..] {
559 "stable" | "beta" | "nightly" => true,
562 config.rust_thinlto = thinlto.unwrap_or(true);
563 config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
564 config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
566 let default = debug == Some(true);
567 config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
568 config.rust_debuginfo = debuginfo.unwrap_or(default);
569 config.rust_debug_assertions = debug_assertions.unwrap_or(default);
570 config.rust_optimize = optimize.unwrap_or(!default);
572 let default = config.channel == "dev";
573 config.ignore_git = ignore_git.unwrap_or(default);
578 /// Try to find the relative path of `libdir`.
579 pub fn libdir_relative(&self) -> Option<&Path> {
580 let libdir = self.libdir.as_ref()?;
581 if libdir.is_relative() {
584 // Try to make it relative to the prefix.
585 libdir.strip_prefix(self.prefix.as_ref()?).ok()
589 pub fn verbose(&self) -> bool {
593 pub fn very_verbose(&self) -> bool {
598 fn set<T>(field: &mut T, val: Option<T>) {
599 if let Some(v) = val {