cemc 0.1.2

Cem language compiler - A concatenative language with green threads and linear types
Documentation
name: CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1

jobs:
  # Rust linting with Clippy
  rust-lint:
    name: Rust Lint (Clippy)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      
      - name: Cache Cargo dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: ${{ runner.os }}-cargo-
      
      - name: Run Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings

  # Rust formatting check
  rust-format:
    name: Rust Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      
      - name: Check formatting
        run: cargo fmt --all -- --check

  # C/C++ linting
  c-lint:
    name: C Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install clang-format and clang-tidy
        run: |
          sudo apt-get update
          sudo apt-get install -y clang-format clang-tidy
      
      - name: Check C formatting
        run: |
          find runtime -name "*.c" -o -name "*.h" | while read file; do
            clang-format --dry-run --Werror "$file" || {
              echo "::error file=$file::C formatting check failed. Run: clang-format -i $file"
              exit 1
            }
          done
      
      - name: Run clang-tidy on runtime
        run: |
          # Run clang-tidy with basic checks
          # We use a lenient config initially to avoid noise
          find runtime -name "*.c" | while read file; do
            clang-tidy "$file" -- -std=c11 -Iruntime || {
              echo "::warning file=$file::clang-tidy found issues (non-blocking)"
            }
          done

  # Test on x86-64 Linux (Ubuntu)
  test-linux-x86:
    name: Test Linux x86-64
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y clang llvm just
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
      
      - name: Cache Cargo dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: linux-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: linux-cargo-
      
      - name: Build runtime library
        run: just build-runtime
      
      - name: Verify epoll symbols
        run: |
          nm runtime/libcem_runtime.a | grep epoll
          echo "✓ epoll symbols found"
      
      - name: Run Rust tests
        run: cargo test --verbose
      
      - name: Run C context switching tests
        run: just test-context
      
      - name: Check build artifacts
        run: |
          ls -lh runtime/libcem_runtime.a
          file runtime/libcem_runtime.a

  # Test on ARM64 macOS (Apple Silicon)
  test-macos-arm64:
    name: Test macOS ARM64
    runs-on: macos-latest  # This is Apple Silicon (M1)
    steps:
      - uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          brew install llvm just
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
      
      - name: Cache Cargo dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/bin/
            ~/.cargo/registry/index/
            ~/.cargo/registry/cache/
            ~/.cargo/git/db/
            target/
          key: macos-arm-cargo-${{ hashFiles('**/Cargo.lock') }}
          restore-keys: macos-arm-cargo-
      
      - name: Build runtime library
        run: just build-runtime
      
      - name: Verify kqueue symbols
        run: |
          nm runtime/libcem_runtime.a | grep kqueue || echo "kqueue is system call"
          echo "✓ Runtime built for macOS"
      
      - name: Run Rust tests
        run: cargo test --verbose
      
      - name: Run C context switching tests
        run: just test-context
      
      - name: Check build artifacts
        run: |
          ls -lh runtime/libcem_runtime.a
          file runtime/libcem_runtime.a

  # Optional: Test on x86-64 macOS (Intel)
  # Uncomment when you add x86-64 macOS context switching support
  # test-macos-x86:
  #   name: Test macOS x86-64
  #   runs-on: macos-13  # Intel-based runner
  #   steps:
  #     - uses: actions/checkout@v4
  #     # ... similar to ARM64 but with context_x86_64.s

  # Build check - ensure everything compiles together
  build-check:
    name: Build Check
    runs-on: ubuntu-latest
    needs: [rust-lint, rust-format, c-lint]
    steps:
      - uses: actions/checkout@v4
      
      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y clang llvm just
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@stable
      
      - name: Build everything
        run: |
          cargo build --verbose
          just build-runtime
          echo "✓ Full build successful"
      
      - name: Check for warnings
        run: cargo build --verbose 2>&1 | tee build.log
      
      - name: Upload build log
        if: always()
        uses: actions/upload-artifact@v4
        with:
          name: build-log
          path: build.log

  # Summary job - only runs if all tests pass
  all-tests-passed:
    name: All Tests Passed
    runs-on: ubuntu-latest
    needs: [rust-lint, rust-format, c-lint, test-linux-x86, test-macos-arm64, build-check]
    steps:
      - name: Success
        run: |
          echo "✅ All CI checks passed!"
          echo "- Rust linting (Clippy)"
          echo "- Rust formatting"
          echo "- C linting"
          echo "- Linux x86-64 tests"
          echo "- macOS ARM64 tests"
          echo "- Build check"