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`.
45 pub compiler_docs: bool,
47 pub target_config: HashMap<String, Target>,
49 // llvm codegen options
50 pub llvm_assertions: bool,
51 pub llvm_optimize: bool,
52 pub llvm_version_check: bool,
53 pub llvm_static_stdcpp: bool,
55 // rust codegen options
56 pub rust_optimize: bool,
57 pub rust_codegen_units: u32,
58 pub rust_debug_assertions: bool,
59 pub rust_debuginfo: bool,
60 pub rust_debuginfo_lines: bool,
62 pub rustc_default_linker: Option<String>,
63 pub rustc_default_ar: Option<String>,
64 pub rust_optimize_tests: bool,
65 pub rust_debuginfo_tests: bool,
68 pub host: Vec<String>,
69 pub target: Vec<String>,
70 pub rustc: Option<PathBuf>,
71 pub cargo: Option<PathBuf>,
72 pub local_rebuild: bool,
75 pub debug_jemalloc: bool,
76 pub use_jemalloc: bool,
77 pub backtrace: bool, // support for RUST_BACKTRACE
81 pub quiet_tests: bool,
82 // Fallback musl-root for all targets
83 pub musl_root: Option<PathBuf>,
84 pub prefix: Option<String>,
85 pub docdir: Option<String>,
86 pub libdir: Option<String>,
87 pub mandir: Option<String>,
88 pub codegen_tests: bool,
89 pub nodejs: Option<PathBuf>,
90 pub gdb: Option<PathBuf>,
93 /// Per-target configuration stored in the global configuration structure.
96 pub llvm_config: Option<PathBuf>,
97 pub jemalloc: Option<PathBuf>,
98 pub cc: Option<PathBuf>,
99 pub cxx: Option<PathBuf>,
100 pub ndk: Option<PathBuf>,
101 pub musl_root: Option<PathBuf>,
104 /// Structure of the `config.toml` file that configuration is read from.
106 /// This structure uses `Decodable` to automatically decode a TOML configuration
107 /// file into this format, and then this is traversed and written into the above
108 /// `Config` structure.
109 #[derive(RustcDecodable, Default)]
111 build: Option<Build>,
114 target: Option<HashMap<String, TomlTarget>>,
117 /// TOML representation of various global build decisions.
118 #[derive(RustcDecodable, Default, Clone)]
120 build: Option<String>,
123 cargo: Option<String>,
124 rustc: Option<String>,
125 compiler_docs: Option<bool>,
127 submodules: Option<bool>,
131 /// TOML representation of how the LLVM build is configured.
132 #[derive(RustcDecodable, Default)]
134 ccache: Option<bool>,
136 assertions: Option<bool>,
137 optimize: Option<bool>,
138 version_check: Option<bool>,
139 static_libstdcpp: Option<bool>,
142 /// TOML representation of how the Rust build is configured.
143 #[derive(RustcDecodable, Default)]
145 optimize: Option<bool>,
146 codegen_units: Option<u32>,
147 debug_assertions: Option<bool>,
148 debuginfo: Option<bool>,
149 debuginfo_lines: Option<bool>,
150 debug_jemalloc: Option<bool>,
151 use_jemalloc: Option<bool>,
152 backtrace: Option<bool>,
153 default_linker: Option<String>,
154 default_ar: Option<String>,
155 channel: Option<String>,
156 musl_root: Option<String>,
158 optimize_tests: Option<bool>,
159 debuginfo_tests: Option<bool>,
160 codegen_tests: Option<bool>,
163 /// TOML representation of how each build target is configured.
164 #[derive(RustcDecodable, Default)]
166 llvm_config: Option<String>,
167 jemalloc: Option<String>,
170 android_ndk: Option<String>,
171 musl_root: Option<String>,
175 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
176 let mut config = Config::default();
177 config.llvm_optimize = true;
178 config.use_jemalloc = true;
179 config.backtrace = true;
180 config.rust_optimize = true;
181 config.rust_optimize_tests = true;
182 config.submodules = true;
184 config.rust_rpath = true;
185 config.rust_codegen_units = 1;
186 config.build = build.to_string();
187 config.channel = "dev".to_string();
188 config.codegen_tests = true;
190 let toml = file.map(|file| {
191 let mut f = t!(File::open(&file));
192 let mut toml = String::new();
193 t!(f.read_to_string(&mut toml));
194 let mut p = Parser::new(&toml);
195 let table = match p.parse() {
196 Some(table) => table,
198 println!("failed to parse TOML configuration:");
199 for err in p.errors.iter() {
200 let (loline, locol) = p.to_linecol(err.lo);
201 let (hiline, hicol) = p.to_linecol(err.hi);
202 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
208 let mut d = Decoder::new(Value::Table(table));
209 match Decodable::decode(&mut d) {
212 println!("failed to decode TOML: {}", e);
216 }).unwrap_or_else(|| TomlConfig::default());
218 let build = toml.build.clone().unwrap_or(Build::default());
219 set(&mut config.build, build.build.clone());
220 config.host.push(config.build.clone());
221 for host in build.host.iter() {
222 if !config.host.contains(host) {
223 config.host.push(host.clone());
226 for target in config.host.iter().chain(&build.target) {
227 if !config.target.contains(target) {
228 config.target.push(target.clone());
231 config.rustc = build.rustc.map(PathBuf::from);
232 config.cargo = build.cargo.map(PathBuf::from);
233 config.gdb = build.gdb.map(PathBuf::from);
234 set(&mut config.compiler_docs, build.compiler_docs);
235 set(&mut config.docs, build.docs);
236 set(&mut config.submodules, build.submodules);
238 if let Some(ref llvm) = toml.llvm {
239 set(&mut config.ccache, llvm.ccache);
240 set(&mut config.ninja, llvm.ninja);
241 set(&mut config.llvm_assertions, llvm.assertions);
242 set(&mut config.llvm_optimize, llvm.optimize);
243 set(&mut config.llvm_version_check, llvm.version_check);
244 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
246 if let Some(ref rust) = toml.rust {
247 set(&mut config.rust_debug_assertions, rust.debug_assertions);
248 set(&mut config.rust_debuginfo, rust.debuginfo);
249 set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
250 set(&mut config.rust_optimize, rust.optimize);
251 set(&mut config.rust_optimize_tests, rust.optimize_tests);
252 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
253 set(&mut config.codegen_tests, rust.codegen_tests);
254 set(&mut config.rust_rpath, rust.rpath);
255 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
256 set(&mut config.use_jemalloc, rust.use_jemalloc);
257 set(&mut config.backtrace, rust.backtrace);
258 set(&mut config.channel, rust.channel.clone());
259 config.rustc_default_linker = rust.default_linker.clone();
260 config.rustc_default_ar = rust.default_ar.clone();
261 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
263 match rust.codegen_units {
264 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
265 Some(n) => config.rust_codegen_units = n,
270 if let Some(ref t) = toml.target {
271 for (triple, cfg) in t {
272 let mut target = Target::default();
274 if let Some(ref s) = cfg.llvm_config {
275 target.llvm_config = Some(env::current_dir().unwrap().join(s));
277 if let Some(ref s) = cfg.jemalloc {
278 target.jemalloc = Some(env::current_dir().unwrap().join(s));
280 if let Some(ref s) = cfg.android_ndk {
281 target.ndk = Some(env::current_dir().unwrap().join(s));
283 target.cxx = cfg.cxx.clone().map(PathBuf::from);
284 target.cc = cfg.cc.clone().map(PathBuf::from);
285 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
287 config.target_config.insert(triple.clone(), target);
294 /// "Temporary" routine to parse `config.mk` into this configuration.
296 /// While we still have `./configure` this implements the ability to decode
297 /// that configuration into this. This isn't exactly a full-blown makefile
298 /// parser, but hey it gets the job done!
299 pub fn update_with_config_mk(&mut self) {
300 let mut config = String::new();
301 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
302 for line in config.lines() {
303 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
304 let key = parts.next().unwrap();
305 let value = match parts.next() {
306 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
312 ($(($name:expr, $val:expr),)*) => {
315 if key == concat!("CFG_ENABLE_", $name) {
319 if key == concat!("CFG_DISABLE_", $name) {
329 ("CCACHE", self.ccache),
330 ("MANAGE_SUBMODULES", self.submodules),
331 ("COMPILER_DOCS", self.compiler_docs),
333 ("LLVM_ASSERTIONS", self.llvm_assertions),
334 ("OPTIMIZE_LLVM", self.llvm_optimize),
335 ("LLVM_VERSION_CHECK", self.llvm_version_check),
336 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
337 ("OPTIMIZE", self.rust_optimize),
338 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
339 ("DEBUGINFO", self.rust_debuginfo),
340 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
341 ("JEMALLOC", self.use_jemalloc),
342 ("DEBUG_JEMALLOC", self.debug_jemalloc),
343 ("RPATH", self.rust_rpath),
344 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
345 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
346 ("QUIET_TESTS", self.quiet_tests),
347 ("LOCAL_REBUILD", self.local_rebuild),
348 ("NINJA", self.ninja),
349 ("CODEGEN_TESTS", self.codegen_tests),
353 "CFG_BUILD" => self.build = value.to_string(),
355 self.host = value.split(" ").map(|s| s.to_string())
359 self.target = value.split(" ").map(|s| s.to_string())
362 "CFG_MUSL_ROOT" if value.len() > 0 => {
363 self.musl_root = Some(parse_configure_path(value));
365 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
366 let target = "x86_64-unknown-linux-musl".to_string();
367 let target = self.target_config.entry(target)
368 .or_insert(Target::default());
369 target.musl_root = Some(parse_configure_path(value));
371 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
372 let target = "i686-unknown-linux-musl".to_string();
373 let target = self.target_config.entry(target)
374 .or_insert(Target::default());
375 target.musl_root = Some(parse_configure_path(value));
377 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
378 let target = "arm-unknown-linux-musleabi".to_string();
379 let target = self.target_config.entry(target)
380 .or_insert(Target::default());
381 target.musl_root = Some(parse_configure_path(value));
383 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
384 let target = "arm-unknown-linux-musleabihf".to_string();
385 let target = self.target_config.entry(target)
386 .or_insert(Target::default());
387 target.musl_root = Some(parse_configure_path(value));
389 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
390 let target = "armv7-unknown-linux-musleabihf".to_string();
391 let target = self.target_config.entry(target)
392 .or_insert(Target::default());
393 target.musl_root = Some(parse_configure_path(value));
395 "CFG_DEFAULT_AR" if value.len() > 0 => {
396 self.rustc_default_ar = Some(value.to_string());
398 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
399 self.rustc_default_linker = Some(value.to_string());
401 "CFG_GDB" if value.len() > 0 => {
402 self.gdb = Some(parse_configure_path(value));
404 "CFG_RELEASE_CHANNEL" => {
405 self.channel = value.to_string();
408 self.prefix = Some(value.to_string());
411 self.docdir = Some(value.to_string());
414 self.libdir = Some(value.to_string());
417 self.mandir = Some(value.to_string());
419 "CFG_LLVM_ROOT" if value.len() > 0 => {
420 let target = self.target_config.entry(self.build.clone())
421 .or_insert(Target::default());
422 let root = parse_configure_path(value);
423 target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
425 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
426 let target = self.target_config.entry(self.build.clone())
427 .or_insert(Target::default());
428 target.jemalloc = Some(parse_configure_path(value));
430 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
431 let target = "arm-linux-androideabi".to_string();
432 let target = self.target_config.entry(target)
433 .or_insert(Target::default());
434 target.ndk = Some(parse_configure_path(value));
436 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
437 let target = "armv7-linux-androideabi".to_string();
438 let target = self.target_config.entry(target)
439 .or_insert(Target::default());
440 target.ndk = Some(parse_configure_path(value));
442 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
443 let target = "i686-linux-android".to_string();
444 let target = self.target_config.entry(target)
445 .or_insert(Target::default());
446 target.ndk = Some(parse_configure_path(value));
448 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
449 let target = "aarch64-linux-android".to_string();
450 let target = self.target_config.entry(target)
451 .or_insert(Target::default());
452 target.ndk = Some(parse_configure_path(value));
454 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
455 let path = parse_configure_path(value);
456 self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
457 self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
466 fn parse_configure_path(path: &str) -> PathBuf {
471 fn parse_configure_path(path: &str) -> PathBuf {
472 // on windows, configure produces unix style paths e.g. /c/some/path but we
473 // only want real windows paths
475 use std::process::Command;
478 // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
479 if !path.contains('/') {
483 let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
484 let win_path = win_path.trim();
489 fn set<T>(field: &mut T, val: Option<T>) {
490 if let Some(v) = val {