name: hdf4 dev CMake CI

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

permissions:
  contents: read

jobs:

  # A workflow that builds the library and runs all the tests
  CMake_build_and_test:
    strategy:
      # The current matrix has one dimensions:
      #
      # * config name
      #
      # Most configuration information is added via the 'include' mechanism,
      # which will append the key-value pairs in the configuration where the
      # names match.
      matrix:
        name:
          - "Windows MSVC"
          - "Ubuntu gcc"
          - "MacOS Clang"

        # This is where we list the bulk of the options for each configuration.
        # The key-value pair values are usually appropriate for being CMake.

        include:

          # Windows w/ MSVC + CMake
          #
          # No Fortran or parallel
          - name: "Windows MSVC"
            os: windows-latest
            toolchain: ""
            fortran: OFF
            java: ON
            docs: ON
            localjpeg: OFF
            locallibaec: OFF
            localzlib: OFF
            cacheinit: "-C $GITHUB_WORKSPACE/config/cmake/cacheinit.cmake"
            generator: "-G \"Visual Studio 17 2022\" -A x64"
            run_tests: true

          # Linux (Ubuntu) w/ gcc + CMake
          #
          - name: "Ubuntu gcc"
            os: ubuntu-latest
            fortran: OFF
            java: ON
            docs: ON
            localjpeg: OFF
            locallibaec: OFF
            localzlib: OFF
            toolchain: "-DCMAKE_TOOLCHAIN_FILE=$GITHUB_WORKSPACE/config/toolchain/gcc.cmake"
            cacheinit: "-C $GITHUB_WORKSPACE/config/cmake/cacheinit.cmake"
            generator: "-G Ninja"
            run_tests: true

          # MacOS w/ Clang + CMake
          #
          - name: "MacOS Clang"
            os: macos-latest
            fortran: OFF
            java: ON
            docs: ON
            localjpeg: OFF
            locallibaec: OFF
            localzlib: OFF
            toolchain: "-DCMAKE_TOOLCHAIN_FILE=$GITHUB_WORKSPACE/config/toolchain/clang.cmake"
            cacheinit: "-C $GITHUB_WORKSPACE/config/cmake/cacheinit.cmake"
            generator: "-G Ninja"
            run_tests: true

    # Sets the job's name from the properties
    name: "${{ matrix.name }}-${{ inputs.build_mode }}-NC=${{ inputs.netcdf }}"

    # Don't run the action if the commit message says to skip CI
    if: "!contains(github.event.head_commit.message, 'skip-ci')"

    # The type of runner that the job will run on
    runs-on: ${{ matrix.os }}

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      #Useful for debugging
      - name: Dump matrix context
        run: echo '${{ toJSON(matrix) }}'

      - name: Install CMake Dependencies (Linux)
        run: |
           sudo apt-get update
           sudo apt-get install -y ninja-build libtirpc-dev graphviz
        if: matrix.os == 'ubuntu-latest'

      - name: Install Dependencies (Windows)
        run: choco install ninja
        if: matrix.os == 'windows-latest'

      - name: Install Dependencies (macOS)
        run: brew install ninja
        if: ${{ matrix.os == 'macos-latest' }}

      - name: Install Dependencies
        uses: ssciwr/doxygen-install@v1
        with:
          version: "1.9.7"

      - name: Set up JDK 19
        uses: actions/setup-java@v4
        with:
          java-version: '19'
          distribution: 'temurin'

      - name: Set environment for MSVC (Windows)
        run: |
          # Set these environment variables so CMake picks the correct compiler
          echo "CXX=cl.exe" >> $GITHUB_ENV
          echo "CC=cl.exe" >> $GITHUB_ENV
        if:  matrix.os == 'windows-latest'

      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Get Sources
        uses: actions/checkout@v4.1.7

      # CMAKE CONFIGURE
      - name: CMake Configure
        run: |
          mkdir "${{ runner.workspace }}/build"
          cd "${{ runner.workspace }}/build"
          cmake -C $GITHUB_WORKSPACE/config/cmake/cacheinit.cmake \
            ${{ matrix.generator }} \
            --log-level=VERBOSE \
            -DCMAKE_BUILD_TYPE=${{ inputs.build_mode }} \
            ${{ matrix.toolchain }} \
            -DBUILD_SHARED_LIBS:BOOL=${{ inputs.shared }} \
            -DHDF4_BUILD_EXAMPLES:BOOL=ON \
            -DBUILD_JPEG_WITH_PIC:BOOL=ON \
            -DHDF4_ENABLE_NETCDF:BOOL=${{ inputs.netcdf }} \
            -DHDF4_BUILD_FORTRAN:BOOL=${{ matrix.fortran }} \
            -DHDF4_BUILD_JAVA:BOOL=${{ inputs.shared }} \
            -DHDF4_BUILD_DOC:BOOL=${{ matrix.docs }} \
            -DJPEG_USE_LOCALCONTENT:BOOL=${{ matrix.localjpeg }} \
            -DLIBAEC_USE_LOCALCONTENT:BOOL=${{ matrix.locallibaec }} \
            -DZLIB_USE_LOCALCONTENT:BOOL=${{ matrix.localzlib }} \
            -DHDF4_PACK_EXAMPLES:BOOL=ON \
            -DHDF4_PACKAGE_EXTLIBS:BOOL=ON \
            -DHDF4_PACK_MACOSX_DMG:BOOL=OFF \
            $GITHUB_WORKSPACE
        shell: bash

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

      # RUN TESTS
      - name: CMake Run Tests
        run: ctest . --parallel 2 -C ${{ inputs.build_mode }} -V
        working-directory: ${{ runner.workspace }}/build
        if: ${{ (matrix.run_tests) && (matrix.name != 'Ubuntu mingw CMake') }}

      - name: CMake Run Package
        run: cpack -C ${{ inputs.build_mode }} -V
        working-directory: ${{ runner.workspace }}/build

#      - name: CMake Run Package (Mac_latest)
#        run: cpack -C ${{ inputs.build_mode }} -G STGZ -V
#        if: ${{ (matrix.os == 'macos-latest') }}

      - name: List files in the space
        run: |
              ls -l ${{ runner.workspace }}/build

      # Save files created by ctest script
      - name: Save published binary (Windows)
        uses: actions/upload-artifact@v4
        with:
              name: zip-vs2022_cl-${{ inputs.build_mode }}-binary
              path: ${{ runner.workspace }}/build/HDF-*-win64.zip
              if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
        if:  ${{ (matrix.os == 'windows-latest') && (matrix.name != 'Ubuntu mingw CMake') && (inputs.netcdf == 'true') && (inputs.shared == 'true') }}

      - name: Save published binary (linux)
        uses: actions/upload-artifact@v4
        with:
              name: tgz-ubuntu-2404_gcc-${{ inputs.build_mode }}-binary
              path: ${{ runner.workspace }}/build/HDF-*-Linux.tar.gz
              if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
        if:  ${{ (matrix.os == 'ubuntu-latest') && (matrix.name != 'Ubuntu mingw CMake') && (inputs.netcdf == 'true') && (inputs.shared == 'true') }}

      - name: Save published binary (Mac_latest)
        uses: actions/upload-artifact@v4
        with:
              name: tgz-macos14_clang-${{ inputs.build_mode }}-binary
              path: ${{ runner.workspace }}/build/HDF-*-Darwin.tar.gz
              if-no-files-found: error # 'warn' or 'ignore' are also available, defaults to `warn`
        if: ${{ (matrix.os == 'macos-latest') && (matrix.name != 'Ubuntu mingw CMake') && (inputs.netcdf == 'true') && (inputs.shared == 'true') }}
