trigger:
  paths:
    include:
      - '*'
    exclude:
      - '.azure/azure-pipelines-android.yml'
      - '.azure/azure-pipelines-mac.yml'
      - '.azure/azure-pipelines-win.yml'
      - '.circleci/config.yml'
      - '.github/workflows/linux.yml'
      - '.github/workflows/sonarcloud.yml'
      - '.cirrus.yml'
      - 'README.md'

parameters:
- name: UseCache
  displayName: Use Dependency Cache
  type: boolean
  default: true

variables:
  toolset: 'v142'
  generator: 'Visual Studio 16 2019'
  configuration: 'RelWithDebInfo'
  VCPKG_REVISION: 'e6dcc079c81161786eb7b052209a2047e79f2c6c'

jobs:
- job: vcpkg
  workspace:
    clean: all
  strategy:
    matrix:
      ARM:
        platform: 'arm'
        cmake_platform: 'ARM'
      ARM64:
        platform: 'arm64'
        cmake_platform: 'ARM64'
      x86:
        platform: 'x86'
        cmake_platform: 'Win32'
      x64:
        platform: 'x64'
        cmake_platform: 'x64'
  pool:
    vmImage: 'windows-2019'
  steps:
    - task: Cache@2
      displayName: "Cache vcpkg's packages"
      condition: and(not(in(variables['Build.Reason'], 'Schedule')), ${{ parameters.useCache }})
      inputs:
        key: $(VCPKG_REVISION) | "$(platform)"
        path: '$(VCPKG_INSTALLATION_ROOT)\installed'
        cacheHitVar: CACHE_RESTORED
    - bash: |
        set -ex
        echo $(generator)
        echo $(toolset)
        # choco upgrade ninja -y
        # ninja --version
        cmake --version
        rm -rf C:/Strawberry/perl/bin/pkg-config*
        choco install --svc --sdc -i pkgconfiglite &
        chocoTask=$!
        # manually update vcpkg
        cd $VCPKG_INSTALLATION_ROOT
        # git checkout master
        git fetch origin
        git checkout $(VCPKG_REVISION)
        sed -i 's/arm64/arm/g' ports/glib/portfile.cmake
        ./bootstrap-vcpkg.sh
        wait $chocoTask
        which pkg-config
      displayName: 'Update vcpkg'
    - task: DownloadBuildArtifacts@0
      inputs:
        buildType: specific
        # https://dev.azure.com/tommbrt/_apis/projects?api-version=5.0
        project: 'd3638885-de4a-4ce7-afe7-f237ae461c07'
        pipeline: 1
        artifactName: libinstpatch-$(cmake_platform)
        downloadPath: '$(Agent.TempDirectory)'
      displayName: 'Get libinstpatch'
      condition: and(succeeded(), and(ne(variables['platform'], 'arm'), ne(variables['platform'], 'arm64')))
    - task: CopyFiles@2
      inputs:
        SourceFolder: '$(Agent.TempDirectory)\libinstpatch-$(cmake_platform)'
        Contents: '**'
        TargetFolder: '$(VCPKG_INSTALLATION_ROOT)\installed\$(platform)-windows'
      displayName: 'Install libinstpatch'
      condition: and(succeeded(), and(ne(variables['platform'], 'arm'), ne(variables['platform'], 'arm64')))
    - bash: |
        set -ex
        vcpkg install glib:$(platform)-windows libsndfile:$(platform)-windows
      displayName: 'vcpkg build Dependencies'
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))
    - bash: |
        set -ex
        cd $VCPKG_INSTALLATION_ROOT/installed/$(platform)-windows/
        pwd
        rm -rf include/FLAC* include/ffi* include/iconv* include/opus tools share
        ls -Rg .
        pkg-config --list-all
        pkg-config --cflags libinstpatch-1.0 || true
      displayName: 'Cleanup dependencies'
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))
    - bash: |
        set -ex
        mkdir build
        cd build
        cmake -Werror=dev -G "$(generator)" -A "$(cmake_platform)" -T "$(toolset)" -Denable-readline=0 -DCMAKE_TOOLCHAIN_FILE=$VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake -DCMAKE_INSTALL_PREFIX=$VCPKG_INSTALLATION_ROOT/installed/$(platform)-windows -DCMAKE_VERBOSE_MAKEFILE=1 -DNO_GUI=1 ..
        cmake --build . --config $(configuration) --parallel 3
      displayName: 'Compile fluidsynth'
    - bash: |
        set -ex
        cmake --build build --config $(configuration) --target check --parallel 3
      displayName: 'Execute Unittests'
      condition: and(succeeded(), and(ne(variables['platform'], 'arm'), ne(variables['platform'], 'arm64')))
    - script: |
        @ECHO ON
        cd build
        cmake --build . --config $(configuration) --target install || exit -1
        REM del bin\concrt*.dll
        REM del $(Build.ArtifactStagingDirectory)\bin\vcruntime*.dll
        REM del $(Build.ArtifactStagingDirectory)\bin\msvcp*.dll
        REM del $(Build.ArtifactStagingDirectory)\lib\instpatch*.lib
        REM del $(Build.ArtifactStagingDirectory)\lib\pkgconfig\libinstpatch*.pc
        REM rd $(Build.ArtifactStagingDirectory)\include\libinstpatch-2 /s /q
      displayName: 'fluidsynth install'
    - task: CopyFiles@2
      inputs:
        SourceFolder: '$(VCPKG_INSTALLATION_ROOT)/installed/$(platform)-windows/'
        Contents: '**'
        TargetFolder: '$(Build.ArtifactStagingDirectory)'
        CleanTargetFolder: true
      displayName: 'Copy Binaries to Artifact Dir'
    - task: PublishBuildArtifacts@1
      inputs:
          pathtoPublish: $(Build.ArtifactStagingDirectory)
          artifactName: fluidsynth-vcpkg-$(platform)