22#![ allow( clippy:: unwrap_used, reason = "this is basically a test" ) ]
33//! `cargo gpu build`, analogous to `cargo build`
44
5- use crate :: args :: BuildArgs ;
5+ use crate :: install :: Install ;
66use crate :: linkage:: Linkage ;
77use crate :: lockfile:: LockfileMismatchHandler ;
8- use crate :: { install:: Install , target_spec_dir} ;
98use anyhow:: Context as _;
10- use spirv_builder:: { CompileResult , ModuleResult } ;
9+ use spirv_builder:: { CompileResult , ModuleResult , SpirvBuilder } ;
1110use 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+ }
1245
1346/// `cargo build` subcommands
1447#[ derive( Clone , clap:: Parser , Debug , serde:: Deserialize , serde:: Serialize ) ]
@@ -19,54 +52,46 @@ pub struct Build {
1952
2053 /// CLI args for configuring the build of the shader
2154 #[ clap( flatten) ]
22- pub build_args : BuildArgs ,
55+ pub build : BuildArgs ,
2356}
2457
2558impl Build {
2659 /// Entrypoint
2760 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 ( ) ?;
2962
3063 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 ,
3667 ) ?;
3768
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) ?;
4672
4773 // Ensure the shader output dir exists
4874 log:: debug!(
4975 "ensuring output-dir '{}' exists" ,
50- self . build_args . output_dir. display( )
76+ self . build . output_dir. display( )
5177 ) ;
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;
5682
5783 // 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 ( ) ?;
6085 anyhow:: ensure!(
61- self . install. spirv_install . shader_crate. exists( ) ,
86+ self . install. shader_crate. exists( ) ,
6287 "shader crate '{}' does not exist. (Current dir is '{}')" ,
63- self . install. spirv_install . shader_crate. display( ) ,
88+ self . install. shader_crate. display( ) ,
6489 std:: env:: current_dir( ) ?. display( )
6590 ) ;
6691
67- if self . build_args . watch {
92+ if self . build . watch {
6893 let this = self . clone ( ) ;
69- self . build_args
94+ self . build
7095 . spirv_builder
7196 . watch ( move |result, accept| {
7297 let result1 = this. parse_compilation_result ( & result) ;
@@ -79,9 +104,9 @@ impl Build {
79104 } else {
80105 crate :: user_output!(
81106 "Compiling shaders at {}...\n " ,
82- self . install. spirv_install . shader_crate. display( )
107+ self . install. shader_crate. display( )
83108 ) ;
84- let result = self . build_args . spirv_builder . build ( ) ?;
109+ let result = self . build . spirv_builder . build ( ) ?;
85110 self . parse_compilation_result ( & result) ?;
86111 }
87112 Ok ( ( ) )
@@ -104,7 +129,7 @@ impl Build {
104129 . into_iter ( )
105130 . map ( |( entry, filepath) | -> anyhow:: Result < Linkage > {
106131 use relative_path:: PathExt as _;
107- let path = self . build_args . output_dir . join (
132+ let path = self . build . output_dir . join (
108133 filepath
109134 . file_name ( )
110135 . context ( "Couldn't parse file name from shader module path" ) ?,
@@ -114,10 +139,10 @@ impl Build {
114139 log:: debug!(
115140 "linkage of {} relative to {}" ,
116141 path. display( ) ,
117- self . install. spirv_install . shader_crate. display( )
142+ self . install. shader_crate. display( )
118143 ) ;
119144 let spv_path = path
120- . relative_to ( & self . install . spirv_install . shader_crate )
145+ . relative_to ( & self . install . shader_crate )
121146 . map_or ( path, |path_relative_to_shader_crate| {
122147 path_relative_to_shader_crate. to_path ( "" )
123148 } ) ;
@@ -128,10 +153,7 @@ impl Build {
128153 linkage. sort ( ) ;
129154
130155 // 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 ) ;
135157 let json = serde_json:: to_string_pretty ( & linkage) ?;
136158 let mut file = std:: fs:: File :: create ( & manifest_path) . with_context ( || {
137159 format ! (
@@ -176,8 +198,8 @@ mod test {
176198 command : Command :: Build ( build) ,
177199 } = Cli :: parse_from ( args)
178200 {
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) ;
181203
182204 // TODO:
183205 // For some reason running a full build (`build.run()`) inside tests fails on Windows.
0 commit comments