@@ -6,7 +6,7 @@ use crate::spirv_source::{
66use crate :: { cache_dir, spirv_source:: SpirvSource , target_spec_dir} ;
77use anyhow:: Context as _;
88use log:: trace;
9- use spirv_builder:: TARGET_SPECS ;
9+ use spirv_builder:: { SpirvBuilder , TARGET_SPECS } ;
1010use std:: io:: Write as _;
1111use std:: path:: { Path , PathBuf } ;
1212
@@ -77,6 +77,40 @@ pub struct Install {
7777 pub force_overwrite_lockfiles_v4_to_v3 : bool ,
7878}
7979
80+ /// Represents a functional backend installation, whether it was cached or just installed.
81+ #[ derive( Clone , Debug ) ]
82+ pub struct InstalledBackend {
83+ /// path to the rustc_codegen_spirv dylib
84+ pub rustc_codegen_spirv_location : PathBuf ,
85+ /// toolchain channel name
86+ pub toolchain_channel : String ,
87+ }
88+
89+ impl InstalledBackend {
90+ /// Creates a new `SpirvBuilder` configured to use this installed backend.
91+ pub fn to_spirv_builder (
92+ & self ,
93+ path_to_crate : impl AsRef < Path > ,
94+ target : impl Into < String > ,
95+ ) -> SpirvBuilder {
96+ let mut builder = SpirvBuilder :: new ( path_to_crate, target) ;
97+ self . configure_spirv_builder ( & mut builder)
98+ . expect ( "unreachable" ) ;
99+ builder
100+ }
101+
102+ /// Configures the supplied [`SpirvBuilder`]. `SpirvBuilder.target` must be set and must not change after calling this function.
103+ pub fn configure_spirv_builder ( & self , builder : & mut SpirvBuilder ) -> anyhow:: Result < ( ) > {
104+ builder. rustc_codegen_spirv_location = Some ( self . rustc_codegen_spirv_location . clone ( ) ) ;
105+ builder. toolchain_overwrite = Some ( self . toolchain_channel . clone ( ) ) ;
106+ builder. path_to_target_spec = Some ( target_spec_dir ( ) ?. join ( format ! (
107+ "{}.json" ,
108+ builder. target. as_ref( ) . context( "expect target to be set" ) ?
109+ ) ) ) ;
110+ Ok ( ( ) )
111+ }
112+ }
113+
80114impl Default for Install {
81115 #[ inline]
82116 fn default ( ) -> Self {
@@ -164,7 +198,7 @@ package = "rustc_codegen_spirv"
164198
165199 /// Install the binary pair and return the `(dylib_path, toolchain_channel)`.
166200 #[ expect( clippy:: too_many_lines, reason = "it's fine" ) ]
167- pub fn run ( & mut self ) -> anyhow:: Result < ( PathBuf , String ) > {
201+ pub fn run ( & mut self ) -> anyhow:: Result < InstalledBackend > {
168202 // Ensure the cache dir exists
169203 let cache_dir = cache_dir ( ) ?;
170204 log:: info!( "cache directory is '{}'" , cache_dir. display( ) ) ;
@@ -282,6 +316,9 @@ package = "rustc_codegen_spirv"
282316 . context ( "writing target spec files" ) ?;
283317 }
284318
285- Ok ( ( dest_dylib_path, toolchain_channel) )
319+ Ok ( InstalledBackend {
320+ rustc_codegen_spirv_location : dest_dylib_path,
321+ toolchain_channel,
322+ } )
286323 }
287324}
0 commit comments