5151//! for example.
5252use std:: io:: Write ;
5353
54- use cargo_gpu:: { spirv_builder_cli:: ShaderModule , Linkage } ;
5554use clap:: { Parser , Subcommand } ;
55+ use spirv_builder_cli:: { Linkage , ShaderModule } ;
5656
5757const SPIRV_BUILDER_CLI_CARGO_TOML : & str = include_str ! ( "../../spirv-builder-cli/Cargo.toml" ) ;
5858const SPIRV_BUILDER_CLI_MAIN : & str = include_str ! ( "../../spirv-builder-cli/src/main.rs" ) ;
59- const SPIRV_BUILDER_CLI_LIB : & str = include_str ! ( "lib.rs" ) ;
59+ const SPIRV_BUILDER_CLI_LIB : & str = include_str ! ( "../../spirv-builder-cli/src/ lib.rs" ) ;
6060const SPIRV_BUILDER_FILES : & [ ( & str , & str ) ] = & [
6161 ( "Cargo.toml" , SPIRV_BUILDER_CLI_CARGO_TOML ) ,
6262 ( "src/main.rs" , SPIRV_BUILDER_CLI_MAIN ) ,
@@ -352,11 +352,21 @@ impl Install {
352352 self . write_source_files ( ) ;
353353 self . write_target_spec_files ( ) ;
354354
355- log :: debug! ( "building artifacts ") ;
356- let output = std :: process :: Command :: new ( "cargo" )
355+ let mut command = std :: process :: Command :: new ( "cargo ") ;
356+ command
357357 . current_dir ( & checkout)
358358 . arg ( format ! ( "+{}" , spirv_version. channel) )
359359 . args ( [ "build" , "--release" ] )
360+ . args ( [ "--no-default-features" ] ) ;
361+
362+ command. args ( [
363+ "--features" ,
364+ & Self :: get_required_spirv_builder_version ( spirv_version. channel ) ,
365+ ] ) ;
366+
367+ log:: debug!( "building artifacts with `{:?}`" , command) ;
368+
369+ let output = command
360370 . stdout ( std:: process:: Stdio :: inherit ( ) )
361371 . stderr ( std:: process:: Stdio :: inherit ( ) )
362372 . output ( )
@@ -382,6 +392,28 @@ impl Install {
382392 }
383393 ( dest_dylib_path, dest_cli_path)
384394 }
395+
396+ /// The `spirv-builder` crate from the main `rust-gpu` repo hasn't always been setup to
397+ /// interact with `cargo-gpu`. Older versions don't have the same `SpirvBuilder` interface. So
398+ /// here we choose the right Cargo feature to enable/disable code in `spirv-builder-cli`.
399+ ///
400+ /// TODO:
401+ /// * Download the actual `rust-gpu` repo as pinned in the shader's `Cargo.lock` and get the
402+ /// `spirv-builder` version from there.
403+ /// * Warn the user that certain `cargo-gpu` features aren't available when building with
404+ /// older versions of `spirv-builder`, eg setting the target spec.
405+ fn get_required_spirv_builder_version ( toolchain_channel : String ) -> String {
406+ let parse_date = chrono:: NaiveDate :: parse_from_str;
407+ let datetime = parse_date ( & toolchain_channel, "nightly-%Y-%m-%d" ) . unwrap ( ) ;
408+ let pre_cli_date = parse_date ( "2024-04-24" , "%Y-%m-%d" ) . unwrap ( ) ;
409+
410+ if datetime < pre_cli_date {
411+ "spirv-builder-pre-cli"
412+ } else {
413+ "spirv-builder-0_10"
414+ }
415+ . into ( )
416+ }
385417}
386418
387419#[ derive( Parser , Debug ) ]
@@ -428,7 +460,7 @@ impl Build {
428460 std:: env:: current_dir( ) . unwrap( ) . display( )
429461 ) ;
430462
431- let spirv_builder_args = cargo_gpu :: spirv_builder_cli:: Args {
463+ let spirv_builder_args = spirv_builder_cli:: Args {
432464 dylib_path,
433465 shader_crate : self . shader_crate . clone ( ) ,
434466 shader_target : self . shader_target . clone ( ) ,
0 commit comments