name: CMake

on:
  push:
  pull_request:

env:
  # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
  BUILD_TYPE: Release

jobs:
  build:
    name: ${{matrix.name}}
    strategy:
      matrix:
        include:
          - os: ubuntu-20.04
            name: Linux
            cache-key: linux
            cmake-args: '-DPICO_SDK_PATH=$GITHUB_WORKSPACE/pico-sdk -DPICO_SDK_POST_LIST_DIRS=$GITHUB_WORKSPACE/pico-extras'
            apt-packages: ccache

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

    env:
      PICO_SDK_PATH: $GITHUB_WORKSPACE/pico-sdk

    steps:
    - name: Compiler Cache
      uses: actions/cache@v2
      with:
        path: /home/runner/.ccache
        key: ccache-cmake-${{github.ref}}-${{github.sha}}
        restore-keys: |
          ccache-cmake-${{github.ref}}
          ccache-cmake

    - uses: actions/checkout@v2
      with:
        submodules: true

    # Check out the Pico SDK
    - name: Checkout Pico SDK
      uses: actions/checkout@v2
      with:
        repository: raspberrypi/pico-sdk
        path: pico-sdk
        submodules: true

    # Check out the Pico Extras
    - name: Checkout Pico Extras
      uses: actions/checkout@v2
      with:
        repository: raspberrypi/pico-extras
        path: pico-extras
        submodules: false  # lwip breaks audio submodule fetching

    # Linux deps
    - name: Install deps
      if: runner.os == 'Linux'
      run: |
        sudo apt update && sudo apt install ${{matrix.apt-packages}}

    - name: Install ARM Toolchain
      if: runner.os == 'Linux'
      working-directory: ${{runner.workspace}}
      run: |
        wget -q https://developer.arm.com/-/media/Files/downloads/gnu-rm/10.3-2021.10/gcc-arm-none-eabi-10.3-2021.10-x86_64-linux.tar.bz2
        tar xf gcc-*.tar.bz2
        cd gcc*/bin
        pwd >> $GITHUB_PATH

    - name: Create Build Environment
      run: cmake -E make_directory ${{runner.workspace}}/build

    - name: Configure CMake
      shell: bash
      working-directory: ${{runner.workspace}}/build
      run: cmake $GITHUB_WORKSPACE  -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache ${{matrix.cmake-args}}

    - name: Build
      working-directory: ${{runner.workspace}}/build
      shell: bash
      run: |
        ccache --zero-stats || true
        cmake --build . --config $BUILD_TYPE -j 2
        ccache --show-stats || true
