#***************************************************************************
#                                  _   _ ____  _
#  Project                     ___| | | |  _ \| |
#                             / __| | | | |_) | |
#                            | (__| |_| |  _ <| |___
#                             \___|\___/|_| \_\_____|
#
# Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at https://curl.se/docs/copyright.html.
#
# You may opt to use, copy, modify, merge, publish, distribute and/or sell
# copies of the Software, and permit persons to whom the Software is
# furnished to do so, under the terms of the COPYING file.
#
# This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
# KIND, either express or implied.
#
# SPDX-License-Identifier: curl
#
###########################################################################

# https://ci.appveyor.com/project/curlorg/curl/history
# AppVeyor configuration:
#   https://www.appveyor.com/docs/appveyor-yml/
# AppVeyor worker images:
#   https://www.appveyor.com/docs/windows-images-software/

version: 7.50.0.{build}

environment:
  UNITY: 'OFF'
  OPENSSL: 'OFF'
  DEBUG: 'ON'
  SHARED: 'OFF'
  matrix:
    # generated CMake-based Visual Studio Release builds
    - job_name: 'CMake, VS2008, Release, x86, Schannel, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 9 2008'
      PRJ_CFG: Release
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      SHARED: 'ON'
      TESTING: 'OFF'
      DISABLED_TESTS: ''
    - job_name: 'CMake, VS2022, Release, x64, OpenSSL 3, WebSockets, Unity, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 17 2022'
      TARGET: '-A x64'
      PRJ_CFG: Release
      OPENSSL: 'ON'
      SCHANNEL: 'OFF'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      SHARED: 'ON'
      TESTING: 'OFF'
      DISABLED_TESTS: ''
      WEBSOCKETS: 'ON'
      UNITY: 'ON'
    - job_name: 'CMake, VS2022, Release, arm64, Schannel, Static, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 17 2022'
      TARGET: '-A ARM64'
      PRJ_CFG: Release
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      TESTING: 'OFF'
      DISABLED_TESTS: ''
    # generated CMake-based Visual Studio Debug builds
    - job_name: 'CMake, VS2010, Debug, x64, no SSL, Static'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 10 2010 Win64'
      PRJ_CFG: Debug
      SCHANNEL: 'OFF'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      DISABLED_TESTS: '!1139 !1501'
      ADD_PATH: 'C:\msys64\usr\bin'
    - job_name: 'CMake, VS2022, Debug, x64, Schannel, Static, Unicode'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 17 2022'
      TARGET: '-A x64'
      PRJ_CFG: Debug
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'ON'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      DISABLED_TESTS: '!1139 !1501'
      ADD_PATH: 'C:\msys64\usr\bin'
    - job_name: 'CMake, VS2022, Debug, x64, no SSL, Static'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 17 2022'
      TARGET: '-A x64'
      PRJ_CFG: Debug
      SCHANNEL: 'OFF'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      DISABLED_TESTS: '!1139 !1501'
      ADD_PATH: 'C:\msys64\usr\bin'
    - job_name: 'CMake, VS2022, Debug, x64, no SSL, Static, HTTP only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'Visual Studio 17 2022'
      TARGET: '-A x64'
      PRJ_CFG: Debug
      SCHANNEL: 'OFF'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'ON'
      TESTING: 'ON'
      DISABLED_TESTS: '!1139 !1501'
      ADD_PATH: 'C:\msys64\usr\bin'
    # generated CMake-based MSYS Makefiles builds (mingw cross-compiling)
    - job_name: 'CMake, mingw-w64, gcc 13, Debug, x64, Schannel, Static, Unicode, Unity'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'MSYS Makefiles'
      PRJ_CFG: Debug
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'ON'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      DISABLED_TESTS: '!1086 !1139 !1451 !1501'
      ADD_PATH: 'C:\msys64\mingw64\bin;C:\msys64\usr\bin'
      MSYS2_ARG_CONV_EXCL: '/*'
      BUILD_OPT: -k
      UNITY: 'ON'
    - job_name: 'CMake, mingw-w64, gcc 7, Debug, x64, Schannel, Static, Unicode'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'MSYS Makefiles'
      PRJ_CFG: Debug
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'ON'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      DISABLED_TESTS: '!1086 !1139 !1451 !1501'
      ADD_PATH: 'C:\mingw-w64\x86_64-7.2.0-posix-seh-rt_v5-rev1\mingw64\bin;C:\msys64\usr\bin'
      MSYS2_ARG_CONV_EXCL: '/*'
      BUILD_OPT: -k
    - job_name: 'CMake, mingw-w64, gcc 9, Debug, x64, Schannel, Static, Unity'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'MSYS Makefiles'
      PRJ_CFG: Debug
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      # test 286 disabled due to https://github.com/curl/curl/issues/12040
      DISABLED_TESTS: '~286 !1086 !1139 !1451 !1501'
      ADD_PATH: 'C:\msys64\mingw64\bin;C:\msys64\usr\bin'
      MSYS2_ARG_CONV_EXCL: '/*'
      BUILD_OPT: -k
      UNITY: 'ON'
    - job_name: 'CMake, mingw-w64, gcc 6, Debug, x86, Schannel, Static'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: CMake
      PRJ_GEN: 'MSYS Makefiles'
      PRJ_CFG: Debug
      SCHANNEL: 'ON'
      ENABLE_UNICODE: 'OFF'
      HTTP_ONLY: 'OFF'
      TESTING: 'ON'
      DISABLED_TESTS: '!1086 !1139 !1451 !1501'
      ADD_PATH: 'C:\mingw-w64\i686-6.3.0-posix-dwarf-rt_v5-rev1\mingw32\bin;C:\msys64\usr\bin'
      MSYS2_ARG_CONV_EXCL: '/*'
      BUILD_OPT: -k
    # winbuild-based builds
    - job_name: 'winbuild, VS2015, Debug, x64, OpenSSL 1.1.1, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: winbuild_vs2015
      DEBUG: 'yes'
      PATHPART: debug
      TESTING: 'OFF'
      ENABLE_UNICODE: 'no'
    - job_name: 'winbuild, VS2015, Release, x64, OpenSSL 1.1.1, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: winbuild_vs2015
      DEBUG: 'no'
      PATHPART: release
      TESTING: 'OFF'
      ENABLE_UNICODE: 'no'
    - job_name: 'winbuild, VS2017, Debug, x64, OpenSSL 1.1.1, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: winbuild_vs2017
      DEBUG: 'yes'
      PATHPART: debug
      TESTING: 'OFF'
      ENABLE_UNICODE: 'no'
    - job_name: 'winbuild, VS2017, Release, x64, OpenSSL 1.1.1, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: winbuild_vs2017
      DEBUG: 'no'
      PATHPART: release
      TESTING: 'OFF'
      ENABLE_UNICODE: 'no'
    - job_name: 'winbuild, VS2015, Debug, x64, OpenSSL 1.1.1, Unicode, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: winbuild_vs2015
      DEBUG: 'yes'
      PATHPART: debug
      TESTING: 'OFF'
      ENABLE_UNICODE: 'yes'
    - job_name: 'winbuild, VS2015, Release, x64, OpenSSL 1.1.1, Unicode, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2015'
      BUILD_SYSTEM: winbuild_vs2015
      DEBUG: 'no'
      PATHPART: release
      TESTING: 'OFF'
      ENABLE_UNICODE: 'yes'
    - job_name: 'winbuild, VS2017, Debug, x64, OpenSSL 1.1.1, Unicode, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: winbuild_vs2017
      DEBUG: 'yes'
      PATHPART: debug
      TESTING: 'OFF'
      ENABLE_UNICODE: 'yes'
    - job_name: 'winbuild, VS2017, Release, x64, OpenSSL 1.1.1, Unicode, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: winbuild_vs2017
      DEBUG: 'no'
      PATHPART: release
      TESTING: 'OFF'
      ENABLE_UNICODE: 'yes'
    # generated VisualStudioSolution-based builds
    - job_name: 'VisualStudioSolution, VS2017, Debug, x86, Schannel, Build-only'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: VisualStudioSolution
      PRJ_CFG: 'DLL Debug - DLL Windows SSPI - DLL WinIDN'
      TESTING: 'OFF'
      VC_VERSION: VC14.10
    # autotools-based builds (NOT mingw cross-compiling, but msys2 native)
    - job_name: 'autotools, msys2, Debug, x86_64, no Proxy, no SSL'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: autotools
      TESTING: 'ON'
      DISABLED_TESTS: '!19 !1233'
      ADD_PATH: 'C:\msys64\usr\bin'
      CONFIG_ARGS: '--enable-debug --enable-werror --disable-threaded-resolver --disable-proxy --without-ssl --enable-websockets'
    - job_name: 'autotools, msys2, Debug, x86_64, no SSL'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: autotools
      TESTING: 'ON'
      DISABLED_TESTS: '!19 !504 !704 !705 !1233'
      ADD_PATH: 'C:\msys64\usr\bin'
      CONFIG_ARGS: '--enable-debug --enable-werror --disable-threaded-resolver --without-ssl --enable-websockets'
    - job_name: 'autotools, msys2, Release, x86_64, no SSL'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2017'
      BUILD_SYSTEM: autotools
      TESTING: 'ON'
      DISABLED_TESTS: '!19 !504 !704 !705 !1233'
      ADD_PATH: 'C:\msys64\usr\bin'
      CONFIG_ARGS: '--enable-warnings --enable-werror --without-ssl --enable-websockets'
    # autotools-based Cygwin build
    - job_name: 'autotools, cygwin, Debug, x86_64, no SSL'
      APPVEYOR_BUILD_WORKER_IMAGE: 'Visual Studio 2022'
      BUILD_SYSTEM: autotools
      TESTING: 'ON'
      DISABLED_TESTS: ''
      ADD_PATH: 'C:\cygwin64\bin'
      CONFIG_ARGS: '--enable-debug --enable-werror --disable-threaded-resolver --without-ssl --enable-websockets'
      POSIX_PATH_PREFIX: '/cygdrive'

