#                          __  __            _
#                       ___\ \/ /_ __   __ _| |_
#                      / _ \\  /| '_ \ / _` | __|
#                     |  __//  \| |_) | (_| | |_
#                      \___/_/\_\ .__/ \__,_|\__|
#                               |_| XML parser
#
# Copyright (c) 2021-2026 Sebastian Pipping <sebastian@pipping.org>
# Copyright (c) 2023      Joyce Brum <joycebrum@google.com>
# Copyright (c) 2024      Dag-Erling Smørgrav <des@des.dev>
# Licensed under the MIT license:
#
# Permission is  hereby granted,  free of charge,  to any  person obtaining
# a  copy  of  this  software   and  associated  documentation  files  (the
# "Software"),  to  deal in  the  Software  without restriction,  including
# without  limitation the  rights  to use,  copy,  modify, merge,  publish,
# distribute, sublicense, and/or sell copies of the Software, and to permit
# persons  to whom  the Software  is  furnished to  do so,  subject to  the
# following conditions:
#
# The above copyright  notice and this permission notice  shall be included
# in all copies or substantial portions of the Software.
#
# THE  SOFTWARE  IS  PROVIDED  "AS  IS",  WITHOUT  WARRANTY  OF  ANY  KIND,
# EXPRESS  OR IMPLIED,  INCLUDING  BUT  NOT LIMITED  TO  THE WARRANTIES  OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
# NO EVENT SHALL THE AUTHORS OR  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR  OTHER LIABILITY, WHETHER  IN AN  ACTION OF CONTRACT,  TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
# USE OR OTHER DEALINGS IN THE SOFTWARE.

name: Ensure that GNU Autotools and CMake build systems agree

on:
  pull_request:
  push:
  schedule:
    - cron: '0 2 * * 5'  # Every Friday at 2am
  workflow_dispatch:

permissions:
  contents: read

