Skip to content

Commit 4c673fc

Browse files
authored
Merge pull request #33 from Rust-GPU/feature/boolean-values-in-toml-become-flags-in-build-parameters
feature: treat boolean values in toml files as flags
2 parents dbf193e + baeaa63 commit 4c673fc

File tree

2 files changed

+48
-25
lines changed

2 files changed

+48
-25
lines changed

crates/cargo-gpu/src/spirv_cli.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl SpirvCli {
166166
if self.is_toolchain_install_consent {
167167
return Ok(());
168168
}
169+
log::debug!("asking for consent to install the required toolchain");
169170
crossterm::terminal::enable_raw_mode()?;
170171
crate::user_output!("{prompt} [y/n]: ");
171172
let input = crossterm::event::read()?;

crates/cargo-gpu/src/toml.rs

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,20 @@ impl Toml {
7575
} else {
7676
anyhow::bail!("toml file '{}' must describe a workspace containing [workspace.metadata.rust-gpu.build] or a describe a crate with [package.metadata.rust-gpu.build]", path.display());
7777
};
78+
7879
log::info!(
7980
"building with [{toml_type}.metadata.rust-gpu.build] section of the toml file at '{}'",
8081
path.display()
8182
);
8283
log::debug!("table: {table:#?}");
8384

84-
let mut parameters: Vec<String> = table
85-
.get("build")
86-
.with_context(|| "toml is missing the 'build' table")?
87-
.as_table()
88-
.with_context(|| {
89-
format!("toml file's '{toml_type}.metadata.rust-gpu.build' property is not a table")
90-
})?
91-
.into_iter()
92-
.map(|(key, val)| -> anyhow::Result<Vec<String>> {
93-
Ok(if let toml::Value::String(string) = val {
94-
[format!("--{key}"), string.clone()].into()
95-
} else {
96-
let mut value = String::new();
97-
let ser = toml::ser::ValueSerializer::new(&mut value);
98-
serde::Serialize::serialize(val, ser)?;
99-
[format!("--{key}"), value].into()
100-
})
101-
})
102-
.collect::<anyhow::Result<Vec<Vec<String>>>>()?
103-
.into_iter()
104-
.flatten()
105-
.collect();
106-
parameters.insert(0, "cargo-gpu".to_owned());
107-
parameters.insert(1, "build".to_owned());
108-
10985
log::info!(
11086
"issuing cargo commands from the working directory '{}'",
11187
working_directory.display()
11288
);
11389
std::env::set_current_dir(working_directory)?;
11490

91+
let parameters = construct_build_parameters_from_toml_table(toml_type, &table)?;
11592
log::debug!("build parameters: {parameters:#?}");
11693
if let Cli {
11794
command: Command::Build(mut build),
@@ -162,3 +139,48 @@ impl Toml {
162139
metadata.get("rust-gpu")?.as_table()
163140
}
164141
}
142+
143+
/// Construct the cli parameters to run a `cargo gpu build` command from a TOML table.
144+
fn construct_build_parameters_from_toml_table(
145+
toml_type: &str,
146+
table: &toml::map::Map<String, toml::Value>,
147+
) -> Result<Vec<String>, anyhow::Error> {
148+
let build_table = table
149+
.get("build")
150+
.with_context(|| "toml is missing the 'build' table")?
151+
.as_table()
152+
.with_context(|| {
153+
format!("toml file's '{toml_type}.metadata.rust-gpu.build' property is not a table")
154+
})?;
155+
let mut parameters: Vec<String> = build_table
156+
.into_iter()
157+
.map(|(key, val)| -> anyhow::Result<Vec<String>> {
158+
Ok(match val {
159+
toml::Value::String(string) => vec![format!("--{key}"), string.clone()],
160+
toml::Value::Boolean(truthy) => {
161+
if *truthy {
162+
vec![format!("--{key}")]
163+
} else {
164+
vec![]
165+
}
166+
}
167+
toml::Value::Integer(_)
168+
| toml::Value::Float(_)
169+
| toml::Value::Datetime(_)
170+
| toml::Value::Array(_)
171+
| toml::Value::Table(_) => {
172+
let mut value = String::new();
173+
let ser = toml::ser::ValueSerializer::new(&mut value);
174+
serde::Serialize::serialize(val, ser)?;
175+
vec![format!("--{key}"), value]
176+
}
177+
})
178+
})
179+
.collect::<anyhow::Result<Vec<Vec<String>>>>()?
180+
.into_iter()
181+
.flatten()
182+
.collect();
183+
parameters.insert(0, "cargo-gpu".to_owned());
184+
parameters.insert(1, "build".to_owned());
185+
Ok(parameters)
186+
}

0 commit comments

Comments
 (0)