#---------------------------------#
#      general configuration      #
#---------------------------------#

#version: 1.0.{build}-{branch}

# Do not build on tags (GitHub only)
skip_tags: true

#---------------------------------#
#    environment configuration    #
#---------------------------------#

# Operating system (build VM template)
os: Visual Studio 2015

environment:
  matrix:
    - APPVEYOR_JOB_ARCH:   x64
      D_COMPILER:          ldc
    - APPVEYOR_JOB_ARCH:   x86
      D_COMPILER:          dmd

#matrix:
#  allow_failures:
#    - APPVEYOR_JOB_ARCH: x86

# scripts that are called at very beginning, before repo cloning
init:
  - git config --global core.autocrlf input

# scripts that run after cloning repository
install:
  - cd c:\projects
  # Fetch submodules
  - cd ldc
  - git submodule update --init --recursive
  - cd ..
  # Clone libconfig (v1.6 does not work due to https://github.com/hyperrealm/libconfig/issues/47)
  - git clone https://github.com/hyperrealm/libconfig.git libconfig
  - cd libconfig
  - git checkout 7585cf6
  - cd ..
  # Download & extract libcurl
  - appveyor DownloadFile "http://d.darktech.org/libcurl-7.47.0-WinSSL-zlib-x86-x64.zip" -FileName libcurl.zip
  - md libcurl
  - cd libcurl
  - 7z x ..\libcurl.zip > nul
  - cd ..
  # Copy libcurl.dll to final LDC installation directory and add to PATH
  - md ldc-%APPVEYOR_JOB_ARCH% && md ldc-%APPVEYOR_JOB_ARCH%\bin
  - if "%APPVEYOR_JOB_ARCH%"=="x64" ( copy libcurl\dmd2\windows\bin64\libcurl.dll ldc-x64\bin )
  - if "%APPVEYOR_JOB_ARCH%"=="x86" ( copy libcurl\dmd2\windows\bin\libcurl.dll ldc-x86\bin )
  - set PATH=%CD%\ldc-%APPVEYOR_JOB_ARCH%\bin;%PATH%
  # Download & extract Ninja
  - appveyor DownloadFile "https://github.com/ninja-build/ninja/releases/download/v1.6.0/ninja-win.zip" -FileName ninja.zip
  - md ninja
  - cd ninja
  - 7z x ..\ninja.zip > nul
  - ninja --version
  - cd ..
  # Install lit
  - python --version
  - python -m pip install lit
  - python -c "import lit; lit.main();" --version
  # Download & extract D compiler
  - ps: |
        If ($Env:D_COMPILER -eq 'dmd') {
            $dmdVersion = '2.071.1'
            appveyor DownloadFile "http://downloads.dlang.org/releases/2.x/$dmdVersion/dmd.$dmdVersion.windows.7z" -FileName dmd2.7z
            7z x dmd2.7z > $null
            Set-Item -path env:DMD -value c:\projects\dmd2\windows\bin\dmd.exe
            c:\projects\dmd2\windows\bin\dmd.exe --version
        } Else {
            If ($Env:APPVEYOR_JOB_ARCH -eq 'x64') {
                appveyor DownloadFile 'http://github.com/ldc-developers/ldc/releases/download/v1.0.0/ldc2-1.0.0-win64-msvc.zip' -FileName ldc2.zip
                7z x ldc2.zip > $null
                Set-Item -path env:DMD -value c:\projects\ldc2-1.0.0-win64-msvc\bin\ldmd2.exe
                c:\projects\ldc2-1.0.0-win64-msvc\bin\ldc2 --version
            } Else {
                appveyor DownloadFile 'http://github.com/ldc-developers/ldc/releases/download/v1.0.0/ldc2-1.0.0-win32-msvc.zip' -FileName ldc2.zip
                7z x ldc2.zip > $null
                Set-Item -path env:DMD -value c:\projects\ldc2-1.0.0-win32-msvc\bin\ldmd2.exe
                c:\projects\ldc2-1.0.0-win32-msvc\bin\ldc2 --version
            }
        }
  # Download & extract GNU make + utils (for dmd-testsuite)
  - bash --version
  - appveyor DownloadFile "https://dl.dropboxusercontent.com/s/4y36f5ydgrk4p5g/make-4.2.1.7z?dl=0" -FileName make.7z
  - md make
  - cd make
  - 7z x ..\make.7z > nul
  - make --version
  - cd ..
  # Download & extract a pre-built LLVM (CMAKE_BUILD_TYPE=Release, LLVM_ENABLE_ASSERTIONS=ON)
  - ps: |
        If ($Env:APPVEYOR_JOB_ARCH -eq 'x64') {
            appveyor DownloadFile 'https://dl.dropboxusercontent.com/s/kkmr30ntlay843f/LLVM-3.9.0-x64.7z?dl=0' -FileName llvm-x64.7z
        } Else {
            appveyor DownloadFile 'https://dl.dropboxusercontent.com/s/7xbpwnf8zyeokw3/LLVM-3.9.0-x86.7z?dl=0' -FileName llvm-x86.7z
        }
  - md llvm-%APPVEYOR_JOB_ARCH%
  - cd llvm-%APPVEYOR_JOB_ARCH%
  - 7z x ..\llvm-%APPVEYOR_JOB_ARCH%.7z > nul
  - cd ..
  # Set environment variables
  - set PATH=%CD%\ninja;%CD%\make;%PATH%
  - if "%APPVEYOR_JOB_ARCH%"=="x64" ( call "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64 )
  - if "%APPVEYOR_JOB_ARCH%"=="x86" ( call "c:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86 )
  # Print environment info
  - set
  - msbuild /version
  - cl
  - cmake --version

#---------------------------------#
#       build configuration       #
#---------------------------------#

before_build:
  - cd c:\projects
  # Build libconfig
  - if "%APPVEYOR_JOB_ARCH%"=="x64" ( msbuild libconfig\lib\libconfig.vcxproj /p:Configuration=ReleaseStatic /p:Platform=x64 )
  - if "%APPVEYOR_JOB_ARCH%"=="x86" ( msbuild libconfig\lib\libconfig.vcxproj /p:Configuration=ReleaseStatic /p:Platform=Win32 )

build_script:
  - cd c:\projects
  # Generate build files for LDC
  - md ninja-ldc
  - cd ninja-ldc
  - if "%APPVEYOR_JOB_ARCH%"=="x64" ( cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=c:\projects\ldc-x64 -DLLVM_ROOT_DIR=c:/projects/llvm-x64 -DLIBCONFIG_INCLUDE_DIR=c:/projects/libconfig/lib -DLIBCONFIG_LIBRARY=c:/projects/libconfig/lib/x64/ReleaseStatic/libconfig.lib ..\ldc )
  - if "%APPVEYOR_JOB_ARCH%"=="x86" ( cmake -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=c:\projects\ldc-x86 -DLLVM_ROOT_DIR=c:/projects/llvm-x86 -DLIBCONFIG_INCLUDE_DIR=c:/projects/libconfig/lib -DLIBCONFIG_LIBRARY=c:/projects/libconfig/lib/ReleaseStatic/libconfig.lib ..\ldc )
  # Build LDC, druntime and phobos
  - ninja -j2

after_build:
  # install LDC, compress & publish as artifact
  - ps: |
        echo 'Preparing artifact...'
        cd c:\projects\ninja-ldc
        ninja install > $null
        copy bin\ldc2.pdb "..\ldc-$Env:APPVEYOR_JOB_ARCH\bin"
        cd "..\ldc-$Env:APPVEYOR_JOB_ARCH"
        (gc etc\ldc2.conf).replace("C:/projects/ldc-$Env:APPVEYOR_JOB_ARCH/", '%%ldcbinarypath%%/../') | sc etc\ldc2.conf
        $artifactFilename = "LDC-master-$Env:APPVEYOR_BUILD_NUMBER-$Env:APPVEYOR_JOB_ARCH.7z"
        7z a "..\$artifactFilename" * > $null
        cd ..
        Push-AppveyorArtifact $artifactFilename
  # x86 job: Create a 32/64-bit multilib artifact if the commit is on the master branch.
  # It consists of the x64 artifact (published by a successful x64 job) downloaded from
  # GitHub, augmented by the 32-bit libs and an extra section in the ldc2.conf file for `-m32`
  # (using the lib32 directory).
  - ps: |
        If (($Env:APPVEYOR_REPO_BRANCH -eq 'master') -And !(Test-Path Env:APPVEYOR_PULL_REQUEST_NUMBER) -And ($Env:APPVEYOR_JOB_ARCH -eq 'x86')) {
            echo 'Preparing 32/64-bit multilib artifact...'
            cd c:\projects
            $artifact64Downloaded = $True
            Try {
                (New-Object Net.WebClient).DownloadFile("https://github.com/ldc-developers/ldc/releases/download/LDC-Win64-master/LDC-master-$Env:APPVEYOR_BUILD_NUMBER-x64.7z", 'c:\projects\LDC-master-x64.7z')
            }
            Catch {
                echo 'Failed to download the 64-bit artifact from GitHub (the x64 job probably failed).'
                echo 'Skipping the 32/64-bit multilib artifact.'
                $artifact64Downloaded = $False
            }
            If ($artifact64Downloaded) {
                md ldc-multilib > $null
                cd ldc-multilib
                7z x ..\LDC-master-x64.7z > $null
                ren lib lib64
                copy ..\ldc-x86\lib -Recurse
                ren lib lib32
                (gc etc\ldc2.conf).replace('%%ldcbinarypath%%/../lib', '%%ldcbinarypath%%/../lib64') | sc etc\ldc2.conf
                $conf32 = gc ..\ldc-x86\etc\ldc2.conf -Raw
                $conf32 = "`r`ni686-pc-windows-msvc:" + $conf32.Substring($conf32.IndexOf("`r`ndefault:") + 10)
                $conf32 = $conf32.Replace('%%ldcbinarypath%%/../lib', '%%ldcbinarypath%%/../lib32')
                ac etc\ldc2.conf $conf32
                $artifactFilename = "LDC-master-$Env:APPVEYOR_BUILD_NUMBER-multilib.7z"
                7z a "..\$artifactFilename" * > $null
                cd ..
                Push-AppveyorArtifact $artifactFilename
            }
        }

#---------------------------------#
#       test configuration        #
#---------------------------------#

test_script:
  - cd c:\projects\ninja-ldc
  - bin\ldc2 -version
  # Compile, link & execute a hello-world program
  - ps: 'echo "import std.stdio; void main() { writeln(""Hello world!""); }" > hello.d'
  - bin\ldc2 hello.d
  - hello.exe
  # Compile the druntime & phobos unit tests
  - ninja -j2 druntime-ldc-unittest-debug phobos2-ldc-unittest-debug druntime-ldc-unittest phobos2-ldc-unittest
  # Execute dmd-testsuite
  - if "%APPVEYOR_JOB_ARCH%"=="x64" ( set OS=Win_64) else ( set OS=Win_32)
  - set DMD_TESTSUITE_MAKE_ARGS=-j2
  - ctest --verbose -R dmd-testsuite
  # Execute the LLVM IR tests
  - ctest --output-on-failure -V -R lit-tests
  # Execute the unit tests
  - ctest -j2 --output-on-failure -E "dmd-testsuite|lit-tests"

#---------------------------------#
#     deployment configuration    #
#---------------------------------#

deploy:
  - provider: GitHub
    release: 'LDC Win64 master'
    description: "Latest successful Windows CI builds of branch 'master'"
    draft: true
    prerelease: true
    auth_token:
      secure: qnbD8agL9mr0SFvy/sMkR2E29oQQ427T5zYwVVZkjRS3IZ361tG+9jlSiyEkyULy
    artifact: LDC-master-$(APPVEYOR_BUILD_NUMBER)-$(APPVEYOR_JOB_ARCH).7z
    on:
      branch: master
  - provider: GitHub
    release: 'LDC Win64 master'
    description: "Latest successful Windows CI builds of branch 'master'"
    draft: true
    prerelease: true
    auth_token:
      secure: qnbD8agL9mr0SFvy/sMkR2E29oQQ427T5zYwVVZkjRS3IZ361tG+9jlSiyEkyULy
    artifact: LDC-master-$(APPVEYOR_BUILD_NUMBER)-multilib.7z
    on:
      branch: master
      APPVEYOR_JOB_ARCH: x86
