name: Build Artifacts (Windows+VS2017+XPSP3)
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, vs2017]
    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') }}
defaults:
  run:
    shell: cmd
env:
  CACHE_EPOCH: 148-2
  GOPROXY: direct
jobs:
  windows-xp-release:
    runs-on: windows-2025-vs2026
    permissions:
      contents: write
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86
            crt-linkage: dynamic
            build_type: Release
            build_options: -msvc-allow-xp
            use_allocator: tbbmalloc
          - arch: x64
            crt-linkage: dynamic
            build_type: Release
            build_options: -msvc-allow-xp
            use_allocator: tbbmalloc
    env:
      BUILD_TYPE: '${{ matrix.build_type }}'
    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: Install WinXP support (via Visual Studio Installer)
        # see https://github.com/actions/runner-images/issues/5143
        # https://learn.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?view=visualstudio
        shell: pwsh
        run: |
          Set-Location "C:\Program Files (x86)\Microsoft Visual Studio\Installer\"
          $InstallPath = "C:\Program Files\Microsoft Visual Studio\18\Enterprise"
          $componentsToAdd = @(
            "Microsoft.VisualStudio.Component.VC.v141.x86.x64",
            "Microsoft.VisualStudio.Component.WinXP"
          )
          [string]$workloadArgs = $componentsToAdd | ForEach-Object {" --add " +  $_}
          $Arguments = ('/c', "vs_installer.exe", 'modify', '--installPath', "`"$InstallPath`"",$workloadArgs, '--quiet', '--norestart', '--nocache')
          $process = Start-Process -FilePath cmd.exe -ArgumentList $Arguments -Wait -PassThru -WindowStyle Hidden
          if ($process.ExitCode -eq 0)
          {
              Write-Host "components have been successfully added"
          }
          else
          {
              Write-Host "components were not installed"
              exit 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
      - 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: Patch libcxx for windows xp
        shell: bash
        run: |
          pushd third_party/libc++/trunk
          patch -p1 < ../winxp.patch
          popd
      - name: Build Binary
        run: |
          set VCToolsVersion=14.16.27012
          set "VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio\18\Enterprise\VC"
          set "WindowsSDKVersion=10.0.10240.0\"
          set "WindowsSdkDir=C:\Program Files (x86)\Windows Kits\10"

          set Platform=${{ matrix.arch }}
          set VSCMD_ARG_TGT_ARCH=${{ matrix.arch }}

          call "scripts\callxp-%Platform%.cmd"

          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 dylib -msvc-tgt-arch=${{ matrix.arch }} -msvc-crt-linkage=${{ matrix.crt-linkage }} -use-allocator=${{ matrix.use_allocator }} -nc ${{ matrix.build_options }} || exit /b

          goto :eof
      - uses: actions/upload-artifact@v6
        with:
          name: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-gui
          path: |
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass.exe
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/LICENSE
            !build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: msvc-winxp-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: msvc-winxp-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: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-cli
          path: |
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_cli.exe
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/LICENSE
            !build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-dylib
          path: |
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/devel
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass.py
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/LICENSE
      - uses: actions/upload-artifact@v6
        with:
          name: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-test
          path: |
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_test.exe
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            !build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-benchmark
          path: |
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/yass_benchmark.exe
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.dll
            !build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/YASS-C.dll
      - uses: actions/upload-artifact@v6
        with:
          name: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-pdb
          path: |
            build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}/*.pdb
          compression-level: 9 # maximum compression
      - name: Run tests (dummy)
        run: |
          build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}\yass_cli --version || exit /b
          build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}\yass --version || exit /b
      - name: Run tests (python)
        if: ${{ matrix.arch == 'x64' }}
        run: |
          python build-msvc-winxp-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}\yass.py --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 }} *.msi *.exe *.zip
  windows-xp-release-native-test:
    runs-on: windows-2022
    needs: windows-xp-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86
            crt-linkage: dynamic
            build_type: Release
            use_allocator: tbbmalloc
          - arch: x64
            crt-linkage: dynamic
            build_type: Release
            use_allocator: tbbmalloc
    steps:
      - name: Download built executables
        uses: actions/download-artifact@v7
        with:
          name: msvc-winxp-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: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-pdb
      - name: Run tests
        run: |
          yass_test || exit /b
  windows-xp-release-native-benchmark:
    runs-on: windows-2022
    needs: windows-xp-release
    strategy:
      fail-fast: false
      matrix:
        include:
          - arch: x86
            crt-linkage: dynamic
            build_type: Release
            use_allocator: tbbmalloc
          - arch: x64
            crt-linkage: dynamic
            build_type: Release
            use_allocator: tbbmalloc
    steps:
      - name: Download built executables
        uses: actions/download-artifact@v7
        with:
          name: msvc-winxp-release-${{ matrix.build_type }}-${{ matrix.arch }}-${{ matrix.crt-linkage }}-${{ matrix.use_allocator }}-${{ github.run_id }}-benchmark
      - name: Run benchmarks
        run: |
          yass_benchmark || exit /b
