name: Windows
on:
  workflow_call:
    inputs:
      override_git_describe:
        type: string
      git_ref:
        type: string
      skip_tests:
        type: string
      run_all:
        type: string
  workflow_dispatch:
    inputs:
      override_git_describe:
        type: string
      git_ref:
        type: string
      skip_tests:
        type: string
      run_all:
        type: string
  push:
    branches-ignore:
      - 'main'
      - 'feature'
      - 'v*.*-*'
    paths-ignore:
      - '**.md'
      - 'test/configs/**'
      - 'tools/**'
      - '!tools/shell/**'
      - '.github/patches/duckdb-wasm/**'
      - '.github/workflows/**'
      - '!.github/workflows/Windows.yml'
      - '.github/config/extensions/*.cmake'
      - '.github/patches/extensions/**/*.patch'

  merge_group:
  pull_request:
    types: [opened, reopened, ready_for_review, converted_to_draft]
    paths-ignore:
      - '**.md'
      - 'test/configs/**'
      - 'tools/**'
      - '!tools/shell/**'
      - '.github/patches/duckdb-wasm/**'
      - '.github/workflows/**'
      - '!.github/workflows/Windows.yml'
      - '.github/config/extensions/*.cmake'
      - '.github/patches/extensions/**/*.patch'

concurrency:
  group: windows-${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref || '' }}-${{ github.base_ref || '' }}-${{ github.ref != 'refs/heads/main' || github.sha }}-${{ inputs.override_git_describe }}
  cancel-in-progress: true

env:
  GH_TOKEN: ${{ secrets.GH_TOKEN }}
  OVERRIDE_GIT_DESCRIBE: ${{ inputs.override_git_describe }}
  AZURE_CODESIGN_ENDPOINT: https://eus.codesigning.azure.net/
  AZURE_CODESIGN_ACCOUNT: duckdb-signing-2
  AZURE_CODESIGN_PROFILE: duckdb-certificate-profile

