name: meson

on:
  push:
    paths:
      - src/**
      - meson.build
  pull_request:
    paths:
      - src/**
      - meson.build
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: ubuntu-latest
    container:
      image: 'registry.opensuse.org/opensuse/tumbleweed'
      env:
        CC: ${{ matrix.cc }}
        CXX: ${{ matrix.cxx }}
        CC_LD: ${{ matrix.ld }}
        CXX_LD: ${{ matrix.ld }}

    strategy:
      fail-fast: false
      matrix:
        compiler: [clang, gcc, mingw-w64-gcc]
        include:
          - compiler: clang
            cc: clang
            cxx: clang++
            ld: lld
            pkg:
              - clang
              - lld
              - libstdc++-devel
            args:
              - -Db_lto_mode=thin
              - -Db_lundef=false
              - -Db_sanitize=address,undefined
          - compiler: gcc
            cc: gcc
            cxx: g++
            pkg:
              - gcc-c++
            args:
              - -Db_sanitize=address,undefined
          - compiler: mingw-w64-gcc
            pkg:
              - mingw64-cross-gcc-c++
              - mingw64-cross-pkgconf
              - wine
            args:
              - --cross-file=cross.ini
              - --pkgconfig.relocatable
              - -Db_lto=false
              - --prefix
              - $(pwd)/artifact
              - --licensedir
              - .
    steps:
      - name: '🛒 Install dependencies'
        run: >
          zypper -n in --no-recommends
          meson
          git-core
          'pkgconfig(lept)'
          ${{ join(matrix.pkg, ' ') }}
          || [ $? -eq 107 ]

      - name: '📂 Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      # checkout action UID differs from container UID
      - name: '🙄 Tell Git to ignore workspace ownership'
        run: git config --global --add safe.directory '*'

      - name: '🧰 Write MinGW cross file'
        if: ${{ matrix.compiler == 'mingw-w64-gcc' }}
        run: |
          cat << EOF > cross.ini
          [binaries]
          c = '/usr/bin/x86_64-w64-mingw32-gcc'
          cpp = '/usr/bin/x86_64-w64-mingw32-g++'
          objc = '/usr/bin/x86_64-w64-mingw32-gcc'
          ar = '/usr/bin/x86_64-w64-mingw32-ar'
          strip = '/usr/bin/x86_64-w64-mingw32-strip'
          pkg-config = '/usr/bin/x86_64-w64-mingw32-pkg-config'
          windres = '/usr/bin/x86_64-w64-mingw32-windres'
          exe_wrapper = 'wine'
          ld = '/usr/bin/x86_64-w64-mingw32-ld'

          [properties]
          root = '/usr/x86_64-w64-mingw32'
          needs_exe_wrapper = true

          [built-in options]
          default_library = 'static'
          prefer_static = true
          c_link_args = '-static'
          cpp_link_args = '-static'

          [host_machine]
          system = 'windows'
          cpu_family = 'x86_64'
          cpu = 'x86_64'
          endian = 'little'
          EOF
          mkdir wineprefix
          echo "WINEPREFIX=$(pwd)/wineprefix" >> $GITHUB_ENV

      - name: '🚧 Configure build'
        run: meson setup --buildtype debug ${{ join(matrix.args, ' ') }} build

      - name: '🛠 Run build'
        run: meson compile -C build

      - name: '🔬 Run tests'
        run: |
          meson test -C build --print-errorlogs 'jbig2enc:'
          if [ "${{ matrix.compiler }}" = "mingw-w64-gcc" ]; then
            wine ./build/src/jbig2.exe -V
          else
            ./build/src/jbig2 -V
          fi
        if: success() || failure()

      - name: '📦 Install build artifact'
        id: install
        if: ${{ matrix.compiler == 'mingw-w64-gcc' }}
        run: |
          meson install -C build
          echo "prjinfo=$(meson introspect --projectinfo build)" >> $GITHUB_OUTPUT

      - name: '🚀 Upload build artifact'
        if: ${{ matrix.compiler == 'mingw-w64-gcc' }}
        uses: actions/upload-artifact@v7
        with:
          name: jbig2enc-${{ fromJSON(steps.install.outputs.prjinfo)['version'] }}${{ github.event.pull_request.number && format('-pr{0}', github.event.pull_request.number) || '' }}-Windows-X64-${{ matrix.compiler }}
          path: artifact/*

  windows:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: ['windows-2025-vs2026', 'windows-11-arm']
    steps:
      - name: '📂 Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: '🛒 Install dependencies'
        run: python -m pip install meson

      - name: '🚧 Configure build'
        shell: bash
        run: >
          meson setup
          --vsenv
          --pkgconfig.relocatable
          --buildtype debug
          --prefix "$(pwd)/artifact"
          --licensedir .
          build

      - name: '🛠 Run build'
        run: meson compile -C build

      - name: '🔬 Run tests'
        run: |
           meson test -C build --print-errorlogs 'jbig2enc:'
           ./build/src/jbig2.exe -V
        if: success() || failure()

      - name: '📦 Install build artifact'
        id: install
        shell: bash
        run: |
          meson install -C build
          echo "prjinfo=$(meson introspect --projectinfo build)" >> $GITHUB_OUTPUT

      - name: '🚀 Upload build artifact'
        uses: actions/upload-artifact@v7
        with:
          name: jbig2enc-${{ fromJSON(steps.install.outputs.prjinfo)['version'] }}${{ github.event.pull_request.number && format('-pr{0}', github.event.pull_request.number) || '' }}-${{ runner.os }}-${{ runner.arch }}-MSVC
          path: artifact/*

  macos:
    runs-on: macos-26
    steps:
      - name: '📂 Checkout'
        uses: actions/checkout@v6
        with:
          fetch-depth: 0
          fetch-tags: true

      - name: '🛒 Install dependencies'
        env:
          HOMEBREW_NO_AUTO_UPDATE: "1"
          HOMEBREW_NO_REQUIRE_TAP_TRUST: "1"
        run: |
          # brew update
          brew install -q meson leptonica

      - name: '🚧 Configure build'
        run: >
          meson setup
          --buildtype debug
          -Db_lto_mode=thin
          -Db_sanitize=address,undefined
          -Db_lundef=false
          build

      - name: '🛠 Run build'
        run: meson compile -C build

      - name: '🔬 Run tests'
        run: |
           meson test -C build --print-errorlogs 'jbig2enc:'
           ./build/src/jbig2 -V
        if: success() || failure()
