Skip to content

Commit 652b22c

Browse files
committed
Add install params which implement Default
1 parent 10bce9f commit 652b22c

File tree

6 files changed

+85
-48
lines changed

6 files changed

+85
-48
lines changed

crates/cargo-gpu-build/src/lib.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,17 @@
22

33
//! Rust GPU shader crate builder.
44
//!
5-
//! This library allows you to easily compile your rust-gpu shaders,
5+
//! This library allows you to easily compile your `rust-gpu` shaders,
66
//! without requiring you to fix your entire project to a specific toolchain.
77
//!
88
//! # How it works
99
//!
1010
//! This library manages installations of `rustc_codegen_spirv`
11-
//! using rust-gpu's [`rustc_codegen_spirv-cache`](cache) crate.
11+
//! using [`rustc_codegen_spirv-cache`](cache) crate.
1212
//!
13-
//! Then we continue to use rust-gpu's [`spirv-builder`](cache::spirv_builder) crate
13+
//! Then is uses [`spirv-builder`](cache::spirv_builder) crate
1414
//! to pass the many additional parameters required to configure rustc and our codegen backend,
15-
//! but provide you with a toolchain agnostic version that you may use from stable rustc.
16-
//! And a `cargo gpu` command line utility to simplify shader building even more.
17-
//!
18-
//! ## How we build the backend
19-
//!
20-
//! * retrieve the version of rust-gpu you want to use based on the version of the
21-
//! `spirv-std` dependency in your shader crate.
22-
//! * create a dummy project at `<cache_dir>/codegen/<version>/` that depends on
23-
//! `rustc_codegen_spirv`
24-
//! * use `cargo metadata` to `cargo update` the dummy project, which downloads the
25-
//! `rustc_codegen_spirv` crate into cargo's cache, and retrieve the path to the
26-
//! download location.
27-
//! * search for the required toolchain in `build.rs` of `rustc_codegen_spirv`
28-
//! * build it with the required toolchain version
29-
//! * copy out the binary and clean the target dir
15+
//! but provide you with a toolchain-agnostic version that you may use from stable rustc.
3016
//!
3117
//! ## Building shader crates
3218
//!

