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::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,
48 pub target_config: HashMap<String, Target>,
49 pub full_bootstrap: bool,
53 // llvm codegen options
54 pub llvm_assertions: bool,
55 pub llvm_optimize: bool,
56 pub llvm_release_debuginfo: bool,
57 pub llvm_version_check: bool,
58 pub llvm_static_stdcpp: bool,
59 pub llvm_link_shared: bool,
60 pub llvm_targets: Option<String>,
62 // rust codegen options
63 pub rust_optimize: bool,
64 pub rust_codegen_units: u32,
65 pub rust_debug_assertions: bool,
66 pub rust_debuginfo: bool,
67 pub rust_debuginfo_lines: bool,
68 pub rust_debuginfo_only_std: bool,
70 pub rustc_default_linker: Option<String>,
71 pub rustc_default_ar: Option<String>,
72 pub rust_optimize_tests: bool,
73 pub rust_debuginfo_tests: bool,
76 pub host: Vec<String>,
77 pub target: Vec<String>,
78 pub rustc: Option<PathBuf>,
79 pub cargo: Option<PathBuf>,
80 pub local_rebuild: bool,
83 pub dist_sign_folder: Option<PathBuf>,
84 pub dist_upload_addr: Option<String>,
85 pub dist_gpg_password_file: Option<PathBuf>,
88 pub debug_jemalloc: bool,
89 pub use_jemalloc: bool,
90 pub backtrace: bool, // support for RUST_BACKTRACE
94 pub quiet_tests: bool,
95 // Fallback musl-root for all targets
96 pub musl_root: Option<PathBuf>,
97 pub prefix: Option<PathBuf>,
98 pub docdir: Option<PathBuf>,
99 pub libdir: Option<PathBuf>,
100 pub libdir_relative: Option<PathBuf>,
101 pub mandir: Option<PathBuf>,
102 pub codegen_tests: bool,
103 pub nodejs: Option<PathBuf>,
104 pub gdb: Option<PathBuf>,
105 pub python: Option<PathBuf>,
106 pub configure_args: Vec<String>,
109 /// Per-target configuration stored in the global configuration structure.
112 pub llvm_config: Option<PathBuf>,
113 pub jemalloc: Option<PathBuf>,
114 pub cc: Option<PathBuf>,
115 pub cxx: Option<PathBuf>,
116 pub ndk: Option<PathBuf>,
117 pub musl_root: Option<PathBuf>,
118 pub qemu_rootfs: Option<PathBuf>,
121 /// Structure of the `config.toml` file that configuration is read from.
123 /// This structure uses `Decodable` to automatically decode a TOML configuration
124 /// file into this format, and then this is traversed and written into the above
125 /// `Config` structure.
126 #[derive(RustcDecodable, Default)]
128 build: Option<Build>,
129 install: Option<Install>,
132 target: Option<HashMap<String, TomlTarget>>,
136 /// TOML representation of various global build decisions.
137 #[derive(RustcDecodable, Default, Clone)]
139 build: Option<String>,
142 cargo: Option<String>,
143 rustc: Option<String>,
144 compiler_docs: Option<bool>,
146 submodules: Option<bool>,
148 vendor: Option<bool>,
149 nodejs: Option<String>,
150 python: Option<String>,
151 full_bootstrap: Option<bool>,
152 extended: Option<bool>,
153 sanitizers: Option<bool>,
156 /// TOML representation of various global install decisions.
157 #[derive(RustcDecodable, Default, Clone)]
159 prefix: Option<String>,
160 mandir: Option<String>,
161 docdir: Option<String>,
162 libdir: Option<String>,
165 /// TOML representation of how the LLVM build is configured.
166 #[derive(RustcDecodable, Default)]
168 ccache: Option<StringOrBool>,
170 assertions: Option<bool>,
171 optimize: Option<bool>,
172 release_debuginfo: Option<bool>,
173 version_check: Option<bool>,
174 static_libstdcpp: Option<bool>,
175 targets: Option<String>,
178 #[derive(RustcDecodable, Default, Clone)]
180 sign_folder: Option<String>,
181 gpg_password_file: Option<String>,
182 upload_addr: Option<String>,
185 #[derive(RustcDecodable)]
191 impl Default for StringOrBool {
192 fn default() -> StringOrBool {
193 StringOrBool::Bool(false)
197 /// TOML representation of how the Rust build is configured.
198 #[derive(RustcDecodable, Default)]
200 optimize: Option<bool>,
201 codegen_units: Option<u32>,
202 debug_assertions: Option<bool>,
203 debuginfo: Option<bool>,
204 debuginfo_lines: Option<bool>,
205 debuginfo_only_std: Option<bool>,
206 debug_jemalloc: Option<bool>,
207 use_jemalloc: Option<bool>,
208 backtrace: Option<bool>,
209 default_linker: Option<String>,
210 default_ar: Option<String>,
211 channel: Option<String>,
212 musl_root: Option<String>,
214 optimize_tests: Option<bool>,
215 debuginfo_tests: Option<bool>,
216 codegen_tests: Option<bool>,
219 /// TOML representation of how each build target is configured.
220 #[derive(RustcDecodable, Default)]
222 llvm_config: Option<String>,
223 jemalloc: Option<String>,
226 android_ndk: Option<String>,
227 musl_root: Option<String>,
228 qemu_rootfs: Option<String>,
232 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
233 let mut config = Config::default();
234 config.llvm_optimize = true;
235 config.use_jemalloc = true;
236 config.backtrace = true;
237 config.rust_optimize = true;
238 config.rust_optimize_tests = true;
239 config.submodules = true;
241 config.rust_rpath = true;
242 config.rust_codegen_units = 1;
243 config.build = build.to_string();
244 config.channel = "dev".to_string();
245 config.codegen_tests = true;
247 let toml = file.map(|file| {
248 let mut f = t!(File::open(&file));
249 let mut toml = String::new();
250 t!(f.read_to_string(&mut toml));
251 let mut p = Parser::new(&toml);
252 let table = match p.parse() {
253 Some(table) => table,
255 println!("failed to parse TOML configuration:");
256 for err in p.errors.iter() {
257 let (loline, locol) = p.to_linecol(err.lo);
258 let (hiline, hicol) = p.to_linecol(err.hi);
259 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
265 let mut d = Decoder::new(Value::Table(table));
266 match Decodable::decode(&mut d) {
269 println!("failed to decode TOML: {}", e);
273 }).unwrap_or_else(|| TomlConfig::default());
275 let build = toml.build.clone().unwrap_or(Build::default());
276 set(&mut config.build, build.build.clone());
277 config.host.push(config.build.clone());
278 for host in build.host.iter() {
279 if !config.host.contains(host) {
280 config.host.push(host.clone());
283 for target in config.host.iter().chain(&build.target) {
284 if !config.target.contains(target) {
285 config.target.push(target.clone());
288 config.rustc = build.rustc.map(PathBuf::from);
289 config.cargo = build.cargo.map(PathBuf::from);
290 config.nodejs = build.nodejs.map(PathBuf::from);
291 config.gdb = build.gdb.map(PathBuf::from);
292 config.python = build.python.map(PathBuf::from);
293 set(&mut config.compiler_docs, build.compiler_docs);
294 set(&mut config.docs, build.docs);
295 set(&mut config.submodules, build.submodules);
296 set(&mut config.vendor, build.vendor);
297 set(&mut config.full_bootstrap, build.full_bootstrap);
298 set(&mut config.extended, build.extended);
299 set(&mut config.sanitizers, build.sanitizers);
301 if let Some(ref install) = toml.install {
302 config.prefix = install.prefix.clone().map(PathBuf::from);
303 config.mandir = install.mandir.clone().map(PathBuf::from);
304 config.docdir = install.docdir.clone().map(PathBuf::from);
305 config.libdir = install.libdir.clone().map(PathBuf::from);
308 if let Some(ref llvm) = toml.llvm {
310 Some(StringOrBool::String(ref s)) => {
311 config.ccache = Some(s.to_string())
313 Some(StringOrBool::Bool(true)) => {
314 config.ccache = Some("ccache".to_string());
316 Some(StringOrBool::Bool(false)) | None => {}
318 set(&mut config.ninja, llvm.ninja);
319 set(&mut config.llvm_assertions, llvm.assertions);
320 set(&mut config.llvm_optimize, llvm.optimize);
321 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
322 set(&mut config.llvm_version_check, llvm.version_check);
323 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
324 config.llvm_targets = llvm.targets.clone();
327 if let Some(ref rust) = toml.rust {
328 set(&mut config.rust_debug_assertions, rust.debug_assertions);
329 set(&mut config.rust_debuginfo, rust.debuginfo);
330 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
331 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
332 set(&mut config.rust_optimize, rust.optimize);
333 set(&mut config.rust_optimize_tests, rust.optimize_tests);
334 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
335 set(&mut config.codegen_tests, rust.codegen_tests);
336 set(&mut config.rust_rpath, rust.rpath);
337 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
338 set(&mut config.use_jemalloc, rust.use_jemalloc);
339 set(&mut config.backtrace, rust.backtrace);
340 set(&mut config.channel, rust.channel.clone());
341 config.rustc_default_linker = rust.default_linker.clone();
342 config.rustc_default_ar = rust.default_ar.clone();
343 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
345 match rust.codegen_units {
346 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
347 Some(n) => config.rust_codegen_units = n,
352 if let Some(ref t) = toml.target {
353 for (triple, cfg) in t {
354 let mut target = Target::default();
356 if let Some(ref s) = cfg.llvm_config {
357 target.llvm_config = Some(env::current_dir().unwrap().join(s));
359 if let Some(ref s) = cfg.jemalloc {
360 target.jemalloc = Some(env::current_dir().unwrap().join(s));
362 if let Some(ref s) = cfg.android_ndk {
363 target.ndk = Some(env::current_dir().unwrap().join(s));
365 target.cxx = cfg.cxx.clone().map(PathBuf::from);
366 target.cc = cfg.cc.clone().map(PathBuf::from);
367 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
368 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
370 config.target_config.insert(triple.clone(), target);
374 if let Some(ref t) = toml.dist {
375 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
376 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
377 config.dist_upload_addr = t.upload_addr.clone();
383 /// "Temporary" routine to parse `config.mk` into this configuration.
385 /// While we still have `./configure` this implements the ability to decode
386 /// that configuration into this. This isn't exactly a full-blown makefile
387 /// parser, but hey it gets the job done!
388 pub fn update_with_config_mk(&mut self) {
389 let mut config = String::new();
390 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
391 for line in config.lines() {
392 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
393 let key = parts.next().unwrap();
394 let value = match parts.next() {
395 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
401 ($(($name:expr, $val:expr),)*) => {
404 if key == concat!("CFG_ENABLE_", $name) {
408 if key == concat!("CFG_DISABLE_", $name) {
418 ("MANAGE_SUBMODULES", self.submodules),
419 ("COMPILER_DOCS", self.compiler_docs),
421 ("LLVM_ASSERTIONS", self.llvm_assertions),
422 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
423 ("OPTIMIZE_LLVM", self.llvm_optimize),
424 ("LLVM_VERSION_CHECK", self.llvm_version_check),
425 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
426 ("LLVM_LINK_SHARED", self.llvm_link_shared),
427 ("OPTIMIZE", self.rust_optimize),
428 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
429 ("DEBUGINFO", self.rust_debuginfo),
430 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
431 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
432 ("JEMALLOC", self.use_jemalloc),
433 ("DEBUG_JEMALLOC", self.debug_jemalloc),
434 ("RPATH", self.rust_rpath),
435 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
436 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
437 ("QUIET_TESTS", self.quiet_tests),
438 ("LOCAL_REBUILD", self.local_rebuild),
439 ("NINJA", self.ninja),
440 ("CODEGEN_TESTS", self.codegen_tests),
441 ("VENDOR", self.vendor),
442 ("FULL_BOOTSTRAP", self.full_bootstrap),
443 ("EXTENDED", self.extended),
444 ("SANITIZERS", self.sanitizers),
448 "CFG_BUILD" => self.build = value.to_string(),
450 self.host = value.split(" ").map(|s| s.to_string())
454 self.target = value.split(" ").map(|s| s.to_string())
457 "CFG_MUSL_ROOT" if value.len() > 0 => {
458 self.musl_root = Some(parse_configure_path(value));
460 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
461 let target = "x86_64-unknown-linux-musl".to_string();
462 let target = self.target_config.entry(target)
463 .or_insert(Target::default());
464 target.musl_root = Some(parse_configure_path(value));
466 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
467 let target = "i686-unknown-linux-musl".to_string();
468 let target = self.target_config.entry(target)
469 .or_insert(Target::default());
470 target.musl_root = Some(parse_configure_path(value));
472 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
473 let target = "arm-unknown-linux-musleabi".to_string();
474 let target = self.target_config.entry(target)
475 .or_insert(Target::default());
476 target.musl_root = Some(parse_configure_path(value));
478 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
479 let target = "arm-unknown-linux-musleabihf".to_string();
480 let target = self.target_config.entry(target)
481 .or_insert(Target::default());
482 target.musl_root = Some(parse_configure_path(value));
484 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
485 let target = "armv7-unknown-linux-musleabihf".to_string();
486 let target = self.target_config.entry(target)
487 .or_insert(Target::default());
488 target.musl_root = Some(parse_configure_path(value));
490 "CFG_DEFAULT_AR" if value.len() > 0 => {
491 self.rustc_default_ar = Some(value.to_string());
493 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
494 self.rustc_default_linker = Some(value.to_string());
496 "CFG_GDB" if value.len() > 0 => {
497 self.gdb = Some(parse_configure_path(value));
499 "CFG_RELEASE_CHANNEL" => {
500 self.channel = value.to_string();
503 self.prefix = Some(PathBuf::from(value));
506 self.docdir = Some(PathBuf::from(value));
509 self.libdir = Some(PathBuf::from(value));
511 "CFG_LIBDIR_RELATIVE" => {
512 self.libdir_relative = Some(PathBuf::from(value));
515 self.mandir = Some(PathBuf::from(value));
517 "CFG_LLVM_ROOT" if value.len() > 0 => {
518 let target = self.target_config.entry(self.build.clone())
519 .or_insert(Target::default());
520 let root = parse_configure_path(value);
521 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
523 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
524 let target = self.target_config.entry(self.build.clone())
525 .or_insert(Target::default());
526 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
528 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
529 let target = "arm-linux-androideabi".to_string();
530 let target = self.target_config.entry(target)
531 .or_insert(Target::default());
532 target.ndk = Some(parse_configure_path(value));
534 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
535 let target = "armv7-linux-androideabi".to_string();
536 let target = self.target_config.entry(target)
537 .or_insert(Target::default());
538 target.ndk = Some(parse_configure_path(value));
540 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
541 let target = "i686-linux-android".to_string();
542 let target = self.target_config.entry(target)
543 .or_insert(Target::default());
544 target.ndk = Some(parse_configure_path(value));
546 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
547 let target = "aarch64-linux-android".to_string();
548 let target = self.target_config.entry(target)
549 .or_insert(Target::default());
550 target.ndk = Some(parse_configure_path(value));
552 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
553 let path = parse_configure_path(value);
554 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
555 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
557 "CFG_PYTHON" if value.len() > 0 => {
558 let path = parse_configure_path(value);
559 self.python = Some(path);
561 "CFG_ENABLE_CCACHE" if value == "1" => {
562 self.ccache = Some("ccache".to_string());
564 "CFG_ENABLE_SCCACHE" if value == "1" => {
565 self.ccache = Some("sccache".to_string());
567 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
568 self.configure_args = value.split_whitespace()
569 .map(|s| s.to_string())
572 "CFG_QEMU_ARMHF_ROOTFS" if value.len() > 0 => {
573 let target = "arm-unknown-linux-gnueabihf".to_string();
574 let target = self.target_config.entry(target)
575 .or_insert(Target::default());
576 target.qemu_rootfs = Some(parse_configure_path(value));
583 pub fn verbose(&self) -> bool {
587 pub fn very_verbose(&self) -> bool {
593 fn parse_configure_path(path: &str) -> PathBuf {
598 fn parse_configure_path(path: &str) -> PathBuf {
599 // on windows, configure produces unix style paths e.g. /c/some/path but we
600 // only want real windows paths
602 use std::process::Command;
605 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
606 if !path.contains('/') {
610 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
611 let win_path = win_path.trim();
616 fn set<T>(field: &mut T, val: Option<T>) {
617 if let Some(v) = val {