55//! From there we can look at the source code to get the required Rust toolchain.
66
77use anyhow:: { anyhow, Context as _} ;
8+ use cargo_metadata:: camino:: Utf8PathBuf ;
89use cargo_metadata:: semver:: Version ;
910use cargo_metadata:: { MetadataCommand , Package } ;
11+ use std:: fs;
1012use std:: path:: { Path , PathBuf } ;
1113
1214/// The canonical `rust-gpu` URI
@@ -32,7 +34,12 @@ pub enum SpirvSource {
3234 /// If the shader specifies a version like:
3335 /// `spirv-std = { path = "/path/to/rust-gpu" ... }`
3436 /// then the source of `rust-gpu` is `Path`.
35- Path ( ( String , Version ) ) ,
37+ Path {
38+ /// File path of rust-gpu repository
39+ rust_gpu_path : Utf8PathBuf ,
40+ /// Version of specified rust-gpu repository
41+ version : Version ,
42+ } ,
3643}
3744
3845impl core:: fmt:: Display for SpirvSource {
@@ -44,7 +51,10 @@ impl core::fmt::Display for SpirvSource {
4451 match self {
4552 Self :: CratesIO ( version) => version. fmt ( f) ,
4653 Self :: Git { url, rev } => f. write_str ( & format ! ( "{url}+{rev}" ) ) ,
47- Self :: Path ( ( a, b) ) => f. write_str ( & format ! ( "{a}+{b}" ) ) ,
54+ Self :: Path {
55+ rust_gpu_path,
56+ version,
57+ } => f. write_str ( & format ! ( "{rust_gpu_path}+{version}" ) ) ,
4858 }
4959 }
5060}
@@ -72,17 +82,18 @@ impl SpirvSource {
7282 /// Convert the source to just its version.
7383 pub fn to_version ( & self ) -> String {
7484 match self {
75- Self :: CratesIO ( version) | Self :: Path ( ( _ , version) ) => version. to_string ( ) ,
85+ Self :: CratesIO ( version) | Self :: Path { version, .. } => version. to_string ( ) ,
7686 Self :: Git { rev, .. } => rev. to_string ( ) ,
7787 }
7888 }
7989
8090 /// Convert the source to just its repo or path.
91+ /// Must be root of git repository.
8192 fn to_repo ( & self ) -> String {
8293 match self {
8394 Self :: CratesIO ( _) => RUST_GPU_REPO . to_owned ( ) ,
8495 Self :: Git { url, .. } => url. to_owned ( ) ,
85- Self :: Path ( ( path , _ ) ) => path . to_owned ( ) ,
96+ Self :: Path { rust_gpu_path , .. } => rust_gpu_path . to_string ( ) ,
8697 }
8798 }
8899
@@ -115,20 +126,28 @@ impl SpirvSource {
115126
116127 /// Checkout the `rust-gpu` repo to the requested version.
117128 fn checkout ( & self ) -> anyhow:: Result < ( ) > {
129+ let Self :: Git { rev, .. } = self else {
130+ log:: trace!( "Skipping checking out rust-gpu" , ) ;
131+ return Ok ( ( ) ) ;
132+ } ;
133+
118134 log:: debug!(
119135 "Checking out `rust-gpu` repo at {} to {}" ,
120136 self . to_dirname( ) ?. display( ) ,
121137 self . to_version( )
122138 ) ;
123- let output_checkout = std:: process:: Command :: new ( "git" )
139+ let mut command_checkout = std:: process:: Command :: new ( "git" ) ;
140+ command_checkout
124141 . current_dir ( self . to_dirname ( ) ?)
125- . args ( [ "checkout" , self . to_version ( ) . as_ref ( ) ] )
126- . output ( ) ?;
142+ . args ( [ "checkout" , rev] ) ;
143+ log:: debug!( "Running command {:?}" , command_checkout) ;
144+ let output_checkout = command_checkout. output ( ) ?;
127145 anyhow:: ensure!(
128146 output_checkout. status. success( ) ,
129- "couldn't checkout revision '{}' of `rust-gpu` at {}" ,
147+ "couldn't checkout revision '{}' of `rust-gpu` at {}. \n Error Output: {} " ,
130148 self . to_version( ) ,
131- self . to_dirname( ) ?. to_string_lossy( )
149+ self . to_dirname( ) ?. to_string_lossy( ) ,
150+ String :: from_utf8( output_checkout. stderr) . unwrap( )
132151 ) ;
133152
134153 Ok ( ( ) )
@@ -150,7 +169,7 @@ impl SpirvSource {
150169 "--no-patch" ,
151170 "--format=%cd" ,
152171 format ! ( "--date=format:'{date_format}'" ) . as_ref ( ) ,
153- self . to_version ( ) . as_ref ( ) ,
172+ "HEAD" ,
154173 ] )
155174 . output ( ) ?;
156175 anyhow:: ensure!(
@@ -179,7 +198,7 @@ impl SpirvSource {
179198 fn get_channel_from_toolchain_toml ( path : & PathBuf ) -> anyhow:: Result < String > {
180199 log:: debug!( "Parsing `rust-toolchain.toml` at {path:?} for the used toolchain" ) ;
181200
182- let contents = std :: fs:: read_to_string ( path. join ( "rust-toolchain.toml" ) ) ?;
201+ let contents = fs:: read_to_string ( path. join ( "rust-toolchain.toml" ) ) ?;
183202 let toml: toml:: Table = toml:: from_str ( & contents) ?;
184203 let Some ( toolchain) = toml. get ( "toolchain" ) else {
185204 anyhow:: bail!(
@@ -255,9 +274,20 @@ impl SpirvSource {
255274 }
256275 }
257276 None => {
258- let path = & spirv_std_package. manifest_path ;
259- let version = & spirv_std_package. version ;
260- Self :: Path ( ( path. to_string ( ) , version. clone ( ) ) )
277+ let rust_gpu_path = spirv_std_package
278+ . manifest_path // rust-gpu/crates/spirv-std/Cargo.toml
279+ . parent ( )
280+ . unwrap ( ) // rust-gpu/crates/spirv-std
281+ . parent ( )
282+ . unwrap ( ) // rust-gpu/crates
283+ . parent ( )
284+ . unwrap ( ) // rust-gpu
285+ . to_owned ( ) ;
286+ let version = spirv_std_package. version . clone ( ) ;
287+ Self :: Path {
288+ rust_gpu_path,
289+ version,
290+ }
261291 }
262292 } ;
263293
0 commit comments