name: Windows 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: ${{ matrix.build_unstable }}
      BOOST_VERSION: 1.72.0
      BOOST_EXE: boost_1_72_0-msvc-14.2

    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: [
            #TODO This build fails, need to understand why.
            # windows-2016-cl,
            windows-2019-cl,
          ]

        build_type: [
            Debug,
            #TODO(Varun) The release build takes over 2.5 hours, need to figure out why.
            # Release
          ]
        build_unstable: [ON]
        include:
          #TODO This build fails, need to understand why.
          # - name: windows-2016-cl
          #   os: windows-2016
          #   compiler: cl
          #   platform: 32

          - name: windows-2019-cl
            os: windows-2019
            compiler: cl
            platform: 64

    steps:
      - name: Install Dependencies
        shell: powershell
        run: |
          iwr -useb get.scoop.sh -outfile 'install_scoop.ps1'
          .\install_scoop.ps1 -RunAsAdmin

          scoop install cmake --global  # So we don't get issues with CMP0074 policy
          scoop install ninja --global

          if ("${{ matrix.compiler }}".StartsWith("clang")) {
            scoop install llvm --global
          }

          if ("${{ matrix.compiler }}" -eq "gcc") {
            # Chocolatey GCC is broken on the windows-2019 image.
            # See: https://github.com/DaanDeMeyer/doctest/runs/231595515
            # See: https://github.community/t5/GitHub-Actions/Something-is-wrong-with-the-chocolatey-installed-version-of-gcc/td-p/32413
            scoop install gcc --global
            echo "CC=gcc" >> $GITHUB_ENV
            echo "CXX=g++" >> $GITHUB_ENV

          } elseif ("${{ matrix.compiler }}" -eq "clang") {
            echo "CC=clang" >> $GITHUB_ENV
            echo "CXX=clang++" >> $GITHUB_ENV

          } else {
            echo "CC=${{ matrix.compiler }}" >> $env:GITHUB_ENV
            echo "CXX=${{ matrix.compiler }}" >> $env:GITHUB_ENV

          }

          # Scoop modifies the PATH so we make the modified PATH global.
          echo "$env:PATH" >> $env:GITHUB_PATH

      - name: Install Boost
        shell: powershell
        run: |
          # Snippet from: https://github.com/actions/virtual-environments/issues/2667
          $BOOST_PATH = "C:\hostedtoolcache\windows\Boost\$env:BOOST_VERSION\x86_64"

          # Use the prebuilt binary for Windows
          $Url = "https://sourceforge.net/projects/boost/files/boost-binaries/$env:BOOST_VERSION/$env:BOOST_EXE-${{matrix.platform}}.exe"
          (New-Object System.Net.WebClient).DownloadFile($Url, "$env:TEMP\boost.exe")
          Start-Process -Wait -FilePath "$env:TEMP\boost.exe" "/SILENT","/SP-","/SUPPRESSMSGBOXES","/DIR=$BOOST_PATH"

          # Set the BOOST_ROOT variable
          echo "BOOST_ROOT=$BOOST_PATH" >> $env:GITHUB_ENV

      - name: Checkout
        uses: actions/checkout@v2

      - name: Configuration
        run: |
          cmake -E remove_directory build
          cmake -B build -S . -DGTSAM_BUILD_EXAMPLES_ALWAYS=OFF -DBOOST_ROOT="${env:BOOST_ROOT}" -DBOOST_INCLUDEDIR="${env:BOOST_ROOT}\boost\include" -DBOOST_LIBRARYDIR="${env:BOOST_ROOT}\lib"

      - name: Build
        run: |
          # Since Visual Studio is a multi-generator, we need to use --config
          # https://stackoverflow.com/a/24470998/1236990
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target gtsam
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target gtsam_unstable
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target wrap

          # Run GTSAM tests
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.base
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.basis
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.discrete
          #cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.geometry
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.inference
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.linear
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.navigation
          #cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.nonlinear
          #cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.sam
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.sfm
          #cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.slam
          cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.symbolic

          # Run GTSAM_UNSTABLE tests
          #cmake --build build -j 4 --config ${{ matrix.build_type }} --target check.base_unstable
          
