# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) Contributors to the OpenEXR Project.
#
# GitHub Actions workflow file
# https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions

name: Analysis

on:
  schedule:
    # Weekly Sunday build
    - cron: "0 0 * * 0"
  workflow_dispatch:

permissions:
  contents: read

jobs:

  # ---------------------------------------------------------------------------
  # SonarCloud static analysis
  # ---------------------------------------------------------------------------

  linux_sonarcloud:
    name: 'SonarCloud Linux CentOS 7 VFX CY2024 <GCC 11.2.1>'
    if: github.repository == 'AcademySoftwareFoundation/Imath'
    # GH-hosted VM. The build runs in CentOS 7 'container' defined below.
    runs-on: ubuntu-latest
    container:
      # DockerHub: https://hub.docker.com/u/aswf
      # Source: https://github.com/AcademySoftwareFoundation/aswf-docker
      image: aswf/ci-openexr:2026
    env:
      CXX: g++
      CC: gcc
    steps:
      # TODO: Remove this workaround following resolution of:
      #       https://github.com/AcademySoftwareFoundation/aswf-docker/issues/43
      - name: Setup container
        run: sudo rm -rf /usr/local/lib64/cmake/glew
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          fetch-depth: 50
      - name: Create build directories
        run: |
          mkdir _install
          mkdir _build
      - name: Configure
        run: |
          cmake .. \
                -DCMAKE_INSTALL_PREFIX=../_install \
                -DCMAKE_BUILD_TYPE=Release \
                -DCMAKE_CXX_STANDARD=17 \
                -DCMAKE_CXX_FLAGS="-g -O0 -fprofile-arcs -ftest-coverage" \
                -DCMAKE_CXX_OUTPUT_EXTENSION_REPLACE=ON \
                -DCMAKE_C_FLAGS="-g -O0 -fprofile-arcs -ftest-coverage" \
                -DCMAKE_C_OUTPUT_EXTENSION_REPLACE=ON \
                -DCMAKE_EXE_LINKER_FLAGS="-lgcov" \
                -DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
                -DBUILD_SHARED_LIBS='OFF' \
                -DPYTHON='ON'
        working-directory: _build
      - name: Build Imath with build-wrapper
        shell: bash
        run: |
          build-wrapper-linux-x86-64 --out-dir bw_output cmake --build . --target install --config Release
        working-directory: _build
      - name: Test
        run: |
          ctest -T Test \
                -C Release \
                --timeout 7200 \
                --output-on-failure \
                -VV
        working-directory: _build
      - name: Coverage
        run: share/ci/scripts/linux/run_gcov.sh
        shell: bash
      - name: Sonar-scanner
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
        run: sonar-scanner -X

  # ------------------------------------------------------------------------------
  #  Valgrind memcheck test
  # ------------------------------------------------------------------------------
  linux_valgrind:
    name: 'Valgrind Linux CentOS 7 VFX CY2024 <GCC 11.2.1>'
    if: github.repository == 'AcademySoftwareFoundation/Imath'
    # GH-hosted VM. The build runs in CentOS 7 'container' defined below.
    runs-on: ubuntu-latest
    container:
      # DockerHub: https://hub.docker.com/u/aswf
      # Source: https://github.com/AcademySoftwareFoundation/aswf-docker
      image: aswf/ci-openexr:2026
    env:
      CXX: g++
      CC: gcc
    steps:
      # TODO: Remove this workaround following resolution of:
      #       https://github.com/AcademySoftwareFoundation/aswf-docker/issues/43
      - name: Setup container
        run: sudo rm -rf /usr/local/lib64/cmake/glew
      - name: Checkout
        uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
        with:
          fetch-depth: 50
      - name: Create build directories
        run: |
          mkdir _install
          mkdir _build
        shell: bash
      - name: Install Dependencies
        run: |
          share/ci/scripts/linux/install_valgrind.sh 
        shell: bash
      - name: Configure
        run: |
          cmake .. \
                -DCMAKE_INSTALL_PREFIX=../_install \
                -DCMAKE_BUILD_TYPE=Release \
                -DCMAKE_CXX_STANDARD=17 \
                -DCMAKE_VERBOSE_MAKEFILE:BOOL='OFF' \
                -DBUILD_SHARED_LIBS='OFF' \
                -DPYTHON='ON'
        working-directory: _build
      - name: Build
        run: |
          cmake --build . \
                --target install \
                --config Release
        working-directory: _build
      - name: Valgrind memcheck tests
        run: |
          ctest -C Release \
                --timeout 50000 \
                --force-new-ctest-process \
                --test-action memcheck \
                --output-on-failure \
                -VV
        working-directory: _build
      - name: Valgrind memcheck test results
        run: |
          share/ci/scripts/linux/log_valgrind.sh _build
        shell: bash

