Skip to content

Commit

Permalink
fix: optimized wasm build
Browse files Browse the repository at this point in the history
  • Loading branch information
0xAlcibiades committed Dec 27, 2024
1 parent a074742 commit c09d082
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 43 deletions.
99 changes: 59 additions & 40 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CI workflow for Rusty Pong
# Handles testing, code quality checks, and web deployment
name: CI

# Trigger on pushes to master branch, version tags, and PRs
on:
push:
branches:
Expand All @@ -10,11 +13,45 @@ on:
types: [ opened, synchronize, reopened ]
branches:
- master

env:
CARGO_TERM_COLOR: always

jobs:
# Check code formatting - fastest check
fmt:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libasound2-dev libudev-dev
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt
- run: cargo fmt --all --check

# Run clippy lints for code quality - second fastest check
clippy:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libasound2-dev libudev-dev
- uses: dtolnay/rust-toolchain@clippy
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- run: cargo clippy --workspace --all-targets --all-features

# Run tests across multiple Rust toolchains and feature combinations
test:
needs: [clippy, fmt]
name: test ${{ matrix.rust }} ${{ matrix.flags }}
runs-on: ubuntu-latest
timeout-minutes: 30
Expand All @@ -24,8 +61,7 @@ jobs:
rust: [ "stable", "beta", "nightly", "1.82" ] # MSRV
flags: [ "--no-default-features", "", "--all-features" ]
exclude:
# Skip because some features have highest MSRV.
- rust: "1.82" # MSRV
- rust: "1.82"
flags: "--all-features"
steps:
- uses: actions/checkout@v4
Expand All @@ -39,15 +75,16 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
# Only run tests on the latest stable and above
- name: check
if: ${{ matrix.rust == '1.82' }} # MSRV
if: ${{ matrix.rust == '1.82' }}
run: cargo check --workspace ${{ matrix.flags }}
- name: test
if: ${{ matrix.rust != '1.82' }} # MSRV
if: ${{ matrix.rust != '1.82' }}
run: cargo test --workspace ${{ matrix.flags }}

# Generate and upload code coverage information
coverage:
needs: [test]
name: Code Coverage
runs-on: ubuntu-latest
env:
Expand Down Expand Up @@ -77,7 +114,9 @@ jobs:
files: lcov.info
fail_ci_if_error: false

# Check all possible feature combinations work
feature-checks:
needs: [test]
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
Expand All @@ -94,22 +133,9 @@ jobs:
- name: cargo hack
run: cargo hack check --feature-powerset --depth 2

clippy:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libasound2-dev libudev-dev
- uses: dtolnay/rust-toolchain@clippy
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- run: cargo clippy --workspace --all-targets --all-features

# Generate and test documentation
docs:
needs: [test]
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
Expand All @@ -126,23 +152,10 @@ jobs:
env:
RUSTDOCFLAGS: "--cfg docsrs"

fmt:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@v4
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libasound2-dev libudev-dev
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt
- run: cargo fmt --all --check

# Build WASM bundle and deploy to GitHub Pages
build-and-deploy:
name: Build and Deploy
needs: [ test, coverage, feature-checks, clippy, docs, fmt ]
needs: [ test, coverage, feature-checks, docs ]
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
permissions:
Expand All @@ -153,19 +166,25 @@ jobs:
- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y pkg-config libasound2-dev libudev-dev
sudo apt-get install -y pkg-config libasound2-dev libudev-dev binaryen
- name: Install wasm-pack
uses: jetli/[email protected]
- uses: dtolnay/rust-toolchain@stable
with:
targets: wasm32-unknown-unknown
- name: Install Rust dependencies
run: |
rustup target add wasm32-unknown-unknown
cargo install wasm-bindgen-cli
- uses: Swatinem/rust-cache@v2
with:
cache-on-failure: true
- name: Build wasm
- name: Build and optimize wasm
run: |
cargo build --release --target wasm32-unknown-unknown
mkdir -p dist
wasm-bindgen --out-dir ./dist --target web target/wasm32-unknown-unknown/release/rusty_pong.wasm
find ./dist -name "*.wasm" -exec wasm-opt -O3 -o {}.opt {} \; -exec mv {}.opt {} \;
- name: Prepare deployment
run: |
mkdir -p dist/assets
Expand All @@ -174,6 +193,6 @@ jobs:
- name: Deploy 🚀
uses: JamesIves/github-pages-deploy-action@v4
with:
folder: dist # The folder the action should deploy
branch: gh-pages # The branch the action should deploy to
clean: true # Automatically remove deleted files from deploy branch
folder: dist
branch: gh-pages
clean: true
15 changes: 12 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ edition = "2021"
authors = ["0xAlcibiades <[email protected]>"]

[dependencies]
bevy = { version = "0.15.0" , features = ["mp3", "webgpu"]}
bevy = { version = "0.15.0", features = ["mp3", "webgpu"] }
bevy_rapier2d = "0.28.0"
rand = "0.8.5"

[profile.dev]
opt-level = 1
# Enable max optimizations for all dependencies in release builds
[profile.release]
opt-level = 3 # Maximum optimization
lto = true # Enable link-time optimization
panic = 'abort' # Abort on panic for smaller binary size
strip = true # Strip symbols from binary
overflow-checks = false # Disable integer overflow checks

# Optimize all dependencies even in debug builds for faster runs
[profile.dev.package."*"]
opt-level = 3

[profile.dev]
opt-level = 1 # Minimum optimization for primary package
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ This project was created as a learning exercise to understand:
- Game state management
- Audio handling in games
- AI behavior implementation
- The state of WASM game development with Rust

## License

Expand Down

0 comments on commit c09d082

Please sign in to comment.