@@ -23,14 +23,33 @@ use crate::{
23
23
#[ cfg( feature = "watch" ) ]
24
24
use crate :: spirv_builder:: SpirvWatcher ;
25
25
26
- /// Parameters for [`CargoGpuBuilder::new()`].
27
- #[ derive( Debug , Clone ) ]
26
+ /// Metadata specific to the build process.
27
+ #[ derive( Clone , Debug , Default , serde:: Deserialize , serde:: Serialize ) ]
28
+ #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
28
29
#[ non_exhaustive]
29
- pub struct CargoGpuBuilderParams < W , T , C , O , E > {
30
- /// Parameters of the shader crate build.
31
- pub build : SpirvBuilder ,
32
- /// Parameters of the codegen backend installation for the shader crate.
33
- pub install : SpirvCodegenBackendInstaller ,
30
+ pub struct CargoGpuBuildMetadata {
31
+ /// The flattened [`SpirvBuilder`].
32
+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
33
+ #[ serde( flatten) ]
34
+ pub spirv_builder : SpirvBuilder ,
35
+ }
36
+
37
+ impl From < SpirvBuilder > for CargoGpuBuildMetadata {
38
+ #[ inline]
39
+ fn from ( spirv_builder : SpirvBuilder ) -> Self {
40
+ Self { spirv_builder }
41
+ }
42
+ }
43
+
44
+ /// Metadata specific to the codegen backend installation process.
45
+ #[ derive( Clone , Debug , Default , serde:: Deserialize , serde:: Serialize ) ]
46
+ #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
47
+ #[ non_exhaustive]
48
+ pub struct CargoGpuInstallMetadata {
49
+ /// The flattened [`SpirvCodegenBackendInstaller`].
50
+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
51
+ #[ serde( flatten) ]
52
+ pub spirv_installer : SpirvCodegenBackendInstaller ,
34
53
/// There is a tricky situation where a shader crate that depends on workspace config can have
35
54
/// a different `Cargo.lock` lockfile version from the the workspace's `Cargo.lock`. This can
36
55
/// prevent builds when an old Rust toolchain doesn't recognise the newer lockfile version.
@@ -52,7 +71,29 @@ pub struct CargoGpuBuilderParams<W, T, C, O, E> {
52
71
/// way source URLs are encoded. See these PRs for more details:
53
72
/// * <https://github.com/rust-lang/cargo/pull/12280>
54
73
/// * <https://github.com/rust-lang/cargo/pull/14595>
74
+ #[ cfg_attr( feature = "clap" , clap( long, action, verbatim_doc_comment) ) ]
55
75
pub force_overwrite_lockfiles_v4_to_v3 : bool ,
76
+ }
77
+
78
+ /// Metadata for both shader crate build and codegen backend installation.
79
+ #[ derive( Clone , Debug , Default , serde:: Deserialize , serde:: Serialize ) ]
80
+ #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
81
+ #[ non_exhaustive]
82
+ pub struct CargoGpuMetadata {
83
+ /// Parameters of the shader crate build.
84
+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
85
+ pub build : CargoGpuBuildMetadata ,
86
+ /// Parameters of the codegen backend installation for the shader crate.
87
+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
88
+ pub install : CargoGpuInstallMetadata ,
89
+ }
90
+
91
+ /// Parameters for [`CargoGpuBuilder::new()`].
92
+ #[ derive( Debug , Clone ) ]
93
+ #[ non_exhaustive]
94
+ pub struct CargoGpuBuilderParams < W , T , C , O , E > {
95
+ /// Parameters of the shader crate build & codegen backend installation.
96
+ pub metadata : CargoGpuMetadata ,
56
97
/// Writer of user output.
57
98
pub writer : W ,
58
99
/// Callbacks to halt toolchain installation.
@@ -62,41 +103,41 @@ pub struct CargoGpuBuilderParams<W, T, C, O, E> {
62
103
}
63
104
64
105
impl < W , T , C , O , E > CargoGpuBuilderParams < W , T , C , O , E > {
65
- /// Replaces build parameters of the shader crate.
106
+ /// Replaces of the shader crate build & codegen backend installation .
66
107
#[ inline]
67
108
#[ must_use]
68
- pub fn build ( self , build : SpirvBuilder ) -> Self {
69
- Self { build , ..self }
109
+ pub fn metadata ( self , metadata : CargoGpuMetadata ) -> Self {
110
+ Self { metadata , ..self }
70
111
}
71
112
72
- /// Replaces codegen backend installation parameters of the shader crate.
113
+ /// Replaces build parameters of the shader crate.
73
114
#[ inline]
74
115
#[ must_use]
75
- pub fn install ( self , install : SpirvCodegenBackendInstaller ) -> Self {
76
- Self { install, ..self }
116
+ pub fn build ( self , build : CargoGpuBuildMetadata ) -> Self {
117
+ let metadata = CargoGpuMetadata {
118
+ build,
119
+ ..self . metadata
120
+ } ;
121
+ Self { metadata, ..self }
77
122
}
78
123
79
- /// Sets whether to force overwriting lockfiles from v4 to v3 .
124
+ /// Replaces codegen backend installation parameters of the shader crate .
80
125
#[ inline]
81
126
#[ must_use]
82
- pub fn force_overwrite_lockfiles_v4_to_v3 (
83
- self ,
84
- force_overwrite_lockfiles_v4_to_v3 : bool ,
85
- ) -> Self {
86
- Self {
87
- force_overwrite_lockfiles_v4_to_v3,
88
- ..self
89
- }
127
+ pub fn install ( self , install : CargoGpuInstallMetadata ) -> Self {
128
+ let metadata = CargoGpuMetadata {
129
+ install,
130
+ ..self . metadata
131
+ } ;
132
+ Self { metadata, ..self }
90
133
}
91
134
92
135
/// Replaces the writer of user output.
93
136
#[ inline]
94
137
#[ must_use]
95
138
pub fn writer < NW > ( self , writer : NW ) -> CargoGpuBuilderParams < NW , T , C , O , E > {
96
139
CargoGpuBuilderParams {
97
- build : self . build ,
98
- install : self . install ,
99
- force_overwrite_lockfiles_v4_to_v3 : self . force_overwrite_lockfiles_v4_to_v3 ,
140
+ metadata : self . metadata ,
100
141
writer,
101
142
halt : self . halt ,
102
143
stdio_cfg : self . stdio_cfg ,
@@ -111,9 +152,7 @@ impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
111
152
halt : HaltToolchainInstallation < NT , NC > ,
112
153
) -> CargoGpuBuilderParams < W , NT , NC , O , E > {
113
154
CargoGpuBuilderParams {
114
- build : self . build ,
115
- install : self . install ,
116
- force_overwrite_lockfiles_v4_to_v3 : self . force_overwrite_lockfiles_v4_to_v3 ,
155
+ metadata : self . metadata ,
117
156
writer : self . writer ,
118
157
halt,
119
158
stdio_cfg : self . stdio_cfg ,
@@ -128,9 +167,7 @@ impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
128
167
stdio_cfg : StdioCfg < NO , NE > ,
129
168
) -> CargoGpuBuilderParams < W , T , C , NO , NE > {
130
169
CargoGpuBuilderParams {
131
- build : self . build ,
132
- install : self . install ,
133
- force_overwrite_lockfiles_v4_to_v3 : self . force_overwrite_lockfiles_v4_to_v3 ,
170
+ metadata : self . metadata ,
134
171
writer : self . writer ,
135
172
halt : self . halt ,
136
173
stdio_cfg,
@@ -147,11 +184,18 @@ pub type DefaultCargoGpuBuilderParams = CargoGpuBuilderParams<
147
184
InheritStderr ,
148
185
> ;
149
186
150
- impl From < SpirvBuilder > for DefaultCargoGpuBuilderParams {
187
+ impl < T > From < T > for DefaultCargoGpuBuilderParams
188
+ where
189
+ T : Into < CargoGpuBuildMetadata > ,
190
+ {
151
191
#[ inline]
152
- fn from ( build : SpirvBuilder ) -> Self {
192
+ fn from ( value : T ) -> Self {
193
+ let metadata = CargoGpuMetadata {
194
+ build : value. into ( ) ,
195
+ install : CargoGpuInstallMetadata :: default ( ) ,
196
+ } ;
153
197
Self {
154
- build ,
198
+ metadata ,
155
199
..Self :: default ( )
156
200
}
157
201
}
@@ -161,9 +205,7 @@ impl Default for DefaultCargoGpuBuilderParams {
161
205
#[ inline]
162
206
fn default ( ) -> Self {
163
207
Self {
164
- build : SpirvBuilder :: default ( ) ,
165
- install : SpirvCodegenBackendInstaller :: default ( ) ,
166
- force_overwrite_lockfiles_v4_to_v3 : false ,
208
+ metadata : CargoGpuMetadata :: default ( ) ,
167
209
writer : io:: stdout ( ) ,
168
210
halt : HaltToolchainInstallation :: noop ( ) ,
169
211
stdio_cfg : StdioCfg :: inherit ( ) ,
@@ -212,23 +254,29 @@ where
212
254
E : FnMut ( ) -> Stdio ,
213
255
{
214
256
let CargoGpuBuilderParams {
215
- mut build,
216
- install,
217
- force_overwrite_lockfiles_v4_to_v3,
257
+ metadata,
218
258
mut writer,
219
259
halt,
220
260
mut stdio_cfg,
221
261
} = params. into ( ) ;
262
+ let CargoGpuMetadata { build, install } = metadata;
263
+ let CargoGpuBuildMetadata {
264
+ spirv_builder : mut builder,
265
+ } = build;
266
+ let CargoGpuInstallMetadata {
267
+ spirv_installer : installer,
268
+ force_overwrite_lockfiles_v4_to_v3,
269
+ } = install;
222
270
223
- if build . target . is_none ( ) {
271
+ if builder . target . is_none ( ) {
224
272
return Err ( NewCargoGpuBuilderError :: MissingTarget ) ;
225
273
}
226
- let path_to_crate = build
274
+ let path_to_crate = builder
227
275
. path_to_crate
228
276
. as_ref ( )
229
277
. ok_or ( NewCargoGpuBuilderError :: MissingCratePath ) ?;
230
278
let shader_crate = dunce:: canonicalize ( path_to_crate) ?;
231
- build . path_to_crate = Some ( shader_crate. clone ( ) ) ;
279
+ builder . path_to_crate = Some ( shader_crate. clone ( ) ) ;
232
280
233
281
let backend_install_params = SpirvCodegenBackendInstallParams :: from ( & shader_crate)
234
282
. writer ( & mut writer)
@@ -240,7 +288,7 @@ where
240
288
stdout : || ( stdio_cfg. stdout ) ( ) ,
241
289
stderr : || ( stdio_cfg. stderr ) ( ) ,
242
290
} ) ;
243
- let codegen_backend = install . install ( backend_install_params) ?;
291
+ let codegen_backend = installer . install ( backend_install_params) ?;
244
292
245
293
let lockfile_mismatch_handler = LockfileMismatchHandler :: new (
246
294
& shader_crate,
@@ -250,12 +298,12 @@ where
250
298
251
299
#[ expect( clippy:: unreachable, reason = "target was set" ) ]
252
300
codegen_backend
253
- . configure_spirv_builder ( & mut build )
301
+ . configure_spirv_builder ( & mut builder )
254
302
. unwrap_or_else ( |_| unreachable ! ( "target was set before calling this function" ) ) ;
255
303
256
304
Ok ( Self {
257
- builder : build ,
258
- installer : install ,
305
+ builder,
306
+ installer,
259
307
codegen_backend,
260
308
lockfile_mismatch_handler,
261
309
writer,
0 commit comments