Skip to content

Updating target json support #75

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ exclude = [
resolver = "2"

[workspace.dependencies]
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu.git", rev = "6d7c1cd6c0920500a3fa8c01c23e7b74302c15c4", default-features = false }
spirv-builder = { git = "https://github.com/Rust-GPU/rust-gpu.git", rev = "86fc48032c4cd4afb74f1d81ae859711d20386a1", default-features = false }
anyhow = "1.0.94"
clap = { version = "4.5.37", features = ["derive"] }
crossterm = "0.28.1"
Expand All @@ -29,6 +29,9 @@ test-log = "0.2.16"
cargo_metadata = "0.19.2"
semver = "1.0.26"

# This crate MUST NEVER be upgraded, we need this particular "first" version to support old rust-gpu builds
legacy_target_specs = { package = "rustc_codegen_spirv-target-specs", version = "0.9.0", features = ["include_str"] }

[workspace.lints.rust]
missing_docs = "warn"

Expand Down
1 change: 1 addition & 0 deletions crates/cargo-gpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ default-run = "cargo-gpu"
cargo_metadata.workspace = true
anyhow.workspace = true
spirv-builder = { workspace = true, features = ["clap", "watch"] }
legacy_target_specs.workspace = true
clap.workspace = true
directories.workspace = true
env_logger.workspace = true
Expand Down
106 changes: 0 additions & 106 deletions crates/cargo-gpu/src/args.rs

This file was deleted.

102 changes: 62 additions & 40 deletions crates/cargo-gpu/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,46 @@
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
//! `cargo gpu build`, analogous to `cargo build`

use crate::args::BuildArgs;
use crate::install::Install;
use crate::linkage::Linkage;
use crate::lockfile::LockfileMismatchHandler;
use crate::{install::Install, target_spec_dir};
use anyhow::Context as _;
use spirv_builder::{CompileResult, ModuleResult};
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder};
use std::io::Write as _;
use std::path::PathBuf;

/// Args for just a build
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct BuildArgs {
/// Path to the output directory for the compiled shaders.
#[clap(long, short, default_value = "./")]
pub output_dir: PathBuf,

/// Watch the shader crate directory and automatically recompile on changes.
#[clap(long, short, action)]
pub watch: bool,

/// the flattened [`SpirvBuilder`]
#[clap(flatten)]
#[serde(flatten)]
pub spirv_builder: SpirvBuilder,

///Renames the manifest.json file to the given name
#[clap(long, short, default_value = "manifest.json")]
pub manifest_file: String,
}

impl Default for BuildArgs {
#[inline]
fn default() -> Self {
Self {
output_dir: PathBuf::from("./"),
watch: false,
spirv_builder: SpirvBuilder::default(),
manifest_file: String::from("manifest.json"),
}
}
}

/// `cargo build` subcommands
#[derive(Clone, clap::Parser, Debug, serde::Deserialize, serde::Serialize)]
Expand All @@ -19,54 +52,46 @@ pub struct Build {

/// CLI args for configuring the build of the shader
#[clap(flatten)]
pub build_args: BuildArgs,
pub build: BuildArgs,
}

