11//! Install a dedicated per-shader crate that has the `rust-gpu` compiler in it.
22
3- use anyhow:: Context as _;
4- use std:: io:: Write as _;
5- use std:: path:: Path ;
6-
73use crate :: args:: InstallArgs ;
84use crate :: spirv_source:: {
95 get_channel_from_rustc_codegen_spirv_build_script, get_package_from_crate,
106} ;
117use crate :: { cache_dir, spirv_source:: SpirvSource , target_spec_dir} ;
8+ use anyhow:: Context as _;
9+ use log:: trace;
10+ use std:: io:: Write as _;
11+ use std:: path:: Path ;
1212
1313/// Metadata for the compile targets supported by `rust-gpu`
1414const TARGET_SPECS : & [ ( & str , & str ) ] = & [
@@ -85,21 +85,32 @@ pub struct Install {
8585impl Install {
8686 /// Create the `rustc_codegen_spirv_dummy` crate that depends on `rustc_codegen_spirv`
8787 fn write_source_files ( source : & SpirvSource , checkout : & Path ) -> anyhow:: Result < ( ) > {
88+ // skip writing a dummy project if we use a local rust-gpu checkout
89+ if matches ! ( source, SpirvSource :: Path { .. } ) {
90+ return Ok ( ( ) ) ;
91+ }
92+ log:: debug!(
93+ "writing `rustc_codegen_spirv_dummy` source files into '{}'" ,
94+ checkout. display( )
95+ ) ;
96+
8897 {
98+ trace ! ( "writing dummy main.rs" ) ;
8999 let main = "fn main() {}" ;
90100 let src = checkout. join ( "src" ) ;
91101 std:: fs:: create_dir_all ( & src) . context ( "creating directory for 'src'" ) ?;
92102 std:: fs:: write ( src. join ( "main.rs" ) , main) . context ( "writing 'main.rs'" ) ?;
93103 } ;
94104
95105 {
106+ trace ! ( "writing dummy Cargo.toml" ) ;
96107 let version_spec = match & source {
97108 SpirvSource :: CratesIO ( version) => {
98109 format ! ( "version = \" {}\" " , version)
99110 }
100111 SpirvSource :: Git { url, rev } => format ! ( "git = \" {url}\" \n rev = \" {rev}\" " ) ,
101112 SpirvSource :: Path {
102- rust_gpu_path,
113+ rust_gpu_repo_root : rust_gpu_path,
103114 version,
104115 } => {
105116 let mut new_path = rust_gpu_path. to_owned ( ) ;
@@ -157,28 +168,37 @@ package = "rustc_codegen_spirv"
157168 self . spirv_install . spirv_builder_source . as_deref ( ) ,
158169 self . spirv_install . spirv_builder_version . as_deref ( ) ,
159170 ) ?;
171+ let source_is_path = matches ! ( source, SpirvSource :: Path { .. } ) ;
160172 let checkout = source. install_dir ( ) ?;
161173
162174 let dylib_filename = format ! (
163175 "{}rustc_codegen_spirv{}" ,
164176 std:: env:: consts:: DLL_PREFIX ,
165177 std:: env:: consts:: DLL_SUFFIX
166178 ) ;
167- let dest_dylib_path = checkout. join ( & dylib_filename) ;
168- if dest_dylib_path. is_file ( ) {
169- log:: info!(
170- "cargo-gpu artifacts are already installed in '{}'" ,
171- checkout. display( )
172- ) ;
179+
180+ let dest_dylib_path;
181+ if source_is_path {
182+ dest_dylib_path = checkout
183+ . join ( "target" )
184+ . join ( "release" )
185+ . join ( & dylib_filename) ;
186+ } else {
187+ dest_dylib_path = checkout. join ( & dylib_filename) ;
188+ if dest_dylib_path. is_file ( ) {
189+ log:: info!(
190+ "cargo-gpu artifacts are already installed in '{}'" ,
191+ checkout. display( )
192+ ) ;
193+ }
173194 }
174195
175- if dest_dylib_path. is_file ( ) && !self . spirv_install . force_spirv_cli_rebuild {
196+ if !source_is_path
197+ && dest_dylib_path. is_file ( )
198+ && !self . spirv_install . force_spirv_cli_rebuild
199+ {
176200 log:: info!( "...and so we are aborting the install step." ) ;
177201 } else {
178- log:: debug!(
179- "writing `rustc_codegen_spirv_dummy` source files into '{}'" ,
180- checkout. display( )
181- ) ;
182202 Self :: write_source_files ( & source, & checkout) . context ( "writing source files" ) ?;
183203
184204 log:: debug!( "resolving toolchain version to use" ) ;
@@ -196,10 +216,6 @@ package = "rustc_codegen_spirv"
196216 )
197217 . context ( "ensuring toolchain and components exist" ) ?;
198218
199- log:: debug!( "write_target_spec_files" ) ;
200- self . write_target_spec_files ( )
201- . context ( "writing target spec files" ) ?;
202-
203219 crate :: user_output!( "Compiling `rustc_codegen_spirv` from source {}\n " , source, ) ;
204220
205221 let mut build_command = std:: process:: Command :: new ( "cargo" ) ;
@@ -208,6 +224,9 @@ package = "rustc_codegen_spirv"
208224 . arg ( format ! ( "+{}" , toolchain_channel) )
209225 . args ( [ "build" , "--release" ] )
210226 . env_remove ( "RUSTC" ) ;
227+ if source_is_path {
228+ build_command. args ( [ "-p" , "rustc_codegen_spirv" , "--lib" ] ) ;
229+ }
211230
212231 log:: debug!( "building artifacts with `{build_command:?}`" ) ;
213232
@@ -231,11 +250,18 @@ package = "rustc_codegen_spirv"
231250 . join ( & dylib_filename) ;
232251 if dylib_path. is_file ( ) {
233252 log:: info!( "successfully built {}" , dylib_path. display( ) ) ;
234- std:: fs:: rename ( & dylib_path, & dest_dylib_path) . context ( "renaming dylib path" ) ?;
253+ if !source_is_path {
254+ std:: fs:: rename ( & dylib_path, & dest_dylib_path)
255+ . context ( "renaming dylib path" ) ?;
256+ }
235257 } else {
236258 log:: error!( "could not find {}" , dylib_path. display( ) ) ;
237259 anyhow:: bail!( "`rustc_codegen_spirv` build failed" ) ;
238260 }
261+
262+ log:: debug!( "write_target_spec_files" ) ;
263+ self . write_target_spec_files ( )
264+ . context ( "writing target spec files" ) ?;
239265 }
240266
241267 self . spirv_install . dylib_path = dest_dylib_path;
0 commit comments