
trigger:
  paths:
    include:
      - '*'
    exclude:
      - '.azure/azure-pipelines-mac.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
  values:
  - true
  - false

schedules:
- cron: "0 0 * * 1"
  displayName: 'Weekly Monday Midnight build without caching'
  branches:
    include:
    - master
  always: true

variables:
  ICONV_VERSION: '1.16'
  # Use recent master libffi, because 3.3 is broken: checking host system type... Invalid configuration `arm-none-linux-eabi': machine `arm-none-linux' not recognized
  FFI_VERSION: '3.4.2'
  GETTEXT_VERSION: '0.21'
  #need to switch to meson build system to use a more recent version 
  GLIB_VERSION: '2.58'
  GLIB_EXTRAVERSION: '3'
  OBOE_VERSION: '1.5.0'
  SNDFILE_VERSION: '1.0.31'
  INSTPATCH_VERSION: '1.1.6'
  VORBIS_VERSION: '1.3.7'
  OGG_VERSION: '1.3.5'
  OPUS_VERSION: '1.3.1'
  FLAC_VERSION: '1.3.4'

  # Android NDK sources and standalone toolchain is put here
  DEV: '$(System.DefaultWorkingDirectory)/android-build-root'

  # This is a symlink pointing to the real Android NDK
  # Must be the same as $ANDROID_NDK_HOME see:
  # https://github.com/actions/virtual-environments/blob/main/images/linux/Ubuntu2004-README.md
  NDK: '/usr/local/lib/android/sdk/ndk-bundle'

  # All the built binaries, libs and their headers will be installed here
  PREFIX: '$(DEV)/opt/android'

  # Prevent installing to lib64/
  # This becomes important, if you would build on e.g. openSUSE
  LIBPATH0: '$(PREFIX)/lib'

  # The path of standalone NDK toolchain
  # Refer to https://developer.android.com/ndk/guides/standalone_toolchain.html
  NDK_TOOLCHAIN: '$(NDK)/toolchains/llvm/prebuilt/linux-x86_64/'

  # Don't mix up .pc files from your host and build target
  PKG_CONFIG_PATH: '$(LIBPATH0)/pkgconfig'
  # setting PKG_CONFIG_PATH alone does not seem to be enough to avoid mixing up with the host, also set PKG_CONFIG_LIBDIR
  PKG_CONFIG_LIBDIR: '$(PKG_CONFIG_PATH)'

  # Set Android target API level
  # when compiling with clang use at least 28 as this makes sure that android provides the posix_spawn functions, so the compilation of gettext will (should) work out of the box
  # it's probably a bug of gettext, if posix_spawn is not available it replaces it with its own implementation. Autotools of gettext set HAVE_POSIX_SPAWN==0 (which is correct) but for some reason REPLACE_POSIX_SPAWN==0 (which is wrong, as it should be 1).
  # 
  # NOTE: API 24 is required because it provides fseeko() and ftello() required by libflac
  ANDROID_API: '24'

  # Tell configure what flags Android requires.
  # Turn Wimplicit-function-declaration into errors. Else autotools will be fooled when checking for available functions (that in fact are NOT available) and compilation will fail later on.
  # Also disable clangs integrated assembler, as the hand written assembly of libffi is not recognized by it, cf. https://crbug.com/801303
  CFLAGS: "-fPIE -fPIC -I$(PREFIX)/include --sysroot=$(NDK_TOOLCHAIN)/sysroot -I$(NDK_TOOLCHAIN)/sysroot/usr/include -Werror=implicit-function-declaration"
  CXXFLAGS: $(CFLAGS)
  CPPFLAGS: $(CXXFLAGS)

  ARTIFACT_NAME: 'fluidsynth-android$(ANDROID_API)'

