name: Linux/MacOS Build

on: [push, pull_request]

#env:

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        build_type : [ Release, Debug ]
        os : [ macos-latest, ubuntu-20.04 ]
        linalg : [netlib, vendor]
        include:
          - os: ubuntu-20.04
            cxx: /usr/bin/g++-9
          - os: macos-latest
            cxx: clang++

    name: "${{ matrix.os }}: ${{ matrix.cxx }} ${{ matrix.build_type }} linalg=${{ matrix.linalg }}"
    runs-on: ${{ matrix.os }}
    env:
      CXX : ${{ matrix.cxx }}
      DOXYGEN_VERSION : 1.9.2
      CCACHE_DIR : ${{github.workspace}}/build/.ccache
      CCACHE_COMPRESS : true
      CCACHE_COMPRESSLEVEL : 6
      BUILD_CONFIG : >
        -G Ninja
        -DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
        -DCMAKE_PREFIX_PATH=/usr/local/opt/bison
        -DBUILD_SHARED_LIBS=OFF
        -DMPIEXEC_PREFLAGS='--bind-to;none;--allow-run-as-root'
        -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/install

    steps:
    - uses: actions/checkout@v2

    - name: Create Build Environment
      # Some projects don't allow in-source building, so create a separate build directory
      # We'll use this as our working directory for all subsequent commands
      run: cmake -E make_directory ${{github.workspace}}/build

    - name: Install prerequisite MacOS packages
      if: ${{ matrix.os == 'macos-latest' }}
      run: |
        brew install ninja gcc@10 boost eigen open-mpi bison ccache
        # install Netlib if want generic linalg
        if [ "${{matrix.linalg}}" = "netlib" ]; then
          brew install lapack
          echo "BLAS_PREFERENCE_LIST=ReferenceBLAS" >> $GITHUB_ENV
        else
          echo "BLAS_PREFERENCE_LIST=Accelerate" >> $GITHUB_ENV
        fi

    - name: Install prerequisites Ubuntu packages
      if: ${{ matrix.os == 'ubuntu-20.04' }}
      run: |
        sudo apt-get update
        sudo apt-get install ninja-build g++-9 liblapack-dev libboost-dev libboost-serialization-dev libeigen3-dev openmpi-bin libopenmpi-dev libtbb-dev ccache
        if [ "${{matrix.build_type}}" = "Release" ]; then
          sudo apt-get install libclang1-9 libclang-cpp9 graphviz fonts-liberation
          cd ${{github.workspace}}/build
          # If we fail getting doxygen-${DOXYGEN_VERSION}.linux.bin.tar.gz from sourceforge,
          # use EFV's gdrive mirror of 1.9.2 to work around the unreliable sourceforge
          # the sharing link: https://drive.google.com/file/d/16GXpH4YOEUxGXQrXOKdAIibhdfzATY0d/view?usp=sharing
          wget https://downloads.sourceforge.net/project/doxygen/rel-${DOXYGEN_VERSION}/doxygen-${DOXYGEN_VERSION}.linux.bin.tar.gz || wget -4 --no-check-certificate -O doxygen-${DOXYGEN_VERSION}.linux.bin.tar.gz "https://drive.google.com/uc?export=download&id=16GXpH4YOEUxGXQrXOKdAIibhdfzATY0d"
          tar xzf ./doxygen-${DOXYGEN_VERSION}.linux.bin.tar.gz
          export DOXYGEN_DIR=${{github.workspace}}/build/doxygen-${DOXYGEN_VERSION}
          ${DOXYGEN_DIR}/bin/doxygen --version
          # doxygen should be in PATH in subsequent steps
          echo "${DOXYGEN_DIR}/bin" >> $GITHUB_PATH
        fi
        # install MKL if want vendor linalg
        if [ "${{matrix.linalg}}" = "vendor" ]; then
          wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB 
          sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB
          rm GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB
          sudo sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list'
          sudo apt-get update
          sudo apt-get install intel-mkl-64bit-2019.4-070
          echo "BLAS_PREFERENCE_LIST=IntelMKL" >> $GITHUB_ENV
          echo "MKLROOT=/opt/intel/mkl" >> $GITHUB_ENV
        else
          echo "BLAS_PREFERENCE_LIST=ReferenceBLAS" >> $GITHUB_ENV
        fi

    - name: Prepare ccache timestamp
      id: ccache_cache_timestamp
      shell: cmake -P {0}
      run: |
        string(TIMESTAMP current_date "%Y-%m-%d-%H;%M;%S" UTC)
        message("::set-output name=timestamp::${current_date}")

    - name: Setup ccache cache files
      uses: actions/cache@v1.1.0
      with:
        path: ${{github.workspace}}/build/.ccache
        key: ${{ matrix.config.name }}-ccache-${{ steps.ccache_cache_timestamp.outputs.timestamp }}
        restore-keys: |
          ${{ matrix.config.name }}-ccache-

    - name: Configure CMake
      # Use a bash shell so we can use the same syntax for environment variable
      # access regardless of the host operating system
      shell: bash
      working-directory: ${{github.workspace}}/build
      # Note the current convention is to use the -S and -B options here to specify source 
      # and build directories, but this is only available with CMake 3.13 and higher.  
      # The CMake binaries on the Github Actions machines are (as of this writing) 3.12
      run: |
        cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBLAS_PREFERENCE_LIST=$BLAS_PREFERENCE_LIST $BUILD_CONFIG

    - name: Build
      working-directory: ${{github.workspace}}/build
      shell: bash
      # Execute the build.  You can specify a specific target with "--target <NAME>"
      run: ccache -p && ccache -z && cmake --build . && ccache -s

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

    - name: Install
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: cmake --build . --target install

    - name: Test Install Tree
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        cmake -S $GITHUB_WORKSPACE/doc/examples -B test_install -DCMAKE_PREFIX_PATH=${{github.workspace}}/install
        cmake --build test_install

    - name: Build+Deploy Dox
      if: ${{ matrix.os == 'ubuntu-20.04' && matrix.build_type == 'Release' && github.ref == 'refs/heads/master' }}
      working-directory: ${{github.workspace}}/build
      shell: bash
      run: |
        #echo "== deploying dox =="
        #git config --global user.email "eduard@valeyev.net"
        #git config --global user.name "GitHub Actions"
        cmake --build . --target html
        #git clone --depth=1 https://github.com/ValeevGroup/BTAS.git --branch gh-pages --single-branch btas-docs-current
        #git clone --depth=1 https://github.com/ValeevGroup/BTAS.git --branch gh-pages-template --single-branch btas-docs-template
