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

name: continuous

on: [push, workflow_dispatch]

jobs:

  ze_raytracing-ubuntu22_04-GCC:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
    with:
      image: embree/ubuntu:22.04
      runs-on: '[ "Linux", "docker", "build" ]'
      cmd: |
        mkdir build
        cd build
        cmake -G Ninja -D CMAKE_CXX_COMPILER=g++ -D CMAKE_C_COMPILER=gcc -D CMAKE_BUILD_TYPE=Release ..
        cmake --build . --target package

  ubuntu22_04-pkg-build:
     secrets: inherit
     uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
     with:
       image: gfx-build/testinstall-ubuntu22.04:18
       runs-on: '[ "Linux", "docker", "build" ]'
       artifact-out: ubuntu22_04-pkg-build
       artifact-path: intel-level-zero-gpu-raytracing_*.dsc intel-level-zero-gpu-raytracing_*.tar.xz intel-level-zero-gpu-raytracing_*amd64.deb intel-level-zero-gpu-raytracing_*amd64.buildinfo
       pre-cmd: |
        apt update
        eatmydata apt -y install git git-lfs
       cmd: |
         # Build source packages.
         mkdir intel-level-zero-gpu-raytracing
         cp -r * intel-level-zero-gpu-raytracing || true
         cd intel-level-zero-gpu-raytracing
         git clone -b v2021.6.0 https://github.com/oneapi-src/oneTBB.git tbb
         dpkg-buildpackage -j`nproc --all` --no-sign -S -d -rfakeroot
         cd ..
         # Install source package in intel-level-zero-gpu-raytracing-build dir.
         # Then we are going to build binaries from it.
         # Keep in mind that packages will be written to this dir.
         eatmydata apt-get build-dep -y ./intel-level-zero-gpu-raytracing_*.dsc
         dpkg-source -x ./intel-level-zero-gpu-raytracing*.dsc intel-level-zero-gpu-raytracing-build
         cd intel-level-zero-gpu-raytracing-build
         dpkg-buildpackage -j`nproc --all` -B --no-sign -rfakeroot

  rhel_86-pkg-build:
     secrets: inherit
     uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
     with:
       image: gfx-build/rhel-8.6:17
       runs-on: '[ "Linux", "docker", "build" ]'
       artifact-out: rhel_86-pkg-build
       artifact-path: intel-level-zero-gpu-raytracing*.rpm
       pre-cmd: |
        dnf makecache
        dnf install -y git git-lfs rpmdevtools
       cmd: |
         VERSION="1.0.0"
         # Prep the source tar exactly like defined in spec file
         rm -rf /build/*
         mkdir -p /build/intel-level-zero-gpu-raytracing-$VERSION
         cp -r * /build/intel-level-zero-gpu-raytracing-$VERSION || true
         pushd /build/intel-level-zero-gpu-raytracing-$VERSION
         git clone -b v2021.6.0 https://github.com/oneapi-src/oneTBB.git tbb
         rm -rf ~/rpmbuild
         rpmdev-setuptree && cd /build
         tar czf ~/rpmbuild/SOURCES/intel-level-zero-gpu-raytracing-$VERSION.tar.gz intel-level-zero-gpu-raytracing-$VERSION
         cp -pr intel-level-zero-gpu-raytracing-$VERSION/rhel/intel-level-zero-gpu-raytracing.spec ~/rpmbuild/SPECS/intel-level-zero-gpu-raytracing.spec

         # Build starts here
         # Build rpm src pkg first, ignoring dependencies because it's only source rpm
         rpmbuild -bs --nodeps '--define=dist .el8_6' ~/rpmbuild/SPECS/intel-level-zero-gpu-raytracing.spec
         #Make sure rpmbuild uses src tarball and not above created sources
         rm -rf ~/rpmbuild/SOURCES/intel-level-zero-gpu-raytracing-$VERSION.tar.gz
         # Build binary pkgs from source pkg, but first install dependencies
         dnf builddep -y ~/rpmbuild/SRPMS/*.src.rpm
         rpmbuild --rebuild '--define=dist .el8_6' --define='__cmake_builddir .' ~/rpmbuild/SRPMS/*.src.rpm
         # Test install the built package
         dnf install -y ~/rpmbuild/RPMS/*/*.rpm
         # Copy artifacts
         popd
         rm -rf ~/rpmbuild/RPMS/*/intel-level-zero-gpu-raytracing-debug*.rpm
         cp -pr ~/rpmbuild/RPMS/*/*.rpm .
         rm -rf ~/rpmbuild

  sles_15sp4-pkg-build:
     secrets: inherit
     uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker.yml@main
     with:
       image: gfx-build/testinstall-sle15-15.4:45
       runs-on: '[ "Linux", "docker", "build" ]'
       artifact-out: sles_15sp4-pkg-build
       artifact-path: intel-level-zero-gpu-raytracing*.rpm
       pre-cmd: |
        unset no_proxy
        unset NO_PROXY
        zypper ref -s
        zypper install -y git rpmbuild
        # Add repo with Intel graphics stack dependencies
        zypper addrepo -r https://repositories.intel.com/graphics/sles/15sp4/intel-graphics.repo
        zypper --non-interactive --gpg-auto-import-keys refresh
       cmd: |
         unset no_proxy
         unset NO_PROXY
         VERSION="1.0.0"
         # Prep the source tar exactly like defined in spec file
         rm -rf /tmp/*
         mkdir -p /tmp/intel-level-zero-gpu-raytracing-$VERSION
         cp -r * /tmp/intel-level-zero-gpu-raytracing-$VERSION || true
         pushd /tmp/intel-level-zero-gpu-raytracing-$VERSION
         git clone -b v2021.6.0 https://github.com/oneapi-src/oneTBB.git tbb
         rm -rf ~/rpmbuild
         rpmdev-setuptree && cd /tmp
         tar czf ~/rpmbuild/SOURCES/intel-level-zero-gpu-raytracing-$VERSION.tar.gz intel-level-zero-gpu-raytracing-$VERSION
         cp -pr intel-level-zero-gpu-raytracing-$VERSION/sle/intel-level-zero-gpu-raytracing.spec ~/rpmbuild/SPECS/intel-level-zero-gpu-raytracing.spec

         # Build starts here
         # Build rpm src pkg first, ignoring dependencies because it's only source rpm
         rpmbuild -bs --nodeps ~/rpmbuild/SPECS/intel-level-zero-gpu-raytracing.spec
         # Set up build dependencies from Intel gfx stack
         # Build binary pkgs from source pkg, but first remove created sources and install dependencies
         rm -rf ~/rpmbuild/SOURCES/intel-level-zero-gpu-raytracing*.tar.gz
         zypper --non-interactive si ~/rpmbuild/SRPMS/*.src.rpm
         rpmbuild --rebuild  ~/rpmbuild/SRPMS/*.src.rpm
         # Test install the built package
         zypper install --allow-unsigned-rpm -y ~/rpmbuild/RPMS/*/*.rpm
         # Copy artifacts
         popd
         rm -rf ~/rpmbuild/RPMS/*/intel-level-zero-gpu-raytracing-debug*.rpm
         cp -pr ~/rpmbuild/RPMS/*/*.rpm .
         rm -rf ~/rpmbuild

  ze_raytracing-linux-test:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker_gpu.yml@main
    with:
      image: embree/ubuntu:22.04
      runs-on: '[ "Linux", "docker", "build" ]'
      env-from-files: ./.github/workflows/dpcpp-sycl-nightly.env
      install-gfx-driver: false
      artifact-out: ze_raytracing-linux-test
      artifact-path: ./build/intel-level-zero-rt-support*.tar.gz
      cmd: |
        mkdir build
        cd build
        cmake -G Ninja -D CMAKE_BUILD_TYPE=ReleaseInternal -D CMAKE_CXX_COMPILER=clang++ -D CMAKE_C_COMPILER=clang -D ZE_RAYTRACING_SYCL_TESTS=INTERNAL_RTAS_BUILDER ..
        cmake --build . --target package

  ze_raytracing-linux-test-DG2:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker_gpu.yml@main
    needs: ["ze_raytracing-linux-test"]
    with:
      image: embree/ubuntu:22.04
      options: --device=/dev/dri:/dev/dri
      runs-on: '[ "Linux", "docker", "dg2" ]'
      env-from-files: ./.github/workflows/gfx-ubuntu22-internal.env
      artifact-in: ze_raytracing-linux-test
      cmd: |
        cd build
        tar xzf intel-level-zero-rt-support*.x86_64.linux.tar.gz
        cd intel-level-zero-rt-support*.x86_64.linux
        cd bin
        ctest

  ze_raytracing-linux-test-PVC:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/docker_gpu.yml@main
    needs: ["ze_raytracing-linux-test"]
    with:
      image: embree/ubuntu:22.04
      options: --device=/dev/dri:/dev/dri
      runs-on: '[ "Linux", "docker", "pvc" ]'
      env-from-files: ./.github/workflows/gfx-ubuntu22-internal.env
      artifact-in: ze_raytracing-linux-test
      cmd: |
        cd build
        tar xzf intel-level-zero-rt-support*.x86_64.linux.tar.gz
        cd intel-level-zero-rt-support*.x86_64.linux
        cd bin
        ctest

  ze_raytracing-windows-V142-Debug:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      runs-on: '[ "Windows", "NAS", "dg2" ]'
      shell: cmd
      cmd: |
        mkdir build || exit /b
        cd build || exit /b
        cmake -G "Visual Studio 16 2019" -T "V142" -A "x64" .. || exit /b
        cmake --build . --config Debug --target package || exit /b

  ze_raytracing-windows-V142-Release:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      runs-on: '[ "Windows", "NAS", "dg2" ]'
      shell: cmd
      cmd: |
        mkdir build || exit /b
        cd build || exit /b
        cmake -G "Visual Studio 16 2019" -T "V142" -A "x64" .. || exit /b
        cmake --build . --config Release --target package || exit /b

  ze_raytracing-windows-V142-ReleaseInternal:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows.yml@main
    with:
      runs-on: '[ "Windows", "NAS", "dg2" ]'
      shell: cmd
      cmd: |
        mkdir build || exit /b
        cd build || exit /b
        cmake -G "Visual Studio 16 2019" -T "V142" -A "x64" .. || exit /b
        cmake --build . --config ReleaseInternal --target package || exit /b
        
  ze_raytracing-windows-test:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows_gpu.yml@devel
    with:
      runs-on: '[ "Windows", "NAS", "build" ]'
      env-from-files: ./.github/workflows/dpcpp-sycl-nightly.env
      install-gfx-driver: false
      artifact-path: ./build/intel-level-zero-rt-support*.zip
      artifact-out: ze_raytracing-windows-test
      shell: cmd
      cmd: |
        mkdir build || exit /b
        cd build || exit /b
        cmake -G Ninja -D CMAKE_CXX_COMPILER=clang++ -D CMAKE_C_COMPILER=clang -D ZE_RAYTRACING_SYCL_TESTS=INTERNAL_RTAS_BUILDER .. || exit /b
        cmake --build . --config ReleaseInternal --target package || exit /b

  ze_raytracing-windows-test-DG2:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/windows_gpu.yml@devel
    needs: ["ze_raytracing-windows-test"]
    with:
      runs-on: '[ "Windows", "NAS", "dg2" ]'
      env-from-files: ./.github/workflows/gfx-windows-internal.env
      artifact-in: ze_raytracing-windows-test
      shell: cmd
      cmd: |
        cd build || exit /b
        unzip intel-level-zero-rt-support*.x64.windows.zip || exit /b
        cd intel-level-zero-rt-support*.x64.windows || exit /b
        cd bin || exit /b
        ctest || exit /b

  static-analysis-klocwork:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/static_analysis.yml@main
    with:
      project: ze_raytracing_support
      coverity: false
      prebuild: |
        cmake -S . -B build -D CMAKE_CXX_COMPILER=g++ -D CMAKE_C_COMPILER=gcc -D CMAKE_BUILD_TYPE=Release -D ZE_RAYTRACING_TBB_STATIC=ON
      build: cmake --build build

  static-analysis-coverity:
    secrets: inherit
    uses: intel-innersource/libraries.devops.renderkit.workflows/.github/workflows/static_analysis.yml@main
    with:
      project: "Level Zero Ray Tracing Support"
      coverity: true
      server: prod4
      prebuild: |
        cmake -S . -B build -D CMAKE_CXX_COMPILER=g++ -D CMAKE_C_COMPILER=gcc -D CMAKE_BUILD_TYPE=Release -D ZE_RAYTRACING_TBB_STATIC=ON
      build: cmake --build build

  success:
    runs-on: ubuntu-latest
    needs:
      - ze_raytracing-ubuntu22_04-GCC
      - ubuntu22_04-pkg-build
      - rhel_86-pkg-build
      - sles_15sp4-pkg-build
      - ze_raytracing-linux-test
      - ze_raytracing-linux-test-DG2
      - ze_raytracing-linux-test-PVC
      - ze_raytracing-windows-V142-Debug
      - ze_raytracing-windows-V142-Release
      - ze_raytracing-windows-V142-ReleaseInternal
      - ze_raytracing-windows-test
      - ze_raytracing-windows-test-DG2
      - static-analysis-klocwork
      - static-analysis-coverity
    if: failure() || cancelled()
    steps:
      - name: Failure
        run: |
          echo "::notice title=Success::Workflow failed"
          exit 1


