Skip to content

Commit 8087e22

Browse files
authored
Updating target json support (#75)
* main new clippy fixes * remove internal `InstallArgs::dylib_path` * remove mod args, flatten InstallArgs -> Install and BuildArgs -> Build * add InstalledBackend struct * rename `checkout` to `install_dir` * allow searching multiple packages from one `cargo metadata` query * remove non_exhaustive attribs * add `SpirvSource.is_path()` shortcut * create target-specs folder per installation, pulled from `rustc_codegen_spirv_target_specs`, with legacy fallback * appease clippy
1 parent 2180d73 commit 8087e22

File tree

12 files changed

+384
-326
lines changed

12 files changed

+384
-326
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = [
1313
resolver = "2"
1414

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

32+
# This crate MUST NEVER be upgraded, we need this particular "first" version to support old rust-gpu builds
33+
legacy_target_specs = { package = "rustc_codegen_spirv-target-specs", version = "0.9.0", features = ["include_str"] }
34+
3235
[workspace.lints.rust]
3336
missing_docs = "warn"
3437

crates/cargo-gpu/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ default-run = "cargo-gpu"
1414
cargo_metadata.workspace = true
1515
anyhow.workspace = true
1616
spirv-builder = { workspace = true, features = ["clap", "watch"] }
17+
legacy_target_specs.workspace = true
1718
clap.workspace = true
1819
directories.workspace = true
1920
env_logger.workspace = true

crates/cargo-gpu/src/args.rs

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

crates/cargo-gpu/src/build.rs

Lines changed: 62 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,46 @@
22
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
33
//! `cargo gpu build`, analogous to `cargo build`
44
5-
use crate::args::BuildArgs;
5+
use crate::install::Install;
66
use crate::linkage::Linkage;
77
use crate::lockfile::LockfileMismatchHandler;
8-
use crate::{install::Install, target_spec_dir};
98
use anyhow::Context as _;
10-
use spirv_builder::{CompileResult, ModuleResult};
9+
use spirv_builder::{CompileResult, ModuleResult, SpirvBuilder};
1110
use std::io::Write as _;
11+
use std::path::PathBuf;
12+
13+
/// Args for just a build
14+
#[derive(clap::Parser, Debug, Clone, serde::Deserialize, serde::Serialize)]
15+
pub struct BuildArgs {
16+
/// Path to the output directory for the compiled shaders.
17+
#[clap(long, short, default_value = "./")]
18+
pub output_dir: PathBuf,
19+
20+
/// Watch the shader crate directory and automatically recompile on changes.
21+
#[clap(long, short, action)]
22+
pub watch: bool,
23+
24+
/// the flattened [`SpirvBuilder`]
25+
#[clap(flatten)]
26+
#[serde(flatten)]
27+
pub spirv_builder: SpirvBuilder,
28+
29+
///Renames the manifest.json file to the given name
30+
#[clap(long, short, default_value = "manifest.json")]
31+
pub manifest_file: String,
32+
}
33+
34+
impl Default for BuildArgs {
35+
#[inline]
36+
fn default() -> Self {
37+
Self {
38+
output_dir: PathBuf::from("./"),
39+
watch: false,
40+
spirv_builder: SpirvBuilder::default(),
41+
manifest_file: String::from("manifest.json"),
42+
}
43+
}
44+
}
1245

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

2053
/// CLI args for configuring the build of the shader
2154
#[clap(flatten)]
22-
pub build_args: BuildArgs,
55+
pub build: BuildArgs,
2356
}
2457

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

3063
let _lockfile_mismatch_handler = LockfileMismatchHandler::new(
31-
&self.install.spirv_install.shader_crate,
32-
&toolchain_channel,
33-
self.install
34-
.spirv_install
35-
.force_overwrite_lockfiles_v4_to_v3,
64+
&self.install.shader_crate,
65+
&installed_backend.toolchain_channel,
66+
self.install.force_overwrite_lockfiles_v4_to_v3,
3667
)?;
3768

38-
let builder = &mut self.build_args.spirv_builder;
39-
builder.rustc_codegen_spirv_location = Some(rustc_codegen_spirv_location);
40-
builder.toolchain_overwrite = Some(toolchain_channel);
41-
builder.path_to_crate = Some(self.install.spirv_install.shader_crate.clone());
42-
builder.path_to_target_spec = Some(target_spec_dir()?.join(format!(
43-
"{}.json",
44-
builder.target.as_ref().context("expect target to be set")?
45-
)));
69+
let builder = &mut self.build.spirv_builder;
70+
builder.path_to_crate = Some(self.install.shader_crate.clone());
71+
installed_backend.configure_spirv_builder(builder)?;
4672

4773
// Ensure the shader output dir exists
4874
log::debug!(
4975
"ensuring output-dir '{}' exists",
50-
self.build_args.output_dir.display()
76+
self.build.output_dir.display()
5177
);
52-
std::fs::create_dir_all(&self.build_args.output_dir)?;
53-
let canonicalized = self.build_args.output_dir.canonicalize()?;
54-
log::debug!("canonicalized output dir: {canonicalized:?}");
55-
self.build_args.output_dir = canonicalized;
78+
std::fs::create_dir_all(&self.build.output_dir)?;
79+
let canonicalized = self.build.output_dir.canonicalize()?;
80+
log::debug!("canonicalized output dir: {}", canonicalized.display());
81+
self.build.output_dir = canonicalized;
5682

5783
// Ensure the shader crate exists
58-
self.install.spirv_install.shader_crate =
59-
self.install.spirv_install.shader_crate.canonicalize()?;
84+
self.install.shader_crate = self.install.shader_crate.canonicalize()?;
6085
anyhow::ensure!(
61-
self.install.spirv_install.shader_crate.exists(),
86+
self.install.shader_crate.exists(),
6287
"shader crate '{}' does not exist. (Current dir is '{}')",
63-
self.install.spirv_install.shader_crate.display(),
88+
self.install.shader_crate.display(),
6489
std::env::current_dir()?.display()
6590
);
6691

67-
if self.build_args.watch {
92+
if self.build.watch {
6893
let this = self.clone();
69-
self.build_args
94+
self.build
7095
.spirv_builder
7196
.watch(move |result, accept| {
7297
let result1 = this.parse_compilation_result(&result);
@@ -79,9 +104,9 @@ impl Build {
79104
} else {
80105
crate::user_output!(
81106
"Compiling shaders at {}...\n",
82-
self.install.spirv_install.shader_crate.display()
107+
self.install.shader_crate.display()
83108
);
84-
let result = self.build_args.spirv_builder.build()?;
109+
let result = self.build.spirv_builder.build()?;
85110
self.parse_compilation_result(&result)?;
86111
}
87112
Ok(())
@@ -104,7 +129,7 @@ impl Build {
104129
.into_iter()
105130
.map(|(entry, filepath)| -> anyhow::Result<Linkage> {
106131
use relative_path::PathExt as _;
107-
let path = self.build_args.output_dir.join(
132+
let path = self.build.output_dir.join(
108133
filepath
109134
.file_name()
110135
.context("Couldn't parse file name from shader module path")?,
@@ -114,10 +139,10 @@ impl Build {
114139
log::debug!(
115140
"linkage of {} relative to {}",
116141
path.display(),
117-
self.install.spirv_install.shader_crate.display()
142+
self.install.shader_crate.display()
118143
);
119144
let spv_path = path
120-
.relative_to(&self.install.spirv_install.shader_crate)
145+
.relative_to(&self.install.shader_crate)
121146
.map_or(path, |path_relative_to_shader_crate| {
122147
path_relative_to_shader_crate.to_path("")
123148
});
@@ -128,10 +153,7 @@ impl Build {
128153
linkage.sort();
129154

130155
// Write the shader manifest json file
131-
let manifest_path = self
132-
.build_args
133-
.output_dir
134-
.join(&self.build_args.manifest_file);
156+
let manifest_path = self.build.output_dir.join(&self.build.manifest_file);
135157
let json = serde_json::to_string_pretty(&linkage)?;
136158
let mut file = std::fs::File::create(&manifest_path).with_context(|| {
137159
format!(
@@ -176,8 +198,8 @@ mod test {
176198
command: Command::Build(build),
177199
} = Cli::parse_from(args)
178200
{
179-
assert_eq!(shader_crate_path, build.install.spirv_install.shader_crate);
180-
assert_eq!(output_dir, build.build_args.output_dir);
201+
assert_eq!(shader_crate_path, build.install.shader_crate);
202+
assert_eq!(output_dir, build.build.output_dir);
181203

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

0 commit comments

Comments
 (0)