on:
  push:
    branches:
    - main
    tags:
    - '!*'
  pull_request:
    branches:
    - main

permissions:
  contents: read

name: Main
jobs:
  build_linux:
    name: Build Linux
    container:
      image: ubuntu:22.04
    runs-on: ubuntu-24.04

    strategy:
      fail-fast: false
      matrix:
        compiler: [ gcc, clang ]
        include:
          - compiler: gcc
            cxx_compiler: g++
            compiler_flags: -Wall -Wextra -Werror -Wno-builtin-declaration-mismatch
            packages: gcc g++
          - compiler: clang
            cxx_compiler: clang++
            compiler_flags: -Wall -Wextra -Werror -Wno-unused-function -Wno-incompatible-library-redeclaration
            packages: clang

    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
        with:
          persist-credentials: false

      - name: Install dependencies
        run: |
          set -e
          export DEBIAN_FRONTEND=noninteractive
          apt-get update -y
          apt-get install -y autoconf git libtool make pkg-config ${{ matrix.packages }}

      - name: Configure ImageMagick
        env:
          CC: ${{ matrix.compiler }}
          CXX: ${{ matrix.cxx_compiler }}
          CFLAGS: ${{ matrix.compiler_flags }}
          CXXFLAGS: ${{ matrix.compiler_flags }}
        run: |
          autoreconf -fiv
          ./configure --with-quantum-depth=16 --enable-hdri=no --without-perl --prefix=/usr

      - name: Build ImageMagick
        run: |
          make

      - name: Test ImageMagick
        run: |
          make check || exit_code=$?
          if [ "$exit_code" != "0" ] ; then cat ./test-suite.log ; fi
          exit $exit_code

      - name: Install ImageMagick
        run: |
          make install

  build_macos:
    name: Build MacOS x64
    runs-on: macos-15-intel

    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
        with:
          persist-credentials: false

      - name: Install dependencies
        run: |
          set -e
          export HOMEBREW_NO_AUTO_UPDATE=1
          brew install automake fftw flif jbigkit

      - name: Configure ImageMagick
        run: |
          export CFLAGS="-Wall -Wextra -Wdeclaration-after-statement -Wno-deprecated-declarations -Wno-incompatible-library-redeclaration -Wno-incompatible-pointer-types-discards-qualifiers -Wno-missing-declarations -Wno-unused-function -Wno-unused-parameter"
          autoreconf -fiv
          ./configure --with-quantum-depth=16 --enable-hdri=no --without-perl --with-fftw --with-flif

      - name: Build ImageMagick
        run: |
          set -e
          make install

  build_macos_arm64:
    name: Build MacOS arm64
    runs-on: macos-14

    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
        with:
          persist-credentials: false

      - name: Install dependencies
        run: |
          set -e
          export HOMEBREW_NO_AUTO_UPDATE=1
          brew install automake fftw flif jbigkit libraqm librsvg libtool libxml2 pango

      - name: Configure ImageMagick
        run: |
          export CFLAGS="-Wall -Wextra -Werror -Wdeclaration-after-statement -Wno-deprecated-declarations -Wno-incompatible-library-redeclaration -Wno-incompatible-pointer-types-discards-qualifiers -Wno-missing-declarations -Wno-unused-function -Wno-unused-parameter"
          export LDFLAGS=$(pkg-config --libs libjpeg)
          autoreconf -fiv
          ./configure --with-quantum-depth=16 --enable-hdri=no --without-perl --with-fftw --with-flif --with-rsvg

      - name: Build ImageMagick
        run: |
          make

      - name: Test ImageMagick
        run: |
          make check || exit_code=$?
          if [ "$exit_code" != "0" ] ; then cat ./test-suite.log ; fi
          exit $exit_code

      - name: Install ImageMagick
        run: |
          sudo make install

  build_windows:
    name: Build Windows ${{matrix.architecture}}
    runs-on: windows-2025-vs2026

    strategy:
      fail-fast: false
      matrix:
        architecture: [ x64, arm64, x86 ]

    steps:
      - name: Clone ImageMagick
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
        with:
          path: ImageMagick
          persist-credentials: false

      - name: Download configure
        shell: cmd
        run: |
          ImageMagick\.github\build\windows\download-configure.cmd

      - name: Download dependencies
        shell: cmd
        run: |
          ImageMagick\.github\build\windows\download-dependencies.cmd windows-${{matrix.architecture}}-static-openMP.zip

      - name: Configure ImageMagick
        shell: cmd
        working-directory: Configure
        run: |
          Configure.Release.x64.exe /noWizard /VS2026 /${{matrix.architecture}} /static

      - name: Build ImageMagick
        shell: cmd
        run: |
          call "C:\Program Files\Microsoft Visual Studio\18\Enterprise\Common7\Tools\VsDevCmd.bat"
          msbuild IM7.Static.${{matrix.architecture}}.sln /m /t:Rebuild /p:Configuration=Release,Platform=${{matrix.architecture}}

  build_msys2:
    name: Build MSYS2
    runs-on: windows-2025

    steps:
      - name: Prepare git
        run: git config --global core.autocrlf false

      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 #v7.0.1
        with:
          persist-credentials: false

      - uses: msys2/setup-msys2@66cd2cce69caa17b53920067426061ca1de3a884 #v2.32.0
        with:
          install: mingw-w64-x86_64-toolchain base-devel binutils
          update: true
          cache: false

      - name: Build ImageMagick
        run: cd .github/build/msys2 && makepkg-mingw --noconfirm --syncdeps
        env:
          MINGW_ARCH: mingw64
          PKGEXT: ".pkg.tar.xz"
        shell: msys2 {0}
