Skip to content

Commit

Permalink
Merge pull request #22 from Rust-GPU/feat/ci
Browse files Browse the repository at this point in the history
feature: initial CI workflow
  • Loading branch information
schell authored Dec 7, 2024
2 parents fab7a39 + 5918e69 commit 18f3ce9
Show file tree
Hide file tree
Showing 9 changed files with 367 additions and 13 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: push

on:
push:
branches:
- main
pull_request:

env:
# For setup-rust, see https://github.com/moonrepo/setup-rust/issues/22
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
install-and-build-shaders:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
defaults:
run:
shell: bash
env:
RUST_LOG: debug
steps:
- uses: actions/checkout@v2
- 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
- run: ls -lah test-shaders
- run: cat test-shaders/manifest.json

clippy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: moonrepo/setup-rust@v1
- run: cargo clippy
89 changes: 85 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/cargo-gpu",
"crates/shader-crate-template"
]

exclude = [
Expand Down
57 changes: 48 additions & 9 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 @@ -381,12 +394,21 @@ impl Install {
panic!("spirv-builder-cli build failed");
}

let cli_path = release.join("spirv-builder-cli");
let cli_path = if cfg!(target_os = "windows") {
release.join("spirv-builder-cli").with_extension("exe")
} else {
release.join("spirv-builder-cli")
};
if cli_path.is_file() {
log::info!("successfully built {}", cli_path.display());
std::fs::rename(&cli_path, &dest_cli_path).unwrap();
} else {
log::error!("could not find {}", cli_path.display());
log::debug!("contents of '{}':", release.display());
for entry in std::fs::read_dir(&release).unwrap() {
let entry = entry.unwrap();
log::debug!("{}", entry.file_name().to_string_lossy());
}
panic!("spirv-builder-cli build failed");
}
}
Expand Down Expand Up @@ -757,7 +779,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 +796,31 @@ 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() {
// Test that
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
1 change: 1 addition & 0 deletions crates/shader-crate-template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
Loading

0 comments on commit 18f3ce9

Please sign in to comment.