Skip to content

Commit d1a00d8

Browse files
authored
Merge pull request #49 from Rust-GPU/fix/cargo-update
fix: crate defined output-dir is relative to the Cargo.toml it's defined in
2 parents f3d13dc + 94e8321 commit d1a00d8

File tree

14 files changed

+581
-70
lines changed

14 files changed

+581
-70
lines changed

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[alias]
2+
xtask = "run --package xtask --"
3+
build-test-shader = "xtask test-build"

.github/workflows/push.yaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
rustup update
3636
- run: cargo test
3737
- name: Run a full build
38-
run: just build-shader-template
38+
run: cargo xtask test-build
3939

4040

4141
lints:
@@ -45,5 +45,7 @@ jobs:
4545
- uses: moonrepo/setup-rust@v1
4646
- uses: extractions/setup-just@v2
4747
- uses: cargo-bins/cargo-binstall@main
48-
- run: just setup-lints
49-
- run: just lints
48+
- run: cargo binstall cargo-shear
49+
- run: cargo clippy -- --deny warnings
50+
- run: cargo fmt --check
51+
- run: cargo shear

Cargo.lock

Lines changed: 74 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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[workspace]
22
members = [
3-
"crates/cargo-gpu",
3+
"crates/cargo-gpu",
4+
"crates/xtask",
45
]
56

