name: Create release artifacts
on:
  push:
    tags:
      - v[0-9]*

env:
  # Force colored output (see https://bixense.com/clicolors/ and https://force-color.org/)
  TERM: xterm-256color
  CLICOLOR: 1
  CLICOLOR_FORCE: 1

  CMAKE_COLOR_DIAGNOSTICS: ON # Tells CMake-generated build systems to have colored output.
  CMAKE_BUILD_TYPE: Release # `cmake -S` now implies `-DCMAKE_BUILD_TYPE=Release`.
  CMAKE_CONFIG_TYPE: Release # `cmake --build` now implies `--config Release`.

  # We instruct CMake to download and build third-party projects in the same place as in `testing.yml`
  # to prevent divergence between testing and release workflows.
  DEPS_ROOT_DIR: ~/_deps

# We use Ninja as the generator for CMake below because it provides synchronous output by default,
# and only shows full command lines when they fail.

jobs:
  windows:
    runs-on: windows-2022
    strategy:
      matrix:
        bits: [32, 64]
      fail-fast: false
    steps:
      - name: Checkout repo
        uses: actions/checkout@v7
      - name: Install deps
        run: |
          bash .github/scripts/install-deps.sh windows
      - name: Build Windows binaries
        shell: cmd
        run: |
          call .github\scripts\msvc-env.bat ${{ matrix.bits }}
          cmake -B build -G Ninja -DFETCHCONTENT_BASE_DIR="${{ env.DEPS_ROOT_DIR }}" -DCMAKE_C_FLAGS="/nologo"
          cmake --build build -- -k 0
      - name: Package binaries
        working-directory: build
        run: |
          cpack -DCPACK_PACKAGE_FILE_NAME=rgbds-win${{ matrix.bits }} -G ZIP --verbose
      - name: Upload Windows binaries
        uses: actions/upload-artifact@v7
        with:
          name: win${{ matrix.bits }}
          path: build/rgbds-win${{ matrix.bits }}.zip
          if-no-files-found: error

  macos:
    runs-on: macos-26
    steps:
      - name: Checkout repo
        uses: actions/checkout@v7
      - name: Install deps
        run: |
          ./.github/scripts/install-deps.sh macos lld
      - name: Build binaries
        run: |
          cmake -B build -G Ninja --preset macos-static -DFETCHCONTENT_BASE_DIR="${{ env.DEPS_ROOT_DIR }}"
          cmake --build build -- -k 0
          strip rgb{asm,link,fix,gfx}
        env:
          LDFLAGS: -fuse-ld=lld # cmake/macos-static.cmake comments explain why we use lld.
      - name: Create install script
        run: |
          make install.sh
      - name: Package binaries
        run: |
          zip --junk-paths rgbds-macos.zip rgb{asm,link,fix,gfx} man/* install.sh
      - name: Upload macOS binaries
        uses: actions/upload-artifact@v7
        with:
          name: macos
          path: rgbds-macos.zip
          if-no-files-found: error

  linux:
    runs-on: ubuntu-22.04 # Oldest supported, for best glibc compatibility.
    steps:
      - name: Checkout repo
        uses: actions/checkout@v7
      - name: Install deps
        run: |
          ./.github/scripts/install-deps.sh ubuntu-22.04
      - name: Build binaries
        run: |
          make -k -j "$(getconf _NPROCESSORS_ONLN)" WARNFLAGS="-Wall -Wextra -pedantic -static" PKG_CONFIG="pkg-config --static" Q=
          strip rgb{asm,link,fix,gfx}
      - name: Create install script
        run: |
          make install.sh Q=
      - name: Package binaries
        run: |
          tar caf rgbds-linux-x86_64.tar.xz --transform='s#.*/##' rgb{asm,link,fix,gfx} man/* install.sh
      - name: Upload Linux binaries
        uses: actions/upload-artifact@v7
        with:
          name: linux
          path: rgbds-linux-x86_64.tar.xz
          if-no-files-found: error

  release:
    runs-on: ubuntu-slim
    needs: [windows, macos, linux]
    permissions:
      contents: write
    steps:
      - name: Checkout repo
        uses: actions/checkout@v7
      - name: Package sources
        run: |
          make dist Q=
      - name: Download all binary packages
        uses: actions/download-artifact@v8
      - name: Draft the release
        run: |
          .github/scripts/draft-release.sh "${ref#refs/tags/}"
        env:
          ref: ${{ github.ref_name }}
          GH_TOKEN: ${{ github.token }} # Required by the `gh` command.
