Skip to content

Commit f3fe48f

Browse files
committed
Move user consent flag into cargo-gpu CLI struct
1 parent 3590ceb commit f3fe48f

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

crates/cargo-gpu/src/build.rs

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use cargo_gpu_build::{
1212

1313
use crate::{linkage::Linkage, user_consent::ask_for_user_consent};
1414

15-
/// Args for just a build
15+
/// Args for just a build.
1616
#[derive(Debug, Clone, clap::Parser, serde::Deserialize, serde::Serialize)]
1717
#[non_exhaustive]
1818
#[expect(clippy::module_name_repetitions, reason = "it is intended")]
@@ -25,12 +25,12 @@ pub struct BuildArgs {
2525
#[clap(long, short, action)]
2626
pub watch: bool,
2727

28-
/// The flattened [`SpirvBuilder`]
28+
/// The flattened [`SpirvBuilder`].
2929
#[clap(flatten)]
3030
#[serde(flatten)]
3131
pub spirv_builder: SpirvBuilder,
3232

33-
/// Renames the manifest.json file to the given name
33+
/// Renames the manifest.json file to the given name.
3434
#[clap(long, short, default_value = "manifest.json")]
3535
pub manifest_file: String,
3636
}
@@ -47,15 +47,29 @@ impl Default for BuildArgs {
4747
}
4848
}
4949

