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
316use 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 ) ?;
0 commit comments