Skip to content

Commit 0d531c3

Browse files
committed
Better config parsing and allow specifying host and target triple in config
1 parent 83cca1b commit 0d531c3

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

build_system/build_sysroot.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,7 @@ fn build_clif_sysroot_for_triple(
165165

166166
let build_dir = Path::new("build_sysroot").join("target").join(triple).join(channel);
167167

168-
let keep_sysroot =
169-
fs::read_to_string("config.txt").unwrap().lines().any(|line| line.trim() == "keep_sysroot");
170-
if !keep_sysroot {
168+
if !crate::config::get_bool("keep_sysroot") {
171169
// Cleanup the target dir with the exception of build scripts and the incremental cache
172170
for dir in ["build", "deps", "examples", "native"] {
173171
if build_dir.join(dir).exists() {

build_system/config.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use std::{fs, process};
2+
3+
fn load_config_file() -> Vec<(String, Option<String>)> {
4+
fs::read_to_string("config.txt")
5+
.unwrap()
6+
.lines()
7+
.map(|line| if let Some((line, _comment)) = line.split_once('#') { line } else { line })
8+
.map(|line| line.trim())
9+
.filter(|line| !line.is_empty())
10+
.map(|line| {
11+
if let Some((key, val)) = line.split_once('=') {
12+
(key.trim().to_owned(), Some(val.trim().to_owned()))
13+
} else {
14+
(line.to_owned(), None)
15+
}
16+
})
17+
.collect()
18+
}
19+
20+
pub(crate) fn get_bool(name: &str) -> bool {
21+
let values = load_config_file()
22+
.into_iter()
23+
.filter(|(key, _)| key == name)
24+
.map(|(_, val)| val)
25+
.collect::<Vec<_>>();
26+
if values.is_empty() {
27+
false
28+
} else {
29+
if values.iter().any(|val| val.is_some()) {
30+
eprintln!("Boolean config `{}` has a value", name);
31+
process::exit(1);
32+
}
33+
true
34+
}
35+
}
36+
37+
pub(crate) fn get_value(name: &str) -> Option<String> {
38+
let values = load_config_file()
39+
.into_iter()
40+
.filter(|(key, _)| key == name)
41+
.map(|(_, val)| val)
42+
.collect::<Vec<_>>();
43+
if values.is_empty() {
44+
None
45+
} else if values.len() == 1 {
46+
if values[0].is_none() {
47+
eprintln!("Config `{}` missing value", name);
48+
process::exit(1);
49+
}
50+
values.into_iter().next().unwrap()
51+
} else {
52+
eprintln!("Config `{}` given multiple values: {:?}", name, values);
53+
process::exit(1);
54+
}
55+
}

config.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# This file allows configuring the build system.
22

3+
# The host triple
4+
#host = x86_64-unknown-linux-gnu
5+
6+
# The target triple
7+
#target = x86_64-unknown-linux-gnu
8+
39
# Disables cleaning of the sysroot dir. This will cause old compiled artifacts to be re-used when
410
# the sysroot source hasn't changed. This is useful when the codegen backend hasn't been modified.
511
# This option can be changed while the build system is already running for as long as sysroot

y.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ use std::process;
3131
mod build_backend;
3232
#[path = "build_system/build_sysroot.rs"]
3333
mod build_sysroot;
34+
#[path = "build_system/config.rs"]
35+
mod config;
3436
#[path = "build_system/prepare.rs"]
3537
mod prepare;
3638
#[path = "build_system/rustc_info.rs"]
@@ -114,6 +116,8 @@ fn main() {
114116

115117
let host_triple = if let Ok(host_triple) = std::env::var("HOST_TRIPLE") {
116118
host_triple
119+
} else if let Some(host_triple) = crate::config::get_value("host") {
120+
host_triple
117121
} else {
118122
rustc_info::get_host_triple()
119123
};
@@ -123,6 +127,8 @@ fn main() {
123127
} else {
124128
host_triple.clone() // Empty target triple can happen on GHA
125129
}
130+
} else if let Some(target_triple) = crate::config::get_value("target") {
131+
target_triple
126132
} else {
127133
host_triple.clone()
128134
};

0 commit comments

Comments
 (0)