name: Intel OneAPI build

on:
  push:
    branches-ignore:
      - 'coverity'
  pull_request:

jobs:
  build-deps:

    runs-on: ubuntu-latest
    
    strategy:
      matrix:
        compiler: [ intel, mpi ]
        hdf5: [ V112 ]
        netcdf: [ 4.8.1 ]
        cgns: [ 4.2.0 ]

    defaults:
      run:
        shell: bash --noprofile --norc {0}

    steps:
    - uses: actions/checkout@v2
    - name: setup repo
      run: |
        wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
        sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
        rm GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
        sudo echo "deb https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list
        sudo apt-get update
    - name: install
      run: |
        sudo apt-get install -y intel-oneapi-common-vars
        sudo apt-get install -y intel-oneapi-compiler-dpcpp-cpp-and-cpp-classic 
        sudo apt-get install -y intel-oneapi-compiler-fortran
        sudo apt-get install -y intel-oneapi-mpi 
        sudo apt-get install -y intel-oneapi-mpi-devel 
        sudo apt-get install -y intel-oneapi-mkl 
        sudo apt-get install -y ninja-build 

    - name: Install System dependencies
      shell: bash -l {0}
      run: sudo apt update && sudo apt install -y libaec-dev zlib1g-dev automake autoconf libcurl4-openssl-dev libjpeg-dev wget curl bzip2 m4 flex bison cmake libzip-dev

      ###
      # Installing TPL
      ###
    - name: Cache TPL-${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}
      id: cache-TPL
      uses: actions/cache@v2
      with:
        path: ~/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}
        key: TPL-v4-${{ runner.os }}-${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}

    - name: Build TPL-${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}
      if: steps.cache-TPL.outputs.cache-hit != 'true'
      run: |
        echo $HOME
        set -x
        source /opt/intel/oneapi/setvars.sh
        printenv >> $GITHUB_ENV
        COMPILER=${{ matrix.compiler }} H5VERSION=${{ matrix.hdf5}} GNU_PARALLEL=OFF INSTALL_PATH=${HOME}/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }} ./install-tpl.sh
        ls ${HOME} && ls ${HOME}/environments && ls ${HOME}/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }} && ls ${HOME}/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}/lib

  seacas-build:
  
    needs: build-deps
    runs-on: ubuntu-latest

    strategy:
      matrix:
        compiler: [ intel, mpi ]
        hdf5: [ V112 ]
        netcdf: [ 4.8.1 ]
        cgns: [ 4.2.0 ]
    steps:

      - uses: actions/checkout@v2
      - name: setup repo
        run: |
          wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
          sudo apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
          rm GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB
          sudo echo "deb https://apt.repos.intel.com/oneapi all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list
          sudo apt-get update
      - name: install
        run: |
          sudo apt-get install -y intel-oneapi-common-vars
          sudo apt-get install -y intel-oneapi-compiler-dpcpp-cpp-and-cpp-classic 
          sudo apt-get install -y intel-oneapi-compiler-fortran
          sudo apt-get install -y intel-oneapi-mpi 
          sudo apt-get install -y intel-oneapi-mpi-devel 
          sudo apt-get install -y intel-oneapi-mkl 
          sudo apt-get install -y ninja-build 

      - name: Install System dependencies
        shell: bash -l {0}
        run: sudo apt update && sudo apt install -y libaec-dev zlib1g-dev automake autoconf libcurl4-openssl-dev libjpeg-dev wget curl bzip2 m4 flex bison cmake libzip-dev

      - name: Fetch TPL Cache
        id: cache-TPL
        uses: actions/cache@v2
        with:
          path: ~/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}
          key: TPL-v4-${{ runner.os }}-${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}

      - name: Check Cache
        shell: bash -l {0}
        run: ls ${HOME} && ls ${HOME}/environments && ls ${HOME}/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }} && ls ${HOME}/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }}/lib

###
# Configure and build
###
      - name: Run cmake
        shell: bash -l {0}
        run: |
          echo $HOME
          mkdir build
          cd build
          source /opt/intel/oneapi/setvars.sh
          printenv >> $GITHUB_ENV
          NUMPROCS=2 COMPILER=${{ matrix.compiler }} INSTALL_PATH=${HOME}/environments/${{ matrix.compiler }}-${{ matrix.hdf5 }}-${{ matrix.netcdf }}-${{ matrix.cgns }} bash ../cmake-config

      - name: Build 
        shell: bash -l {0}
        run: |
          cd build 
          make -j 4
        if: ${{ success() }}

      - name: Install
        shell: bash -l {0}
        run: |
          cd build 
          make -j 4 install
        if: ${{ success() }}

      - name: Run Tests
        shell: bash -l {0}
        run: |
          cd build
          ctest -j 4 --output-on-failure
        if: ${{ success() }}

#    - name: Compile
#   run: |
#     source /opt/intel/oneapi/setvars.sh
#     printenv >> $GITHUB_ENV
#     ifort test.f90 -o test_program
#     mpiifort test2.f90 -o mpitest

# - name: "Test the code"
#   run: |
#     echo "Running the tests using the Intel oneapi fortran compiler"
#     cd tests/test01
#     mpirun -np 1  $HOME/work/reponame/reponame/mpitest
#     ./$HOME/work/reponame/reponame/test_program
