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.mk` and `config.toml` configuration
14 //! files to tweak how the build runs.
16 use std::collections::HashMap;
19 use std::io::prelude::*;
20 use std::path::PathBuf;
24 use rustc_serialize::Decodable;
25 use toml::{Parser, Decoder, Value};
26 use util::{exe, push_exe_path};
28 /// Global configuration for the entire build and/or bootstrap.
30 /// This structure is derived from a combination of both `config.toml` and
31 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
32 /// is used all that much, so this is primarily filled out by `config.mk` which
33 /// is generated from `./configure`.
35 /// Note that this structure is not decoded directly into, but rather it is
36 /// filled out from the decoded forms of the structs below. For documentation
37 /// each field, see the corresponding fields in
38 /// `src/bootstrap/config.toml.example`.
41 pub ccache: Option<String>,
45 pub compiler_docs: bool,
47 pub locked_deps: bool,
49 pub target_config: HashMap<String, Target>,
50 pub full_bootstrap: bool,
54 // llvm codegen options
55 pub llvm_assertions: bool,
56 pub llvm_optimize: bool,
57 pub llvm_release_debuginfo: bool,
58 pub llvm_version_check: bool,
59 pub llvm_static_stdcpp: bool,
60 pub llvm_link_shared: bool,
61 pub llvm_targets: Option<String>,
62 pub llvm_link_jobs: Option<u32>,
63 pub llvm_clean_rebuild: bool,
65 // rust codegen options
66 pub rust_optimize: bool,
67 pub rust_codegen_units: u32,
68 pub rust_debug_assertions: bool,
69 pub rust_debuginfo: bool,
70 pub rust_debuginfo_lines: bool,
71 pub rust_debuginfo_only_std: bool,
73 pub rustc_default_linker: Option<String>,
74 pub rustc_default_ar: Option<String>,
75 pub rust_optimize_tests: bool,
76 pub rust_debuginfo_tests: bool,
77 pub rust_dist_src: bool,
80 pub host: Vec<String>,
81 pub target: Vec<String>,
82 pub rustc: Option<PathBuf>,
83 pub cargo: Option<PathBuf>,
84 pub local_rebuild: bool,
87 pub dist_sign_folder: Option<PathBuf>,
88 pub dist_upload_addr: Option<String>,
89 pub dist_gpg_password_file: Option<PathBuf>,
92 pub debug_jemalloc: bool,
93 pub use_jemalloc: bool,
94 pub backtrace: bool, // support for RUST_BACKTRACE
98 pub quiet_tests: bool,
99 // Fallback musl-root for all targets
100 pub musl_root: Option<PathBuf>,
101 pub prefix: Option<PathBuf>,
102 pub sysconfdir: Option<PathBuf>,
103 pub docdir: Option<PathBuf>,
104 pub bindir: Option<PathBuf>,
105 pub libdir: Option<PathBuf>,
106 pub libdir_relative: Option<PathBuf>,
107 pub mandir: Option<PathBuf>,
108 pub codegen_tests: bool,
109 pub nodejs: Option<PathBuf>,
110 pub gdb: Option<PathBuf>,
111 pub python: Option<PathBuf>,
112 pub configure_args: Vec<String>,
113 pub openssl_static: bool,
116 /// Per-target configuration stored in the global configuration structure.
119 pub llvm_config: Option<PathBuf>,
120 pub jemalloc: Option<PathBuf>,
121 pub cc: Option<PathBuf>,
122 pub cxx: Option<PathBuf>,
123 pub ndk: Option<PathBuf>,
124 pub musl_root: Option<PathBuf>,
125 pub qemu_rootfs: Option<PathBuf>,
128 /// Structure of the `config.toml` file that configuration is read from.
130 /// This structure uses `Decodable` to automatically decode a TOML configuration
131 /// file into this format, and then this is traversed and written into the above
132 /// `Config` structure.
133 #[derive(RustcDecodable, Default)]
135 build: Option<Build>,
136 install: Option<Install>,
139 target: Option<HashMap<String, TomlTarget>>,
143 /// TOML representation of various global build decisions.
144 #[derive(RustcDecodable, Default, Clone)]
146 build: Option<String>,
149 cargo: Option<String>,
150 rustc: Option<String>,
151 compiler_docs: Option<bool>,
153 submodules: Option<bool>,
155 locked_deps: Option<bool>,
156 vendor: Option<bool>,
157 nodejs: Option<String>,
158 python: Option<String>,
159 full_bootstrap: Option<bool>,
160 extended: Option<bool>,
161 verbose: Option<usize>,
162 sanitizers: Option<bool>,
163 openssl_static: Option<bool>,
166 /// TOML representation of various global install decisions.
167 #[derive(RustcDecodable, Default, Clone)]
169 prefix: Option<String>,
170 sysconfdir: Option<String>,
171 docdir: Option<String>,
172 bindir: Option<String>,
173 libdir: Option<String>,
174 mandir: Option<String>,
177 /// TOML representation of how the LLVM build is configured.
178 #[derive(RustcDecodable, Default)]
180 ccache: Option<StringOrBool>,
182 assertions: Option<bool>,
183 optimize: Option<bool>,
184 release_debuginfo: Option<bool>,
185 version_check: Option<bool>,
186 static_libstdcpp: Option<bool>,
187 targets: Option<String>,
188 link_jobs: Option<u32>,
189 clean_rebuild: Option<bool>,
192 #[derive(RustcDecodable, Default, Clone)]
194 sign_folder: Option<String>,
195 gpg_password_file: Option<String>,
196 upload_addr: Option<String>,
197 src_tarball: Option<bool>,
200 #[derive(RustcDecodable)]
206 impl Default for StringOrBool {
207 fn default() -> StringOrBool {
208 StringOrBool::Bool(false)
212 /// TOML representation of how the Rust build is configured.
213 #[derive(RustcDecodable, Default)]
215 optimize: Option<bool>,
216 codegen_units: Option<u32>,
217 debug_assertions: Option<bool>,
218 debuginfo: Option<bool>,
219 debuginfo_lines: Option<bool>,
220 debuginfo_only_std: Option<bool>,
221 debug_jemalloc: Option<bool>,
222 use_jemalloc: Option<bool>,
223 backtrace: Option<bool>,
224 default_linker: Option<String>,
225 default_ar: Option<String>,
226 channel: Option<String>,
227 musl_root: Option<String>,
229 optimize_tests: Option<bool>,
230 debuginfo_tests: Option<bool>,
231 codegen_tests: Option<bool>,
234 /// TOML representation of how each build target is configured.
235 #[derive(RustcDecodable, Default)]
237 llvm_config: Option<String>,
238 jemalloc: Option<String>,
241 android_ndk: Option<String>,
242 musl_root: Option<String>,
243 qemu_rootfs: Option<String>,
247 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
248 let mut config = Config::default();
249 config.llvm_optimize = true;
250 config.use_jemalloc = true;
251 config.backtrace = true;
252 config.rust_optimize = true;
253 config.rust_optimize_tests = true;
254 config.submodules = true;
256 config.rust_rpath = true;
257 config.rust_codegen_units = 1;
258 config.build = build.to_string();
259 config.channel = "dev".to_string();
260 config.codegen_tests = true;
261 config.rust_dist_src = true;
263 let toml = file.map(|file| {
264 let mut f = t!(File::open(&file));
265 let mut toml = String::new();
266 t!(f.read_to_string(&mut toml));
267 let mut p = Parser::new(&toml);
268 let table = match p.parse() {
269 Some(table) => table,
271 println!("failed to parse TOML configuration '{}':", file.to_str().unwrap());
272 for err in p.errors.iter() {
273 let (loline, locol) = p.to_linecol(err.lo);
274 let (hiline, hicol) = p.to_linecol(err.hi);
275 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
281 let mut d = Decoder::new(Value::Table(table));
282 match Decodable::decode(&mut d) {
285 println!("failed to decode TOML: {}", e);
289 }).unwrap_or_else(|| TomlConfig::default());
291 let build = toml.build.clone().unwrap_or(Build::default());
292 set(&mut config.build, build.build.clone());
293 config.host.push(config.build.clone());
294 for host in build.host.iter() {
295 if !config.host.contains(host) {
296 config.host.push(host.clone());
299 for target in config.host.iter().chain(&build.target) {
300 if !config.target.contains(target) {
301 config.target.push(target.clone());
304 config.rustc = build.rustc.map(PathBuf::from);
305 config.cargo = build.cargo.map(PathBuf::from);
306 config.nodejs = build.nodejs.map(PathBuf::from);
307 config.gdb = build.gdb.map(PathBuf::from);
308 config.python = build.python.map(PathBuf::from);
309 set(&mut config.compiler_docs, build.compiler_docs);
310 set(&mut config.docs, build.docs);
311 set(&mut config.submodules, build.submodules);
312 set(&mut config.locked_deps, build.locked_deps);
313 set(&mut config.vendor, build.vendor);
314 set(&mut config.full_bootstrap, build.full_bootstrap);
315 set(&mut config.extended, build.extended);
316 set(&mut config.verbose, build.verbose);
317 set(&mut config.sanitizers, build.sanitizers);
318 set(&mut config.openssl_static, build.openssl_static);
320 if let Some(ref install) = toml.install {
321 config.prefix = install.prefix.clone().map(PathBuf::from);
322 config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
323 config.docdir = install.docdir.clone().map(PathBuf::from);
324 config.bindir = install.bindir.clone().map(PathBuf::from);
325 config.libdir = install.libdir.clone().map(PathBuf::from);
326 config.mandir = install.mandir.clone().map(PathBuf::from);
329 if let Some(ref llvm) = toml.llvm {
331 Some(StringOrBool::String(ref s)) => {
332 config.ccache = Some(s.to_string())
334 Some(StringOrBool::Bool(true)) => {
335 config.ccache = Some("ccache".to_string());
337 Some(StringOrBool::Bool(false)) | None => {}
339 set(&mut config.ninja, llvm.ninja);
340 set(&mut config.llvm_assertions, llvm.assertions);
341 set(&mut config.llvm_optimize, llvm.optimize);
342 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
343 set(&mut config.llvm_version_check, llvm.version_check);
344 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
345 set(&mut config.llvm_clean_rebuild, llvm.clean_rebuild);
346 config.llvm_targets = llvm.targets.clone();
347 config.llvm_link_jobs = llvm.link_jobs;
350 if let Some(ref rust) = toml.rust {
351 set(&mut config.rust_debug_assertions, rust.debug_assertions);
352 set(&mut config.rust_debuginfo, rust.debuginfo);
353 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
354 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
355 set(&mut config.rust_optimize, rust.optimize);
356 set(&mut config.rust_optimize_tests, rust.optimize_tests);
357 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
358 set(&mut config.codegen_tests, rust.codegen_tests);
359 set(&mut config.rust_rpath, rust.rpath);
360 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
361 set(&mut config.use_jemalloc, rust.use_jemalloc);
362 set(&mut config.backtrace, rust.backtrace);
363 set(&mut config.channel, rust.channel.clone());
364 config.rustc_default_linker = rust.default_linker.clone();
365 config.rustc_default_ar = rust.default_ar.clone();
366 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
368 match rust.codegen_units {
369 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
370 Some(n) => config.rust_codegen_units = n,
375 if let Some(ref t) = toml.target {
376 for (triple, cfg) in t {
377 let mut target = Target::default();
379 if let Some(ref s) = cfg.llvm_config {
380 target.llvm_config = Some(env::current_dir().unwrap().join(s));
382 if let Some(ref s) = cfg.jemalloc {
383 target.jemalloc = Some(env::current_dir().unwrap().join(s));
385 if let Some(ref s) = cfg.android_ndk {
386 target.ndk = Some(env::current_dir().unwrap().join(s));
388 target.cxx = cfg.cxx.clone().map(PathBuf::from);
389 target.cc = cfg.cc.clone().map(PathBuf::from);
390 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
391 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
393 config.target_config.insert(triple.clone(), target);
397 if let Some(ref t) = toml.dist {
398 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
399 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
400 config.dist_upload_addr = t.upload_addr.clone();
401 set(&mut config.rust_dist_src, t.src_tarball);
407 /// "Temporary" routine to parse `config.mk` into this configuration.
409 /// While we still have `./configure` this implements the ability to decode
410 /// that configuration into this. This isn't exactly a full-blown makefile
411 /// parser, but hey it gets the job done!
412 pub fn update_with_config_mk(&mut self) {
413 let mut config = String::new();
414 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
415 for line in config.lines() {
416 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
417 let key = parts.next().unwrap();
418 let value = match parts.next() {
419 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
425 ($(($name:expr, $val:expr),)*) => {
428 if key == concat!("CFG_ENABLE_", $name) {
432 if key == concat!("CFG_DISABLE_", $name) {
442 ("MANAGE_SUBMODULES", self.submodules),
443 ("COMPILER_DOCS", self.compiler_docs),
445 ("LLVM_ASSERTIONS", self.llvm_assertions),
446 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
447 ("OPTIMIZE_LLVM", self.llvm_optimize),
448 ("LLVM_VERSION_CHECK", self.llvm_version_check),
449 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
450 ("LLVM_LINK_SHARED", self.llvm_link_shared),
451 ("LLVM_CLEAN_REBUILD", self.llvm_clean_rebuild),
452 ("OPTIMIZE", self.rust_optimize),
453 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
454 ("DEBUGINFO", self.rust_debuginfo),
455 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
456 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
457 ("JEMALLOC", self.use_jemalloc),
458 ("DEBUG_JEMALLOC", self.debug_jemalloc),
459 ("RPATH", self.rust_rpath),
460 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
461 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
462 ("QUIET_TESTS", self.quiet_tests),
463 ("LOCAL_REBUILD", self.local_rebuild),
464 ("NINJA", self.ninja),
465 ("CODEGEN_TESTS", self.codegen_tests),
466 ("LOCKED_DEPS", self.locked_deps),
467 ("VENDOR", self.vendor),
468 ("FULL_BOOTSTRAP", self.full_bootstrap),
469 ("EXTENDED", self.extended),
470 ("SANITIZERS", self.sanitizers),
471 ("DIST_SRC", self.rust_dist_src),
472 ("CARGO_OPENSSL_STATIC", self.openssl_static),
476 "CFG_BUILD" if value.len() > 0 => self.build = value.to_string(),
477 "CFG_HOST" if value.len() > 0 => {
478 self.host.extend(value.split(" ").map(|s| s.to_string()));
481 "CFG_TARGET" if value.len() > 0 => {
482 self.target.extend(value.split(" ").map(|s| s.to_string()));
484 "CFG_MUSL_ROOT" if value.len() > 0 => {
485 self.musl_root = Some(parse_configure_path(value));
487 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
488 let target = "x86_64-unknown-linux-musl".to_string();
489 let target = self.target_config.entry(target)
490 .or_insert(Target::default());
491 target.musl_root = Some(parse_configure_path(value));
493 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
494 let target = "i686-unknown-linux-musl".to_string();
495 let target = self.target_config.entry(target)
496 .or_insert(Target::default());
497 target.musl_root = Some(parse_configure_path(value));
499 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
500 let target = "arm-unknown-linux-musleabi".to_string();
501 let target = self.target_config.entry(target)
502 .or_insert(Target::default());
503 target.musl_root = Some(parse_configure_path(value));
505 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
506 let target = "arm-unknown-linux-musleabihf".to_string();
507 let target = self.target_config.entry(target)
508 .or_insert(Target::default());
509 target.musl_root = Some(parse_configure_path(value));
511 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
512 let target = "armv7-unknown-linux-musleabihf".to_string();
513 let target = self.target_config.entry(target)
514 .or_insert(Target::default());
515 target.musl_root = Some(parse_configure_path(value));
517 "CFG_DEFAULT_AR" if value.len() > 0 => {
518 self.rustc_default_ar = Some(value.to_string());
520 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
521 self.rustc_default_linker = Some(value.to_string());
523 "CFG_GDB" if value.len() > 0 => {
524 self.gdb = Some(parse_configure_path(value));
526 "CFG_RELEASE_CHANNEL" => {
527 self.channel = value.to_string();
530 self.prefix = Some(PathBuf::from(value));
532 "CFG_SYSCONFDIR" => {
533 self.sysconfdir = Some(PathBuf::from(value));
536 self.docdir = Some(PathBuf::from(value));
539 self.bindir = Some(PathBuf::from(value));
542 self.libdir = Some(PathBuf::from(value));
544 "CFG_LIBDIR_RELATIVE" => {
545 self.libdir_relative = Some(PathBuf::from(value));
548 self.mandir = Some(PathBuf::from(value));
550 "CFG_LLVM_ROOT" if value.len() > 0 => {
551 let target = self.target_config.entry(self.build.clone())
552 .or_insert(Target::default());
553 let root = parse_configure_path(value);
554 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
556 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
557 let target = self.target_config.entry(self.build.clone())
558 .or_insert(Target::default());
559 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
561 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
562 let target = "arm-linux-androideabi".to_string();
563 let target = self.target_config.entry(target)
564 .or_insert(Target::default());
565 target.ndk = Some(parse_configure_path(value));
567 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
568 let target = "armv7-linux-androideabi".to_string();
569 let target = self.target_config.entry(target)
570 .or_insert(Target::default());
571 target.ndk = Some(parse_configure_path(value));
573 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
574 let target = "i686-linux-android".to_string();
575 let target = self.target_config.entry(target)
576 .or_insert(Target::default());
577 target.ndk = Some(parse_configure_path(value));
579 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
580 let target = "aarch64-linux-android".to_string();
581 let target = self.target_config.entry(target)
582 .or_insert(Target::default());
583 target.ndk = Some(parse_configure_path(value));
585 "CFG_X86_64_LINUX_ANDROID_NDK" if value.len() > 0 => {
586 let target = "x86_64-linux-android".to_string();
587 let target = self.target_config.entry(target)
588 .or_insert(Target::default());
589 target.ndk = Some(parse_configure_path(value));
591 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
592 let path = parse_configure_path(value);
593 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
594 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
596 "CFG_PYTHON" if value.len() > 0 => {
597 let path = parse_configure_path(value);
598 self.python = Some(path);
600 "CFG_ENABLE_CCACHE" if value == "1" => {
601 self.ccache = Some(exe("ccache", &self.build));
603 "CFG_ENABLE_SCCACHE" if value == "1" => {
604 self.ccache = Some(exe("sccache", &self.build));
606 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
607 self.configure_args = value.split_whitespace()
608 .map(|s| s.to_string())
611 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
612 let target = "arm-unknown-linux-gnueabihf".to_string();
613 let target = self.target_config.entry(target)
614 .or_insert(Target::default());
615 target.qemu_rootfs = Some(parse_configure_path(value));
622 pub fn verbose(&self) -> bool {
626 pub fn very_verbose(&self) -> bool {
632 fn parse_configure_path(path: &str) -> PathBuf {
637 fn parse_configure_path(path: &str) -> PathBuf {
638 // on windows, configure produces unix style paths e.g. /c/some/path but we
639 // only want real windows paths
641 use std::process::Command;
644 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
645 if !path.contains('/') {
649 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
650 let win_path = win_path.trim();
655 fn set<T>(field: &mut T, val: Option<T>) {
656 if let Some(v) = val {