name: CI

on:
  pull_request:
    branches:
      - master
  push:
    branches:
      - ci
    tags:
      - lighttpd-*

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

jobs:

  # note: could combine Linux-ubuntu-gcc and Linux-ubuntu-clang by using
  #     strategy: matrix: compiler: ['gcc','clang']
  #   but a bit excessive for combinatorial explosion of different build systems
  # note: use compiler package gcc-multilib or clang-multilib
  #   if platform not x64 or Windows

  Linux-ubuntu-gcc:
    runs-on: ubuntu-latest
    name: Linux-ubuntu-${{matrix.platform}}-${{matrix.compiler}}
    strategy:
      matrix:
        platform: ['x64']
        compiler: ['gcc']
    steps:
      - uses: actions/checkout@v6
      - name: Install packages
        run: |
          sudo apt-get update
          sudo DEBIAN_FRONTEND=noninteractive \
            apt-get install -yq --no-install-recommends \
            build-essential ${{matrix.compiler}} \
            cmake meson ninja-build pkg-config scons \
            autoconf automake autotools-dev libtool m4 \
            nettle-dev gnutls-dev \
            libmbedtls-dev libnss3-dev libssl-dev \
            libpcre2-dev libbrotli-dev libdeflate-dev zlib1g-dev libzstd-dev \
            libsasl2-dev libkrb5-dev libdbi-dev libpam-dev libldap-dev \
            libmariadb-dev libpq-dev \
            libmaxminddb-dev libunwind-dev liblua5.1-0-dev liblua5.4-dev \
            libxml2-dev libsqlite3-dev
      - name: Compile and Test
        run: |
          set -e
          cd "${{github.workspace}}" && scripts/ci-build.sh meson
          #cd "${{github.workspace}}" && scripts/ci-build.sh cmake
          cd "${{github.workspace}}" && scripts/ci-build.sh cmake-asan
          cd "${{github.workspace}}" && scripts/ci-build.sh scons
          cd "${{github.workspace}}" && scripts/ci-build.sh autobuild

  Linux-ubuntu-clang:
    runs-on: ubuntu-latest
    name: Linux-ubuntu-${{matrix.platform}}-${{matrix.compiler}}
    strategy:
      matrix:
        platform: ['x64']
        compiler: ['clang']
    steps:
      - uses: actions/checkout@v6
      - name: Install packages
        run: |
          sudo apt-get update
          sudo DEBIAN_FRONTEND=noninteractive \
            apt-get install -yq --no-install-recommends \
            build-essential ${{matrix.compiler}} \
            cmake meson ninja-build pkg-config scons \
            autoconf automake autotools-dev libtool m4 \
            nettle-dev gnutls-dev \
            libmbedtls-dev libnss3-dev libssl-dev \
            libpcre2-dev libbrotli-dev libdeflate-dev zlib1g-dev libzstd-dev \
            libsasl2-dev libkrb5-dev libdbi-dev libpam-dev libldap-dev \
            libmariadb-dev libpq-dev \
            libmaxminddb-dev libunwind-dev liblua5.1-0-dev liblua5.4-dev \
            libxml2-dev libsqlite3-dev
      - name: Compile and Test
        run: |
          set -e
          #cd "${{github.workspace}}" && scripts/ci-build.sh meson
          cd "${{github.workspace}}" && scripts/ci-build.sh cmake
          #cd "${{github.workspace}}" && scripts/ci-build.sh scons
          #cd "${{github.workspace}}" && scripts/ci-build.sh autobuild

  macOS:
    runs-on: macos-latest
    steps:
      - uses: actions/checkout@v6
      - name: Install packages
        run: |
          brew install -q meson ninja pkg-config pcre2 perl \
            gnutls libressl mbedtls nettle nss wolfssl \
            cyrus-sasl krb5 libdbi openldap \
            mariadb-connector-c libpq \
            brotli libdeflate zlib zstd \
            libmaxminddb lua \
            libxml2 sqlite
          # autoconf automake libtool m4 cmake scons
      - name: Compile and Test
        run: |
          set -e
          export WITH_WOLFSSL=1
          export NO_UNWIND=1
          cd "${{github.workspace}}" && scripts/ci-build.sh meson

  DragonflyBSD:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: vmactions/dragonflybsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg install -y bash ninja meson pkgconf pcre2 perl5
          run: |
            set -e
            meson setup "${{github.workspace}}/build" -Dwarning_level=3
            meson compile -C "${{github.workspace}}/build" --verbose
            meson test -C "${{github.workspace}}/build" --verbose

  FreeBSD:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: vmactions/freebsd-vm@v1
        with:
          mem: 11264
          cpu: 4
          usesh: true
          copyback: false
          prepare: |
            mount -t procfs proc /proc
          run: |
            #mkdir -p /usr/local/etc/pkg/repos
            #echo 'FreeBSD: { url: "pkg+https://pkg.FreeBSD.org/${ABI}/latest" }' > /usr/local/etc/pkg/repos/FreeBSD.conf
            pkg update -f -q
            pkg upgrade -y
            pkg install -y autoconf automake autotools libtool m4 pkgconf \
              ninja meson cmake scons-py311 pcre2 perl5 \
              gnutls libressl mbedtls3 nettle nss wolfssl \
              cyrus-sasl krb5 libdbi openldap26-client \
              mariadb1011-client postgresql16-client \
              brotli libdeflate zstd \
              libmaxminddb libunwind lua51 lua54 \
              libxml2 sqlite3
            set -e
            export WITH_WOLFSSL=1
            cd "${{github.workspace}}" && scripts/ci-build.sh meson
            cd "${{github.workspace}}" && scripts/ci-build.sh cmake
            cd "${{github.workspace}}" && scripts/ci-build.sh scons
            cd "${{github.workspace}}" && scripts/ci-build.sh autobuild

  NetBSD:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: vmactions/netbsd-vm@v1
        with:
          #usesh: true
          prepare: |
            pkg_add -u dash cmake ninja pkgconf pcre2 perl \
              gnutls libressl mbedtls nettle nss wolfssl \
              cyrus-sasl mit-krb5 libdbi openldap-client \
              mariadb-client postgresql16-client \
              brotli libdeflate zlib zstd \
              libmaxminddb libunwind lua51 lua54 \
              libxml2 sqlite3
          # autoconf automake gmake libtool m4 meson py311-scons
          run: |
            set -e
            export WITH_WOLFSSL=1
            export NO_UNWIND=1
            cd "${{github.workspace}}" && scripts/ci-build.sh cmake

  OpenBSD:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: vmactions/openbsd-vm@v1
        with:
          usesh: true
          prepare: |
            pkg_add ninja meson pkgconf pcre2 \
              gnutls mbedtls libnettle nss \
              cyrus-sasl-- heimdal-libs libdbi openldap-client-- \
              mariadb-client postgresql-client \
              brotli libdeflate zstd \
              libmaxminddb lua--%5.1 lua--%5.4 \
              libxml sqlite3
          # autoconf automake cmake gmake libtool m4 scons
          run: |
            set -e
            export NO_PAM=1 NO_UNWIND=1 NO_WOLFSSL=1
            cd "${{github.workspace}}" && scripts/ci-build.sh meson

  # Solaris VM test is slow compared to other VMs tests
  # (retrieving and installing packages may be slow)
  Solaris:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - uses: vmactions/solaris-vm@v1
        with:
          release: "11.4-gcc"
          usesh: true
          copyback: false
          prepare: |
            pkgutil -U
            pkgutil -y -i cmake gmake pkgconfig perl \
              libpcre2_dev \
              libnettle_dev libgnutls_dev libnss_dev libssl_dev \
              libbrotli_dev libz_dev libzstd_dev \
              libkrb5_dev libdbi_dev openldap_dev sasl_dev \
              mysql56_dev postgresql_dev \
              lua_dev \
              libxml2_dev libsqlite3_dev
          run: |
            set -e
            cmake -S "${{github.workspace}}" -B "build" -Wno-dev -DCMAKE_BUILD_TYPE=Release \
              -DBUILD_EXTRA_WARNINGS=ON \
              -DWITH_PCRE2=ON \
              -DWITH_ZLIB=ON \
              -DWITH_LDAP=ON \
              -DWITH_DBI=ON \
              -DWITH_PGSQL=ON \
              -DWITH_SASL=ON \
              -DWITH_GNUTLS=ON \
              -DWITH_NETTLE=ON \
              -DWITH_NSS=ON \
              -DWITH_OPENSSL=ON \
              -DWITH_WEBDAV_PROPS=ON
            cmake --build build --config Release
            ctest -V --test-dir build --build-config Release --no-tests=error
            # no packages for libdeflate, maxminddb, mbedtls
            #   (modifications needed for scripts/ci-build.sh)
            # additional work needed for build config to find lua, brotli, zstd
            # additional work needed for build to find 64-bit mysql libs
            # additional work needed for build to find krb5.h
            #set -e
            #export NO_PAM=1 NO_UNWIND=1 NO_WOLFSSL=1
            #export NO_LIBDEFLATE=1 NO_MAXMINDDB=1 NO_MBEDTLS=1
            #cd "${{github.workspace}}" && scripts/ci-build.sh cmake

  Windows-Cygwin:
    runs-on: windows-latest
    env:
      CYGWIN: winsymlinks:native
    steps:
      - run: git config --global core.autocrlf input
      - uses: actions/checkout@v6
      - name: Set up env and create cache dir
        id: env
        shell: pwsh
        run: |
          Add-Content -Path $env:GITHUB_ENV -Value "CACHE_KEY=$(Get-Date -UFormat "%Y-%m")" -Encoding utf8NoBOM
          New-Item -ItemType Directory -Force -Path D:\cygwin
      - name: Cache Cygwin installation
        uses: actions/cache@v5
        with:
          path: /cygwin
          key: cygwin-${{ runner.os }}-${{ env.CACHE_KEY }}
          restore-keys: |
            cygwin-${{ runner.os }}-${{ env.CACHE_KEY }}
      #- name: Create Cygwin package dir under Cygwin cache dir
      #  shell: pwsh
      #  run: |
      #    New-Item -ItemType Directory -Force -Path D:\cygwin\packages
      #    New-Item -ItemType SymbolicLink -Path D:\cygwin-packages -Target D:\cygwin\packages
      - uses: cygwin/cygwin-install-action@master
        with:
          install-dir: D:\cygwin
          allow-test-packages: true
          packages: >
            autoconf automake libtool m4 make
            cmake meson ninja scons
            gcc-g++ git pkgconf perl
            libpcre2-devel
            libnettle-devel
            libgnutls-devel mbedtls-devel libnss-devel libssl-devel
            libbrotli-devel libdeflate-devel zlib-devel libzstd-devel
            libsasl2-devel libkrb5-devel libdbi-devel openldap-devel
            libmariadb-devel libpq-devel
            libmaxminddb-devel libunwind-devel lua-devel lua5.1-devel
            libxml2-devel libsqlite3-devel
            libintl-devel
      - name: Compile and Test
        shell: D:\cygwin\bin\bash.exe --noprofile --norc -o igncr -eo pipefail '{0}'
        run: |
          set -e
          export PATH=/usr/bin:$(cygpath ${SYSTEMROOT})/system32
          export NO_PAM=1 NO_UNWIND=1 NO_WOLFSSL=1
          export SHELL=/bin/dash
          cd "${{github.workspace}}" && /bin/dash scripts/ci-build.sh autobuild

  Windows-VisualStudio:
    runs-on: windows-2025-vs2026
    name: Windows-VisualStudio-${{matrix.platform}}
    strategy:
      matrix:
        platform: ['x64']
        #platform: ['x64', 'win32']
    #error: The GHA binary source requires the ACTIONS_RUNTIME_TOKEN and ACTIONS_CACHE_URL environment variables to be set. See https://learn.microsoft.com/vcpkg/users/binarycaching?WT.mc_id=vcpkg_inproduct_cli#gha for details.
    #env:
    #  VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
    steps:
      - uses: actions/checkout@v6
      - uses: ilammy/msvc-dev-cmd@v1
      #- name: Install dependencies
      #  run: vcpkg install --triplet ${{matrix.platform}}-windows openssl pcre2 zlib
      - name: Compile and Test
        run: |
          $ErrorActionPreference = 'Stop'
          #cmake -G "Visual Studio 18 2026" -A "${{matrix.platform}}" -S "${{github.workspace}}" -B "build" -Wno-dev -DCMAKE_BUILD_TYPE=Release -DWITH_OPENSSL=ON -DWITH_PCRE2=OFF -DWITH_PCRE=OFF -DWITH_ZLIB=OFF
          cmake -G "Visual Studio 18 2026" -A "${{matrix.platform}}" -S "${{github.workspace}}" -B "build" -Wno-dev -DCMAKE_BUILD_TYPE=Release -DWITH_PCRE2=OFF -DWITH_PCRE=OFF -DWITH_ZLIB=OFF
          cmake --build build --config Release
          # run the compiled unit tests src/t/test_*;
          # (skip the Perl test framework in tests/*)
          # (note: tests/CMakeLists.txt also skips Perl tests/* on native WIN32)
          ctest -V --test-dir build --build-config Release --no-tests=error -R '^test_'

  Windows-MSYS2:
    runs-on: windows-latest
    name: Windows-MSYS2-${{matrix.platform}}
    strategy:
      matrix:
        platform: ['mingw64', 'ucrt64', 'clang64']
        #platform: ['mingw32', 'mingw64', 'ucrt64', 'clang32', 'clang64']
    steps:
      - run: git config --global core.autocrlf input
      - uses: actions/checkout@v6
      - uses: msys2/setup-msys2@v2
        with:
          msystem: ${{matrix.platform}}
          update: true
          install: git m4
          pacboy: >-
            cc:p autotools:p libtool:p make:p pcre2:p pkgconf:p
            nettle:p gnutls:p mbedtls:p nss:p openssl:p
            brotli:p libdeflate:p zlib:p zstd:p
            cyrus-sasl:p openldap:p
            libmariadbclient:p postgresql:p
            libmaxminddb:p libunwind:p lua51:p lua:p luajit:p
            libxml2:p sqlite3:p
          # heimdal-devel: available only on platform: msys (?)
      - name: Compile and Test
        shell: msys2 {0}
        run: |
          set -e
          # NO_GNUTLS=1 gnutls/socket.h can not find <sys/socket.h>
          # NO_MYSQL=1 build not finding mysql_config
          export NO_DBI=1 NO_GNUTLS=1 NO_KRB5=1 NO_MYSQL=1 NO_PAM=1 NO_UNWIND=1 NO_WOLFSSL=1
          export SHELL=/bin/dash
          cd "${{github.workspace}}" && /bin/dash scripts/ci-build.sh autobuild

  Linux-alpine-x86_64:
    runs-on: ubuntu-latest
    name: Linux-alpine-${{matrix.platform}}
    # abort if x86_64 fails (x86_64 or x86 are fastest "native" builds)
    continue-on-error: ${{ matrix.platform != 'x86_64' }}
    strategy:
      fail-fast: true
      matrix:
        platform: ['x86_64']
    steps:
      - uses: actions/checkout@v6
      - uses: jirutka/setup-alpine@v1
        with:
          branch: latest-stable
          arch: ${{matrix.platform}}
          packages: >
            build-base cmake meson ninja pkgconf perl scons
            autoconf automake libtool m4
            gnutls-dev mbedtls-dev openssl-dev nettle-dev nss-dev
            pcre2-dev brotli-dev libdeflate-dev zlib-dev zstd-dev
            cyrus-sasl-dev krb5-dev libdbi-dev linux-pam-dev openldap-dev
            mariadb-connector-c-dev postgresql16-dev
            libmaxminddb-dev libunwind-dev lua5.1-dev lua5.4-dev
            libxml2-dev sqlite-dev
            zlib-static openssl-libs-static pcre2-static
      - name: Compile and Test
        shell: alpine.sh {0}
        run: |
          set -e
          export SHELL=/bin/sh
          # Alpine Linux wolfssl package built w/o features required by lighttpd
          #('scons check_fullstatic' needs *-static packages in list above)
          export NO_WOLFSSL=1
          cd "${{github.workspace}}" && scripts/ci-build.sh meson
          cd "${{github.workspace}}" && scripts/ci-build.sh cmake
          cd "${{github.workspace}}" && scripts/ci-build.sh scons
          cd "${{github.workspace}}" && scripts/ci-build.sh autobuild

  Linux-alpine:
    runs-on: ubuntu-latest
    name: Linux-alpine-${{matrix.platform}}
    # abort if x86 fails (x86_64 or x86 are fastest "native" builds)
    continue-on-error: ${{ matrix.platform != 'x86' }}
    strategy:
      fail-fast: true
      matrix:
        platform: ['x86','armhf','armv7','aarch64','ppc64le','riscv64','s390x']
    steps:
      - uses: actions/checkout@v6
      - uses: jirutka/setup-alpine@v1
        with:
          # riscv64 currently requires 'edge'
          branch: edge
          arch: ${{matrix.platform}}
          packages: >
            build-base meson ninja perl pkgconf pcre2-dev zlib-dev
      - name: Compile and Test
        shell: alpine.sh {0}
        run: |
          set -e
          export SHELL=/bin/sh
          meson setup "${{github.workspace}}/build" -Dwarning_level=3
          meson compile -C "${{github.workspace}}/build" --verbose
          meson test -C "${{github.workspace}}/build" --verbose
