Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions crates/cargo-gpu/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ impl Build {
pub fn run(&mut self) -> anyhow::Result<()> {
let installed_backend = self.install.run()?;

if let Some(package) = self.install.package.as_ref() {
self.install.shader_crate =
crate::metadata::Metadata::resolve_package_to_shader_crate(package)?;
}

let _lockfile_mismatch_handler = LockfileMismatchHandler::new(
&self.install.shader_crate,
&installed_backend.toolchain_channel,
Expand Down
12 changes: 10 additions & 2 deletions crates/cargo-gpu/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,16 @@ impl InstalledBackend {
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, serde::Serialize)]
#[non_exhaustive]
pub struct Install {
/// Cargo target to compile.
///
/// Conflicts with `--shader-crate`.
#[clap(short, long, conflicts_with("shader_crate"))]
pub package: Option<String>,

/// Directory containing the shader crate to compile.
#[clap(long, alias("package"), short_alias('p'), default_value = "./")]
#[serde(alias = "package")]
///
/// Conflicts with `--package` or `-p`.
#[clap(long, default_value = "./", conflicts_with("package"))]
pub shader_crate: PathBuf,

#[expect(
Expand Down Expand Up @@ -132,6 +139,7 @@ impl Install {
#[must_use]
pub const fn from_shader_crate(shader_crate: PathBuf) -> Self {
Self {
package: None,
shader_crate,
spirv_builder_source: None,
spirv_builder_version: None,
Expand Down
25 changes: 25 additions & 0 deletions crates/cargo-gpu/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Get config from the shader crate's `Cargo.toml` `[*.metadata.rust-gpu.*]`

use anyhow::Context as _;
use cargo_metadata::MetadataCommand;
use serde_json::Value;

Expand All @@ -9,6 +10,28 @@ use serde_json::Value;
pub struct Metadata;

impl Metadata {
/// Resolve a package name to a crate directory.
pub fn resolve_package_to_shader_crate(package: &str) -> anyhow::Result<std::path::PathBuf> {
log::debug!("resolving package '{package}' to shader crate");
let metadata = MetadataCommand::new().exec()?;
let meta_package = metadata
.packages
.iter()
.find(|pkg| pkg.name.as_str() == package)
.context("Package not found in metadata")?;
let shader_crate_path: std::path::PathBuf = meta_package
.manifest_path
.parent()
.context("manifest is missing a parent directory")?
.to_path_buf()
.into();
log::debug!(
" determined shader crate path to be '{}'",
shader_crate_path.display()
);
Ok(shader_crate_path)
}

/// Convert `rust-gpu`-specific sections in `Cargo.toml` to `clap`-compatible arguments.
/// The section in question is: `[package.metadata.rust-gpu.*]`. See the `shader-crate-template`
/// for an example.
Expand All @@ -17,6 +40,7 @@ impl Metadata {
/// from the workspace `Cargo.toml`, then on top of those we merge any config from the shader
/// crate's `Cargo.toml`.
pub fn as_json(path: &std::path::PathBuf) -> anyhow::Result<Value> {
log::debug!("reading package metadata from {}", path.display());
let cargo_json = Self::get_cargo_toml_as_json(path)?;
let config = Self::merge_configs(&cargo_json, path)?;
Ok(config)
Expand All @@ -27,6 +51,7 @@ impl Metadata {
cargo_json: &cargo_metadata::Metadata,
path: &std::path::Path,
) -> anyhow::Result<Value> {
log::debug!("merging cargo metadata from {}", path.display());
let mut metadata = crate::config::Config::defaults_as_json()?;
crate::config::Config::json_merge(
&mut metadata,
Expand Down
Loading