1+ //! `cargo gpu build`, analogous to `cargo build`
2+
13#![ allow( clippy:: shadow_reuse, reason = "let's not be silly" ) ]
24#![ allow( clippy:: unwrap_used, reason = "this is basically a test" ) ]
3- //! `cargo gpu build`, analogous to `cargo build`
45
5- use anyhow:: Context as _;
6- use spirv_builder:: { CompileResult , ModuleResult , SpirvBuilder } ;
76use std:: io:: { self , Write as _} ;
87use std:: path:: PathBuf ;
98
10- use crate :: user_consent:: ask_for_user_consent;
11- use crate :: Install ;
9+ use anyhow:: Context as _;
10+ use rustc_codegen_spirv_cache:: install:: Install ;
11+ use spirv_builder:: { CompileResult , ModuleResult , SpirvBuilder } ;
1212
1313use crate :: linkage:: Linkage ;
1414use crate :: lockfile:: LockfileMismatchHandler ;
15+ use crate :: user_consent:: ask_for_user_consent;
1516
1617/// Args for just a build
17- #[ derive( Debug , Clone , serde:: Deserialize , serde:: Serialize ) ]
18- #[ cfg_attr( feature = "clap" , derive( clap:: Parser ) ) ]
18+ #[ derive( Debug , Clone , clap:: Parser , serde:: Deserialize , serde:: Serialize ) ]
19+ #[ non_exhaustive]
20+ #[ expect( clippy:: module_name_repetitions, reason = "it is intended" ) ]
1921pub struct BuildArgs {
2022 /// Path to the output directory for the compiled shaders.
21- #[ cfg_attr ( feature = " clap" , clap ( long, short, default_value = "./" ) ) ]
23+ #[ clap( long, short, default_value = "./" ) ]
2224 pub output_dir : PathBuf ,
2325
2426 /// Watch the shader crate directory and automatically recompile on changes.
25- #[ cfg( feature = "watch" ) ]
26- #[ cfg_attr( feature = "clap" , clap( long, short, action) ) ]
27+ #[ clap( long, short, action) ]
2728 pub watch : bool ,
2829
29- /// the flattened [`SpirvBuilder`]
30- #[ cfg_attr ( feature = " clap" , clap ( flatten) ) ]
30+ /// The flattened [`SpirvBuilder`]
31+ #[ clap( flatten) ]
3132 #[ serde( flatten) ]
3233 pub spirv_builder : SpirvBuilder ,
3334
34- ///Renames the manifest.json file to the given name
35- #[ cfg_attr ( feature = " clap" , clap ( long, short, default_value = "manifest.json" ) ) ]
35+ /// Renames the manifest.json file to the given name
36+ #[ clap( long, short, default_value = "manifest.json" ) ]
3637 pub manifest_file : String ,
3738}
3839
@@ -41,7 +42,6 @@ impl Default for BuildArgs {
4142 fn default ( ) -> Self {
4243 Self {
4344 output_dir : PathBuf :: from ( "./" ) ,
44- #[ cfg( feature = "watch" ) ]
4545 watch : false ,
4646 spirv_builder : SpirvBuilder :: default ( ) ,
4747 manifest_file : String :: from ( "manifest.json" ) ,
@@ -50,20 +50,25 @@ impl Default for BuildArgs {
5050}
5151
5252/// `cargo build` subcommands
53- #[ derive( Clone , Debug , serde:: Deserialize , serde:: Serialize ) ]
54- #[ cfg_attr ( feature = "clap" , derive ( clap :: Parser ) ) ]
53+ #[ derive( Clone , Debug , clap :: Parser , serde:: Deserialize , serde:: Serialize ) ]
54+ #[ non_exhaustive ]
5555pub struct Build {
5656 /// CLI args for install the `rust-gpu` compiler and components
57- #[ cfg_attr ( feature = " clap" , clap ( flatten) ) ]
57+ #[ clap( flatten) ]
5858 pub install : Install ,
5959
6060 /// CLI args for configuring the build of the shader
61- #[ cfg_attr ( feature = " clap" , clap ( flatten) ) ]
61+ #[ clap( flatten) ]
6262 pub build : BuildArgs ,
6363}
6464
6565impl Build {
66- /// Entrypoint
66+ /// Builds the shader crate.
67+ ///
68+ /// # Errors
69+ ///
70+ /// Returns an error if the build process fails somehow.
71+ #[ inline]
6772 pub fn run ( & mut self ) -> anyhow:: Result < ( ) > {
6873 let skip_consent = self . install . auto_install_rust_toolchain ;
6974 let halt_installation = ask_for_user_consent ( skip_consent) ;
@@ -98,14 +103,9 @@ impl Build {
98103 std:: env:: current_dir( ) ?. display( )
99104 ) ;
100105
101- #[ cfg( feature = "watch" ) ]
102- let watching = self . build . watch ;
103- #[ cfg( not( feature = "watch" ) ) ]
104- let watching = false ;
105- if watching {
106+ if self . build . watch {
106107 return self . watch ( ) ;
107108 }
108-
109109 self . build ( )
110110 }
111111
@@ -123,24 +123,18 @@ impl Build {
123123 /// Watches shader crate for changes using [`SpirvBuilder`]
124124 /// or returns an error depending on presence of `watch` feature.
125125 fn watch ( & self ) -> anyhow:: Result < ( ) > {
126- #[ cfg( feature = "watch" ) ]
127- {
128- let this = self . clone ( ) ;
129- self . build
130- . spirv_builder
131- . watch ( move |result, accept| {
132- let parse_result = this. parse_compilation_result ( & result) ;
133- if let Some ( accept) = accept {
134- accept. submit ( parse_result) ;
135- }
136- } ) ?
137- . context ( "should always return the first compile result" )
138- . flatten ( ) ?;
139- anyhow:: bail!( "unexpected end of watch" )
140- }
141-
142- #[ cfg( not( feature = "watch" ) ) ]
143- anyhow:: bail!( "cannot watch for changes without the `watch` feature" )
126+ let this = self . clone ( ) ;
127+ self . build
128+ . spirv_builder
129+ . watch ( move |result, accept| {
130+ let parse_result = this. parse_compilation_result ( & result) ;
131+ if let Some ( accept) = accept {
132+ accept. submit ( parse_result) ;
133+ }
134+ } ) ?
135+ . context ( "should always return the first compile result" )
136+ . flatten ( ) ?;
137+ anyhow:: bail!( "unexpected end of watch" )
144138 }
145139
146140 /// Parses compilation result from [`SpirvBuilder`] and writes it out to a file
@@ -206,8 +200,6 @@ impl Build {
206200
207201#[ cfg( test) ]
208202mod test {
209- #![ cfg( feature = "clap" ) ]
210-
211203 use cargo_gpu_test_utils:: { shader_crate_template_path, tests_teardown} ;
212204 use clap:: Parser as _;
213205
0 commit comments