---

name: Build on various Operating Systems

on:
  push:
    branches: [devel, master]
  pull_request:
    branches: [devel, master]

env:
  CARLA_BRIDGE_DUMMY: 1
  CARLA_BRIDGE_TESTING: native
  # FIXME some system library is causing memory leaks, switch to --leak-check=full when fixed
  PLUGIN_METADATA: .build/target/lsp-plugin-fw/plugins.json
  VALGRIND_ARGS: --error-exitcode=255 --leak-check=no --track-origins=yes --suppressions=.github/workflows/valgrind.supp
  VALGRIND_DEBUG_ARGS: --error-exitcode=255 --leak-check=full --track-origins=yes --keep-debuginfo=yes --suppressions=.github/workflows/valgrind.supp

jobs:
  arch_linux:
    runs-on: ubuntu-latest
    container:
      image: archlinux:latest
    steps:
      - name: Add debug repositories
        run: |
          printf "[core-debug]\nInclude = /etc/pacman.d/mirrorlist\n[extra-debug]\nInclude = /etc/pacman.d/mirrorlist\n[multilib-debug]\nInclude = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf
          printf 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch\n%s\n' "$(cat /etc/pacman.d/mirrorlist)" > /etc/pacman.d/mirrorlist
      - name: Install dependencies
        run: pacman --noconfirm -Syu alsa-lib base-devel cairo carla git glibc-debug hicolor-icon-theme jack2 jq libglvnd libsndfile libx11 libxrandr lv2lint php valgrind gstreamer gst-plugins-base
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config FEATURES='strict clap doc gst jack ladspa lv2 standalone ui vst2 vst3 xdg' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install
      - name: System information
        run: lscpu
      - name: Lint LV2 plugins
        run: |
          for _plugin in $(jq -r '.plugins[].lv2_uri | select( . != null )' ${{ env.PLUGIN_METADATA }} ); do \
            lv2lint -Mpack "${_plugin}"; \
          done
      - name: Validate LV2 syntax
        run: lv2_validate /usr/lib/lv2/lsp-*.lv2/*.ttl
      - name: LADSPA runtime checks
        run: |
          for _plugin in $(jq -r '.plugins[].ladspa_label | select( . != null )' ${{ env.PLUGIN_METADATA }}); do \
            valgrind ${{ env.VALGRIND_ARGS }} /usr/lib/carla/carla-bridge-native ladspa /usr/lib/ladspa/lsp-*.so "${_plugin}" 1>/dev/null; \
          done
      - name: LV2 runtime checks
        run: |
          for _plugin in $(jq -r '.plugins[].lv2_uri | select( . != null )' ${{ env.PLUGIN_METADATA }}); do \
            valgrind ${{ env.VALGRIND_ARGS }} /usr/lib/carla/carla-bridge-native lv2 "" "${_plugin}" 1>/dev/null; \
          done
      - name: VST2 runtime checks
        run: |
          for _binary in $(ls /usr/lib/vst/lsp-plugins/*.so | grep -v /liblsp-plugins-); do \
            valgrind ${{ env.VALGRIND_ARGS }} /usr/lib/carla/carla-bridge-native vst2 "${_binary}" "" 1>/dev/null; \
          done

  arch_linux_asan:
    runs-on: ubuntu-latest
    container:
      image: archlinux:latest
    steps:
      - name: Add debug repositories
        run: |
          printf "[core-debug]\nInclude = /etc/pacman.d/mirrorlist\n[extra-debug]\nInclude = /etc/pacman.d/mirrorlist\n[multilib-debug]\nInclude = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf
          printf 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch\n%s\n' "$(cat /etc/pacman.d/mirrorlist)" > /etc/pacman.d/mirrorlist
      - name: Install dependencies
        run: pacman --noconfirm -Syu alsa-lib base-devel cairo carla git glibc-debug hicolor-icon-theme jack2 jq libglvnd libsndfile libx11 libxrandr php valgrind gstreamer gst-plugins-base
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config FEATURES='strict debug clap gst jack ladspa lv2 standalone ui vst2 vst3 xdg' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install
      - name: System information
        run: lscpu
      - name: LADSPA runtime checks
        run: |
          for _plugin in $(jq -r '.plugins[].ladspa_label | select( . != null )' ${{ env.PLUGIN_METADATA }}); do \
            export ASAN_OPTIONS=verify_asan_link_order=0; \
            /usr/lib/carla/carla-bridge-native ladspa /usr/lib/ladspa/lsp-*.so "${_plugin}" 1>/dev/null; \
          done
      - name: LV2 runtime checks
        run: |
          for _plugin in $(jq -r '.plugins[].lv2_uri | select( . != null )' ${{ env.PLUGIN_METADATA }}); do \
            export ASAN_OPTIONS=verify_asan_link_order=0; \
            /usr/lib/carla/carla-bridge-native lv2 "" "${_plugin}" 1>/dev/null; \
          done
      - name: VST2 runtime checks
        run: |
          for _binary in $(ls /usr/lib/vst/lsp-plugins/*.so | grep -v /liblsp-plugins-); do \
            export ASAN_OPTIONS=verify_asan_link_order=0; \
            /usr/lib/carla/carla-bridge-native vst2 "${_binary}" "" 1>/dev/null; \
          done

  arch_linux_debug:
    runs-on: ubuntu-latest
    container:
      image: archlinux:latest
    steps:
      - name: Add debug repositories
        run: |
          printf "[core-debug]\nInclude = /etc/pacman.d/mirrorlist\n[extra-debug]\nInclude = /etc/pacman.d/mirrorlist\n[multilib-debug]\nInclude = /etc/pacman.d/mirrorlist" >> /etc/pacman.conf
          printf 'Server = https://geo.mirror.pkgbuild.com/$repo/os/$arch\n%s\n' "$(cat /etc/pacman.d/mirrorlist)" > /etc/pacman.d/mirrorlist
      - name: Install dependencies
        run: pacman --noconfirm -Syu alsa-lib base-devel cairo carla git glibc-debug hicolor-icon-theme jack2 jq libglvnd libsndfile libx11 libxrandr php valgrind gstreamer gst-plugins-base
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config VERBOSE=1 FEATURES='strict debug clap jack gst ladspa lv2 ui vst2 vst3 xdg' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install
      - name: System information
        run: lscpu
      - name: LADSPA runtime checks
        run: |
          for _plugin in $(jq -r '.plugins[].ladspa_label | select( . != null )' ${{ env.PLUGIN_METADATA }}); do \
            valgrind ${{ env.VALGRIND_DEBUG_ARGS }} /usr/lib/carla/carla-bridge-native ladspa /usr/lib/ladspa/lsp-*.so "${_plugin}" 1>/dev/null; \
          done
      - name: LV2 runtime checks
        run: |
          for _plugin in $(jq -r '.plugins[].lv2_uri | select( . != null )' ${{ env.PLUGIN_METADATA }}); do \
            valgrind ${{ env.VALGRIND_DEBUG_ARGS }} /usr/lib/carla/carla-bridge-native lv2 "" "${_plugin}" 1>/dev/null; \
          done
      - name: VST2 runtime checks
        run: |
          for _binary in $(ls /usr/lib/vst/lsp-plugins/*.so | grep -v /liblsp-plugins-); do \
            valgrind ${{ env.VALGRIND_DEBUG_ARGS }} /usr/lib/carla/carla-bridge-native vst2 "${_binary}" "" 1>/dev/null; \
          done

  opensuse_leap:
    runs-on: ubuntu-latest
    container:
      image: opensuse/leap:latest
    steps:
      - name: Install dependencies
        run: zypper --non-interactive --no-gpg-checks in tar gzip gcc gcc-c++ glibc-locale git make php valgrind libX11-devel libXrandr-devel Mesa-libGL-devel libjack-devel cairo-devel freetype2-devel libsndfile-devel gstreamer-devel gstreamer-plugins-base-devel
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config FEATURES='strict clap doc gst jack ladspa lv2 standalone ui vst2 vst3 xdg' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install

  opensuse_tumbleweed_clang:
    runs-on: ubuntu-latest
    container:
      image: opensuse/tumbleweed:latest
    steps:
      - name: Install dependencies
        run: zypper --non-interactive --no-gpg-checks in tar gzip gcc gcc-c++ glibc-locale glibc-gconv-modules-extra clang lld git make php8-cli valgrind libstdc++-devel libX11-devel libXrandr-devel Mesa-libGL-devel libjack-devel cairo-devel freetype2-devel libsndfile-devel gstreamer-devel gstreamer-plugins-base-devel
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config CC=clang CXX=clang++ FEATURES='strict clap doc gst jack ladspa lv2 standalone ui vst2 vst3 xdg' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install

  debian_stable:
    runs-on: ubuntu-latest
    container:
      image: debian:stable
    steps:
      - name: Update repositories
        run: apt-get update
      - name: Install dependencies
        run: apt-get -y install gcc g++ git make php-cli pkg-config valgrind libx11-dev libxrandr-dev libjack-dev libcairo2-dev libgl-dev libfreetype6-dev libsndfile1-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config FEATURES='strict clap doc gst jack ladspa lv2 standalone ui vst2 vst3 xdg' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install

  debian_stable_no_ui:
    runs-on: ubuntu-latest
    container:
      image: debian:stable
    steps:
      - name: Update repositories
        run: apt-get update
      - name: Install dependencies
        run: apt-get -y install gcc g++ git make php-cli pkg-config valgrind libx11-dev libxrandr-dev libjack-dev libcairo2-dev libgl-dev libfreetype6-dev libsndfile1-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
      - uses: actions/checkout@v3
      - name: Configure project
        run: make config FEATURES='strict clap doc gst jack ladspa lv2 standalone vst2 vst3' PREFIX=/usr
      - name: Fetch project dependencies
        run: make fetch
      - name: Build project
        run: make VERBOSE=1
      - name: Install binaries
        run: make install

  windows_mingw64:
    runs-on: windows-2022
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - name: Setup MSYS2 and install dependencies
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          release: false
          update: false
          install: >-
            base-devel
            git
            mingw-w64-x86_64-gcc
      - uses: actions/checkout@v3
      - name: Configure project
        shell: msys2 {0}
        run: make config FEATURES='strict clap ladspa lv2 ui vst2 vst3'
      - name: Fetch project dependencies
        shell: msys2 {0}
        run: make fetch
      - name: Build project
        shell: msys2 {0}
        run: make VERBOSE=1

  macos_stable:
    runs-on: macos-latest
    steps:
      - name: Update repositories
        run: brew update
      - name: Install dependencies
        run: brew install make pkgconf cairo freetype
      - uses: actions/checkout@v3
      - name: Configure project
        run: gmake config FEATURES='strict lv2 ui'
      - name: Fetch project dependencies
        run: gmake fetch
      - name: Build project
        run: gmake VERBOSE=1
