name: developer
on:
  push:
    branches:
    - develop
  pull_request:
    branches:
    - develop

# Cancel in-progress workflows when pushing to a branch
concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  developer:
    runs-on: ubuntu-24.04
    env:
      CC: gcc
      CXX: g++
    
    strategy:
      fail-fast: true
      matrix:
        config: ["asan", "code coverage", "memcheck", "docs", "format", "warning"]

    steps:
    - name: install-dependencies
      run: |
        sudo apt-get update
        sudo apt-get install libpng-dev zlib1g-dev libjpeg-dev libopenjp2-7-dev libaec-dev 

    - name: install-format-dependencies
      if: matrix.config == 'format'      
      run: |
        sudo apt-get install clang-format

    - name: install-docs-dependencies
      if: matrix.config == 'docs'      
      run: |
        sudo apt-get install doxygen

    - name: install-memory-dependencies
      if: matrix.config == 'memcheck'      
      run: |
        sudo apt-get install valgrind

    - name: install-code_coverage-dependencies
      if: matrix.config == 'code coverage'      
      run: |
        sudo apt-get install clang-format
        python3 -m pip install gcovr

    - name: "Build dependencies"
      uses: NOAA-EMC/ci-build-nceplibs@develop
      with:
        jasper-version: version-4.0.0

    - name: checkout
      uses: actions/checkout@v4
      with:
        path: g2c

    - name: cache-data
      id: cache-data
      uses: actions/cache@v4
      with:
        path: ~/data
        key: data-2

    - name: asan
      if: matrix.config == 'asan'
      run: |
        set -x
        export CC=gcc
        cmake -S g2c -B g2c/build \
          -DUSE_AEC=ON \
          -DJasper_ROOT=$GITHUB_WORKSPACE/nceplibs/jasper \
          -DBUILD_G2C=ON \
          -DLOGGING=On \
          -DPTHREADS=ON \
          -DFTP_TEST_FILES=ON \
          -DFTP_LARGE_TEST_FILES=OFF \
          -DTEST_FILE_DIR=/home/runner/data \
          -DCMAKE_BUILD_TYPE=Debug \
          -DCMAKE_C_FLAGS="-g"
        cmake --build g2c/build --parallel 2 --verbose
        ctest --test-dir g2c/build --verbose --output-on-failure --rerun-failed
        
    - name: warning
      if: matrix.config == 'warning'
      run: |
        set -x
        export CC=gcc
        cmake -S g2c -B g2c/build \
          -DUSE_AEC=ON \
          -DJasper_ROOT=$GITHUB_WORKSPACE/nceplibs/jasper \
          -DBUILD_G2C=ON \
          -DLOGGING=On \
          -DPTHREADS=ON \
          -DFTP_TEST_FILES=ON \
          -DFTP_LARGE_TEST_FILES=OFF \
          -DTEST_FILE_DIR=/home/runner/data \
          -DCMAKE_BUILD_TYPE=Debug \
          -DCMAKE_C_FLAGS="-g"
        cmake --build g2c/build --verbose
        
    - name: code_coverage
      if: matrix.config == 'code coverage'
      run: |
        set -x
        export CC=gcc
        cmake -S g2c -B g2c/build \
          -DUSE_AEC=ON \
          -DJasper_ROOT=$GITHUB_WORKSPACE/nceplibs/jasper \
          -DBUILD_G2C=ON \
          -DLOGGING=On \
          -DPTHREADS=ON \
          -DFTP_TEST_FILES=ON \
          -DFTP_LARGE_TEST_FILES=OFF \
          -DTEST_FILE_DIR=/home/runner/data \
          -DCMAKE_BUILD_TYPE=Debug \
          -DCMAKE_C_FLAGS="-g"
        pwd
        cd g2c/build
        make VERBOSE=1
        ctest --verbose --output-on-failure --rerun-failed
        gcovr --root .. -v  --html-details --exclude tests --exclude CMakeFiles --print-summary -o test-coverage.html
        pwd
        ls -l
        ls -l src
        
    - name: memcheck
      if: matrix.config == 'memcheck'
      run: |
        set -x
        export LD_LIBRARY_PATH="/home/runner/jasper/lib:$LD_LIBRARY_PATH"
        cmake -S g2c -B g2c/build \
          -DUSE_AEC=ON \
          -DJasper_ROOT=$GITHUB_WORKSPACE/nceplibs/jasper \
          -DBUILD_G2C=ON \
          -DLOGGING=On \
          -DPTHREADS=ON \
          -DFTP_TEST_FILES=ON \
          -DTEST_FILE_DIR=/home/runner/data \
          -DCMAKE_BUILD_TYPE=Debug \
          -DCMAKE_C_FLAGS="-g"
        cmake --build g2c/build --parallel 2 --verbose
        ctest --test-dir g2c/build
        ls -l g2c/build
        
    - name: docs
      if: matrix.config == 'docs'
      run: |
        set -x
        export LD_LIBRARY_PATH="/home/runner/jasper/lib:$LD_LIBRARY_PATH"
        cmake -S g2c -B g2c/build \
          -DUSE_AEC=ON \
          -DJasper_ROOT=$GITHUB_WORKSPACE/nceplibs/jasper \
          -DBUILD_G2C=ON \
          -DENABLE_DOCS=On \
          -DCMAKE_BUILD_TYPE=Debug \
          -DCMAKE_C_FLAGS="-g"
        cmake --build g2c/build --parallel 2 --verbose
        ls -l g2c/build/docs/html
        
    - name: format
      if: matrix.config == 'format'
      run: |
        set -x
        cd g2c
        clang-format --version        
        find . -name '*.[ch]'|xargs clang-format -Werror --verbose --dry-run
        
    - name: populate-data
      if: steps.cache-data.outputs.cache-hit != 'true'
      run: |
        # create data dir and copy test data into it.
        # Prefer source test data (g2c/tests/data). Fall back to build/tests/data if present.
        mkdir -p ~/data
        if [ -d "$GITHUB_WORKSPACE/g2c/tests/data" ]; then
          cp -r $GITHUB_WORKSPACE/g2c/tests/data/* ~/data
        elif [ -d "$GITHUB_WORKSPACE/g2c/build/tests/data" ]; then
          cp -r $GITHUB_WORKSPACE/g2c/build/tests/data/* ~/data
        else
          echo "Warning: no test data directory found at either $GITHUB_WORKSPACE/g2c/tests/data or $GITHUB_WORKSPACE/g2c/build/tests/data"
        fi
        
    - name: upload-test-coverage
      if: matrix.config == 'code coverage'      
      uses: actions/upload-artifact@v4
      with:
        name: g2c-test-coverage
        path: |
          *.html
          *.css
    - uses: actions/upload-artifact@v4
      if: matrix.config == 'docs'
      with:
        name: docs
        path: |
          g2c/build/docs/html
