Skip to content

Make cargo-gpu a library, usable from build scripts #71

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
106 changes: 0 additions & 106 deletions crates/cargo-gpu/src/args.rs

This file was deleted.

100 changes: 61 additions & 39 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()?;
std::fs::create_dir_all(&self.build.output_dir)?;
let canonicalized = self.build.output_dir.canonicalize()?;
log::debug!("canonicalized output dir: {canonicalized:?}");
self.build_args.output_dir = canonicalized;
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