Skip to content

Commit ee3743d

Browse files
authored
Rollup merge of rust-lang#76628 - jyn514:default-config-files, r=Mark-Simulacrum
Add sample defaults for config.toml - Allow including defaults in `src/bootstrap/defaults` using `profile = "..."`. - Add default config files, with a README noting they're experimental and asking you to open an issue if you run into trouble. The config files have comments explaining why the defaults are set. - Combine config files using the `merge` dependency. This introduces a new dependency on `merge` that hasn't yet been vetted. I want to improve the output when `include = "x"` isn't found: ``` thread 'main' panicked at 'fs::read_to_string(&file) failed with No such file or directory (os error 2) ("configuration file did not exist")', src/bootstrap/config.rs:522:28 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace failed to run: /home/joshua/rustc/build/bootstrap/debug/bootstrap test tidy Build completed unsuccessfully in 0:00:00 ``` However that seems like it could be fixed in a follow-up. Closes rust-lang#76619
2 parents f20fa17 + c9c8fb8 commit ee3743d

9 files changed

+136
-25
lines changed

Cargo.lock

+23
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ dependencies = [
207207
"ignore",
208208
"lazy_static",
209209
"libc",
210+
"merge",
210211
"num_cpus",
211212
"opener",
212213
"pretty_assertions",
@@ -1909,6 +1910,28 @@ dependencies = [
19091910
"autocfg",
19101911
]
19111912

1913+
[[package]]
1914+
name = "merge"
1915+
version = "0.1.0"
1916+
source = "registry+https://github.com/rust-lang/crates.io-index"
1917+
checksum = "10bbef93abb1da61525bbc45eeaff6473a41907d19f8f9aa5168d214e10693e9"
1918+
dependencies = [
1919+
"merge_derive",
1920+
"num-traits",
1921+
]
1922+
1923+
[[package]]
1924+
name = "merge_derive"
1925+
version = "0.1.0"
1926+
source = "registry+https://github.com/rust-lang/crates.io-index"
1927+
checksum = "209d075476da2e63b4b29e72a2ef627b840589588e71400a25e3565c4f849d07"
1928+
dependencies = [
1929+
"proc-macro-error",
1930+
"proc-macro2",
1931+
"quote",
1932+
"syn",
1933+
]
1934+
19121935
[[package]]
19131936
name = "minifier"
19141937
version = "0.0.33"

config.toml.example

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@
1515
# See `src/bootstrap/CHANGELOG.md` for more information.
1616
changelog-seen = 1
1717

18+
# =============================================================================
19+
# Global Settings
20+
# =============================================================================
21+
22+
# Use different pre-set defaults than the global defaults.
23+
#
24+
# See `src/bootstrap/defaults` for more information.
25+
# Note that this has no default value (x.py uses the defaults in `config.toml.example`).
26+
#profile = <none>
27+
1828
# =============================================================================
1929
# Tweaking how LLVM is compiled
2030
# =============================================================================

src/bootstrap/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ lazy_static = "1.3.0"
4949
time = "0.1"
5050
ignore = "0.4.10"
5151
opener = "0.4"
52+
merge = "0.1.0"
5253

5354
[target.'cfg(windows)'.dependencies.winapi]
5455
version = "0.3"

src/bootstrap/config.rs

+51-25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::flags::Flags;
1616
pub use crate::flags::Subcommand;
1717
use crate::util::exe;
1818
use build_helper::t;
19+
use merge::Merge;
1920
use serde::Deserialize;
2021

2122
macro_rules! check_ci_llvm {
@@ -280,10 +281,31 @@ struct TomlConfig {
280281
rust: Option<Rust>,
281282
target: Option<HashMap<String, TomlTarget>>,
282283
dist: Option<Dist>,
284+
profile: Option<String>,
285+
}
286+
287+
impl Merge for TomlConfig {
288+
fn merge(&mut self, TomlConfig { build, install, llvm, rust, dist, target, profile: _ }: Self) {
289+
fn do_merge<T: Merge>(x: &mut Option<T>, y: Option<T>) {
290+
if let Some(new) = y {
291+
if let Some(original) = x {
292+
original.merge(new);
293+
} else {
294+
*x = Some(new);
295+
}
296+
}
297+
};
298+
do_merge(&mut self.build, build);
299+
do_merge(&mut self.install, install);
300+
do_merge(&mut self.llvm, llvm);
301+
do_merge(&mut self.rust, rust);
302+
do_merge(&mut self.dist, dist);
303+
assert!(target.is_none(), "merging target-specific config is not currently supported");
304+
}
283305
}
284306

285307
/// TOML representation of various global build decisions.
286-
#[derive(Deserialize, Default, Clone)]
308+
#[derive(Deserialize, Default, Clone, Merge)]
287309
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
288310
struct Build {
289311
build: Option<String>,
@@ -323,7 +345,7 @@ struct Build {
323345
}
324346

325347
/// TOML representation of various global install decisions.
326-
#[derive(Deserialize, Default, Clone)]
348+
#[derive(Deserialize, Default, Clone, Merge)]
327349
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
328350
struct Install {
329351
prefix: Option<String>,
@@ -340,7 +362,7 @@ struct Install {
340362
}
341363

342364
/// TOML representation of how the LLVM build is configured.
343-
#[derive(Deserialize, Default)]
365+
#[derive(Deserialize, Default, Merge)]
344366
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
345367
struct Llvm {
346368
skip_rebuild: Option<bool>,
@@ -367,7 +389,7 @@ struct Llvm {
367389
download_ci_llvm: Option<bool>,
368390
}
369391

370-
#[derive(Deserialize, Default, Clone)]
392+
#[derive(Deserialize, Default, Clone, Merge)]
371393
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
372394
struct Dist {
373395
sign_folder: Option<String>,
@@ -391,7 +413,7 @@ impl Default for StringOrBool {
391413
}
392414

393415
/// TOML representation of how the Rust build is configured.
394-
#[derive(Deserialize, Default)]
416+
#[derive(Deserialize, Default, Merge)]
395417
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
396418
struct Rust {
397419
optimize: Option<bool>,
@@ -436,7 +458,7 @@ struct Rust {
436458
}
437459

438460
/// TOML representation of how each build target is configured.
439-
#[derive(Deserialize, Default)]
461+
#[derive(Deserialize, Default, Merge)]
440462
#[serde(deny_unknown_fields, rename_all = "kebab-case")]
441463
struct TomlTarget {
442464
cc: Option<String>,
@@ -526,27 +548,31 @@ impl Config {
526548
}
527549

528550
#[cfg(test)]
529-
let toml = TomlConfig::default();
551+
let get_toml = |_| TomlConfig::default();
530552
#[cfg(not(test))]
531-
let toml = flags
532-
.config
533-
.map(|file| {
534-
use std::process;
535-
536-
let contents = t!(fs::read_to_string(&file));
537-
match toml::from_str(&contents) {
538-
Ok(table) => table,
539-
Err(err) => {
540-
println!(
541-
"failed to parse TOML configuration '{}': {}",
542-
file.display(),
543-
err
544-
);
545-
process::exit(2);
546-
}
553+
let get_toml = |file: PathBuf| {
554+
use std::process;
555+
556+
let contents = t!(fs::read_to_string(&file), "configuration file did not exist");
557+
match toml::from_str(&contents) {
558+
Ok(table) => table,
559+
Err(err) => {
560+
println!("failed to parse TOML configuration '{}': {}", file.display(), err);
561+
process::exit(2);
547562
}
548-
})
549-
.unwrap_or_else(TomlConfig::default);
563+
}
564+
};
565+
566+
let mut toml = flags.config.map(get_toml).unwrap_or_else(TomlConfig::default);
567+
if let Some(include) = &toml.profile {
568+
let mut include_path = config.src.clone();
569+
include_path.push("src");
570+
include_path.push("bootstrap");
571+
include_path.push("defaults");
572+
include_path.push(format!("config.toml.{}", include));
573+
let included_toml = get_toml(include_path);
574+
toml.merge(included_toml);
575+
}
550576

551577
config.changelog_seen = toml.changelog_seen;
552578

src/bootstrap/defaults/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# About bootstrap defaults
2+
3+
These defaults are intended to be a good starting point for working with x.py,
4+
with the understanding that no one set of defaults make sense for everyone.
5+
6+
They are still experimental, and we'd appreciate your help improving them!
7+
If you use a setting that's not in these defaults that you think others would benefit from, please [file an issue] or make a PR with the changes.
8+
Similarly, if one of these defaults doesn't match what you use personally,
9+
please open an issue to get it changed.
10+
11+
[file an issue]: https://github.com/rust-lang/rust/issues/new/choose
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# These defaults are meant for contributors to the compiler who modify codegen or LLVM
2+
[llvm]
3+
# This enables debug-assertions in LLVM,
4+
# catching logic errors in codegen much earlier in the process.
5+
assertions = true
6+
7+
[rust]
8+
# This enables `RUSTC_LOG=debug`, avoiding confusing situations
9+
# where adding `debug!()` appears to do nothing.
10+
# However, it makes running the compiler slightly slower.
11+
debug-logging = true
12+
# This greatly increases the speed of rebuilds, especially when there are only minor changes. However, it makes the initial build slightly slower.
13+
incremental = true
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# These defaults are meant for contributors to the compiler who do not modify codegen or LLVM
2+
[rust]
3+
# This enables `RUSTC_LOG=debug`, avoiding confusing situations
4+
# where adding `debug!()` appears to do nothing.
5+
# However, it makes running the compiler slightly slower.
6+
debug-logging = true
7+
# This greatly increases the speed of rebuilds, especially when there are only minor changes. However, it makes the initial build slightly slower.
8+
incremental = true
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# These defaults are meant for contributors to the standard library and documentation.
2+
[build]
3+
# When building the standard library, you almost never want to build the compiler itself.
4+
build-stage = 0
5+
test-stage = 0
6+
bench-stage = 0
7+
8+
[rust]
9+
# This greatly increases the speed of rebuilds, especially when there are only minor changes. However, it makes the initial build slightly slower.
10+
incremental = true
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# These defaults are meant for users and distro maintainers building from source, without intending to make multiple changes.
2+
[build]
3+
# When compiling from source, you almost always want a full stage 2 build,
4+
# which has all the latest optimizations from nightly.
5+
build-stage = 2
6+
test-stage = 2
7+
doc-stage = 2
8+
# When compiling from source, you usually want all tools.
9+
extended = true

0 commit comments

Comments
 (0)