1
+ //! `cargo gpu build`, analogous to `cargo build`
2
+
1
3
#![ allow( clippy:: shadow_reuse, reason = "let's not be silly" ) ]
2
4
#![ allow( clippy:: unwrap_used, reason = "this is basically a test" ) ]
3
- //! `cargo gpu build`, analogous to `cargo build`
4
5
5
- use anyhow:: Context as _;
6
- use spirv_builder:: { CompileResult , ModuleResult , SpirvBuilder } ;
7
6
use std:: io:: { self , Write as _} ;
8
7
use std:: path:: PathBuf ;
9
8
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 } ;
12
12
13
13
use crate :: linkage:: Linkage ;
14
14
use crate :: lockfile:: LockfileMismatchHandler ;
15
+ use crate :: user_consent:: ask_for_user_consent;
15
16
16
17
/// 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" ) ]
19
21
pub struct BuildArgs {
20
22
/// 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 = "./" ) ]
22
24
pub output_dir : PathBuf ,
23
25
24
26
/// 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) ]
27
28
pub watch : bool ,
28
29
29
- /// the flattened [`SpirvBuilder`]
30
- #[ cfg_attr ( feature = " clap" , clap ( flatten) ) ]
30
+ /// The flattened [`SpirvBuilder`]
31
+ #[ clap( flatten) ]
31
32
#[ serde( flatten) ]
32
33
pub spirv_builder : SpirvBuilder ,
33
34
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" ) ]
36
37
pub manifest_file : String ,
37
38
}
38
39
@@ -41,7 +42,6 @@ impl Default for BuildArgs {
41
42
fn default ( ) -> Self {
42
43
Self {
43
44
output_dir : PathBuf :: from ( "./" ) ,
44
- #[ cfg( feature = "watch" ) ]
45
45
watch : false ,
46
46
spirv_builder : SpirvBuilder :: default ( ) ,
47
47
manifest_file : String :: from ( "manifest.json" ) ,
@@ -50,20 +50,25 @@ impl Default for BuildArgs {
50
50
}
51
51
52
52
/// `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 ]
55
55
pub struct Build {
56
56
/// CLI args for install the `rust-gpu` compiler and components
57
- #[ cfg_attr ( feature = " clap" , clap ( flatten) ) ]
57
+ #[ clap( flatten) ]
58
58
pub install : Install ,
59
59
60
60
/// CLI args for configuring the build of the shader
61
- #[ cfg_attr ( feature = " clap" , clap ( flatten) ) ]
61
+ #[ clap( flatten) ]
62
62
pub build : BuildArgs ,
63
63
}
64
64
65
65
impl Build {
66
- /// Entrypoint
66
+ /// Builds the shader crate.
67
+ ///
68
+ /// # Errors
69
+ ///
70
+ /// Returns an error if the build process fails somehow.
71
+ #[ inline]
67
72
pub fn run ( & mut self ) -> anyhow:: Result < ( ) > {
68
73
let skip_consent = self . install . auto_install_rust_toolchain ;
69
74
let halt_installation = ask_for_user_consent ( skip_consent) ;
@@ -98,14 +103,9 @@ impl Build {
98
103
std:: env:: current_dir( ) ?. display( )
99
104
) ;
100
105
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 {
106
107
return self . watch ( ) ;
107
108
}
108
-
109
109
self . build ( )
110
110
}
111
111
@@ -123,24 +123,18 @@ impl Build {
123
123
/// Watches shader crate for changes using [`SpirvBuilder`]
124
124
/// or returns an error depending on presence of `watch` feature.
125
125
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" )
144
138
}
145
139
146
140
/// Parses compilation result from [`SpirvBuilder`] and writes it out to a file
@@ -206,8 +200,6 @@ impl Build {
206
200
207
201
#[ cfg( test) ]
208
202
mod test {
209
- #![ cfg( feature = "clap" ) ]
210
-
211
203
use cargo_gpu_test_utils:: { shader_crate_template_path, tests_teardown} ;
212
204
use clap:: Parser as _;
213
205
0 commit comments