Better config parsing and allow specifying host and target triple in config

This commit is contained in:
bjorn3 2021-07-04 15:10:06 +02:00
parent 83cca1b03c
commit 0d531c3737
4 changed files with 68 additions and 3 deletions

View File

@ -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() {

55
build_system/config.rs Normal file
View File

@ -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);
}
}

View File

@ -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

6
y.rs
View File

@ -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()
};