# Psi4 pipeline

trigger:
  - master
  - 1.3.x

variables:
  mkl.version: '2019.1'

jobs:

  # Configure, build, install, and test job
  - job: 'build'
    pool:
      vmImage: 'vs2015-win2012r2'
    timeoutInMinutes: 360
    steps:

      # Check Python
      - script: |
          setlocal EnableDelayedExpansion
          echo python.version: %PYTHON_VERSION%
          set VALID=false
          if "%PYTHON_VERSION%"=="3.6" set VALID=true
          if "%PYTHON_VERSION%"=="3.7" set VALID=true
          if "!VALID!"=="false" (
            echo ERROR: Invalid "python.version" value: "%PYTHON_VERSION%". Valid values: "3.6" and "3.7".
            exit 1
          )
        displayName: "Check Python version: $(python.version)"

      # Check cmake configuration
      - script: |
          setlocal EnableDelayedExpansion
          echo cmake.build_type: %CMAKE_BUILD_TYPE%
          set VALID=false
          if "%CMAKE_BUILD_TYPE%"=="Debug" set VALID=true
          if "%CMAKE_BUILD_TYPE%"=="Release" set VALID=true
          if "!VALID!"=="false" (
            echo ERROR: Invalid "cmake.build_type" value: "%CMAKE_BUILD_TYPE%". Valid values: "Debug" and "Release".
            exit 1
          )
        displayName: "Check cmake configuration"

      # Check ctest configuration
      - script: |
          setlocal EnableDelayedExpansion
          echo ctest.type: %CTEST_TYPE%
          set VALID=false
          if "%CTEST_TYPE%"=="quick" set VALID=true
          if "%CTEST_TYPE%"=="full" set VALID=true
          if "!VALID!"=="false" (
            echo ERROR: Invalid "ctest.type" value: "%CTEST_TYPE%". Valid values: "quick" and "full".
            exit 1
          )
        displayName: "Check ctest configuration"

      # Check pytest configuration
      - script: |
          setlocal EnableDelayedExpansion
          echo pytest.type: %PYTEST_TYPE%
          set VALID=false
          if "%PYTEST_TYPE%"=="quick" set VALID=true
          if "%PYTEST_TYPE%"=="full" set VALID=true
          if "!VALID!"=="false" (
            echo ERROR: Invalid "pytest.type" value: "%PYTEST_TYPE%". Valid values: "quick" and "full".
            exit 1
          )
        displayName: "Check pytest configuration"

      # Check conda configuration
      - script: |
          setlocal EnableDelayedExpansion
          echo conda.build: %CONDA_BUILD%
          set VALID=false
          if "%CONDA_BUILD%"=="true" set VALID=true
          if "%CONDA_BUILD%"=="false" set VALID=true
          if "!VALID!"=="false" (
            echo ERROR: Invalid "conda.build" value: "%CONDA_BUILD%". Valid values: "true" and "false".
            exit 1
          )
        displayName: "Check conda configuration"

      # Install Chocolatey (https://chocolatey.org/install#install-with-powershellexe)
      - powershell: |
          Set-ExecutionPolicy Bypass -Scope Process -Force
          iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
          Write-Host "##vso[task.setvariable variable=PATH]$env:PATH"
          choco --version
        displayName: "Install Chocolatey"

      # Install Miniconda
      - script: |
          choco install miniconda3 --yes
          set PATH=C:\tools\miniconda3\Scripts;C:\tools\miniconda3;C:\tools\miniconda3\Library\bin;%PATH%
          echo '##vso[task.setvariable variable=PATH]%PATH%'
          set LIB=C:\tools\miniconda3\Library\lib;%LIB%
          echo '##vso[task.setvariable variable=LIB]%LIB%'
          conda --version
        displayName: "Install Miniconda"

      # Configure Miniconda
      - script: |
          conda config --set always_yes yes
          conda config --append channels conda-forge
          conda config --append channels psi4
          conda info
        displayName: "Configure Miniconda"

      # Create conda enviroment
      # Note: conda activate doesn't work here, because it creates a new shell!
      - script: |
          conda install cmake ^
                        conda-build ^
                        conda-verify ^
                        deepdiff ^
                        intel-openmp=%MKL_VERSION% ^
                        mkl-devel=%MKL_VERSION% ^
                        mpmath ^
                        networkx ^
                        ninja ^
                        numpy ^
                        pybind11 ^
                        pytest ^
                        python=%PYTHON_VERSION% ^
                        qcelemental
          conda list
        displayName: "Install conda packages"

      # Install LLVM
      # Note: LLVM distributed by conda is too old
      - script: |
          choco install llvm --yes
          set PATH=C:\Program Files\LLVM\bin;%PATH%
          echo '##vso[task.setvariable variable=PATH]%PATH%'
          clang-cl --version
        displayName: "Install LLVM"

      # Install Intel OpenMP import library
      # NOTE: libiomp5md.lib is located in conda/win/$(mkl.version)
      - script: |
          set LIB=$(Build.SourcesDirectory)\conda\win\$(mkl.version);%LIB%
          echo '##vso[task.setvariable variable=LIB]%LIB%'
        displayName: "Install Intel OpenMP import library"

      # Configure
      - script: |
          setlocal EnableDelayedExpansion
          call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64
          mkdir build & cd build
          if "%CMAKE_BUILD_TYPE%"=="Debug" set MAX_AM_ERI=6
          if "%CMAKE_BUILD_TYPE%"=="Release" set MAX_AM_ERI=8
          if not defined MAX_AM_ERI exit 1
          if "%CMAKE_BUILD_TYPE%"=="Debug" set ENABLE_XHOST=OFF
          if "%CMAKE_BUILD_TYPE%"=="Release" set ENABLE_XHOST=ON
          if not defined ENABLE_XHOST exit 1
          cmake -G Ninja ^
                -DCMAKE_BUILD_TYPE=%CMAKE_BUILD_TYPE% ^
                -DCMAKE_INSTALL_PREFIX=../install ^
                -DCMAKE_C_COMPILER=clang-cl ^
                -DCMAKE_CXX_COMPILER=clang-cl ^
                -DENABLE_XHOST=!ENABLE_XHOST! ^
                -DMAX_AM_ERI=!MAX_AM_ERI! ^
                $(Build.SourcesDirectory)
        displayName: "Configure Psi4"
        workingDirectory: $(Build.BinariesDirectory)

      # Build
      - script: |
          call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64
          cmake --build . ^
                --config %CMAKE_BUILD_TYPE% ^
                -- -j %NUMBER_OF_PROCESSORS%
        displayName: "Build Psi4"
        workingDirectory: $(Build.BinariesDirectory)/build

      # Install
      - script: |
          call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64
          cmake --build . ^
                --config %CMAKE_BUILD_TYPE% ^
                --target install ^
                -- -j %NUMBER_OF_PROCESSORS%
        displayName: "Install Psi4"
        workingDirectory: $(Build.BinariesDirectory)/build

      # Test (OpenMP)
      # Check if OpenMP configuration had not failed silently
      - script: |
          python -c "from psi4 import core; core.set_num_threads(42); assert core.get_num_threads() == 42"
        displayName: "Test Psi4 (OpenMP)"
        workingDirectory: $(Build.BinariesDirectory)/install/lib

      # Test (ctest)
      - script: |
          setlocal EnableDelayedExpansion
          if "%CTEST_TYPE%"=="full" set CTEST_TYPE=".*"
          ctest --build-config %CMAKE_BUILD_TYPE% ^
                --label-regex !CTEST_TYPE! ^
                --output-on-failure ^
                --parallel %NUMBER_OF_PROCESSORS% ^
                --timeout 3000
        displayName: "Test Psi4 (ctest $(ctest.type))"
        workingDirectory: $(Build.BinariesDirectory)/build

      # Test (pytest)
      - script: |
          set PATH=$(Build.BinariesDirectory)\install\bin;%PATH%
          set PYTHONPATH=$(Build.BinariesDirectory)\install\lib;%PYTHONPATH%
          psi4 --test %PYTEST_TYPE%
        displayName: "Test Psi4 (pytest $(pytest.type))"
        workingDirectory: $(Build.BinariesDirectory)

      # Build package
      - script: |
          set INSTALL_DIR=$(Build.BinariesDirectory)\install
          conda build --python %PYTHON_VERSION% ^
                      conda\win
          copy /y C:\tools\miniconda3\conda-bld\win-64\psi4-*.bz2 $(Build.ArtifactStagingDirectory)
        condition: and(succeeded(), eq(variables['conda.build'], 'true'))
        displayName: "Build Psi4 package"

      # Publish package
      - task: PublishBuildArtifacts@1
        inputs:
          artifactName: conda
        condition: and(succeeded(), eq(variables['conda.build'], 'true'))
        displayName: "Publish Psi4 package"
