name: Build Artifacts (Intel TBB)
on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'
        required: true
        default: 'warning'
        type: choice
        options:
        - info
        - warning
        - debug
      tags:
        description: 'Test scenario tags'
        required: false
        type: boolean
  pull_request:
    branches: [master, develop]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  push:
    branches: [master, develop, tbb]
    paths-ignore:
    - '**/*.md'
    - '.circleci/**'
    - '.cirrus.yml'
  release:
    types: [published]
  schedule:
    - cron: '0 16 * * *'
concurrency:
  group: ${{ github.workflow }} / ${{ startsWith(github.event_name, 'pull') && github.ref_name || github.run_id }}
  cancel-in-progress: ${{ startsWith(github.event_name, 'pull') }}
env:
  CACHE_EPOCH: 148-2
  GOPROXY: direct
jobs:
  tbb-windows-release:
    runs-on: windows-2025-vs2026
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            crt-linkage: dynamic
            build_type: Release
            winsdk_version: 10.0.26100.0
            build_options: -component-build -use-tbb
            use_allocator: system
    env:
      BUILD_TYPE: '${{ matrix.build_type }}'
    defaults:
      run:
        shell: cmd
    steps:
      - uses: actions/checkout@v5
      - name: Checkout with shallow submodules
        run: |
          # unshallow must come first otherwise submodule may be get unshallowed
          git fetch --tags --unshallow
          git submodule update --init --depth 1
      - name: Cache toolchain, nasm, nsis and wixtoolset 3
        id: toolchain-cache
        uses: actions/cache@v5
        with:
          path: |
            third_party/nasm
            third_party/llvm-build/Release+Asserts
            third_party/wix314
            third_party/nsis
          key: ${{ runner.os }}-toolchain-nsis-${{ hashFiles('CLANG_REVISION') }}-v${{ env.CACHE_EPOCH }}-v2
      - name: Cache msvc curl
        id: curl-cache
        uses: actions/cache@v5
        with:
          path: |
            third_party/libcurl-vc16-arm64-release-static-ipv6-sspi-schannel
            third_party/libcurl-vc16-x64-release-static-ipv6-sspi-schannel
            third_party/libcurl-vc16-x86-release-static-ipv6-sspi-schannel
          key: ${{ runner.os }}-msvc-dynamic-curl-vc16-${{ matrix.arch }}-v2
      - uses: actions/setup-go@v6
        with:
          go-version: '>=1.20.0'
          cache-dependency-path: |
            tools/go.sum
            third_party/boringssl/src/go.sum
      - name: Build build tool
        run: |
          cd tools
          go build
      - name: "Install dependency: clang, nasm, nsis and wixtoolset 3"
        if: ${{ steps.toolchain-cache.outputs.cache-hit != 'true' }}
        run: |
          scripts\download-toolchain.bat
      - name: "Install dependency: curl"
        if: ${{ steps.curl-cache.outputs.cache-hit != 'true' }}
        run: |
          scripts\download-curl-msvc.bat ${{ matrix.arch }}
      - name: Build Binary
        run: |
          set VCToolsVersion=
          set Winsdk=${{ matrix.winsdk_version }}
          set "WindowsSDKVersion=%Winsdk%\"
          if "${{ matrix.arch }}" == "arm64" (call :SetNewVCToolsVersion)
          set vsdevcmd=C:\Program Files\Microsoft Visual Studio\18\Enterprise\Common7\Tools\VsDevCmd.bat
          set "VSCMD_START_DIR=%CD%"
          set Platform=${{ matrix.arch }}

          call "%vsdevcmd%" -arch=%Platform% -host_arch=amd64 -winsdk=%Winsdk% -no_logo -vcvars_ver=%VCToolsVersion%

          tools\build --variant cli -msvc-tgt-arch=${{ matrix.arch }} -msvc-crt-linkage=${{ matrix.crt-linkage }} -use-allocator=${{ matrix.use_allocator }} -nc -build-benchmark -build-test ${{ matrix.build_options }} || exit /b
          tools\build --variant gui -msvc-tgt-arch=${{ matrix.arch }} -msvc-crt-linkage=${{ matrix.crt-linkage }} -use-allocator=${{ matrix.use_allocator }} -nc ${{ matrix.build_options }} || exit /b
          tools\build --variant server -msvc-tgt-arch=${{ matrix.arch }} -msvc-crt-linkage=${{ matrix.crt-linkage }} -use-allocator=${{ matrix.use_allocator }} -nc ${{ matrix.build_options }} || exit /b

          exit /b 0

          :SetNewVCToolsVersion
          set VCToolsVersion=
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-gui
          path: |
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass.exe
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/LICENSE
            !build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-nsis-installer
          path: |
            *.exe
          compression-level: 0 # no compression
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-msi-installer
          path: |
            *.msi
          compression-level: 0 # no compression
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-cli
          path: |
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_cli.exe
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/LICENSE
            !build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-server
          path: |
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_server.exe
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/LICENSE
            !build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-test
          path: |
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_test.exe
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            !build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-benchmark
          path: |
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_benchmark.exe
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            !build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-pdb
          path: |
            build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.pdb
          compression-level: 9 # maximum compression
      - name: Run tests (dummy)
        if: ${{ matrix.arch == 'x64' || matrix.arch == 'x86' }}
        run: |
          build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}\yass_cli --version || exit /b
          build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}\yass_server --version || exit /b
          build-msvc-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}\yass --version || exit /b
      - name: Upload dist tarball (including debuginfo)
        if: ${{ github.event_name == 'release' && matrix.build_type == 'Release' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        shell: bash
        run: |
          gh release upload ${{ github.event.release.tag_name }} *.zip *.exe *.msi
  tbb-windows-release-native-test:
    runs-on: windows-2022
    needs: tbb-windows-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            crt-linkage: dynamic
            build_type: Release
            use_allocator: system
            gtest_repeat_times: 5
    defaults:
      run:
        shell: cmd
    steps:
      - name: Download built executables
        uses: actions/download-artifact@v7
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-test
      - name: Download debuginfo
        uses: actions/download-artifact@v7
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-pdb
      - name: Run tests
        if: ${{ matrix.arch == 'x86' || matrix.arch == 'x64' }}
        run: |
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks4 || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks4a || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5 || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5h || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type http || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks4a --ipv6_mode || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5 --ipv6_mode || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5h --ipv6_mode || exit /b
          yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type http --ipv6_mode || exit /b
  tbb-windows-release-native-benchmark:
    runs-on: windows-2022
    needs: tbb-windows-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x64
            crt-linkage: dynamic
            build_type: Release
            use_allocator: system
    defaults:
      run:
        shell: cmd
    steps:
      - name: Download built executables
        uses: actions/download-artifact@v7
        with:
          name: tbb-msvc-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-benchmark
      - name: Run benchmarks
        if: ${{ matrix.arch == 'x86' || matrix.arch == 'x64' }}
        run: |
          yass_benchmark || exit /b
  tbb-linux-release:
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: i686
            sysroot_arch: i386
            build_type: Release
            build_options: -component-build -use-tbb
            use_allocator: system
          - arch: amd64
            sysroot_arch: amd64
            build_type: Release
            build_options: -component-build -use-tbb
            use_allocator: system
    runs-on: ubuntu-24.04
    env:
      BUILD_TYPE: ${{ matrix.build_type }}
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v5
      - name: Checkout with shallow submodules
        run: |
          # unshallow must come first otherwise submodule may be get unshallowed
          git fetch --tags --unshallow
          git submodule update --init --depth 1
      - name: Set SDK_ROOT
        run: |
          echo "SDK_ROOT=${{ github.workspace }}/debian_bullseye_${{ matrix.sysroot_arch }}-sysroot" >> $GITHUB_ENV
      - name: Cache toolchain
        id: toolchain-cache
        uses: actions/cache@v5
        with:
          path: |
            third_party/llvm-build/Release+Asserts
          key: ${{ runner.os }}-toolchain-${{ hashFiles('CLANG_REVISION') }}-v${{ env.CACHE_EPOCH }}
      - name: Cache sysroot
        id: sysroot-cache
        uses: actions/cache@v5
        with:
          path: |
            ${{ github.workspace }}/debian_bullseye_${{ matrix.sysroot_arch }}-sysroot
          key: ${{ runner.os }}-sysroot-${{ matrix.sysroot_arch }}-${{ hashFiles('scripts/sysroots.json') }}-v3
      - uses: actions/setup-go@v6
        with:
          go-version: '>=1.20.0'
          cache-dependency-path: |
            tools/go.sum
            third_party/boringssl/src/go.sum
      - name: Build build tool
        run: |
          cd tools
          go build
      - name: "Install dependency: prebuilt clang and clang-tidy binaries"
        if: ${{ steps.toolchain-cache.outputs.cache-hit != 'true' }}
        run: |
          ./scripts/download-clang-prebuilt-binaries.py
          rm -f third_party/llvm-build/Release+Asserts/*.tgz
      - name: "Install dependency: sysroot"
        if: ${{ steps.sysroot-cache.outputs.cache-hit != 'true' }}
        run: |
          ./scripts/install-sysroot.py --arch ${{ matrix.sysroot_arch }}
      - name: Change ubuntu mirror
        run: |
          sudo sed -i 's/azure.archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
          sudo apt-get update -qq
      - name: Populate depedencies
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y pkgconf gettext
          # libc6-i386 interferes with x86 build
          sudo apt remove libc6-i386
      - name: Build Binary (CLI)
        run: |
          ./tools/build --variant cli --arch ${{ matrix.arch }} --system linux --sysroot ${{ env.SDK_ROOT }} -use-allocator=${{ matrix.use_allocator }} -nc -build-benchmark -build-test ${{ matrix.build_options }}
      - name: Build Binary (SERVER)
        run: |
          ./tools/build --variant server --arch ${{ matrix.arch }} --system linux --sysroot ${{ env.SDK_ROOT }} -use-allocator=${{ matrix.use_allocator }} -nc ${{ matrix.build_options }}
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-cli
          path: |
            build-linux-${{ matrix.arch }}/yass_cli
            build-linux-${{ matrix.arch }}/*.so*
            !build-linux-${{ matrix.arch }}/*.dbg
            !build-linux-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-server
          path: |
            build-linux-${{ matrix.arch }}/yass_server
            build-linux-${{ matrix.arch }}/*.so*
            !build-linux-${{ matrix.arch }}/*.dbg
            !build-linux-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-test
          path: |
            build-linux-${{ matrix.arch }}/yass_test
            build-linux-${{ matrix.arch }}/*.so*
            !build-linux-${{ matrix.arch }}/*.dbg
            !build-linux-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-benchmark
          path: |
            build-linux-${{ matrix.arch }}/yass_benchmark
            build-linux-${{ matrix.arch }}/*.so*
            !build-linux-${{ matrix.arch }}/*.dbg
            !build-linux-${{ matrix.arch }}/libYASS-C.so
      - uses: actions/upload-artifact@v6
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-dbg
          path: |
            build-linux-${{ matrix.arch }}/*.dbg
          compression-level: 9 # maximum compression
      - name: Upload dist tarball (including debuginfo)
        if: ${{ github.event_name == 'release' && matrix.build_type == 'Release' }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release upload ${{ github.event.release.tag_name }} yass*.tgz
  tbb-linux-release-native-test:
    runs-on: ubuntu-22.04
    needs: tbb-linux-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: i686
            sysroot_arch: i386
            build_type: Release
            use_allocator: system
            gtest_repeat_times: 10
          - arch: amd64
            sysroot_arch: amd64
            build_type: Release
            use_allocator: system
            gtest_repeat_times: 10
    steps:
      - name: Change ubuntu mirror
        if: ${{ matrix.arch == 'i686' || matrix.arch == 'amd64' }}
        run: |
          sudo sed -i 's/azure.archive.ubuntu.com/azure.archive.ubuntu.com/g' /etc/apt/sources.list
          sudo apt-get update -qq
      - name: Populate depedencies (test purposes)
        if: ${{ matrix.arch == 'i686' || matrix.arch == 'amd64' }}
        run: |
          sudo dpkg --add-architecture i386
          sudo apt-get update -qq
          sudo apt-get install -y libcurl4-gnutls-dev:${{ matrix.sysroot_arch }} # based on sysroot's libcurl
      - name: Download built executables
        uses: actions/download-artifact@v7
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-test
      - name: Run tests
        if: ${{ matrix.arch == 'i686' || matrix.arch == 'amd64' }}
        run: |
          chmod 755 yass_test
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks4
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks4a
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5h
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type http
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks4a --ipv6_mode
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5 --ipv6_mode
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type socks5h --ipv6_mode
          ./yass_test --gtest_shuffle --gtest_repeat=${{ matrix.gtest_repeat_times }} --gtest_throw_on_failure --proxy_type http --ipv6_mode
  tbb-linux-release-native-benchmark:
    runs-on: ubuntu-22.04
    needs: tbb-linux-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: i686
            sysroot_arch: i386
            build_type: Release
            use_allocator: system
          - arch: amd64
            sysroot_arch: amd64
            build_type: Release
            use_allocator: system
    steps:
      - name: Download built executables
        uses: actions/download-artifact@v7
        with:
          name: tbb-linux-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.use_allocator }}-${{ github.run_id }}-benchmark
      - name: Run benchmarks
        if: ${{ matrix.arch == 'i686' || matrix.arch == 'amd64' }}
        run: |
          chmod 755 yass_benchmark
          ./yass_benchmark
