name: Metrics
on: [pull_request_target]

jobs:
  binary-size:
    runs-on: ubuntu-24.04-arm
    steps:
      - name: Install dependencies
        run: sudo apt-get install build-essential imagemagick libfreetype6-dev libjpeg-dev libpng-dev pkg-config
      - name: Install ARM toolchain
        run: sudo apt-get install gcc-arm-none-eabi binutils-arm-none-eabi
      - name: Checkout PR base
        uses: actions/checkout@v5
        with:
          submodules: recursive
          ref: ${{ github.event.pull_request.base.sha }}
          path: base
      - name: Build base
        run: make -j2 -C base MODEL=n0110 epsilon.elf
      - name: Checkout PR head
        uses: actions/checkout@v5
        with:
          submodules: recursive
          ref: ${{ github.event.pull_request.head.sha }}
          path: head
      - name: Build head
        run: make -j2 -C head MODEL=n0110 epsilon.elf
      - name: Retrieve binary size analysis
        run: |
          table="$(python3 head/build/metrics/binary_size.py base/output/release/device/n0110/epsilon.elf head/output/release/device/n0110/epsilon.elf --labels Base Head --sections .text .rodata .bss .data --custom 'Total (RAM)' .data .bss --custom 'Total (ROM)' .text .rodata .data --escape)"
          {
            echo 'table<<EOF'
            echo "${table//%0A/$'\n'}"
            echo EOF
          } >> "$GITHUB_ENV"
      - name: Add comment
        uses: actions/github-script@v8
        with:
          script: |
            await  github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.payload.pull_request.number,
              body: `${{ env.table }}`,
            });
