name: Build executables

"on":
  push:
    branches:
      - main
    tags:
      - "v*"
  pull_request:
  workflow_dispatch:

permissions:
  contents: read

env:
  BIN_NAME: refray
  CARGO_TERM_COLOR: always

jobs:
  windows-x86_64:
    name: Windows x86_64
    runs-on: windows-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-pc-windows-msvc

      - name: Cache Rust builds
        uses: Swatinem/rust-cache@v2
        with:
          key: windows-x86_64

      - name: Build
        run: cargo build --release --locked --target x86_64-pc-windows-msvc

      - name: Package
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force dist | Out-Null
          Copy-Item target/x86_64-pc-windows-msvc/release/${env:BIN_NAME}.exe dist/${env:BIN_NAME}.exe
          Compress-Archive -Path dist/${env:BIN_NAME}.exe -DestinationPath ${env:BIN_NAME}-windows-x86_64.zip -Force

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BIN_NAME }}-windows-x86_64
          path: ${{ env.BIN_NAME }}-windows-x86_64.zip
          if-no-files-found: error

  macos-universal:
    name: macOS universal
    runs-on: macos-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: x86_64-apple-darwin,aarch64-apple-darwin

      - name: Cache Rust builds
        uses: Swatinem/rust-cache@v2
        with:
          key: macos-universal

      - name: Build x86_64
        run: cargo build --release --locked --target x86_64-apple-darwin

      - name: Build arm64
        run: cargo build --release --locked --target aarch64-apple-darwin

      - name: Create universal binary
        run: |
          mkdir -p dist
          lipo -create \
            target/x86_64-apple-darwin/release/${BIN_NAME} \
            target/aarch64-apple-darwin/release/${BIN_NAME} \
            -output dist/${BIN_NAME}
          chmod +x dist/${BIN_NAME}
          lipo -info dist/${BIN_NAME}

      - name: Package
        run: tar -C dist -czf ${BIN_NAME}-macos-universal.tar.gz ${BIN_NAME}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BIN_NAME }}-macos-universal
          path: ${{ env.BIN_NAME }}-macos-universal.tar.gz
          if-no-files-found: error

  linux-musl:
    name: Linux ${{ matrix.arch }} musl static
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86_64
            target: x86_64-unknown-linux-musl
          - arch: arm64
            target: aarch64-unknown-linux-musl
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

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

      - name: Install cargo-zigbuild
        uses: taiki-e/install-action@v2
        with:
          tool: cargo-zigbuild

      - name: Cache Rust builds
        uses: Swatinem/rust-cache@v2
        with:
          key: linux-${{ matrix.target }}

      - name: Build static musl binary
        run: cargo zigbuild --release --locked --target ${{ matrix.target }}

      - name: Verify static binary
        run: |
          file target/${{ matrix.target }}/release/${BIN_NAME} | tee binary-info.txt
          grep -Eiq 'static|statically' binary-info.txt

      - name: Package
        run: |
          mkdir -p dist
          cp target/${{ matrix.target }}/release/${BIN_NAME} dist/${BIN_NAME}
          chmod +x dist/${BIN_NAME}
          tar -C dist -czf ${BIN_NAME}-linux-${{ matrix.arch }}-musl.tar.gz ${BIN_NAME}

      - name: Upload artifact
        uses: actions/upload-artifact@v4
        with:
          name: ${{ env.BIN_NAME }}-linux-${{ matrix.arch }}-musl
          path: ${{ env.BIN_NAME }}-linux-${{ matrix.arch }}-musl.tar.gz
          if-no-files-found: error