crates/cargo-gpu/src/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ impl Build {
7070
/// Returns an error if the build process fails somehow.
7171
#[inline]
7272
pub fn run(&mut self) -> anyhow::Result<()> {
73-
let skip_consent = self.install.auto_install_rust_toolchain;
73+
let skip_consent = self.install.params.auto_install_rust_toolchain;
7474
let halt_installation = ask_for_user_consent(skip_consent);
7575
let installed_backend = self.install.run(io::stdout(), halt_installation)?;
7676

7777
let _lockfile_mismatch_handler = LockfileMismatchHandler::new(
7878
&self.install.shader_crate,
7979
&installed_backend.toolchain_channel,
80-
self.install.force_overwrite_lockfiles_v4_to_v3,
80+
self.install.params.force_overwrite_lockfiles_v4_to_v3,
8181
)?;
8282

8383
let builder = &mut self.build.spirv_builder;

crates/cargo-gpu/src/config.rs

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

112112
#[test_log::test]
@@ -127,7 +127,7 @@ mod test {
127127

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

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

crates/cargo-gpu/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl Command {
6767
command.install
6868
);
6969

70-
let skip_consent = command.install.auto_install_rust_toolchain;
70+
let skip_consent = command.install.params.auto_install_rust_toolchain;
7171
let halt_installation = ask_for_user_consent(skip_consent);
7272
command.install.run(std::io::stdout(), halt_installation)?;
7373
}

crates/rustc_codegen_spirv-cache/src/install.rs

Lines changed: 70 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
1-
//! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
1+
//! This module deals with an installation a dedicated per-shader crate
2+
//! that has the `rust-gpu` codegen backend in it.
3+
//!
4+
//! This process could be described as follows:
5+
//! * first retrieve the version of rust-gpu you want to use based on the version of the
6+
//! `spirv-std` dependency in your shader crate,
7+
//! * then create a dummy project at `<cache_dir>/codegen/<version>/`
8+
//! that depends on `rustc_codegen_spirv`,
9+
//! * use `cargo metadata` to `cargo update` the dummy project, which downloads the
10+
//! `rustc_codegen_spirv` crate into cargo's cache, and retrieve the path to the
11+
//! download location,
12+
//! * search for the required toolchain in `build.rs` of `rustc_codegen_spirv`,
13+
//! * build it with the required toolchain version,
14+
//! * copy out the resulting dylib and clean the target directory.
215
316
use std::{
417
fs, io,
@@ -20,8 +33,7 @@ use crate::{
2033
user_output,
2134
};
2235

23-
/// Settings for an installation of `rustc_codegen_spirv`.
24-
#[expect(clippy::struct_excessive_bools, reason = "expected to have many bools")]
36+
/// Settings for an installation of the codegen backend.
2537
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
2638
#[cfg_attr(feature = "clap", derive(clap::Parser))]
2739
#[non_exhaustive]
@@ -34,17 +46,32 @@ pub struct Install {
3446
#[serde(alias = "package")]
3547
pub shader_crate: PathBuf,
3648

49+
/// Parameters of the codegen backend installation.
50+
#[cfg_attr(feature = "clap", clap(flatten))]
51+
#[serde(flatten)]
52+
pub params: InstallParams,
53+
}
54+
55+
/// Parameters of the codegen backend installation.
56+
#[expect(clippy::struct_excessive_bools, reason = "expected to have many bools")]
57+
#[expect(clippy::module_name_repetitions, reason = "this is intended")]
58+
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
59+
#[cfg_attr(feature = "clap", derive(clap::Parser))]
60+
#[non_exhaustive]
61+
pub struct InstallParams {
3762
#[expect(
3863
rustdoc::bare_urls,
3964
clippy::doc_markdown,
4065
reason = "The URL should appear literally like this. But Clippy & rustdoc want a markdown clickable link"
4166
)]
42-
/// Source of `spirv-builder` dependency
43-
/// Eg: "https://github.com/Rust-GPU/rust-gpu"
67+
/// Source of [`spirv-builder`](spirv_builder) dependency.
68+
///
69+
/// E.g. "https://github.com/Rust-GPU/rust-gpu".
4470
#[cfg_attr(feature = "clap", clap(long))]
4571
pub spirv_builder_source: Option<String>,
4672

47-
/// Version of `spirv-builder` dependency.
73+
/// Version of [`spirv-builder`](spirv_builder) dependency.
74+
///
4875
/// * If `--spirv-builder-source` is not set, then this is assumed to be a crates.io semantic
4976
/// version such as "0.9.0".
5077
/// * If `--spirv-builder-source` is set, then this is assumed to be a Git "commitsh", such
@@ -58,12 +85,12 @@ pub struct Install {
5885

5986
/// Assume "yes" to "Install Rust toolchain: [y/n]" prompt.
6087
///
61-
/// Defaults to `false` in cli, `true` in [`Default`]
88+
/// Defaults to `false` in cli, `true` in [`Default`] implementation.
6289
#[cfg_attr(feature = "clap", clap(long, action))]
6390
pub auto_install_rust_toolchain: bool,
6491

65-
/// Clear target dir of `rustc_codegen_spirv` build after a successful build, saves about
66-
/// 200MiB of disk space.
92+
/// Clear target dir of `rustc_codegen_spirv` build after a successful build,
93+
/// saves about 200MiB of disk space.
6794
#[cfg_attr(feature = "clap", clap(long = "no-clear-target", default_value = "true", action = clap::ArgAction::SetFalse))]
6895
pub clear_target: bool,
6996

@@ -92,16 +119,10 @@ pub struct Install {
92119
pub force_overwrite_lockfiles_v4_to_v3: bool,
93120
}
94121

95-
impl Install {
96-
/// Creates a default installation settings for a shader crate of the given path.
122+
impl Default for InstallParams {
97123
#[inline]
98-
#[must_use]
99-
pub fn from_shader_crate<P>(shader_crate: P) -> Self
100-
where
101-
P: Into<PathBuf>,
102-
{
124+
fn default() -> Self {
103125
Self {
104-
shader_crate: shader_crate.into(),
105126
spirv_builder_source: None,
106127
spirv_builder_version: None,
107128
rebuild_codegen: false,
@@ -110,6 +131,33 @@ impl Install {
110131
force_overwrite_lockfiles_v4_to_v3: false,
111132
}
112133
}
134+
}
135+
136+
impl Install {
137+
/// Creates a default installation settings for a shader crate of the given path.
138+
#[inline]
139+
#[must_use]
140+
pub fn from_shader_crate<C>(shader_crate: C) -> Self
141+
where
142+
C: Into<PathBuf>,
143+
{
144+
Self::from_shader_crate_with_params(shader_crate, InstallParams::default())
145+
}
146+
147+
/// Creates an installation settings for a shader crate of the given path
148+
/// and the given parameters.
149+
#[inline]
150+
#[must_use]
151+
pub fn from_shader_crate_with_params<C, P>(shader_crate: C, params: P) -> Self
152+
where
153+
C: Into<PathBuf>,
154+
P: Into<InstallParams>,
155+
{
156+
Self {
157+
shader_crate: shader_crate.into(),
158+
params: params.into(),
159+
}
160+
}
113161

114162
/// Installs the binary pair and return the [`InstalledBackend`],
115163
/// from which you can create [`SpirvBuilder`] instances.
@@ -139,8 +187,8 @@ impl Install {
139187

140188
let source = SpirvSource::new(
141189
&self.shader_crate,
142-
self.spirv_builder_source.as_deref(),
143-
self.spirv_builder_version.as_deref(),
190+
self.params.spirv_builder_source.as_deref(),
191+
self.params.spirv_builder_version.as_deref(),
144192
)?;
145193
let install_dir = source.install_dir()?;
146194

@@ -167,7 +215,8 @@ impl Install {
167215
}
168216

169217
// if `source` is a path, always rebuild
170-
let skip_rebuild = !source.is_path() && dest_dylib_path.is_file() && !self.rebuild_codegen;
218+
let skip_rebuild =
219+
!source.is_path() && dest_dylib_path.is_file() && !self.params.rebuild_codegen;
171220
if skip_rebuild {
172221
log::info!("...and so we are aborting the install step.");
173222
} else {
@@ -220,7 +269,7 @@ impl Install {
220269
fs::rename(&dylib_path, &dest_dylib_path)
221270
.map_err(InstallError::MoveRustcCodegenSpirvDylib)?;
222271

223-
if self.clear_target {
272+
if self.params.clear_target {
224273
log::warn!("clearing target dir {}", target.display());
225274
fs::remove_dir_all(&target)
226275
.map_err(InstallError::RemoveRustcCodegenSpirvTargetDir)?;

crates/rustc_codegen_spirv-cache/src/toolchain.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::process::{Command, Stdio};
55

66
use crate::command::{execute_command, CommandExecError};
77

8-
/// Allows to halt the installation process of toolchain or its required components.
8+
/// Allows to halt the installation process of toolchain or its [required components](REQUIRED_TOOLCHAIN_COMPONENTS).
99
#[derive(Debug, Clone, Copy)]
1010
#[expect(clippy::exhaustive_structs, reason = "intended to be exhaustive")]
1111
pub struct HaltToolchainInstallation<T, C> {
@@ -16,7 +16,7 @@ pub struct HaltToolchainInstallation<T, C> {
1616
}
1717

1818
impl HaltToolchainInstallation<(), ()> {
19-
/// Do not halt the installation process of toolchain or its required components.
19+
/// Do not halt the installation process of toolchain or its [required components](REQUIRED_TOOLCHAIN_COMPONENTS).
2020
///
2121
/// Calling either [`on_toolchain_install`] or [`on_components_install`]
2222
/// returns [`Ok`] without any side effects.
@@ -134,7 +134,8 @@ pub fn install_toolchain(channel: &str) -> Result<(), CommandExecError> {
134134
/// Components which are required to be installed for a toolchain to be usable with `rust-gpu`.
135135
pub const REQUIRED_TOOLCHAIN_COMPONENTS: [&str; 3] = ["rust-src", "rustc-dev", "llvm-tools"];
136136

137-
/// Checks if all the required components of the given toolchain are installed using `rustup`.
137+
/// Checks if all the [required components](REQUIRED_TOOLCHAIN_COMPONENTS)
138+
/// of the given toolchain are installed using `rustup`.
138139
///
139140
/// # Errors
140141
///
@@ -163,7 +164,8 @@ pub fn all_required_toolchain_components_installed(
163164
Ok(installed)
164165
}
165166

166-
/// Installs all the required components for the given toolchain using `rustup`.
167+
/// Installs all the [required components](REQUIRED_TOOLCHAIN_COMPONENTS)
168+
/// for the given toolchain using `rustup`.
167169
///
168170
/// # Errors
169171
///

0 commit comments

Comments
 (0)