name: Build Firmware

on: 
  push:
    branches: [ 4.3-bsp-rebase ]
  pull_request:
    branches: [ 4.3-bsp-rebase ]
  workflow_dispatch:
    branches: [ 4.3-bsp-rebase ]
  workflow_call:
    inputs:
      release_build:
        description: 'Specifies if it is a debug build or a release build'
        default: false
        required: false
        type: boolean

jobs:
  setup:
    name: Setup
    runs-on: ubuntu-22.04
    steps:
      - uses: actions/checkout@v3

      - name: Cache ARM toolchain
        uses: actions/cache@v3
        id: cache-toolchain
        with:
          path: tools
          key: ${{ runner.os }}-${{ hashFiles('make/tools.mk') }}

      - name: Download and install toolchain
        if: steps.cache-toolchain.outputs.cache-hit != 'true'
        run: make arm_sdk_install

  build:
    name: Build
    needs: setup
    runs-on: ubuntu-22.04
    strategy:
      matrix:
        targets: [ EMSRPROTO1, EMSRPROTO2, EMSRPROTO3, EMSRPROTO4, NEUTRONRCF435MINI, NEUTRONRCF435SE ]
    steps:
      - name: Code Checkout
        uses: actions/checkout@v2

      - name: Fetch cached ARM toolchain
        uses: actions/cache@v3
        id: cache-toolchain
        with:
          path: tools
          key: ${{ runner.os }}-${{ hashFiles('make/tools.mk') }}

      - name: Build All firmware
        run: make TARGET=${{ matrix.targets }} RELEASE=${{ inputs.release_build && 'yes' || 'no' }}

      - name: Upload artifacts
        uses: actions/upload-artifact@v3
        with:
          name: ci_${{ matrix.targets }}_${{ github.run_number }}
          path: obj/*.hex