67
exclude = [
@@ -22,14 +23,14 @@ clap = { version = "4.4.8", features = ["derive"] }
2223
chrono = { version = "0.4.38", default-features = false, features = ["std"] }
2324
crossterm = "0.28.1"
2425
directories = "5.0.1"
25-
env_home = "0.1.0"
2626
env_logger = "0.10"
2727
http = "1.2.0"
2828
log = "0.4"
2929
relative-path = "1.9.3"
3030
serde = { version = "1.0.214", features = ["derive"] }
3131
serde_json = "1.0.132"
3232
toml = "0.8.19"
33+
tempdir = "0.3.7"
3334
test-log = "0.2.16"
3435

3536
[workspace.lints.rust]

crates/cargo-gpu/src/build.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::shadow_reuse, reason = "let's not be silly")]
2+
#![allow(clippy::unwrap_used, reason = "this is basically a test")]
13
//! `cargo gpu build`, analogous to `cargo build`
24
35
use anyhow::Context as _;
@@ -20,6 +22,7 @@ pub struct Build {
2022

2123
impl Build {
2224
/// Entrypoint
25+
#[expect(clippy::too_many_lines, reason = "these lines are fine")]
2326
pub fn run(&mut self) -> anyhow::Result<()> {
2427
let spirv_builder_cli_path = self.install.run()?;
2528

@@ -100,11 +103,19 @@ impl Build {
100103
.file_name()
101104
.context("Couldn't parse file name from shader module path")?,
102105
);
106+
log::debug!("copying {} to {}", filepath.display(), path.display());
103107
std::fs::copy(&filepath, &path)?;
104-
let path_relative_to_shader_crate = path
105-
.relative_to(&self.install.spirv_install.shader_crate)?
106-
.to_path("");
107-
Ok(Linkage::new(entry, path_relative_to_shader_crate))
108+
log::debug!(
109+
"linkage of {} relative to {}",
110+
path.display(),
111+
self.install.spirv_install.shader_crate.display()
112+
);
113+
let spv_path = path
114+
.relative_to(&self.install.spirv_install.shader_crate)
115+
.map_or(path, |path_relative_to_shader_crate| {
116+
path_relative_to_shader_crate.to_path("")
117+
});
118+
Ok(Linkage::new(entry, spv_path))
108119
},
109120
)
110121
.collect::<anyhow::Result<Vec<Linkage>>>()?;
@@ -173,7 +184,7 @@ mod test {
173184
// For some reason running a full build (`build.run()`) inside tests fails on Windows.
174185
// The error is in the `build.rs` step of compiling `spirv-tools-sys`. It is not clear
175186
// from the logged error what the problem is. For now we'll just run a full build
176-
// outside the tests environment, see `justfile`'s `build-shader-template`.
187+
// outside the tests environment, see `xtask`'s `test-build`.
177188
} else {
178189
panic!("was not a build command");
179190
}

crates/cargo-gpu/src/config.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ impl Config {
104104
}
105105
(left, right) => {
106106
if let Some(pointer) = maybe_pointer {
107-
let default = defaults
108-
.pointer(pointer)
109-
.context(format!("Config `{pointer}` not found in defaults"))?;
107+
let default = defaults.pointer(pointer).context(format!(
108+
"Configuration option with path `{pointer}` was not found in the default configuration, \
109+
which is:\ndefaults: {defaults:#?}"
110+
))?;
110111
if &right != default {
111112
// Only overwrite if the new value differs from the defaults.
112113
*left = right;
@@ -184,10 +185,17 @@ mod test {
184185
let shader_crate_path = update_cargo_output_dir();
185186

186187
let args = Config::clap_command_with_cargo_config(&shader_crate_path, vec![]).unwrap();
187-
assert_eq!(
188-
args.build_args.output_dir,
189-
std::path::Path::new("/the/moon")
190-
);
188+
if cfg!(target_os = "windows") {
189+
assert_eq!(
190+
args.build_args.output_dir,
191+
std::path::Path::new("C:/the/moon")
192+
);
193+
} else {
194+
assert_eq!(
195+
args.build_args.output_dir,
196+
std::path::Path::new("/the/moon")
197+
);
198+
}
191199
}
192200

193201
#[test_log::test]

crates/cargo-gpu/src/install.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -221,25 +221,25 @@ impl Install {
221221
self.spirv_install.shader_crate.display()
222222
);
223223

224-
let mut command = std::process::Command::new("cargo");
225-
command
224+
let mut build_command = std::process::Command::new("cargo");
225+
build_command
226226
.current_dir(&checkout)
227227
.arg(format!("+{}", spirv_version.channel))
228228
.args(["build", "--release"])
229229
.args(["--no-default-features"]);
230230

231-
command.args([
231+
build_command.args([
232232
"--features",
233233
&Self::get_required_spirv_builder_version(spirv_version.date)?,
234234
]);
235235

236-
log::debug!("building artifacts with `{:?}`", command);
236+
log::debug!("building artifacts with `{:?}`", build_command);
237237

238-
let output = command
238+
let build_output = build_command
239239
.stdout(std::process::Stdio::inherit())
240240
.stderr(std::process::Stdio::inherit())
241241
.output()?;
242-
anyhow::ensure!(output.status.success(), "...build error!");
242+
anyhow::ensure!(build_output.status.success(), "...build error!");
243243

244244
if dylib_path.is_file() {
245245
log::info!("successfully built {}", dylib_path.display());

crates/cargo-gpu/src/metadata.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Get config from the shader crate's `Cargo.toml` `[*.metadata.rust-gpu.*]`
22
3+
use serde_json::Value;
4+
35
/// `Metadata` refers to the `[metadata.*]` section of `Cargo.toml` that `cargo` formally
46
/// ignores so that packages can implement their own behaviour with it.
57
#[derive(Debug)]
@@ -47,12 +49,33 @@ impl Metadata {
4749
let mut metadata = crate::config::Config::defaults_as_json()?;
4850
crate::config::Config::json_merge(
4951
&mut metadata,
50-
Self::get_workspace_metadata(cargo_json),
52+
{
53+
log::debug!("looking for workspace metadata");
54+
let ws_meta = Self::get_workspace_metadata(cargo_json);
55+
log::trace!("workspace_metadata: {ws_meta:#?}");
56+
ws_meta
57+
},
5158
None,
5259
)?;
5360
crate::config::Config::json_merge(
5461
&mut metadata,
55-
Self::get_crate_metadata(cargo_json, path)?,
62+
{
63+
log::debug!("looking for crate metadata");
64+
let mut crate_meta = Self::get_crate_metadata(cargo_json, path)?;
65+
log::trace!("crate_metadata: {crate_meta:#?}");
66+
if let Some(output_path) = crate_meta.pointer_mut("/build/output_dir") {
67+
log::debug!("found output-dir path in crate metadata: {:?}", output_path);
68+
if let Some(output_dir) = output_path.clone().as_str() {
69+
let new_output_path = path.join(output_dir);
70+
*output_path = Value::String(format!("{}", new_output_path.display()));
71+
log::debug!(
72+
"setting that to be relative to the Cargo.toml it was found in: {}",
73+
new_output_path.display()
74+
);
75+
}
76+
}
77+
crate_meta
78+
},
5679
None,
5780
)?;
5881

@@ -118,6 +141,7 @@ impl Metadata {
118141
let manifest_path = manifest_path_dirty.replace(r"\\?\", "");
119142
log::debug!("Matching shader crate path with manifest path: {shader_crate_path} == {manifest_path}?");
120143
if manifest_path == shader_crate_path {
144+
log::debug!("...matches! Getting metadata");
121145
let mut metadata = package
122146
.pointer("/metadata/rust-gpu")
123147
.unwrap_or(&empty_json_object)

0 commit comments

Comments
 (0)