44//! version. Then with that we `git checkout` the `rust-gpu` repo that corresponds to that version.
55//! From there we can look at the source code to get the required Rust toolchain.
66
7- use anyhow:: { anyhow , Context as _} ;
7+ use anyhow:: Context as _;
88use cargo_metadata:: camino:: { Utf8Path , Utf8PathBuf } ;
99use cargo_metadata:: semver:: Version ;
1010use cargo_metadata:: { MetadataCommand , Package } ;
@@ -65,30 +65,31 @@ impl core::fmt::Display for SpirvSource {
6565}
6666
6767impl SpirvSource {
68+ /// Figures out which source of `rust-gpu` to use
6869 pub fn new (
6970 shader_crate_path : & Path ,
7071 maybe_rust_gpu_source : Option < & str > ,
7172 maybe_rust_gpu_version : Option < & str > ,
7273 ) -> anyhow:: Result < Self > {
7374 let source = if let Some ( rust_gpu_version) = maybe_rust_gpu_version {
7475 if let Some ( rust_gpu_source) = maybe_rust_gpu_source {
75- SpirvSource :: Git {
76+ Self :: Git {
7677 url : rust_gpu_source. to_owned ( ) ,
7778 rev : rust_gpu_version. to_owned ( ) ,
7879 }
7980 } else {
80- SpirvSource :: CratesIO ( Version :: parse ( & rust_gpu_version) ?)
81+ Self :: CratesIO ( Version :: parse ( rust_gpu_version) ?)
8182 }
8283 } else {
83- SpirvSource :: get_rust_gpu_deps_from_shader ( shader_crate_path)
84+ Self :: get_rust_gpu_deps_from_shader ( shader_crate_path)
8485 . context ( "get_rust_gpu_deps_from_shader" ) ?
8586 } ;
8687 Ok ( source)
8788 }
8889
8990 /// Look into the shader crate to get the version of `rust-gpu` it's using.
9091 pub fn get_rust_gpu_deps_from_shader ( shader_crate_path : & Path ) -> anyhow:: Result < Self > {
91- let spirv_std_package = get_package_from_crate ( & shader_crate_path, "spirv-std" ) ?;
92+ let spirv_std_package = get_package_from_crate ( shader_crate_path, "spirv-std" ) ?;
9293 let spirv_source = Self :: parse_spirv_std_source_and_version ( & spirv_std_package) ?;
9394 log:: debug!(
9495 "Parsed `SpirvSource` from crate `{}`: \
@@ -103,10 +104,10 @@ impl SpirvSource {
103104 /// maybe using their own fork for example.
104105 pub fn install_dir ( & self ) -> anyhow:: Result < PathBuf > {
105106 match self {
106- SpirvSource :: Path {
107+ Self :: Path {
107108 rust_gpu_repo_root, ..
108109 } => Ok ( rust_gpu_repo_root. as_std_path ( ) . to_owned ( ) ) ,
109- SpirvSource :: CratesIO { .. } | SpirvSource :: Git { .. } => {
110+ Self :: CratesIO { .. } | Self :: Git { .. } => {
110111 let dir = crate :: to_dirname ( self . to_string ( ) . as_ref ( ) ) ;
111112 Ok ( crate :: cache_dir ( ) ?
112113 . join ( "rustc_backend_spirv_install" )
@@ -120,53 +121,47 @@ impl SpirvSource {
120121 /// Which would return:
121122 /// `SpirvSource::Git("https://github.com/Rust-GPU/rust-gpu", "54f6978c")`
122123 fn parse_spirv_std_source_and_version ( spirv_std_package : & Package ) -> anyhow:: Result < Self > {
123- log:: trace!(
124- "parsing spirv-std source and version from package: '{:?}'" ,
125- spirv_std_package
126- ) ;
124+ log:: trace!( "parsing spirv-std source and version from package: '{spirv_std_package:?}'" ) ;
127125
128- let result = match & spirv_std_package. source {
129- Some ( source) => {
130- let is_git = source. repr . starts_with ( "git+" ) ;
131- let is_crates_io = source. is_crates_io ( ) ;
126+ let result = if let Some ( source) = & spirv_std_package. source {
127+ let is_git = source. repr . starts_with ( "git+" ) ;
128+ let is_crates_io = source. is_crates_io ( ) ;
132129
133- match ( is_git, is_crates_io) {
134- ( true , true ) => unreachable ! ( ) ,
135- ( true , false ) => {
136- let link = & source. repr [ 4 ..] ;
137- let sharp_index = link. find ( '#' ) . ok_or ( anyhow ! (
138- "Git url of spirv-std package does not contain revision!"
139- ) ) ?;
140- let question_mark_index = link. find ( '?' ) . ok_or ( anyhow ! (
141- "Git url of spirv-std package does not contain revision!"
142- ) ) ?;
143- let url = link[ ..question_mark_index] . to_string ( ) ;
144- let rev = link[ sharp_index + 1 ..] . to_string ( ) ;
145- Self :: Git { url, rev }
146- }
147- ( false , true ) => Self :: CratesIO ( spirv_std_package. version . clone ( ) ) ,
148- ( false , false ) => {
149- anyhow:: bail!( "Metadata of spirv-std package uses unknown url format!" )
150- }
130+ match ( is_git, is_crates_io) {
131+ ( true , true ) => anyhow:: bail!( "parsed both git and crates.io?" ) ,
132+ ( true , false ) => {
133+ let parse_git = || {
134+ let link = & source. repr . get ( 4 ..) ?;
135+ let sharp_index = link. find ( '#' ) ?;
136+ let question_mark_index = link. find ( '?' ) ?;
137+ let url = link. get ( ..question_mark_index) ?. to_owned ( ) ;
138+ let rev = link. get ( sharp_index + 1 ..) ?. to_owned ( ) ;
139+ Some ( Self :: Git { url, rev } )
140+ } ;
141+ parse_git ( )
142+ . with_context ( || format ! ( "Failed to parse git url {}" , & source. repr) ) ?
151143 }
152- }
153- None => {
154- let rust_gpu_repo_root = spirv_std_package
155- . manifest_path // rust-gpu/crates/spirv-std/Cargo.toml
156- . parent ( ) // rust-gpu/crates/spirv-std
157- . and_then ( Utf8Path :: parent) // rust-gpu/crates
158- . and_then ( Utf8Path :: parent) // rust-gpu
159- . context ( "selecting rust-gpu workspace root dir in local path" ) ?
160- . to_owned ( ) ;
161- if !rust_gpu_repo_root. is_dir ( ) {
162- anyhow:: bail!( "path {rust_gpu_repo_root} is not a directory" ) ;
163- }
164- let version = spirv_std_package. version . clone ( ) ;
165- Self :: Path {
166- rust_gpu_repo_root,
167- version,
144+ ( false , true ) => Self :: CratesIO ( spirv_std_package. version . clone ( ) ) ,
145+ ( false , false ) => {
146+ anyhow:: bail!( "Metadata of spirv-std package uses unknown url format!" )
168147 }
169148 }
149+ } else {
150+ let rust_gpu_repo_root = spirv_std_package
151+ . manifest_path // rust-gpu/crates/spirv-std/Cargo.toml
152+ . parent ( ) // rust-gpu/crates/spirv-std
153+ . and_then ( Utf8Path :: parent) // rust-gpu/crates
154+ . and_then ( Utf8Path :: parent) // rust-gpu
155+ . context ( "selecting rust-gpu workspace root dir in local path" ) ?
156+ . to_owned ( ) ;
157+ if !rust_gpu_repo_root. is_dir ( ) {
158+ anyhow:: bail!( "path {rust_gpu_repo_root} is not a directory" ) ;
159+ }
160+ let version = spirv_std_package. version . clone ( ) ;
161+ Self :: Path {
162+ rust_gpu_repo_root,
163+ version,
164+ }
170165 } ;
171166
172167 log:: debug!( "Parsed `rust-gpu` source and version: {result:?}" ) ;
@@ -234,8 +229,10 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
234229 . lines ( )
235230 . find_map ( |line| line. strip_prefix ( channel_start) )
236231 . context ( format ! ( "Can't find `{channel_start}` line in {build_rs:?}" ) ) ?;
237- let channel = & channel_line[ ..channel_line. find ( "\" " ) . context ( "ending \" missing" ) ?] ;
238- Ok ( channel. to_string ( ) )
232+ let channel = channel_line
233+ . get ( ..channel_line. find ( '"' ) . context ( "ending \" missing" ) ?)
234+ . context ( "can't slice version" ) ?;
235+ Ok ( channel. to_owned ( ) )
239236}
240237
241238#[ cfg( test) ]
0 commit comments