]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/config.rs
Auto merge of #47203 - varkor:output-filename-conflicts-with-directory, r=estebank
[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.toml` configuration files to tweak
14 //! 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 use std::cmp;
23
24 use num_cpus;
25 use toml;
26 use util::exe;
27 use cache::{INTERNER, Interned};
28 use flags::Flags;
29 pub use flags::Subcommand;
30
31 /// Global configuration for the entire build and/or bootstrap.
32 ///
33 /// This structure is derived from a combination of both `config.toml` and
34 /// `config.mk`. As of the time of this writing it's unlikely that `config.toml`
35 /// is used all that much, so this is primarily filled out by `config.mk` which
36 /// is generated from `./configure`.
37 ///
38 /// Note that this structure is not decoded directly into, but rather it is
39 /// filled out from the decoded forms of the structs below. For documentation
40 /// each field, see the corresponding fields in
41 /// `config.toml.example`.
42 #[derive(Default)]
43 pub struct Config {
44     pub ccache: Option<String>,
45     pub ninja: bool,
46     pub verbose: usize,
47     pub submodules: bool,
48     pub compiler_docs: bool,
49     pub docs: bool,
50     pub locked_deps: bool,
51     pub vendor: bool,
52     pub target_config: HashMap<Interned<String>, Target>,
53     pub full_bootstrap: bool,
54     pub extended: bool,
55     pub sanitizers: bool,
56     pub profiler: bool,
57     pub ignore_git: bool,
58
59     pub run_host_only: bool,
60
61     pub on_fail: Option<String>,
62     pub stage: Option<u32>,
63     pub keep_stage: Option<u32>,
64     pub src: PathBuf,
65     pub jobs: Option<u32>,
66     pub cmd: Subcommand,
67     pub incremental: bool,
68
69     // llvm codegen options
70     pub llvm_enabled: bool,
71     pub llvm_assertions: bool,
72     pub llvm_optimize: bool,
73     pub llvm_release_debuginfo: bool,
74     pub llvm_version_check: bool,
75     pub llvm_static_stdcpp: bool,
76     pub llvm_link_shared: bool,
77     pub llvm_targets: Option<String>,
78     pub llvm_experimental_targets: String,
79     pub llvm_link_jobs: Option<u32>,
80
81     // rust codegen options
82     pub rust_optimize: bool,
83     pub rust_codegen_units: Option<u32>,
84     pub rust_thinlto: bool,
85     pub rust_debug_assertions: bool,
86     pub rust_debuginfo: bool,
87     pub rust_debuginfo_lines: bool,
88     pub rust_debuginfo_only_std: bool,
89     pub rust_rpath: bool,
90     pub rustc_parallel_queries: bool,
91     pub rustc_default_linker: Option<String>,
92     pub rust_optimize_tests: bool,
93     pub rust_debuginfo_tests: bool,
94     pub rust_dist_src: bool,
95     pub rust_codegen_backends: Vec<Interned<String>>,
96
97     pub build: Interned<String>,
98     pub hosts: Vec<Interned<String>>,
99     pub targets: Vec<Interned<String>>,
100     pub local_rebuild: bool,
101
102     // dist misc
103     pub dist_sign_folder: Option<PathBuf>,
104     pub dist_upload_addr: Option<String>,
105     pub dist_gpg_password_file: Option<PathBuf>,
106
107     // libstd features
108     pub debug_jemalloc: bool,
109     pub use_jemalloc: bool,
110     pub backtrace: bool, // support for RUST_BACKTRACE
111     pub wasm_syscall: bool,
112
113     // misc
114     pub low_priority: bool,
115     pub channel: String,
116     pub quiet_tests: bool,
117     pub test_miri: bool,
118     pub save_toolstates: Option<PathBuf>,
119
120     // Fallback musl-root for all targets
121     pub musl_root: Option<PathBuf>,
122     pub prefix: Option<PathBuf>,
123     pub sysconfdir: Option<PathBuf>,
124     pub docdir: Option<PathBuf>,
125     pub bindir: Option<PathBuf>,
126     pub libdir: Option<PathBuf>,
127     pub mandir: Option<PathBuf>,
128     pub codegen_tests: bool,
129     pub nodejs: Option<PathBuf>,
130     pub gdb: Option<PathBuf>,
131     pub python: Option<PathBuf>,
132     pub openssl_static: bool,
133     pub configure_args: Vec<String>,
134
135     // These are either the stage0 downloaded binaries or the locally installed ones.
136     pub initial_cargo: PathBuf,
137     pub initial_rustc: PathBuf,
138 }
139
140 /// Per-target configuration stored in the global configuration structure.
141 #[derive(Default)]
142 pub struct Target {
143     /// Some(path to llvm-config) if using an external LLVM.
144     pub llvm_config: Option<PathBuf>,
145     pub jemalloc: Option<PathBuf>,
146     pub cc: Option<PathBuf>,
147     pub cxx: Option<PathBuf>,
148     pub ar: Option<PathBuf>,
149     pub linker: Option<PathBuf>,
150     pub ndk: Option<PathBuf>,
151     pub crt_static: Option<bool>,
152     pub musl_root: Option<PathBuf>,
153     pub qemu_rootfs: Option<PathBuf>,
154 }
155
156 /// Structure of the `config.toml` file that configuration is read from.
157 ///
158 /// This structure uses `Decodable` to automatically decode a TOML configuration
159 /// file into this format, and then this is traversed and written into the above
160 /// `Config` structure.
161 #[derive(Deserialize, Default)]
162 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
163 struct TomlConfig {
164     build: Option<Build>,
165     install: Option<Install>,
166     llvm: Option<Llvm>,
167     rust: Option<Rust>,
168     target: Option<HashMap<String, TomlTarget>>,
169     dist: Option<Dist>,
170 }
171
172 /// TOML representation of various global build decisions.
173 #[derive(Deserialize, Default, Clone)]
174 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
175 struct Build {
176     build: Option<String>,
177     #[serde(default)]
178     host: Vec<String>,
179     #[serde(default)]
180     target: Vec<String>,
181     cargo: Option<String>,
182     rustc: Option<String>,
183     low_priority: Option<bool>,
184     compiler_docs: Option<bool>,
185     docs: Option<bool>,
186     submodules: Option<bool>,
187     gdb: Option<String>,
188     locked_deps: Option<bool>,
189     vendor: Option<bool>,
190     nodejs: Option<String>,
191     python: Option<String>,
192     full_bootstrap: Option<bool>,
193     extended: Option<bool>,
194     verbose: Option<usize>,
195     sanitizers: Option<bool>,
196     profiler: Option<bool>,
197     openssl_static: Option<bool>,
198     configure_args: Option<Vec<String>>,
199     local_rebuild: Option<bool>,
200 }
201
202 /// TOML representation of various global install decisions.
203 #[derive(Deserialize, Default, Clone)]
204 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
205 struct Install {
206     prefix: Option<String>,
207     sysconfdir: Option<String>,
208     docdir: Option<String>,
209     bindir: Option<String>,
210     libdir: Option<String>,
211     mandir: Option<String>,
212
213     // standard paths, currently unused
214     datadir: Option<String>,
215     infodir: Option<String>,
216     localstatedir: Option<String>,
217 }
218
219 /// TOML representation of how the LLVM build is configured.
220 #[derive(Deserialize, Default)]
221 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
222 struct Llvm {
223     enabled: Option<bool>,
224     ccache: Option<StringOrBool>,
225     ninja: Option<bool>,
226     assertions: Option<bool>,
227     optimize: Option<bool>,
228     release_debuginfo: Option<bool>,
229     version_check: Option<bool>,
230     static_libstdcpp: Option<bool>,
231     targets: Option<String>,
232     experimental_targets: Option<String>,
233     link_jobs: Option<u32>,
234     link_shared: Option<bool>,
235 }
236
237 #[derive(Deserialize, Default, Clone)]
238 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
239 struct Dist {
240     sign_folder: Option<String>,
241     gpg_password_file: Option<String>,
242     upload_addr: Option<String>,
243     src_tarball: Option<bool>,
244 }
245
246 #[derive(Deserialize)]
247 #[serde(untagged)]
248 enum StringOrBool {
249     String(String),
250     Bool(bool),
251 }
252
253 impl Default for StringOrBool {
254     fn default() -> StringOrBool {
255         StringOrBool::Bool(false)
256     }
257 }
258
259 /// TOML representation of how the Rust build is configured.
260 #[derive(Deserialize, Default)]
261 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
262 struct Rust {
263     optimize: Option<bool>,
264     codegen_units: Option<u32>,
265     thinlto: Option<bool>,
266     debug_assertions: Option<bool>,
267     debuginfo: Option<bool>,
268     debuginfo_lines: Option<bool>,
269     debuginfo_only_std: Option<bool>,
270     experimental_parallel_queries: Option<bool>,
271     debug_jemalloc: Option<bool>,
272     use_jemalloc: Option<bool>,
273     backtrace: Option<bool>,
274     default_linker: Option<String>,
275     channel: Option<String>,
276     musl_root: Option<String>,
277     rpath: Option<bool>,
278     optimize_tests: Option<bool>,
279     debuginfo_tests: Option<bool>,
280     codegen_tests: Option<bool>,
281     ignore_git: Option<bool>,
282     debug: Option<bool>,
283     dist_src: Option<bool>,
284     quiet_tests: Option<bool>,
285     test_miri: Option<bool>,
286     save_toolstates: Option<String>,
287     codegen_backends: Option<Vec<String>>,
288     wasm_syscall: Option<bool>,
289 }
290
291 /// TOML representation of how each build target is configured.
292 #[derive(Deserialize, Default)]
293 #[serde(deny_unknown_fields, rename_all = "kebab-case")]
294 struct TomlTarget {
295     llvm_config: Option<String>,
296     jemalloc: Option<String>,
297     cc: Option<String>,
298     cxx: Option<String>,
299     ar: Option<String>,
300     linker: Option<String>,
301     android_ndk: Option<String>,
302     crt_static: Option<bool>,
303     musl_root: Option<String>,
304     qemu_rootfs: Option<String>,
305 }
306
307 impl Config {
308     pub fn parse(args: &[String]) -> Config {
309         let flags = Flags::parse(&args);
310         let file = flags.config.clone();
311         let mut config = Config::default();
312         config.llvm_enabled = true;
313         config.llvm_optimize = true;
314         config.llvm_version_check = true;
315         config.use_jemalloc = true;
316         config.backtrace = true;
317         config.rust_optimize = true;
318         config.rust_optimize_tests = true;
319         config.submodules = true;
320         config.docs = true;
321         config.rust_rpath = true;
322         config.channel = "dev".to_string();
323         config.codegen_tests = true;
324         config.ignore_git = false;
325         config.rust_dist_src = true;
326         config.test_miri = false;
327         config.rust_codegen_backends = vec![INTERNER.intern_str("llvm")];
328
329         config.on_fail = flags.on_fail;
330         config.stage = flags.stage;
331         config.src = flags.src;
332         config.jobs = flags.jobs;
333         config.cmd = flags.cmd;
334         config.incremental = flags.incremental;
335         config.keep_stage = flags.keep_stage;
336
337         // If --target was specified but --host wasn't specified, don't run any host-only tests.
338         config.run_host_only = flags.host.is_empty() && !flags.target.is_empty();
339
340         let toml = file.map(|file| {
341             let mut f = t!(File::open(&file));
342             let mut contents = String::new();
343             t!(f.read_to_string(&mut contents));
344             match toml::from_str(&contents) {
345                 Ok(table) => table,
346                 Err(err) => {
347                     println!("failed to parse TOML configuration '{}': {}",
348                         file.display(), err);
349                     process::exit(2);
350                 }
351             }
352         }).unwrap_or_else(|| TomlConfig::default());
353
354         let build = toml.build.clone().unwrap_or(Build::default());
355         set(&mut config.build, build.build.clone().map(|x| INTERNER.intern_string(x)));
356         set(&mut config.build, flags.build);
357         if config.build.is_empty() {
358             // set by bootstrap.py
359             config.build = INTERNER.intern_str(&env::var("BUILD").unwrap());
360         }
361         config.hosts.push(config.build.clone());
362         for host in build.host.iter() {
363             let host = INTERNER.intern_str(host);
364             if !config.hosts.contains(&host) {
365                 config.hosts.push(host);
366             }
367         }
368         for target in config.hosts.iter().cloned()
369             .chain(build.target.iter().map(|s| INTERNER.intern_str(s)))
370         {
371             if !config.targets.contains(&target) {
372                 config.targets.push(target);
373             }
374         }
375         config.hosts = if !flags.host.is_empty() {
376             flags.host
377         } else {
378             config.hosts
379         };
380         config.targets = if !flags.target.is_empty() {
381             flags.target
382         } else {
383             config.targets
384         };
385
386
387         config.nodejs = build.nodejs.map(PathBuf::from);
388         config.gdb = build.gdb.map(PathBuf::from);
389         config.python = build.python.map(PathBuf::from);
390         set(&mut config.low_priority, build.low_priority);
391         set(&mut config.compiler_docs, build.compiler_docs);
392         set(&mut config.docs, build.docs);
393         set(&mut config.submodules, build.submodules);
394         set(&mut config.locked_deps, build.locked_deps);
395         set(&mut config.vendor, build.vendor);
396         set(&mut config.full_bootstrap, build.full_bootstrap);
397         set(&mut config.extended, build.extended);
398         set(&mut config.verbose, build.verbose);
399         set(&mut config.sanitizers, build.sanitizers);
400         set(&mut config.profiler, build.profiler);
401         set(&mut config.openssl_static, build.openssl_static);
402         set(&mut config.configure_args, build.configure_args);
403         set(&mut config.local_rebuild, build.local_rebuild);
404         config.verbose = cmp::max(config.verbose, flags.verbose);
405
406         if let Some(ref install) = toml.install {
407             config.prefix = install.prefix.clone().map(PathBuf::from);
408             config.sysconfdir = install.sysconfdir.clone().map(PathBuf::from);
409             config.docdir = install.docdir.clone().map(PathBuf::from);
410             config.bindir = install.bindir.clone().map(PathBuf::from);
411             config.libdir = install.libdir.clone().map(PathBuf::from);
412             config.mandir = install.mandir.clone().map(PathBuf::from);
413         }
414
415         // Store off these values as options because if they're not provided
416         // we'll infer default values for them later
417         let mut thinlto = None;
418         let mut llvm_assertions = None;
419         let mut debuginfo_lines = None;
420         let mut debuginfo_only_std = None;
421         let mut debug = None;
422         let mut debug_jemalloc = None;
423         let mut debuginfo = None;
424         let mut debug_assertions = None;
425         let mut optimize = None;
426         let mut ignore_git = None;
427
428         if let Some(ref llvm) = toml.llvm {
429             match llvm.ccache {
430                 Some(StringOrBool::String(ref s)) => {
431                     config.ccache = Some(s.to_string())
432                 }
433                 Some(StringOrBool::Bool(true)) => {
434                     config.ccache = Some("ccache".to_string());
435                 }
436                 Some(StringOrBool::Bool(false)) | None => {}
437             }
438             set(&mut config.ninja, llvm.ninja);
439             set(&mut config.llvm_enabled, llvm.enabled);
440             llvm_assertions = llvm.assertions;
441             set(&mut config.llvm_optimize, llvm.optimize);
442             set(&mut config.llvm_release_debuginfo, llvm.release_debuginfo);
443             set(&mut config.llvm_version_check, llvm.version_check);
444             set(&mut config.llvm_static_stdcpp, llvm.static_libstdcpp);
445             set(&mut config.llvm_link_shared, llvm.link_shared);
446             config.llvm_targets = llvm.targets.clone();
447             config.llvm_experimental_targets = llvm.experimental_targets.clone()
448                 .unwrap_or("WebAssembly".to_string());
449             config.llvm_link_jobs = llvm.link_jobs;
450         }
451
452         if let Some(ref rust) = toml.rust {
453             debug = rust.debug;
454             debug_assertions = rust.debug_assertions;
455             debuginfo = rust.debuginfo;
456             debuginfo_lines = rust.debuginfo_lines;
457             debuginfo_only_std = rust.debuginfo_only_std;
458             optimize = rust.optimize;
459             ignore_git = rust.ignore_git;
460             debug_jemalloc = rust.debug_jemalloc;
461             thinlto = rust.thinlto;
462             set(&mut config.rust_optimize_tests, rust.optimize_tests);
463             set(&mut config.rust_debuginfo_tests, rust.debuginfo_tests);
464             set(&mut config.codegen_tests, rust.codegen_tests);
465             set(&mut config.rust_rpath, rust.rpath);
466             set(&mut config.use_jemalloc, rust.use_jemalloc);
467             set(&mut config.backtrace, rust.backtrace);
468             set(&mut config.channel, rust.channel.clone());
469             set(&mut config.rust_dist_src, rust.dist_src);
470             set(&mut config.quiet_tests, rust.quiet_tests);
471             set(&mut config.test_miri, rust.test_miri);
472             set(&mut config.wasm_syscall, rust.wasm_syscall);
473             config.rustc_parallel_queries = rust.experimental_parallel_queries.unwrap_or(false);
474             config.rustc_default_linker = rust.default_linker.clone();
475             config.musl_root = rust.musl_root.clone().map(PathBuf::from);
476             config.save_toolstates = rust.save_toolstates.clone().map(PathBuf::from);
477
478             if let Some(ref backends) = rust.codegen_backends {
479                 config.rust_codegen_backends = backends.iter()
480                     .map(|s| INTERNER.intern_str(s))
481                     .collect();
482             }
483
484             match rust.codegen_units {
485                 Some(0) => config.rust_codegen_units = Some(num_cpus::get() as u32),
486                 Some(n) => config.rust_codegen_units = Some(n),
487                 None => {}
488             }
489         }
490
491         if let Some(ref t) = toml.target {
492             for (triple, cfg) in t {
493                 let mut target = Target::default();
494
495                 if let Some(ref s) = cfg.llvm_config {
496                     target.llvm_config = Some(env::current_dir().unwrap().join(s));
497                 }
498                 if let Some(ref s) = cfg.jemalloc {
499                     target.jemalloc = Some(env::current_dir().unwrap().join(s));
500                 }
501                 if let Some(ref s) = cfg.android_ndk {
502                     target.ndk = Some(env::current_dir().unwrap().join(s));
503                 }
504                 target.cc = cfg.cc.clone().map(PathBuf::from);
505                 target.cxx = cfg.cxx.clone().map(PathBuf::from);
506                 target.ar = cfg.ar.clone().map(PathBuf::from);
507                 target.linker = cfg.linker.clone().map(PathBuf::from);
508                 target.crt_static = cfg.crt_static.clone();
509                 target.musl_root = cfg.musl_root.clone().map(PathBuf::from);
510                 target.qemu_rootfs = cfg.qemu_rootfs.clone().map(PathBuf::from);
511
512                 config.target_config.insert(INTERNER.intern_string(triple.clone()), target);
513             }
514         }
515
516         if let Some(ref t) = toml.dist {
517             config.dist_sign_folder = t.sign_folder.clone().map(PathBuf::from);
518             config.dist_gpg_password_file = t.gpg_password_file.clone().map(PathBuf::from);
519             config.dist_upload_addr = t.upload_addr.clone();
520             set(&mut config.rust_dist_src, t.src_tarball);
521         }
522
523         let cwd = t!(env::current_dir());
524         let out = cwd.join("build");
525
526         let stage0_root = out.join(&config.build).join("stage0/bin");
527         config.initial_rustc = match build.rustc {
528             Some(s) => PathBuf::from(s),
529             None => stage0_root.join(exe("rustc", &config.build)),
530         };
531         config.initial_cargo = match build.cargo {
532             Some(s) => PathBuf::from(s),
533             None => stage0_root.join(exe("cargo", &config.build)),
534         };
535
536         // Now that we've reached the end of our configuration, infer the
537         // default values for all options that we haven't otherwise stored yet.
538
539         let default = false;
540         config.llvm_assertions = llvm_assertions.unwrap_or(default);
541
542         let default = match &config.channel[..] {
543             "stable" | "beta" | "nightly" => true,
544             _ => false,
545         };
546         config.rust_thinlto = thinlto.unwrap_or(true);
547         config.rust_debuginfo_lines = debuginfo_lines.unwrap_or(default);
548         config.rust_debuginfo_only_std = debuginfo_only_std.unwrap_or(default);
549
550         let default = debug == Some(true);
551         config.debug_jemalloc = debug_jemalloc.unwrap_or(default);
552         config.rust_debuginfo = debuginfo.unwrap_or(default);
553         config.rust_debug_assertions = debug_assertions.unwrap_or(default);
554         config.rust_optimize = optimize.unwrap_or(!default);
555
556         let default = config.channel == "dev";
557         config.ignore_git = ignore_git.unwrap_or(default);
558
559         config
560     }
561
562     pub fn verbose(&self) -> bool {
563         self.verbose > 0
564     }
565
566     pub fn very_verbose(&self) -> bool {
567         self.verbose > 1
568     }
569 }
570
571 fn set<T>(field: &mut T, val: Option<T>) {
572     if let Some(v) = val {
573         *field = v;
574     }
575 }