11//! This module provides a `rust-gpu` shader crate builder
22//! usable inside of build scripts or as a part of CLI.
33
4- use std:: { io, process:: Stdio } ;
4+ use std:: { io, path :: Path , process:: Stdio } ;
55
66use crate :: {
77 lockfile:: { LockfileMismatchError , LockfileMismatchHandler } ,
8+ metadata:: { RustGpuMetadata , RustGpuMetadataSource } ,
89 spirv_builder:: { CompileResult , SpirvBuilder , SpirvBuilderError } ,
910 spirv_cache:: {
1011 backend:: {
@@ -23,14 +24,33 @@ use crate::{
2324#[ cfg( feature = "watch" ) ]
2425use crate :: spirv_builder:: SpirvWatcher ;
2526
26- /// Parameters for [`CargoGpuBuilder::new()`].
27- #[ derive( Debug , Clone ) ]
27+ /// Metadata specific to the build process.
28+ #[ derive( Clone , Debug , Default , serde:: Deserialize , serde:: Serialize ) ]
29+ #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
2830#[ 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 ,
31+ pub struct CargoGpuBuildMetadata {
32+ /// The flattened [`SpirvBuilder`].
33+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
34+ #[ serde( flatten) ]
35+ pub spirv_builder : SpirvBuilder ,
36+ }
37+
38+ impl From < SpirvBuilder > for CargoGpuBuildMetadata {
39+ #[ inline]
40+ fn from ( spirv_builder : SpirvBuilder ) -> Self {
41+ Self { spirv_builder }
42+ }
43+ }
44+
45+ /// Metadata specific to the codegen backend installation process.
46+ #[ derive( Clone , Debug , Default , serde:: Deserialize , serde:: Serialize ) ]
47+ #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
48+ #[ non_exhaustive]
49+ pub struct CargoGpuInstallMetadata {
50+ /// The flattened [`SpirvCodegenBackendInstaller`].
51+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
52+ #[ serde( flatten) ]
53+ pub spirv_installer : SpirvCodegenBackendInstaller ,
3454 /// There is a tricky situation where a shader crate that depends on workspace config can have
3555 /// a different `Cargo.lock` lockfile version from the the workspace's `Cargo.lock`. This can
3656 /// prevent builds when an old Rust toolchain doesn't recognise the newer lockfile version.
@@ -52,7 +72,38 @@ pub struct CargoGpuBuilderParams<W, T, C, O, E> {
5272 /// way source URLs are encoded. See these PRs for more details:
5373 /// * <https://github.com/rust-lang/cargo/pull/12280>
5474 /// * <https://github.com/rust-lang/cargo/pull/14595>
75+ #[ cfg_attr( feature = "clap" , clap( long, action, verbatim_doc_comment) ) ]
5576 pub force_overwrite_lockfiles_v4_to_v3 : bool ,
77+ }
78+
79+ /// Metadata for both shader crate build and codegen backend installation.
80+ #[ derive( Clone , Debug , Default , serde:: Deserialize , serde:: Serialize ) ]
81+ #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
82+ #[ non_exhaustive]
83+ pub struct CargoGpuMetadata {
84+ /// Parameters of the shader crate build.
85+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
86+ pub build : CargoGpuBuildMetadata ,
87+ /// Parameters of the codegen backend installation for the shader crate.
88+ #[ cfg_attr( feature = "clap" , clap( flatten) ) ]
89+ pub install : CargoGpuInstallMetadata ,
90+ }
91+
92+ impl RustGpuMetadata for CargoGpuMetadata {
93+ #[ inline]
94+ fn patch < P > ( & mut self , _shader_crate : P , _source : RustGpuMetadataSource < ' _ > )
95+ where
96+ P : AsRef < Path > ,
97+ {
98+ }
99+ }
100+
101+ /// Parameters for [`CargoGpuBuilder::new()`].
102+ #[ derive( Debug , Clone ) ]
103+ #[ non_exhaustive]
104+ pub struct CargoGpuBuilderParams < W , T , C , O , E > {
105+ /// Parameters of the shader crate build & codegen backend installation.
106+ pub metadata : CargoGpuMetadata ,
56107 /// Writer of user output.
57108 pub writer : W ,
58109 /// Callbacks to halt toolchain installation.
@@ -62,41 +113,41 @@ pub struct CargoGpuBuilderParams<W, T, C, O, E> {
62113}
63114
64115impl < W , T , C , O , E > CargoGpuBuilderParams < W , T , C , O , E > {
65- /// Replaces build parameters of the shader crate.
116+ /// Replaces of the shader crate build & codegen backend installation .
66117 #[ inline]
67118 #[ must_use]
68- pub fn build ( self , build : SpirvBuilder ) -> Self {
69- Self { build , ..self }
119+ pub fn metadata ( self , metadata : CargoGpuMetadata ) -> Self {
120+ Self { metadata , ..self }
70121 }
71122
72- /// Replaces codegen backend installation parameters of the shader crate.
123+ /// Replaces build parameters of the shader crate.
73124 #[ inline]
74125 #[ must_use]
75- pub fn install ( self , install : SpirvCodegenBackendInstaller ) -> Self {
76- Self { install, ..self }
126+ pub fn build ( self , build : CargoGpuBuildMetadata ) -> Self {
127+ let metadata = CargoGpuMetadata {
128+ build,
129+ ..self . metadata
130+ } ;
131+ Self { metadata, ..self }
77132 }
78133
79- /// Sets whether to force overwriting lockfiles from v4 to v3 .
134+ /// Replaces codegen backend installation parameters of the shader crate .
80135 #[ inline]
81136 #[ 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- }
137+ pub fn install ( self , install : CargoGpuInstallMetadata ) -> Self {
138+ let metadata = CargoGpuMetadata {
139+ install,
140+ ..self . metadata
141+ } ;
142+ Self { metadata, ..self }
90143 }
91144
92145 /// Replaces the writer of user output.
93146 #[ inline]
94147 #[ must_use]
95148 pub fn writer < NW > ( self , writer : NW ) -> CargoGpuBuilderParams < NW , T , C , O , E > {
96149 CargoGpuBuilderParams {
97- build : self . build ,
98- install : self . install ,
99- force_overwrite_lockfiles_v4_to_v3 : self . force_overwrite_lockfiles_v4_to_v3 ,
150+ metadata : self . metadata ,
100151 writer,
101152 halt : self . halt ,
102153 stdio_cfg : self . stdio_cfg ,
@@ -111,9 +162,7 @@ impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
111162 halt : HaltToolchainInstallation < NT , NC > ,
112163 ) -> CargoGpuBuilderParams < W , NT , NC , O , E > {
113164 CargoGpuBuilderParams {
114- build : self . build ,
115- install : self . install ,
116- force_overwrite_lockfiles_v4_to_v3 : self . force_overwrite_lockfiles_v4_to_v3 ,
165+ metadata : self . metadata ,
117166 writer : self . writer ,
118167 halt,
119168 stdio_cfg : self . stdio_cfg ,
@@ -128,9 +177,7 @@ impl<W, T, C, O, E> CargoGpuBuilderParams<W, T, C, O, E> {
128177 stdio_cfg : StdioCfg < NO , NE > ,
129178 ) -> CargoGpuBuilderParams < W , T , C , NO , NE > {
130179 CargoGpuBuilderParams {
131- build : self . build ,
132- install : self . install ,
133- force_overwrite_lockfiles_v4_to_v3 : self . force_overwrite_lockfiles_v4_to_v3 ,
180+ metadata : self . metadata ,
134181 writer : self . writer ,
135182 halt : self . halt ,
136183 stdio_cfg,
@@ -147,11 +194,18 @@ pub type DefaultCargoGpuBuilderParams = CargoGpuBuilderParams<
147194 InheritStderr ,
148195> ;
149196
150- impl From < SpirvBuilder > for DefaultCargoGpuBuilderParams {
197+ impl < T > From < T > for DefaultCargoGpuBuilderParams
198+ where
199+ T : Into < CargoGpuBuildMetadata > ,
200+ {
151201 #[ inline]
152- fn from ( build : SpirvBuilder ) -> Self {
202+ fn from ( value : T ) -> Self {
203+ let metadata = CargoGpuMetadata {
204+ build : value. into ( ) ,
205+ install : CargoGpuInstallMetadata :: default ( ) ,
206+ } ;
153207 Self {
154- build ,
208+ metadata ,
155209 ..Self :: default ( )
156210 }
157211 }
@@ -161,9 +215,7 @@ impl Default for DefaultCargoGpuBuilderParams {
161215 #[ inline]
162216 fn default ( ) -> Self {
163217 Self {
164- build : SpirvBuilder :: default ( ) ,
165- install : SpirvCodegenBackendInstaller :: default ( ) ,
166- force_overwrite_lockfiles_v4_to_v3 : false ,
218+ metadata : CargoGpuMetadata :: default ( ) ,
167219 writer : io:: stdout ( ) ,
168220 halt : HaltToolchainInstallation :: noop ( ) ,
169221 stdio_cfg : StdioCfg :: inherit ( ) ,
@@ -212,23 +264,29 @@ where
212264 E : FnMut ( ) -> Stdio ,
213265 {
214266 let CargoGpuBuilderParams {
215- mut build,
216- install,
217- force_overwrite_lockfiles_v4_to_v3,
267+ metadata,
218268 mut writer,
219269 halt,
220270 mut stdio_cfg,
221271 } = params. into ( ) ;
272+ let CargoGpuMetadata { build, install } = metadata;
273+ let CargoGpuBuildMetadata {
274+ spirv_builder : mut builder,
275+ } = build;
276+ let CargoGpuInstallMetadata {
277+ spirv_installer : installer,
278+ force_overwrite_lockfiles_v4_to_v3,
279+ } = install;
222280
223- if build . target . is_none ( ) {
281+ if builder . target . is_none ( ) {
224282 return Err ( NewCargoGpuBuilderError :: MissingTarget ) ;
225283 }
226- let path_to_crate = build
284+ let path_to_crate = builder
227285 . path_to_crate
228286 . as_ref ( )
229287 . ok_or ( NewCargoGpuBuilderError :: MissingCratePath ) ?;
230288 let shader_crate = dunce:: canonicalize ( path_to_crate) ?;
231- build . path_to_crate = Some ( shader_crate. clone ( ) ) ;
289+ builder . path_to_crate = Some ( shader_crate. clone ( ) ) ;
232290
233291 let backend_install_params = SpirvCodegenBackendInstallParams :: from ( & shader_crate)
234292 . writer ( & mut writer)
@@ -240,7 +298,7 @@ where
240298 stdout : || ( stdio_cfg. stdout ) ( ) ,
241299 stderr : || ( stdio_cfg. stderr ) ( ) ,
242300 } ) ;
243- let codegen_backend = install . install ( backend_install_params) ?;
301+ let codegen_backend = installer . install ( backend_install_params) ?;
244302
245303 let lockfile_mismatch_handler = LockfileMismatchHandler :: new (
246304 & shader_crate,
@@ -250,12 +308,12 @@ where
250308
251309 #[ expect( clippy:: unreachable, reason = "target was set" ) ]
252310 codegen_backend
253- . configure_spirv_builder ( & mut build )
311+ . configure_spirv_builder ( & mut builder )
254312 . unwrap_or_else ( |_| unreachable ! ( "target was set before calling this function" ) ) ;
255313
256314 Ok ( Self {
257- builder : build ,
258- installer : install ,
315+ builder,
316+ installer,
259317 codegen_backend,
260318 lockfile_mismatch_handler,
261319 writer,
0 commit comments