jobs:
 check-draft:
    # We run all other jobs on PRs only if they are not draft PR
    if: github.event_name != 'pull_request' || github.event.pull_request.draft == false
    runs-on: ubuntu-24.04
    steps:
      - name: Preliminary checks on CI
        run: echo "Event name is ${{ github.event_name }}"

 win-release-64:
    # Builds binaries for windows_amd64
    name: Windows (64 Bit)
    needs: check-draft
    runs-on: windows-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
        ref: ${{ inputs.git_ref }}

    - uses: actions/setup-python@v5
      with:
        python-version: '3.12'

    - uses: ./.github/actions/ccache-action

    - name: Build
      shell: bash
      run: |
        python scripts/windows_ci.py
        cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_GENERATOR_PLATFORM=x64 -DENABLE_EXTENSION_AUTOLOADING=1 -DENABLE_EXTENSION_AUTOINSTALL=1 -DDUCKDB_EXTENSION_CONFIGS="${GITHUB_WORKSPACE}/.github/config/bundled_extensions.cmake" -DDISABLE_UNITY=1 -DOVERRIDE_GIT_DESCRIBE="$OVERRIDE_GIT_DESCRIBE"
        cmake --build . --config Release --parallel

    - name: Set DUCKDB_INSTALL_LIB for ADBC tests
      shell: pwsh
      run: echo "DUCKDB_INSTALL_LIB=$((Get-ChildItem -Recurse -Filter "duckdb.dll" | Select-Object -First 1).FullName)" >> $GITHUB_ENV

    - name: Test DUCKDB_INSTALL_LIB variable
      shell: bash
      run: echo $DUCKDB_INSTALL_LIB

    - name: Test
      shell: bash
      if: ${{ inputs.skip_tests != 'true' }}
      run: |
        test/Release/unittest.exe

    - name: Test with VS2019 C++ stdlib
      shell: bash
      if: ${{ inputs.skip_tests != 'true' }}
      run: |
        choco install wget -y --no-progress
        wget -P ./test/Release https://blobs.duckdb.org/ci/msvcp140.dll
        ls ./test/Release
        ./test/Release/unittest.exe
        rm ./test/Release/msvcp140.dll

    - name: Tools Test
      shell: bash
      if: ${{ inputs.skip_tests != 'true' }}
      run: |
        python -m pip install pytest
        python -m pytest tools/shell/tests --shell-binary Release/duckdb.exe

    - name: Sign files with Azure Trusted Signing (TM)
      if: github.repository == 'duckdb/duckdb' && github.event_name != 'pull_request'
      uses: azure/trusted-signing-action@v0
      with:
        azure-tenant-id:              ${{ secrets.AZURE_CODESIGN_TENANT_ID }}
        azure-client-id:              ${{ secrets.AZURE_CODESIGN_CLIENT_ID }}
        azure-client-secret:          ${{ secrets.AZURE_CODESIGN_CLIENT_SECRET }}
        endpoint:                     ${{ env.AZURE_CODESIGN_ENDPOINT }}
        trusted-signing-account-name: ${{ env.AZURE_CODESIGN_ACCOUNT }}
        certificate-profile-name:     ${{ env.AZURE_CODESIGN_PROFILE }}
        files-folder:                 ${{ github.workspace }}
        files-folder-filter:          exe,dll
        files-folder-recurse:         true
        file-digest:                  SHA256
        timestamp-rfc3161:            http://timestamp.acs.microsoft.com
        timestamp-digest:             SHA256

    - name: Deploy
      shell: bash
      env:
        AWS_ENDPOINT_URL: ${{ secrets.S3_DUCKDB_STAGING_ENDPOINT }}
        AWS_ACCESS_KEY_ID: ${{ secrets.S3_DUCKDB_STAGING_ID }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DUCKDB_STAGING_KEY }}
      run: |
        python scripts/amalgamation.py
        /c/msys64/usr/bin/bash.exe -lc "pacman -Sy --noconfirm zip"
        /c/msys64/usr/bin/zip.exe -j duckdb_cli-windows-amd64.zip Release/duckdb.exe
        /c/msys64/usr/bin/zip.exe -j libduckdb-windows-amd64.zip src/Release/duckdb.dll src/Release/duckdb.lib src/amalgamation/duckdb.hpp src/include/duckdb.h
        ./scripts/upload-assets-to-staging.sh github_release libduckdb-windows-amd64.zip duckdb_cli-windows-amd64.zip

    - uses: actions/upload-artifact@v4
      with:
        name: duckdb-binaries-windows-amd64
        path: |
          libduckdb-windows-amd64.zip
          duckdb_cli-windows-amd64.zip

    - uses: ilammy/msvc-dev-cmd@v1
    - name: Duckdb.dll export symbols with C++ on Windows
      shell: bash
      run: cl -I src/include examples/embedded-c++-windows/cppintegration.cpp -link src/Release/duckdb.lib

 win-release-32:
    name: Windows (32 Bit)
    needs:
      - win-release-64
      - check-draft
    if: ${{ github.ref == 'refs/heads/main' || github.repository != 'duckdb/duckdb' || inputs.run_all == 'true' }}
    runs-on: windows-latest

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
        ref: ${{ inputs.git_ref }}

    - uses: actions/setup-python@v5
      with:
        python-version: '3.12'

    - uses: ./.github/actions/ccache-action

    - name: Build
      shell: bash
      run: |
        cmake -DCMAKE_BUILD_TYPE=Release  -DCMAKE_GENERATOR_PLATFORM=Win32 -DDUCKDB_EXTENSION_CONFIGS="${GITHUB_WORKSPACE}/.github/config/bundled_extensions.cmake" -DOVERRIDE_GIT_DESCRIBE="$OVERRIDE_GIT_DESCRIBE"
        cmake --build . --config Release --parallel

    - name: Test
      shell: bash
      run: test/Release/unittest.exe

    - name: Tools Test
      shell: bash
      run: |
        python -m pip install pytest
        python -m pytest tools/shell/tests --shell-binary Release/duckdb.exe


 win-release-arm64:
   name: Windows (ARM64)
   needs:
    - win-release-64
    - check-draft
   if: ${{ github.ref == 'refs/heads/main' || github.repository != 'duckdb/duckdb' || inputs.run_all == 'true' }}
   runs-on: windows-latest

   steps:
     - uses: actions/checkout@v4
       with:
         fetch-depth: 0
         ref: ${{ inputs.git_ref }}

     - uses: actions/setup-python@v5
       with:
         python-version: '3.12'

     - uses: ./.github/actions/ccache-action

     - name: Build
       shell: bash
       run: |
         cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_GENERATOR_PLATFORM=ARM64 -DDUCKDB_EXTENSION_CONFIGS="${GITHUB_WORKSPACE}/.github/config/bundled_extensions.cmake" -DOVERRIDE_GIT_DESCRIBE="$OVERRIDE_GIT_DESCRIBE" -DDUCKDB_EXPLICIT_PLATFORM=windows_arm64 -DDUCKDB_CUSTOM_PLATFORM=windows_arm64 -DBUILD_UNITTESTS=FALSE
         cmake --build . --config Release --parallel

     - name: Sign files with Azure Trusted Signing (TM)
       if: github.repository == 'duckdb/duckdb' && github.event_name != 'pull_request'
       uses: azure/trusted-signing-action@v0
       with:
         azure-tenant-id:              ${{ secrets.AZURE_CODESIGN_TENANT_ID }}
         azure-client-id:              ${{ secrets.AZURE_CODESIGN_CLIENT_ID }}
         azure-client-secret:          ${{ secrets.AZURE_CODESIGN_CLIENT_SECRET }}
         endpoint:                     ${{ env.AZURE_CODESIGN_ENDPOINT }}
         trusted-signing-account-name: ${{ env.AZURE_CODESIGN_ACCOUNT }}
         certificate-profile-name:     ${{ env.AZURE_CODESIGN_PROFILE }}
         files-folder:                 ${{ github.workspace }}
         files-folder-filter:          exe,dll
         files-folder-recurse:         true
         file-digest:                  SHA256
         timestamp-rfc3161:            http://timestamp.acs.microsoft.com
         timestamp-digest:             SHA256

     - name: Deploy
       shell: bash
       env:
         AWS_ENDPOINT_URL: ${{ secrets.S3_DUCKDB_STAGING_ENDPOINT }}
         AWS_ACCESS_KEY_ID: ${{ secrets.S3_DUCKDB_STAGING_ID }}
         AWS_SECRET_ACCESS_KEY: ${{ secrets.S3_DUCKDB_STAGING_KEY }}
       run: |
         python scripts/amalgamation.py
         /c/msys64/usr/bin/bash.exe -lc "pacman -Sy --noconfirm zip"
         /c/msys64/usr/bin/zip.exe -j duckdb_cli-windows-arm64.zip Release/duckdb.exe
         /c/msys64/usr/bin/zip.exe -j libduckdb-windows-arm64.zip src/Release/duckdb.dll src/Release/duckdb.lib src/amalgamation/duckdb.hpp src/include/duckdb.h
         ./scripts/upload-assets-to-staging.sh github_release libduckdb-windows-arm64.zip duckdb_cli-windows-arm64.zip

     - uses: actions/upload-artifact@v4
       with:
         name: duckdb-binaries-windows-arm64
         path: |
           libduckdb-windows-arm64.zip
           duckdb_cli-windows-arm64.zip

 mingw:
     name: MinGW (64 Bit)
     needs:
      - win-release-64
      - check-draft
     if: ${{ inputs.skip_tests != 'true' }}
     runs-on: windows-latest
     env:
       GEN: ninja
       ENABLE_EXTENSION_AUTOLOADING: 1
       ENABLE_EXTENSION_AUTOINSTALL: 1
     steps:
       - uses: actions/checkout@v4
         with:
           ref: ${{ inputs.git_ref }}

       - uses: actions/setup-python@v5
         with:
           python-version: '3.12'

       - name: Dependencies
         shell: bash
         run: |
           choco install \
             ccache \
             make \
             ninja \
             --no-progress
           choco install \
             mingw --version 14.2.0 \
             --no-progress

       - uses: ./.github/actions/ccache-action

       - name: Build Environment
         shell: bash
         run: |
           # bash from Git
           echo 'C:/Program Files/Git/bin' >> "${GITHUB_PATH}"
           # make, ninja and ccache from Chocolatey
           echo 'C:/ProgramData/Chocolatey/bin' >> "${GITHUB_PATH}"
           # g++ from Chocolatey
           echo 'C:/ProgramData/mingw64/mingw64/bin' >> "${GITHUB_PATH}"

       - name: Build Environment Check
         shell: cmd
         run: |
           where bash
           bash --version
           where make
           make --version
           where ninja
           ninja --version
           where ccache
           ccache --version
           where g++
           g++ --version
           echo %PATH%

       - name: Build and Test
         shell: cmd
         run: |
           make unittest_release

 win-packaged-upload:
   runs-on: windows-latest
   needs:
     - win-release-64
     - win-release-arm64
   steps:
    - uses: actions/download-artifact@v4
      with:
        name: duckdb-binaries-windows-arm64

    - uses: actions/download-artifact@v4
      with:
        name: duckdb-binaries-windows-amd64

    - uses: actions/upload-artifact@v4
      with:
        name: duckdb-binaries-windows
        path: |
          libduckdb-windows-amd64.zip
          duckdb_cli-windows-amd64.zip
          libduckdb-windows-arm64.zip
          duckdb_cli-windows-arm64.zip
