Skip to content

Commit fab7a39

Browse files
authored
Merge pull request #20 from tombh/tombh/spirv-builder-backwards-compat
Features to allow `spirv-builder` backwards compat
2 parents 791d025 + f0b0da7 commit fab7a39

File tree

11 files changed

+349
-113
lines changed

11 files changed

+349
-113
lines changed

Cargo.lock

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ resolver = "2"
1111

1212
[workspace.dependencies]
1313
clap = { version = "4.4.8", features = ["derive"] }
14+
chrono = { version = "0.4.38", default-features = false }
1415
directories = "5.0.1"
1516
env_home = "0.1.0"
1617
env_logger = "0.10"

crates/cargo-gpu/Cargo.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ description = "Generates shader .spv files from rust-gpu shader crates"
66
readme = "../../README.md"
77

88
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
9-
10-
[lib]
11-
name = "cargo_gpu"
129

1310
[dependencies]
11+
spirv-builder-cli = { path = "../spirv-builder-cli", default-features = false }
1412
clap.workspace = true
1513
directories.workspace = true
1614
env_logger.workspace = true
@@ -19,6 +17,7 @@ relative-path.workspace = true
1917
serde.workspace = true
2018
serde_json.workspace = true
2119
toml.workspace = true
20+
chrono.workspace = true
2221

2322
# Enable incremental by default in release mode.
2423
[profile.release]

crates/cargo-gpu/src/lib.rs

Lines changed: 0 additions & 75 deletions
This file was deleted.

crates/cargo-gpu/src/main.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@
5151
//! for example.
5252
use std::io::Write;
5353

54-
use cargo_gpu::{spirv_builder_cli::ShaderModule, Linkage};
5554
use clap::{Parser, Subcommand};
55+
use spirv_builder_cli::{Linkage, ShaderModule};
5656

5757
const SPIRV_BUILDER_CLI_CARGO_TOML: &str = include_str!("../../spirv-builder-cli/Cargo.toml");
5858
const 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");
6060
const 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

Comments
 (0)