Skip to content

Commit

Permalink
sanitise cache checkout dir and sanity test it
Browse files Browse the repository at this point in the history
  • Loading branch information
schell committed Dec 7, 2024
1 parent c5c88d0 commit 9b95094
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- uses: moonrepo/setup-rust@v1
- run: rustup default stable
- run: rustup update
- run: cargo test
- run: cargo install --path crates/cargo-gpu
- run: cargo gpu install
- run: cargo gpu build --shader-crate crates/shader-crate-template --output-dir test-shaders
Expand Down
45 changes: 37 additions & 8 deletions crates/cargo-gpu/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,34 @@ struct Spirv {
channel: String,
}

impl Default for Spirv {
fn default() -> Self {
Self {
dep: Self::DEFAULT_DEP.into(),
channel: Self::DEFAULT_CHANNEL.into(),
}
}
}

impl core::fmt::Display for Spirv {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format!("{}+{}", self.dep, self.channel).fmt(f)
}
}

impl Spirv {
const DEFAULT_DEP: &str = r#"{ git = "https://github.com/Rust-GPU/rust-gpu.git" }"#;
const DEFAULT_CHANNEL: &str = "nightly-2024-04-24";

/// Returns a string suitable to use as a directory.
///
/// Created from the spirv-builder source dep and the rustc channel.
fn to_dirname(&self) -> String {
self.to_string()
.replace([std::path::MAIN_SEPARATOR, '.', ':', '@', '='], "_")
.replace(
[std::path::MAIN_SEPARATOR, '\\', '/', '.', ':', '@', '='],
"_",
)
.split(['{', '}', ' ', '\n', '"', '\''])
.collect::<Vec<_>>()
.concat()
Expand Down Expand Up @@ -258,16 +273,13 @@ fn target_spec_dir() -> std::path::PathBuf {
#[derive(Parser, Debug)]
struct Install {
/// spirv-builder dependency, written just like in a Cargo.toml file.
#[clap(
long,
default_value = r#"{ git = "https://github.com/Rust-GPU/rust-gpu.git" }"#
)]
#[clap(long, default_value = Spirv::DEFAULT_DEP)]
spirv_builder: String,

/// Rust toolchain channel to use to build `spirv-builder`.
///
/// This must match the `spirv_builder` argument.
#[clap(long, default_value = "nightly-2024-04-24")]
#[clap(long, default_value = Spirv::DEFAULT_CHANNEL)]
rust_toolchain: String,

/// Force `spirv-builder-cli` and `rustc_codegen_spirv` to be rebuilt.
Expand Down Expand Up @@ -312,6 +324,7 @@ impl Install {
fn run(&self) -> (std::path::PathBuf, std::path::PathBuf) {
// Ensure the cache dir exists
let cache_dir = cache_dir();
log::info!("cache directory is '{}'", cache_dir.display());
std::fs::create_dir_all(&cache_dir).unwrap_or_else(|e| {
log::error!(
"could not create cache directory '{}': {e}",
Expand Down Expand Up @@ -757,7 +770,7 @@ fn dump_full_usage_for_readme() {
println!("{}", buffer);
}

fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, depth: usize) {
fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, _depth: usize) {
if cmd.get_name() == "help" {
return;
}
Expand All @@ -774,14 +787,30 @@ fn write_help(buffer: &mut impl std::io::Write, cmd: &mut clap::Command, depth:

for sub in cmd.get_subcommands_mut() {
let _ = writeln!(buffer);
write_help(buffer, sub, depth + 1);
write_help(buffer, sub, _depth + 1);
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn cached_checkout_dir_sanity() {
let spirv = Spirv::default();
let dir = spirv.cached_checkout_path();
let name = dir
.file_name()
.unwrap()
.to_str()
.map(|s| s.to_string())
.unwrap();
assert_eq!(
"git_https___github_com_Rust-GPU_rust-gpu_git+nightly-2024-04-24",
&name
);
}

#[test]
fn builder_from_params() {
let shader_crate = std::path::PathBuf::from("../shader-crate-template");
Expand Down

0 comments on commit 9b95094

Please sign in to comment.