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};
27 /// Global configuration for the entire build and/or bootstrap.
29 /// This structure is derived from a combination of both `config.toml` and
30 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
31 /// is used all that much, so this is primarily filled out by `config.mk` which
32 /// is generated from `./configure`.
34 /// Note that this structure is not decoded directly into, but rather it is
35 /// filled out from the decoded forms of the structs below. For documentation
36 /// each field, see the corresponding fields in
37 /// `src/bootstrap/config.toml.example`.
44 pub compiler_docs: bool,
46 pub target_config: HashMap<String, Target>,
48 // llvm codegen options
49 pub llvm_assertions: bool,
50 pub llvm_optimize: bool,
51 pub llvm_version_check: bool,
52 pub llvm_static_stdcpp: bool,
54 // rust codegen options
55 pub rust_optimize: bool,
56 pub rust_codegen_units: u32,
57 pub rust_debug_assertions: bool,
58 pub rust_debuginfo: bool,
60 pub rustc_default_linker: Option<String>,
61 pub rustc_default_ar: Option<String>,
62 pub rust_optimize_tests: bool,
63 pub rust_debuginfo_tests: bool,
66 pub host: Vec<String>,
67 pub target: Vec<String>,
68 pub rustc: Option<PathBuf>,
69 pub cargo: Option<PathBuf>,
70 pub local_rebuild: bool,
73 pub debug_jemalloc: bool,
74 pub use_jemalloc: bool,
78 pub musl_root: Option<PathBuf>,
79 pub prefix: Option<String>,
82 /// Per-target configuration stored in the global configuration structure.
85 pub llvm_config: Option<PathBuf>,
86 pub jemalloc: Option<PathBuf>,
87 pub cc: Option<PathBuf>,
88 pub cxx: Option<PathBuf>,
89 pub ndk: Option<PathBuf>,
92 /// Structure of the `config.toml` file that configuration is read from.
94 /// This structure uses `Decodable` to automatically decode a TOML configuration
95 /// file into this format, and then this is traversed and written into the above
96 /// `Config` structure.
97 #[derive(RustcDecodable, Default)]
102 target: Option<HashMap<String, TomlTarget>>,
105 /// TOML representation of various global build decisions.
106 #[derive(RustcDecodable, Default, Clone)]
108 build: Option<String>,
111 cargo: Option<String>,
112 rustc: Option<String>,
113 compiler_docs: Option<bool>,
117 /// TOML representation of how the LLVM build is configured.
118 #[derive(RustcDecodable, Default)]
120 ccache: Option<bool>,
122 assertions: Option<bool>,
123 optimize: Option<bool>,
124 version_check: Option<bool>,
125 static_libstdcpp: Option<bool>,
128 /// TOML representation of how the Rust build is configured.
129 #[derive(RustcDecodable, Default)]
131 optimize: Option<bool>,
132 codegen_units: Option<u32>,
133 debug_assertions: Option<bool>,
134 debuginfo: Option<bool>,
135 debug_jemalloc: Option<bool>,
136 use_jemalloc: Option<bool>,
137 default_linker: Option<String>,
138 default_ar: Option<String>,
139 channel: Option<String>,
140 musl_root: Option<String>,
142 optimize_tests: Option<bool>,
143 debuginfo_tests: Option<bool>,
146 /// TOML representation of how each build target is configured.
147 #[derive(RustcDecodable, Default)]
149 llvm_config: Option<String>,
150 jemalloc: Option<String>,
153 android_ndk: Option<String>,
157 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
158 let mut config = Config::default();
159 config.llvm_optimize = true;
160 config.use_jemalloc = true;
161 config.rust_optimize = true;
162 config.rust_optimize_tests = true;
163 config.submodules = true;
165 config.rust_rpath = true;
166 config.rust_codegen_units = 1;
167 config.build = build.to_string();
168 config.channel = "dev".to_string();
170 let toml = file.map(|file| {
171 let mut f = t!(File::open(&file));
172 let mut toml = String::new();
173 t!(f.read_to_string(&mut toml));
174 let mut p = Parser::new(&toml);
175 let table = match p.parse() {
176 Some(table) => table,
178 println!("failed to parse TOML configuration:");
179 for err in p.errors.iter() {
180 let (loline, locol) = p.to_linecol(err.lo);
181 let (hiline, hicol) = p.to_linecol(err.hi);
182 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
188 let mut d = Decoder::new(Value::Table(table));
189 match Decodable::decode(&mut d) {
192 println!("failed to decode TOML: {}", e);
196 }).unwrap_or_else(|| TomlConfig::default());
198 let build = toml.build.clone().unwrap_or(Build::default());
199 set(&mut config.build, build.build.clone());
200 config.host.push(config.build.clone());
201 for host in build.host.iter() {
202 if !config.host.contains(host) {
203 config.host.push(host.clone());
206 for target in config.host.iter().chain(&build.target) {
207 if !config.target.contains(target) {
208 config.target.push(target.clone());
211 config.rustc = build.rustc.map(PathBuf::from);
212 config.cargo = build.cargo.map(PathBuf::from);
213 set(&mut config.compiler_docs, build.compiler_docs);
214 set(&mut config.docs, build.docs);
216 if let Some(ref llvm) = toml.llvm {
217 set(&mut config.ccache, llvm.ccache);
218 set(&mut config.ninja, llvm.ninja);
219 set(&mut config.llvm_assertions, llvm.assertions);
220 set(&mut config.llvm_optimize, llvm.optimize);
221 set(&mut config.llvm_version_check, llvm.version_check);
222 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
224 if let Some(ref rust) = toml.rust {
225 set(&mut config.rust_debug_assertions, rust.debug_assertions);
226 set(&mut config.rust_debuginfo, rust.debuginfo);
227 set(&mut config.rust_optimize, rust.optimize);
228 set(&mut config.rust_optimize_tests, rust.optimize_tests);
229 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
230 set(&mut config.rust_rpath, rust.rpath);
231 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
232 set(&mut config.use_jemalloc, rust.use_jemalloc);
233 set(&mut config.channel, rust.channel.clone());
234 config.rustc_default_linker = rust.default_linker.clone();
235 config.rustc_default_ar = rust.default_ar.clone();
236 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
238 match rust.codegen_units {
239 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
240 Some(n) => config.rust_codegen_units = n,
245 if let Some(ref t) = toml.target {
246 for (triple, cfg) in t {
247 let mut target = Target::default();
249 if let Some(ref s) = cfg.llvm_config {
250 target.llvm_config = Some(env::current_dir().unwrap().join(s));
252 if let Some(ref s) = cfg.jemalloc {
253 target.jemalloc = Some(env::current_dir().unwrap().join(s));
255 if let Some(ref s) = cfg.android_ndk {
256 target.ndk = Some(env::current_dir().unwrap().join(s));
258 target.cxx = cfg.cxx.clone().map(PathBuf::from);
259 target.cc = cfg.cc.clone().map(PathBuf::from);
261 config.target_config.insert(triple.clone(), target);
268 /// "Temporary" routine to parse `config.mk` into this configuration.
270 /// While we still have `./configure` this implements the ability to decode
271 /// that configuration into this. This isn't exactly a full-blown makefile
272 /// parser, but hey it gets the job done!
273 pub fn update_with_config_mk(&mut self) {
274 let mut config = String::new();
275 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
276 for line in config.lines() {
277 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
278 let key = parts.next().unwrap();
279 let value = match parts.next() {
280 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
286 ($(($name:expr, $val:expr),)*) => {
289 if key == concat!("CFG_ENABLE_", $name) {
293 if key == concat!("CFG_DISABLE_", $name) {
303 ("CCACHE", self.ccache),
304 ("MANAGE_SUBMODULES", self.submodules),
305 ("COMPILER_DOCS", self.compiler_docs),
307 ("LLVM_ASSERTIONS", self.llvm_assertions),
308 ("OPTIMIZE_LLVM", self.llvm_optimize),
309 ("LLVM_VERSION_CHECK", self.llvm_version_check),
310 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
311 ("OPTIMIZE", self.rust_optimize),
312 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
313 ("DEBUGINFO", self.rust_debuginfo),
314 ("JEMALLOC", self.use_jemalloc),
315 ("DEBUG_JEMALLOC", self.debug_jemalloc),
316 ("RPATH", self.rust_rpath),
317 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
318 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
319 ("LOCAL_REBUILD", self.local_rebuild),
320 ("NINJA", self.ninja),
324 "CFG_BUILD" => self.build = value.to_string(),
326 self.host = value.split(" ").map(|s| s.to_string())
330 self.target = value.split(" ").map(|s| s.to_string())
333 "CFG_MUSL_ROOT" if value.len() > 0 => {
334 self.musl_root = Some(PathBuf::from(value));
336 "CFG_DEFAULT_AR" if value.len() > 0 => {
337 self.rustc_default_ar = Some(value.to_string());
339 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
340 self.rustc_default_linker = Some(value.to_string());
342 "CFG_RELEASE_CHANNEL" => {
343 self.channel = value.to_string();
346 self.prefix = Some(value.to_string());
348 "CFG_LLVM_ROOT" if value.len() > 0 => {
349 let target = self.target_config.entry(self.build.clone())
350 .or_insert(Target::default());
351 let root = PathBuf::from(value);
352 target.llvm_config = Some(root.join("bin/llvm-config"));
354 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
355 let target = self.target_config.entry(self.build.clone())
356 .or_insert(Target::default());
357 target.jemalloc = Some(PathBuf::from(value));
359 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
360 let target = "arm-linux-androideabi".to_string();
361 let target = self.target_config.entry(target)
362 .or_insert(Target::default());
363 target.ndk = Some(PathBuf::from(value));
365 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
366 let target = "armv7-linux-androideabi".to_string();
367 let target = self.target_config.entry(target)
368 .or_insert(Target::default());
369 target.ndk = Some(PathBuf::from(value));
371 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
372 let target = "i686-linux-android".to_string();
373 let target = self.target_config.entry(target)
374 .or_insert(Target::default());
375 target.ndk = Some(PathBuf::from(value));
377 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
378 let target = "aarch64-linux-android".to_string();
379 let target = self.target_config.entry(target)
380 .or_insert(Target::default());
381 target.ndk = Some(PathBuf::from(value));
383 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
384 self.rustc = Some(PathBuf::from(value).join("bin/rustc"));
385 self.cargo = Some(PathBuf::from(value).join("bin/cargo"));
393 fn set<T>(field: &mut T, val: Option<T>) {
394 if let Some(v) = val {