Skip to content

Commit de9d1b6

Browse files
Add --config-file option to override default location of config.toml
1 parent 79241b8 commit de9d1b6

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

build_system/src/config.rs

+30-18
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,8 @@ impl Channel {
2323
}
2424
}
2525

26-
fn failed_config_parsing(err: &str) -> Result<ConfigFile, String> {
27-
Err(format!(
28-
"Failed to parse `{}`: {}",
29-
ConfigFile::CONFIG_FILE,
30-
err
31-
))
26+
fn failed_config_parsing(config_file: &str, err: &str) -> Result<ConfigFile, String> {
27+
Err(format!("Failed to parse `{}`: {}", config_file, err))
3228
}
3329

3430
#[derive(Default)]
@@ -38,13 +34,12 @@ pub struct ConfigFile {
3834
}
3935

4036
impl ConfigFile {
41-
pub const CONFIG_FILE: &'static str = "config.toml";
42-
43-
pub fn new() -> Result<Self, String> {
44-
let content = fs::read_to_string(Self::CONFIG_FILE).map_err(|_| {
37+
pub fn new(config_file: Option<&str>) -> Result<Self, String> {
38+
let config_file = config_file.unwrap_or("config.toml");
39+
let content = fs::read_to_string(config_file).map_err(|_| {
4540
format!(
4641
"Failed to read `{}`. Take a look at `Readme.md` to see how to set up the project",
47-
Self::CONFIG_FILE,
42+
config_file,
4843
)
4944
})?;
5045
let toml = Toml::parse(&content).map_err(|err| {
@@ -61,19 +56,23 @@ impl ConfigFile {
6156
config.gcc_path = Some(value.as_str().to_string())
6257
}
6358
("gcc-path", _) => {
64-
return failed_config_parsing("Expected a string for `gcc-path`")
59+
return failed_config_parsing(config_file, "Expected a string for `gcc-path`")
6560
}
6661
("download-gccjit", TomlValue::Boolean(value)) => {
6762
config.download_gccjit = Some(*value)
6863
}
6964
("download-gccjit", _) => {
70-
return failed_config_parsing("Expected a boolean for `download-gccjit`")
65+
return failed_config_parsing(
66+
config_file,
67+
"Expected a boolean for `download-gccjit`",
68+
)
7169
}
72-
_ => return failed_config_parsing(&format!("Unknown key `{}`", key)),
70+
_ => return failed_config_parsing(config_file, &format!("Unknown key `{}`", key)),
7371
}
7472
}
7573
if config.gcc_path.is_none() && config.download_gccjit.is_none() {
7674
return failed_config_parsing(
75+
config_file,
7776
"At least one of `gcc-path` or `download-gccjit` value must be set",
7877
);
7978
}
@@ -104,6 +103,7 @@ pub struct ConfigInfo {
104103
pub cg_backend_path: String,
105104
pub sysroot_path: String,
106105
pub gcc_path: String,
106+
config_file: Option<String>,
107107
}
108108

109109
impl ConfigInfo {
@@ -135,6 +135,14 @@ impl ConfigInfo {
135135
}
136136
_ => return Err("Expected a value after `--out-dir`, found nothing".to_string()),
137137
},
138+
"--config-file" => match args.next() {
139+
Some(arg) if !arg.is_empty() => {
140+
self.config_file = Some(arg.to_string());
141+
}
142+
_ => {
143+
return Err("Expected a value after `--config-file`, found nothing".to_string())
144+
}
145+
},
138146
"--release-sysroot" => self.sysroot_release_channel = true,
139147
"--release" => self.channel = Channel::Release,
140148
"--sysroot-panic-abort" => self.sysroot_panic_abort = true,
@@ -152,12 +160,15 @@ impl ConfigInfo {
152160
}
153161

154162
pub fn setup_gcc_path(&mut self, override_gcc_path: Option<&str>) -> Result<(), String> {
155-
let ConfigFile { gcc_path, .. } = ConfigFile::new()?;
163+
let ConfigFile { gcc_path, .. } = ConfigFile::new(self.config_file.as_deref())?;
156164

157165
self.gcc_path = match override_gcc_path {
158166
Some(path) => {
159167
if gcc_path.is_some() {
160-
println!("overriding setting from `{}`", ConfigFile::CONFIG_FILE);
168+
println!(
169+
"overriding setting from `{}`",
170+
self.config_file.as_deref().unwrap_or("config.toml")
171+
);
161172
}
162173
path.to_string()
163174
}
@@ -168,7 +179,7 @@ impl ConfigInfo {
168179
None => {
169180
return Err(format!(
170181
"missing `gcc-path` value from `{}`",
171-
ConfigFile::CONFIG_FILE
182+
self.config_file.as_deref().unwrap_or("config.toml"),
172183
))
173184
}
174185
}
@@ -363,7 +374,8 @@ impl ConfigInfo {
363374
--out-dir : Location where the files will be generated
364375
--release : Build in release mode
365376
--release-sysroot : Build sysroot in release mode
366-
--sysroot-panic-abort : Build the sysroot without unwinding support."
377+
--sysroot-panic-abort : Build the sysroot without unwinding support
378+
--config-file : Location of the config file to be used"
367379
);
368380
}
369381
}

0 commit comments

Comments
 (0)