4
4
//! version. Then with that we `git checkout` the `rust-gpu` repo that corresponds to that version.
5
5
//! From there we can look at the source code to get the required Rust toolchain.
6
6
7
- use anyhow:: { anyhow , Context as _} ;
7
+ use anyhow:: Context as _;
8
8
use cargo_metadata:: camino:: { Utf8Path , Utf8PathBuf } ;
9
9
use cargo_metadata:: semver:: Version ;
10
10
use cargo_metadata:: { MetadataCommand , Package } ;
@@ -65,30 +65,31 @@ impl core::fmt::Display for SpirvSource {
65
65
}
66
66
67
67
impl SpirvSource {
68
+ /// Figures out which source of `rust-gpu` to use
68
69
pub fn new (
69
70
shader_crate_path : & Path ,
70
71
maybe_rust_gpu_source : Option < & str > ,
71
72
maybe_rust_gpu_version : Option < & str > ,
72
73
) -> anyhow:: Result < Self > {
73
74
let source = if let Some ( rust_gpu_version) = maybe_rust_gpu_version {
74
75
if let Some ( rust_gpu_source) = maybe_rust_gpu_source {
75
- SpirvSource :: Git {
76
+ Self :: Git {
76
77
url : rust_gpu_source. to_owned ( ) ,
77
78
rev : rust_gpu_version. to_owned ( ) ,
78
79
}
79
80
} else {
80
- SpirvSource :: CratesIO ( Version :: parse ( & rust_gpu_version) ?)
81
+ Self :: CratesIO ( Version :: parse ( rust_gpu_version) ?)
81
82
}
82
83
} else {
83
- SpirvSource :: get_rust_gpu_deps_from_shader ( shader_crate_path)
84
+ Self :: get_rust_gpu_deps_from_shader ( shader_crate_path)
84
85
. context ( "get_rust_gpu_deps_from_shader" ) ?
85
86
} ;
86
87
Ok ( source)
87
88
}
88
89
89
90
/// Look into the shader crate to get the version of `rust-gpu` it's using.
90
91
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" ) ?;
92
93
let spirv_source = Self :: parse_spirv_std_source_and_version ( & spirv_std_package) ?;
93
94
log:: debug!(
94
95
"Parsed `SpirvSource` from crate `{}`: \
@@ -103,10 +104,10 @@ impl SpirvSource {
103
104
/// maybe using their own fork for example.
104
105
pub fn install_dir ( & self ) -> anyhow:: Result < PathBuf > {
105
106
match self {
106
- SpirvSource :: Path {
107
+ Self :: Path {
107
108
rust_gpu_repo_root, ..
108
109
} => Ok ( rust_gpu_repo_root. as_std_path ( ) . to_owned ( ) ) ,
109
- SpirvSource :: CratesIO { .. } | SpirvSource :: Git { .. } => {
110
+ Self :: CratesIO { .. } | Self :: Git { .. } => {
110
111
let dir = crate :: to_dirname ( self . to_string ( ) . as_ref ( ) ) ;
111
112
Ok ( crate :: cache_dir ( ) ?
112
113
. join ( "rustc_backend_spirv_install" )
@@ -120,53 +121,47 @@ impl SpirvSource {
120
121
/// Which would return:
121
122
/// `SpirvSource::Git("https://github.com/Rust-GPU/rust-gpu", "54f6978c")`
122
123
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:?}'" ) ;
127
125
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 ( ) ;
132
129
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) ) ?
151
143
}
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!" )
168
147
}
169
148
}
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
+ }
170
165
} ;
171
166
172
167
log:: debug!( "Parsed `rust-gpu` source and version: {result:?}" ) ;
@@ -234,8 +229,10 @@ pub fn get_channel_from_rustc_codegen_spirv_build_script(
234
229
. lines ( )
235
230
. find_map ( |line| line. strip_prefix ( channel_start) )
236
231
. 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 ( ) )
239
236
}
240
237
241
238
#[ cfg( test) ]
0 commit comments