name: CI

on:
  push:
  pull_request:

env:
  BOOST_VERSION: 1_85_0

jobs:
  static-analysis:

    strategy:
      fail-fast: false
      matrix:
        tool:
          - cppcheck

    runs-on: ubuntu-22.04

    steps:
      - uses: actions/checkout@v4
      - name: print environment
        run: env
      - name: dependencies
        run: sudo apt-get update && sudo apt-get install -yq libasound2-dev libsoxr-dev libvorbisidec-dev libvorbis-dev libflac-dev libopus-dev alsa-utils libpulse-dev libavahi-client-dev avahi-daemon debhelper ccache expat cppcheck
      - name: cache boost
        id: cache-boost
        uses: actions/cache@v4
        with:
          path: boost_${{ env.BOOST_VERSION }}
          key: boost-${{ env.BOOST_VERSION }}
          enableCrossOsArchive: true
      - name: get boost
        if: steps.cache-boost.outputs.cache-hit != 'true'
        run: |
          wget https://boostorg.jfrog.io/artifactory/main/release/${BOOST_VERSION//_/.}/source/boost_${BOOST_VERSION}.tar.bz2
          tar xjf boost_${BOOST_VERSION}.tar.bz2
      - name: configure
        run: |
          cmake -S . -B build \
            -DWERROR=ON -DBUILD_TESTS=ON \
            -DBOOST_ROOT=boost_${BOOST_VERSION} \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE \
            -DCMAKE_CXX_FLAGS="-DCMAKE_CXX_FLAGS=-DJSON_HAS_CPP_14"
      - name: analysis
        run: cmake --build build --target ${{ matrix.tool }}


  linux:

    strategy:
      fail-fast: false
      matrix:
        compiler:
          - clang-10
          - clang-11
          - clang-12
          - clang-13
          - clang-14
          - clang-15
          - clang-16
          - clang-17
          - clang-18
          - gcc-9
          - gcc-10
          - gcc-11
          - gcc-12
          - gcc-13
          - gcc-14
        include:
          - compiler: clang-10
            cc: clang-10
            cxx: clang++-10
            os: ubuntu-20.04
          - compiler: clang-11
            cc: clang-11
            cxx: clang++-11
            os: ubuntu-20.04
          - compiler: clang-12
            cc: clang-12
            cxx: clang++-12
            os: ubuntu-20.04
          - compiler: clang-13
            cc: clang-13
            cxx: clang++-13
            os: ubuntu-22.04
          - compiler: clang-14
            cc: clang-14
            cxx: clang++-14
            os: ubuntu-22.04
          - compiler: clang-15
            cc: clang-15
            cxx: clang++-15
            os: ubuntu-22.04
          - compiler: clang-16
            cc: clang-16
            cxx: clang++-16
            os: ubuntu-24.04
          - compiler: clang-17
            cc: clang-17
            cxx: clang++-17
            os: ubuntu-24.04
          - compiler: clang-18
            cc: clang-18
            cxx: clang++-18
            os: ubuntu-24.04
          - compiler: gcc-9
            cc: gcc-9
            cxx: g++-9
            os: ubuntu-22.04
          - compiler: gcc-10
            cc: gcc-10
            cxx: g++-10
            os: ubuntu-22.04
          - compiler: gcc-11
            cc: gcc-11
            cxx: g++-11
            os: ubuntu-22.04
          - compiler: gcc-12
            cc: gcc-12
            cxx: g++-12
            os: ubuntu-22.04
          - compiler: gcc-13
            cc: gcc-13
            cxx: g++-13
            os: ubuntu-24.04
          - compiler: gcc-14
            cc: gcc-14
            cxx: g++-14
            os: ubuntu-24.04

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
      - name: print environment
        run: env
      - name: dependencies
        run: sudo apt-get update && sudo apt-get install -yq libasound2-dev libsoxr-dev libvorbisidec-dev libvorbis-dev libflac-dev libopus-dev alsa-utils libpulse-dev libavahi-client-dev avahi-daemon debhelper ccache expat cppcheck
      - name: cache boost
        id: cache-boost
        uses: actions/cache@v4
        with:
          path: boost_${{ env.BOOST_VERSION }}
          key: boost-${{ env.BOOST_VERSION }}
          enableCrossOsArchive: true
      - name: get boost
        if: steps.cache-boost.outputs.cache-hit != 'true'
        run: |
          wget https://boostorg.jfrog.io/artifactory/main/release/${BOOST_VERSION//_/.}/source/boost_${BOOST_VERSION}.tar.bz2
          tar xjf boost_${BOOST_VERSION}.tar.bz2
      - name: cache ccache
        id: cache-ccache
        uses: actions/cache@v4
        with:
          # TODO: use environment variable $HOME/.ccache
          path: /home/runner/.ccache
          key: ${{ runner.os }}-${{ matrix.compiler }}-ccache-${{ github.sha }}
          restore-keys: ${{ runner.os }}-${{ matrix.compiler }}-ccache-
      #- name: ccache dump config
      #  run: ccache -p
      - name: configure
        run: |
          cmake -S . -B build \
            -DWERROR=ON -DBUILD_TESTS=ON \
            -DBOOST_ROOT=boost_${BOOST_VERSION} \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=TRUE \
            -DCMAKE_CXX_FLAGS="-DCMAKE_CXX_FLAGS=-DJSON_HAS_CPP_14"
        env:
          CC: ${{ matrix.cc }}
          CXX: ${{ matrix.cxx }}
      - name: build
        env:
          # TODO: use environment variable $HOME/.ccache
          CCACHE_DIR: /home/runner/.ccache
        run: cmake --build build --parallel 3 --verbose


  macos:

    strategy:
      fail-fast: false
      matrix:
        xcode:
          - "13.1"
          - "13.2"
          - "13.3"
          - "13.4"
          - "14.0"
          - "14.1"
          - "14.2"
          - "14.3"
          - "15.0"
          - "15.1"
          - "15.2"
          - "15.3"
          - "15.4"
        include:
          - xcode: "13.1"
            os: macos-12
          - xcode: "13.2"
            os: macos-12
          - xcode: "13.3"
            os: macos-12
          - xcode: "13.4"
            os: macos-12
          - xcode: "14.0"
            os: macos-12
          - xcode: "14.1"
            os: macos-13
          - xcode: "14.2"
            os: macos-13
          - xcode: "14.3"
            os: macos-14
          - xcode: "15.0"
            os: macos-14
          - xcode: "15.1"
            os: macos-14
          - xcode: "15.2"
            os: macos-14
          - xcode: "15.3"
            os: macos-14
          - xcode: "15.4"
            os: macos-14

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
      - name: print environment
        run: env
      - name: dependencies
        run: brew install pkgconfig libsoxr flac libvorbis opus ccache expat
      - name: cache boost
        id: cache-boost
        uses: actions/cache@v4
        with:
          path: boost_${{ env.BOOST_VERSION }}
          key: boost-${{ env.BOOST_VERSION }}
          enableCrossOsArchive: true
      - name: get boost
        if: steps.cache-boost.outputs.cache-hit != 'true'
        run: |
          wget https://boostorg.jfrog.io/artifactory/main/release/${BOOST_VERSION//_/.}/source/boost_${BOOST_VERSION}.tar.bz2
          tar xjf boost_${BOOST_VERSION}.tar.bz2
      - name: cache ccache
        id: cache-ccache
        uses: actions/cache@v4
        with:
          path: /Users/runner/Library/Caches/ccache
          key: ${{ runner.os }}-${{ matrix.xcode }}-ccache-${{ github.sha }}
          restore-keys: ${{ runner.os }}-${{ matrix.xcode }}-ccache-
      - name: configure
        run: |
          cmake -S . -B build \
            -DWERROR=ON -DBUILD_TESTS=ON \
            -DBOOST_ROOT=boost_${BOOST_VERSION} \
            -DCMAKE_BUILD_TYPE=Release \
            -DCMAKE_CXX_COMPILER_LAUNCHER=ccache \
            -DCMAKE_CXX_FLAGS="-I/usr/local/include -DCMAKE_CXX_FLAGS=-DJSON_HAS_CPP_14"
        env:
          DEVELOPER_DIR: /Applications/Xcode_${{ matrix.xcode }}.app/Contents/Developer
      - name: build
        run: cmake --build build --parallel 3 --verbose


  windows:

    strategy:
      fail-fast: false
      matrix:
        compiler:
          - vs-16
          - vs-17
        include:
          - compiler: vs-16
            os: windows-2019
            vs: "Visual Studio 16 2019"
          - compiler: vs-17
            os: windows-2022
            vs: "Visual Studio 17 2022"

    runs-on: ${{ matrix.os }}

    steps:
      - uses: actions/checkout@v4
      - name: cache dependencies
        id: cache-dependencies
        uses: actions/cache@v4
        with:
          #path: ${VCPKG_INSTALLATION_ROOT}\installed
          path: c:\vcpkg\installed
          key: ${{ runner.os }}-dependencies
      - name: dependencies
        if: steps.cache-dependencies.outputs.cache-hit != 'true'
        run: |
          cd c:\vcpkg
          git pull
          vcpkg.exe update
          vcpkg.exe --triplet x64-windows install libflac libvorbis soxr opus boost-asio
      - name: configure
        run: |
          echo vcpkg installation root: ${env:VCPKG_INSTALLATION_ROOT}
          cmake -S . -B build -G "${{ matrix.vs }}" `
            -DWERROR=ON -DBUILD_TESTS=ON `
            -DCMAKE_TOOLCHAIN_FILE="$env:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake" `
            -DVCPKG_TARGET_TRIPLET="x64-windows" `
            -DCMAKE_BUILD_TYPE=Release `
            -DREVISION="${{ github.sha }}"
      - name: build
        run: cmake --build build --config Release --parallel 3 --verbose
