2
2
#![ allow( clippy:: unwrap_used, reason = "this is basically a test" ) ]
3
3
//! `cargo gpu build`, analogous to `cargo build`
4
4
5
- use crate :: args :: BuildArgs ;
5
+ use crate :: install :: Install ;
6
6
use crate :: linkage:: Linkage ;
7
7
use crate :: lockfile:: LockfileMismatchHandler ;
8
- use crate :: { install:: Install , target_spec_dir} ;
9
8
use anyhow:: Context as _;
10
- use spirv_builder:: { CompileResult , ModuleResult } ;
9
+ use spirv_builder:: { CompileResult , ModuleResult , SpirvBuilder } ;
11
10
use std:: io:: Write as _;
11
+ use std:: path:: PathBuf ;
12
+
13
+ /// Args for just a build
14
+ #[ derive( clap:: Parser , Debug , Clone , serde:: Deserialize , serde:: Serialize ) ]
15
+ pub struct BuildArgs {
16
+ /// Path to the output directory for the compiled shaders.
17
+ #[ clap( long, short, default_value = "./" ) ]
18
+ pub output_dir : PathBuf ,
19
+
20
+ /// Watch the shader crate directory and automatically recompile on changes.
21
+ #[ clap( long, short, action) ]
22
+ pub watch : bool ,
23
+
24
+ /// the flattened [`SpirvBuilder`]
25
+ #[ clap( flatten) ]
26
+ #[ serde( flatten) ]
27
+ pub spirv_builder : SpirvBuilder ,
28
+
29
+ ///Renames the manifest.json file to the given name
30
+ #[ clap( long, short, default_value = "manifest.json" ) ]
31
+ pub manifest_file : String ,
32
+ }
33
+
34
+ impl Default for BuildArgs {
35
+ #[ inline]
36
+ fn default ( ) -> Self {
37
+ Self {
38
+ output_dir : PathBuf :: from ( "./" ) ,
39
+ watch : false ,
40
+ spirv_builder : SpirvBuilder :: default ( ) ,
41
+ manifest_file : String :: from ( "manifest.json" ) ,
42
+ }
43
+ }
44
+ }
12
45
13
46
/// `cargo build` subcommands
14
47
#[ derive( Clone , clap:: Parser , Debug , serde:: Deserialize , serde:: Serialize ) ]
@@ -19,54 +52,46 @@ pub struct Build {
19
52
20
53
/// CLI args for configuring the build of the shader
21
54
#[ clap( flatten) ]
22
- pub build_args : BuildArgs ,
55
+ pub build : BuildArgs ,
23
56
}
24
57
25
58
impl Build {
26
59
/// Entrypoint
27
60
pub fn run ( & mut self ) -> anyhow:: Result < ( ) > {
28
- let ( rustc_codegen_spirv_location , toolchain_channel ) = self . install . run ( ) ?;
61
+ let installed_backend = self . install . run ( ) ?;
29
62
30
63
let _lockfile_mismatch_handler = LockfileMismatchHandler :: new (
31
- & self . install . spirv_install . shader_crate ,
32
- & toolchain_channel,
33
- self . install
34
- . spirv_install
35
- . force_overwrite_lockfiles_v4_to_v3 ,
64
+ & self . install . shader_crate ,
65
+ & installed_backend. toolchain_channel ,
66
+ self . install . force_overwrite_lockfiles_v4_to_v3 ,
36
67
) ?;
37
68
38
- let builder = & mut self . build_args . spirv_builder ;
39
- builder. rustc_codegen_spirv_location = Some ( rustc_codegen_spirv_location) ;
40
- builder. toolchain_overwrite = Some ( toolchain_channel) ;
41
- builder. path_to_crate = Some ( self . install . spirv_install . shader_crate . clone ( ) ) ;
42
- builder. path_to_target_spec = Some ( target_spec_dir ( ) ?. join ( format ! (
43
- "{}.json" ,
44
- builder. target. as_ref( ) . context( "expect target to be set" ) ?
45
- ) ) ) ;
69
+ let builder = & mut self . build . spirv_builder ;
70
+ builder. path_to_crate = Some ( self . install . shader_crate . clone ( ) ) ;
71
+ installed_backend. configure_spirv_builder ( builder) ?;
46
72
47
73
// Ensure the shader output dir exists
48
74
log:: debug!(
49
75
"ensuring output-dir '{}' exists" ,
50
- self . build_args . output_dir. display( )
76
+ self . build . output_dir. display( )
51
77
) ;
52
- std:: fs:: create_dir_all ( & self . build_args . output_dir ) ?;
53
- let canonicalized = self . build_args . output_dir . canonicalize ( ) ?;
54
- log:: debug!( "canonicalized output dir: {canonicalized:?}" ) ;
55
- self . build_args . output_dir = canonicalized;
78
+ std:: fs:: create_dir_all ( & self . build . output_dir ) ?;
79
+ let canonicalized = self . build . output_dir . canonicalize ( ) ?;
80
+ log:: debug!( "canonicalized output dir: {}" , canonicalized . display ( ) ) ;
81
+ self . build . output_dir = canonicalized;
56
82
57
83
// Ensure the shader crate exists
58
- self . install . spirv_install . shader_crate =
59
- self . install . spirv_install . shader_crate . canonicalize ( ) ?;
84
+ self . install . shader_crate = self . install . shader_crate . canonicalize ( ) ?;
60
85
anyhow:: ensure!(
61
- self . install. spirv_install . shader_crate. exists( ) ,
86
+ self . install. shader_crate. exists( ) ,
62
87
"shader crate '{}' does not exist. (Current dir is '{}')" ,
63
- self . install. spirv_install . shader_crate. display( ) ,
88
+ self . install. shader_crate. display( ) ,
64
89
std:: env:: current_dir( ) ?. display( )
65
90
) ;
66
91
67
- if self . build_args . watch {
92
+ if self . build . watch {
68
93
let this = self . clone ( ) ;
69
- self . build_args
94
+ self . build
70
95
. spirv_builder
71
96
. watch ( move |result, accept| {
72
97
let result1 = this. parse_compilation_result ( & result) ;
@@ -79,9 +104,9 @@ impl Build {
79
104
} else {
80
105
crate :: user_output!(
81
106
"Compiling shaders at {}...\n " ,
82
- self . install. spirv_install . shader_crate. display( )
107
+ self . install. shader_crate. display( )
83
108
) ;
84
- let result = self . build_args . spirv_builder . build ( ) ?;
109
+ let result = self . build . spirv_builder . build ( ) ?;
85
110
self . parse_compilation_result ( & result) ?;
86
111
}
87
112
Ok ( ( ) )
@@ -104,7 +129,7 @@ impl Build {
104
129
. into_iter ( )
105
130
. map ( |( entry, filepath) | -> anyhow:: Result < Linkage > {
106
131
use relative_path:: PathExt as _;
107
- let path = self . build_args . output_dir . join (
132
+ let path = self . build . output_dir . join (
108
133
filepath
109
134
. file_name ( )
110
135
. context ( "Couldn't parse file name from shader module path" ) ?,
@@ -114,10 +139,10 @@ impl Build {
114
139
log:: debug!(
115
140
"linkage of {} relative to {}" ,
116
141
path. display( ) ,
117
- self . install. spirv_install . shader_crate. display( )
142
+ self . install. shader_crate. display( )
118
143
) ;
119
144
let spv_path = path
120
- . relative_to ( & self . install . spirv_install . shader_crate )
145
+ . relative_to ( & self . install . shader_crate )
121
146
. map_or ( path, |path_relative_to_shader_crate| {
122
147
path_relative_to_shader_crate. to_path ( "" )
123
148
} ) ;
@@ -128,10 +153,7 @@ impl Build {
128
153
linkage. sort ( ) ;
129
154
130
155
// Write the shader manifest json file
131
- let manifest_path = self
132
- . build_args
133
- . output_dir
134
- . join ( & self . build_args . manifest_file ) ;
156
+ let manifest_path = self . build . output_dir . join ( & self . build . manifest_file ) ;
135
157
let json = serde_json:: to_string_pretty ( & linkage) ?;
136
158
let mut file = std:: fs:: File :: create ( & manifest_path) . with_context ( || {
137
159
format ! (
@@ -176,8 +198,8 @@ mod test {
176
198
command : Command :: Build ( build) ,
177
199
} = Cli :: parse_from ( args)
178
200
{
179
- assert_eq ! ( shader_crate_path, build. install. spirv_install . shader_crate) ;
180
- assert_eq ! ( output_dir, build. build_args . output_dir) ;
201
+ assert_eq ! ( shader_crate_path, build. install. shader_crate) ;
202
+ assert_eq ! ( output_dir, build. build . output_dir) ;
181
203
182
204
// TODO:
183
205
// For some reason running a full build (`build.run()`) inside tests fails on Windows.
0 commit comments