name: hdf4 Check Application Binary Interface (ABI)

# Triggers the workflow on a call from another workflow
on:
  workflow_call:
    inputs:
      use_tag:
        description: 'Release version tag'
        type: string
        required: false
        default: snapshot
      use_environ:
        description: 'Environment to locate files'
        type: string
        required: true
        default: snapshots
      file_base:
        description: "The common base name of the binary"
        required: true
        type: string
      file_ref:
        description: "The reference name for the release binary"
        required: true
        type: string

permissions:
  contents: read

jobs:
  check:
    runs-on: ubuntu-latest
    continue-on-error: true

    steps:
      - name: Install System dependencies
        run: |
          sudo apt update
          sudo apt install -q -y abi-compliance-checker abi-dumper
          sudo apt install -q -y japi-compliance-checker

      - name: Convert hdf4 reference name (Linux)
        id: convert-hdf4lib-refname
        run:  |
          FILE_DOTS=$(echo "${{ inputs.file_ref }}" | sed -r "s/([0-9]+)\_([0-9]+)\_([0-9]+)\-([0-9]+).*/\1\.\2\.\3\-\4/")
          echo "HDF4R_DOTS=$FILE_DOTS" >> $GITHUB_OUTPUT

      - uses: actions/checkout@v4.1.7

      - name: Get published binary (Linux)
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4.3.0
        with:
            name: tgz-ubuntu-2404_gcc-binary
            path: ${{ github.workspace }}

      - name: List files for the space (Linux)
        run: |
          ls -l ${{ github.workspace }}

      - name: Uncompress gh binary (Linux)
        run: tar -zxvf ${{ github.workspace }}/${{ inputs.file_base }}-ubuntu-2404_gcc.tar.gz

      - name: Uncompress hdf4 binary (Linux)
        run:  |
          cd "${{ github.workspace }}/hdf4"
          tar -zxvf ${{ github.workspace }}/hdf4/HDF-*-Linux.tar.gz --strip-components 1

      - name: List files for the HDF space (Linux)
        run: |
          ls -l ${{ github.workspace }}/hdf4
          ls -l ${{ github.workspace }}/hdf4/HDF_Group/HDF

      - name: set hdf4lib name
        id: set-hdf4lib-name
        run: |
          HDF4DIR=${{ github.workspace }}/hdf4/HDF_Group/HDF/
          FILE_NAME_HDF4=$(ls ${{ github.workspace }}/hdf4/HDF_Group/HDF)
          FILE_VERS=$(echo "$FILE_NAME_HDF4" | sed -r "s/([0-9]+\.[0-9]+\.[0-9]+).*/\1/")
          echo "HDF4_ROOT=$HDF4DIR$FILE_NAME_HDF4" >> $GITHUB_OUTPUT
          echo "HDF4_VERS=$FILE_VERS" >> $GITHUB_OUTPUT

      - name: Download reference version
        run: |
          mkdir "${{ github.workspace }}/hdf4R"
          cd "${{ github.workspace }}/hdf4R"
          #wget -q https://github.com/HDFGroup/hdf4/releases/download/hdf${{ inputs.file_ref }}/hdf${{ inputs.file_ref }}-ubuntu-2404_gcc.tar.gz
          #tar zxf hdf${{ inputs.file_ref }}-ubuntu-2404_gcc.tar.gz
          wget -q https://github.com/HDFGroup/hdf4/releases/download/hdf${{ inputs.file_ref }}/hdf${{ inputs.file_ref }}-ubuntu-2204_gcc.tar.gz
          tar zxf hdf${{ inputs.file_ref }}-ubuntu-2204_gcc.tar.gz

      - name: List files for the space (Linux)
        run: |
          ls -l ${{ github.workspace }}/hdf4R

      - name: Uncompress hdf4 reference binary (Linux)
        run:  |
          cd "${{ github.workspace }}/hdf4R"
          tar -zxvf ${{ github.workspace }}/hdf4R/hdf4/HDF-${{ steps.convert-hdf4lib-refname.outputs.HDF4R_DOTS }}-Linux.tar.gz --strip-components 1

      - name: List files for the HDFR space (Linux)
        run: |
          ls -l ${{ github.workspace }}/hdf4R
          ls -l ${{ github.workspace }}/hdf4R/HDF_Group/HDF

      - name: set hdf4lib reference name
        id: set-hdf4lib-refname
        run: |
          HDF4RDIR=${{ github.workspace }}/hdf4R/HDF_Group/HDF/
          FILE_NAME_HDF4R=$(ls ${{ github.workspace }}/hdf4R/HDF_Group/HDF)
          echo "HDF4R_ROOT=$HDF4RDIR$FILE_NAME_HDF4R" >> $GITHUB_OUTPUT
          echo "HDF4R_VERS=$FILE_NAME_HDF4R" >> $GITHUB_OUTPUT

      - name: List files for the lib spaces (Linux)
        run: |
          ls -l ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/lib
          ls -l ${{ steps.set-hdf4lib-refname.outputs.HDF4R_ROOT }}/lib

      - name: Run Java API report
        run: |
          japi-compliance-checker ${{ steps.set-hdf4lib-refname.outputs.HDF4R_ROOT }}/lib/jarhdf-${{ steps.convert-hdf4lib-refname.outputs.HDF4R_DOTS }}.jar ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/lib/jarhdf-${{ steps.set-hdf4lib-name.outputs.HDF4_VERS }}.jar

      - name: Run hdf ABI report
        run: |
          abi-dumper ${{ steps.set-hdf4lib-refname.outputs.HDF4R_ROOT }}/lib/libhdf.so -o ABI-0.dump -public-headers ${{ steps.set-hdf4lib-refname.outputs.HDF4R_ROOT }}/include
          abi-dumper ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/lib/libhdf.so -o ABI-1.dump -public-headers ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/include
          abi-compliance-checker -l ${{ inputs.file_base }}-hdf -old ABI-0.dump -new ABI-1.dump
        continue-on-error: true

      - name: Run mfhdf ABI report
        run: |
          abi-dumper ${{ steps.set-hdf4lib-refname.outputs.HDF4R_ROOT }}/lib/libmfhdf.so -o ABI-2.dump -public-headers ${{ steps.set-hdf4lib-refname.outputs.HDF4R_ROOT }}/include
          abi-dumper ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/lib/libmfhdf.so -o ABI-3.dump -public-headers ${{ steps.set-hdf4lib-name.outputs.HDF4_ROOT }}/include
          abi-compliance-checker -l ${{ inputs.file_base }}-mfhdf -old ABI-2.dump -new ABI-3.dump
        continue-on-error: true

      - name: Copy ABI reports
        run: |
          cp compat_reports/jarhdf/${{ steps.set-hdf4lib-refname.outputs.HDF4R_VERS }}_to_${{ steps.set-hdf4lib-name.outputs.HDF4_VERS }}/compat_report.html ${{ inputs.file_base }}-java_compat_report.html
          ls -l compat_reports/${{ inputs.file_base }}-hdf/X_to_Y
          cp compat_reports/${{ inputs.file_base }}-hdf/X_to_Y/compat_report.html ${{ inputs.file_base }}-hdf_compat_report.html
          ls -l compat_reports/${{ inputs.file_base }}-mfhdf/X_to_Y
          cp compat_reports/${{ inputs.file_base }}-mfhdf/X_to_Y/compat_report.html ${{ inputs.file_base }}-mfhdf_compat_report.html

      - name: List files for the report spaces (Linux)
        run: |
          ls -l compat_reports
          ls -l *.html

      - name: Publish ABI reports
        id: publish-abi-reports
        run: |
          mkdir "${{ runner.workspace }}/buildabi"
          mkdir "${{ runner.workspace }}/buildabi/hdf4"
          cp ${{ inputs.file_base }}-hdf_compat_report.html ${{ runner.workspace }}/buildabi/hdf4
          cp ${{ inputs.file_base }}-hdf_compat_report.html ${{ runner.workspace }}/buildabi/hdf4
          cp ${{ inputs.file_base }}-java_compat_report.html ${{ runner.workspace }}/buildabi/hdf4
          cd "${{ runner.workspace }}/buildabi"
          tar -zcvf ${{ inputs.file_base }}.html.abi.reports.tar.gz hdf4
        shell: bash

      - name: Save output as artifact
        uses: actions/upload-artifact@v4
        with:
          name: abi-reports
          path: |
            ${{ runner.workspace }}/buildabi/${{ inputs.file_base }}.html.abi.reports.tar.gz
