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,
52 // llvm codegen options
53 pub llvm_assertions: bool,
54 pub llvm_optimize: bool,
55 pub llvm_release_debuginfo: bool,
56 pub llvm_version_check: bool,
57 pub llvm_static_stdcpp: bool,
58 pub llvm_link_shared: bool,
59 pub llvm_targets: Option<String>,
61 // rust codegen options
62 pub rust_optimize: bool,
63 pub rust_codegen_units: u32,
64 pub rust_debug_assertions: bool,
65 pub rust_debuginfo: bool,
66 pub rust_debuginfo_lines: bool,
67 pub rust_debuginfo_only_std: bool,
69 pub rustc_default_linker: Option<String>,
70 pub rustc_default_ar: Option<String>,
71 pub rust_optimize_tests: bool,
72 pub rust_debuginfo_tests: bool,
75 pub host: Vec<String>,
76 pub target: Vec<String>,
77 pub rustc: Option<PathBuf>,
78 pub cargo: Option<PathBuf>,
79 pub local_rebuild: bool,
82 pub dist_sign_folder: Option<PathBuf>,
83 pub dist_upload_addr: Option<String>,
84 pub dist_gpg_password_file: Option<PathBuf>,
87 pub debug_jemalloc: bool,
88 pub use_jemalloc: bool,
89 pub backtrace: bool, // support for RUST_BACKTRACE
93 pub quiet_tests: bool,
94 // Fallback musl-root for all targets
95 pub musl_root: Option<PathBuf>,
96 pub prefix: Option<PathBuf>,
97 pub docdir: Option<PathBuf>,
98 pub libdir: Option<PathBuf>,
99 pub libdir_relative: Option<PathBuf>,
100 pub mandir: Option<PathBuf>,
101 pub codegen_tests: bool,
102 pub nodejs: Option<PathBuf>,
103 pub gdb: Option<PathBuf>,
104 pub python: Option<PathBuf>,
105 pub configure_args: Vec<String>,
108 /// Per-target configuration stored in the global configuration structure.
111 pub llvm_config: Option<PathBuf>,
112 pub jemalloc: Option<PathBuf>,
113 pub cc: Option<PathBuf>,
114 pub cxx: Option<PathBuf>,
115 pub ndk: Option<PathBuf>,
116 pub musl_root: Option<PathBuf>,
119 /// Structure of the `config.toml` file that configuration is read from.
121 /// This structure uses `Decodable` to automatically decode a TOML configuration
122 /// file into this format, and then this is traversed and written into the above
123 /// `Config` structure.
124 #[derive(RustcDecodable, Default)]
126 build: Option<Build>,
127 install: Option<Install>,
130 target: Option<HashMap<String, TomlTarget>>,
134 /// TOML representation of various global build decisions.
135 #[derive(RustcDecodable, Default, Clone)]
137 build: Option<String>,
140 cargo: Option<String>,
141 rustc: Option<String>,
142 compiler_docs: Option<bool>,
144 submodules: Option<bool>,
146 vendor: Option<bool>,
147 nodejs: Option<String>,
148 python: Option<String>,
149 full_bootstrap: Option<bool>,
150 extended: Option<bool>,
153 /// TOML representation of various global install decisions.
154 #[derive(RustcDecodable, Default, Clone)]
156 prefix: Option<String>,
157 mandir: Option<String>,
158 docdir: Option<String>,
159 libdir: Option<String>,
162 /// TOML representation of how the LLVM build is configured.
163 #[derive(RustcDecodable, Default)]
165 ccache: Option<StringOrBool>,
167 assertions: Option<bool>,
168 optimize: Option<bool>,
169 release_debuginfo: Option<bool>,
170 version_check: Option<bool>,
171 static_libstdcpp: Option<bool>,
172 targets: Option<String>,
175 #[derive(RustcDecodable, Default, Clone)]
177 sign_folder: Option<String>,
178 gpg_password_file: Option<String>,
179 upload_addr: Option<String>,
182 #[derive(RustcDecodable)]
188 impl Default for StringOrBool {
189 fn default() -> StringOrBool {
190 StringOrBool::Bool(false)
194 /// TOML representation of how the Rust build is configured.
195 #[derive(RustcDecodable, Default)]
197 optimize: Option<bool>,
198 codegen_units: Option<u32>,
199 debug_assertions: Option<bool>,
200 debuginfo: Option<bool>,
201 debuginfo_lines: Option<bool>,
202 debuginfo_only_std: Option<bool>,
203 debug_jemalloc: Option<bool>,
204 use_jemalloc: Option<bool>,
205 backtrace: Option<bool>,
206 default_linker: Option<String>,
207 default_ar: Option<String>,
208 channel: Option<String>,
209 musl_root: Option<String>,
211 optimize_tests: Option<bool>,
212 debuginfo_tests: Option<bool>,
213 codegen_tests: Option<bool>,
216 /// TOML representation of how each build target is configured.
217 #[derive(RustcDecodable, Default)]
219 llvm_config: Option<String>,
220 jemalloc: Option<String>,
223 android_ndk: Option<String>,
224 musl_root: Option<String>,
228 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
229 let mut config = Config::default();
230 config.llvm_optimize = true;
231 config.use_jemalloc = true;
232 config.backtrace = true;
233 config.rust_optimize = true;
234 config.rust_optimize_tests = true;
235 config.submodules = true;
237 config.rust_rpath = true;
238 config.rust_codegen_units = 1;
239 config.build = build.to_string();
240 config.channel = "dev".to_string();
241 config.codegen_tests = true;
243 let toml = file.map(|file| {
244 let mut f = t!(File::open(&file));
245 let mut toml = String::new();
246 t!(f.read_to_string(&mut toml));
247 let mut p = Parser::new(&toml);
248 let table = match p.parse() {
249 Some(table) => table,
251 println!("failed to parse TOML configuration:");
252 for err in p.errors.iter() {
253 let (loline, locol) = p.to_linecol(err.lo);
254 let (hiline, hicol) = p.to_linecol(err.hi);
255 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
261 let mut d = Decoder::new(Value::Table(table));
262 match Decodable::decode(&mut d) {
265 println!("failed to decode TOML: {}", e);
269 }).unwrap_or_else(|| TomlConfig::default());
271 let build = toml.build.clone().unwrap_or(Build::default());
272 set(&mut config.build, build.build.clone());
273 config.host.push(config.build.clone());
274 for host in build.host.iter() {
275 if !config.host.contains(host) {
276 config.host.push(host.clone());
279 for target in config.host.iter().chain(&build.target) {
280 if !config.target.contains(target) {
281 config.target.push(target.clone());
284 config.rustc = build.rustc.map(PathBuf::from);
285 config.cargo = build.cargo.map(PathBuf::from);
286 config.nodejs = build.nodejs.map(PathBuf::from);
287 config.gdb = build.gdb.map(PathBuf::from);
288 config.python = build.python.map(PathBuf::from);
289 set(&mut config.compiler_docs, build.compiler_docs);
290 set(&mut config.docs, build.docs);
291 set(&mut config.submodules, build.submodules);
292 set(&mut config.vendor, build.vendor);
293 set(&mut config.full_bootstrap, build.full_bootstrap);
294 set(&mut config.extended, build.extended);
296 if let Some(ref install) = toml.install {
297 config.prefix = install.prefix.clone().map(PathBuf::from);
298 config.mandir = install.mandir.clone().map(PathBuf::from);
299 config.docdir = install.docdir.clone().map(PathBuf::from);
300 config.libdir = install.libdir.clone().map(PathBuf::from);
303 if let Some(ref llvm) = toml.llvm {
305 Some(StringOrBool::String(ref s)) => {
306 config.ccache = Some(s.to_string())
308 Some(StringOrBool::Bool(true)) => {
309 config.ccache = Some("ccache".to_string());
311 Some(StringOrBool::Bool(false)) | None => {}
313 set(&mut config.ninja, llvm.ninja);
314 set(&mut config.llvm_assertions, llvm.assertions);
315 set(&mut config.llvm_optimize, llvm.optimize);
316 set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
317 set(&mut config.llvm_version_check, llvm.version_check);
318 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
319 config.llvm_targets = llvm.targets.clone();
322 if let Some(ref rust) = toml.rust {
323 set(&mut config.rust_debug_assertions, rust.debug_assertions);
324 set(&mut config.rust_debuginfo, rust.debuginfo);
325 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
326 set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
327 set(&mut config.rust_optimize, rust.optimize);
328 set(&mut config.rust_optimize_tests, rust.optimize_tests);
329 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
330 set(&mut config.codegen_tests, rust.codegen_tests);
331 set(&mut config.rust_rpath, rust.rpath);
332 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
333 set(&mut config.use_jemalloc, rust.use_jemalloc);
334 set(&mut config.backtrace, rust.backtrace);
335 set(&mut config.channel, rust.channel.clone());
336 config.rustc_default_linker = rust.default_linker.clone();
337 config.rustc_default_ar = rust.default_ar.clone();
338 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
340 match rust.codegen_units {
341 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
342 Some(n) => config.rust_codegen_units = n,
347 if let Some(ref t) = toml.target {
348 for (triple, cfg) in t {
349 let mut target = Target::default();
351 if let Some(ref s) = cfg.llvm_config {
352 target.llvm_config = Some(env::current_dir().unwrap().join(s));
354 if let Some(ref s) = cfg.jemalloc {
355 target.jemalloc = Some(env::current_dir().unwrap().join(s));
357 if let Some(ref s) = cfg.android_ndk {
358 target.ndk = Some(env::current_dir().unwrap().join(s));
360 target.cxx = cfg.cxx.clone().map(PathBuf::from);
361 target.cc = cfg.cc.clone().map(PathBuf::from);
362 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
364 config.target_config.insert(triple.clone(), target);
368 if let Some(ref t) = toml.dist {
369 config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
370 config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
371 config.dist_upload_addr = t.upload_addr.clone();
377 /// "Temporary" routine to parse `config.mk` into this configuration.
379 /// While we still have `./configure` this implements the ability to decode
380 /// that configuration into this. This isn't exactly a full-blown makefile
381 /// parser, but hey it gets the job done!
382 pub fn update_with_config_mk(&mut self) {
383 let mut config = String::new();
384 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
385 for line in config.lines() {
386 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
387 let key = parts.next().unwrap();
388 let value = match parts.next() {
389 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
395 ($(($name:expr, $val:expr),)*) => {
398 if key == concat!("CFG_ENABLE_", $name) {
402 if key == concat!("CFG_DISABLE_", $name) {
412 ("MANAGE_SUBMODULES", self.submodules),
413 ("COMPILER_DOCS", self.compiler_docs),
415 ("LLVM_ASSERTIONS", self.llvm_assertions),
416 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
417 ("OPTIMIZE_LLVM", self.llvm_optimize),
418 ("LLVM_VERSION_CHECK", self.llvm_version_check),
419 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
420 ("LLVM_LINK_SHARED", self.llvm_link_shared),
421 ("OPTIMIZE", self.rust_optimize),
422 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
423 ("DEBUGINFO", self.rust_debuginfo),
424 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
425 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
426 ("JEMALLOC", self.use_jemalloc),
427 ("DEBUG_JEMALLOC", self.debug_jemalloc),
428 ("RPATH", self.rust_rpath),
429 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
430 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
431 ("QUIET_TESTS", self.quiet_tests),
432 ("LOCAL_REBUILD", self.local_rebuild),
433 ("NINJA", self.ninja),
434 ("CODEGEN_TESTS", self.codegen_tests),
435 ("VENDOR", self.vendor),
436 ("FULL_BOOTSTRAP", self.full_bootstrap),
437 ("EXTENDED", self.extended),
441 "CFG_BUILD" => self.build = value.to_string(),
443 self.host = value.split(" ").map(|s| s.to_string())
447 self.target = value.split(" ").map(|s| s.to_string())
450 "CFG_MUSL_ROOT" if value.len() > 0 => {
451 self.musl_root = Some(parse_configure_path(value));
453 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
454 let target = "x86_64-unknown-linux-musl".to_string();
455 let target = self.target_config.entry(target)
456 .or_insert(Target::default());
457 target.musl_root = Some(parse_configure_path(value));
459 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
460 let target = "i686-unknown-linux-musl".to_string();
461 let target = self.target_config.entry(target)
462 .or_insert(Target::default());
463 target.musl_root = Some(parse_configure_path(value));
465 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
466 let target = "arm-unknown-linux-musleabi".to_string();
467 let target = self.target_config.entry(target)
468 .or_insert(Target::default());
469 target.musl_root = Some(parse_configure_path(value));
471 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
472 let target = "arm-unknown-linux-musleabihf".to_string();
473 let target = self.target_config.entry(target)
474 .or_insert(Target::default());
475 target.musl_root = Some(parse_configure_path(value));
477 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
478 let target = "armv7-unknown-linux-musleabihf".to_string();
479 let target = self.target_config.entry(target)
480 .or_insert(Target::default());
481 target.musl_root = Some(parse_configure_path(value));
483 "CFG_DEFAULT_AR" if value.len() > 0 => {
484 self.rustc_default_ar = Some(value.to_string());
486 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
487 self.rustc_default_linker = Some(value.to_string());
489 "CFG_GDB" if value.len() > 0 => {
490 self.gdb = Some(parse_configure_path(value));
492 "CFG_RELEASE_CHANNEL" => {
493 self.channel = value.to_string();
496 self.prefix = Some(PathBuf::from(value));
499 self.docdir = Some(PathBuf::from(value));
502 self.libdir = Some(PathBuf::from(value));
504 "CFG_LIBDIR_RELATIVE" => {
505 self.libdir_relative = Some(PathBuf::from(value));
508 self.mandir = Some(PathBuf::from(value));
510 "CFG_LLVM_ROOT" if value.len() > 0 => {
511 let target = self.target_config.entry(self.build.clone())
512 .or_insert(Target::default());
513 let root = parse_configure_path(value);
514 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
516 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
517 let target = self.target_config.entry(self.build.clone())
518 .or_insert(Target::default());
519 target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
521 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
522 let target = "arm-linux-androideabi".to_string();
523 let target = self.target_config.entry(target)
524 .or_insert(Target::default());
525 target.ndk = Some(parse_configure_path(value));
527 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
528 let target = "armv7-linux-androideabi".to_string();
529 let target = self.target_config.entry(target)
530 .or_insert(Target::default());
531 target.ndk = Some(parse_configure_path(value));
533 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
534 let target = "i686-linux-android".to_string();
535 let target = self.target_config.entry(target)
536 .or_insert(Target::default());
537 target.ndk = Some(parse_configure_path(value));
539 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
540 let target = "aarch64-linux-android".to_string();
541 let target = self.target_config.entry(target)
542 .or_insert(Target::default());
543 target.ndk = Some(parse_configure_path(value));
545 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
546 let path = parse_configure_path(value);
547 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
548 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
550 "CFG_PYTHON" if value.len() > 0 => {
551 let path = parse_configure_path(value);
552 self.python = Some(path);
554 "CFG_ENABLE_CCACHE" if value == "1" => {
555 self.ccache = Some("ccache".to_string());
557 "CFG_ENABLE_SCCACHE" if value == "1" => {
558 self.ccache = Some("sccache".to_string());
560 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
561 self.configure_args = value.split_whitespace()
562 .map(|s| s.to_string())
570 pub fn verbose(&self) -> bool {
574 pub fn very_verbose(&self) -> bool {
580 fn parse_configure_path(path: &str) -> PathBuf {
585 fn parse_configure_path(path: &str) -> PathBuf {
586 // on windows, configure produces unix style paths e.g. /c/some/path but we
587 // only want real windows paths
589 use std::process::Command;
592 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
593 if !path.contains('/') {
597 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
598 let win_path = win_path.trim();
603 fn set<T>(field: &mut T, val: Option<T>) {
604 if let Some(v) = val {