name: CI

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  macOS:
    strategy:
      fail-fast: false
      matrix:
        target:
          - macos-aarch64-dyn
          - macos-x86_64-dyn
          - ios-arm64-dyn
          - ios-x86_64-dyn
        include:
          - target: macos-aarch64-dyn
            arch_name: arm64-apple-macos
            run_test: true
          - target: macos-x86_64-dyn
            arch_name: x86_64-apple-darwin
            run_test: true
          - target: ios-arm64-dyn
            arch_name: aarch64-apple-ios
            run_test: false
          - target: ios-x86_64-dyn
            arch_name: x86-apple-ios-simulator
            run_test: false
    runs-on: macos-15

    env:
      HOME: /Users/runner
    steps:
      - name: Retrieve source code
        uses: actions/checkout@v4

      - name: Install packages
        run: |
          brew update
          brew install ninja meson

      - name: Install dependencies
        uses: kiwix/kiwix-build/actions/dl_deps_archive@main
        with:
          target_platform: ${{ matrix.target }}

      - name: Compile
        env:
          PKG_CONFIG_PATH: ${{env.HOME}}/BUILD_${{matrix.arch_name}}/INSTALL/lib/pkgconfig
          CPPFLAGS: -I${{env.HOME}}/BUILD_${{matrix.arch_name}}/INSTALL/include
          MESON_OPTION: --default-library=shared -Db_coverage=true
          MESON_CROSSFILE: ${{env.HOME}}/BUILD_${{matrix.arch_name}}/meson_cross_file.txt
        shell: bash
        run: |
          if [ -e $MESON_CROSSFILE ]; then
            MESON_OPTION="$MESON_OPTION --cross-file $MESON_CROSSFILE -Dstatic-linkage=true"
          fi
          meson . build ${MESON_OPTION}
          ninja -C build

      - name: Test libkiwix
        if: matrix.run_test
        env:
          SKIP_BIG_MEMORY_TEST: 1
          LD_LIBRARY_PATH: ${{env.HOME}}/BUILD_${{matrix.arch_name}}/INSTALL/lib:${{env.HOME}}/BUILD_${{matrix.arch_name}}/INSTALL/lib64
        run: meson test -C build --verbose

  Windows:
    runs-on: windows-2022

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup python 3.10
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'

      - name: Install packages
        run:
          choco install pkgconfiglite ninja

      - name: Install python modules
        run: pip3 install meson

      - name: Setup MSVC compiler
        uses: bus1/cabuild/action/msdevshell@v1
        with:
          architecture: x64

      - name: Install dependencies
        uses: kiwix/kiwix-build/actions/dl_deps_archive@main
        with:
          target_platform: win-x86_64-static

      - name: Compile
        shell: cmd
        run: |
          set PKG_CONFIG_PATH=%cd%\BUILD_win-amd64\INSTALL\lib\pkgconfig
          set CPPFLAGS=-I%cd%\BUILD_win-amd64\INSTALL\include
          meson.exe setup . build -Dwerror=false --default-library=static --buildtype=release
          cd build
          ninja.exe

      - name: Test
        shell: cmd
        run: |
          cd build
          meson.exe test --verbose
        env:
          WAIT_TIME_FACTOR_TEST: 10

  Linux:
    strategy:
      fail-fast: false
      matrix:
        target:
          - linux-x86_64-static
          - linux-x86_64-dyn
          - android-arm
          - android-arm64
        image_variant: ['jammy']
        include:
          - target: linux-x86_64-static
            lib_postfix: '/x86_64-linux-gnu'
            arch_name: linux-x86_64
            run_test: true
            coverage: true
          - target: linux-x86_64-dyn
            lib_postfix: '/x86_64-linux-gnu'
            arch_name: linux-x86_64
            run_test: true
            coverage: true
          - target: android-arm
            lib_postfix: '/arm-linux-androideabi'
            arch_name: arm-linux-androideabi
            run_test: false
            coverage: false
          - target: android-arm64
            lib_postfix: '/aarch64-linux-android'
            arch_name: aarch64-linux-android
            run_test: false
            coverage: false
    env:
      HOME: /home/runner
    runs-on: ubuntu-22.04
    container:
      image: "ghcr.io/kiwix/kiwix-build_ci_${{matrix.image_variant}}:2025-06-07"
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
    - name: Install dependencies
      uses: kiwix/kiwix-build/actions/dl_deps_archive@main
      with:
        target_platform: ${{ matrix.target }}
    - name: Compile
      shell: bash
      run: |
        meson --version
        if [[ "${{matrix.target}}" =~ .*-dyn ]]; then
          MESON_OPTION="--default-library=shared"
        else
          MESON_OPTION="--default-library=static"
        fi
        if [ -e "${{env.HOME}}/BUILD_${{matrix.arch_name}}/meson_cross_file.txt" ]; then
          MESON_OPTION="$MESON_OPTION --cross-file ${{env.HOME}}/BUILD_${{matrix.arch_name}}/meson_cross_file.txt"
        else
          MESON_OPTION="$MESON_OPTION -Db_coverage=true"
        fi
        if [[ "${{matrix.target}}" =~ android-.* ]]; then
          MESON_OPTION="$MESON_OPTION -Dstatic-linkage=true"
        fi
        meson . build ${MESON_OPTION}
        cd build
        ninja
      env:
        PKG_CONFIG_PATH: "/home/runner/BUILD_${{matrix.arch_name}}/INSTALL/lib/pkgconfig:/home/runner/BUILD_${{matrix.arch_name}}/INSTALL/lib${{matrix.lib_postfix}}/pkgconfig"
        CPPFLAGS: "-I/home/runner/BUILD_${{matrix.arch_name}}/INSTALL/include"
    - name: Test
      if: matrix.run_test
      shell: bash
      run: |
        cd build
        meson test --verbose
        if [[ "${{matrix.coverage}}" = "true" ]]; then
          ninja coverage
        fi
      env:
        LD_LIBRARY_PATH: "/home/runner/BUILD_${{matrix.arch_name}}/INSTALL/lib:/home/runner/BUILD_${{matrix.arch_name}}/INSTALL/lib${{matrix.lib_postfix}}"
        SKIP_BIG_MEMORY_TEST: 1

    - name: Publish coverage
      if: matrix.coverage
      uses: codecov/codecov-action@v3
      with:
        token: ${{ secrets.CODECOV_TOKEN }}
