name: Build

on:
  push:
    branches: [master]
    tags: ['v*']
  pull_request:
    branches: [master]

jobs:
  build-linux:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@v4

      - name: Install Zig
        uses: mlugg/setup-zig@v2
        with:
          version: master

      - name: Install LLVM 18
        run: sudo apt-get update && sudo apt-get install -y llvm-18-dev gcc

      - name: Build
        run: zig build -Dllvm-prefix=/usr/lib/llvm-18

      - name: Test
        run: zig build test -Dllvm-prefix=/usr/lib/llvm-18 --summary all

      - name: Package
        run: tar czf sx-linux-x86_64.tar.gz -C zig-out/bin .

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: sx-linux-x86_64
          path: zig-out/bin/

      - name: Release
        if: startsWith(github.ref, 'refs/tags/v')
        uses: softprops/action-gh-release@v2
        with:
          files: sx-linux-x86_64.tar.gz

  build-windows:
    runs-on: windows-latest
    env:
      LLVM_PREFIX: C:\LLVM\llvm-18.1.8-windows-amd64-msvc17-msvcrt
    steps:
      - uses: actions/checkout@v4

      - name: Install Zig
        uses: mlugg/setup-zig@v2
        with:
          version: master

      - name: Download LLVM 18
        run: |
          Invoke-WebRequest -Uri "https://github.com/vovkos/llvm-package-windows/releases/download/llvm-18.1.8/llvm-18.1.8-windows-amd64-msvc17-msvcrt.7z" -OutFile "$env:TEMP\llvm.7z"
          7z x "$env:TEMP\llvm.7z" -oC:\LLVM

      - name: Setup MSVC
        uses: ilammy/msvc-dev-cmd@v1

      - name: Build
        shell: cmd
        run: zig build -Dstatic-llvm -Dllvm-prefix=%LLVM_PREFIX% -Dtarget=x86_64-windows-msvc

      - name: Test
        shell: cmd
        continue-on-error: true
        run: zig build test -Dstatic-llvm -Dllvm-prefix=%LLVM_PREFIX% -Dtarget=x86_64-windows-msvc --summary all

      - name: Package
        run: Compress-Archive -Path zig-out\bin\* -DestinationPath sx-windows-x86_64.zip

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: sx-windows-x86_64
          path: zig-out/bin/

      - name: Release
        if: startsWith(github.ref, 'refs/tags/v')
        uses: softprops/action-gh-release@v2
        with:
          files: sx-windows-x86_64.zip
