name: CMake Build Matrix

on: [push, pull_request]

env:
  BUILD_TYPE: Release

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

jobs:
  build:
    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
        - {
            name: "Ubuntu Latest GCC SWIG",
            os: ubuntu-latest,
            cc: "gcc", cxx: "g++",
            cmake_flags: "-DPYTHON_BINDINGS=ON -DRUN_SWIG=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo"
          }
        - {
            name: "Ubuntu Latest GCC", artifact: "Linux.tar.xz",
            os: ubuntu-latest,
            cc: "gcc", cxx: "g++",
          }
        - {
            name: "macOS Latest Clang", artifact: "macOS.tar.xz",
            os: macos-latest,
            cc: "clang", cxx: "clang++",
          }
        - {
            name: "Windows Latest MSVC x86", artifact: "Windows-MSVC.tar.xz",
            os: windows-latest,
            cc: "cl", cxx: "cl",
            environment_script: "C:/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/VC/Auxiliary/Build/vcvars64.bat",
            cmake_flags: '-DLIBXML2_LIBRARY="$GITHUB_WORKSPACE/$DEPS/libs-common/i386/libxml2.lib" -DCAIRO_INCLUDE_DIRS="$GITHUB_WORKSPACE/$DEPS/include/cairo" -DCAIRO_LIBRARIES="$GITHUB_WORKSPACE/$DEPS/libs-common/i386/cairo.lib" -DLIBXML2_INCLUDE_DIR=. -DZLIB_LIBRARY="$GITHUB_WORKSPACE/$DEPS/libs-common/i386/zlib1.lib" -DZLIB_INCLUDE_DIR=. -DXDR_INCLUDE_DIR="$GITHUB_WORKSPACE/$DEPS/include" -DXDR_LIBRARY="$GITHUB_WORKSPACE/$DEPS/libs-common/i386/xdr.lib"  -DJSON_LIBRARY="$GITHUB_WORKSPACE/$DEPS/libs-vs12/i386/jsoncpp.lib" -DwxWidgets_LIB_DIR="$GITHUB_WORKSPACE/$DEPS/wx/lib/vc120_dll" -DEIGEN3_INCLUDE_DIR="$GITHUB_WORKSPACE/$DEPS/include" -DWITH_MAEPARSER=OFF'
          }
        - {
            name: "Ubuntu ARM ASAN+UBSAN",
            os: 	ubuntu-24.04-arm,
            cc: "clang", cxx: "clang++",
            sanitizer: "asan-ubsan",
            cmake_flags: '-DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g" -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address,undefined" -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address,undefined" -DCMAKE_MODULE_LINKER_FLAGS="-fsanitize=address,undefined"'
          }
        - {
            name: "Ubuntu Clang ASAN+UBSAN",
            os: ubuntu-latest,
            cc: "clang", cxx: "clang++",
            sanitizer: "asan-ubsan",
            cmake_flags: '-DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g" -DCMAKE_CXX_FLAGS="-fsanitize=address,undefined -fno-sanitize-recover=all -fno-omit-frame-pointer -g" -DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address,undefined" -DCMAKE_SHARED_LINKER_FLAGS="-fsanitize=address,undefined" -DCMAKE_MODULE_LINKER_FLAGS="-fsanitize=address,undefined"'
          }

    steps:
    - name: Check variables
      run: echo "${{runner.os}} ${{matrix.config.os}} ${{matrix.config.cc}}"

    - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2

    - name: Install MSVC Command Prompt
      if: runner.os == 'Windows'
      uses: ilammy/msvc-dev-cmd@0b201ec74fa43914dc39ae48a89fd1d8cb592756 # v1.13.0
      with:
        arch: x86

    - name: Install Dependencies (Linux)
      if: runner.os == 'Linux'
      run: |
        sudo apt-get -qq update
        sudo apt-get -qq install ninja-build swig libeigen3-dev libboost-all-dev libcairo2-dev

    - name: Install Dependencies (Windows)
      if: runner.os == 'Windows'
      run: |
        choco install ninja
        echo "DEPS=msvc-dependencies-master" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append

    - name: Install Dependencies (macOS)
      if: runner.os == 'macOS'
      run: brew install ninja swig eigen boost

    - name: Install MSVC Dependencies
      if: runner.os == 'Windows'
      shell: cmake -P {0}
      run: |
          set(msvc_deps_url "https://github.com/timvdm/msvc-dependencies/archive/master.zip")
          file(DOWNLOAD "${msvc_deps_url}" "$ENV{GITHUB_WORKSPACE}/msvc_deps.zip" SHOW_PROGRESS)
          execute_process(COMMAND ${CMAKE_COMMAND} -E tar xvf "$ENV{GITHUB_WORKSPACE}/msvc_deps.zip")
    - name: Install Dependencies (macOS)
      if: runner.os == 'macOS'
      run: brew install ninja swig eigen boost

    - name: Configure
      run: |
        mkdir "${{ runner.workspace }}/build"
        cd "${{ runner.workspace }}/build"
        cmake $GITHUB_WORKSPACE -GNinja -DCMAKE_C_COMPILER=${{matrix.config.cc}} -DCMAKE_CXX_COMPILER=${{matrix.config.cxx}} ${{matrix.config.cmake_flags}}
      shell: bash

    - name: Build
      run: CC=${{matrix.config.cc}} CXX=${{matrix.config.cxx}} cmake --build .
      shell: bash
      working-directory: ${{ runner.workspace }}/build

    - name: Test
      if: runner.os != 'Windows'
      working-directory: ${{ runner.workspace }}/build
      env:
        # detect_odr_violation=0: OpenBabel has known ODR issues unrelated
        # to memory safety; toggling on floods reports for now
        # detect_leaks=0: enable later once initial leaks are fixed.
        ASAN_OPTIONS: detect_odr_violation=0:detect_leaks=0:abort_on_error=1:print_stacktrace=1:symbolize=1
        UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1
      run: ctest --output-on-failure

    - name: Upload sanitizer log on failure
      if: failure() && matrix.config.sanitizer != ''
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: sanitizer-log-${{ matrix.config.sanitizer }}
        path: |
          ${{ runner.workspace }}/build/Testing/Temporary/LastTest.log
          ${{ runner.workspace }}/build/Testing/Temporary/LastTestsFailed.log
        if-no-files-found: ignore
        retention-days: 14

    - name: Install Strip
      if: matrix.config.artifact != 0
      run: cmake --install build --prefix instdir --strip
      working-directory: ${{ runner.workspace }}

    - name: Pack
      if: matrix.config.artifact != 0
      run: cmake -E tar cJfv ../${{ matrix.config.artifact }} .
      working-directory: ${{ runner.workspace }}/instdir

    - name: Upload
      if: matrix.config.artifact != 0
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        path: ${{ runner.workspace }}/${{ matrix.config.artifact }}
        name: ${{ matrix.config.artifact }}

    - name: Setup tmate debugging session
      if: ${{ failure() }}
      uses: mxschmitt/action-tmate@c0afd6f790e3a5564914980036ebf83216678101 # v3.23
      timeout-minutes: 60
