name: hdf4 CMake nvhpc

# Triggers the workflow on a call from another workflow
on:
  workflow_call:
    inputs:
      build_mode:
        description: "release vs. debug build"
        required: true
        type: string

permissions:
  contents: read

jobs:
  nvhpc_build_and_test:
    name: "nvhpc ${{ inputs.build_mode }}"
    runs-on: ubuntu-latest
    steps:
      - name: Get Sources
        uses: actions/checkout@v4.2.1

      - name: Install Dependencies
        shell: bash
        run: |
          sudo apt-get update
          sudo apt-get install ninja-build doxygen graphviz
          sudo apt install libssl3 libssl-dev libcurl4 libcurl4-openssl-dev
          sudo apt install -y libaec-dev zlib1g-dev wget curl bzip2 flex bison cmake libzip-dev openssl build-essential

      - name: Install NVHPC
        shell: bash
        run: |
          curl https://developer.download.nvidia.com/hpc-sdk/ubuntu/DEB-GPG-KEY-NVIDIA-HPC-SDK | sudo gpg --dearmor -o /usr/share/keyrings/nvidia-hpcsdk-archive-keyring.gpg
          echo 'deb [signed-by=/usr/share/keyrings/nvidia-hpcsdk-archive-keyring.gpg] https://developer.download.nvidia.com/hpc-sdk/ubuntu/amd64 /' | sudo tee /etc/apt/sources.list.d/nvhpc.list
          sudo apt-get update -y
          sudo apt-get install -y nvhpc-24-11
          echo "NVHPCSDK=/opt/nvidia/hpc_sdk" >> $GITHUB_ENV
          echo "OMPI_CC=/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/compilers/bin/nvc" >> $GITHUB_ENV
          echo "OMPI_FC=/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/compilers/bin/nvfortran" >> $GITHUB_ENV
          echo "CC=/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/compilers/bin/nvc" >> $GITHUB_ENV
          echo "FC=/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/compilers/bin/nvfortran" >> $GITHUB_ENV
          echo "LD_LIBRARY_PATH=/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/cuda/12.3/lib64:/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/compilers/lib" >> $GITHUB_ENV
          echo "DESTDIR=/tmp" >> $GITHUB_ENV

      - name: CMake Configure
        shell: bash
        run: |
          export PATH=/opt/nvidia/hpc_sdk/Linux_x86_64/24.11/compilers/bin:$PATH
          mkdir "${{ runner.workspace }}/build"
          cd "${{ runner.workspace }}/build"
          cmake -C $GITHUB_WORKSPACE/config/cmake/cacheinit.cmake \
            -G Ninja \
            --log-level=VERBOSE \
            -DCMAKE_BUILD_TYPE=${{ inputs.build_mode }} \
            -DJPEG_USE_LOCALCONTENT:BOOL=OFF \
            -DLIBAEC_USE_LOCALCONTENT:BOOL=OFF \
            -DZLIB_USE_LOCALCONTENT:BOOL=OFF \
            -DHDF4_BUILD_FORTRAN:BOOL=ON \
            -DHDF4_BUILD_JAVA:BOOL=OFF \
            $GITHUB_WORKSPACE

      - name: CMake Build
        shell: bash
        run: |
          cmake --build . --parallel 3 --config ${{ inputs.build_mode }}
        working-directory: ${{ runner.workspace }}/build

      - name: CMake Run Tests
        shell: bash
        run: |
          ctest . --parallel 2 -C ${{ inputs.build_mode }} -V
        working-directory: ${{ runner.workspace }}/build
