name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
  workflow_dispatch:

jobs:
  clang-format:
    name: Check clang-format
    runs-on: ubuntu-latest
    container: archlinux:latest
    steps:
      - name: Install dependencies
        run: |
          pacman -Syu --noconfirm git clang diffutils
          git config --global --add safe.directory $GITHUB_WORKSPACE
      - uses: actions/checkout@v4
      - uses: fcitx/github-actions@clang-format
  check:
    name: Build and test
    needs: clang-format
    runs-on: ubuntu-latest
    container: archlinux:latest
    strategy:
      fail-fast: false
      matrix:
        compiler: [gcc, clang]
        include:
          - compiler: gcc
            cxx_compiler: g++
          - compiler: clang
            cxx_compiler: clang++
    env:
      CC: ${{ matrix.compiler }}
      CXX: ${{ matrix.cxx_compiler }}
    steps:
      - name: Install dependencies
        run: |
          pacman -Syu --noconfirm base-devel clang cmake ninja extra-cmake-modules xcb-util xcb-util-keysyms cairo enchant iso-codes libxkbcommon-x11 pango systemd wayland xcb-util-wm libxkbfile nlohmann-json gdk-pixbuf2 wayland-protocols git plasma-wayland-protocols
      - uses: actions/checkout@v4
        with:
          path: xcb-imdkit
          repository: fcitx/xcb-imdkit
      - name: Build and Install xcb-imdkit
        uses: fcitx/github-actions@cmake
        with:
          path: xcb-imdkit
      - uses: actions/checkout@v4
        with:
          path: fcitx5
          submodules: true
      - name: Cache fcitx5 data files
        uses: actions/cache@v4
        with:
          path: 'fcitx5/**/*.tar.*'
          key: ${{ runner.os }}-${{ hashFiles('fcitx5/src/modules/spell/CMakeLists.txt') }}
      - name: Init CodeQL
        uses: github/codeql-action/init@v3
        with:
          languages: cpp
          source-root: fcitx5
      - name: Build and Install fcitx5
        uses: fcitx/github-actions@cmake
        with:
          path: fcitx5
      - name: Test
        run: |
          ctest --output-on-failure --test-dir fcitx5/build
      - name: CodeQL Analysis
        uses: github/codeql-action/analyze@v2

  check-windows:
    name: Build on Windows
    needs: clang-format
    runs-on: windows-2025
    strategy:
      fail-fast: false

    steps:
      - uses: actions/checkout@v4
        with:
          path: fcitx5
          submodules: true

      - name: Install dependencies
        run: |
          C:/msys64/usr/bin/pacman -Syu --noconfirm
          C:/msys64/usr/bin/pacman -S --noconfirm `
            mingw-w64-clang-x86_64-extra-cmake-modules `
            mingw-w64-clang-x86_64-dlfcn `
            mingw-w64-clang-x86_64-libuv `
            mingw-w64-clang-x86_64-clang `
            mingw-w64-clang-x86_64-cmake `
            mingw-w64-clang-x86_64-ninja `
            mingw-w64-clang-x86_64-pkgconf `
            mingw-w64-clang-x86_64-gettext-tools
          Add-Content $env:GITHUB_PATH "C:/msys64/clang64/bin"

      - name: Cache fcitx5 data files
        uses: actions/cache@v4
        with:
          path: 'fcitx5/**/*.tar.*'
          key: ${{ runner.os }}-${{ hashFiles('fcitx5/src/modules/spell/CMakeLists.txt') }}

      - name: Build
        uses: fcitx/github-actions@cmake
        with:
          path: fcitx5
          cmake-option: >-
            -DENABLE_DBUS=Off -DENABLE_X11=Off -DENABLE_WAYLAND=Off -DENABLE_ENCHANT=Off -DENABLE_SERVER=Off -DENABLE_XDGAUTOSTART=Off -DENABLE_LIBUUID=Off -DENABLE_KEYBOARD=Off -DCMAKE_CXX_FLAGS=-fexperimental-library
          shell: pwsh

      - name: Test
        run: |
          ctest --output-on-failure --test-dir fcitx5/build
