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.
2
15
3
16
use std:: {
4
17
fs, io,
@@ -20,8 +33,7 @@ use crate::{
20
33
user_output,
21
34
} ;
22
35
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.
25
37
#[ derive( Debug , Clone , serde:: Deserialize , serde:: Serialize ) ]
26
38
#[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
27
39
#[ non_exhaustive]
@@ -34,17 +46,32 @@ pub struct Install {
34
46
#[ serde( alias = "package" ) ]
35
47
pub shader_crate : PathBuf ,
36
48
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 {
37
62
#[ expect(
38
63
rustdoc:: bare_urls,
39
64
clippy:: doc_markdown,
40
65
reason = "The URL should appear literally like this. But Clippy & rustdoc want a markdown clickable link"
41
66
) ]
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".
44
70
#[ cfg_attr( feature = "clap" , clap( long) ) ]
45
71
pub spirv_builder_source : Option < String > ,
46
72
47
- /// Version of `spirv-builder` dependency.
73
+ /// Version of [`spirv-builder`](spirv_builder) dependency.
74
+ ///
48
75
/// * If `--spirv-builder-source` is not set, then this is assumed to be a crates.io semantic
49
76
/// version such as "0.9.0".
50
77
/// * If `--spirv-builder-source` is set, then this is assumed to be a Git "commitsh", such
@@ -58,12 +85,12 @@ pub struct Install {
58
85
59
86
/// Assume "yes" to "Install Rust toolchain: [y/n]" prompt.
60
87
///
61
- /// Defaults to `false` in cli, `true` in [`Default`]
88
+ /// Defaults to `false` in cli, `true` in [`Default`] implementation.
62
89
#[ cfg_attr( feature = "clap" , clap( long, action) ) ]
63
90
pub auto_install_rust_toolchain : bool ,
64
91
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.
67
94
#[ cfg_attr( feature = "clap" , clap( long = "no-clear-target" , default_value = "true" , action = clap:: ArgAction :: SetFalse ) ) ]
68
95
pub clear_target : bool ,
69
96
@@ -92,16 +119,10 @@ pub struct Install {
92
119
pub force_overwrite_lockfiles_v4_to_v3 : bool ,
93
120
}
94
121
95
- impl Install {
96
- /// Creates a default installation settings for a shader crate of the given path.
122
+ impl Default for InstallParams {
97
123
#[ 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 {
103
125
Self {
104
- shader_crate : shader_crate. into ( ) ,
105
126
spirv_builder_source : None ,
106
127
spirv_builder_version : None ,
107
128
rebuild_codegen : false ,
@@ -110,6 +131,33 @@ impl Install {
110
131
force_overwrite_lockfiles_v4_to_v3 : false ,
111
132
}
112
133
}
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
+ }
113
161
114
162
/// Installs the binary pair and return the [`InstalledBackend`],
115
163
/// from which you can create [`SpirvBuilder`] instances.
@@ -139,8 +187,8 @@ impl Install {
139
187
140
188
let source = SpirvSource :: new (
141
189
& 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 ( ) ,
144
192
) ?;
145
193
let install_dir = source. install_dir ( ) ?;
146
194
@@ -167,7 +215,8 @@ impl Install {
167
215
}
168
216
169
217
// 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 ;
171
220
if skip_rebuild {
172
221
log:: info!( "...and so we are aborting the install step." ) ;
173
222
} else {
@@ -220,7 +269,7 @@ impl Install {
220
269
fs:: rename ( & dylib_path, & dest_dylib_path)
221
270
. map_err ( InstallError :: MoveRustcCodegenSpirvDylib ) ?;
222
271
223
- if self . clear_target {
272
+ if self . params . clear_target {
224
273
log:: warn!( "clearing target dir {}" , target. display( ) ) ;
225
274
fs:: remove_dir_all ( & target)
226
275
. map_err ( InstallError :: RemoveRustcCodegenSpirvTargetDir ) ?;
0 commit comments