1- use std:: io:: Write ;
1+ //! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
2+ use std:: io:: Write as _;
23
34use crate :: { cache_dir, spirv:: Spirv , target_spec_dir} ;
45
5- const SPIRV_BUILDER_CLI_CARGO_TOML : & str = include_str ! ( "../../spirv-builder-cli/Cargo.toml" ) ;
6- const SPIRV_BUILDER_CLI_MAIN : & str = include_str ! ( "../../spirv-builder-cli/src/main.rs" ) ;
7- const SPIRV_BUILDER_CLI_LIB : & str = include_str ! ( "../../spirv-builder-cli/src/lib.rs" ) ;
6+ /// These are the files needed to create the dedicated, per-shader `rust-gpu` builder create.
87const SPIRV_BUILDER_FILES : & [ ( & str , & str ) ] = & [
9- ( "Cargo.toml" , SPIRV_BUILDER_CLI_CARGO_TOML ) ,
10- ( "src/main.rs" , SPIRV_BUILDER_CLI_MAIN ) ,
11- ( "src/lib.rs" , SPIRV_BUILDER_CLI_LIB ) ,
8+ (
9+ "Cargo.toml" ,
10+ include_str ! ( "../../spirv-builder-cli/Cargo.toml" ) ,
11+ ) ,
12+ (
13+ "src/main.rs" ,
14+ include_str ! ( "../../spirv-builder-cli/src/main.rs" ) ,
15+ ) ,
16+ (
17+ "src/lib.rs" ,
18+ include_str ! ( "../../spirv-builder-cli/src/lib.rs" ) ,
19+ ) ,
1220] ;
1321
22+ /// Metadata for the compile targets supported by `rust-gpu`
1423const TARGET_SPECS : & [ ( & str , & str ) ] = & [
1524 (
1625 "spirv-unknown-opengl4.0.json" ,
@@ -74,15 +83,16 @@ const TARGET_SPECS: &[(&str, &str)] = &[
7483 ) ,
7584] ;
7685
86+ /// `cargo gpu install`
7787#[ derive( clap:: Parser , Debug ) ]
78- pub ( crate ) struct Install {
88+ pub struct Install {
7989 /// spirv-builder dependency, written just like in a Cargo.toml file.
8090 #[ clap( long, default_value = Spirv :: DEFAULT_DEP ) ]
8191 spirv_builder : String ,
8292
8393 /// Rust toolchain channel to use to build `spirv-builder`.
8494 ///
85- /// This must match the `spirv_builder` argument.
95+ /// This must be compatible with the `spirv_builder` argument as defined in the `rust-gpu` repo .
8696 #[ clap( long, default_value = Spirv :: DEFAULT_CHANNEL ) ]
8797 rust_toolchain : String ,
8898
@@ -92,18 +102,20 @@ pub(crate) struct Install {
92102}
93103
94104impl Install {
105+ /// Returns a [`Spirv`] instance, responsible for ensuring the right version of the `spirv-builder-cli` crate.
95106 fn spirv_cli ( & self ) -> Spirv {
96107 Spirv {
97108 dep : self . spirv_builder . clone ( ) ,
98109 channel : self . rust_toolchain . clone ( ) ,
99110 }
100111 }
101112
113+ /// Create the `spirv-builder-cli` crate.
102114 fn write_source_files ( & self ) {
103115 let cli = self . spirv_cli ( ) ;
104116 let checkout = cli. cached_checkout_path ( ) ;
105117 std:: fs:: create_dir_all ( checkout. join ( "src" ) ) . unwrap ( ) ;
106- for ( filename, contents) in SPIRV_BUILDER_FILES . iter ( ) {
118+ for ( filename, contents) in SPIRV_BUILDER_FILES {
107119 log:: debug!( "writing {filename}" ) ;
108120 let path = checkout. join ( filename) ;
109121 let mut file = std:: fs:: File :: create ( & path) . unwrap ( ) ;
@@ -114,8 +126,9 @@ impl Install {
114126 }
115127 }
116128
129+ /// Add the target spec files to the crate.
117130 fn write_target_spec_files ( & self ) {
118- for ( filename, contents) in TARGET_SPECS . iter ( ) {
131+ for ( filename, contents) in TARGET_SPECS {
119132 let path = target_spec_dir ( ) . join ( filename) ;
120133 if !path. is_file ( ) || self . force_spirv_cli_rebuild {
121134 let mut file = std:: fs:: File :: create ( & path) . unwrap ( ) ;
@@ -124,14 +137,14 @@ impl Install {
124137 }
125138 }
126139
127- // Install the binary pair and return the paths, (dylib, cli).
140+ /// Install the binary pair and return the paths, (dylib, cli).
128141 pub fn run ( & self ) -> ( std:: path:: PathBuf , std:: path:: PathBuf ) {
129142 // Ensure the cache dir exists
130143 let cache_dir = cache_dir ( ) ;
131144 log:: info!( "cache directory is '{}'" , cache_dir. display( ) ) ;
132- std:: fs:: create_dir_all ( & cache_dir) . unwrap_or_else ( |e | {
145+ std:: fs:: create_dir_all ( & cache_dir) . unwrap_or_else ( |error | {
133146 log:: error!(
134- "could not create cache directory '{}': {e }" ,
147+ "could not create cache directory '{}': {error }" ,
135148 cache_dir. display( )
136149 ) ;
137150 panic ! ( "could not create cache dir" ) ;
@@ -178,7 +191,7 @@ impl Install {
178191
179192 command. args ( [
180193 "--features" ,
181- & Self :: get_required_spirv_builder_version ( spirv_version. channel ) ,
194+ & Self :: get_required_spirv_builder_version ( & spirv_version. channel ) ,
182195 ] ) ;
183196
184197 log:: debug!( "building artifacts with `{:?}`" , command) ;
@@ -209,8 +222,8 @@ impl Install {
209222 } else {
210223 log:: error!( "could not find {}" , cli_path. display( ) ) ;
211224 log:: debug!( "contents of '{}':" , release. display( ) ) ;
212- for entry in std:: fs:: read_dir ( & release) . unwrap ( ) {
213- let entry = entry . unwrap ( ) ;
225+ for maybe_entry in std:: fs:: read_dir ( & release) . unwrap ( ) {
226+ let entry = maybe_entry . unwrap ( ) ;
214227 log:: debug!( "{}" , entry. file_name( ) . to_string_lossy( ) ) ;
215228 }
216229 panic ! ( "spirv-builder-cli build failed" ) ;
@@ -228,9 +241,9 @@ impl Install {
228241 /// `spirv-builder` version from there.
229242 /// * Warn the user that certain `cargo-gpu` features aren't available when building with
230243 /// older versions of `spirv-builder`, eg setting the target spec.
231- fn get_required_spirv_builder_version ( toolchain_channel : String ) -> String {
244+ fn get_required_spirv_builder_version ( toolchain_channel : & str ) -> String {
232245 let parse_date = chrono:: NaiveDate :: parse_from_str;
233- let datetime = parse_date ( & toolchain_channel, "nightly-%Y-%m-%d" ) . unwrap ( ) ;
246+ let datetime = parse_date ( toolchain_channel, "nightly-%Y-%m-%d" ) . unwrap ( ) ;
234247 let pre_cli_date = parse_date ( "2024-04-24" , "%Y-%m-%d" ) . unwrap ( ) ;
235248
236249 if datetime < pre_cli_date {
0 commit comments