name: CI
on: [push, pull_request]
env:
    CI: 'true'
    ASAN_OPTIONS: detect_leaks=0
    LC_ALL: en_US.UTF-8
    LANG: en_US.UTF-8

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
    linux:
        name: Linux (python=${{ matrix.pyver }} cc=${{ matrix.cc }} sanitize=${{ matrix.sanitize }})
        runs-on: ubuntu-latest
        env:
            CC: ${{ matrix.cc }}
            KITTY_SANITIZE: ${{ matrix.sanitize }}

        strategy:
            matrix:
                python: [a, b, c]
                cc: [gcc, clang]
                include:
                    - python: a
                      pyver: "3.12"
                      sanitize: 0

                    - python: b
                      pyver: "3.13"
                      sanitize: 1

                    - python: c
                      pyver: "3.14"
                      sanitize: 1


                exclude:
                    - python: a
                      cc: clang
                    - python: b
                      cc: clang
                    - python: c
                      cc: gcc

        steps:
          - name: Checkout source code
            uses: actions/checkout@v7.0.0
            with:
              fetch-depth: 10
              persist-credentials: false

          - name: Set up Python ${{ matrix.pyver }}
            uses: actions/setup-python@v6.3.0
            with:
              python-version: ${{ matrix.pyver }}

          - name: Install Go
            uses: actions/setup-go@v6.5.0
            with:
              go-version-file: go.mod

          - name: Build kitty
            run: python .github/workflows/ci.py build

          - name: Test kitty
            run: python .github/workflows/ci.py test

    linux-package:
        name: Linux package
        runs-on: ubuntu-latest
        env:
            CFLAGS: -funsigned-char
        steps:
          - name: Checkout source code
            uses: actions/checkout@v7.0.0
            with:
              fetch-depth: 0  # needed for :commit: docs role
              persist-credentials: false

          - name: Test for trailing whitespace
            run: if grep -Inr '\s$' kitty kitty_tests kittens docs *.py *.asciidoc *.rst *.go .gitattributes .gitignore; then echo Trailing whitespace found, aborting.; exit 1; fi

          - name: Test for bad code block formatting
            run: if grep -Inr ':code:`\s' kitty kitty_tests kittens docs *.py *.asciidoc *.rst *.go .gitattributes .gitignore; then echo Space at code block start found, aborting.; exit 1; fi

          - name: Set up Python
            uses: actions/setup-python@v6.3.0
            with:
              python-version: "3.14"

          - name: Install Go
            uses: actions/setup-go@v6.5.0
            with:
              go-version-file: go.mod
              cache: false

          - name: Cache Go build artifacts separately
            uses: actions/cache@v6.1.0
            with:
              path: |
                ~/.cache/go-build
                ~/go/pkg/mod
              key: ${{ runner.os }}-golang-static-${{ hashFiles('**/go.sum') }}
              restore-keys: |
                ${{ runner.os }}-golang-static-

          - name: Install build-only deps
            run: python -m pip install -r docs/requirements.txt ruff ty types-requests types-docutils types-Pygments

          - name: Run ruff
            run: ruff check .

          - name: Run gofmt
            run: go version && python .github/workflows/ci.py gofmt

          - name: Build kitty package
            run: python .github/workflows/ci.py package

          - name: Build kitty
            run: python setup.py build --debug

          - name: Run type checking
            run: which python && ./test.py type-check

          - name: Run go vet
            run: go version && go vet -tags testing ./...

          - name: Build docs
            run: make FAIL_WARN=1 docs

          - name: Build static kittens
            run: python setup.py build-static-binaries

    bundle:
        name: Bundle test (${{ matrix.os }})
        runs-on: ${{ matrix.os }}
        strategy:
            matrix:
                os: [ubuntu-latest, macos-latest]
        env:
            KITTY_BUNDLE: 1
            KITTY_SANITIZE: 1
        steps:
          - name: Checkout source code
            uses: actions/checkout@v7.0.0
            with:
              fetch-depth: 10
              persist-credentials: false

          - name: Install Go
            uses: actions/setup-go@v6.5.0
            with:
              go-version-file: go.mod

          - name: Build kitty
            run: which python3 && python3 .github/workflows/ci.py build

          - name: Test kitty
            run: python3 .github/workflows/ci.py test

    brew:
        name: macOS Brew
        runs-on: macos-latest
        steps:
          - name: Checkout source code
            uses: actions/checkout@v7.0.0
            with:
              fetch-depth: 0  # needed for :commit: docs role
              persist-credentials: false

          - name: Set up Python
            uses: actions/setup-python@v6.3.0
            with:
              python-version: "3.14"

          - name: Install Go
            uses: actions/setup-go@v6.5.0
            with:
              go-version-file: go.mod

          - name: Build kitty
            run: python3 .github/workflows/ci.py build

          - name: Test kitty
            run: python3 .github/workflows/ci.py test

          - name: Install deps for docs
            run: python3 -m pip install -r docs/requirements.txt

          - name: Builds docs
            run: make FAIL_WARN=1 docs

          - name: Build kitty package
            run: python3 .github/workflows/ci.py package

          - name: Run benchmarks
            run: ./benchmark.py

    linux-dev:
        name: Test ./dev.sh and benchmark
        runs-on: ubuntu-latest
        steps:
          - name: Checkout source code
            uses: actions/checkout@v7.0.0
            with:
              fetch-depth: 10
              persist-credentials: false

          - name: Install build deps
            run: sudo apt-get update && sudo apt-get install -y curl xz-utils build-essential git pkg-config libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libgl1-mesa-dev libxkbcommon-x11-dev libfontconfig-dev libx11-xcb-dev libdbus-1-dev

          - name: Install Go
            uses: actions/setup-go@v6.5.0
            with:
              go-version-file: go.mod

          - name: Build kitty
            run: ./dev.sh build

          - name: Run benchmarks
            run: ./benchmark.py
