# Copyright (c) 2021-2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

name: oneTBB CI

on:
  push:
    branches: [master]

  pull_request:
    branches: [master]
    types:
      - opened
      - synchronize
      - reopened

permissions: read-all

env:
  BUILD_CONCURRENCY: 2
  MACOS_BUILD_CONCURRENCY: 3
  TEST_TIMEOUT: 180

jobs:
  codespell:
    runs-on: [ubuntu-20.04]
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2
      - name: Run scan
        run: |
          sudo apt update && sudo apt install -y codespell
          ${GITHUB_WORKSPACE}/.github/scripts/codespell.sh `pwd`

  examples_clang-format:
    runs-on: [ubuntu-20.04]
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2
      - name: Run scan
        run: |
          command -v clang-format-10
          cp -r examples examples_formatted
          find examples_formatted -regex '.*\.\(cpp\|hpp\)' -exec clang-format-10 -style=file -i {} \;
          diff -r examples examples_formatted

  documentation:
    needs: [codespell]
    env:
      BUILD_TYPE: oss
    runs-on: [ubuntu-22.04]
    timeout-minutes: 10
    steps:
      - uses: actions/checkout@v2
      - name: Install prerequisites
        run: |
          pip3 install -U Jinja2
          pip3 install git+https://github.com/executablebooks/sphinx-book-theme.git
          pip3 install sphinx-tabs
          echo GITHUB_SHA_SHORT=${GITHUB_SHA::8} >> $GITHUB_ENV
          mkdir html
      - name: Build documentation
        run: |
          export BUILD_TYPE=${BUILD_TYPE} && sphinx-build doc html
          tar -czvf html.tar.gz html/
      - name: Save docs
        uses: actions/upload-artifact@v2.2.1
        with:
          name: oneTBB-html-docs-${{ env.GITHUB_SHA_SHORT }}
          path: html.tar.gz

  pages:
    if: ${{ github.ref == 'refs/heads/master' }}
    runs-on: ubuntu-latest
    needs: [documentation]
    steps:
      - name: Checkout gh-pages
        uses: actions/checkout@v2
        with:
          ref: gh-pages
          path: gh-pages
      - name: Set env
        run: echo GITHUB_SHA_SHORT=${GITHUB_SHA::8} >> $GITHUB_ENV
      - name: Download documetation
        uses: actions/download-artifact@v2
        with:
          name: oneTBB-html-docs-${{ env.GITHUB_SHA_SHORT }}
      - name: Publish to github pages
        run: |
          tar -xvf html.tar.gz
          cd gh-pages
          rm -rf *
          touch .nojekyll # https://github.blog/2009-12-29-bypassing-jekyll-on-github-pages/
          cp -r ../html/* .
          git config user.name github-actions
          git config user.email github-actions@github.com
          git add .
          git commit --reset-author --amend -m "Update from GitHub Actions"
          git push --force origin gh-pages

  copyright_check:
    if: ${{ github.ref != 'refs/heads/master' }}
    runs-on: [ubuntu-20.04]
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Run check
        run: |
          sed -i \
            -e "/Copyright (c) .* Intel Corporation/s/ \([0-9]\+\)[-0-9]*/ \\1-$(date +%Y)/" \
            -e "/Copyright (c) .* Intel Corporation/s/$(date +%Y)-$(date +%Y)/$(date +%Y)/" \
            $(git diff --diff-filter=d --name-only ${{ github.event.pull_request.base.sha }})
          git checkout -- third-party-programs.txt
          git diff > years.diff
          if [[ -s years.diff ]]; then
            echo "Wrong copyright years"
            cat years.diff
            exit 1
          fi
  python_module_test_ubuntu_latest:
    runs-on: [ubuntu-latest]
    timeout-minutes: 15
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        run: |
          mkdir build && cd build
          cmake -DTBB4PY_BUILD=ON -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc ..
          make VERBOSE=1 -j${BUILD_CONCURRENCY} python_build
          ctest -R python_test --output-on-failure --timeout ${TEST_TIMEOUT}

  linux-testing:
    name: ${{ matrix.os }}_${{ matrix.cxx_compiler }}_cxx${{ matrix.std }}_${{ matrix.build_type }}_preview=${{ matrix.preview }}
    runs-on: ['${{ matrix.os }}']
    timeout-minutes: 45
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            c_compiler: gcc
            cxx_compiler: g++
            std: 14
            build_type: relwithdebinfo
            preview: 'OFF'
          - os: ubuntu-20.04
            c_compiler: gcc
            cxx_compiler: g++
            std: 17
            build_type: release
            preview: 'ON'
          - os: ubuntu-20.04
            c_compiler: gcc-10
            cxx_compiler: g++-10
            std: 20
            build_type: debug
            preview: 'ON'
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        shell: bash
        run: |
          set -x
          mkdir build && cd build
          cmake -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DTBB_CPF=${{ matrix.preview }} ..
          make VERBOSE=1 -j${BUILD_CONCURRENCY}
          ctest --timeout ${TEST_TIMEOUT} --output-on-failure

  macos-testing:
    name: ${{ matrix.os }}_${{ matrix.cxx_compiler }}_cxx${{ matrix.std }}_${{ matrix.build_type }}_preview=${{ matrix.preview }}
    runs-on: ['${{ matrix.os }}']
    timeout-minutes: 45
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-12
            c_compiler: clang
            cxx_compiler: clang++
            std: 14
            build_type: relwithdebinfo
            preview: 'ON'
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        shell: bash
        run: |
          set -x
          mkdir build && cd build
          cmake -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DTBB_CPF=${{ matrix.preview }} ..
          make VERBOSE=1 -j${MACOS_BUILD_CONCURRENCY}
          ctest --timeout ${TEST_TIMEOUT} --output-on-failure

  windows-testing:
    name: ${{ matrix.job_name }}
    runs-on: ['${{ matrix.os }}']
    timeout-minutes: 45
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-2019
            generator: Visual Studio 16 2019
            c_compiler: cl
            cxx_compiler: cl
            std: 14
            build_type: relwithdebinfo
            preview: 'ON'
            job_name: windows_cl2019_cxx14_relwithdebinfo_preview=ON
          - os: windows-2022
            generator: Visual Studio 17 2022
            c_compiler: cl
            cxx_compiler: cl
            std: 17
            build_type: relwithdebinfo
            preview: 'OFF'
            job_name: windows_cl2022_cxx17_relwithdebinfo_preview=OFF
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        run: |
          mkdir build
          cd build
          cmake -G "${{ matrix.generator }}" -A x64 -DCMAKE_CXX_STANDARD=${{ matrix.std }} `
            -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} `
            -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DTBB_CPF=${{ matrix.preview }} ..
          cmake --build . --config ${{ matrix.build_type }} -j -v
          ctest -C ${{ matrix.build_type }} --timeout ${env:TEST_TIMEOUT} --output-on-failure

  linux-examples-testing:
    name: examples_${{ matrix.os }}_${{ matrix.cxx_compiler }}_cxx${{ matrix.std }}_${{ matrix.build_type }}_preview=${{ matrix.preview }}
    runs-on: ['${{ matrix.os }}']
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: ubuntu-latest
            c_compiler: gcc
            cxx_compiler: g++
            std: 14
            build_type: relwithdebinfo
            preview: 'OFF'
          - os: ubuntu-20.04
            c_compiler: gcc
            cxx_compiler: g++
            std: 17
            build_type: release
            preview: 'ON'
          - os: ubuntu-20.04
            c_compiler: gcc-10
            cxx_compiler: g++-10
            std: 20
            build_type: debug
            preview: 'ON'
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        shell: bash
        run: |
          set -x
          mkdir build && cd build
          cmake -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} \
            -DTBB_CPF=${{ matrix.preview }} -DTBB_TEST=OFF -DTBB_EXAMPLES=ON ..
          cmake --build . -v --target light_test_examples

  macos-examples-testing:
    name: examples_${{ matrix.os }}_${{ matrix.cxx_compiler }}_cxx${{ matrix.std }}_${{ matrix.build_type }}_preview=${{ matrix.preview }}
    runs-on: ['${{ matrix.os }}']
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-12
            c_compiler: clang
            cxx_compiler: clang++
            std: 14
            build_type: relwithdebinfo
            preview: 'ON'
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        shell: bash
        run: |
          set -x
          mkdir build && cd build
          cmake -DCMAKE_CXX_STANDARD=${{ matrix.std }} -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} \
            -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} \
            -DTBB_CPF=${{ matrix.preview }} -DTBB_TEST=OFF -DTBB_EXAMPLES=ON ..
          cmake --build . -v --target light_test_examples

  windows-examples-testing:
    name: ${{ matrix.job_name }}
    runs-on: ['${{ matrix.os }}']
    timeout-minutes: 20
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: windows-2019
            generator: Visual Studio 16 2019
            c_compiler: cl
            cxx_compiler: cl
            std: 14
            build_type: relwithdebinfo
            preview: 'ON'
            job_name: examples_windows_cl2019_cxx14_relwithdebinfo_preview=ON
          - os: windows-2022
            generator: Visual Studio 17 2022
            c_compiler: cl
            cxx_compiler: cl
            std: 17
            build_type: relwithdebinfo
            preview: 'OFF'
            job_name: examples_windows_cl2022_cxx17_relwithdebinfo_preview=OFF
    steps:
      - uses: actions/checkout@v2
      - name: Run testing
        run: |
          mkdir build
          cd build
          cmake -G "${{ matrix.generator }}" -A x64 -DCMAKE_CXX_STANDARD=${{ matrix.std }} `
            -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} -DCMAKE_CXX_COMPILER=${{ matrix.cxx_compiler }} `
            -DCMAKE_C_COMPILER=${{ matrix.c_compiler }} -DTBB_CPF=${{ matrix.preview }} -DTBB_TEST=OFF -DTBB_EXAMPLES=ON ..
          cmake --build . -v --target light_test_examples