jobs:
  checks:
    name: Ensure that GNU Autotools and CMake build systems agree
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-14
            configure_args:
            cmake_args:
          - os: ubuntu-22.04
            configure_args:
            cmake_args:
          - os: ubuntu-22.04
            configure_args: --host=i686-w64-mingw32
            cmake_args: -DCMAKE_TOOLCHAIN_FILE=cmake/mingw-toolchain.cmake
          - os: ubuntu-24.04
            configure_args: >-
              --enable-symbol-versioning
              LDFLAGS=-Wl,--no-undefined-version
            cmake_args: >-
              -DEXPAT_SYMBOL_VERSIONING=ON
              -DCMAKE_EXE_LINKER_FLAGS=-Wl,--no-undefined-version
              -DCMAKE_MODULE_LINKER_FLAGS=-Wl,--no-undefined-version
              -DCMAKE_SHARED_LINKER_FLAGS=-Wl,--no-undefined-version
          - os: ubuntu-24.04
            configure_args: >-
              --enable-xml-attr-info
              --enable-symbol-versioning
              LDFLAGS=-Wl,--no-undefined-version
            cmake_args: >-
              -DEXPAT_ATTR_INFO=ON
              -DEXPAT_SYMBOL_VERSIONING=ON
              -DCMAKE_EXE_LINKER_FLAGS=-Wl,--no-undefined-version
              -DCMAKE_MODULE_LINKER_FLAGS=-Wl,--no-undefined-version
              -DCMAKE_SHARED_LINKER_FLAGS=-Wl,--no-undefined-version
    defaults:
      run:
        shell: bash
    runs-on: "${{ matrix.os }}"
    steps:
    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd  # v6.0.2

    - name: (Linux) Install build dependencies
      if: "${{ runner.os == 'Linux' }}"
      run: |-
        set -x
        sudo apt-get update
        sudo apt-get install --yes --no-install-recommends -V \
            cmake \
            docbook2x \
            lzip \
            mingw-w64

    - name: (macOS) Install build dependencies
      if: "${{ runner.os == 'macOS' }}"
      run: |
        brew uninstall cmake
        brew install \
            autoconf \
            automake \
            cmake \
            docbook2x \
            libtool \
            lzip

    - name: Produce and extract a release archive
      run: |
        set -x
        cd expat
        ./buildconf.sh
        ./configure
        make dist
        tar xf expat-*.*.*.tar.gz

    - name: Build and install using GNU Autotools
      run: |
        set -x
        cd expat/expat-*.*.*/
        mkdir build_autotools
        cd build_autotools
        ../configure \
            --libdir='${exec_prefix}/lib123' \
            ${{matrix.configure_args}}
        make install DESTDIR="${PWD}"/ROOT
        find ROOT | sort | xargs ls -ld

    - name: Build and install using CMake
      run: |
        set -x
        cd expat/expat-*.*.*/
        mkdir build_cmake
        cd build_cmake
        cmake \
            -DCMAKE_INSTALL_LIBDIR=lib123 \
            ${{matrix.cmake_args}} \
            ..
        make install DESTDIR="${PWD}"/ROOT
        find ROOT | sort | xargs ls -ld

    - name: Check for identical CMake files from both build systems
      run: |
        set -x
        cd expat/expat-*.*.*/

        if [[ "${{ runner.os }}" == macOS ]]; then
          # There is a known difference between Autotools and CMake for macOS:
          #   Autotools:
          #   - /usr/local/lib123/libexpat.<major>.dylib
          #   - /usr/local/lib123/libexpat.dylib
          #   CMake:
          #   - /usr/local/lib123/libexpat.<major>.<minor>.<patch>.dylib
          #   - /usr/local/lib123/libexpat.<major>.dylib
          #   - /usr/local/lib123/libexpat.dylib
          # We patch the CMake side in line here to get the differ below to empty.
          ( cd build_cmake/ROOT/usr/local/lib*/cmake/expat-*/ \
              && sed -E 's,(libexpat\.[0-9]+)\.[0-9]+\.[0-9]+(\.dylib),\1\2,' expat-noconfig.cmake > expat-noconfig-patched.cmake \
              && ! diff -u0 expat-noconfig{,-patched}.cmake \
              && mv expat-noconfig{-patched,}.cmake )

          # Autotools' LT_LIB_M has a hardcoded exclude for "*-*-darwin*" hosts,
          # while macOS does have libm and is successfully found by CMake.
          # We patch the CMake side in line here to get the differ below to empty.
          #
          # Both GNU and BSD sed can edit in-place without creating a backup,
          # but not with the same syntax.  The syntax for editing in-place
          # _with_ a backup however is the same, so do that, then remove the
          # backup so it doesn't show up in the diff later.
          sed -e 's,-lm,,' -i.bak \
              build_cmake/ROOT/usr/local/lib*/pkgconfig/expat.pc
          rm -f build_cmake/ROOT/usr/local/lib*/pkgconfig/expat.pc.bak
        fi

        diff \
            --recursive \
            --unified \
            --exclude=xmlwf \
            --exclude=xmlwf.exe \
            --exclude=libexpat.a \
            --exclude=libexpat-*.dll \
            --exclude=libexpat.dll.a \
            --exclude=libexpat.la \
            --exclude=libexpat.so\* \
            --exclude=libexpat.\*dylib \
            --exclude=expat_config.h \
            build_{autotools,cmake}/ROOT

    - name: (Linux except MinGW) Check for identical exported symbols from both build systems
      if: "${{ runner.os == 'Linux' && ! contains(matrix.configure_args, 'mingw') }}"
      env:
        maximum: "${{ contains(matrix.configure_args, '--enable-xml-attr-info') && 'true' || 'false' }}"
        symbol_versioning: "${{ contains(matrix.configure_args, '--enable-symbol-versioning') && 'true' || 'false' }}"
      run: |
        list_shared_library_symbols_sh="${GITHUB_WORKSPACE}"/.github/workflows/scripts/list-shared-library-symbols.sh
        if ${symbol_versioning} ; then
            if ${maximum} ; then
                exported_symbols_txt="${GITHUB_WORKSPACE}"/.github/workflows/data/exported-symbols-versioned-maximum.txt
            else
                exported_symbols_txt="${GITHUB_WORKSPACE}"/.github/workflows/data/exported-symbols-versioned-default.txt
            fi
        else
            exported_symbols_txt="${GITHUB_WORKSPACE}"/.github/workflows/data/exported-symbols-unversioned.txt
        fi

        set -x
        cd expat/expat-*.*.*/
        diff -u "${exported_symbols_txt}" <("${list_shared_library_symbols_sh}" build_autotools/ROOT/usr/local/lib*/libexpat.so)
        diff -u "${exported_symbols_txt}" <("${list_shared_library_symbols_sh}" build_cmake/ROOT/usr/local/lib*/libexpat.so)
