name: MacOS

# https://www.jeffgeerling.com/blog/2020/running-github-actions-workflow-on-schedule-and-other-events
on:
  push:
  pull_request:
  schedule:
    - cron: '0 2 * * SUN'

jobs:
  build-macos:
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-11, macos-12]

    steps:
    # https://github.com/marketplace/actions/cancel-workflow-action
    - name: Cancel Previous Runs
      uses: styfle/cancel-workflow-action@0.9.1
      with:
        access_token: ${{ github.token }}

    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Print system information
      run: |
        sysctl -a | grep machdep.cpu
        sysctl -a | grep logical

    - name: Print OS information
      run: system_profiler SPSoftwareDataType

    - name: Install dependencies
      run: brew install libpng libjpeg libdc1394 opencv pcl librealsense zbar pkg-config nlohmann-json

    - name: Install java dependencies
      run: |
        brew install openjdk ant
        sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
        echo "JAVA_HOME=$(/usr/libexec/java_home)" >> $GITHUB_ENV
        echo ${JAVA_HOME}

    - name: Clone visp-images
      env:
        BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
      # https://remarkablemark.org/blog/2022/09/25/check-git-branch-exists-in-remote-repository/
      run: |
        git clone --depth 1 https://github.com/lagadic/visp-images ${HOME}/visp-images
        echo "VISP_INPUT_IMAGE_PATH=$HOME/visp-images" >> $GITHUB_ENV
        echo ${VISP_INPUT_IMAGE_PATH}

    - name: Clone visp_sample
      run: |
        git clone --depth 1 https://github.com/lagadic/visp_sample ${HOME}/visp_sample

    # Openblas location is exported explicitly because openblas is keg-only,
    # which means it was not symlinked into /usr/local/.
    # qt@5 is as a requested dependency for vtk and thus for pcl 1.12.1
    # qt@5 is keg-only, which means it was not symlinked into /usr/local that's why we need to set Qt5_DIR
    - name: Configure CMake
      run: |
        export LDFLAGS="-L/usr/local/opt/openblas/lib"
        export CPPFLAGS="-I/usr/local/opt/openblas/include"
        export Qt5_DIR="$(brew --prefix qt5)/lib/cmake/Qt5"
        mkdir build
        cd build
        cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/tmp/usr/local -DCMAKE_VERBOSE_MAKEFILE=ON -DQt5_DIR=$(brew --prefix qt5)/lib/cmake/Qt5
        cat ViSP-third-party.txt

    - name: Compile
      working-directory: build
      run: make -j$(sysctl -n hw.logicalcpu) install

    - name: Run unit tests
      working-directory: build
      run: ctest -j$(sysctl -n hw.logicalcpu) --output-on-failure -V

    - name: ViSP as 3rdparty with cmake
      run: |
        cd ${HOME}/visp_sample
        mkdir visp_sample-build
        cd visp_sample-build
        cmake ../ -DVISP_DIR=/tmp/usr/local/lib/cmake/visp -DCMAKE_VERBOSE_MAKEFILE=ON
        make -j$(sysctl -n hw.logicalcpu)

    - name: ViSP as 3rdparty with visp.pc and pkg-config
      if: matrix.os != 'macOS-10.15'
      run: |
        # With macOS 10.15 there is the following build error:
        # /Applications/Xcode_12.4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9:
        #   error: no member named 'signbit' in the global namespace
        #   using ::signbit;
        # also reported here https://stackoverflow.com/questions/58313047/cannot-compile-r-packages-with-c-code-after-updating-to-macos-catalina
        # As suggested adding -isysroot build flag in VISPGenerateConfigScript.cmake around line 149 with
        #   execute_process(COMMAND xcrun --show-sdk-path
        #                   OUTPUT_VARIABLE SDK_PATH OUTPUT_STRIP_TRAILING_WHITESPACE)
        #   list(APPEND _cxx_flags "-isysroot ${SDK_PATH}")
        # doesn't fix the build issue. That's why here the test is only done for macOS 11.0
        cd ${HOME}/visp_sample
        export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/tmp/usr/local/lib/pkgconfig
        pkg-config --cflags visp
        pkg-config --libs visp
        make -j$(sysctl -n hw.logicalcpu) -f Makefile.visp.pc
        make -j$(sysctl -n hw.logicalcpu) -f Makefile.visp.pc clean

    - name: ViSP as 3rdparty with visp-config
      if: matrix.os != 'macos-10.15'
      run: |
        cd ${HOME}/visp_sample
        export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/tmp/usr/local/lib/pkgconfig
        export VISP_INSTALL_PREFIX=/tmp/usr/local
        $VISP_INSTALL_PREFIX/bin/visp-config --cflags
        $VISP_INSTALL_PREFIX/bin/visp-config --libs
        make -j$(sysctl -n hw.logicalcpu) -f Makefile.visp-config
        make -j$(sysctl -n hw.logicalcpu) -f Makefile.visp-config clean