jobs:
- job: Android
  strategy:
    matrix:
      ARM:
        ARCH: 'arm'
        ANDROID_ARCH: 'armv7a'
        ANDROID_ABI_CMAKE: 'armeabi-v7a'
        ANDROID_TARGET_ABI: "eabi"
        # the --target to be used by autotools
        AUTOTOOLS_TARGET: "$(ARCH)-linux-android$(ANDROID_TARGET_ABI)"
        #AUTOTOOLS_TARGET: "$(ARCH)-none-linux-$(ANDROID_TARGET_ABI)"
      AARCH64:
        ARCH: 'aarch64'
        ANDROID_ARCH: 'aarch64'
        ANDROID_ABI_CMAKE: 'arm64-v8a'
        ANDROID_TARGET_ABI:
        AUTOTOOLS_TARGET: "$(ARCH)-none-linux-android"
        #AUTOTOOLS_TARGET: "$(ARCH)-none-linux"
      i686:
        ARCH: 'i686'
        ANDROID_ARCH: 'i686'
        ANDROID_ABI_CMAKE: 'x86'
        ANDROID_TARGET_ABI:
        AUTOTOOLS_TARGET: "$(ARCH)-pc-linux-android"
        #AUTOTOOLS_TARGET: "$(ARCH)-pc-linux"
      x86_64:
        ARCH: 'x86_64'
        ANDROID_ARCH: 'x86_64'
        ANDROID_ABI_CMAKE: 'x86_64'
        ANDROID_TARGET_ABI:
        AUTOTOOLS_TARGET: "$(ARCH)-pc-linux-android"
        #AUTOTOOLS_TARGET: "$(ARCH)-pc-linux"

  pool:
    vmImage: 'ubuntu-20.04'

  steps:
    - script: |
        set -ex
        mkdir -p $(DEV)
      displayName: 'mkdir $(DEV)'

    - script: |
        set -ex

        wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-${ICONV_VERSION}.tar.gz
        tar zxf libiconv-${ICONV_VERSION}.tar.gz

        wget -O libffi-${FFI_VERSION}.tar.gz https://github.com/libffi/libffi/releases/download/v${FFI_VERSION}/libffi-${FFI_VERSION}.tar.gz
        tar zxf libffi-${FFI_VERSION}.tar.gz

        wget http://ftp.gnu.org/pub/gnu/gettext/gettext-${GETTEXT_VERSION}.tar.gz
        tar zxf gettext-${GETTEXT_VERSION}.tar.gz

        wget http://ftp.gnome.org/pub/gnome/sources/glib/${GLIB_VERSION}/glib-${GLIB_VERSION}.${GLIB_EXTRAVERSION}.tar.xz
        tar xf glib-${GLIB_VERSION}.${GLIB_EXTRAVERSION}.tar.xz

        wget -O oboe-${OBOE_VERSION}.tar.gz https://github.com/google/oboe/archive/${OBOE_VERSION}.tar.gz
        tar zxf oboe-${OBOE_VERSION}.tar.gz

        wget https://github.com/libsndfile/libsndfile/releases/download/${SNDFILE_VERSION}/libsndfile-${SNDFILE_VERSION}.tar.bz2
        tar jxf libsndfile-${SNDFILE_VERSION}.tar.bz2

        wget -O libinstpatch-${INSTPATCH_VERSION}.tar.gz https://github.com/swami/libinstpatch/archive/refs/tags/v${INSTPATCH_VERSION}.tar.gz
        tar zxf libinstpatch-${INSTPATCH_VERSION}.tar.gz

        wget https://github.com/xiph/vorbis/releases/download/v${VORBIS_VERSION}/libvorbis-${VORBIS_VERSION}.tar.gz
        tar zxf libvorbis-${VORBIS_VERSION}.tar.gz

        wget https://github.com/xiph/ogg/releases/download/v${OGG_VERSION}/libogg-${OGG_VERSION}.tar.gz
        tar zxf libogg-${OGG_VERSION}.tar.gz

        wget -O flac-${FLAC_VERSION}.tar.gz https://github.com/xiph/flac/archive/refs/tags/${FLAC_VERSION}.tar.gz
        tar xf flac-${FLAC_VERSION}.tar.gz

        wget -O opus-${OPUS_VERSION}.tar.gz https://github.com/xiph/opus/archive/refs/tags/v${OPUS_VERSION}.tar.gz
        tar xf opus-${OPUS_VERSION}.tar.gz

      displayName: 'Download Dependencies'
      workingDirectory: $(DEV)

    - task: Cache@2
      inputs:
        key: '$(ARCH) | $(DEV)/*.tar.gz | cacheVersion3'
        path: '$(PREFIX)'
        cacheHitVar: 'CACHE_RESTORED'
      displayName: 'Cache fluidsynth dependency libraries'
      condition: and(not(in(variables['Build.Reason'], 'Schedule')), ${{ parameters.useCache }})

    - script: |
        set -ex

        sudo apt remove --purge --auto-remove cmake
        wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
        sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ focal main'
      displayName: 'Use recent CMake Version'
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))
      enabled: 'false'

    - script: |
        sudo apt-get update -y
      displayName: 'Update apt'
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - script: |
        set -ex
        sudo -E apt-get -yq --no-install-suggests --no-install-recommends install gettext cmake zlib1g-dev autogen automake autoconf libtool pkg-config autotools-dev build-essential meson ninja-build
      displayName: 'apt-get install'
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - script: |
        set -e

        # The cross-compile toolchain we use
        export ANDROID_TARGET=${ARCH}-linux-android${ANDROID_TARGET_ABI}
        echo "##vso[task.setvariable variable=ANDROID_TARGET]$ANDROID_TARGET"

        export ANDROID_TARGET_API=${ANDROID_ARCH}-linux-android${ANDROID_TARGET_ABI}${ANDROID_API}
        echo "##vso[task.setvariable variable=ANDROID_TARGET_API]$ANDROID_TARGET_API"

        # Add the standalone toolchain to the search path.
        export PATH=$PATH:${PREFIX}/bin:${PREFIX}/lib:${PREFIX}/include:${NDK_TOOLCHAIN}/bin
        echo "##vso[task.setvariable variable=PATH]$PATH"
        
        LIBPATH1=$(NDK_TOOLCHAIN)/sysroot/usr/lib
        LIBPATH2=$(NDK_TOOLCHAIN)/sysroot/usr/lib/$(ARCH)-linux-android$(ANDROID_TARGET_ABI)/$(ANDROID_API)
        LIBPATH3=$(NDK_TOOLCHAIN)/sysroot/usr/lib/$(ARCH)-linux-android$(ANDROID_TARGET_ABI)

        export LDFLAGS="-pie -Wl,-rpath-link=${LIBPATH1} -L${LIBPATH1} -Wl,-rpath-link=${LIBPATH2} -L${LIBPATH2} -Wl,-rpath-link=${LIBPATH3} -L${LIBPATH3} -Wl,-rpath-link=${LIBPATH0} -L${LIBPATH0}"
        echo "##vso[task.setvariable variable=LDFLAGS]$LDFLAGS"

        # Tell configure what tools to use.
        export AR=${ANDROID_TARGET}-ar
        echo "##vso[task.setvariable variable=AR]$AR"
        export AS=${ANDROID_TARGET_API}-clang
        echo "##vso[task.setvariable variable=AS]$AS"
        export CC=${ANDROID_TARGET_API}-clang
        echo "##vso[task.setvariable variable=CC]$CC"
        export CXX=${ANDROID_TARGET_API}-clang++
        echo "##vso[task.setvariable variable=CXX]$CXX"
        export LD=ld.lld
        echo "##vso[task.setvariable variable=LD]$LD"
        export STRIP=${ANDROID_TARGET}-strip
        echo "##vso[task.setvariable variable=STRIP]$STRIP"
        export RANLIB=${ANDROID_TARGET}-ranlib
        echo "##vso[task.setvariable variable=RANLIB]$RANLIB"

      displayName: 'Set environment variables'

    - script: |
        set -ex

        pushd libiconv-${ICONV_VERSION}
        ./configure \
          --host=${AUTOTOOLS_TARGET} \
          --prefix=${PREFIX} \
          --libdir=${LIBPATH0} \
          --disable-rpath \
          --enable-static \
          --disable-shared \
          --with-pic \
          --disable-maintainer-mode \
          --disable-silent-rules \
          --disable-gtk-doc \
          --disable-introspection \
          --disable-nls
        make -j$((`nproc`+1))
        make install
        popd
  
      displayName: 'Compile libiconv'
      workingDirectory: $(DEV)
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - script: |
        set -ex

        pushd libffi-${FFI_VERSION}
        NOCONFIGURE=true autoreconf -v -i
        # install headers into the conventional ${PREFIX}/include rather than ${PREFIX}/lib/libffi-3.2.1/include.
        #sed -e '/^includesdir/ s/$(libdir).*$/$(includedir)/' -i include/Makefile.in
        #sed -e '/^includedir/ s/=.*$/=@includedir@/' -e 's/^Cflags: -I${includedir}/Cflags:/' -i libffi.pc.in
        ./configure --host=${AUTOTOOLS_TARGET} --prefix=${PREFIX} --libdir=${LIBPATH0} --enable-static --disable-shared
        make -j$((`nproc`+1))
        make install
        popd
      displayName: 'Compile libffi'
      workingDirectory: $(DEV)
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - script: |
        set -ex
        export PKGCFG=`which pkg-config`
        pushd glib-${GLIB_VERSION}.${GLIB_EXTRAVERSION}

        cat << EOF > cross-file.txt
        [host_machine]
        system = 'android'
        cpu_family = 'arm'
        cpu = 'arm'
        endian = 'little'

        [binaries]
        c = '${CC}'
        cpp = '${CXX}'
        ar = '${AR}'
        ld = '${LD}'
        strip = '${STRIP}'
        pkgconfig = '${PKGCFG}'

        [built-in options]
        c_std = 'c11'
        c_args = ['-fPIC','-I/home/git/work/ndk/builddir/out/include']
        cpp_args = ['-fPIC','-I/home/git/work/ndk/builddir/out/include']
        c_link_args = ['-fPIE','-L/home/git/work/ndk/builddir/out/lib']

        pkg_config_path = '${PKG_CONFIG_PATH}'
        EOF

        cat << EOF > native-file.txt
        [host_machine]
        system = 'linux'
        cpu_family = 'x86_64'
        cpu = 'x86_64'
        endian = 'little'

        [properties]

        [binaries]
        c = ['false']
        cpp = ['false']
        objc = ['false']
        objcpp = ['false']
        ar = ['false']
        pkgconfig = ['false']
        cmake = ['false']
        EOF

        cat native-file.txt
        cat cross-file.txt

        unset AR
        unset AS
        unset CC
        unset CFLAGS
        unset CPPFLAGS
        unset CPP
        unset CXXFLAGS
        unset CXX
        unset LDFLAGS
        unset LD
        unset STRIP

        meson \
          --cross-file cross-file.txt \
          --native-file native-file.txt \
          --prefix=${PREFIX} \
          --libdir=lib \
          -Ddebug=false \
          --default-library=both \
          -Doptimization=s \
          --backend=ninja \
          --wrap-mode=nodownload \
          -Dinternal_pcre=true \
          -Dlibmount=false \
          -Ddtrace=false \
          -Diconv=auto \
          -Dxattr=false \
          -Dgtk_doc=false \
          _builddir \
          .
        ninja
        popd
      displayName: 'Compile glib (meson)'
      workingDirectory: $(DEV)
      enabled: 'false'

    - script: |
        pushd glib-${GLIB_VERSION}.${GLIB_EXTRAVERSION}
        cat _builddir/meson-logs/meson-log.txt
        popd
      displayName: 'Meson LOG'
      workingDirectory: $(DEV)
      condition: failed()
      enabled: 'false'

    - script: |
        set -ex

        pushd gettext-${GETTEXT_VERSION}
        ./configure \
          --host=${AUTOTOOLS_TARGET} \
          --prefix=${PREFIX} \
          --libdir=${LIBPATH0} \
          --disable-rpath \
          --disable-libasprintf \
          --disable-java \
          --disable-native-java \
          --disable-openmp \
          --disable-curses \
          --enable-static \
          --disable-shared \
          --with-pic  \
          --disable-maintainer-mode \
          --disable-silent-rules \
          --disable-gtk-doc \
          --disable-introspection
        make -j$((`nproc`+1))
        make install
        popd
      displayName: 'Compile gettext'
      workingDirectory: $(DEV)
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - script: |
        set -ex

        pushd glib-${GLIB_VERSION}.${GLIB_EXTRAVERSION}
        cat << EOF > android.cache
        glib_cv_long_long_format=ll
        glib_cv_stack_grows=no
        glib_cv_sane_realloc=yes
        glib_cv_have_strlcpy=no
        glib_cv_va_val_copy=yes
        glib_cv_rtldglobal_broken=no
        glib_cv_uscore=no
        glib_cv_monotonic_clock=no
        ac_cv_func_nonposix_getpwuid_r=no
        ac_cv_func_posix_getpwuid_r=no
        ac_cv_func_posix_getgrgid_r=no
        glib_cv_use_pid_surrogate=yes
        ac_cv_func_printf_unix98=no
        ac_cv_func_vsnprintf_c99=yes
        ac_cv_func_realloc_0_nonnull=yes
        ac_cv_func_realloc_works=yes
        EOF

        # Unfortunately, libffi is not linked against libgobject when compiling for aarch64, leading to the following error:
        #
        # /bin/bash ../libtool  --tag=CC   --mode=link aarch64-linux-android23-clang -Wall -Wstrict-prototypes -Wno-bad-function-cast -Werror=declaration-after-statement -Werror=missing-prototypes -Werror=implicit-function-declaration -Werror=pointer-arith -Werror=init-self -Werror=format=2 -Werror=missing-include-dirs -fPIE -fPIC -I/home/vsts/work/1/s/android-build-root/opt/android/include --sysroot=/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot -I/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot/include -Werror=implicit-function-declaration -fno-integrated-as -fno-strict-aliasing  -pie -Wl,-rpath-link=-I/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot/usr/lib -L/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot/usr/lib -L/home/vsts/work/1/s/android-build-root/opt/android/lib -L/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//lib -o gobject-query gobject-query.o ./libgobject-2.0.la ../glib/libglib-2.0.la -lintl -liconv  
        # libtool: link: aarch64-linux-android23-clang -Wall -Wstrict-prototypes -Wno-bad-function-cast -Werror=declaration-after-statement -Werror=missing-prototypes -Werror=implicit-function-declaration -Werror=pointer-arith -Werror=init-self -Werror=format=2 -Werror=missing-include-dirs -fPIE -fPIC -I/home/vsts/work/1/s/android-build-root/opt/android/include --sysroot=/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot -I/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot/include -Werror=implicit-function-declaration -fno-integrated-as -fno-strict-aliasing -pie -Wl,-rpath-link=-I/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot/usr/lib -o .libs/gobject-query gobject-query.o  -L/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//sysroot/usr/lib -L/home/vsts/work/1/s/android-build-root/opt/android/lib -L/usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//lib ./.libs/libgobject-2.0.so ../glib/.libs/libglib-2.0.so /home/vsts/work/1/s/android-build-root/opt/android/lib/libintl.so /home/vsts/work/1/s/android-build-root/opt/android/lib/libiconv.so -pthread -L/home/vsts/work/1/s/android-build-root/opt/android/lib
        # /usr/local/lib/android/sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64//bin/../lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: warning: libffi.so, needed by ./.libs/libgobject-2.0.so, not found (try using -rpath or -rpath-link)
        # ./.libs/libgobject-2.0.so: undefined reference to `ffi_type_sint32@LIBFFI_BASE_8.0'
        # ./.libs/libgobject-2.0.so: undefined reference to `ffi_prep_cif@LIBFFI_BASE_8.0'
        #
        # So, just add it to LDFLAGS to make sure it's always linked.
        # libz.so is also missing...
        #FFILIB=`pkg-config --libs libffi`
        #echo ${FFILIB}
        #export LDFLAGS="${LDFLAGS} ${FFILIB} -lz"
        #unset FFILIB

        chmod a-x android.cache
        NOCONFIGURE=true ./autogen.sh
        ./configure \
          --host=${ANDROID_TARGET} \
          --prefix=${PREFIX} \
          --libdir=${LIBPATH0} \
          --disable-dependency-tracking \
          --cache-file=android.cache \
          --enable-included-printf \
          --with-pcre=no \
          --enable-libmount=no \
          --enable-xattr=no \
          --with-libiconv=gnu \
          --disable-static \
          --enable-shared \
          --with-pic \
          --disable-maintainer-mode \
          --disable-silent-rules
        make -j$((`nproc`+1))
        make install
        popd
      displayName: 'Compile glib'
      workingDirectory: $(DEV)
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - template: cmake-android.yml
      parameters:
        sourceDir: 'libogg-$(OGG_VERSION)'
        cmakeArgs: '-DINSTALL_DOCS=0'

    - template: cmake-android.yml
      parameters:
        sourceDir: 'libvorbis-$(VORBIS_VERSION)'

    # flac uses c99 macros, but doesnt specify a standard, so we need to do it explicitly.
    # On i686, they invoke yasm with -fstack-protector-strong flag... turn off asm optimizations.
    - template: cmake-android.yml
      parameters:
        sourceDir: 'flac-$(FLAC_VERSION)'
        cmakeArgs: '-DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=1 -DWITH_ASM=0 -DBUILD_CXXLIBS=0 -DBUILD_PROGRAMS=0 -DBUILD_EXAMPLES=0 -DBUILD_DOCS=0 -DINSTALL_MANPAGES=0'

    # another broken xiph project that doesn't specify the C standard and keeps complaining of you don't have C99
    - template: cmake-android.yml
      parameters:
        sourceDir: 'opus-$(OPUS_VERSION)'
        cmakeArgs: '-DBUILD_PROGRAMS=0 -DOPUS_MAY_HAVE_NEON=1 -DCMAKE_C_STANDARD=99 -DCMAKE_C_STANDARD_REQUIRED=1'

    - template: cmake-android.yml
      parameters:
        sourceDir: 'libsndfile-$(SNDFILE_VERSION)'
        cmakeArgs: '-DBUILD_PROGRAMS=0 -DBUILD_EXAMPLES=0'

    - template: cmake-android.yml
      parameters:
        sourceDir: 'oboe-$(OBOE_VERSION)'
        installCommand: 'cp liboboe.* ${PREFIX}/lib/ && cp -ur ../include/oboe ${PREFIX}/include'
  
    - script: |
        set -ex

        # create a custom pkgconfig file for oboe to allow fluidsynth to find it
        cat << EOF > ${PKG_CONFIG_PATH}/oboe-1.0.pc
        prefix=${PREFIX}
        exec_prefix=\${prefix}
        libdir=\${prefix}/lib
        includedir=\${prefix}/include

        Name: Oboe
        Description: Oboe library
        Version: ${OBOE_VERSION}
        Libs: -L\${libdir} -loboe -landroid -llog
        Cflags: -I\${includedir}
        EOF

        cat ${PKG_CONFIG_PATH}/oboe-1.0.pc

      displayName: 'Create fake oboe.pc'
      workingDirectory: $(DEV)
      condition: and(succeeded(), ne(variables.CACHE_RESTORED, 'true'))

    - template: cmake-android.yml
      parameters:
        sourceDir: 'libinstpatch-$(INSTPATCH_VERSION)'

    # finally, compile fluidsynth
    - template: cmake-android.yml
      parameters:
        workDir: '$(System.DefaultWorkingDirectory)'
        sourceDir: '.'
        condition: succeeded()
        cmakeArgs: '-Denable-opensles=1 -Denable-floats=1 -Denable-oboe=1 -Denable-dbus=0 -Denable-oss=0'
        installCommand: ''

    - script: |
        set -x
        export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:$(PREFIX)/lib:$(NDK_TOOLCHAIN)/sysroot/usr/lib/$(ARCH)-linux-android$(ANDROID_TARGET_ABI)/$(ANDROID_API)
        pushd build
        make -j$((`nproc`+1)) check
        ldd test/test_sample_cache
        popd
      displayName: 'Execute fluidsynth unit test'
      condition: and(succeeded(), in(variables['ARCH'], 'x86_64', 'i686'))
      enabled: 'false'

    - script: |
        set -ex
        pushd build
        make install
        popd
      displayName: 'Install fluidsynth'

    - template: cmake-android.yml
      parameters:
        workDir: '$(System.DefaultWorkingDirectory)/doc/android/fluidsynth-assetloader/'
        sourceDir: '.'
        condition: succeeded()
        installCommand: 'cp *.so ${PREFIX}/lib/'

    - script: |
        ls -Rg $(PREFIX)
      displayName: 'Show cross-compiled files in $(PREFIX)'
      condition: or(succeeded(), failed())

    - script: |
        set -ex
        # use ANDROID_ABI_CMAKE so libs can be simply copied to the archive contents in src/main/jniLibs
        mkdir -p $(Build.ArtifactStagingDirectory)/lib/$(ANDROID_ABI_CMAKE)
        cd $(Build.ArtifactStagingDirectory)/lib/$(ANDROID_ABI_CMAKE)
        cp -LR $(PREFIX)/lib/* .
        ls -Rg .
        rm -rf *.dll *.alias gettext/ libtextstyle.* *.a *.la
        rm -f *.so.*
        mkdir -p $(Build.ArtifactStagingDirectory)/include
        cd $(Build.ArtifactStagingDirectory)/include
        cp -a $(PREFIX)/include/fluidsynth* .
      displayName: 'Collecting artifacts'
    
    - script: |
        set -ex
        ls libFLAC.so
        ls libfluidsynth-assetloader.so
        ls libfluidsynth.so
        ls libgio-2.0.so
        ls libglib-2.0.so
        ls libgmodule-2.0.so
        ls libgobject-2.0.so
        ls libgthread-2.0.so
        ls libinstpatch-1.0.so
        ls liboboe.so
        ls libogg.so
        ls libopus.so
        ls libsndfile.so
        ls libvorbis.so
        ls libvorbisenc.so
        ls libvorbisfile.so
      displayName: 'Verify all libs exist'
      workingDirectory: '$(PREFIX)/lib'

    - task: PublishBuildArtifacts@1
      displayName: 'Publishing Artefacts for Android API$(ANDROID_API) $(ANDROID_ABI_CMAKE)'
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: '$(ARTIFACT_NAME)'
        publishLocation: 'Container'

    - script: |
        set -ex
        # as very last step before creating the pipeline cache, remove fluidsynth
        pushd build
        make uninstall
        popd
      displayName: 'Uninstall fluidsynth'
