---
name: build
'on': [push, pull_request]
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
jobs:
  linux-gcc:
    name: linux-gcc
    runs-on: ubuntu-latest
    steps:
      - name: Update
        run: sudo apt-get --yes update
      - name: Install dependencies
        run: sudo apt-get --yes install fluidsynth libfluidsynth-dev sordi gtk+2.0 libgtk2.0-dev libcairo2 lv2-dev
      - name: Check out
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Configure
        run: |
          mkdir build && cd build
          cmake -DCMAKE_INSTALL_PREFIX=../install-dir -DCMAKE_BUILD_TYPE=Release ..
      - name: Build
        run: cmake --build build
      - name: Package
        run: |
          cd build
          cpack -G TGZ --config CPackSourceConfig.cmake
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: linux
          path: build/calf-*gz
  linux-clang-with-lld:
    name: linux-clang-with-lld  # clang with LLVM's linker LLD
    runs-on: ubuntu-latest
    steps:
      - name: Update
        run: sudo apt-get --yes update
      - name: Install dependencies
        run: sudo apt-get --yes install fluidsynth libfluidsynth-dev sordi gtk+2.0 libgtk2.0-dev libcairo2 lv2-dev
      - name: Check out
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Configure
        run: >
          mkdir build && cd build &&
          cmake ..
          -DCMAKE_INSTALL_PREFIX=../install-dir
          -DCMAKE_C_COMPILER=clang
          -DCMAKE_CXX_COMPILER=clang++
          -DCMAKE_C_FLAGS="-fuse-ld=lld"
          -DCMAKE_CXX_FLAGS="-fuse-ld=lld"
      - name: Build
        run: cmake --build build
  macos:
    name: macos
    runs-on: macos-13
    steps:
      - name: Install python3
        run: brew install --overwrite python@3.12
      - name: Install dependencies
        run: brew install automake fluid-synth gtk+ lv2 gtk-mac-integration expat
      - name: Check out
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Configure
        run: |
          export LDFLAGS="-L$(brew --prefix)/lib"
          export CPPFLAGS="-I$(brew --prefix expat)/include"
          mkdir build && cd build
          cmake -DCMAKE_INSTALL_PREFIX=../install-dir -DCMAKE_SHARED_LINKER_FLAGS="-L$(brew --prefix)/lib -Wl,-rpath,$(brew --prefix)/lib" -DCMAKE_CXX_FLAGS="-I$(brew --prefix)/include" -DCMAKE_BUILD_TYPE=Release ..
      - name: Build
        run: cmake --build build
      - name: Package
        run: |
          cd build
          cpack -G productbuild
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: macos
          path: build/calf-*.pkg
  msvc:
    name: msvc
    runs-on: windows-latest
    steps:
      - name: Check out
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - name: Cache vcpkg dependencies
        id: cache-deps
        uses: actions/cache@v4
        with:
          key: vcpkg-x64-${{ hashFiles('vcpkg.json') }}
          restore-keys: |
            vcpkg-x64-
          path: build\vcpkg_installed
      - name: Configure
        run: |
          mkdir build -Force
          cmake `
          -B build `
          -DCMAKE_TOOLCHAIN_FILE=C:/vcpkg/scripts/buildsystems/vcpkg.cmake `
          -DCMAKE_BUILD_TYPE=Debug `
          .
      - name: Build
        run: cmake --build build --config Release
      - name: Package
        run: |
          cd build
          cpack -G NSIS -C Release
      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: windows
          path: build\calf-*.exe

#  mingw-cross:
#    strategy:
#      matrix:
#        compiler:
#          - {bits: '32'}
#          - {bits: '64'}
#    name: mingw-${{matrix.compiler.bits}}-cross
#    runs-on: ubuntu-latest
#    steps:
#      - name: Check out
#        uses: actions/checkout@v4
#        with:
#          fetch-depth: 0
#      - name: Install MinGW
#        run: |
#          sudo dpkg --add-architecture i386  # for wine32
#          sudo apt-get update  # due to new architecture
#          sudo apt-get install -y mingw-w64
#      - name: Compile dependencies
#        run: |
#          calf_pwd=$PWD
#          cd /tmp
#          wget https://github.com/libexpat/libexpat/releases/download/R_2_5_0/expat-2.5.0.tar.bz2
#          tar xjf expat-2.5.0.tar.bz2
#          cd expat-2.5.0
#          ./buildconf.sh
#          mkdir build
#          cd build
#          export CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ LD=i686-w64-mingw32-ld QA_PROCESSOR=gcov
#          cmake -DCMAKE_SYSTEM_NAME=Windows -DWIN32=ON -DMINGW=ON -DEXPAT_ATTR_INFO=ON ..
#          make VERBOSE=1 CTEST_OUTPUT_ON_FAILURE=1 all
#          sudo make install
#          cd /tmp
#          rm -rf /tmp/expat-2.5.0
#          wget https://github.com/FluidSynth/fluidsynth/archive/refs/tags/v2.3.4.tar.gz
#          tar xfz v2.3.4.tar.gz
#          cd fluidsynth-2.3.4
#          mkdir build
#          cd build
#          ls -la $calf_pwd/..
#          ls -la $calf_pwd
#          ls -la $calf_pwd/cmake/toolchains
#          export PKG_CONFIG_LIBDIR=/usr/x86_64-w64-mingw32/sys-root/mingw/lib/pkgconfig
#          cmake -DCMAKE_TOOLCHAIN_FILE=$calf_pwd/cmake/toolchains/FluidSynth-${{matrix.compiler.bits}}.cmake -Denable-libsndfile=0 -Denable-dbus=0 -Denable-pulseaudio=0 -Denable-jack=0 ..
#          sudo make install
#      - name: Install dependencies
#        run: |
#          sudo add-apt-repository ppa:tobydox/mingw-w64
#          sudo apt update
#          sudo apt-get install -y fluidsynth-mingw-w64
#      - name: Configure
#        run: >
#          mkdir build && cd build &&
#          PATH=/opt/mingw${{matrix.compiler.bits}}/bin:$PATH
#          cmake ..
#          -DCMAKE_INSTALL_PREFIX=../install
#          -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/Ubuntu-MinGW-W64-${{matrix.compiler.bits}}.cmake
#          -DCMAKE_PREFIX_PATH=/opt/mingw${{matrix.compiler.bits}}
#      - name: Build
#        run: cmake --build build