50+
/// Args for just an install.
51+
#[derive(Clone, Debug, clap::Parser, serde::Deserialize, serde::Serialize)]
52+
#[non_exhaustive]
53+
pub struct InstallArgs {
54+
/// The flattened [`Install`].
55+
#[clap(flatten)]
56+
#[serde(flatten)]
57+
pub backend: Install,
58+
59+
/// Assume "yes" to "Install Rust toolchain: [y/n]" prompt.
60+
#[clap(long, action)]
61+
pub auto_install_rust_toolchain: bool,
62+
}
63+
5064
/// `cargo build` subcommands
5165
#[derive(Clone, Debug, clap::Parser, serde::Deserialize, serde::Serialize)]
5266
#[non_exhaustive]
5367
pub struct Build {
54-
/// CLI args for install the `rust-gpu` compiler and components
68+
/// CLI args for install the `rust-gpu` compiler and components.
5569
#[clap(flatten)]
56-
pub install: Install,
70+
pub install: InstallArgs,
5771

58-
/// CLI args for configuring the build of the shader
72+
/// CLI args for configuring the build of the shader.
5973
#[clap(flatten)]
6074
pub build: BuildArgs,
6175
}
@@ -68,15 +82,15 @@ impl Build {
6882
/// Returns an error if the build process fails somehow.
6983
#[inline]
7084
pub fn run(&mut self) -> anyhow::Result<()> {
71-
self.build.spirv_builder.path_to_crate = Some(self.install.shader_crate.clone());
85+
self.build.spirv_builder.path_to_crate = Some(self.install.backend.shader_crate.clone());
7286

73-
let halt = ask_for_user_consent(self.install.params.auto_install_rust_toolchain);
87+
let halt = ask_for_user_consent(self.install.auto_install_rust_toolchain);
7488
let crate_builder_params = ShaderCrateBuilderParams::from(self.build.spirv_builder.clone())
75-
.install(self.install.params.clone())
89+
.install(self.install.backend.params.clone())
7690
.halt(halt);
7791
let crate_builder = ShaderCrateBuilder::new(crate_builder_params)?;
7892

79-
self.install = crate_builder.installed_backend_args.clone();
93+
self.install.backend = crate_builder.installed_backend_args.clone();
8094
self.build.spirv_builder = crate_builder.builder.clone();
8195

8296
// Ensure the shader output dir exists
@@ -130,7 +144,7 @@ impl Build {
130144
}
131145
}
132146

133-
/// Parses compilation result from [`SpirvBuilder`] and writes it out to a file
147+
/// Parses compilation result from [`SpirvBuilder`] and writes it out to a file.
134148
fn parse_compilation_result(&self, result: &CompileResult) -> anyhow::Result<()> {
135149
let shaders = match &result.module {
136150
ModuleResult::MultiModule(modules) => {
@@ -157,10 +171,10 @@ impl Build {
157171
log::debug!(
158172
"linkage of {} relative to {}",
159173
path.display(),
160-
self.install.shader_crate.display()
174+
self.install.backend.shader_crate.display()
161175
);
162176
let spv_path = path
163-
.relative_to(&self.install.shader_crate)
177+
.relative_to(&self.install.backend.shader_crate)
164178
.map_or(path, |path_relative_to_shader_crate| {
165179
path_relative_to_shader_crate.to_path("")
166180
});
@@ -216,7 +230,7 @@ mod test {
216230
command: Command::Build(build),
217231
} = Cli::parse_from(args)
218232
{
219-
assert_eq!(shader_crate_path, build.install.shader_crate);
233+
assert_eq!(shader_crate_path, build.install.backend.shader_crate);
220234
assert_eq!(output_dir, build.build.output_dir);
221235

222236
// TODO:

crates/cargo-gpu/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ mod test {
103103
)
104104
.unwrap();
105105
assert!(!args.build.spirv_builder.release);
106-
assert!(args.install.params.auto_install_rust_toolchain);
106+
assert!(args.install.auto_install_rust_toolchain);
107107
}
108108

109109
#[test_log::test]
@@ -124,7 +124,7 @@ mod test {
124124

125125
let args = Config::clap_command_with_cargo_config(&shader_crate_path, vec![]).unwrap();
126126
assert!(!args.build.spirv_builder.release);
127-
assert!(args.install.params.auto_install_rust_toolchain);
127+
assert!(args.install.auto_install_rust_toolchain);
128128
}
129129

130130
fn update_cargo_output_dir() -> std::path::PathBuf {

crates/cargo-gpu/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ impl Command {
8585
command.install
8686
);
8787

88-
let skip_consent = command.install.params.auto_install_rust_toolchain;
88+
let skip_consent = command.install.auto_install_rust_toolchain;
8989
let halt = ask_for_user_consent(skip_consent);
9090
let install_params = InstallRunParams::default()
9191
.writer(std::io::stdout())
9292
.halt(halt)
9393
.stdio_cfg(StdioCfg::inherit());
94-
command.install.run(install_params)?;
94+
command.install.backend.run(install_params)?;
9595
}
9696
Self::Build(build) => {
97-
let shader_crate_path = &build.install.shader_crate;
97+
let shader_crate_path = &build.install.backend.shader_crate;
9898
let mut command =
9999
config::Config::clap_command_with_cargo_config(shader_crate_path, env_args)?;
100100
log::debug!("building with final merged arguments: {command:#?}");

crates/rustc_codegen_spirv-cache/src/backend.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ pub struct Install {
111111
}
112112

113113
/// Parameters of the codegen backend installation.
114-
#[expect(clippy::struct_excessive_bools, reason = "expected to have many bools")]
115114
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
116115
#[cfg_attr(feature = "clap", derive(clap::Parser))]
117116
#[non_exhaustive]
@@ -140,12 +139,6 @@ pub struct InstallParams {
140139
#[cfg_attr(feature = "clap", clap(long))]
141140
pub rebuild_codegen: bool,
142141

143-
/// Assume "yes" to "Install Rust toolchain: [y/n]" prompt.
144-
///
145-
/// Defaults to `false` in cli, `true` in [`Default`] implementation.
146-
#[cfg_attr(feature = "clap", clap(long, action))]
147-
pub auto_install_rust_toolchain: bool,
148-
149142
/// Clear target dir of `rustc_codegen_spirv` build after a successful build,
150143
/// saves about 200MiB of disk space.
151144
#[cfg_attr(feature = "clap", clap(long = "no-clear-target", default_value = "true", action = clap::ArgAction::SetFalse))]
@@ -183,7 +176,6 @@ impl Default for InstallParams {
183176
spirv_builder_source: None,
184177
spirv_builder_version: None,
185178
rebuild_codegen: false,
186-
auto_install_rust_toolchain: true,
187179
clear_target: true,
188180
force_overwrite_lockfiles_v4_to_v3: false,
189181
}

0 commit comments

Comments
 (0)