name: Checks

env:
  CARGO_INCREMENTAL: 0
  CARGO_TERM_COLOR: "always"

on:
  push:
    branches: ["master"]
  pull_request:

jobs:
  clippy-version-features:
    name: Clippy - ${{ matrix.build_mode }} - feature ${{ matrix.feature }}
    runs-on: ubuntu-latest
    
    strategy:
      fail-fast: false
      matrix:
        build_mode:
          - build-from-source
          - build-from-source-static
        feature:
          - ash
          - unsafe_textures
          - ttf
          - hidapi
          - raw-window-handle
          - image
          - mixer

    steps:
      - name: checkout sources
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Install Linux dependencies
        uses: ./.github/actions/install-linux-deps

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master
        with:
          toolchain: stable
          components: clippy

      - name: Fetch dependencies
        run: cargo +stable fetch --locked

      # TODO: deny all warnings
      - name: Clippy
        run: |
          cargo +stable \
            clippy \
            --verbose \
            --locked \
            --offline \
            --all-targets \
            --features "${{ matrix.build_mode }} ${{ matrix.feature }}" \
            -- \
            -A warnings \
            -W clippy::not_unsafe_ptr_arg_deref

  build-version-features:
    name: Build - ${{ matrix.os }} - ${{ matrix.build_mode }} - feature ${{ matrix.feature }}
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: false
      matrix:
        os: [macos-latest, ubuntu-latest, windows-latest]
        build_mode:
          - build-from-source
          - build-from-source-static
        feature:
          - ash
          - unsafe_textures
          - ttf
          - hidapi
          - raw-window-handle
          - image
          - mixer
        exclude:
          # harfbuzz has CRT linking issues on Windows static builds
          # https://github.com/libsdl-org/SDL_ttf/issues/289
          - os: windows-latest
            build_mode: build-from-source-static
            feature: ttf

    steps:
      - name: checkout sources
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Install Linux dependencies
        uses: ./.github/actions/install-linux-deps

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master
        with:
          toolchain: stable

      - name: Fetch dependencies
        run: cargo +stable fetch --locked

      - name: Build
        shell: bash
        run: |
          cargo +stable \
            build \
            --verbose \
            --locked \
            --offline \
            --examples \
            --all-targets \
            --features "${{ matrix.build_mode }} ${{ matrix.feature }}"

  tests:
    name: Tests - ${{ matrix.name }}
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        include:
          - name: default (build-from-source)
            build_mode: build-from-source
            features: ""
          - name: default (build-from-source-static)
            build_mode: build-from-source-static
            features: ""
          - name: image (build-from-source)
            build_mode: build-from-source
            features: "image"
          - name: image (build-from-source-static)
            build_mode: build-from-source-static
            features: "image"
          - name: mixer (build-from-source)
            build_mode: build-from-source
            features: "mixer"
          - name: mixer (build-from-source-static)
            build_mode: build-from-source-static
            features: "mixer"

    steps:
      - name: checkout sources
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Install Linux dependencies
        uses: ./.github/actions/install-linux-deps

      - name: Install Xvfb
        if: runner.os == 'Linux'
        run: |
          sudo apt-get update
          sudo apt-get install --no-install-recommends -y xvfb
      - name: Install toolchain
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master
        with:
          toolchain: stable

      - name: Fetch dependencies
        run: cargo +stable fetch --locked

      - name: Test
        env:
          SDL_VIDEODRIVER: x11
          SDL_AUDIODRIVER: dummy
        run: |
          FEATURES="$(echo "${{ matrix.build_mode }} ${{ matrix.features }}" | xargs)"
          xvfb-run --auto-servernum --server-args="-screen 0 1280x720x24" \
            cargo +stable \
              test \
              --verbose \
              --locked \
              --offline \
              --lib \
              --tests \
              --examples \
              --features "$FEATURES"

  fmt:
    name: Formatting
    runs-on: ubuntu-latest

    steps:
      - name: Checkout sources
        uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6

      - name: Install toolchain
        uses: dtolnay/rust-toolchain@3c5f7ea28cd621ae0bf5283f0e981fb97b8a7af9 # master
        with:
          toolchain: stable
          components: rustfmt

      - name: Check formatting
        run: cargo fmt --all -- --check
