# Specify version format
version: "3.6.0-{build}"

image:
  - Visual Studio 2017

# to add several platforms to build matrix
platform:
  - x64

configuration:
  - Release

environment:
  VISP_OPENCV_MINGW_FILE: opencv-4.1.0-MinGW-build-package.7z
  VISP_OPENCV_MINGW_DIR: opencv-4.1.0-MinGW-build-package
  VISP_OPENCV_MINGW_PKG: https://github.com/lagadic/visp-dependencies/blob/master/opencv-4.1.0-MinGW-x86_64-7.3.0-release-posix-seh-rt_v5-rev0-build-package.7z?raw=true
  VISP_OPENCV_VS2015_2017_FILE: opencv-4.1.0-vc14_vc15.exe
  VISP_OPENCV_VS2015_2017_DIR: opencv-4.1.0-vc14_vc15
  VISP_OPENCV_VS2015_2017_PKG: https://github.com/opencv/opencv/releases/download/4.1.0/opencv-4.1.0-vc14_vc15.exe

  matrix:
    - TARGET: mingw
    # - TARGET: msvc
    - TARGET: uwp
    - TARGET: visp_sample

# Scripts that are called at very beginning, before repo cloning
init:
  - cmake --version
  - msbuild /version

install:
  # Create temp dir to collect test outputs
  - md C:\temp

  # All external dependencies are installed in C:\projects\deps
  - mkdir C:\projects\deps
  - cd C:\projects\deps

  # visp-images
  - git clone --depth 1 https://github.com/lagadic/visp-images
  - set VISP_INPUT_IMAGE_PATH=C:\projects\deps\visp-images

  # visp_sample
  - git clone --depth 1 https://github.com/lagadic/visp_sample

  # opencv
  - ps: >-
      if ((${env:TARGET} -imatch "msvc" -or ${env:TARGET} -imatch "visp_sample") -and !(Test-Path ${env:VISP_OPENCV_VS2015_2017_FILE})) {
        echo "Downloading OpenCV Visual Studio from ${env:VISP_OPENCV_VS2015_2017_PKG} ..."
        appveyor DownloadFile "${env:VISP_OPENCV_VS2015_2017_PKG}" -FileName ${env:VISP_OPENCV_VS2015_2017_FILE} -Timeout 1200000
      }

  - ps: >-
      if (${env:TARGET} -imatch "msvc" -or ${env:TARGET} -imatch "visp_sample") {
        echo "Extracting OpenCV Visual Studio to C:\projects\deps ..."
        7z x ${env:VISP_OPENCV_VS2015_2017_FILE} -y "-o${env:VISP_OPENCV_VS2015_2017_DIR}"
      }

  - ps: >-
      if (${env:TARGET} -imatch "mingw" -and !(Test-Path ${env:VISP_OPENCV_MINGW_FILE})) {
        echo "Downloading OpenCV MinGW from ${env:VISP_OPENCV_MINGW_PKG} ..."
        appveyor DownloadFile "${env:VISP_OPENCV_MINGW_PKG}" -FileName ${env:VISP_OPENCV_MINGW_FILE} -Timeout 1200000
      }

  - ps: >-
      if (${env:TARGET} -imatch "mingw") {
        echo "Extracting OpenCV MinGW to C:\projects\deps ..."
        7z x ${env:VISP_OPENCV_MINGW_FILE} -y "-o${env:VISP_OPENCV_MINGW_DIR}"
      }

cache:
  - opencv-4.1.0-MinGW-build-package.7z -> appveyor.yml
  - opencv-4.1.0-vc14_vc15.exe -> appveyor.yml

# visp clone directory
clone_folder: C:\projects\visp\visp