install:
  - ps: |
      if($env:ADD_PATH -ne $null) {
        $env:PATH = "$env:ADD_PATH;$env:PATH"
      }

build_script:
  - ps: |
      function Pull-BatchFile-Env {
        param([string]$Path, [string]$Parameters)
        $tempFile = [IO.Path]::GetTempFileName()
        cmd.exe /c " `"$Path`" $Parameters && set " > $tempFile
        Get-Content $tempFile | ForEach-Object { if($_ -match '^(.*?)=(.*)$') { Set-Content "env:\$($matches[1])" $matches[2] } }
        Remove-Item $tempFile
      }

      $ErrorActionPreference = 'Stop'

      if($env:APPVEYOR_BUILD_WORKER_IMAGE -eq 'Visual Studio 2022') {
        $openssl_root = 'C:\OpenSSL-v30-Win64'
      }
      else {
        $openssl_root = 'C:\OpenSSL-v111-Win64'
      }

      if($env:BUILD_SYSTEM -eq 'CMake') {

        $options = @('-DCURL_WERROR=ON')
        $options += "-G$env:PRJ_GEN"
        if($env:TARGET -ne $null) {
          $options += "$env:TARGET"
          if($env:TARGET.Contains('ARM64')) {
            $env:SKIP_RUN = 'ARM64 architecture'
          }
        }
        $options += "-DCURL_USE_OPENSSL=$env:OPENSSL"
        if($env:OPENSSL -eq 'ON') {
          $options += "-DOPENSSL_ROOT_DIR=$openssl_root"
        }
        $options += "-DCURL_USE_SCHANNEL=$env:SCHANNEL"
        $options += "-DHTTP_ONLY=$env:HTTP_ONLY"
        $options += "-DBUILD_SHARED_LIBS=$env:SHARED"
        $options += "-DBUILD_TESTING=$env:TESTING"
        $options += "-DENABLE_WEBSOCKETS=$env:WEBSOCKETS"
        $options += "-DCMAKE_UNITY_BUILD=$env:UNITY"
        $options += "-DENABLE_DEBUG=$env:DEBUG"
        $options += "-DENABLE_UNICODE=$env:ENABLE_UNICODE"
        $options += '-DCMAKE_INSTALL_PREFIX=C:/CURL'
        $options += "-DCMAKE_BUILD_TYPE=$env:PRJ_CFG"
        if($env:PRJ_CFG -eq 'Debug') {
          $options += '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG='
        }
        elseif ($env:PRJ_CFG -eq 'Release') {
          $options += '-DCMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE='
        }
        if($env:PRJ_GEN.Contains('Visual Studio')) {
          $options += '-DCMAKE_VS_GLOBALS=TrackFileAccess=false'
        }
        if($env:PRJ_GEN -eq 'Visual Studio 9 2008') {
          # Without this it fails to run due to missing MSVCR90.dll
          $options += '-DCURL_STATIC_CRT=ON'
        }

        Write-Host 'CMake options:' $options
        cmake . $options
        cmake --build . --config $env:PRJ_CFG --parallel 2 --clean-first -- $env:BUILD_OPT
        if($env:SHARED -eq 'ON') {
          Copy-Item -Path 'C:\Projects\curl\lib\*.dll' -Destination 'C:\projects\curl\src'
          Copy-Item -Path 'C:\Projects\curl\lib\*.dll' -Destination 'C:\projects\curl\tests\libtest'
        }
        if($env:OPENSSL -eq 'ON') {
          Copy-Item -Path "$openssl_root\*.dll" -Destination 'C:\projects\curl\src'
        }
        $curl = '.\src\curl.exe'
      }
      elseif($env:BUILD_SYSTEM -eq 'VisualStudioSolution') {
        cd projects
        .\generate.bat $env:VC_VERSION
        msbuild.exe -maxcpucount "-property:Configuration=$env:PRJ_CFG" "Windows\$env:VC_VERSION\curl-all.sln"
        $curl = "..\build\Win32\$env:VC_VERSION\$env:PRJ_CFG\curld.exe"
      }
      elseif($env:BUILD_SYSTEM -eq 'winbuild_vs2015') {
        .\buildconf.bat
        cd winbuild
        Pull-BatchFile-Env 'C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd' /x64
        Pull-BatchFile-Env 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat' x86_amd64
        nmake /nologo /f Makefile.vc mode=dll VC=14 "SSL_PATH=$openssl_root" WITH_SSL=dll MACHINE=x64 DEBUG=$env:DEBUG ENABLE_UNICODE=$env:ENABLE_UNICODE
        $curl = "..\builds\libcurl-vc14-x64-$env:PATHPART-dll-ssl-dll-ipv6-sspi\bin\curl.exe"
      }
      elseif($env:BUILD_SYSTEM -eq 'winbuild_vs2017') {
        .\buildconf.bat
        cd winbuild
        Pull-BatchFile-Env 'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat'
        nmake /nologo /f Makefile.vc mode=dll VC=14.10 "SSL_PATH=$openssl_root" WITH_SSL=dll MACHINE=x64 DEBUG=$env:DEBUG ENABLE_UNICODE=$env:ENABLE_UNICODE
        $curl = "..\builds\libcurl-vc14.10-x64-$env:PATHPART-dll-ssl-dll-ipv6-sspi\bin\curl.exe"
      }
      elseif($env:BUILD_SYSTEM -eq 'autotools') {
        & bash -e -c "cd $env:POSIX_PATH_PREFIX/c/projects/curl && autoreconf -fi 2>&1 && ./configure $env:CONFIG_ARGS 2>&1 && make V=1 && make V=1 examples && cd tests && make V=1"
        $curl = '.\src\curl.exe'
      }

      Get-ChildItem -Path C:\projects\curl -Include ('*.exe', '*.dll') -Recurse -Name
      if($env:SKIP_RUN -eq $null) {
        cmd.exe /c "`"$curl`" -V 2>&1"
        if(-not $?) {
          Write-Host "Error running curl: '$curl':" ("0x" + $LastExitCode.ToString("X"))
          exit 1
        }
      }
      else {
        Write-Host "Skip running curl.exe. Reason: $env:SKIP_RUN"
      }

      if($false) {
        if(Test-Path CMakeFiles/CMakeConfigureLog.yaml) { cat CMakeFiles/CMakeConfigureLog.yaml }
        if(Test-Path CMakeFiles/CMakeOutput.log) { cat CMakeFiles/CMakeOutput.log }
        if(Test-Path CMakeFiles/CMakeError.log) { cat CMakeFiles/CMakeError.log }
        if(Test-Path config.log) { cat config.log }
      }

      if($env:TESTING -eq 'ON' -and $env:BUILD_SYSTEM -eq 'CMake') {
        cmake --build . --config $env:PRJ_CFG --parallel 2 --target testdeps
      }

