Skip to content

Commit 687403d

Browse files
committed
feat: add CI
1 parent c759d01 commit 687403d

File tree

2 files changed

+275
-0
lines changed

2 files changed

+275
-0
lines changed

.github/workflows/ci.yaml

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
branches: [main]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
RUSTFLAGS: --deny warnings
12+
RUSTDOCFLAGS: --deny warnings
13+
SLANG_TAG: 2025.18.2
14+
15+
jobs:
16+
# Check formatting.
17+
format:
18+
name: Format
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 30
21+
steps:
22+
- name: Checkout repository
23+
uses: actions/checkout@v4
24+
25+
- name: Install Rust toolchain
26+
uses: dtolnay/rust-toolchain@stable
27+
with:
28+
components: rustfmt
29+
30+
- name: Run cargo fmt
31+
run: cargo fmt --all -- --check
32+
setup-slang:
33+
strategy:
34+
matrix:
35+
os: [ubuntu-latest]
36+
runs-on: ${{ matrix.os }}
37+
outputs:
38+
slang-dir: ${{ steps.setup.outputs.slang-dir }} # Pass SLANG_DIR to dependent jobs
39+
slang-cache-key: ${{ steps.setup.outputs.slang-cache-key }} # Pass SLANG_DIR to dependent jobs
40+
steps:
41+
- name: Checkout code
42+
uses: actions/checkout@v4
43+
44+
- name: Cache Slang
45+
id: cache-slang
46+
uses: actions/cache/restore@v4 # Restore first
47+
with:
48+
path: |
49+
~/.cache/slang # Matches script's default OUTPUT_DIR
50+
key: slang-v$SLANG_TAG-${{ runner.os }}-${{ runner.arch }}
51+
52+
- name: Setup Slang
53+
id: setup
54+
run: |
55+
echo "version=$SLANG_TAG" >> $GITHUB_OUTPUT # Output for cache key
56+
SLANG_DIR=$(./.github/workflows/download_slang.sh --version $SLANG_TAG | grep '^SLANG_DIR=' | cut -d'=' -f2-)
57+
echo "slang-dir=$SLANG_DIR" >> $GITHUB_OUTPUT # Output for dependents
58+
echo "slang-cache-key=slang-v$SLANG_TAG-${{ runner.os }}-${{ runner.arch }}" >> $GITHUB_OUTPUT
59+
echo "SLANG_DIR=$SLANG_DIR" >> $GITHUB_ENV # For this job if needed
60+
61+
- name: Save Slang Cache
62+
if: steps.cache-slang.outputs.cache-hit != 'true' # Only save on miss
63+
uses: actions/cache/save@v4
64+
with:
65+
path: ~/.cache/slang
66+
key: ${{ steps.setup.outputs.slang-cache-key }}
67+
# Run clippy lints.
68+
clippy:
69+
needs: setup-slang # Depends on setup-slang
70+
name: Clippy
71+
runs-on: ubuntu-latest
72+
env:
73+
SLANG_DIR: ${{ needs.setup-slang.outputs.slang-dir }}
74+
timeout-minutes: 30
75+
steps:
76+
- name: Checkout repository
77+
uses: actions/checkout@v4
78+
79+
- name: Install Rust toolchain
80+
uses: dtolnay/rust-toolchain@stable
81+
with:
82+
components: clippy
83+
84+
- name: Install dependencies
85+
run: sudo apt-get update; sudo apt-get install --no-install-recommends build-essential curl wget file libssl-dev
86+
87+
- name: Retrieve Cache for Slang
88+
uses: actions/cache/restore@v4
89+
with:
90+
path: ~/.cache/slang
91+
key: ${{ needs.setup-slang.outputs.slang-cache-key }}
92+
93+
- name: Populate target directory from cache
94+
uses: Leafwing-Studios/cargo-cache@v2
95+
with:
96+
sweep-cache: true
97+
98+
- name: Run clippy lints
99+
run: SLANG_DIR=$SLANG_DIR cargo clippy --locked --workspace --all-targets -- --deny warnings
100+
101+
# Check documentation.
102+
doc:
103+
needs: setup-slang # Depends on setup-slang
104+
name: Docs
105+
runs-on: ubuntu-latest
106+
timeout-minutes: 30
107+
env:
108+
SLANG_DIR: ${{ needs.setup-slang.outputs.slang-dir }}
109+
steps:
110+
- name: Checkout repository
111+
uses: actions/checkout@v4
112+
113+
- name: Install Rust toolchain
114+
uses: dtolnay/rust-toolchain@stable
115+
116+
- name: Install dependencies
117+
run: sudo apt-get update; sudo apt-get install --no-install-recommends build-essential curl wget file libssl-dev
118+
119+
- name: Retrieve Cache for Slang
120+
uses: actions/cache/restore@v4
121+
with:
122+
path: ~/.cache/slang
123+
key: ${{ needs.setup-slang.outputs.slang-cache-key }}
124+
125+
- name: Populate target directory from cache
126+
uses: Leafwing-Studios/cargo-cache@v2
127+
with:
128+
sweep-cache: true
129+
130+
- name: Check documentation
131+
run: SLANG_DIR=$SLANG_DIR cargo doc --locked --workspace --document-private-items --no-deps
132+
# Testing.
133+
test:
134+
needs: setup-slang # Depends on setup-slang
135+
name: Tests
136+
runs-on: ubuntu-latest
137+
timeout-minutes: 30
138+
env:
139+
SLANG_DIR: ${{ needs.setup-slang.outputs.slang-dir }}
140+
steps:
141+
- name: Checkout repository
142+
uses: actions/checkout@v4
143+
144+
- name: Install Rust toolchain
145+
uses: dtolnay/rust-toolchain@stable
146+
147+
- name: Install dependencies
148+
run: |
149+
sudo apt-get update
150+
sudo apt-get install --no-install-recommends -y \
151+
build-essential curl wget file libssl-dev \
152+
libegl1-mesa-dev libgl1-mesa-dri libxcb-xfixes0-dev mesa-vulkan-drivers
153+
154+
- name: Retrieve Cache for Slang
155+
uses: actions/cache/restore@v4
156+
with:
157+
path: ~/.cache/slang
158+
key: ${{ needs.setup-slang.outputs.slang-cache-key }}
159+
160+
- name: Populate target directory from cache
161+
uses: Leafwing-Studios/cargo-cache@v2
162+
with:
163+
sweep-cache: true
164+
- name: Run Cargo Tests
165+
run: |
166+
SLANG_DIR=$SLANG_DIR LIBGL_ALWAYS_SOFTWARE=1 cargo test --verbose
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/bin/bash
2+
3+
# Default values
4+
OS=""
5+
OUTPUT_DIR="$HOME/.cache/slang"
6+
SLANG_VERSION=""
7+
SLANG_TAG=""
8+
ASSET_SUFFIX=""
9+
SLANG_URL_BASE="https://github.com/shader-slang/slang/releases/download"
10+
11+
# Help message
12+
usage() {
13+
echo "Usage: $0 [--os <linux|macos|macos-arm64|windows>] [--output-dir <path>] [--version <version>]"
14+
echo " --os: Target OS (default: auto-detect from current platform)"
15+
echo " --output-dir: Directory to extract Slang (default: ~/.cache/slang)"
16+
echo " --version: Slang version (e.g., 2025.18.2, default: latest)"
17+
echo "Example: $0 --os linux --output-dir /tmp/slang"
18+
}
19+
20+
# Parse arguments
21+
while [[ "$#" -gt 0 ]]; do
22+
case $1 in
23+
--os) OS="$2"; shift ;;
24+
--output-dir) export OUTPUT_DIR="$2"; shift ;;
25+
--version) export SLANG_VERSION="$2"; shift ;;
26+
*) usage ; exit 1 ;;
27+
esac
28+
shift
29+
done
30+
31+
# Detect OS if not specified
32+
if [[ -z "$OS" ]]; then
33+
case "$(uname -s)" in
34+
Linux*) OS="linux" ;;
35+
Darwin*)
36+
if [[ "$(uname -m)" == "arm64" ]]; then
37+
OS="macos-aarch64"
38+
else
39+
OS="macos"
40+
fi
41+
;;
42+
CYGWIN*|MINGW*|MSYS*) OS="windows" ;;
43+
*) echo "Error: Unable to detect OS. Specify --os (linux, macos, macos-arm64, windows)"; exit 1 ;;
44+
esac
45+
fi
46+
47+
# Determine asset suffix based on OS
48+
case "$OS" in
49+
linux) ASSET_SUFFIX="linux-x86_64.zip" ;;
50+
macos) ASSET_SUFFIX="macos-x86_64.zip" ;;
51+
macos-aarch64) ASSET_SUFFIX="macos-aarch64.zip" ;;
52+
windows) ASSET_SUFFIX="windows-x86_64.zip" ;;
53+
*) echo "Error: Unsupported OS: $OS"; exit 1 ;;
54+
esac
55+
56+
# Get Slang version if not specified
57+
if [[ -z "$SLANG_VERSION" ]]; then
58+
export SLANG_TAG=$(curl -s https://api.github.com/repos/shader-slang/slang/releases/latest | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/')
59+
export SLANG_VERSION=$(echo "$SLANG_TAG" | sed 's/v//') # e.g., v2025.18.2 -> 2025.18.2
60+
else
61+
export SLANG_TAG="v$SLANG_VERSION"
62+
fi
63+
64+
if [[ -z "$SLANG_VERSION" ]]; then
65+
echo "Error: Could not determine Slang version"
66+
exit 1
67+
fi
68+
69+
# Set up paths
70+
SLANG_DIR="$OUTPUT_DIR/slang-v$SLANG_VERSION-$OS"
71+
ZIP_URL="$SLANG_URL_BASE/$SLANG_TAG/slang-$SLANG_VERSION-$ASSET_SUFFIX"
72+
TEMP_ZIP="/tmp/slang-$SLANG_VERSION.zip"
73+
74+
# Check if Slang is already extracted
75+
if [[ -d "$SLANG_DIR" ]] && [[ -f "$SLANG_DIR/bin/slangc" || -f "$SLANG_DIR/bin/slangc.exe" ]]; then
76+
echo "Using existing Slang at $SLANG_DIR"
77+
echo "SLANG_DIR=$SLANG_DIR"
78+
exit 0
79+
fi
80+
81+
# Download Slang release
82+
echo "Downloading Slang v$SLANG_VERSION for $OS from $ZIP_URL..."
83+
mkdir -p "$OUTPUT_DIR"
84+
curl -L -o "$TEMP_ZIP" "$ZIP_URL" || { echo "Error: Download failed for $ZIP_URL"; exit 1; }
85+
86+
# Extract based on OS
87+
echo "Extracting to $SLANG_DIR..."
88+
if [[ "$OS" == "windows" ]]; then
89+
# Windows: Assume 7z is available (or adjust for PowerShell/Expand-Archive)
90+
7z x "$TEMP_ZIP" -o"$SLANG_DIR" -y > /dev/null || { echo "Error: Extraction failed"; rm -f "$TEMP_ZIP"; exit 1; }
91+
else
92+
# Linux/macOS: Use unzip
93+
unzip -q "$TEMP_ZIP" -d "$SLANG_DIR" || { echo "Error: Extraction failed"; rm -f "$TEMP_ZIP"; exit 1; }
94+
fi
95+
96+
# Clean up
97+
rm -f "$TEMP_ZIP"
98+
99+
# Verify extraction
100+
if [[ ! -f "$SLANG_DIR/bin/slangc" && ! -f "$SLANG_DIR/bin/slangc.exe" ]]; then
101+
echo "Error: Extraction incomplete, slangc not found in $SLANG_DIR/bin"
102+
exit 1
103+
fi
104+
105+
echo "Slang v$SLANG_VERSION extracted to $SLANG_DIR"
106+
echo "SLANG_DIR=$SLANG_DIR"
107+
108+
# For use in calling script
109+
export SLANG_DIR

0 commit comments

Comments
 (0)