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,
75 pub backtrace: bool, // support for RUST_BACKTRACE
79 pub musl_root: Option<PathBuf>,
80 pub prefix: Option<String>,
81 pub codegen_tests: bool,
84 /// Per-target configuration stored in the global configuration structure.
87 pub llvm_config: Option<PathBuf>,
88 pub jemalloc: Option<PathBuf>,
89 pub cc: Option<PathBuf>,
90 pub cxx: Option<PathBuf>,
91 pub ndk: Option<PathBuf>,
94 /// Structure of the `config.toml` file that configuration is read from.
96 /// This structure uses `Decodable` to automatically decode a TOML configuration
97 /// file into this format, and then this is traversed and written into the above
98 /// `Config` structure.
99 #[derive(RustcDecodable, Default)]
101 build: Option<Build>,
104 target: Option<HashMap<String, TomlTarget>>,
107 /// TOML representation of various global build decisions.
108 #[derive(RustcDecodable, Default, Clone)]
110 build: Option<String>,
113 cargo: Option<String>,
114 rustc: Option<String>,
115 compiler_docs: Option<bool>,
119 /// TOML representation of how the LLVM build is configured.
120 #[derive(RustcDecodable, Default)]
122 ccache: Option<bool>,
124 assertions: Option<bool>,
125 optimize: Option<bool>,
126 version_check: Option<bool>,
127 static_libstdcpp: Option<bool>,
130 /// TOML representation of how the Rust build is configured.
131 #[derive(RustcDecodable, Default)]
133 optimize: Option<bool>,
134 codegen_units: Option<u32>,
135 debug_assertions: Option<bool>,
136 debuginfo: Option<bool>,
137 debug_jemalloc: Option<bool>,
138 use_jemalloc: Option<bool>,
139 backtrace: Option<bool>,
140 default_linker: Option<String>,
141 default_ar: Option<String>,
142 channel: Option<String>,
143 musl_root: Option<String>,
145 optimize_tests: Option<bool>,
146 debuginfo_tests: Option<bool>,
149 /// TOML representation of how each build target is configured.
150 #[derive(RustcDecodable, Default)]
152 llvm_config: Option<String>,
153 jemalloc: Option<String>,
156 android_ndk: Option<String>,
160 pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
161 let mut config = Config::default();
162 config.llvm_optimize = true;
163 config.use_jemalloc = true;
164 config.backtrace = true;
165 config.rust_optimize = true;
166 config.rust_optimize_tests = true;
167 config.submodules = true;
169 config.rust_rpath = true;
170 config.rust_codegen_units = 1;
171 config.build = build.to_string();
172 config.channel = "dev".to_string();
173 config.codegen_tests = true;
175 let toml = file.map(|file| {
176 let mut f = t!(File::open(&file));
177 let mut toml = String::new();
178 t!(f.read_to_string(&mut toml));
179 let mut p = Parser::new(&toml);
180 let table = match p.parse() {
181 Some(table) => table,
183 println!("failed to parse TOML configuration:");
184 for err in p.errors.iter() {
185 let (loline, locol) = p.to_linecol(err.lo);
186 let (hiline, hicol) = p.to_linecol(err.hi);
187 println!("{}:{}-{}:{}: {}", loline, locol, hiline,
193 let mut d = Decoder::new(Value::Table(table));
194 match Decodable::decode(&mut d) {
197 println!("failed to decode TOML: {}", e);
201 }).unwrap_or_else(|| TomlConfig::default());
203 let build = toml.build.clone().unwrap_or(Build::default());
204 set(&mut config.build, build.build.clone());
205 config.host.push(config.build.clone());
206 for host in build.host.iter() {
207 if !config.host.contains(host) {
208 config.host.push(host.clone());
211 for target in config.host.iter().chain(&build.target) {
212 if !config.target.contains(target) {
213 config.target.push(target.clone());
216 config.rustc = build.rustc.map(PathBuf::from);
217 config.cargo = build.cargo.map(PathBuf::from);
218 set(&mut config.compiler_docs, build.compiler_docs);
219 set(&mut config.docs, build.docs);
221 if let Some(ref llvm) = toml.llvm {
222 set(&mut config.ccache, llvm.ccache);
223 set(&mut config.ninja, llvm.ninja);
224 set(&mut config.llvm_assertions, llvm.assertions);
225 set(&mut config.llvm_optimize, llvm.optimize);
226 set(&mut config.llvm_version_check, llvm.version_check);
227 set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
229 if let Some(ref rust) = toml.rust {
230 set(&mut config.rust_debug_assertions, rust.debug_assertions);
231 set(&mut config.rust_debuginfo, rust.debuginfo);
232 set(&mut config.rust_optimize, rust.optimize);
233 set(&mut config.rust_optimize_tests, rust.optimize_tests);
234 set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
235 set(&mut config.rust_rpath, rust.rpath);
236 set(&mut config.debug_jemalloc, rust.debug_jemalloc);
237 set(&mut config.use_jemalloc, rust.use_jemalloc);
238 set(&mut config.backtrace, rust.backtrace);
239 set(&mut config.channel, rust.channel.clone());
240 config.rustc_default_linker = rust.default_linker.clone();
241 config.rustc_default_ar = rust.default_ar.clone();
242 config.musl_root = rust.musl_root.clone().map(PathBuf::from);
244 match rust.codegen_units {
245 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
246 Some(n) => config.rust_codegen_units = n,
251 if let Some(ref t) = toml.target {
252 for (triple, cfg) in t {
253 let mut target = Target::default();
255 if let Some(ref s) = cfg.llvm_config {
256 target.llvm_config = Some(env::current_dir().unwrap().join(s));
258 if let Some(ref s) = cfg.jemalloc {
259 target.jemalloc = Some(env::current_dir().unwrap().join(s));
261 if let Some(ref s) = cfg.android_ndk {
262 target.ndk = Some(env::current_dir().unwrap().join(s));
264 target.cxx = cfg.cxx.clone().map(PathBuf::from);
265 target.cc = cfg.cc.clone().map(PathBuf::from);
267 config.target_config.insert(triple.clone(), target);
274 /// "Temporary" routine to parse `config.mk` into this configuration.
276 /// While we still have `./configure` this implements the ability to decode
277 /// that configuration into this. This isn't exactly a full-blown makefile
278 /// parser, but hey it gets the job done!
279 pub fn update_with_config_mk(&mut self) {
280 let mut config = String::new();
281 File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
282 for line in config.lines() {
283 let mut parts = line.splitn(2, ":=").map(|s| s.trim());
284 let key = parts.next().unwrap();
285 let value = match parts.next() {
286 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
292 ($(($name:expr, $val:expr),)*) => {
295 if key == concat!("CFG_ENABLE_", $name) {
299 if key == concat!("CFG_DISABLE_", $name) {
309 ("CCACHE", self.ccache),
310 ("MANAGE_SUBMODULES", self.submodules),
311 ("COMPILER_DOCS", self.compiler_docs),
313 ("LLVM_ASSERTIONS", self.llvm_assertions),
314 ("OPTIMIZE_LLVM", self.llvm_optimize),
315 ("LLVM_VERSION_CHECK", self.llvm_version_check),
316 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
317 ("OPTIMIZE", self.rust_optimize),
318 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
319 ("DEBUGINFO", self.rust_debuginfo),
320 ("JEMALLOC", self.use_jemalloc),
321 ("DEBUG_JEMALLOC", self.debug_jemalloc),
322 ("RPATH", self.rust_rpath),
323 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
324 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
325 ("LOCAL_REBUILD", self.local_rebuild),
326 ("NINJA", self.ninja),
327 ("CODEGEN_TESTS", self.codegen_tests),
331 "CFG_BUILD" => self.build = value.to_string(),
333 self.host = value.split(" ").map(|s| s.to_string())
337 self.target = value.split(" ").map(|s| s.to_string())
340 "CFG_MUSL_ROOT" if value.len() > 0 => {
341 self.musl_root = Some(PathBuf::from(value));
343 "CFG_DEFAULT_AR" if value.len() > 0 => {
344 self.rustc_default_ar = Some(value.to_string());
346 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
347 self.rustc_default_linker = Some(value.to_string());
349 "CFG_RELEASE_CHANNEL" => {
350 self.channel = value.to_string();
353 self.prefix = Some(value.to_string());
355 "CFG_LLVM_ROOT" if value.len() > 0 => {
356 let target = self.target_config.entry(self.build.clone())
357 .or_insert(Target::default());
358 let root = PathBuf::from(value);
359 target.llvm_config = Some(root.join("bin/llvm-config"));
361 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
362 let target = self.target_config.entry(self.build.clone())
363 .or_insert(Target::default());
364 target.jemalloc = Some(PathBuf::from(value));
366 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
367 let target = "arm-linux-androideabi".to_string();
368 let target = self.target_config.entry(target)
369 .or_insert(Target::default());
370 target.ndk = Some(PathBuf::from(value));
372 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
373 let target = "armv7-linux-androideabi".to_string();
374 let target = self.target_config.entry(target)
375 .or_insert(Target::default());
376 target.ndk = Some(PathBuf::from(value));
378 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
379 let target = "i686-linux-android".to_string();
380 let target = self.target_config.entry(target)
381 .or_insert(Target::default());
382 target.ndk = Some(PathBuf::from(value));
384 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
385 let target = "aarch64-linux-android".to_string();
386 let target = self.target_config.entry(target)
387 .or_insert(Target::default());
388 target.ndk = Some(PathBuf::from(value));
390 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
391 self.rustc = Some(PathBuf::from(value).join("bin/rustc"));
392 self.cargo = Some(PathBuf::from(value).join("bin/cargo"));
400 fn set<T>(field: &mut T, val: Option<T>) {
401 if let Some(v) = val {