before_build:
  # Setup path for mingw
  # Use MinGW-w64 in C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin
  - if "%TARGET%"=="mingw" set MINGW_DIR=C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64
  # Workaround for CMake not wanting sh.exe on PATH for MinGW
  - if "%TARGET%"=="mingw" set PATH=%PATH:C:\Program Files\Git\usr\bin;=%
  - if "%TARGET%"=="mingw" set PATH=C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin;%PATH%
  - if "%TARGET%"=="mingw" dir C:\mingw-w64\x86_64-7.3.0-posix-seh-rt_v5-rev0\mingw64\bin
  - if "%TARGET%"=="mingw" mingw32-make --version

  # Setup path for ViSP
  - if "%TARGET%"=="msvc" set VISP_DLL_DIR=C:\projects\visp\build\install\%platform%\vc15\bin
  - if "%TARGET%"=="msvc" set PATH=%VISP_DLL_DIR%;%PATH%
  - if "%TARGET%"=="mingw" set VISP_DLL_DIR=C:\projects\visp\build\install\%platform%\mingw\bin
  - if "%TARGET%"=="mingw" set PATH=%VISP_DLL_DIR%;%PATH%
  - if "%TARGET%"=="visp_sample" set VISP_DLL_DIR=C:\projects\visp\build\install\%platform%\vc15\bin
  - if "%TARGET%"=="visp_sample" set PATH=%VISP_DLL_DIR%;%PATH%

  # Setup path for OpenCV
  - if "%TARGET%"=="msvc" set OpenCV_DIR=C:\projects\deps\%VISP_OPENCV_VS2015_2017_DIR%\opencv\build
  - if "%TARGET%"=="msvc" set OpenCV_DLL_DIR=%OpenCV_DIR%\%platform%\vc15\bin
  - if "%TARGET%"=="msvc" set PATH=%OPENCV_DLL_DIR%;%PATH%
  - if "%TARGET%"=="mingw" set OpenCV_DIR=C:\projects\deps\%VISP_OPENCV_MINGW_DIR%\install
  - if "%TARGET%"=="mingw" set OpenCV_DLL_DIR=%OpenCV_DIR%\x64\mingw\bin
  - if "%TARGET%"=="mingw" set PATH=%OPENCV_DLL_DIR%;%PATH%
  - if "%TARGET%"=="visp_sample" set OpenCV_DIR=C:\projects\deps\%VISP_OPENCV_VS2015_2017_DIR%\opencv\build
  - if "%TARGET%"=="visp_sample" set OpenCV_DLL_DIR=%OpenCV_DIR%\%platform%\vc15\bin
  - if "%TARGET%"=="visp_sample" set PATH=%OPENCV_DLL_DIR%;%PATH%

build_script:
  - echo %PATH%
  - dir C:\projects\deps
  - dir C:\tools
  - md C:\projects\visp\build
  - cd C:\projects\visp\build

  # mingw case
  - if "%TARGET%"=="mingw" dir %OpenCV_DLL_DIR%
  - if "%TARGET%"=="mingw" cmake -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=%configuration% ..\visp
  - if "%TARGET%"=="mingw" cmake --build . --config %configuration% --target install -- -j2
  - if "%TARGET%"=="mingw" dir C:\projects\visp\build\install
  - if "%TARGET%"=="mingw" dir %VISP_DLL_DIR%
  - if "%TARGET%"=="mingw" ctest --output-on-failure

  # msvc case
  - if "%TARGET%"=="msvc" dir %OpenCV_DLL_DIR%
  - if "%TARGET%"=="msvc" cmake -G "Visual Studio 15 2017" -A %platform% ..\visp
  - if "%TARGET%"=="msvc" cmake --build . --config %configuration% --target install -- /m:2
  - if "%TARGET%"=="msvc" dir C:\projects\visp\build\install
  - if "%TARGET%"=="msvc" dir %VISP_DLL_DIR%
  - if "%TARGET%"=="msvc" ctest --output-on-failure

  # uwp case
  - if "%TARGET%"=="uwp" cmake -G "Visual Studio 15 2017" -A %platform% -DCMAKE_SYSTEM_NAME="WindowsStore" -DCMAKE_SYSTEM_VERSION="10.0" -DBUILD_DEMOS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_TUTORIALS=OFF ..\visp
  - if "%TARGET%"=="uwp" cmake --build . --config %configuration% -- /m:2

  # visp_sample case: ViSP as 3rdparty with cmake
  - if "%TARGET%"=="visp_sample" dir %OpenCV_DLL_DIR%
  - if "%TARGET%"=="visp_sample" cmake -G "Visual Studio 15 2017" -A %platform% -DBUILD_DEMOS=OFF -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF -DBUILD_TUTORIALS=OFF ..\visp
  - if "%TARGET%"=="visp_sample" cmake --build . --config %configuration% --target install -- /m:2
  - if "%TARGET%"=="visp_sample" cd C:\projects\deps\visp_sample
  - if "%TARGET%"=="visp_sample" mkdir build
  - if "%TARGET%"=="visp_sample" cd build
  - if "%TARGET%"=="visp_sample" cmake -G "Visual Studio 15 2017" -A %platform% .. -DVISP_DIR=C:\projects\visp\build\install
  - if "%TARGET%"=="visp_sample" cmake --build . --config %configuration% -- /m:2
