name: Special Cases CI

on: [pull_request]

jobs:
  build:
    name: ${{ matrix.name }} ${{ matrix.build_type }}
    runs-on: ${{ matrix.os }}

    env:
      CTEST_OUTPUT_ON_FAILURE: ON
      CTEST_PARALLEL_LEVEL: 2
      CMAKE_BUILD_TYPE: ${{ matrix.build_type }}
      GTSAM_BUILD_UNSTABLE: ON
      BOOST_VERSION: 1.67.0

    strategy:
      fail-fast: false

      matrix:
        # Github Actions requires a single row to be added to the build matrix.
        # See https://help.github.com/en/articles/workflow-syntax-for-github-actions.
        name:
          [
            ubuntu-gcc-deprecated,
            ubuntu-gcc-quaternions,
            ubuntu-gcc-tbb,
            ubuntu-cayleymap,
          ]

        build_type: [Debug, Release]

        include:
          - name: ubuntu-gcc-deprecated
            os: ubuntu-20.04
            compiler: gcc
            version: "9"
            flag: deprecated

          - name: ubuntu-gcc-quaternions
            os: ubuntu-20.04
            compiler: gcc
            version: "9"
            flag: quaternions

          - name: ubuntu-gcc-tbb
            os: ubuntu-20.04
            compiler: gcc
            version: "9"
            flag: tbb

          - name: ubuntu-cayleymap
            os: ubuntu-20.04
            compiler: gcc
            version: "9"
            flag: cayley

          - name: ubuntu-system-libs
            os: ubuntu-20.04
            compiler: gcc
            version: "9"
            flag: system-libs

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install (Linux)
        if: runner.os == 'Linux'
        run: |
          # LLVM 9 is not in Bionic's repositories so we add the official LLVM repository.
          if [ "${{ matrix.compiler }}" = "clang" ] && [ "${{ matrix.version }}" = "9" ]; then
            gpg --keyserver keyserver.ubuntu.com --recv-key $LLVM_KEY || gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-key $LLVM_KEY
            gpg -a --export 15CF4D18AF4F7421 | sudo apt-key add -
            sudo add-apt-repository "deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-9 main"
          fi

          sudo apt-get -y update
          sudo apt-get -y install cmake build-essential pkg-config libpython3-dev python3-numpy libicu-dev

          if [ "${{ matrix.compiler }}" = "gcc" ]; then
            sudo apt-get install -y g++-${{ matrix.version }} g++-${{ matrix.version }}-multilib
            echo "CC=gcc-${{ matrix.version }}" >> $GITHUB_ENV
            echo "CXX=g++-${{ matrix.version }}" >> $GITHUB_ENV
          else
            sudo apt-get install -y clang-${{ matrix.version }} g++-multilib
            echo "CC=clang-${{ matrix.version }}" >> $GITHUB_ENV
            echo "CXX=clang++-${{ matrix.version }}" >> $GITHUB_ENV
          fi

      - name: Install Boost
        if: runner.os == 'Linux'
        run: |
          bash .github/scripts/boost.sh

      - name: Install (macOS)
        if: runner.os == 'macOS'
        run: |
          brew install cmake ninja boost
          if [ "${{ matrix.compiler }}" = "gcc" ]; then
            brew install gcc@${{ matrix.version }}
            echo "CC=gcc-${{ matrix.version }}" >> $GITHUB_ENV
            echo "CXX=g++-${{ matrix.version }}" >> $GITHUB_ENV
          else
            sudo xcode-select -switch /Applications/Xcode_${{ matrix.version }}.app
            echo "CC=clang" >> $GITHUB_ENV
            echo "CXX=clang++" >> $GITHUB_ENV
            fi

      - name: Set Allow Deprecated Flag
        if: matrix.flag == 'deprecated'
        run: |
          echo "GTSAM_ALLOW_DEPRECATED_SINCE_V42=ON" >> $GITHUB_ENV
          echo "Allow deprecated since version 4.2"

      - name: Set Use Quaternions Flag
        if: matrix.flag == 'quaternions'
        run: |
          echo "GTSAM_USE_QUATERNIONS=ON" >> $GITHUB_ENV
          echo "Use Quaternions for rotations"

      - name: Set GTSAM_WITH_TBB Flag
        if: matrix.flag == 'tbb'
        run: |
          echo "GTSAM_WITH_TBB=ON" >> $GITHUB_ENV
          echo "GTSAM Uses TBB"

      - name: Use Cayley Transform for Rot3
        if: matrix.flag == 'cayley'
        run: |
          echo "GTSAM_POSE3_EXPMAP=OFF" >> $GITHUB_ENV
          echo "GTSAM_ROT3_EXPMAP=OFF" >> $GITHUB_ENV
          echo "GTSAM Uses Cayley map for Rot3"

      - name: Use system versions of 3rd party libraries
        if: matrix.flag == 'system'
        run: |
          echo "GTSAM_USE_SYSTEM_EIGEN=ON" >> $GITHUB_ENV
          echo "GTSAM_USE_SYSTEM_METIS=ON" >> $GITHUB_ENV

      - name: Build & Test
        run: |
          bash .github/scripts/unix.sh -t
