
trigger:
  paths:
    include:
      - '*'
    exclude:
      - '.azure/azure-pipelines-android.yml'
      - '.azure/cmake-android.yml'
      - '.azure/azure-pipelines-vcpkg.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

jobs:
- job: macOS_brew
  strategy:
    matrix:
      UnixLibs11:
        imageName: 'macos-11'
        CMakeFlags: '-Denable-framework=0'
        ReadlinePkgConf: ''
      UnixLibs11_static:
        imageName: 'macos-11'
        CMakeFlags: '-Denable-framework=0 -DBUILD_SHARED_LIBS=0'
        ReadlinePkgConf: ''
      UnixLibs11_static_pkgconf_readline:
        imageName: 'macos-11'
        CMakeFlags: '-Denable-framework=0 -DBUILD_SHARED_LIBS=0'
        ReadlinePkgConf: '$(brew --prefix readline)/lib/pkgconfig'
      UnixLibs12:
        imageName: 'macos-12'
        CMakeFlags: '-Denable-framework=0'
        ReadlinePkgConf: ''
      UnixLibs12_static:
        imageName: 'macos-12'
        CMakeFlags: '-Denable-framework=0 -DBUILD_SHARED_LIBS=0'
        ReadlinePkgConf: ''
      UnixLibs12_static_pkgconf_readline:
        imageName: 'macos-12'
        CMakeFlags: '-Denable-framework=0 -DBUILD_SHARED_LIBS=0'
        ReadlinePkgConf: '$(brew --prefix readline)/lib/pkgconfig'
      FrameWork11:
        imageName: 'macos-11'
        CMakeFlags: ''
        ReadlinePkgConf: ''
      FrameWork12:
        imageName: 'macos-12'
        CMakeFlags: ''
        ReadlinePkgConf: ''

  pool:
    vmImage: $(imageName)
# recommended by https://github.com/Homebrew/brew/issues/2491#issuecomment-294207661
#        brew update || brew update
#        brew upgrade $PACKAGES
  steps:
    - script: |
        set -ex
        PACKAGES="glib gobject-introspection libsndfile pkg-config jack dbus-glib pulseaudio portaudio sdl2 libomp readline"
        brew install $PACKAGES
      displayName: 'Prerequisites'
    - script: |
        set -ex
        mkdir build && cd build
        export PKG_CONFIG_PATH="$(brew --prefix libffi)/lib/pkgconfig:$(ReadlinePkgConf)"
        # assist cmake in finding openmp installed via homebrew
        cmake -Werror=dev $(CMakeFlags) -DCMAKE_BUILD_TYPE=Release -DCMAKE_VERBOSE_MAKEFILE=1 -DNO_GUI=1 -DCMAKE_PREFIX_PATH=$(brew --prefix libomp) ..
        make -j3
      displayName: 'Compile fluidsynth'
    - script: |
        set -ex
        cd build
        make -j3 check
      displayName: 'Execute Unittests'
    - script: |
        set -ex
        cd build
        make -j3 demo
      displayName: 'Compile demos'
    - script: |
        set -ex
        cd build
        sudo make install
        rm -f install_manifest.txt
      displayName: 'Install fluidsynth to default location'
    - script: |
        set -ex
        cd build
        cmake -DCMAKE_INSTALL_PREFIX=$(Build.ArtifactStagingDirectory) ..
        make install
      displayName: 'Install fluidsynth to artifact dir'
    - script: |
        set -e
        cat << EOF > static-link-test.c
        #include <fluidsynth.h>
        int main() { fluid_synth_process(0,0,0,0,0,0); return 0; }
        EOF
        export PKG_CONFIG_PATH=$(Build.ArtifactStagingDirectory)/lib/pkgconfig:$(ReadlinePkgConf)
        set -x
        cc -o static-link-test static-link-test.c $(pkg-config --cflags --static fluidsynth) $(pkg-config --libs --static fluidsynth)
        ./static-link-test
        echo $?
      displayName: 'Consume from pkg-config'
      condition: and(succeeded(), contains(variables.CMakeFlags, 'Denable-framework=0'))


