]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
bb05b75a3fc2cf8ff02c798b75ae5cb9ff1ff432
[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 use util::push_exe_path;
27
28 /// Global configuration for the entire build and/or bootstrap.
29 ///
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`.
34 ///
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`.
39 #[derive(Default)]
40 pub struct Config {
41     pub ccache: bool,
42     pub ninja: bool,
43     pub verbose: bool,
44     pub submodules: bool,
45     pub compiler_docs: bool,
46     pub docs: bool,
47     pub target_config: HashMap<String, Target>,
48
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,
54
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,
61     pub rust_rpath: 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,
66
67     pub build: String,
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,
73
74     // libstd features
75     pub debug_jemalloc: bool,
76     pub use_jemalloc: bool,
77     pub backtrace: bool, // support for RUST_BACKTRACE
78
79     // misc
80     pub channel: String,
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>,
91 }
92
93 /// Per-target configuration stored in the global configuration structure.
94 #[derive(Default)]
95 pub struct Target {
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>,
102 }
103
104 /// Structure of the `config.toml` file that configuration is read from.
105 ///
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)]
110 struct TomlConfig {
111     build: Option<Build>,
112     llvm: Option<Llvm>,
113     rust: Option<Rust>,
114     target: Option<HashMap<String, TomlTarget>>,
115 }
116
117 /// TOML representation of various global build decisions.
118 #[derive(RustcDecodable, Default, Clone)]
119 struct Build {
120     build: Option<String>,
121     host: Vec<String>,
122     target: Vec<String>,
123     cargo: Option<String>,
124     rustc: Option<String>,
125     compiler_docs: Option<bool>,
126     docs: Option<bool>,
127     submodules: Option<bool>,
128     gdb: Option<String>,
129 }
130
131 /// TOML representation of how the LLVM build is configured.
132 #[derive(RustcDecodable, Default)]
133 struct Llvm {
134     ccache: Option<bool>,
135     ninja: Option<bool>,
136     assertions: Option<bool>,
137     optimize: Option<bool>,
138     version_check: Option<bool>,
139     static_libstdcpp: Option<bool>,
140 }
141
142 /// TOML representation of how the Rust build is configured.
143 #[derive(RustcDecodable, Default)]
144 struct Rust {
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>,
157     rpath: Option<bool>,
158     optimize_tests: Option<bool>,
159     debuginfo_tests: Option<bool>,
160     codegen_tests: Option<bool>,
161 }
162
163 /// TOML representation of how each build target is configured.
164 #[derive(RustcDecodable, Default)]
165 struct TomlTarget {
166     llvm_config: Option<String>,
167     jemalloc: Option<String>,
168     cc: Option<String>,
169     cxx: Option<String>,
170     android_ndk: Option<String>,
171     musl_root: Option<String>,
172 }
173
174 impl Config {
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;
183         config.docs = 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;
189
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,
197                 None => {
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,
203                                  hicol, err.desc);
204                     }
205                     process::exit(2);
206                 }
207             };
208             let mut d = Decoder::new(Value::Table(table));
209             match Decodable::decode(&mut d) {
210                 Ok(cfg) => cfg,
211                 Err(e) => {
212                     println!("failed to decode TOML: {}", e);
213                     process::exit(2);
214                 }
215             }
216         }).unwrap_or_else(|| TomlConfig::default());
217
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());
224             }
225         }
226         for target in config.host.iter().chain(&build.target) {
227             if !config.target.contains(target) {
228                 config.target.push(target.clone());
229             }
230         }
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);
237
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);
245         }
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);
262
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,
266                 None => {}
267             }
268         }
269
270         if let Some(ref t) = toml.target {
271             for (triple, cfg) in t {
272                 let mut target = Target::default();
273
274                 if let Some(ref s) = cfg.llvm_config {
275                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
276                 }
277                 if let Some(ref s) = cfg.jemalloc {
278                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
279                 }
280                 if let Some(ref s) = cfg.android_ndk {
281                     target.ndk = Some(env::current_dir().unwrap().join(s));
282                 }
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);
286
287                 config.target_config.insert(triple.clone(), target);
288             }
289         }
290
291         return config
292     }
293
294     /// "Temporary" routine to parse `config.mk` into this configuration.
295     ///
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],
307                 Some(n) => n,
308                 None => continue
309             };
310
311             macro_rules! check {
312                 ($(($name:expr, $val:expr),)*) => {
313                     if value == "1" {
314                         $(
315                             if key == concat!("CFG_ENABLE_", $name) {
316                                 $val = true;
317                                 continue
318                             }
319                             if key == concat!("CFG_DISABLE_", $name) {
320                                 $val = false;
321                                 continue
322                             }
323                         )*
324                     }
325                 }
326             }
327
328             check! {
329                 ("CCACHE", self.ccache),
330                 ("MANAGE_SUBMODULES", self.submodules),
331                 ("COMPILER_DOCS", self.compiler_docs),
332                 ("DOCS", self.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),
350             }
351
352             match key {
353                 "CFG_BUILD" => self.build = value.to_string(),
354                 "CFG_HOST" => {
355                     self.host = value.split(" ").map(|s| s.to_string())
356                                      .collect();
357                 }
358                 "CFG_TARGET" => {
359                     self.target = value.split(" ").map(|s| s.to_string())
360                                        .collect();
361                 }
362                 "CFG_MUSL_ROOT" if value.len() > 0 => {
363                     self.musl_root = Some(parse_configure_path(value));
364                 }
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));
370                 }
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));
376                 }
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));
382                 }
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));
388                 }
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));
394                 }
395                 "CFG_DEFAULT_AR" if value.len() > 0 => {
396                     self.rustc_default_ar = Some(value.to_string());
397                 }
398                 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
399                     self.rustc_default_linker = Some(value.to_string());
400                 }
401                 "CFG_GDB" if value.len() > 0 => {
402                     self.gdb = Some(parse_configure_path(value));
403                 }
404                 "CFG_RELEASE_CHANNEL" => {
405                     self.channel = value.to_string();
406                 }
407                 "CFG_PREFIX" => {
408                     self.prefix = Some(value.to_string());
409                 }
410                 "CFG_DOCDIR" => {
411                     self.docdir = Some(value.to_string());
412                 }
413                 "CFG_LIBDIR" => {
414                     self.libdir = Some(value.to_string());
415                 }
416                 "CFG_MANDIR" => {
417                     self.mandir = Some(value.to_string());
418                 }
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"]));
424                 }
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));
429                 }
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));
435                 }
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));
441                 }
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));
447                 }
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));
453                 }
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"]));
458                 }
459                 _ => {}
460             }
461         }
462     }
463 }
464
465 #[cfg(not(windows))]
466 fn parse_configure_path(path: &str) -> PathBuf {
467     path.into()
468 }
469
470 #[cfg(windows)]
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
474
475     use std::process::Command;
476     use build_helper;
477
478     // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
479     if !path.contains('/') {
480         return path.into();
481     }
482
483     let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
484     let win_path = win_path.trim();
485
486     win_path.into()
487 }
488
489 fn set<T>(field: &mut T, val: Option<T>) {
490     if let Some(v) = val {
491         *field = v;
492     }
493 }