impl Build {
/// Entrypoint
pub fn run(&mut self) -> anyhow::Result<()> {
let (rustc_codegen_spirv_location, toolchain_channel) = self.install.run()?;
let installed_backend = self.install.run()?;

let _lockfile_mismatch_handler = LockfileMismatchHandler::new(
&self.install.spirv_install.shader_crate,
&toolchain_channel,
self.install
.spirv_install
.force_overwrite_lockfiles_v4_to_v3,
&self.install.shader_crate,
&installed_backend.toolchain_channel,
self.install.force_overwrite_lockfiles_v4_to_v3,
)?;

let builder = &mut self.build_args.spirv_builder;
builder.rustc_codegen_spirv_location = Some(rustc_codegen_spirv_location);
builder.toolchain_overwrite = Some(toolchain_channel);
builder.path_to_crate = Some(self.install.spirv_install.shader_crate.clone());
builder.path_to_target_spec = Some(target_spec_dir()?.join(format!(
"{}.json",
builder.target.as_ref().context("expect target to be set")?
)));
let builder = &mut self.build.spirv_builder;
builder.path_to_crate = Some(self.install.shader_crate.clone());
installed_backend.configure_spirv_builder(builder)?;

// Ensure the shader output dir exists
log::debug!(
"ensuring output-dir '{}' exists",
self.build_args.output_dir.display()
self.build.output_dir.display()
);
std::fs::create_dir_all(&self.build_args.output_dir)?;
let canonicalized = self.build_args.output_dir.canonicalize()?;
log::debug!("canonicalized output dir: {canonicalized:?}");
self.build_args.output_dir = canonicalized;
std::fs::create_dir_all(&self.build.output_dir)?;
let canonicalized = self.build.output_dir.canonicalize()?;
log::debug!("canonicalized output dir: {}", canonicalized.display());
self.build.output_dir = canonicalized;

// Ensure the shader crate exists
self.install.spirv_install.shader_crate =
self.install.spirv_install.shader_crate.canonicalize()?;
self.install.shader_crate = self.install.shader_crate.canonicalize()?;
anyhow::ensure!(
self.install.spirv_install.shader_crate.exists(),
self.install.shader_crate.exists(),
"shader crate '{}' does not exist. (Current dir is '{}')",
self.install.spirv_install.shader_crate.display(),
self.install.shader_crate.display(),
std::env::current_dir()?.display()
);

if self.build_args.watch {
if self.build.watch {
let this = self.clone();
self.build_args
self.build
.spirv_builder
.watch(move |result, accept| {
let result1 = this.parse_compilation_result(&result);
Expand All @@ -79,9 +104,9 @@ impl Build {
} else {
crate::user_output!(
"Compiling shaders at {}...\n",
self.install.spirv_install.shader_crate.display()
self.install.shader_crate.display()
);
let result = self.build_args.spirv_builder.build()?;
let result = self.build.spirv_builder.build()?;
self.parse_compilation_result(&result)?;
}
Ok(())
Expand All @@ -104,7 +129,7 @@ impl Build {
.into_iter()
.map(|(entry, filepath)| -> anyhow::Result<Linkage> {
use relative_path::PathExt as _;
let path = self.build_args.output_dir.join(
let path = self.build.output_dir.join(
filepath
.file_name()
.context("Couldn't parse file name from shader module path")?,
Expand All @@ -114,10 +139,10 @@ impl Build {
log::debug!(
"linkage of {} relative to {}",
path.display(),
self.install.spirv_install.shader_crate.display()
self.install.shader_crate.display()
);
let spv_path = path
.relative_to(&self.install.spirv_install.shader_crate)
.relative_to(&self.install.shader_crate)
.map_or(path, |path_relative_to_shader_crate| {
path_relative_to_shader_crate.to_path("")
});
Expand All @@ -128,10 +153,7 @@ impl Build {
linkage.sort();

// Write the shader manifest json file
let manifest_path = self
.build_args
.output_dir
.join(&self.build_args.manifest_file);
let manifest_path = self.build.output_dir.join(&self.build.manifest_file);
let json = serde_json::to_string_pretty(&linkage)?;
let mut file = std::fs::File::create(&manifest_path).with_context(|| {
format!(
Expand Down Expand Up @@ -176,8 +198,8 @@ mod test {
command: Command::Build(build),
} = Cli::parse_from(args)
{
assert_eq!(shader_crate_path, build.install.spirv_install.shader_crate);
assert_eq!(output_dir, build.build_args.output_dir);
assert_eq!(shader_crate_path, build.install.shader_crate);
assert_eq!(output_dir, build.build.output_dir);

// TODO:
// For some reason running a full build (`build.run()`) inside tests fails on Windows.
Expand Down
Loading
Loading