]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Unignore u128 test for stage 0,1
[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: Option<String>,
42     pub ninja: bool,
43     pub verbose: usize,
44     pub submodules: bool,
45     pub compiler_docs: bool,
46     pub docs: bool,
47     pub vendor: bool,
48     pub target_config: HashMap<String, Target>,
49     pub full_bootstrap: bool,
50     pub extended: bool,
51
52     // llvm codegen options
53     pub llvm_assertions: bool,
54     pub llvm_optimize: bool,
55     pub llvm_release_debuginfo: bool,
56     pub llvm_version_check: bool,
57     pub llvm_static_stdcpp: bool,
58     pub llvm_link_shared: bool,
59     pub llvm_targets: Option<String>,
60
61     // rust codegen options
62     pub rust_optimize: bool,
63     pub rust_codegen_units: u32,
64     pub rust_debug_assertions: bool,
65     pub rust_debuginfo: bool,
66     pub rust_debuginfo_lines: bool,
67     pub rust_debuginfo_only_std: bool,
68     pub rust_rpath: bool,
69     pub rustc_default_linker: Option<String>,
70     pub rustc_default_ar: Option<String>,
71     pub rust_optimize_tests: bool,
72     pub rust_debuginfo_tests: bool,
73
74     pub build: String,
75     pub host: Vec<String>,
76     pub target: Vec<String>,
77     pub rustc: Option<PathBuf>,
78     pub cargo: Option<PathBuf>,
79     pub local_rebuild: bool,
80
81     // dist misc
82     pub dist_sign_folder: Option<PathBuf>,
83     pub dist_upload_addr: Option<String>,
84     pub dist_gpg_password_file: Option<PathBuf>,
85
86     // libstd features
87     pub debug_jemalloc: bool,
88     pub use_jemalloc: bool,
89     pub backtrace: bool, // support for RUST_BACKTRACE
90
91     // misc
92     pub channel: String,
93     pub quiet_tests: bool,
94     // Fallback musl-root for all targets
95     pub musl_root: Option<PathBuf>,
96     pub prefix: Option<PathBuf>,
97     pub docdir: Option<PathBuf>,
98     pub libdir: Option<PathBuf>,
99     pub libdir_relative: Option<PathBuf>,
100     pub mandir: Option<PathBuf>,
101     pub codegen_tests: bool,
102     pub nodejs: Option<PathBuf>,
103     pub gdb: Option<PathBuf>,
104     pub python: Option<PathBuf>,
105     pub configure_args: Vec<String>,
106 }
107
108 /// Per-target configuration stored in the global configuration structure.
109 #[derive(Default)]
110 pub struct Target {
111     pub llvm_config: Option<PathBuf>,
112     pub jemalloc: Option<PathBuf>,
113     pub cc: Option<PathBuf>,
114     pub cxx: Option<PathBuf>,
115     pub ndk: Option<PathBuf>,
116     pub musl_root: Option<PathBuf>,
117 }
118
119 /// Structure of the `config.toml` file that configuration is read from.
120 ///
121 /// This structure uses `Decodable` to automatically decode a TOML configuration
122 /// file into this format, and then this is traversed and written into the above
123 /// `Config` structure.
124 #[derive(RustcDecodable, Default)]
125 struct TomlConfig {
126     build: Option<Build>,
127     install: Option<Install>,
128     llvm: Option<Llvm>,
129     rust: Option<Rust>,
130     target: Option<HashMap<String, TomlTarget>>,
131     dist: Option<Dist>,
132 }
133
134 /// TOML representation of various global build decisions.
135 #[derive(RustcDecodable, Default, Clone)]
136 struct Build {
137     build: Option<String>,
138     host: Vec<String>,
139     target: Vec<String>,
140     cargo: Option<String>,
141     rustc: Option<String>,
142     compiler_docs: Option<bool>,
143     docs: Option<bool>,
144     submodules: Option<bool>,
145     gdb: Option<String>,
146     vendor: Option<bool>,
147     nodejs: Option<String>,
148     python: Option<String>,
149     full_bootstrap: Option<bool>,
150     extended: Option<bool>,
151 }
152
153 /// TOML representation of various global install decisions.
154 #[derive(RustcDecodable, Default, Clone)]
155 struct Install {
156     prefix: Option<String>,
157     mandir: Option<String>,
158     docdir: Option<String>,
159     libdir: Option<String>,
160 }
161
162 /// TOML representation of how the LLVM build is configured.
163 #[derive(RustcDecodable, Default)]
164 struct Llvm {
165     ccache: Option<StringOrBool>,
166     ninja: Option<bool>,
167     assertions: Option<bool>,
168     optimize: Option<bool>,
169     release_debuginfo: Option<bool>,
170     version_check: Option<bool>,
171     static_libstdcpp: Option<bool>,
172     targets: Option<String>,
173 }
174
175 #[derive(RustcDecodable, Default, Clone)]
176 struct Dist {
177     sign_folder: Option<String>,
178     gpg_password_file: Option<String>,
179     upload_addr: Option<String>,
180 }
181
182 #[derive(RustcDecodable)]
183 enum StringOrBool {
184     String(String),
185     Bool(bool),
186 }
187
188 impl Default for StringOrBool {
189     fn default() -> StringOrBool {
190         StringOrBool::Bool(false)
191     }
192 }
193
194 /// TOML representation of how the Rust build is configured.
195 #[derive(RustcDecodable, Default)]
196 struct Rust {
197     optimize: Option<bool>,
198     codegen_units: Option<u32>,
199     debug_assertions: Option<bool>,
200     debuginfo: Option<bool>,
201     debuginfo_lines: Option<bool>,
202     debuginfo_only_std: Option<bool>,
203     debug_jemalloc: Option<bool>,
204     use_jemalloc: Option<bool>,
205     backtrace: Option<bool>,
206     default_linker: Option<String>,
207     default_ar: Option<String>,
208     channel: Option<String>,
209     musl_root: Option<String>,
210     rpath: Option<bool>,
211     optimize_tests: Option<bool>,
212     debuginfo_tests: Option<bool>,
213     codegen_tests: Option<bool>,
214 }
215
216 /// TOML representation of how each build target is configured.
217 #[derive(RustcDecodable, Default)]
218 struct TomlTarget {
219     llvm_config: Option<String>,
220     jemalloc: Option<String>,
221     cc: Option<String>,
222     cxx: Option<String>,
223     android_ndk: Option<String>,
224     musl_root: Option<String>,
225 }
226
227 impl Config {
228     pub fn parse(build: &str, file: Option<PathBuf>) -> Config {
229         let mut config = Config::default();
230         config.llvm_optimize = true;
231         config.use_jemalloc = true;
232         config.backtrace = true;
233         config.rust_optimize = true;
234         config.rust_optimize_tests = true;
235         config.submodules = true;
236         config.docs = true;
237         config.rust_rpath = true;
238         config.rust_codegen_units = 1;
239         config.build = build.to_string();
240         config.channel = "dev".to_string();
241         config.codegen_tests = true;
242
243         let toml = file.map(|file| {
244             let mut f = t!(File::open(&file));
245             let mut toml = String::new();
246             t!(f.read_to_string(&mut toml));
247             let mut p = Parser::new(&toml);
248             let table = match p.parse() {
249                 Some(table) => table,
250                 None => {
251                     println!("failed to parse TOML configuration:");
252                     for err in p.errors.iter() {
253                         let (loline, locol) = p.to_linecol(err.lo);
254                         let (hiline, hicol) = p.to_linecol(err.hi);
255                         println!("{}:{}-{}:{}: {}", loline, locol, hiline,
256                                  hicol, err.desc);
257                     }
258                     process::exit(2);
259                 }
260             };
261             let mut d = Decoder::new(Value::Table(table));
262             match Decodable::decode(&mut d) {
263                 Ok(cfg) => cfg,
264                 Err(e) => {
265                     println!("failed to decode TOML: {}", e);
266                     process::exit(2);
267                 }
268             }
269         }).unwrap_or_else(|| TomlConfig::default());
270
271         let build = toml.build.clone().unwrap_or(Build::default());
272         set(&mut config.build, build.build.clone());
273         config.host.push(config.build.clone());
274         for host in build.host.iter() {
275             if !config.host.contains(host) {
276                 config.host.push(host.clone());
277             }
278         }
279         for target in config.host.iter().chain(&build.target) {
280             if !config.target.contains(target) {
281                 config.target.push(target.clone());
282             }
283         }
284         config.rustc = build.rustc.map(PathBuf::from);
285         config.cargo = build.cargo.map(PathBuf::from);
286         config.nodejs = build.nodejs.map(PathBuf::from);
287         config.gdb = build.gdb.map(PathBuf::from);
288         config.python = build.python.map(PathBuf::from);
289         set(&mut config.compiler_docs, build.compiler_docs);
290         set(&mut config.docs, build.docs);
291         set(&mut config.submodules, build.submodules);
292         set(&mut config.vendor, build.vendor);
293         set(&mut config.full_bootstrap, build.full_bootstrap);
294         set(&mut config.extended, build.extended);
295
296         if let Some(ref install) = toml.install {
297             config.prefix = install.prefix.clone().map(PathBuf::from);
298             config.mandir = install.mandir.clone().map(PathBuf::from);
299             config.docdir = install.docdir.clone().map(PathBuf::from);
300             config.libdir = install.libdir.clone().map(PathBuf::from);
301         }
302
303         if let Some(ref llvm) = toml.llvm {
304             match llvm.ccache {
305                 Some(StringOrBool::String(ref s)) => {
306                     config.ccache = Some(s.to_string())
307                 }
308                 Some(StringOrBool::Bool(true)) => {
309                     config.ccache = Some("ccache".to_string());
310                 }
311                 Some(StringOrBool::Bool(false)) | None => {}
312             }
313             set(&mut config.ninja, llvm.ninja);
314             set(&mut config.llvm_assertions, llvm.assertions);
315             set(&mut config.llvm_optimize, llvm.optimize);
316             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
317             set(&mut config.llvm_version_check, llvm.version_check);
318             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
319             config.llvm_targets = llvm.targets.clone();
320         }
321
322         if let Some(ref rust) = toml.rust {
323             set(&mut config.rust_debug_assertions, rust.debug_assertions);
324             set(&mut config.rust_debuginfo, rust.debuginfo);
325             set(&mut config.rust_debuginfo_lines, rust.debuginfo_lines);
326             set(&mut config.rust_debuginfo_only_std, rust.debuginfo_only_std);
327             set(&mut config.rust_optimize, rust.optimize);
328             set(&mut config.rust_optimize_tests, rust.optimize_tests);
329             set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
330             set(&mut config.codegen_tests, rust.codegen_tests);
331             set(&mut config.rust_rpath, rust.rpath);
332             set(&mut config.debug_jemalloc, rust.debug_jemalloc);
333             set(&mut config.use_jemalloc, rust.use_jemalloc);
334             set(&mut config.backtrace, rust.backtrace);
335             set(&mut config.channel, rust.channel.clone());
336             config.rustc_default_linker = rust.default_linker.clone();
337             config.rustc_default_ar = rust.default_ar.clone();
338             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
339
340             match rust.codegen_units {
341                 Some(0) => config.rust_codegen_units = num_cpus::get() as u32,
342                 Some(n) => config.rust_codegen_units = n,
343                 None => {}
344             }
345         }
346
347         if let Some(ref t) = toml.target {
348             for (triple, cfg) in t {
349                 let mut target = Target::default();
350
351                 if let Some(ref s) = cfg.llvm_config {
352                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
353                 }
354                 if let Some(ref s) = cfg.jemalloc {
355                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
356                 }
357                 if let Some(ref s) = cfg.android_ndk {
358                     target.ndk = Some(env::current_dir().unwrap().join(s));
359                 }
360                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
361                 target.cc = cfg.cc.clone().map(PathBuf::from);
362                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
363
364                 config.target_config.insert(triple.clone(), target);
365             }
366         }
367
368         if let Some(ref t) = toml.dist {
369             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
370             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
371             config.dist_upload_addr = t.upload_addr.clone();
372         }
373
374         return config
375     }
376
377     /// "Temporary" routine to parse `config.mk` into this configuration.
378     ///
379     /// While we still have `./configure` this implements the ability to decode
380     /// that configuration into this. This isn't exactly a full-blown makefile
381     /// parser, but hey it gets the job done!
382     pub fn update_with_config_mk(&mut self) {
383         let mut config = String::new();
384         File::open("config.mk").unwrap().read_to_string(&mut config).unwrap();
385         for line in config.lines() {
386             let mut parts = line.splitn(2, ":=").map(|s| s.trim());
387             let key = parts.next().unwrap();
388             let value = match parts.next() {
389                 Some(n) if n.starts_with('\"') => &n[1..n.len() - 1],
390                 Some(n) => n,
391                 None => continue
392             };
393
394             macro_rules! check {
395                 ($(($name:expr, $val:expr),)*) => {
396                     if value == "1" {
397                         $(
398                             if key == concat!("CFG_ENABLE_", $name) {
399                                 $val = true;
400                                 continue
401                             }
402                             if key == concat!("CFG_DISABLE_", $name) {
403                                 $val = false;
404                                 continue
405                             }
406                         )*
407                     }
408                 }
409             }
410
411             check! {
412                 ("MANAGE_SUBMODULES", self.submodules),
413                 ("COMPILER_DOCS", self.compiler_docs),
414                 ("DOCS", self.docs),
415                 ("LLVM_ASSERTIONS", self.llvm_assertions),
416                 ("LLVM_RELEASE_DEBUGINFO", self.llvm_release_debuginfo),
417                 ("OPTIMIZE_LLVM", self.llvm_optimize),
418                 ("LLVM_VERSION_CHECK", self.llvm_version_check),
419                 ("LLVM_STATIC_STDCPP", self.llvm_static_stdcpp),
420                 ("LLVM_LINK_SHARED", self.llvm_link_shared),
421                 ("OPTIMIZE", self.rust_optimize),
422                 ("DEBUG_ASSERTIONS", self.rust_debug_assertions),
423                 ("DEBUGINFO", self.rust_debuginfo),
424                 ("DEBUGINFO_LINES", self.rust_debuginfo_lines),
425                 ("DEBUGINFO_ONLY_STD", self.rust_debuginfo_only_std),
426                 ("JEMALLOC", self.use_jemalloc),
427                 ("DEBUG_JEMALLOC", self.debug_jemalloc),
428                 ("RPATH", self.rust_rpath),
429                 ("OPTIMIZE_TESTS", self.rust_optimize_tests),
430                 ("DEBUGINFO_TESTS", self.rust_debuginfo_tests),
431                 ("QUIET_TESTS", self.quiet_tests),
432                 ("LOCAL_REBUILD", self.local_rebuild),
433                 ("NINJA", self.ninja),
434                 ("CODEGEN_TESTS", self.codegen_tests),
435                 ("VENDOR", self.vendor),
436                 ("FULL_BOOTSTRAP", self.full_bootstrap),
437                 ("EXTENDED", self.extended),
438             }
439
440             match key {
441                 "CFG_BUILD" => self.build = value.to_string(),
442                 "CFG_HOST" => {
443                     self.host = value.split(" ").map(|s| s.to_string())
444                                      .collect();
445                 }
446                 "CFG_TARGET" => {
447                     self.target = value.split(" ").map(|s| s.to_string())
448                                        .collect();
449                 }
450                 "CFG_MUSL_ROOT" if value.len() > 0 => {
451                     self.musl_root = Some(parse_configure_path(value));
452                 }
453                 "CFG_MUSL_ROOT_X86_64" if value.len() > 0 => {
454                     let target = "x86_64-unknown-linux-musl".to_string();
455                     let target = self.target_config.entry(target)
456                                      .or_insert(Target::default());
457                     target.musl_root = Some(parse_configure_path(value));
458                 }
459                 "CFG_MUSL_ROOT_I686" if value.len() > 0 => {
460                     let target = "i686-unknown-linux-musl".to_string();
461                     let target = self.target_config.entry(target)
462                                      .or_insert(Target::default());
463                     target.musl_root = Some(parse_configure_path(value));
464                 }
465                 "CFG_MUSL_ROOT_ARM" if value.len() > 0 => {
466                     let target = "arm-unknown-linux-musleabi".to_string();
467                     let target = self.target_config.entry(target)
468                                      .or_insert(Target::default());
469                     target.musl_root = Some(parse_configure_path(value));
470                 }
471                 "CFG_MUSL_ROOT_ARMHF" if value.len() > 0 => {
472                     let target = "arm-unknown-linux-musleabihf".to_string();
473                     let target = self.target_config.entry(target)
474                                      .or_insert(Target::default());
475                     target.musl_root = Some(parse_configure_path(value));
476                 }
477                 "CFG_MUSL_ROOT_ARMV7" if value.len() > 0 => {
478                     let target = "armv7-unknown-linux-musleabihf".to_string();
479                     let target = self.target_config.entry(target)
480                                      .or_insert(Target::default());
481                     target.musl_root = Some(parse_configure_path(value));
482                 }
483                 "CFG_DEFAULT_AR" if value.len() > 0 => {
484                     self.rustc_default_ar = Some(value.to_string());
485                 }
486                 "CFG_DEFAULT_LINKER" if value.len() > 0 => {
487                     self.rustc_default_linker = Some(value.to_string());
488                 }
489                 "CFG_GDB" if value.len() > 0 => {
490                     self.gdb = Some(parse_configure_path(value));
491                 }
492                 "CFG_RELEASE_CHANNEL" => {
493                     self.channel = value.to_string();
494                 }
495                 "CFG_PREFIX" => {
496                     self.prefix = Some(PathBuf::from(value));
497                 }
498                 "CFG_DOCDIR" => {
499                     self.docdir = Some(PathBuf::from(value));
500                 }
501                 "CFG_LIBDIR" => {
502                     self.libdir = Some(PathBuf::from(value));
503                 }
504                 "CFG_LIBDIR_RELATIVE" => {
505                     self.libdir_relative = Some(PathBuf::from(value));
506                 }
507                 "CFG_MANDIR" => {
508                     self.mandir = Some(PathBuf::from(value));
509                 }
510                 "CFG_LLVM_ROOT" if value.len() > 0 => {
511                     let target = self.target_config.entry(self.build.clone())
512                                      .or_insert(Target::default());
513                     let root = parse_configure_path(value);
514                     target.llvm_config = Some(push_exe_path(root, &["bin", "llvm-config"]));
515                 }
516                 "CFG_JEMALLOC_ROOT" if value.len() > 0 => {
517                     let target = self.target_config.entry(self.build.clone())
518                                      .or_insert(Target::default());
519                     target.jemalloc = Some(parse_configure_path(value).join("libjemalloc_pic.a"));
520                 }
521                 "CFG_ARM_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
522                     let target = "arm-linux-androideabi".to_string();
523                     let target = self.target_config.entry(target)
524                                      .or_insert(Target::default());
525                     target.ndk = Some(parse_configure_path(value));
526                 }
527                 "CFG_ARMV7_LINUX_ANDROIDEABI_NDK" if value.len() > 0 => {
528                     let target = "armv7-linux-androideabi".to_string();
529                     let target = self.target_config.entry(target)
530                                      .or_insert(Target::default());
531                     target.ndk = Some(parse_configure_path(value));
532                 }
533                 "CFG_I686_LINUX_ANDROID_NDK" if value.len() > 0 => {
534                     let target = "i686-linux-android".to_string();
535                     let target = self.target_config.entry(target)
536                                      .or_insert(Target::default());
537                     target.ndk = Some(parse_configure_path(value));
538                 }
539                 "CFG_AARCH64_LINUX_ANDROID_NDK" if value.len() > 0 => {
540                     let target = "aarch64-linux-android".to_string();
541                     let target = self.target_config.entry(target)
542                                      .or_insert(Target::default());
543                     target.ndk = Some(parse_configure_path(value));
544                 }
545                 "CFG_LOCAL_RUST_ROOT" if value.len() > 0 => {
546                     let path = parse_configure_path(value);
547                     self.rustc = Some(push_exe_path(path.clone(), &["bin", "rustc"]));
548                     self.cargo = Some(push_exe_path(path, &["bin", "cargo"]));
549                 }
550                 "CFG_PYTHON" if value.len() > 0 => {
551                     let path = parse_configure_path(value);
552                     self.python = Some(path);
553                 }
554                 "CFG_ENABLE_CCACHE" if value == "1" => {
555                     self.ccache = Some("ccache".to_string());
556                 }
557                 "CFG_ENABLE_SCCACHE" if value == "1" => {
558                     self.ccache = Some("sccache".to_string());
559                 }
560                 "CFG_CONFIGURE_ARGS" if value.len() > 0 => {
561                     self.configure_args = value.split_whitespace()
562                                                .map(|s| s.to_string())
563                                                .collect();
564                 }
565                 _ => {}
566             }
567         }
568     }
569
570     pub fn verbose(&self) -> bool {
571         self.verbose > 0
572     }
573
574     pub fn very_verbose(&self) -> bool {
575         self.verbose > 1
576     }
577 }
578
579 #[cfg(not(windows))]
580 fn parse_configure_path(path: &str) -> PathBuf {
581     path.into()
582 }
583
584 #[cfg(windows)]
585 fn parse_configure_path(path: &str) -> PathBuf {
586     // on windows, configure produces unix style paths e.g. /c/some/path but we
587     // only want real windows paths
588
589     use std::process::Command;
590     use build_helper;
591
592     // '/' is invalid in windows paths, so we can detect unix paths by the presence of it
593     if !path.contains('/') {
594         return path.into();
595     }
596
597     let win_path = build_helper::output(Command::new("cygpath").arg("-w").arg(path));
598     let win_path = win_path.trim();
599
600     win_path.into()
601 }
602
603 fn set<T>(field: &mut T, val: Option<T>) {
604     if let Some(v) = val {
605         *field = v;
606     }
607 }