about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbjorn3 <bjorn3@users.noreply.github.com>2021-07-04 15:10:06 +0200
committerbjorn3 <bjorn3@users.noreply.github.com>2021-07-04 15:10:06 +0200
commit0d531c37375006917a151ed3932fb09d3e6a431d (patch)
tree3958586531a2a4137fb5d4ecee741225f6b3260f
parent83cca1b03c808998032295ab845589d4a4316908 (diff)
downloadrust-0d531c37375006917a151ed3932fb09d3e6a431d.tar.gz
rust-0d531c37375006917a151ed3932fb09d3e6a431d.zip
Better config parsing and allow specifying host and target triple in config
-rw-r--r--build_system/build_sysroot.rs4
-rw-r--r--build_system/config.rs55
-rw-r--r--config.txt6
-rwxr-xr-xy.rs6
4 files changed, 68 insertions, 3 deletions
diff --git a/build_system/build_sysroot.rs b/build_system/build_sysroot.rs
index e7946aa36ad..00735ac1493 100644
--- a/build_system/build_sysroot.rs
+++ b/build_system/build_sysroot.rs
@@ -165,9 +165,7 @@ fn build_clif_sysroot_for_triple(
 
     let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
 
-    let keep_sysroot =
-        fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
-    if !keep_sysroot {
+    if !crate::config::get_bool("keep_sysroot") {
         // Cleanup the target dir with the exception of build scripts and the incremental cache
         for dir in ["build", "deps", "examples", "native"] {
             if build_dir.join(dir).exists() {
diff --git a/build_system/config.rs b/build_system/config.rs
new file mode 100644
index 00000000000..ef540cf1f82
--- /dev/null
+++ b/build_system/config.rs
@@ -0,0 +1,55 @@
+use std::{fs, process};
+
+fn load_config_file() -> Vec<(String, Option<String>)> {
+    fs::read_to_string("config.txt")
+        .unwrap()
+        .lines()
+        .map(|line| if let Some((line, _comment)) = line.split_once('#') { line } else { line })
+        .map(|line| line.trim())
+        .filter(|line| !line.is_empty())
+        .map(|line| {
+            if let Some((key, val)) = line.split_once('=') {
+                (key.trim().to_owned(), Some(val.trim().to_owned()))
+            } else {
+                (line.to_owned(), None)
+            }
+        })
+        .collect()
+}
+
+pub(crate) fn get_bool(name: &str) -> bool {
+    let values = load_config_file()
+        .into_iter()
+        .filter(|(key, _)| key == name)
+        .map(|(_, val)| val)
+        .collect::<Vec<_>>();
+    if values.is_empty() {
+        false
+    } else {
+        if values.iter().any(|val| val.is_some()) {
+            eprintln!("Boolean config `{}` has a value", name);
+            process::exit(1);
+        }
+        true
+    }
+}
+
+pub(crate) fn get_value(name: &str) -> Option<String> {
+    let values = load_config_file()
+        .into_iter()
+        .filter(|(key, _)| key == name)
+        .map(|(_, val)| val)
+        .collect::<Vec<_>>();
+    if values.is_empty() {
+        None
+    } else if values.len() == 1 {
+        if values[0].is_none() {
+            eprintln!("Config `{}` missing value", name);
+            process::exit(1);
+        }
+        values.into_iter().next().unwrap()
+    } else {
+        eprintln!("Config `{}` given multiple values: {:?}", name, values);
+        process::exit(1);
+    }
+}
diff --git a/config.txt b/config.txt
index f707b9322af..c5fd7beb747 100644
--- a/config.txt
+++ b/config.txt
@@ -1,5 +1,11 @@
 # This file allows configuring the build system.
 
+# The host triple
+#host = x86_64-unknown-linux-gnu
+
+# The target triple
+#target = x86_64-unknown-linux-gnu
+
 # Disables cleaning of the sysroot dir. This will cause old compiled artifacts to be re-used when
 # the sysroot source hasn't changed. This is useful when the codegen backend hasn't been modified.
 # This option can be changed while the build system is already running for as long as sysroot
diff --git a/y.rs b/y.rs
index 55457745d25..aeaac59fff0 100755
--- a/y.rs
+++ b/y.rs
@@ -31,6 +31,8 @@ use std::process;
 mod build_backend;
 #[path = "build_system/build_sysroot.rs"]
 mod build_sysroot;
+#[path = "build_system/config.rs"]
+mod config;
 #[path = "build_system/prepare.rs"]
 mod prepare;
 #[path = "build_system/rustc_info.rs"]
@@ -114,6 +116,8 @@ fn main() {
 
     let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
         host_triple
+    } else if let Some(host_triple) = crate::config::get_value("host") {
+        host_triple
     } else {
         rustc_info::get_host_triple()
     };
@@ -123,6 +127,8 @@ fn main() {
         } else {
             host_triple.clone() // Empty target triple can happen on GHA
         }
+    } else if let Some(target_triple) = crate::config::get_value("target") {
+        target_triple
     } else {
         host_triple.clone()
     };