name: CMake-MacOS

on:
  push:
    branches: 
      - master
  pull_request:
    branches: 
      - '**'

env:
  BUILD_TYPE: Release

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

jobs:
  build:
    name: ${{ matrix.build_name }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: true
      matrix:
        build_name: [macos-sequoia-intel, macos-sequoia-apple-silicon, macos-tahoe-intel, macos-tahoe-apple-silicon]
        include:
        - build_name: macos-sequoia-intel
          os: macos-15-intel
        - build_name: macos-sequoia-apple-silicon
          os: macos-15
        - build_name: macos-tahoe-intel
          os: macos-26-intel
        - build_name: macos-tahoe-apple-silicon
          os: macos-26
    steps:
    - uses: actions/checkout@v4

    - name: Install Brew Dependencies
      run: |
        # Update brew and install from Brewfile if present, or specific packages
        brew install pcl opencv octomap pdal yaml-cpp librealsense libfreenect libusb zlib libomp suite-sparse ceres-solver

    - name: Install Source Dependencies
      # Build (and per-dependency cache) the source-only deps not available from
      # Homebrew, then install them into /usr/local. See the composite action.
      uses: ./.github/actions/install-macos-source-deps
      with:
        os: ${{ matrix.os }}
        build_type: ${{ env.BUILD_TYPE }}

    - name: Configure CMake
      run: |
        # Apple clang has no built-in OpenMP; point find_package(OpenMP) at
        # Homebrew's keg-only libomp so PCL/g2o/rtabmap enable OpenMP instead of
        # repeatedly logging "Could NOT find OpenMP".
        LIBOMP=$(brew --prefix libomp)
        cmake -B ${{github.workspace}}/build \
          -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} \
          -DBUILD_AS_BUNDLE=ON \
          -DWITH_ORBBEC_SDK=ON \
          -DWITH_DEPTHAI=ON \
          -DWITH_CERES=ON \
          -DOpenMP_C_FLAGS="-Xclang -fopenmp -I$LIBOMP/include" \
          -DOpenMP_C_LIB_NAMES=omp \
          -DOpenMP_CXX_FLAGS="-Xclang -fopenmp -I$LIBOMP/include" \
          -DOpenMP_CXX_LIB_NAMES=omp \
          -DOpenMP_omp_LIBRARY="$LIBOMP/lib/libomp.dylib"

    - name: Build
      run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

    - name: Info
      working-directory: ${{github.workspace}}/build/bin
      run: |
        ./rtabmap-console --version

    - name: Build MacOS Package
      run: |
        cmake --build ${{ github.workspace }}/build --config ${{ env.BUILD_TYPE }} --target package

    - name: Upload RTABMap Artifacts (DMG)
      uses: actions/upload-artifact@v4
      with:
        name: RTABMap-Binaries-${{ matrix.build_name }}-nonsigned-zip
        path: |
          build/RTABMap-*.dmg
        compression-level: 0
        if-no-files-found: warn
        retention-days: ${{ github.event_name == 'pull_request' && 1 || 90 }}

#    - name: Test
#      working-directory: ${{github.workspace}}/build
#      # Execute tests defined by the CMake configuration.  
#      # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
#      run: ctest -C ${{env.BUILD_TYPE}}
      