test_script:
  - ps: |
      if(Test-Path 'C:/msys64/usr/bin/curl.exe') {
        $acurl="-ac $env:POSIX_PATH_PREFIX/c/msys64/usr/bin/curl.exe"
      }
      if(Test-Path 'C:/Windows/System32/curl.exe') {
        $acurl="-ac $env:POSIX_PATH_PREFIX/c/Windows/System32/curl.exe"
      }
      if($env:TESTING -eq 'ON') {
        if($env:BUILD_SYSTEM -eq 'CMake') {
          $env:TFLAGS="$acurl $env:DISABLED_TESTS"
          cmake --build . --config $env:PRJ_CFG --target test-ci
        }
        elseif($env:BUILD_SYSTEM -eq 'autotools') {
          & bash -e -c "cd $env:POSIX_PATH_PREFIX/c/projects/curl && make V=1 TFLAGS='$acurl $env:DISABLED_TESTS' test-ci"
        }
        else {
          & bash -e -c "cd $env:POSIX_PATH_PREFIX/c/projects/curl/tests && ./runtests.pl -a -p !flaky -r -rm $acurl $env:DISABLED_TESTS"
        }
      }

clone_depth: 10

# select branches to avoid testing feature branches twice (as branch and as pull request)
branches:
  only:
    - master
    - /\/ci$/

skip_commits:
  files:
    - '.azure-pipelines.yml'
    - '.circleci/**/*'
    - '.cirrus.yml'
    - '.github/**/*'
    - 'packages/**/*'
    - 'plan9/**/*'

artifacts:
  - path: '**/curl.exe'
    name: curl
  - path: '**/*curl*.dll'
    name: libcurl dll