]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Rollup merge of #35754 - QuietMisdreavus:must-use-reference, r=Manishearth
[rust.git] / src / bootstrap / config.rs
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.
4 //
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.
10
11 //! Serialized configuration of a build.
12 //!
13 //! This module implements parsing `config.mk` and `config.toml` configuration
14 //! files to tweak how the build runs.
15
16 use std::collections::HashMap;
17 use std::env;
18 use std::fs::File;
19 use std::io::prelude::*;
20 use std::path::PathBuf;
21 use std::process;
22
23 use num_cpus;
24 use rustc_serialize::Decodable;
25 use toml::{Parser, Decoder, Value};
26
27 /// Global configuration for the entire build and/or bootstrap.
28 ///
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`.
33 ///
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`.
38 #[derive(Default)]
39 pub struct Config {
40     pub ccache: bool,
41     pub ninja: bool,
42     pub verbose: bool,
43     pub submodules: bool,
44     pub compiler_docs: bool,
45     pub docs: bool,
46     pub target_config: HashMap<String, Target>,
47
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,
53
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,
59     pub rust_rpath: 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,
64
65     pub build: String,
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,
71
72     // libstd features
73     pub debug_jemalloc: bool,
74     pub use_jemalloc: bool,
75     pub backtrace: bool, // support for RUST_BACKTRACE
76
77     // misc
78     pub channel: String,
79     pub musl_root: Option<PathBuf>,
80     pub prefix: Option<String>,
81     pub codegen_tests: bool,
82 }
83
84 /// Per-target configuration stored in the global configuration structure.
85 #[derive(Default)]
86 pub struct Target {
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>,
92 }
93
94 /// Structure of the `config.toml` file that configuration is read from.
95 ///
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)]
100 struct TomlConfig {
101     build: Option<Build>,
102     llvm: Option<Llvm>,
103     rust: Option<Rust>,
104     target: Option<HashMap<String, TomlTarget>>,
105 }
106
107 /// TOML representation of various global build decisions.
108 #[derive(RustcDecodable, Default, Clone)]
109 struct Build {
110     build: Option<String>,
111     host: Vec<String>,
112     target: Vec<String>,
113     cargo: Option<String>,
114     rustc: Option<String>,
115     compiler_docs: Option<bool>,
116     docs: Option<bool>,
117 }
118
119 /// TOML representation of how the LLVM build is configured.
120 #[derive(RustcDecodable, Default)]
121 struct Llvm {
122     ccache: Option<bool>,
123     ninja: Option<bool>,
124     assertions: Option<bool>,
125     optimize: Option<bool>,
126     version_check: Option<bool>,
127     static_libstdcpp: Option<bool>,
128 }
129
130 /// TOML representation of how the Rust build is configured.
131 #[derive(RustcDecodable, Default)]
132 struct Rust {
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>,
144     rpath: Option<bool>,
145     optimize_tests: Option<bool>,
146     debuginfo_tests: Option<bool>,
147 }
148
149 /// TOML representation of how each build target is configured.
150 #[derive(RustcDecodable, Default)]
151 struct TomlTarget {
152     llvm_config: Option<String>,
153     jemalloc: Option<String>,
154     cc: Option<String>,
155     cxx: Option<String>,
156     android_ndk: Option<String>,
157 }
158
159 impl Config {
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;
168         config.docs = 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;
174
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,
182                 None => {
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,
188                                  hicol, err.desc);
189                     }
190                     process::exit(2);
191                 }
192             };
193             let mut d = Decoder::new(Value::Table(table));
194             match Decodable::decode(&mut d) {
195                 Ok(cfg) => cfg,
196                 Err(e) => {
197                     println!("failed to decode TOML: {}", e);
198                     process::exit(2);
199                 }
200             }
201         }).unwrap_or_else(|| TomlConfig::default());
202
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());
209             }
210         }
211         for target in config.host.iter().chain(&build.target) {
212             if !config.target.contains(target) {
213                 config.target.push(target.clone());
214             }
215         }
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);
220
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);
228         }
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);
243
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,
247                 None => {}
248             }
249         }
250
251         if let Some(ref t) = toml.target {
252             for (triple, cfg) in t {
253                 let mut target = Target::default();
254
255                 if let Some(ref s) = cfg.llvm_config {
256                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
257                 }
258                 if let Some(ref s) = cfg.jemalloc {
259                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
260                 }
261                 if let Some(ref s) = cfg.android_ndk {
262                     target.ndk = Some(env::current_dir().unwrap().join(s));
263                 }
264                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
265                 target.cc = cfg.cc.clone().map(PathBuf::from);
266
267                 config.target_config.insert(triple.clone(), target);
268             }
269         }
270
271         return config
272     }
273
274     /// "Temporary" routine to parse `config.mk` into this configuration.
275     ///
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],
287                 Some(n) => n,
288                 None => continue
289             };
290
291             macro_rules! check {
292                 ($(($name:expr, $val:expr),)*) => {
293                     if value == "1" {
294                         $(
295                             if key == concat!("CFG_ENABLE_", $name) {
296                                 $val = true;
297                                 continue
298                             }
299                             if key == concat!("CFG_DISABLE_", $name) {
300                                 $val = false;
301                                 continue
302                             }
303                         )*
304                     }
305                 }
306             }
307
308             check! {
309                 ("CCACHE", self.ccache),
310                 ("MANAGE_SUBMODULES", self.submodules),
311                 ("COMPILER_DOCS", self.compiler_docs),
312                 ("DOCS", self.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),
328             }
329
330             match key {
331                 "CFG_BUILD" => self.build = value.to_string(),
332                 "CFG_HOST" => {
333                     self.host = value.split(" ").map(|s| s.to_string())
334                                      .collect();
335                 }
336                 "CFG_TARGET" => {
337                     self.target = value.split(" ").map(|s| s.to_string())
338                                        .collect();
339                 }
340                 "CFG_MUSL_ROOT" if value.len() > 0 => {
341                     self.musl_root = Some(PathBuf::from(value));
342                 }
343                 "CFG_DEFAULT_AR" if value.len() > 0 => {
344                     self.rustc_default_ar = Some(value.to_string());
345                 }
346                 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
347                     self.rustc_default_linker = Some(value.to_string());
348                 }
349                 "CFG_RELEASE_CHANNEL" => {
350                     self.channel = value.to_string();
351                 }
352                 "CFG_PREFIX" => {
353                     self.prefix = Some(value.to_string());
354                 }
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"));
360                 }
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));
365                 }
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));
371                 }
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));
377                 }
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));
383                 }
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));
389                 }
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"));
393                 }
394                 _ => {}
395             }
396         }
397     }
398 }
399
400 fn set<T>(field: &mut T, val: Option<T>) {
401     if let Some(v) = val {
402         *field = v;
403     }
404 }