]> git.lizzy.rs Git - rust.git/blobdiff - src/bootstrap/config.rs
Rename derive_merge macro to define_config and move Deserialize impl into it
[rust.git] / src / bootstrap / config.rs
index 73a855ae4d72aa38660d475725987524be3ac2e4..7e6eecaaa884f5f4102aeb50c1f448bab7c03479 100644 (file)
@@ -362,11 +362,13 @@ fn do_merge<T: Merge>(x: &mut Option<T>, y: Option<T>) {
 
 // We are using a decl macro instead of a derive proc macro here to reduce the compile time of
 // rustbuild.
-macro_rules! derive_merge {
+macro_rules! define_config {
     ($(#[$attr:meta])* struct $name:ident {
         $($field:ident: $field_ty:ty,)*
     }) => {
         $(#[$attr])*
+        #[derive(Deserialize)]
+        #[serde(deny_unknown_fields, rename_all = "kebab-case")]
         struct $name {
             $($field: $field_ty,)*
         }
@@ -383,10 +385,9 @@ fn merge(&mut self, other: Self) {
     }
 }
 
-derive_merge! {
+define_config! {
     /// TOML representation of various global build decisions.
-    #[derive(Deserialize, Default)]
-    #[serde(deny_unknown_fields, rename_all = "kebab-case")]
+    #[derive(Default)]
     struct Build {
         build: Option<String>,
         host: Option<Vec<String>>,
@@ -429,10 +430,8 @@ struct Build {
     }
 }
 
-derive_merge! {
+define_config! {
     /// TOML representation of various global install decisions.
-    #[derive(Deserialize)]
-    #[serde(deny_unknown_fields, rename_all = "kebab-case")]
     struct Install {
         prefix: Option<String>,
         sysconfdir: Option<String>,
@@ -444,10 +443,8 @@ struct Install {
     }
 }
 
-derive_merge! {
+define_config! {
     /// TOML representation of how the LLVM build is configured.
-    #[derive(Deserialize)]
-    #[serde(deny_unknown_fields, rename_all = "kebab-case")]
     struct Llvm {
         skip_rebuild: Option<bool>,
         optimize: Option<bool>,
@@ -479,9 +476,7 @@ struct Llvm {
     }
 }
 
-derive_merge! {
-    #[derive(Deserialize)]
-    #[serde(deny_unknown_fields, rename_all = "kebab-case")]
+define_config! {
     struct Dist {
         sign_folder: Option<String>,
         gpg_password_file: Option<String>,
@@ -505,10 +500,8 @@ fn default() -> StringOrBool {
     }
 }
 
-derive_merge! {
+define_config! {
     /// TOML representation of how the Rust build is configured.
-    #[derive(Deserialize)]
-    #[serde(deny_unknown_fields, rename_all = "kebab-case")]
     struct Rust {
         optimize: Option<bool>,
         debug: Option<bool>,
@@ -560,10 +553,8 @@ struct Rust {
     }
 }
 
-derive_merge! {
+define_config! {
     /// TOML representation of how each build target is configured.
-    #[derive(Deserialize)]
-    #[serde(deny_unknown_fields, rename_all = "kebab-case")]
     struct TomlTarget {
         cc: Option<String>,
         cxx: Option<String>,
@@ -647,7 +638,8 @@ pub fn parse(args: &[String]) -> Config {
         let get_toml = |file: &Path| {
             use std::process;
 
-            let contents = t!(fs::read_to_string(file), "`include` config not found");
+            let contents =
+                t!(fs::read_to_string(file), format!("config file {} not found", file.display()));
             match toml::from_str(&contents) {
                 Ok(table) => table,
                 Err(err) => {
@@ -657,14 +649,24 @@ pub fn parse(args: &[String]) -> Config {
             }
         };
 
-        // check --config first, then `$RUST_BOOTSTRAP_CONFIG` first, then `config.toml`
+        // Read from `--config`, then `RUST_BOOTSTRAP_CONFIG`, then `./config.toml`, then `config.toml` in the root directory.
         let toml_path = flags
             .config
             .clone()
-            .or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from))
-            .unwrap_or_else(|| PathBuf::from("config.toml"));
-        let mut toml =
-            if toml_path.exists() { get_toml(&toml_path) } else { TomlConfig::default() };
+            .or_else(|| env::var_os("RUST_BOOTSTRAP_CONFIG").map(PathBuf::from));
+        let using_default_path = toml_path.is_none();
+        let mut toml_path = toml_path.unwrap_or_else(|| PathBuf::from("config.toml"));
+        if using_default_path && !toml_path.exists() {
+            toml_path = config.src.join(toml_path);
+        }
+
+        // Give a hard error if `--config` or `RUST_BOOTSTRAP_CONFIG` are set to a missing path,
+        // but not if `config.toml` hasn't been created.
+        let mut toml = if !using_default_path || toml_path.exists() {
+            get_toml(&toml_path)
+        } else {
+            TomlConfig::default()
+        };
 
         if let Some(include) = &toml.profile {
             let mut include_path = config.src.clone();