## Copyright 2009 Intel Corporation
## SPDX-License-Identifier: Apache-2.0

name: Release

on:
  push:
    branches:
      - devel
      - '**release**'
      - master
  workflow_dispatch:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions: read-all

jobs:

### DOC JOBS ###

  pdf:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      image: ospray/docker-images:docu
      submodules: true
      cmd: |
        cd doc
        git clone --depth 1 -b release-2.12.x https://${RENDERKIT_GITHUB_TOKEN}@github.com/intel-innersource/documentation.graphics.renderkit.ospray-website.git ospray-doc
        make pdf
        make checklinks
      artifact-out: pdf
      artifact-path: readme.pdf

### BUILD JOBS ###

  release-linux:
    needs: pdf
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    secrets: inherit
    with:
      image: rockylinux:8
      env-from-files: .github/workflows/dpcpp.env
      cmd: |
        module load cmake/3.25.3
        module load mpi/mpich-x86_64
        export CC=clang
        export CXX=clang++
        scripts/release/linux.sh
        cp build_release/*.gz $STORAGE_PATH/packages/renderkit/ || true
      artifact-in: pdf
      artifact-out: release-linux
      artifact-path: build_release/*.gz

  release-linux-arm:
    needs: pdf
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/perf.yml@main
    secrets: inherit
    with:
      runs-on: '[ "Linux", "ARM64" ]'
      env-from-files: .github/workflows/dpcpp.env
      cmd: |
        scripts/release/linux.sh -DBUILD_GLFW=OFF -DBUILD_TBB_FROM_SOURCE=ON -DBUILD_EMBREE_FROM_SOURCE=ON -DBUILD_OPENVKL_FROM_SOURCE=ON -DBUILD_OIDN_FROM_SOURCE=ON
        cp build_release/*.gz $STORAGE_PATH/packages/renderkit/ || true
      artifact-in: pdf
      artifact-out: release-linux-arm
      artifact-path: build_release/*.gz

  release-osx:
    needs: pdf
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    with:
      runs-on: '["sign", "x86_64"]'
      cmd: |
        export CC=clang
        export CXX=clang++
        scripts/release/macosx.sh
        cp build_release/*.zip $STORAGE_PATH/packages/renderkit/ || true
      artifact-in: pdf
      artifact-out: release-osx
      artifact-path: build_release/*.zip

  release-osx-arm:
    needs: pdf
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    with:
      runs-on: '["arm", "build"]'
      cmd: |
        scripts/release/macosx.sh -DBUILD_TBB_FROM_SOURCE=ON -DBUILD_OPENVKL_FROM_SOURCE=ON
        cp build_release/*.zip $STORAGE_PATH/packages/renderkit/ || true
      artifact-in: pdf
      artifact-out: release-osx-arm
      artifact-path: build_release/*.zip

  release-windows:
    needs: pdf
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    secrets: inherit
    with:
      runs-on: '["build", "mpi"]'
      env-from-files: .github/workflows/dpcpp.env
      cmd: |
        $env:SYCL_BUNDLE_ROOT = $env:DPCPP_ROOT
        # disable warnings which lead to excessively large log files
        $env:CXXFLAGS = '-w'
        $env:CFLAGS = '-w'
        $exitCode = 0 # keep job status to properly fail
        scripts\release\win.ps1 -G Ninja -DCMAKE_CXX_COMPILER=clang-cl -DCMAKE_C_COMPILER=clang-cl
        if ($LastExitCode) { $exitCode++ }
        & $env:SIGN_FILE_WINDOWS ospray*.msi
        if ($LastExitCode) { $exitCode++ }
        cp (Get-ChildItem *.zip | Select-Object -Expand FullName) $env:STORAGE_PATH/packages/renderkit/
        cp (Get-ChildItem *.msi | Select-Object -Expand FullName) $env:STORAGE_PATH/packages/renderkit/
        exit $exitCode
      artifact-in: pdf
      artifact-out: release-windows
      artifact-path: "build_release/ospray*.zip build_release/ospray*.msi"


### SCAN JOBS ###

  static-analysis:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/static_analysis.yml@main
    with:
      coverity: true
      project: OSPRay
      prebuild: >
        module load impi/latest &&
        echo "Configure dependencies" &&
        cmake -L -S scripts/superbuild -B build_deps -DBUILD_DEPENDENCIES_ONLY=ON -DBUILD_EMBREE_FROM_SOURCE=ON -DBUILD_OPENVKL_FROM_SOURCE=ON -DBUILD_OIDN_FROM_SOURCE=ON -DBUILD_OSPRAY_MODULE_MPI=ON -DINSTALL_IN_SEPARATE_DIRECTORIES=OFF -DBUILD_ISA_AVX512=OFF &&
        echo "Build dependencies" &&
        cmake --build build_deps &&
        INSTALL_DIR=$(pwd)/build_deps/install &&
        export CMAKE_PREFIX_PATH=${INSTALL_DIR} &&
        echo "Configure OSPRay" &&
        cmake -L -S . -B build -DTBB_ROOT=$INSTALL_DIR -DOSPRAY_MODULE_DENOISER=ON -DOSPRAY_MODULE_BILINEAR_PATCH=ON -DOSPRAY_MODULE_MPI=ON &&
        echo "Done: Configure OSPRay"
      build: cmake --build build

  binary-analysis:
    needs:
      - release-linux
      - release-osx
      - release-windows
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/binary_analysis.yml@main
    with:
      project: ospray
      artifact-in-linux: release-linux
      artifact-in-macos: release-osx
      artifact-in-windows: release-windows
      path: build_release/ospray-*

  binary-analysis-arm:
    needs:
      - release-linux-arm
      - release-osx-arm
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/binary_analysis.yml@main
    with:
      project: ospray
      artifact-in-linux: release-linux-arm
      artifact-in-macos: release-osx-arm
      path: build_release/ospray-*
      artifact-out-name: binary-analysis-arm

  antivirus-scan:
    needs:
      - release-linux
      - release-osx
      - release-windows
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/antivirus_scan.yml@main
    with:
      project: ospray
      artifact-in-linux: release-linux
      artifact-in-macos: release-osx
      artifact-in-windows: release-windows
      path: build_release/ospray-*

  antivirus-scan-arm:
    needs:
      - release-linux-arm
      - release-osx-arm
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/antivirus_scan.yml@main
    with:
      project: ospray
      artifact-in-linux: release-linux-arm
      artifact-in-macos: release-osx-arm
      path: build_release/ospray-*
      artifact-out-name: antivirus-scan-arm

### TEST JOBS ###

  test-rocky-cpu:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      image: rockylinux:8
      cmd: |
        module load cmake/3.15.2
        tar -xzf build_release/*.gz
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX
      artifact-in: release-linux

  test-rocky-mpi-cpu:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      image: rockylinux:8
      cmd: |
        module load cmake/3.15.2
        module load mpi/mpich-x86_64
        tar -xzf build_release/*.gz
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX SKIP_CPU TEST_MPI
      artifact-in: release-linux

  test-ubuntu-oidnsycl:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    secrets: inherit
    with:
      timeout-minutes: 100
      image: ubuntu:22.04
      env-from-files: .github/workflows/gfx-ubuntu22.env
      options: --device=/dev/dri:/dev/dri
      runs-on: '"dg2"'
      cmd: |
        module load cmake/3.15.2
        tar -xzf build_release/*.gz
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        export OIDN_DEFAULT_DEVICE=sycl
        export OIDN_VERBOSE=2
        mkdir build_regression_tests
        cd build_regression_tests
        cmake -G Ninja -D OSPRAY_TEST_ISA=AVX2 $GITHUB_WORKSPACE/test_image_data
        cmake --build . --target ospray_test_data
        ospTestSuite --baseline-dir=regression_test_baseline --gtest_filter="DenoiserOp.DenoiserOp"
      artifact-in: release-linux

  test-ubuntu-cpu:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      image: ubuntu:22.04
      cmd: |
        tar -xzf build_release/*.gz
        export OIDN_VERBOSE=2
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX
      artifact-in: release-linux

  test-ubuntu-mpi-cpu:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      image: ubuntu:22.04
      cmd: |
        tar -xzf build_release/*.gz
        update-alternatives --set mpi-x86_64-linux-gnu /usr/include/x86_64-linux-gnu/mpich
        update-alternatives --set mpirun /usr/bin/mpirun.mpich
        OSPDIR=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/)
        ln -s /usr/lib/x86_64-linux-gnu/libmpich.so.12 $OSPDIR/lib/libmpi.so.12 # XXX find better way
        ln -s /usr/lib/x86_64-linux-gnu/libmpichcxx.so.12 $OSPDIR/lib/libmpicxx.so.12
        export OIDN_VERBOSE=2
        export PATH=$OSPDIR/bin:$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX SKIP_CPU TEST_MPI
      artifact-in: release-linux

  test-ubuntu-dg2:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      timeout-minutes: 100
      image: ubuntu:22.04
      runs-on: '"dg2"'
      env-from-files: .github/workflows/gfx-ubuntu22.env
      options: --device=/dev/dri:/dev/dri
      cmd: |
        tar -xzf build_release/*.gz
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_gpu_tests.sh $GITHUB_WORKSPACE
      artifact-in: release-linux

  test-ubuntu-mpi-dg2:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      timeout-minutes: 100
      image: ubuntu:22.04
      runs-on: '"dg2"'
      env-from-files: .github/workflows/gfx-ubuntu22.env
      options: --device=/dev/dri:/dev/dri
      cmd: |
        tar -xzf build_release/*.gz
        update-alternatives --set mpi-x86_64-linux-gnu /usr/include/x86_64-linux-gnu/mpich
        update-alternatives --set mpirun /usr/bin/mpirun.mpich
        OSPDIR=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/)
        ln -s /usr/lib/x86_64-linux-gnu/libmpich.so.12 $OSPDIR/lib/libmpi.so.12 # XXX find better way
        ln -s /usr/lib/x86_64-linux-gnu/libmpichcxx.so.12 $OSPDIR/lib/libmpicxx.so.12
        export OIDN_VERBOSE=2
        export PATH=$OSPDIR/bin:$PATH
        scripts/tests/run_gpu_tests.sh $GITHUB_WORKSPACE SKIP_GPU TEST_MPI
      artifact-in: release-linux

  test-ubuntu-pvc:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      timeout-minutes: 100
      image: ubuntu:22.04
      runs-on: '"pvc"'
      env-from-files: .github/workflows/gfx-ubuntu22.env
      options: --device=/dev/dri:/dev/dri
      cmd: |
        tar -xzf build_release/*.gz
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_gpu_tests.sh $GITHUB_WORKSPACE
      artifact-in: release-linux

  test-ubuntu-mpi-pvc:
    needs: release-linux
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      timeout-minutes: 100
      image: ubuntu:22.04
      runs-on: '"pvc"'
      env-from-files: .github/workflows/gfx-ubuntu22.env
      options: --device=/dev/dri:/dev/dri
      cmd: |
        tar -xzf build_release/*.gz
        update-alternatives --set mpi-x86_64-linux-gnu /usr/include/x86_64-linux-gnu/mpich
        update-alternatives --set mpirun /usr/bin/mpirun.mpich
        OSPDIR=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/)
        ln -s /usr/lib/x86_64-linux-gnu/libmpich.so.12 $OSPDIR/lib/libmpi.so.12 # XXX find better way
        ln -s /usr/lib/x86_64-linux-gnu/libmpichcxx.so.12 $OSPDIR/lib/libmpicxx.so.12
        export OIDN_VERBOSE=2
        export PATH=$OSPDIR/bin:$PATH
        scripts/tests/run_gpu_tests.sh $GITHUB_WORKSPACE SKIP_GPU TEST_MPI
      artifact-in: release-linux

  test-ubuntu-cpu-arm:
    needs: release-linux-arm
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/perf.yml@main
    with:
      runs-on: '[ "Linux", "ARM64" ]'
      cmd: |
        tar -xzf build_release/*.gz
        export OIDN_VERBOSE=2
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX
      artifact-in: release-linux-arm

  test-ubuntu-mpi-cpu-arm:
    needs: release-linux-arm
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/perf.yml@main
    with:
      runs-on: '[ "Linux", "ARM64" ]'
      cmd: |
        tar -xzf build_release/*.gz
        export OIDN_VERBOSE=2
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX SKIP_CPU TEST_MPI
      artifact-in: release-linux-arm

  test-osx-avx2:
    needs: release-osx
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    with:
      runs-on: '["x86_64", "build", "avx2"]'
      cmd: |
        unzip build_release/*.zip
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX2
      artifact-in: release-osx

  test-osx-avx512:
    needs: release-osx
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    with:
      runs-on: '["x86_64", "build", "avx512"]'
      cmd: |
        unzip build_release/*.zip
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX
      artifact-in: release-osx

  test-osx-arm:
    needs: release-osx-arm
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    with:
      runs-on: '["arm", "build"]'
      cmd: |
        unzip build_release/*.zip
        export PATH=$GITHUB_WORKSPACE/$(ls -d1 ./ospray-*/bin):$PATH
        scripts/tests/run_tests.sh $GITHUB_WORKSPACE AVX512SKX
      artifact-in: release-osx-arm

  test-osx-signing:
    needs: release-osx
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    secrets: inherit
    with:
      runs-on: '["sign", "x86_64" ]'
      cmd: |
        unzip build_release/*.zip
      sign-path-verify: ospray-*
      artifact-in: release-osx

  test-osx-signing-arm:
    needs: release-osx-arm
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/macos.yml@main
    secrets: inherit
    with:
      runs-on: '"arm"'
      cmd: |
        unzip build_release/*.zip
      sign-path-verify: ospray-*
      artifact-in: release-osx-arm

  test-windows-signing:
    needs: release-windows
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      cmd: |
        $exitCode = 0 # keep job status to properly fail
        Expand-Archive build_release/*.zip -DestinationPath .
        if ($LastExitCode) { $exitCode++ }
        $msiFile = (Get-ChildItem build_release/*.msi | Select-Object -Expand FullName)
        Start-Process msiexec "/a $msiFile /qn TARGETDIR=$env:GITHUB_WORKSPACE" -Wait
        if ($LastExitCode) { $exitCode++ }
        & $env:SIGN_FILE_WINDOWS -t build_release\*.msi (Get-ChildItem "Intel\OSPRay*\bin\*" | Select-Object -Expand FullName) (Get-ChildItem "ospray-*\bin\*" | Select-Object -Expand FullName)
        if ($LastExitCode) { $exitCode++ }
        exit $exitCode
      artifact-in: release-windows

  test-windows-cpu:
    needs: release-windows
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      runs-on: '"avx512"'
      cmd: |
        $exitCode = 0 # keep job status to properly fail
        Expand-Archive build_release/*.zip -DestinationPath .
        if ($LastExitCode) { $exitCode++ }
        $env:Path += ";" + (Get-ChildItem ospray-*\bin | Select-Object -Expand FullName)
        scripts\tests\run_tests.ps1 $env:GITHUB_WORKSPACE AVX512SKX
        if ($LastExitCode) { $exitCode++ }
        exit $exitCode
      artifact-in: release-windows

  test-windows-mpi-cpu:
    needs: release-windows
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      runs-on: '["avx512", "mpi"]'
      cmd: |
        $exitCode = 0 # keep job status to properly fail
        Expand-Archive build_release/*.zip -DestinationPath .
        if ($LastExitCode) { $exitCode++ }
        $env:Path += ";" + (Get-ChildItem ospray-*\bin | Select-Object -Expand FullName)
        scripts\tests\run_tests.ps1 $env:GITHUB_WORKSPACE AVX512SKX SKIP_CPU TEST_MPI
        if ($LastExitCode) { $exitCode++ }
        exit $exitCode
      artifact-in: release-windows

  test-windows-oidnsycl:
    needs: release-windows
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    secrets: inherit
    with:
      timeout-minutes: 100
      runs-on: '"dg2"'
      env-from-files: .github/workflows/gfx-windows.env
      cmd: |
        $exitCode = 0 # keep job status to properly fail
        Expand-Archive build_release/*.zip -DestinationPath .
        if ($LastExitCode) { $exitCode++ }
        $env:Path += ";" + (Get-ChildItem ospray-*\bin | Select-Object -Expand FullName)
        $env:OIDN_DEFAULT_DEVICE="sycl"
        $env:OIDN_VERBOSE="2"
        md build_regression_tests
        cd build_regression_tests
        cmake -G Ninja -D OSPRAY_TEST_ISA=AVX2 $env:GITHUB_WORKSPACE\test_image_data
        if ($LastExitCode) { $exitCode++ }
        cmake --build . --config Release --target ospray_test_data
        if ($LastExitCode) { $exitCode++ }
        ospTestSuite.exe --baseline-dir=regression_test_baseline --gtest_filter="DenoiserOp.DenoiserOp"
        if ($LastExitCode) { $exitCode++ }
        exit $exitCode
      artifact-in: release-windows

  test-windows-dg2:
    needs: release-windows
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      timeout-minutes: 100
      runs-on: '"dg2"'
      env-from-files: .github/workflows/gfx-windows.env
      cmd: |
        $exitCode = 0 # keep job status to properly fail
        $env:Path += ";C:\Program Files (x86)\Intel\oneAPI\mpi\latest\libfabric\bin\utils;C:\Program Files (x86)\Intel\oneAPI\mpi\latest\libfabric\bin;C:\Program Files (x86)\Intel\oneAPI\mpi\latest\bin\release;C:\Program Files (x86)\Intel\oneAPI\mpi\latest\bin"
        Expand-Archive build_release/*.zip -DestinationPath .
        if ($LastExitCode) { $exitCode++ }
        $env:Path += ";" + (Get-ChildItem ospray-*\bin | Select-Object -Expand FullName)
        scripts\tests\run_gpu_tests.ps1 $env:GITHUB_WORKSPACE
        if ($LastExitCode) { $exitCode++ }
        exit $exitCode
      artifact-in: release-windows

  test-windows-mpi-dg2:
    needs: release-windows
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      timeout-minutes: 100
      runs-on: '"dg2"' # FIXME: mpi
      env-from-files: .github/workflows/gfx-windows.env
      cmd: |
        $exitCode = 0 # keep job status to properly fail
        $env:Path += ";C:\Program Files (x86)\Intel\oneAPI\mpi\latest\libfabric\bin\utils;C:\Program Files (x86)\Intel\oneAPI\mpi\latest\libfabric\bin;C:\Program Files (x86)\Intel\oneAPI\mpi\latest\bin\release;C:\Program Files (x86)\Intel\oneAPI\mpi\latest\bin"
        Expand-Archive build_release/*.zip -DestinationPath .
        if ($LastExitCode) { $exitCode++ }
        $env:Path += ";" + (Get-ChildItem ospray-*\bin | Select-Object -Expand FullName)
        scripts\tests\run_gpu_tests.ps1 $env:GITHUB_WORKSPACE SKIP_GPU TEST_MPI
        if ($LastExitCode) { $exitCode++ }
        exit $exitCode
      artifact-in: release-windows
