name: Continuous Integration

on: [push, pull_request]

jobs:
  build:
    name: ${{ matrix.config.name }}
    runs-on: ${{ matrix.config.os }}
    strategy:
      fail-fast: false
      matrix:
        config:
        - {
            name: "Visual Studio - Release",
            os: windows-2019,
            build_type: "Release"
          }
        - {
            name: "Visual Studio - Debug",
            os: windows-2019,
            build_type: "Debug"
          }
        - {
            name: "macOS Clang - Dynamic Deps",
            os: macos-10.15,
            build_type: "Release"
          }
        - {
            name: "macOS Clang - Static Deps",
            os: macos-10.15,
            build_type: "Release",
            extra_options: "-DDYN_FLUIDSYNTH=OFF -DDYN_MPG123=OFF -DDYN_SNDFILE=OFF"
          }
        - {
            name: "Linux GCC - Dynamic Deps",
            os: ubuntu-20.04,
            build_type: "Release"
          }
        - {
            name: "Linux GCC - Static Deps",
            os: ubuntu-20.04,
            build_type: "Release",
            extra_options: "-DDYN_FLUIDSYNTH=OFF -DDYN_MPG123=OFF -DDYN_SNDFILE=OFF"
          }
        - {
            name: "Linux Clang - Dynamic Deps",
            os: ubuntu-20.04,
            build_type: "Release",
            extra_options: "-DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++"
          }
        - {
            name: "Linux Clang - Static Deps",
            os: ubuntu-20.04,
            build_type: "Release",
            extra_options: "-DCMAKE_C_COMPILER=/usr/bin/clang -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DDYN_FLUIDSYNTH=OFF -DDYN_MPG123=OFF -DDYN_SNDFILE=OFF"
          }

    steps:
    - uses: actions/checkout@v1

    - name: Install Dependencies
      shell: bash
      run: |
        if [[ "${{ runner.os }}" == 'Linux' ]]; then
          sudo apt update
          sudo apt install libasound2-dev libfluidsynth-dev libmpg123-dev libsndfile1-dev
        elif [[ "${{ runner.os }}" == 'macOS' ]]; then
          brew install fluidsynth mpg123 libsndfile
        fi

    - name: Configure
      shell: bash
      run: |
        mkdir build
        cd build
        cmake -DCMAKE_BUILD_TYPE=${{ matrix.config.build_type }} -DCMAKE_INSTALL_PREFIX=../build_install ${{ matrix.config.extra_options }} ..

    - name: Build
      shell: bash
      run: |
        cd build
        if [[ "${{ runner.os }}" == 'Windows' ]]; then
          cmake --build . --target install --config ${{ matrix.config.build_type }} -- -maxcpucount -verbosity:minimal
        else
          cmake --build . --target install -- --jobs=2 --keep-going
        fi

    - name: Test
      shell: bash
      run: |
        cd samples/list_midi_devices
        mkdir build
        cd build
        declare -x PREFIX=`pwd`/../../../build_install
        cmake -DCMAKE_PREFIX_PATH=${PREFIX} ${{ matrix.config.extra_options }} ..
        cmake --build . --config ${{ matrix.config.build_type }}
        if [[ "${{ runner.os }}" == 'macOS' ]]; then
          declare -x DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:${PREFIX}
        fi
        if [[ "${{ runner.os }}" == 'Windows' ]]; then
          cp ${PREFIX}/bin/zmusic.dll ${{ matrix.config.build_type }}
          ${{ matrix.config.build_type }}/list_midi_devices.exe
        else
          ./list_midi_devices
        fi
