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 _;
2
3
3
4
use crate :: { cache_dir, spirv:: Spirv , target_spec_dir} ;
4
5
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.
8
7
const 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
+ ) ,
12
20
] ;
13
21
22
+ /// Metadata for the compile targets supported by `rust-gpu`
14
23
const TARGET_SPECS : & [ ( & str , & str ) ] = & [
15
24
(
16
25
"spirv-unknown-opengl4.0.json" ,
@@ -74,15 +83,16 @@ const TARGET_SPECS: &[(&str, &str)] = &[
74
83
) ,
75
84
] ;
76
85
86
+ /// `cargo gpu install`
77
87
#[ derive( clap:: Parser , Debug ) ]
78
- pub ( crate ) struct Install {
88
+ pub struct Install {
79
89
/// spirv-builder dependency, written just like in a Cargo.toml file.
80
90
#[ clap( long, default_value = Spirv :: DEFAULT_DEP ) ]
81
91
spirv_builder : String ,
82
92
83
93
/// Rust toolchain channel to use to build `spirv-builder`.
84
94
///
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 .
86
96
#[ clap( long, default_value = Spirv :: DEFAULT_CHANNEL ) ]
87
97
rust_toolchain : String ,
88
98
@@ -92,18 +102,20 @@ pub(crate) struct Install {
92
102
}
93
103
94
104
impl Install {
105
+ /// Returns a [`Spirv`] instance, responsible for ensuring the right version of the `spirv-builder-cli` crate.
95
106
fn spirv_cli ( & self ) -> Spirv {
96
107
Spirv {
97
108
dep : self . spirv_builder . clone ( ) ,
98
109
channel : self . rust_toolchain . clone ( ) ,
99
110
}
100
111
}
101
112
113
+ /// Create the `spirv-builder-cli` crate.
102
114
fn write_source_files ( & self ) {
103
115
let cli = self . spirv_cli ( ) ;
104
116
let checkout = cli. cached_checkout_path ( ) ;
105
117
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 {
107
119
log:: debug!( "writing {filename}" ) ;
108
120
let path = checkout. join ( filename) ;
109
121
let mut file = std:: fs:: File :: create ( & path) . unwrap ( ) ;
@@ -114,8 +126,9 @@ impl Install {
114
126
}
115
127
}
116
128
129
+ /// Add the target spec files to the crate.
117
130
fn write_target_spec_files ( & self ) {
118
- for ( filename, contents) in TARGET_SPECS . iter ( ) {
131
+ for ( filename, contents) in TARGET_SPECS {
119
132
let path = target_spec_dir ( ) . join ( filename) ;
120
133
if !path. is_file ( ) || self . force_spirv_cli_rebuild {
121
134
let mut file = std:: fs:: File :: create ( & path) . unwrap ( ) ;
@@ -124,14 +137,14 @@ impl Install {
124
137
}
125
138
}
126
139
127
- // Install the binary pair and return the paths, (dylib, cli).
140
+ /// Install the binary pair and return the paths, (dylib, cli).
128
141
pub fn run ( & self ) -> ( std:: path:: PathBuf , std:: path:: PathBuf ) {
129
142
// Ensure the cache dir exists
130
143
let cache_dir = cache_dir ( ) ;
131
144
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 | {
133
146
log:: error!(
134
- "could not create cache directory '{}': {e }" ,
147
+ "could not create cache directory '{}': {error }" ,
135
148
cache_dir. display( )
136
149
) ;
137
150
panic ! ( "could not create cache dir" ) ;
@@ -178,7 +191,7 @@ impl Install {
178
191
179
192
command. args ( [
180
193
"--features" ,
181
- & Self :: get_required_spirv_builder_version ( spirv_version. channel ) ,
194
+ & Self :: get_required_spirv_builder_version ( & spirv_version. channel ) ,
182
195
] ) ;
183
196
184
197
log:: debug!( "building artifacts with `{:?}`" , command) ;
@@ -209,8 +222,8 @@ impl Install {
209
222
} else {
210
223
log:: error!( "could not find {}" , cli_path. display( ) ) ;
211
224
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 ( ) ;
214
227
log:: debug!( "{}" , entry. file_name( ) . to_string_lossy( ) ) ;
215
228
}
216
229
panic ! ( "spirv-builder-cli build failed" ) ;
@@ -228,9 +241,9 @@ impl Install {
228
241
/// `spirv-builder` version from there.
229
242
/// * Warn the user that certain `cargo-gpu` features aren't available when building with
230
243
/// 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 {
232
245
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 ( ) ;
234
247
let pre_cli_date = parse_date ( "2024-04-24" , "%Y-%m-%d" ) . unwrap ( ) ;
235
248
236
249
if datetime < pre_cli_date {
0 commit comments