version: 2.38.{build}

image: Visual Studio 2015

environment:
  # Required so bash starts in the same folder as from where it was called.
  CHERE_INVOKING: 1
  matrix:
    - compiler: msbuild
    #- compiler: msys2
    #- compiler: cygwin

configuration:
  - Debug
  - Release

install:
  # Retrieve submodules, dependencies are stored there.
  - git submodule update --init
  # Only has to be installed when MSYS2 or Cygwin are enabled.
  #- cinst ghostscript
  # Compile libgd
  - cd windows\dependencies\libgd
  - mkdir deps
  - cd deps
  - ps: wget http://windows.php.net/downloads/php-sdk/deps/vc14/x86/archives/freetype-2.6.2-vc14-x86.zip -O freetype.zip
  - ps: wget http://windows.php.net/downloads/php-sdk/deps/vc14/x86/libiconv-1.14-vc14-x86.zip -O iconv.zip
  - ps: wget http://windows.php.net/downloads/php-sdk/deps/vc14/x86/libjpeg-9b-vc14-x86.zip -O jpeg.zip
  - ps: wget http://windows.php.net/downloads/php-sdk/deps/vc14/x86/libpng-1.6.21-vc14-x86.zip -O png.zip
  - ps: wget http://windows.php.net/downloads/php-sdk/deps/vc14/x86/zlib-1.2.8-vc14-x86.zip -O zlib.zip
  - 7z x freetype.zip
  - 7z x iconv.zip
  - 7z x jpeg.zip
  - 7z x png.zip
  - 7z x zlib.zip
  - cd ..
  - cmd: >-
      call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86

      set WITH_DEVEL=deps

      set WITH_BUILD=build

      nmake /f windows/Makefile.vc build_libs

before_build:
# This file is deleted to reduce noise in the build output. Otherwise several 
# "target does not exist and will be ignored" messages are printed.
- del "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets"

build_script:
  # Ghostscript only has to be configured when MSYS2 or Cygwin are enabled.
  ## Add Ghostscript folder to the path so ps2pdf can be called to create docs.
  ## When executing ps2pdf, 'gs' gets called. On Windows the program is required program is called gswin64c.exe,
  ## so we create a symlink called 'gs' that points to gswin64c.
  ##- SET PATH="%PATH%;C:\Program Files\gs\gs9.19\lib;C:\Program Files\gs\gs9.19\bin;"
  ##- mklink "C:\Program Files\gs\gs9.19\bin\gs" "C:\Program Files\gs\gs9.19\bin\gswin64c.exe"
  - ps: >-
      cd $env:APPVEYOR_BUILD_FOLDER 

      if($env:compiler -eq "msbuild") {
        $env:Path += ";" + $env:APPVEYOR_BUILD_FOLDER + "\windows\dependencies\graphviz-build-utilities";
        msbuild /p:Configuration=$env:configuration;
        if($env:configuration -eq "Release") {
          rm Release\Graphviz\bin\*.lastcodeanalysissucceeded;
          rm Release\Graphviz\bin\*.iobj;
          rm Release\Graphviz\bin\*.ipdb;
          rm Release\Graphviz\bin\*.ilk;
        }

        # Append build destination to the PATH, configure dot and execute regression tests 
        $env:Path += ";" + $env:APPVEYOR_BUILD_FOLDER + "\" + $env:configuration + "\Graphviz\bin";
        dot -c;
        cd tests\regression_tests;
        ./regression_tests.bat; 
      }
      
      if ($env:compiler -eq "msys2" -Or $env:compiler -eq "cygwin") {
        $BashPath = ""

        if ($env:compiler -eq "msys2") {
          $BashPath = "C:\msys64\usr\bin\bash"
          & $BashPath -lc "pacman -S --noconfirm autoconf-archive mingw-w64-x86_64-cairo mingw-w64-x86_64-freetype mingw-w64-x86_64-fontconfig mingw-w64-x86_64-libpng mingw-w64-x86_64-pango"
        }

        if ($env:compiler -eq "cygwin") {
          C:\cygwin64\setup-x86_64.exe -qnNdO -R C:/cygwin64 -s http://cygwin.mirror.constant.com -l C:/cygwin64/var/cache/setup -P autoconf-archive -P flex -P swig -P mingw64-x86_64-cairo -P expat -P mingw64-x86_64-freetype -P mingw64-x86_64-fontconfig -P mingw64-x86_64-glib2.0 -P mingw64-x86_64-libpng -P mingw64-x86_64-pango -P zlib -P mingw64-x86_64-libpng
          $BashPath = "C:\cygwin64\bin\bash"
          # For some reason Cygwin ignores CHERE_INVOKING=1 for the first bash command.
          # Executing some arbitrary command solves this. So without this next line, the build will fail.
          & $BashPath -lc "ls"
        }

        Write-Output "Execute ./autogen.sh, which also executes ./configure"

        & $BashPath -lc "./autogen.sh"
        
        Write-Output "Execute make"

        & $BashPath -lc "make"
      }

artifacts:
  - path: Release
    name: graphviz-windows
  - path: Debug
    name: graphviz-windows-debug
