name: MicroPython

on:
  push:
  pull_request:
  release:
    types: [created]

env:
  MICROPYTHON_VERSION: v1.22.2

jobs:
  build:
    name: ${{ matrix.name }} (${{ matrix.board }})
    runs-on: ubuntu-20.04
    continue-on-error: true
    strategy:
      matrix:
        include:
        - name: pico
          board: RPI_PICO
        - name: picow
          board: RPI_PICO_W
        - name: pimoroni_pico
          board: PIMORONI_PICO
        - name: tiny2040_8mb
          board: PIMORONI_TINY2040
        - name: picolipo_4mb
          board: PIMORONI_PICOLIPO_4MB
        - name: picolipo_16mb
          board: PIMORONI_PICOLIPO_16MB
        - name: tufty2040
          board: PIMORONI_TUFTY2040
        - name: enviro
          board: PICO_W_ENVIRO
        - name: galactic_unicorn
          board: RPI_PICO_W
        - name: cosmic_unicorn
          board: RPI_PICO_W
        - name: stellar_unicorn
          board: RPI_PICO_W
        - name: inky_frame
          board: PICO_W_INKY

    env:
      # MicroPython version will be contained in github.event.release.tag_name for releases
      RELEASE_FILE: pimoroni-${{ matrix.name }}-${{ github.event.release.tag_name || github.sha }}-micropython
      PIMORONI_PICO_DIR: "${{ github.workspace }}/pimoroni-pico-${{ github.sha }}"
      MICROPY_BOARD_DIR: "${{ github.workspace }}/pimoroni-pico-${{ github.sha }}/micropython/board/${{ matrix.BOARD }}"
      USER_C_MODULES: "${{ github.workspace }}/pimoroni-pico-${{ github.sha }}/micropython/modules/micropython-${{ matrix.name }}.cmake"
      TAG_OR_SHA: ${{ github.event.release.tag_name || github.sha }}
      MICROPY_BOARD: ${{ matrix.board }}
      BOARD_NAME: ${{ matrix.name }}
      BUILD_TOOLS: pimoroni-pico-${{ github.sha }}/ci/micropython.sh

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

    - uses: actions/checkout@v4
      with:
        submodules: true
        path: pimoroni-pico-${{ github.sha }}

    - name: Install Arm GNU Toolchain (arm-none-eabi-gcc)
      uses: carlosperate/arm-none-eabi-gcc-action@v1
      with:
        release: '9-2020-q2'

    - name: Install CCache
      run: |
        source $BUILD_TOOLS
        apt_install_build_deps
      
    - name: Checkout MicroPython & Submodules
      run: |
        source $BUILD_TOOLS
        micropython_clone

    - name: Build MPY Cross
      run: |
        source $BUILD_TOOLS
        micropython_build_mpy_cross

    - name: "HACK: CMakeLists.txt Disable C++ Exceptions Patch"
      shell: bash
      run: |
        source $BUILD_TOOLS
        hack_patch_micropython_disable_exceptions

    - name: "HACK: CMakeLists.txt Print Memory Usage"
      shell: bash
      run: |
        source $BUILD_TOOLS
        hack_patch_micropython_print_memory_usage

    - name: "HACK: CMakeLists.txt Enable Custom Linker Patch"
      shell: bash
      run: |
        source $BUILD_TOOLS
        hack_patch_micropython_enable_custom_linker

    - name: "HACK: Pico SDK Patch"
      shell: bash
      run: |
        source $BUILD_TOOLS
        hack_patch_pico_sdk

    - name: Configure MicroPython
      shell: bash
      run: |
        source $BUILD_TOOLS
        micropython_version
        cmake_configure

    - name: Build MicroPython
      shell: bash
      run: |
        source $BUILD_TOOLS
        cmake_build

    - name: Store .uf2 as artifact
      uses: actions/upload-artifact@v4
      with:
        name: ${{ env.RELEASE_FILE }}.uf2
        path: build-${{ matrix.name }}/${{ env.RELEASE_FILE }}.uf2

    - name: Upload .uf2
      if: github.event_name == 'release'
      uses: actions/upload-release-asset@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        asset_path: build-${{ matrix.name }}/firmware.uf2
        upload_url: ${{ github.event.release.upload_url }}
        asset_name: ${{ env.RELEASE_FILE }}.uf2
        asset_content_type: application/octet-stream
