]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Rollup merge of #34853 - frewsxcv:vec-truncate, r=GuillaumeGomez
[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
76     // misc
77     pub channel: String,
78     pub musl_root: Option<PathBuf>,
79     pub prefix: Option<String>,
80 }
81
82 /// Per-target configuration stored in the global configuration structure.
83 #[derive(Default)]
84 pub struct Target {
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>,
90 }
91
92 /// Structure of the `config.toml` file that configuration is read from.
93 ///
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)]
98 struct TomlConfig {
99     build: Option<Build>,
100     llvm: Option<Llvm>,
101     rust: Option<Rust>,
102     target: Option<HashMap<String, TomlTarget>>,
103 }
104
105 /// TOML representation of various global build decisions.
106 #[derive(RustcDecodable, Default, Clone)]
107 struct Build {
108     build: Option<String>,
109     host: Vec<String>,
110     target: Vec<String>,
111     cargo: Option<String>,
112     rustc: Option<String>,
113     compiler_docs: Option<bool>,
114     docs: Option<bool>,
115 }
116
117 /// TOML representation of how the LLVM build is configured.
118 #[derive(RustcDecodable, Default)]
119 struct Llvm {
120     ccache: Option<bool>,
121     ninja: Option<bool>,
122     assertions: Option<bool>,
123     optimize: Option<bool>,
124     version_check: Option<bool>,
125     static_libstdcpp: Option<bool>,
126 }
127
128 /// TOML representation of how the Rust build is configured.
129 #[derive(RustcDecodable, Default)]
130 struct Rust {
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>,
141     rpath: Option<bool>,
142     optimize_tests: Option<bool>,
143     debuginfo_tests: Option<bool>,
144 }
145
146 /// TOML representation of how each build target is configured.
147 #[derive(RustcDecodable, Default)]
148 struct TomlTarget {
149     llvm_config: Option<String>,
150     jemalloc: Option<String>,
151     cc: Option<String>,
152     cxx: Option<String>,
153     android_ndk: Option<String>,
154 }
155
156 impl Config {
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;
164         config.docs = true;
165         config.rust_rpath = true;
166         config.rust_codegen_units = 1;
167         config.build = build.to_string();
168         config.channel = "dev".to_string();
169
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,
177                 None => {
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,
183                                  hicol, err.desc);
184                     }
185                     process::exit(2);
186                 }
187             };
188             let mut d = Decoder::new(Value::Table(table));
189             match Decodable::decode(&mut d) {
190                 Ok(cfg) => cfg,
191                 Err(e) => {
192                     println!("failed to decode TOML: {}", e);
193                     process::exit(2);
194                 }
195             }
196         }).unwrap_or_else(|| TomlConfig::default());
197
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());
204             }
205         }
206         for target in config.host.iter().chain(&build.target) {
207             if !config.target.contains(target) {
208                 config.target.push(target.clone());
209             }
210         }
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);
215
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);
223         }
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);
237
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,
241                 None => {}
242             }
243         }
244
245         if let Some(ref t) = toml.target {
246             for (triple, cfg) in t {
247                 let mut target = Target::default();
248
249                 if let Some(ref s) = cfg.llvm_config {
250                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
251                 }
252                 if let Some(ref s) = cfg.jemalloc {
253                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
254                 }
255                 if let Some(ref s) = cfg.android_ndk {
256                     target.ndk = Some(env::current_dir().unwrap().join(s));
257                 }
258                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
259                 target.cc = cfg.cc.clone().map(PathBuf::from);
260
261                 config.target_config.insert(triple.clone(), target);
262             }
263         }
264
265         return config
266     }
267
268     /// "Temporary" routine to parse `config.mk` into this configuration.
269     ///
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],
281                 Some(n) => n,
282                 None => continue
283             };
284
285             macro_rules! check {
286                 ($(($name:expr, $val:expr),)*) => {
287                     if value == "1" {
288                         $(
289                             if key == concat!("CFG_ENABLE_", $name) {
290                                 $val = true;
291                                 continue
292                             }
293                             if key == concat!("CFG_DISABLE_", $name) {
294                                 $val = false;
295                                 continue
296                             }
297                         )*
298                     }
299                 }
300             }
301
302             check! {
303                 ("CCACHE", self.ccache),
304                 ("MANAGE_SUBMODULES", self.submodules),
305                 ("COMPILER_DOCS", self.compiler_docs),
306                 ("DOCS", self.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),
321             }
322
323             match key {
324                 "CFG_BUILD" => self.build = value.to_string(),
325                 "CFG_HOST" => {
326                     self.host = value.split(" ").map(|s| s.to_string())
327                                      .collect();
328                 }
329                 "CFG_TARGET" => {
330                     self.target = value.split(" ").map(|s| s.to_string())
331                                        .collect();
332                 }
333                 "CFG_MUSL_ROOT" if value.len() > 0 => {
334                     self.musl_root = Some(PathBuf::from(value));
335                 }
336                 "CFG_DEFAULT_AR" if value.len() > 0 => {
337                     self.rustc_default_ar = Some(value.to_string());
338                 }
339                 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
340                     self.rustc_default_linker = Some(value.to_string());
341                 }
342                 "CFG_RELEASE_CHANNEL" => {
343                     self.channel = value.to_string();
344                 }
345                 "CFG_PREFIX" => {
346                     self.prefix = Some(value.to_string());
347                 }
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"));
353                 }
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));
358                 }
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));
364                 }
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));
370                 }
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));
376                 }
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));
382                 }
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"));
386                 }
387                 _ => {}
388             }
389         }
390     }
391 }
392
393 fn set<T>(field: &mut T, val: Option<T>) {
394     if let Some(v) = val {
395         *field = v;
396     }
397 }