- job: macOS_ports
  timeoutInMinutes: 300
  strategy:
    matrix:
      # SDL2/SDL_cpuinfo.h includes some x86 specific headers, which ofc doesn't work when cross compiling for arm64
      # And this universal build thingy doesn't work on Mac10.15 for some reason...
      # Furthermore, there is a problem when restoring the cache on Mac11, so disable this job as well
      #11_0_universal_unixlibs:
      #  macPortsUrl: 'https://github.com/macports/macports-base/releases/download/v2.7.2/MacPorts-2.7.2-11-BigSur.pkg'
      #  imageName: 'macos-11'
      #  CMakeFlags: '-Denable-sdl2=0 -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -Denable-framework=0
      12_0_universal_unixlibs:
        macPortsUrl: 'https://github.com/macports/macports-base/releases/download/v2.7.2/MacPorts-2.7.2-12-Monterey.pkg'
        imageName: 'macos-12'
        CMakeFlags: '-Denable-sdl2=0 -DCMAKE_OSX_ARCHITECTURES="arm64;x86_64" -Denable-framework=0'
  pool:
    vmImage: $(imageName)
  steps:
    - script: |
        set -ex
        brew install wget pkg-config
        wget $(macPortsUrl)
        sudo installer -pkg *.pkg -target /
        rm -f *.pkg
        sudo chown -R $(id -u):$(id -g) /opt/local
        sudo chflags nouchg /opt/local
      displayName: 'Prerequisites'
      workingDirectory: $(Agent.TempDirectory)
    - task: Cache@2
      continueOnError: true
      displayName: "Cache macPort packages"
      condition: and(not(in(variables['Build.Reason'], 'Schedule')), ${{ parameters.useCache }})
      inputs:
        key: '"$(Agent.OS)" | "$(imageName)" | "$(macPortsUrl)" | "versionalways"'
        path: '/opt/local'
        cacheHitVar: CACHE_RESTORED
    - script: |
        set -ex
        export PATH=$PATH:/opt/local/bin
        echo $PATH
        which codesign
        which port
        cmake --version || true

        echo "+universal" | sudo tee -a /opt/local/etc/macports/variants.conf

        sudo sh -c 'cat << EOF >> /opt/local/etc/macports/macports.conf
        buildfromsource     	always
        universal_archs     	arm64 x86_64
        ui_interactive     		no
        EOF'

        sudo port install glib2-devel libsndfile dbus-glib readline
        # fixup permissions to allow non-priv pipeline user access every directory, to make the caching step succeed at the end
        sudo chown -R $(id -u):$(id -g) /opt/local
        # remove all extended attributes, as they cannot be restored when restoring the pipeline cache
        #sudo xattr -rcv /opt/local
      displayName: 'Port install universal'
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))
    - script: |
        set -ex
        export PATH=$PATH:/opt/local/bin
        export PKG_CONFIG_PATH="/opt/local/lib/pkgconfig"
        mkdir build && cd build
        cmake -Werror=dev $(CMakeFlags) -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_VERBOSE_MAKEFILE=1 -DNO_GUI=1 ..
        make -j3
      displayName: 'Compile fluidsynth'
    - script: |
        export PATH=$PATH:/opt/local/bin
        sudo port -v install fluidsynth +universal
      displayName: 'port install fluidsynth +universal'
      condition: failed()
      enabled: false
    - script: |
        set -x
        cat /opt/local/var/macports/logs/*cmake-bootstrap/cmake-bootstrap/main.log
        cat /opt/local/var/macports/build/*cmake-bootstrap/cmake-bootstrap/work/cmake-3.9.4-arm64/Bootstrap.cmk/*.log
        cat /opt/local/var/macports/logs/*_fluidsynth/fluidsynth/main.log
      condition: failed()
      displayName: 'Print fluidsynth error log'
    - script: |
        set -ex
        cd build
        make -j3 check
      displayName: 'Execute Unittests'
    - script: |
        set -ex
        cd build
        make -j3 demo
      displayName: 'Compile demos'
    - script: |
        set -ex
        cd build
        sudo make install
        rm -f install_manifest.txt
      displayName: 'Install fluidsynth to default location'
    - script: |
        set -ex
        cd build
        cmake -DCMAKE_INSTALL_PREFIX=$(Build.ArtifactStagingDirectory) ..
        make install
        cd /opt/local/lib/
        cp -a libgthread*.dylib libglib*.dylib libintl*.dylib libsndfile*.dylib libdbus-*.dylib libreadline*.dylib $(Build.ArtifactStagingDirectory)/lib
        file /opt/local/lib/libglib-2.0.dylib $(Build.ArtifactStagingDirectory)/bin/fluidsynth $(Build.ArtifactStagingDirectory)/lib/libfluidsynth*.dylib
      displayName: 'Install fluidsynth to artifact dir'
    - task: PublishBuildArtifacts@1
      displayName: 'Publish fluidsynth artifacts'
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'fluidsynth-$(imageName)'
        publishLocation: 'Container'
    - task: PublishBuildArtifacts@1
      displayName: 'Publish macPorts deps'
      enabled: false
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))
      inputs:
        PathtoPublish: '/opt/local/lib'
        ArtifactName: 'macports-$(imageName)'
        publishLocation: 'Container'
