name: Build SSR with different compilers
on: [push, pull_request]
jobs:
  build:
    strategy:
      matrix:
        include:
          - os: ubuntu-22.04
            compiler-pkg: g++-12
            env:
              CC: gcc-12
              CXX: g++-12
          - os: ubuntu-20.04
            compiler-pkg: g++-9
            env:
              CC: gcc-9
              CXX: g++-9
          - os: ubuntu-22.04
            compiler-pkg: clang-14
            env:
              CC: clang-14
              CXX: clang++-14
          - os: ubuntu-20.04
            compiler-pkg: clang-9
            env:
              CC: clang-9
              CXX: clang++-9
              CXXFLAGS: -fno-builtin
          - os: macos-13
            xcode: Xcode_14.3
          - os: macos-12
            xcode: Xcode_14.2
    runs-on: ${{ matrix.os }}
    env: ${{ matrix.env || fromJSON('{}') }}
    steps:
      - name: Clone SSR repo
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: Select Xcode
        if: matrix.xcode
        run: |
          sudo xcode-select --switch /Applications/${{ matrix.xcode }}.app
      - name: Install Ubuntu packages
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          PACKAGES=(
            automake
            ecasound
            help2man
            jackd1
            libasio-dev
            libecasoundc-dev
            libfftw3-dev
            libfmt-dev
            libjack-dev
            libjack0
            libmysofa-dev
            libqt5opengl5-dev
            libsndfile1-dev
            libtool
            libwebsocketpp-dev
            libxml2-dev
            pkg-config
          )
          sudo apt-get update
          sudo apt-get install --no-install-recommends ${PACKAGES[@]}
          echo QT_SELECT=qt5 >> $GITHUB_ENV
      - name: Install homebrew packages
        if: startsWith(matrix.os, 'macos')
        run: |
          # This is only needed (temporarily?) for macos-12 and macos-13, see
          # https://github.com/actions/runner-images/issues/4020
          # Fix based on https://github.com/TeamForbiddenLLC/warfork-qfusion/pull/325:
          brew uninstall --force azure-cli
          brew uninstall --force aws-sam-cli
          brew install python@3 || brew link --overwrite python@3
          # End of fix.

          PACKAGES=(
            asio
            automake
            ecasound
            fftw
            fmt
            help2man
            jack
            SoundScapeRenderer/ssr/libmysofa
            libsndfile
            libtool
            pkg-config
            qt@5
            vrpn
            websocketpp
          )
          brew install ${PACKAGES[@]}
          # Link keg-only Qt5 according to homebrew info
          echo "/usr/local/opt/qt@5/bin" >> $GITHUB_PATH
          echo LDFLAGS="-L/usr/local/opt/qt@5/lib" >> $GITHUB_ENV
          echo CPPFLAGS="-I/usr/local/opt/qt@5/include" >> $GITHUB_ENV
          echo PKG_CONFIG_PATH="/usr/local/opt/qt@5/lib/pkgconfig" >> $GITHUB_ENV
          # Perl module needed for help2man
          #cpan Locale::gettext
      - name: Install compiler
        if: matrix.compiler-pkg
        run: |
          sudo apt-get install --no-install-recommends ${{ matrix.compiler-pkg }}
      - name: Check compiler versions
        run: |
          which ${CC:-gcc}
          ${CC:-gcc} --version
          which ${CXX:-g++}
          ${CXX:-g++} --version
      - name: Build some Linux dependencies from source
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          ci/build-deps-ubuntu.sh
      - name: upload VRPN logs
        if: failure() && startsWith(matrix.os, 'ubuntu')
        uses: actions/upload-artifact@v3
        with:
          name: ${{ matrix.os }} ${{ matrix.xcode }}${{ env.CXX }} VRPN logs
          path: "vrpn/build/CMakeFiles/*.log"
      - name: Download and unzip InterSense SDK
        run: |
          wget https://www.intersense.com/wp-content/uploads/2018/12/InterSense_SDK_4.2381.zip
          unzip InterSense_SDK_4.2381.zip
      - name: Install InterSense SDK
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          sudo cp SDK/Linux/Sample/*.h /usr/local/include
          sudo cp SDK/Linux/x86_64/libisense.so /usr/local/lib
          sudo ldconfig
      - name: Install InterSense SDK
        if: startsWith(matrix.os, 'macos')
        run: |
          sudo cp SDK/MacOSX/Sample/*.h /usr/local/include
          sudo cp SDK/MacOSX/UniversalLib/libisense.dylib /usr/local/lib
      - name: Install cargo-c
        run: |
          cargo install cargo-c --force
      - name: Clone ASDF repo
        uses: actions/checkout@v3
        with:
          repository: AudioSceneDescriptionFormat/asdf-rust
          submodules: true
          path: asdf-rust
      - name: Build and install ASDF library
        working-directory: asdf-rust
        run: |
          cargo cinstall --release --prefix=/usr/local --destdir=temp
          sudo cp -r temp/usr/local/* /usr/local/
      - name: Update dynamic linker cache for libasdf
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          sudo ldconfig
      - run: |
          ./autogen.sh
      - name: configure
        # NB: browser-gui is tested with CircleCI
        run: |
          ./configure --enable-gui --enable-ip-interface --enable-websocket-interface --enable-fudi-interface --enable-ecasound --enable-sofa --enable-polhemus --enable-razor --enable-vrpn --enable-intersense --disable-browser-gui --enable-dynamic-asdf --disable-dependency-tracking
      - name: upload config.log
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: ${{ matrix.os }} ${{ matrix.xcode }}${{ env.CXX }} configure log
          path: config.log
      - run: |
          make
      - run: |
          sudo make install
      - name: Check dynamic libraries
        if: startsWith(matrix.os, 'ubuntu')
        run: |
          ldd $(which ssr-binaural)
      - name: Check dynamic libraries
        if: startsWith(matrix.os, 'macos')
        run: |
          otool -L $(which ssr-binaural)
      # It's probably hard to run the SSR with JACK and everything,
      # but we can at least check if we can run the executable,
      # which should also test if all dynamic libraries are found:
      - run: |
          ssr-binaural --help
