on: [push, pull_request]

permissions:
  contents: read

jobs:
  build_and_test:
    strategy:
      matrix:
        platform: [
          { ARCHITECTURE: x86, VCPKG_TRIPLET: "x86-windows-static", RUNNER: "windows-latest", CMAKE_EXTRA_FLAGS: ""},
          { ARCHITECTURE: x64, VCPKG_TRIPLET: "x64-windows-static", RUNNER: "windows-latest", CMAKE_EXTRA_FLAGS: ""},
          { ARCHITECTURE: arm64, VCPKG_TRIPLET: "arm64-windows-static", RUNNER: "windows-11-arm", CMAKE_EXTRA_FLAGS: "-DOQS_PERMIT_UNSUPPORTED_ARCHITECTURE=ON"}
        ]
    runs-on: ${{ matrix.platform.RUNNER }}
    name: ${{ matrix.platform.ARCHITECTURE }}
    steps:
    - uses: actions/checkout@v4
      with:
        submodules: true
    - name: Cache vcpkg
      uses: actions/cache@v4
      with:
        path: 'build/vcpkg_installed/'
        key: vcpkg-${{ matrix.platform.VCPKG_TRIPLET }}
    - name: Set version variables
      run: |
        $v = python version.py
        echo "VERSION=$v" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
      shell: pwsh
    - name: Build
      env:
        ARCHITECTURE: ${{ matrix.platform.ARCHITECTURE }}
        VCPKG_TRIPLET: ${{ matrix.platform.VCPKG_TRIPLET }}
        CMAKE_EXTRA_FLAGS: ${{ matrix.platform.CMAKE_EXTRA_FLAGS }}
      run: |
        set BUILD_NUMBER=0
        mkdir build
        cd build

        for /f "usebackq delims=" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -property installationPath`) do set "VSINSTALL=%%i"
        if "%VSINSTALL%"=="" (
          echo Visual Studio installation was not found.
          exit /b 1
        )

        if /i "%ARCHITECTURE%"=="x86" (
          set "VCVARS_PATH=%VSINSTALL%\VC\Auxiliary\Build\vcvars32.bat"
          set "COMPILER_PATH=%VSINSTALL%\VC\Tools\Llvm\bin\clang-cl.exe"
        ) else if /i "%ARCHITECTURE%"=="x64" (
          set "VCVARS_PATH=%VSINSTALL%\VC\Auxiliary\Build\vcvars64.bat"
          set "COMPILER_PATH=%VSINSTALL%\VC\Tools\Llvm\x64\bin\clang-cl.exe"
        ) else if /i "%ARCHITECTURE%"=="arm64" (
          set "VCVARS_PATH=%VSINSTALL%\VC\Auxiliary\Build\vcvarsarm64.bat"
          set "COMPILER_PATH=%VSINSTALL%\VC\Tools\Llvm\ARM64\bin\clang-cl.exe"
        ) else (
          echo Unsupported architecture: %ARCHITECTURE%
          exit /b 1
        )

        call "%VCVARS_PATH%"
        cmake -G "Ninja" -DCMAKE_TOOLCHAIN_FILE="C:\vcpkg\scripts\buildsystems\vcpkg.cmake" -DVCPKG_TARGET_TRIPLET=%VCPKG_TRIPLET% -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_C_COMPILER="%COMPILER_PATH%" -DCMAKE_CXX_COMPILER="%COMPILER_PATH%" -DBUILD_NUMBER=%BUILD_NUMBER% %CMAKE_EXTRA_FLAGS% ..
        cmake --build .
        mkdir installers
        vpnsetup /SFXMODE:vpnclient /SFXOUT:"installers\softether-vpnclient-%VERSION%.%BUILD_NUMBER%.%ARCHITECTURE%.exe"
        vpnsetup /SFXMODE:vpnserver_vpnbridge /SFXOUT:"installers\softether-vpnserver_vpnbridge-%VERSION%.%BUILD_NUMBER%.%ARCHITECTURE%.exe"
      shell: cmd
    - name: Test
      shell: powershell
      run: |
        . .ci/appveyor-vpntest.ps1
    - uses: actions/upload-artifact@v4
      with:
        if-no-files-found: error
        name: Binaries-${{ matrix.platform.ARCHITECTURE }}
        path: |
          build/*.exe
          build/*.pdb
          build/*.se2
    - uses: actions/upload-artifact@v4
      with:
        if-no-files-found: error
        name: Installers-${{ matrix.platform.ARCHITECTURE }}
        path: build/installers