From 3d190475ad2b71d913e059dfc6f5cc5dafe6555d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 3 Dec 2025 00:08:45 +0700 Subject: upload metrics.json, test ci --- .github/workflows/build.yml | 374 +++++++++++++++++++-------------------- .github/workflows/build_util.yml | 9 +- 2 files changed, 195 insertions(+), 188 deletions(-) (limited to '.github/workflows') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f1b134b8a..7d7901c3a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -83,190 +83,190 @@ jobs: # --------------------------------------- # Build Make: only build on push with one-per-family # --------------------------------------- - make: - if: github.event_name == 'push' - needs: set-matrix - uses: ./.github/workflows/build_util.yml - strategy: - fail-fast: false - matrix: - toolchain: - - 'aarch64-gcc' - #- 'arm-clang' - - 'arm-gcc' - - 'msp430-gcc' - - 'riscv-gcc' - - 'rx-gcc' - with: - build-system: 'make' - toolchain: ${{ matrix.toolchain }} - build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} - one-per-family: true - - # --------------------------------------- - # Build IAR - # Since IAR Token secret is not passed to forked PR, only build non-forked PR with make. - # cmake is built by circle-ci. Due to IAR limit capacity, only build oe per family - # --------------------------------------- - arm-iar: - if: false # disable for now since we got reach capacity limit too often - #if: github.event_name == 'push' && github.repository_owner == 'hathach' - needs: set-matrix - uses: ./.github/workflows/build_util.yml - secrets: inherit - strategy: - fail-fast: false - matrix: - build-system: - - 'make' - with: - build-system: ${{ matrix.build-system }} - toolchain: 'arm-iar' - build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)['arm-iar']) }} - one-per-family: true - - # --------------------------------------- - # Build Make/CMake on Windows/MacOS - # --------------------------------------- - build-os: - if: github.event_name == 'pull_request' - uses: ./.github/workflows/build_util.yml - strategy: - fail-fast: false - matrix: - os: [windows-latest, macos-latest] - build-system: [ 'make', 'cmake' ] - with: - os: ${{ matrix.os }} - build-system: ${{ matrix.build-system }} - toolchain: 'arm-gcc-${{ matrix.os }}' - build-args: '["stm32h7"]' - one-per-family: true - - # --------------------------------------- - # Zephyr - # --------------------------------------- - zephyr: - if: github.event_name == 'push' - runs-on: ubuntu-latest - steps: - - name: Checkout TinyUSB - uses: actions/checkout@v4 - - - name: Setup Zephyr project - uses: zephyrproject-rtos/action-zephyr-setup@v1 - with: - app-path: examples - toolchains: arm-zephyr-eabi - - - name: Build - run: | - west build -b nrf52840dk -d examples/device/cdc_msc/build examples/device/cdc_msc -- -DRTOS=zephyr - west build -b nrf52840dk -d examples/device/msc_dual_lun/build examples/device/msc_dual_lun -- -DRTOS=zephyr - - # --------------------------------------- - # Hardware in the loop (HIL) - # Run on PR only (hil-tinyusb), hil-hfp only run on non-forked PR - # --------------------------------------- - hil-build: - if: | - github.repository_owner == 'hathach' && - (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') - needs: set-matrix - uses: ./.github/workflows/build_util.yml - strategy: - fail-fast: false - matrix: - toolchain: - - 'arm-gcc' - - 'esp-idf' - with: - build-system: 'cmake' - toolchain: ${{ matrix.toolchain }} - build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.hil_json)[matrix.toolchain]) }} - one-per-family: true - upload-artifacts: true - - # --------------------------------------- - # Hardware in the loop (HIL) - # self-hosted on local VM, for attached hardware checkout HIL_JSON - # --------------------------------------- - hil-tinyusb: - if: | - github.repository_owner == 'hathach' && - (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') - needs: hil-build - runs-on: [self-hosted, X64, hathach, hardware-in-the-loop] - steps: - - name: Get Skip Boards from previous run - if: github.run_attempt != '1' - run: | - if [ -f "${{ env.HIL_JSON }}.skip" ]; then - SKIP_BOARDS=$(cat "${{ env.HIL_JSON }}.skip") - else - SKIP_BOARDS="" - fi - echo "SKIP_BOARDS=$SKIP_BOARDS" - echo "SKIP_BOARDS=$SKIP_BOARDS" >> $GITHUB_ENV - - - name: Clean workspace - run: | - echo "Cleaning up for the first run" - rm -rf "${{ github.workspace }}" - mkdir -p "${{ github.workspace }}" - - - name: Checkout TinyUSB - uses: actions/checkout@v4 - - - name: Download Artifacts - uses: actions/download-artifact@v5 - with: - path: cmake-build - merge-multiple: true - - - name: Test on actual hardware - run: | - python3 test/hil/hil_test.py ${{ env.HIL_JSON }} $SKIP_BOARDS - - # --------------------------------------- - # Hardware in the loop (HIL) - # self-hosted by HFP, build with IAR toolchain, for attached hardware checkout test/hil/hfp.json - # Since IAR Token secret is not passed to forked PR, only build non-forked PR - # --------------------------------------- - hil-hfp: - if: | - github.repository_owner == 'hathach' && - github.event.pull_request.head.repo.fork == false && - (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') - runs-on: [self-hosted, Linux, X64, hifiphile] - env: - IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} - steps: - - name: Clean workspace - run: | - echo "Cleaning up previous run" - rm -rf "${{ github.workspace }}"3 - mkdir -p "${{ github.workspace }}" - - - name: Toolchain version - run: | - iccarm --version - - - name: Checkout TinyUSB - uses: actions/checkout@v4 - - - name: Get build boards - run: | - MATRIX_JSON=$(python test/hil/hil_ci_set_matrix.py test/hil/hfp.json) - BUILD_ARGS=$(echo $MATRIX_JSON | jq -r '.["arm-gcc"] | join(" ")') - echo "BUILD_ARGS=$BUILD_ARGS" - echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV - - - name: Get Dependencies - run: python3 tools/get_deps.py $BUILD_ARGS - - - name: Build - run: python3 tools/build.py -j 4 --toolchain iar $BUILD_ARGS - - - name: Test on actual hardware (hardware in the loop) - run: python3 test/hil/hil_test.py hfp.json +# make: +# if: github.event_name == 'push' +# needs: set-matrix +# uses: ./.github/workflows/build_util.yml +# strategy: +# fail-fast: false +# matrix: +# toolchain: +# - 'aarch64-gcc' +# #- 'arm-clang' +# - 'arm-gcc' +# - 'msp430-gcc' +# - 'riscv-gcc' +# - 'rx-gcc' +# with: +# build-system: 'make' +# toolchain: ${{ matrix.toolchain }} +# build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} +# one-per-family: true +# +# # --------------------------------------- +# # Build IAR +# # Since IAR Token secret is not passed to forked PR, only build non-forked PR with make. +# # cmake is built by circle-ci. Due to IAR limit capacity, only build oe per family +# # --------------------------------------- +# arm-iar: +# if: false # disable for now since we got reach capacity limit too often +# #if: github.event_name == 'push' && github.repository_owner == 'hathach' +# needs: set-matrix +# uses: ./.github/workflows/build_util.yml +# secrets: inherit +# strategy: +# fail-fast: false +# matrix: +# build-system: +# - 'make' +# with: +# build-system: ${{ matrix.build-system }} +# toolchain: 'arm-iar' +# build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)['arm-iar']) }} +# one-per-family: true +# +# # --------------------------------------- +# # Build Make/CMake on Windows/MacOS +# # --------------------------------------- +# build-os: +# if: github.event_name == 'pull_request' +# uses: ./.github/workflows/build_util.yml +# strategy: +# fail-fast: false +# matrix: +# os: [windows-latest, macos-latest] +# build-system: [ 'make', 'cmake' ] +# with: +# os: ${{ matrix.os }} +# build-system: ${{ matrix.build-system }} +# toolchain: 'arm-gcc-${{ matrix.os }}' +# build-args: '["stm32h7"]' +# one-per-family: true +# +# # --------------------------------------- +# # Zephyr +# # --------------------------------------- +# zephyr: +# if: github.event_name == 'push' +# runs-on: ubuntu-latest +# steps: +# - name: Checkout TinyUSB +# uses: actions/checkout@v4 +# +# - name: Setup Zephyr project +# uses: zephyrproject-rtos/action-zephyr-setup@v1 +# with: +# app-path: examples +# toolchains: arm-zephyr-eabi +# +# - name: Build +# run: | +# west build -b nrf52840dk -d examples/device/cdc_msc/build examples/device/cdc_msc -- -DRTOS=zephyr +# west build -b nrf52840dk -d examples/device/msc_dual_lun/build examples/device/msc_dual_lun -- -DRTOS=zephyr +# +# # --------------------------------------- +# # Hardware in the loop (HIL) +# # Run on PR only (hil-tinyusb), hil-hfp only run on non-forked PR +# # --------------------------------------- +# hil-build: +# if: | +# github.repository_owner == 'hathach' && +# (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') +# needs: set-matrix +# uses: ./.github/workflows/build_util.yml +# strategy: +# fail-fast: false +# matrix: +# toolchain: +# - 'arm-gcc' +# - 'esp-idf' +# with: +# build-system: 'cmake' +# toolchain: ${{ matrix.toolchain }} +# build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.hil_json)[matrix.toolchain]) }} +# one-per-family: true +# upload-artifacts: true +# +# # --------------------------------------- +# # Hardware in the loop (HIL) +# # self-hosted on local VM, for attached hardware checkout HIL_JSON +# # --------------------------------------- +# hil-tinyusb: +# if: | +# github.repository_owner == 'hathach' && +# (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') +# needs: hil-build +# runs-on: [self-hosted, X64, hathach, hardware-in-the-loop] +# steps: +# - name: Get Skip Boards from previous run +# if: github.run_attempt != '1' +# run: | +# if [ -f "${{ env.HIL_JSON }}.skip" ]; then +# SKIP_BOARDS=$(cat "${{ env.HIL_JSON }}.skip") +# else +# SKIP_BOARDS="" +# fi +# echo "SKIP_BOARDS=$SKIP_BOARDS" +# echo "SKIP_BOARDS=$SKIP_BOARDS" >> $GITHUB_ENV +# +# - name: Clean workspace +# run: | +# echo "Cleaning up for the first run" +# rm -rf "${{ github.workspace }}" +# mkdir -p "${{ github.workspace }}" +# +# - name: Checkout TinyUSB +# uses: actions/checkout@v4 +# +# - name: Download Artifacts +# uses: actions/download-artifact@v5 +# with: +# path: cmake-build +# merge-multiple: true +# +# - name: Test on actual hardware +# run: | +# python3 test/hil/hil_test.py ${{ env.HIL_JSON }} $SKIP_BOARDS +# +# # --------------------------------------- +# # Hardware in the loop (HIL) +# # self-hosted by HFP, build with IAR toolchain, for attached hardware checkout test/hil/hfp.json +# # Since IAR Token secret is not passed to forked PR, only build non-forked PR +# # --------------------------------------- +# hil-hfp: +# if: | +# github.repository_owner == 'hathach' && +# github.event.pull_request.head.repo.fork == false && +# (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') +# runs-on: [self-hosted, Linux, X64, hifiphile] +# env: +# IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} +# steps: +# - name: Clean workspace +# run: | +# echo "Cleaning up previous run" +# rm -rf "${{ github.workspace }}"3 +# mkdir -p "${{ github.workspace }}" +# +# - name: Toolchain version +# run: | +# iccarm --version +# +# - name: Checkout TinyUSB +# uses: actions/checkout@v4 +# +# - name: Get build boards +# run: | +# MATRIX_JSON=$(python test/hil/hil_ci_set_matrix.py test/hil/hfp.json) +# BUILD_ARGS=$(echo $MATRIX_JSON | jq -r '.["arm-gcc"] | join(" ")') +# echo "BUILD_ARGS=$BUILD_ARGS" +# echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV +# +# - name: Get Dependencies +# run: python3 tools/get_deps.py $BUILD_ARGS +# +# - name: Build +# run: python3 tools/build.py -j 4 --toolchain iar $BUILD_ARGS +# +# - name: Test on actual hardware (hardware in the loop) +# run: python3 test/hil/hil_test.py hfp.json diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index 55901b838..848694597 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -69,11 +69,18 @@ jobs: fi shell: bash + - name: Upload Artifacts for Metrics + if: inputs.build-system == 'cmake' + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.arg }}-metrics + path: cmake-build/cmake-build-*/metrics.json + - name: Upload Artifacts for Hardware Testing if: ${{ inputs.upload-artifacts }} uses: actions/upload-artifact@v4 with: - name: ${{ matrix.arg }} + name: ${{ matrix.arg }}-binaries path: | cmake-build/cmake-build-*/*/*/*.elf cmake-build/cmake-build-*/*/*/*.bin -- cgit v1.3.1 From ee3d3e3551f95757b85de1c2c9777a1daed8f78d Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 3 Dec 2025 09:57:49 +0700 Subject: upload metrics.json and aggregate code metrics, fine tune ci matrix run --- .github/workflows/build.yml | 26 +++++++++++++++++++++++--- .github/workflows/build_util.yml | 12 ++++++++---- .github/workflows/ci_set_matrix.py | 18 +++++------------- tools/build.py | 13 +++++++++---- tools/get_deps.py | 2 +- 5 files changed, 46 insertions(+), 25 deletions(-) (limited to '.github/workflows') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7d7901c3a..5e996d9d9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,11 +57,10 @@ jobs: echo "hil_matrix=$HIL_MATRIX_JSON" >> $GITHUB_OUTPUT # --------------------------------------- - # Build CMake: only build on push with one-per-family. + # Build CMake: only one-per-family. # Full built is done by CircleCI in PR # --------------------------------------- cmake: - if: github.event_name == 'push' needs: set-matrix uses: ./.github/workflows/build_util.yml strategy: @@ -71,7 +70,7 @@ jobs: - 'aarch64-gcc' #- 'arm-clang' - 'arm-gcc' - - 'esp-idf' + # - 'esp-idf' - 'msp430-gcc' - 'riscv-gcc' with: @@ -79,6 +78,27 @@ jobs: toolchain: ${{ matrix.toolchain }} build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} one-per-family: true + upload-metrics: true + + code-metrics: + needs: cmake + runs-on: ubuntu-latest + steps: + - name: Checkout TinyUSB + uses: actions/checkout@v4 + + - name: Download Artifacts + uses: actions/download-artifact@v5 + with: + pattern: metrics-* + path: cmake-build + merge-multiple: true + + - name: Aggregate Code Metrics + run: | + tree cmake-build + python tools/get_deps.py + python tools/metrics.py -f tinyusb/src cmake-build/*/metrics.json # --------------------------------------- # Build Make: only build on push with one-per-family diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index 848694597..2de0ed229 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -20,6 +20,10 @@ on: required: false default: false type: boolean + upload-metrics: + required: false + default: false + type: boolean os: required: false type: string @@ -70,17 +74,17 @@ jobs: shell: bash - name: Upload Artifacts for Metrics - if: inputs.build-system == 'cmake' + if: ${{ inputs.upload-metrics }} uses: actions/upload-artifact@v4 with: - name: ${{ matrix.arg }}-metrics + name: metrics-${{ matrix.arg }} path: cmake-build/cmake-build-*/metrics.json - name: Upload Artifacts for Hardware Testing if: ${{ inputs.upload-artifacts }} - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: - name: ${{ matrix.arg }}-binaries + name: binaries-${{ matrix.arg }} path: | cmake-build/cmake-build-*/*/*/*.elf cmake-build/cmake-build-*/*/*/*.bin diff --git a/.github/workflows/ci_set_matrix.py b/.github/workflows/ci_set_matrix.py index 9d0e42c2e..5032c83ae 100755 --- a/.github/workflows/ci_set_matrix.py +++ b/.github/workflows/ci_set_matrix.py @@ -15,28 +15,22 @@ toolchain_list = [ # family: [supported toolchain] family_list = { - "at32f402_405 at32f403a_407 at32f413 at32f415 at32f423 at32f425 at32f435_437": ["arm-gcc"], - "broadcom_32bit": ["arm-gcc"], + "at32f402_405 at32f403a_407 at32f413 at32f415 at32f423 at32f425 at32f435_437 broadcom_32bit da1469x": ["arm-gcc"], "broadcom_64bit": ["aarch64-gcc"], "ch32v10x ch32v20x ch32v30x fomu gd32vf103": ["riscv-gcc"], - "da1469x": ["arm-gcc"], "imxrt": ["arm-gcc", "arm-clang"], "kinetis_k kinetis_kl kinetis_k32l2": ["arm-gcc", "arm-clang"], "lpc11 lpc13 lpc15": ["arm-gcc", "arm-clang"], "lpc17 lpc18 lpc40 lpc43": ["arm-gcc", "arm-clang"], "lpc51 lpc54 lpc55": ["arm-gcc", "arm-clang"], - "maxim": ["arm-gcc"], - "mcx": ["arm-gcc"], - "mm32": ["arm-gcc"], + "maxim mcx mm32 msp432e4 tm4c": ["arm-gcc"], "msp430": ["msp430-gcc"], - "msp432e4 tm4c": ["arm-gcc"], "nrf": ["arm-gcc", "arm-clang"], - "nuc100_120 nuc121_125 nuc126 nuc505": ["arm-gcc"], + "nuc100_120 nuc121_125 nuc126 nuc505 xmc4000": ["arm-gcc"], "ra": ["arm-gcc"], "rp2040": ["arm-gcc"], "rx": ["rx-gcc"], - "samd11 samd2x_l2x": ["arm-gcc", "arm-clang"], - "samd5x_e5x samg": ["arm-gcc", "arm-clang"], + "samd11 samd2x_l2x samd5x_e5x samg": ["arm-gcc", "arm-clang"], "stm32c0 stm32f0 stm32f1 stm32f2 stm32f3": ["arm-gcc", "arm-clang", "arm-iar"], "stm32f4": ["arm-gcc", "arm-clang", "arm-iar"], "stm32f7": ["arm-gcc", "arm-clang", "arm-iar"], @@ -45,9 +39,7 @@ family_list = { "stm32h7rs": ["arm-gcc", "arm-clang", "arm-iar"], "stm32l0 stm32l4": ["arm-gcc", "arm-clang", "arm-iar"], "stm32n6": ["arm-gcc"], - "stm32u0 stm32u5 stm32wb": ["arm-gcc", "arm-clang", "arm-iar"], - "stm32wba": ["arm-gcc", "arm-clang"], - "xmc4000": ["arm-gcc"], + "stm32u0 stm32u5 stm32wb stm32wba": ["arm-gcc", "arm-clang", "arm-iar"], "-bespressif_s2_devkitc": ["esp-idf"], # S3, P4 will be built by hil test # "-bespressif_s3_devkitm": ["esp-idf"], diff --git a/tools/build.py b/tools/build.py index 5392a9aa4..b87af6c6a 100755 --- a/tools/build.py +++ b/tools/build.py @@ -6,8 +6,6 @@ import sys import time import subprocess import shlex -import glob -import metrics from pathlib import Path from multiprocessing import Pool @@ -26,6 +24,7 @@ build_separator = '-' * 95 build_status = [STATUS_OK, STATUS_FAILED, STATUS_SKIPPED] verbose = False +clean_build = False parallel_jobs = os.cpu_count() # ----------------------------- @@ -117,11 +116,13 @@ def cmake_board(board, build_args, build_flags_on): f'-DBOARD={board}', '-DCMAKE_BUILD_TYPE=MinSizeRel', '-DLINKERMAP_OPTION=-q -f tinyusb/src', *build_args, *build_flags]) if rcmd.returncode == 0: + if clean_build: + run_cmd(["cmake", "--build", build_dir, '--target', 'clean']) cmd = ["cmake", "--build", build_dir, '--parallel', str(parallel_jobs)] rcmd = run_cmd(cmd) if rcmd.returncode == 0: ret[0] += 1 - rcmd = run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_examples_metrics']) + run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_examples_metrics']) # print(rcmd.stdout.decode("utf-8")) else: ret[1] += 1 @@ -148,7 +149,8 @@ def make_one_example(example, board, make_option): if make_option: make_args += shlex.split(make_option) make_args.append("all") - # run_cmd(make_args + ["clean"]) + if clean_build: + run_cmd(make_args + ["clean"]) build_result = run_cmd(make_args) r = 0 if build_result.returncode == 0 else 1 print_build_result(board, example, r, time.monotonic() - start_time) @@ -235,11 +237,13 @@ def get_family_boards(family, one_per_family, boards): # ----------------------------- def main(): global verbose + global clean_build global parallel_jobs parser = argparse.ArgumentParser() parser.add_argument('families', nargs='*', default=[], help='Families to build') parser.add_argument('-b', '--board', action='append', default=[], help='Boards to build') + parser.add_argument('-c', '--clean', action='store_true', default=False, help='Clean before build') parser.add_argument('-t', '--toolchain', default='gcc', help='Toolchain to use, default is gcc') parser.add_argument('-s', '--build-system', default='cmake', help='Build system to use, default is cmake') parser.add_argument('-D', '--define-symbol', action='append', default=[], help='Define to pass to build system') @@ -257,6 +261,7 @@ def main(): build_flags_on = args.build_flags_on one_per_family = args.one_per_family verbose = args.verbose + clean_build = args.clean parallel_jobs = args.jobs build_defines.append(f'TOOLCHAIN={toolchain}') diff --git a/tools/get_deps.py b/tools/get_deps.py index fe2f51e01..9634451e2 100755 --- a/tools/get_deps.py +++ b/tools/get_deps.py @@ -15,7 +15,7 @@ deps_mandatory = { '159e31b689577dbf69cf0683bbaffbd71fa5ee10', 'all'], 'tools/linkermap': ['https://github.com/hathach/linkermap.git', - '87f94869f9ff828812f4551138f82c3bfcaf2620', + '46c3c2947db366fb66af6723709febf80d860bc1', 'all'], 'tools/uf2': ['https://github.com/microsoft/uf2.git', 'c594542b2faa01cc33a2b97c9fbebc38549df80a', -- cgit v1.3.1 From f51ca33f25841147e93c72458c927261806cdc0e Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 3 Dec 2025 11:09:41 +0700 Subject: upload metrics.json and aggregate code metrics, post metrics comment fine tune ci matrix run --- .github/workflows/build.yml | 416 ++++++++++++++++++++----------------- .github/workflows/build_util.yml | 2 +- .github/workflows/ci_set_matrix.py | 6 +- examples/CMakeLists.txt | 4 +- hw/bsp/family_support.cmake | 6 +- tools/build.py | 2 +- tools/get_deps.py | 2 +- tools/metrics.py | 254 ++++++++++++++++++++-- 8 files changed, 472 insertions(+), 220 deletions(-) (limited to '.github/workflows') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5e996d9d9..b0b636c65 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -83,6 +83,8 @@ jobs: code-metrics: needs: cmake runs-on: ubuntu-latest + permissions: + pull-requests: write steps: - name: Checkout TinyUSB uses: actions/checkout@v4 @@ -96,197 +98,233 @@ jobs: - name: Aggregate Code Metrics run: | - tree cmake-build python tools/get_deps.py - python tools/metrics.py -f tinyusb/src cmake-build/*/metrics.json + pip install tools/linkermap/ + python tools/metrics.py combine -j -m -f tinyusb/src cmake-build/*/metrics.json + + - name: Upload Metrics Artifact + if: github.event_name == 'push' + uses: actions/upload-artifact@v5 + with: + name: metrics-tinyusb + path: metrics.json + + - name: Download Base Branch Metrics + if: github.event_name == 'pull_request' + uses: dawidd6/action-download-artifact@v11 + with: + workflow: build.yml + branch: ${{ github.base_ref }} + name: metrics-tinyusb + path: base-metrics + continue-on-error: true + + - name: Compare with Base Branch + if: github.event_name == 'pull_request' + run: | + if [ -f base-metrics/metrics.json ]; then + python tools/metrics.py compare -f tinyusb/src base-metrics/metrics.json metrics.json + cat metrics_compare.md + else + echo "No base metrics found, skipping comparison" + cp metrics.md metrics_compare.md + fi + + - name: Post Code Metrics as PR Comment + if: github.event_name == 'pull_request' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: code-metrics + path: metrics_compare.md + # --------------------------------------- # Build Make: only build on push with one-per-family # --------------------------------------- -# make: -# if: github.event_name == 'push' -# needs: set-matrix -# uses: ./.github/workflows/build_util.yml -# strategy: -# fail-fast: false -# matrix: -# toolchain: -# - 'aarch64-gcc' -# #- 'arm-clang' -# - 'arm-gcc' -# - 'msp430-gcc' -# - 'riscv-gcc' -# - 'rx-gcc' -# with: -# build-system: 'make' -# toolchain: ${{ matrix.toolchain }} -# build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} -# one-per-family: true -# -# # --------------------------------------- -# # Build IAR -# # Since IAR Token secret is not passed to forked PR, only build non-forked PR with make. -# # cmake is built by circle-ci. Due to IAR limit capacity, only build oe per family -# # --------------------------------------- -# arm-iar: -# if: false # disable for now since we got reach capacity limit too often -# #if: github.event_name == 'push' && github.repository_owner == 'hathach' -# needs: set-matrix -# uses: ./.github/workflows/build_util.yml -# secrets: inherit -# strategy: -# fail-fast: false -# matrix: -# build-system: -# - 'make' -# with: -# build-system: ${{ matrix.build-system }} -# toolchain: 'arm-iar' -# build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)['arm-iar']) }} -# one-per-family: true -# -# # --------------------------------------- -# # Build Make/CMake on Windows/MacOS -# # --------------------------------------- -# build-os: -# if: github.event_name == 'pull_request' -# uses: ./.github/workflows/build_util.yml -# strategy: -# fail-fast: false -# matrix: -# os: [windows-latest, macos-latest] -# build-system: [ 'make', 'cmake' ] -# with: -# os: ${{ matrix.os }} -# build-system: ${{ matrix.build-system }} -# toolchain: 'arm-gcc-${{ matrix.os }}' -# build-args: '["stm32h7"]' -# one-per-family: true -# -# # --------------------------------------- -# # Zephyr -# # --------------------------------------- -# zephyr: -# if: github.event_name == 'push' -# runs-on: ubuntu-latest -# steps: -# - name: Checkout TinyUSB -# uses: actions/checkout@v4 -# -# - name: Setup Zephyr project -# uses: zephyrproject-rtos/action-zephyr-setup@v1 -# with: -# app-path: examples -# toolchains: arm-zephyr-eabi -# -# - name: Build -# run: | -# west build -b nrf52840dk -d examples/device/cdc_msc/build examples/device/cdc_msc -- -DRTOS=zephyr -# west build -b nrf52840dk -d examples/device/msc_dual_lun/build examples/device/msc_dual_lun -- -DRTOS=zephyr -# -# # --------------------------------------- -# # Hardware in the loop (HIL) -# # Run on PR only (hil-tinyusb), hil-hfp only run on non-forked PR -# # --------------------------------------- -# hil-build: -# if: | -# github.repository_owner == 'hathach' && -# (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') -# needs: set-matrix -# uses: ./.github/workflows/build_util.yml -# strategy: -# fail-fast: false -# matrix: -# toolchain: -# - 'arm-gcc' -# - 'esp-idf' -# with: -# build-system: 'cmake' -# toolchain: ${{ matrix.toolchain }} -# build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.hil_json)[matrix.toolchain]) }} -# one-per-family: true -# upload-artifacts: true -# -# # --------------------------------------- -# # Hardware in the loop (HIL) -# # self-hosted on local VM, for attached hardware checkout HIL_JSON -# # --------------------------------------- -# hil-tinyusb: -# if: | -# github.repository_owner == 'hathach' && -# (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') -# needs: hil-build -# runs-on: [self-hosted, X64, hathach, hardware-in-the-loop] -# steps: -# - name: Get Skip Boards from previous run -# if: github.run_attempt != '1' -# run: | -# if [ -f "${{ env.HIL_JSON }}.skip" ]; then -# SKIP_BOARDS=$(cat "${{ env.HIL_JSON }}.skip") -# else -# SKIP_BOARDS="" -# fi -# echo "SKIP_BOARDS=$SKIP_BOARDS" -# echo "SKIP_BOARDS=$SKIP_BOARDS" >> $GITHUB_ENV -# -# - name: Clean workspace -# run: | -# echo "Cleaning up for the first run" -# rm -rf "${{ github.workspace }}" -# mkdir -p "${{ github.workspace }}" -# -# - name: Checkout TinyUSB -# uses: actions/checkout@v4 -# -# - name: Download Artifacts -# uses: actions/download-artifact@v5 -# with: -# path: cmake-build -# merge-multiple: true -# -# - name: Test on actual hardware -# run: | -# python3 test/hil/hil_test.py ${{ env.HIL_JSON }} $SKIP_BOARDS -# -# # --------------------------------------- -# # Hardware in the loop (HIL) -# # self-hosted by HFP, build with IAR toolchain, for attached hardware checkout test/hil/hfp.json -# # Since IAR Token secret is not passed to forked PR, only build non-forked PR -# # --------------------------------------- -# hil-hfp: -# if: | -# github.repository_owner == 'hathach' && -# github.event.pull_request.head.repo.fork == false && -# (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') -# runs-on: [self-hosted, Linux, X64, hifiphile] -# env: -# IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} -# steps: -# - name: Clean workspace -# run: | -# echo "Cleaning up previous run" -# rm -rf "${{ github.workspace }}"3 -# mkdir -p "${{ github.workspace }}" -# -# - name: Toolchain version -# run: | -# iccarm --version -# -# - name: Checkout TinyUSB -# uses: actions/checkout@v4 -# -# - name: Get build boards -# run: | -# MATRIX_JSON=$(python test/hil/hil_ci_set_matrix.py test/hil/hfp.json) -# BUILD_ARGS=$(echo $MATRIX_JSON | jq -r '.["arm-gcc"] | join(" ")') -# echo "BUILD_ARGS=$BUILD_ARGS" -# echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV -# -# - name: Get Dependencies -# run: python3 tools/get_deps.py $BUILD_ARGS -# -# - name: Build -# run: python3 tools/build.py -j 4 --toolchain iar $BUILD_ARGS -# -# - name: Test on actual hardware (hardware in the loop) -# run: python3 test/hil/hil_test.py hfp.json + make: + if: github.event_name == 'push' + needs: set-matrix + uses: ./.github/workflows/build_util.yml + strategy: + fail-fast: false + matrix: + toolchain: + - 'aarch64-gcc' + #- 'arm-clang' + - 'arm-gcc' + - 'msp430-gcc' + - 'riscv-gcc' + - 'rx-gcc' + with: + build-system: 'make' + toolchain: ${{ matrix.toolchain }} + build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} + one-per-family: true + + # --------------------------------------- + # Build IAR + # Since IAR Token secret is not passed to forked PR, only build non-forked PR with make. + # cmake is built by circle-ci. Due to IAR limit capacity, only build oe per family + # --------------------------------------- + arm-iar: + if: false # disable for now since we got reach capacity limit too often + #if: github.event_name == 'push' && github.repository_owner == 'hathach' + needs: set-matrix + uses: ./.github/workflows/build_util.yml + secrets: inherit + strategy: + fail-fast: false + matrix: + build-system: + - 'make' + with: + build-system: ${{ matrix.build-system }} + toolchain: 'arm-iar' + build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)['arm-iar']) }} + one-per-family: true + + # --------------------------------------- + # Build Make/CMake on Windows/MacOS + # --------------------------------------- + build-os: + if: github.event_name == 'pull_request' + uses: ./.github/workflows/build_util.yml + strategy: + fail-fast: false + matrix: + os: [ windows-latest, macos-latest ] + build-system: [ 'make', 'cmake' ] + with: + os: ${{ matrix.os }} + build-system: ${{ matrix.build-system }} + toolchain: 'arm-gcc-${{ matrix.os }}' + build-args: '["stm32h7"]' + one-per-family: true + + # --------------------------------------- + # Zephyr + # --------------------------------------- + zephyr: + if: github.event_name == 'push' + runs-on: ubuntu-latest + steps: + - name: Checkout TinyUSB + uses: actions/checkout@v4 + + - name: Setup Zephyr project + uses: zephyrproject-rtos/action-zephyr-setup@v1 + with: + app-path: examples + toolchains: arm-zephyr-eabi + + - name: Build + run: | + west build -b nrf52840dk -d examples/device/cdc_msc/build examples/device/cdc_msc -- -DRTOS=zephyr + west build -b nrf52840dk -d examples/device/msc_dual_lun/build examples/device/msc_dual_lun -- -DRTOS=zephyr + + # --------------------------------------- + # Hardware in the loop (HIL) + # Run on PR only (hil-tinyusb), hil-hfp only run on non-forked PR + # --------------------------------------- + hil-build: + if: | + github.repository_owner == 'hathach' && + (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') + needs: set-matrix + uses: ./.github/workflows/build_util.yml + strategy: + fail-fast: false + matrix: + toolchain: + - 'arm-gcc' + - 'esp-idf' + with: + build-system: 'cmake' + toolchain: ${{ matrix.toolchain }} + build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.hil_json)[matrix.toolchain]) }} + one-per-family: true + upload-artifacts: true + + # --------------------------------------- + # Hardware in the loop (HIL) + # self-hosted on local VM, for attached hardware checkout HIL_JSON + # --------------------------------------- + hil-tinyusb: + if: | + github.repository_owner == 'hathach' && + (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') + needs: hil-build + runs-on: [ self-hosted, X64, hathach, hardware-in-the-loop ] + steps: + - name: Get Skip Boards from previous run + if: github.run_attempt != '1' + run: | + if [ -f "${{ env.HIL_JSON }}.skip" ]; then + SKIP_BOARDS=$(cat "${{ env.HIL_JSON }}.skip") + else + SKIP_BOARDS="" + fi + echo "SKIP_BOARDS=$SKIP_BOARDS" + echo "SKIP_BOARDS=$SKIP_BOARDS" >> $GITHUB_ENV + + - name: Clean workspace + run: | + echo "Cleaning up for the first run" + rm -rf "${{ github.workspace }}" + mkdir -p "${{ github.workspace }}" + + - name: Checkout TinyUSB + uses: actions/checkout@v4 + + - name: Download Artifacts + uses: actions/download-artifact@v5 + with: + path: cmake-build + merge-multiple: true + + - name: Test on actual hardware + run: | + python3 test/hil/hil_test.py ${{ env.HIL_JSON }} $SKIP_BOARDS + + # --------------------------------------- + # Hardware in the loop (HIL) + # self-hosted by HFP, build with IAR toolchain, for attached hardware checkout test/hil/hfp.json + # Since IAR Token secret is not passed to forked PR, only build non-forked PR + # --------------------------------------- + hil-hfp: + if: | + github.repository_owner == 'hathach' && + github.event.pull_request.head.repo.fork == false && + (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') + runs-on: [ self-hosted, Linux, X64, hifiphile ] + env: + IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} + steps: + - name: Clean workspace + run: | + echo "Cleaning up previous run" + rm -rf "${{ github.workspace }}"3 + mkdir -p "${{ github.workspace }}" + + - name: Toolchain version + run: | + iccarm --version + + - name: Checkout TinyUSB + uses: actions/checkout@v4 + + - name: Get build boards + run: | + MATRIX_JSON=$(python test/hil/hil_ci_set_matrix.py test/hil/hfp.json) + BUILD_ARGS=$(echo $MATRIX_JSON | jq -r '.["arm-gcc"] | join(" ")') + echo "BUILD_ARGS=$BUILD_ARGS" + echo "BUILD_ARGS=$BUILD_ARGS" >> $GITHUB_ENV + + - name: Get Dependencies + run: python3 tools/get_deps.py $BUILD_ARGS + + - name: Build + run: python3 tools/build.py -j 4 --toolchain iar $BUILD_ARGS + + - name: Test on actual hardware (hardware in the loop) + run: python3 test/hil/hil_test.py hfp.json diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index 2de0ed229..36043a1d5 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -75,7 +75,7 @@ jobs: - name: Upload Artifacts for Metrics if: ${{ inputs.upload-metrics }} - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v5 with: name: metrics-${{ matrix.arg }} path: cmake-build/cmake-build-*/metrics.json diff --git a/.github/workflows/ci_set_matrix.py b/.github/workflows/ci_set_matrix.py index 5032c83ae..933a8375f 100755 --- a/.github/workflows/ci_set_matrix.py +++ b/.github/workflows/ci_set_matrix.py @@ -20,8 +20,7 @@ family_list = { "ch32v10x ch32v20x ch32v30x fomu gd32vf103": ["riscv-gcc"], "imxrt": ["arm-gcc", "arm-clang"], "kinetis_k kinetis_kl kinetis_k32l2": ["arm-gcc", "arm-clang"], - "lpc11 lpc13 lpc15": ["arm-gcc", "arm-clang"], - "lpc17 lpc18 lpc40 lpc43": ["arm-gcc", "arm-clang"], + "lpc11 lpc13 lpc15 lpc17 lpc18 lpc40 lpc43": ["arm-gcc", "arm-clang"], "lpc51 lpc54 lpc55": ["arm-gcc", "arm-clang"], "maxim mcx mm32 msp432e4 tm4c": ["arm-gcc"], "msp430": ["msp430-gcc"], @@ -36,8 +35,7 @@ family_list = { "stm32f7": ["arm-gcc", "arm-clang", "arm-iar"], "stm32g0 stm32g4 stm32h5": ["arm-gcc", "arm-clang", "arm-iar"], "stm32h7": ["arm-gcc", "arm-clang", "arm-iar"], - "stm32h7rs": ["arm-gcc", "arm-clang", "arm-iar"], - "stm32l0 stm32l4": ["arm-gcc", "arm-clang", "arm-iar"], + "stm32h7rs stm32l0 stm32l4": ["arm-gcc", "arm-clang", "arm-iar"], "stm32n6": ["arm-gcc"], "stm32u0 stm32u5 stm32wb stm32wba": ["arm-gcc", "arm-clang", "arm-iar"], "-bespressif_s2_devkitc": ["esp-idf"], diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 694681467..b34131c2b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -20,9 +20,9 @@ endforeach () # Post-build: run metrics.py on all map.json files find_package(Python3 REQUIRED COMPONENTS Interpreter) -add_custom_target(tinyusb_examples_metrics +add_custom_target(tinyusb_metrics COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/metrics.py - -f tinyusb/src -j -o ${CMAKE_BINARY_DIR}/metrics + combine -f tinyusb/src -j -o ${CMAKE_BINARY_DIR}/metrics ${MAPJSON_PATTERNS} COMMENT "Generating average code size metrics" VERBATIM diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake index 3ede95e3f..15d9f1eae 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -351,8 +351,10 @@ function(family_configure_common TARGET RTOS) endif () endif () - # Generate linkermap target and post build. LINKERMAP_OPTION can be set with -D to change default options - family_add_linkermap(${TARGET}) + if (NOT RTOS STREQUAL zephyr) + # Generate linkermap target and post build. LINKERMAP_OPTION can be set with -D to change default options + family_add_linkermap(${TARGET}) + endif () # run size after build # find_program(SIZE_EXE ${CMAKE_SIZE}) diff --git a/tools/build.py b/tools/build.py index b87af6c6a..e4909f45f 100755 --- a/tools/build.py +++ b/tools/build.py @@ -122,7 +122,7 @@ def cmake_board(board, build_args, build_flags_on): rcmd = run_cmd(cmd) if rcmd.returncode == 0: ret[0] += 1 - run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_examples_metrics']) + run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_metrics']) # print(rcmd.stdout.decode("utf-8")) else: ret[1] += 1 diff --git a/tools/get_deps.py b/tools/get_deps.py index 9634451e2..99e406ce7 100755 --- a/tools/get_deps.py +++ b/tools/get_deps.py @@ -15,7 +15,7 @@ deps_mandatory = { '159e31b689577dbf69cf0683bbaffbd71fa5ee10', 'all'], 'tools/linkermap': ['https://github.com/hathach/linkermap.git', - '46c3c2947db366fb66af6723709febf80d860bc1', + '8a8206c39d0dfd7abfa615a676b3291165fcd65c', 'all'], 'tools/uf2': ['https://github.com/microsoft/uf2.git', 'c594542b2faa01cc33a2b97c9fbebc38549df80a', diff --git a/tools/metrics.py b/tools/metrics.py index c6cd49d57..7e54531f5 100644 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -3,6 +3,7 @@ import argparse import glob +import json import sys import os @@ -39,8 +40,6 @@ def combine_maps(map_files, filters=None): Returns: all_json_data: Dictionary with mapfiles list and data from each map file """ - import json - filters = filters or [] all_json_data = {"mapfiles": [], "data": []} @@ -128,24 +127,185 @@ def compute_avg(all_json_data): return json_average -def main(argv=None): - parser = argparse.ArgumentParser(description='Calculate average size from linker map files') - parser.add_argument('files', nargs='+', help='Path to map file(s) or glob pattern(s)') - parser.add_argument('-f', '--filter', dest='filters', action='append', default=[], - help='Only include object files whose path contains this substring (can be repeated)') - parser.add_argument('-o', '--out', dest='out', default='metrics', - help='Output path basename for JSON and Markdown files (default: metrics)') - parser.add_argument('-j', '--json', dest='json_out', action='store_true', - help='Write JSON output file') - parser.add_argument('-m', '--markdown', dest='markdown_out', action='store_true', - help='Write Markdown output file') - parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', - help='Suppress summary output') - args = parser.parse_args(argv) +def compare_maps(base_file, new_file, filters=None): + """Compare two map/json files and generate difference report. + + Args: + base_file: Path to base map/json file + new_file: Path to new map/json file + filters: List of path substrings to filter object files + + Returns: + Dictionary with comparison data + """ + filters = filters or [] + + # Load both files + base_data = combine_maps([base_file], filters) + new_data = combine_maps([new_file], filters) + + if not base_data["data"] or not new_data["data"]: + return None + + base_avg = compute_avg(base_data) + new_avg = compute_avg(new_data) + + if not base_avg or not new_avg: + return None + + # Collect all sections from both + all_sections = list(base_avg["sections"]) + for s in new_avg["sections"]: + if s not in all_sections: + all_sections.append(s) + + # Build file lookup + base_files = {f["file"]: f for f in base_avg["files"]} + new_files = {f["file"]: f for f in new_avg["files"]} + + # Get all file names + all_file_names = set(base_files.keys()) | set(new_files.keys()) + + # Build comparison data + comparison = [] + for fname in sorted(all_file_names): + base_f = base_files.get(fname) + new_f = new_files.get(fname) + + row = {"file": fname, "sections": {}, "total": {}} + + for section in all_sections: + base_val = base_f["sections"].get(section, 0) if base_f else 0 + new_val = new_f["sections"].get(section, 0) if new_f else 0 + row["sections"][section] = {"base": base_val, "new": new_val, "diff": new_val - base_val} + + base_total = base_f["total"] if base_f else 0 + new_total = new_f["total"] if new_f else 0 + row["total"] = {"base": base_total, "new": new_total, "diff": new_total - base_total} + + comparison.append(row) + + return { + "base_file": base_file, + "new_file": new_file, + "sections": all_sections, + "files": comparison + } - # Expand glob patterns - map_files = expand_files(args.files) +def format_diff(base, new, diff): + """Format a diff value with percentage.""" + if base == 0 and new == 0: + return "0" + if base == 0: + return f"{new} (new)" + if new == 0: + return f"{base} ➡ 0" + if diff == 0: + return f"{base} ➡ {new}" + pct = (diff / base) * 100 + sign = "+" if diff > 0 else "" + return f"{base} ➡ {new} ({sign}{diff}, {sign}{pct:.1f}%)" + + +def get_sort_key(sort_order): + """Get sort key function based on sort order. + + Args: + sort_order: One of 'size-', 'size+', 'name-', 'name+' + + Returns: + Tuple of (key_func, reverse) + """ + if sort_order == 'size-': + return lambda x: x.get('total', 0) if isinstance(x.get('total'), int) else x['total']['new'], True + elif sort_order == 'size+': + return lambda x: x.get('total', 0) if isinstance(x.get('total'), int) else x['total']['new'], False + elif sort_order == 'name-': + return lambda x: x.get('file', ''), True + else: # name+ + return lambda x: x.get('file', ''), False + + +def write_compare_markdown(comparison, path, sort_order='size'): + """Write comparison data to markdown file.""" + sections = comparison["sections"] + + md_lines = [ + "# TinyUSB Code Size Different Report", + "", + f"**Base:** `{comparison['base_file']}`", + f"**New:** `{comparison['new_file']}`", + "", + ] + + # Build header + header = "| File |" + separator = "|:-----|" + for s in sections: + header += f" {s} |" + separator += "-----:|" + header += " Total |" + separator += "------:|" + + md_lines.append(header) + md_lines.append(separator) + + # Sort files based on sort_order + if sort_order == 'size-': + key_func = lambda x: abs(x["total"]["diff"]) + reverse = True + elif sort_order in ('size', 'size+'): + key_func = lambda x: abs(x["total"]["diff"]) + reverse = False + elif sort_order == 'name-': + key_func = lambda x: x['file'] + reverse = True + else: # name or name+ + key_func = lambda x: x['file'] + reverse = False + sorted_files = sorted(comparison["files"], key=key_func, reverse=reverse) + + sum_base = {s: 0 for s in sections} + sum_base["total"] = 0 + sum_new = {s: 0 for s in sections} + sum_new["total"] = 0 + + for f in sorted_files: + # Skip files with no changes + if f["total"]["diff"] == 0 and all(f["sections"][s]["diff"] == 0 for s in sections): + continue + + row = f"| {f['file']} |" + for s in sections: + sd = f["sections"][s] + sum_base[s] += sd["base"] + sum_new[s] += sd["new"] + row += f" {format_diff(sd['base'], sd['new'], sd['diff'])} |" + + td = f["total"] + sum_base["total"] += td["base"] + sum_new["total"] += td["new"] + row += f" {format_diff(td['base'], td['new'], td['diff'])} |" + + md_lines.append(row) + + # Add sum row + sum_row = "| **SUM** |" + for s in sections: + diff = sum_new[s] - sum_base[s] + sum_row += f" {format_diff(sum_base[s], sum_new[s], diff)} |" + total_diff = sum_new["total"] - sum_base["total"] + sum_row += f" {format_diff(sum_base['total'], sum_new['total'], total_diff)} |" + md_lines.append(sum_row) + + with open(path, "w", encoding="utf-8") as f: + f.write("\n".join(md_lines)) + + +def cmd_combine(args): + """Handle combine subcommand.""" + map_files = expand_files(args.files) all_json_data = combine_maps(map_files, args.filters) json_average = compute_avg(all_json_data) @@ -154,11 +314,65 @@ def main(argv=None): sys.exit(1) if not args.quiet: - linkermap.print_summary(json_average, False) + linkermap.print_summary(json_average, False, args.sort) if args.json_out: linkermap.write_json(json_average, args.out + '.json') if args.markdown_out: - linkermap.write_markdown(json_average, args.out + '.md') + linkermap.write_markdown(json_average, args.out + '.md', sort_opt=args.sort, + title="TinyUSB Average Code Size Metrics") + + +def cmd_compare(args): + """Handle compare subcommand.""" + comparison = compare_maps(args.base, args.new, args.filters) + + if comparison is None: + print("Failed to compare files", file=sys.stderr) + sys.exit(1) + + write_compare_markdown(comparison, args.out + '.md', args.sort) + print(f"Comparison written to {args.out}.md") + + +def main(argv=None): + parser = argparse.ArgumentParser(description='Code size metrics tool') + subparsers = parser.add_subparsers(dest='command', required=True, help='Available commands') + + # Combine subcommand + combine_parser = subparsers.add_parser('combine', help='Combine and average multiple map files') + combine_parser.add_argument('files', nargs='+', help='Path to map file(s) or glob pattern(s)') + combine_parser.add_argument('-f', '--filter', dest='filters', action='append', default=[], + help='Only include object files whose path contains this substring (can be repeated)') + combine_parser.add_argument('-o', '--out', dest='out', default='metrics', + help='Output path basename for JSON and Markdown files (default: metrics)') + combine_parser.add_argument('-j', '--json', dest='json_out', action='store_true', + help='Write JSON output file') + combine_parser.add_argument('-m', '--markdown', dest='markdown_out', action='store_true', + help='Write Markdown output file') + combine_parser.add_argument('-q', '--quiet', dest='quiet', action='store_true', + help='Suppress summary output') + combine_parser.add_argument('-S', '--sort', dest='sort', default='name+', + choices=['size', 'size-', 'size+', 'name', 'name-', 'name+'], + help='Sort order: size/size- (descending), size+ (ascending), name/name+ (ascending), name- (descending). Default: name+') + + # Compare subcommand + compare_parser = subparsers.add_parser('compare', help='Compare two map files') + compare_parser.add_argument('base', help='Base map/json file') + compare_parser.add_argument('new', help='New map/json file') + compare_parser.add_argument('-f', '--filter', dest='filters', action='append', default=[], + help='Only include object files whose path contains this substring (can be repeated)') + compare_parser.add_argument('-o', '--out', dest='out', default='metrics_compare', + help='Output path basename for Markdown file (default: metrics_compare)') + compare_parser.add_argument('-S', '--sort', dest='sort', default='name+', + choices=['size', 'size-', 'size+', 'name', 'name-', 'name+'], + help='Sort order: size/size- (descending), size+ (ascending), name/name+ (ascending), name- (descending). Default: name+') + + args = parser.parse_args(argv) + + if args.command == 'combine': + cmd_combine(args) + elif args.command == 'compare': + cmd_compare(args) if __name__ == '__main__': -- cgit v1.3.1 From 1b6f2b90a45e172a2eb45b3dea2fa2d11382c2b0 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 3 Dec 2025 23:10:21 +0700 Subject: clean up --- .github/workflows/build.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to '.github/workflows') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b0b636c65..77f2d573f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -70,7 +70,7 @@ jobs: - 'aarch64-gcc' #- 'arm-clang' - 'arm-gcc' - # - 'esp-idf' + - 'esp-idf' - 'msp430-gcc' - 'riscv-gcc' with: @@ -279,6 +279,7 @@ jobs: - name: Download Artifacts uses: actions/download-artifact@v5 with: + pattern: binaries-* path: cmake-build merge-multiple: true -- cgit v1.3.1 From e7105b1fa3ccd8200fe7fb8b0759d00afc9b07c1 Mon Sep 17 00:00:00 2001 From: Ha Thach Date: Thu, 4 Dec 2025 21:34:10 +0700 Subject: fine tune ci to build more with circleci (#3386) * fine tune ci to build more with circleci * skip make for arm-iar, esp-idf * skip make + clang for circleci since llvm-objcopy got killed due to memory issue. --- .circleci/config.yml | 51 ++++++++++-------- .circleci/config2.yml | 15 +++++- .github/workflows/build.yml | 60 +++------------------- .github/workflows/build_util.yml | 3 ++ examples/build_system/make/toolchain/gcc_common.mk | 3 ++ hw/bsp/kinetis_k/family.mk | 6 ++- hw/bsp/kinetis_kl/family.mk | 6 ++- tools/build.py | 8 +-- tools/metrics.py | 19 +++---- 9 files changed, 74 insertions(+), 97 deletions(-) (limited to '.github/workflows') diff --git a/.circleci/config.yml b/.circleci/config.yml index 580f5fe2e..d04a33959 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -18,25 +18,34 @@ jobs: MATRIX_JSON=$(python .github/workflows/ci_set_matrix.py) echo "MATRIX_JSON=$MATRIX_JSON" - BUILDSYSTEM_TOOLCHAIN=( - "cmake aarch64-gcc" - "cmake arm-clang" - "cmake arm-gcc" - "cmake esp-idf" - "cmake msp430-gcc" - "cmake riscv-gcc" + BUILDSYSTEM_LIST=( + "cmake" + "make" + ) + + TOOLCHAIN_LIST=( + "aarch64-gcc" + "arm-clang" + "arm-gcc" + "esp-idf" + "msp430-gcc" + "riscv-gcc" ) # only build IAR if not forked PR, since IAR token is not shared if [ -z $CIRCLE_PR_USERNAME ]; then - BUILDSYSTEM_TOOLCHAIN+=("cmake arm-iar") + TOOLCHAIN_LIST+=("arm-iar") fi gen_build_entry() { local build_system="$1" local toolchain="$2" local family="$3" - local resource_class="$4" + local build_args="" + + if [[ "$toolchain" == "arm-iar" || "$build_system" == "make" ]]; then + build_args="--one-per-family" + fi if [[ "$toolchain" == "esp-idf" ]]; then echo " - build-vm:" >> .circleci/config2.yml @@ -49,17 +58,21 @@ jobs: echo " build-system: ['$build_system']" >> .circleci/config2.yml echo " toolchain: ['$toolchain']" >> .circleci/config2.yml echo " family: $family" >> .circleci/config2.yml - echo " resource_class: ['$resource_class']" >> .circleci/config2.yml + echo " resource_class: ['large']" >> .circleci/config2.yml + echo " build-args: ['$build_args']" >> .circleci/config2.yml } - for e in "${BUILDSYSTEM_TOOLCHAIN[@]}"; do - e_arr=($e) - build_system="${e_arr[0]}" - toolchain="${e_arr[1]}" - FAMILY=$(echo $MATRIX_JSON | jq -r ".\"$toolchain\"") - echo "FAMILY_${toolchain}=$FAMILY" + for build_system in "${BUILDSYSTEM_LIST[@]}"; do + for toolchain in "${TOOLCHAIN_LIST[@]}"; do + # make does not support these toolchains + if [ "$build_system" == "make" ] && { [ "$toolchain" == "arm-clang" ] || [ "$toolchain" == "arm-iar" ] || [ "$toolchain" == "esp-idf" ]; }; then + continue + fi - gen_build_entry "$build_system" "$toolchain" "$FAMILY" "large" + FAMILY=$(echo $MATRIX_JSON | jq -r ".\"$toolchain\"") + echo "FAMILY_${toolchain}=$FAMILY" + gen_build_entry "$build_system" "$toolchain" "$FAMILY" + done done - continuation/continue: @@ -67,9 +80,5 @@ jobs: workflows: set-matrix: - # Only build PR here, Push will be built by github action. - when: - and: - - not: << pipeline.git.branch.is_default >> jobs: - set-matrix diff --git a/.circleci/config2.yml b/.circleci/config2.yml index 869597289..77bc4f790 100644 --- a/.circleci/config2.yml +++ b/.circleci/config2.yml @@ -66,6 +66,9 @@ commands: type: string family: type: string + build-args: + type: string + default: "" steps: - checkout @@ -107,7 +110,7 @@ commands: no_output_timeout: 20m command: | if [ << parameters.toolchain >> == esp-idf ]; then - docker run --rm -v $PWD:/project -w /project espressif/idf:v5.3.2 python tools/build.py << parameters.family >> + docker run --rm -v $PWD:/project -w /project espressif/idf:v5.3.2 python tools/build.py << parameters.build-args >> << parameters.family >> else # Toolchain option default is gcc if [ << parameters.toolchain >> == arm-clang ]; then @@ -121,7 +124,7 @@ commands: # circleci docker return $nproc as 36 core, limit parallel to 4 (resource-class = large) # Required for IAR, also prevent crashed/killed by docker - python tools/build.py -s << parameters.build-system >> $TOOLCHAIN_OPTION -j 4 << parameters.family >> + python tools/build.py -s << parameters.build-system >> $TOOLCHAIN_OPTION -j 4 << parameters.build-args >> << parameters.family >> fi jobs: @@ -137,6 +140,9 @@ jobs: type: string family: type: string + build-args: + type: string + default: "" docker: - image: cimg/base:current @@ -147,6 +153,7 @@ jobs: build-system: << parameters.build-system >> toolchain: << parameters.toolchain >> family: << parameters.family >> + build-args: << parameters.build-args >> # Build using VM build-vm: @@ -160,6 +167,9 @@ jobs: type: string family: type: string + build-args: + type: string + default: "" machine: image: ubuntu-2404:current @@ -170,6 +180,7 @@ jobs: build-system: << parameters.build-system >> toolchain: << parameters.toolchain >> family: << parameters.family >> + build-args: << parameters.build-args >> workflows: build: diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 77f2d573f..a1bacbc27 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -56,10 +56,11 @@ jobs: echo "hil_matrix=$HIL_MATRIX_JSON" echo "hil_matrix=$HIL_MATRIX_JSON" >> $GITHUB_OUTPUT - # --------------------------------------- - # Build CMake: only one-per-family. - # Full built is done by CircleCI in PR - # --------------------------------------- + # ------------------------------------------------------------------------------ + # CMake build: only one-per-family. Full built is done by CircleCI in PR + # Note: + # For Make and IAR build: will be done on CircleCI only (one-per-family too) + # ------------------------------------------------------------------------------ cmake: needs: set-matrix uses: ./.github/workflows/build_util.yml @@ -70,7 +71,7 @@ jobs: - 'aarch64-gcc' #- 'arm-clang' - 'arm-gcc' - - 'esp-idf' + #- 'esp-idf' - 'msp430-gcc' - 'riscv-gcc' with: @@ -137,52 +138,6 @@ jobs: header: code-metrics path: metrics_compare.md - - # --------------------------------------- - # Build Make: only build on push with one-per-family - # --------------------------------------- - make: - if: github.event_name == 'push' - needs: set-matrix - uses: ./.github/workflows/build_util.yml - strategy: - fail-fast: false - matrix: - toolchain: - - 'aarch64-gcc' - #- 'arm-clang' - - 'arm-gcc' - - 'msp430-gcc' - - 'riscv-gcc' - - 'rx-gcc' - with: - build-system: 'make' - toolchain: ${{ matrix.toolchain }} - build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} - one-per-family: true - - # --------------------------------------- - # Build IAR - # Since IAR Token secret is not passed to forked PR, only build non-forked PR with make. - # cmake is built by circle-ci. Due to IAR limit capacity, only build oe per family - # --------------------------------------- - arm-iar: - if: false # disable for now since we got reach capacity limit too often - #if: github.event_name == 'push' && github.repository_owner == 'hathach' - needs: set-matrix - uses: ./.github/workflows/build_util.yml - secrets: inherit - strategy: - fail-fast: false - matrix: - build-system: - - 'make' - with: - build-system: ${{ matrix.build-system }} - toolchain: 'arm-iar' - build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)['arm-iar']) }} - one-per-family: true - # --------------------------------------- # Build Make/CMake on Windows/MacOS # --------------------------------------- @@ -193,10 +148,9 @@ jobs: fail-fast: false matrix: os: [ windows-latest, macos-latest ] - build-system: [ 'make', 'cmake' ] with: os: ${{ matrix.os }} - build-system: ${{ matrix.build-system }} + build-system: 'cmake-make' toolchain: 'arm-gcc-${{ matrix.os }}' build-args: '["stm32h7"]' one-per-family: true diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index 36043a1d5..2fc0eead0 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -68,6 +68,9 @@ jobs: run: | if [ "$TOOLCHAIN" == "esp-idf" ]; then docker run --rm -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py ${{ matrix.arg }} + elif [ "${{ inputs.build-system }}" == "cmake-make" ] || [ "${{ inputs.build-system }}" == "make-cmake" ]; then + python tools/build.py -s make ${{ steps.setup-toolchain.outputs.build_option }} ${{ steps.set-one-per-family.outputs.build_option }} ${{ matrix.arg }} + python tools/build.py -s cmake ${{ steps.setup-toolchain.outputs.build_option }} ${{ steps.set-one-per-family.outputs.build_option }} ${{ matrix.arg }} else python tools/build.py -s ${{ inputs.build-system }} ${{ steps.setup-toolchain.outputs.build_option }} ${{ steps.set-one-per-family.outputs.build_option }} ${{ matrix.arg }} fi diff --git a/examples/build_system/make/toolchain/gcc_common.mk b/examples/build_system/make/toolchain/gcc_common.mk index 0cbb6774d..42fd01183 100644 --- a/examples/build_system/make/toolchain/gcc_common.mk +++ b/examples/build_system/make/toolchain/gcc_common.mk @@ -31,6 +31,9 @@ CFLAGS += \ -Wreturn-type \ -Wredundant-decls \ +CFLAGS_CLANG += \ + -Wno-error=unknown-warning-option + # -Wmissing-prototypes \ # conversion is too strict for most mcu driver, may be disable sign/int/arith-conversion # -Wconversion diff --git a/hw/bsp/kinetis_k/family.mk b/hw/bsp/kinetis_k/family.mk index e95cdb717..7a51a77d8 100644 --- a/hw/bsp/kinetis_k/family.mk +++ b/hw/bsp/kinetis_k/family.mk @@ -9,11 +9,13 @@ CFLAGS += \ -DCFG_TUSB_MCU=OPT_MCU_KINETIS_K \ LDFLAGS += \ - -nostartfiles \ - --specs=nosys.specs --specs=nano.specs \ -Wl,--defsym,__stack_size__=0x400 \ -Wl,--defsym,__heap_size__=0 +LDFLAGS_GCC += \ + -nostartfiles \ + --specs=nosys.specs --specs=nano.specs \ + SRC_C += \ src/portable/nxp/khci/dcd_khci.c \ src/portable/nxp/khci/hcd_khci.c \ diff --git a/hw/bsp/kinetis_kl/family.mk b/hw/bsp/kinetis_kl/family.mk index 8d113aecf..aec53d486 100644 --- a/hw/bsp/kinetis_kl/family.mk +++ b/hw/bsp/kinetis_kl/family.mk @@ -9,11 +9,13 @@ CFLAGS += \ -DCFG_TUSB_MCU=OPT_MCU_KINETIS_KL \ LDFLAGS += \ - -nostartfiles \ - -specs=nosys.specs -specs=nano.specs \ -Wl,--defsym,__stack_size__=0x400 \ -Wl,--defsym,__heap_size__=0 +LDFLAGS_GCC += \ + -nostartfiles \ + -specs=nosys.specs -specs=nano.specs \ + SRC_C += \ src/portable/nxp/khci/dcd_khci.c \ src/portable/nxp/khci/hcd_khci.c \ diff --git a/tools/build.py b/tools/build.py index e4909f45f..c4f1558c0 100755 --- a/tools/build.py +++ b/tools/build.py @@ -142,16 +142,12 @@ def make_one_example(example, board, make_option): r = 2 else: start_time = time.monotonic() - # skip -j for circleci - if not os.getenv('CIRCLECI'): - make_option += ' -j' - make_args = ["make", "-C", f"examples/{example}", f"BOARD={board}"] + make_args = ["make", "-C", f"examples/{example}", f"BOARD={board}", '-j', str(parallel_jobs)] if make_option: make_args += shlex.split(make_option) - make_args.append("all") if clean_build: run_cmd(make_args + ["clean"]) - build_result = run_cmd(make_args) + build_result = run_cmd(make_args + ['all']) r = 0 if build_result.returncode == 0 else 1 print_build_result(board, example, r, time.monotonic() - start_time) diff --git a/tools/metrics.py b/tools/metrics.py index bb84f803e..c3b366e42 100644 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -195,17 +195,13 @@ def compare_maps(base_file, new_file, filters=None): def format_diff(base, new, diff): """Format a diff value with percentage.""" - if base == 0 and new == 0: - return "0" - if base == 0: - return f"{new} (new)" - if new == 0: - return f"{base} ➡ 0" if diff == 0: - return f"{base} ➡ {new}" + return f"{new}" + if base == 0 or new == 0: + return f"{base} ➙ {new}" pct = (diff / base) * 100 sign = "+" if diff > 0 else "" - return f"{base} ➡ {new} ({sign}{diff}, {sign}{pct:.1f}%)" + return f"{base} ➙ {new} ({sign}{diff}, {sign}{pct:.1f}%)" def get_sort_key(sort_order): @@ -232,10 +228,11 @@ def write_compare_markdown(comparison, path, sort_order='size'): sections = comparison["sections"] md_lines = [ - "# TinyUSB Code Size Different Report", + "# Size Difference Report", "", - f"**Base:** `{comparison['base_file']}`", - f"**New:** `{comparison['new_file']}`", + "Because TinyUSB code size varies by port and configuration, the metrics below represent the averaged totals across all example builds." + "", + "Note: If there is no change, only one value is shown.", "", ] -- cgit v1.3.1 From 93b53158f02bce9497419298ac27150eebe567d3 Mon Sep 17 00:00:00 2001 From: Ha Thach Date: Fri, 5 Dec 2025 10:21:28 +0700 Subject: Run CI build with fixed set of boards (#3389) * run cmake ci build on github with a fixed set of board to keep the size stable * Size Difference Report contain major >1% and minor <1& table --- .circleci/config.yml | 2 +- .github/workflows/build.yml | 9 ++--- .github/workflows/build_util.yml | 22 +++------- hw/bsp/rp2040/skip_ci.txt | 7 ---- tools/build.py | 62 ++++++++++++++++++++--------- tools/metrics.py | 86 ++++++++++++++++++++++++++-------------- 6 files changed, 112 insertions(+), 76 deletions(-) delete mode 100644 hw/bsp/rp2040/skip_ci.txt (limited to '.github/workflows') diff --git a/.circleci/config.yml b/.circleci/config.yml index d04a33959..42b790c83 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -44,7 +44,7 @@ jobs: local build_args="" if [[ "$toolchain" == "arm-iar" || "$build_system" == "make" ]]; then - build_args="--one-per-family" + build_args="--one-random" fi if [[ "$toolchain" == "esp-idf" ]]; then diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a1bacbc27..bc2fdac77 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,9 +57,9 @@ jobs: echo "hil_matrix=$HIL_MATRIX_JSON" >> $GITHUB_OUTPUT # ------------------------------------------------------------------------------ - # CMake build: only one-per-family. Full built is done by CircleCI in PR + # CMake build: only one board per family (first alphabetically). Full build is done by CircleCI in PR # Note: - # For Make and IAR build: will be done on CircleCI only (one-per-family too) + # For Make and IAR build: will be done on CircleCI only (one random per family as well) # ------------------------------------------------------------------------------ cmake: needs: set-matrix @@ -78,7 +78,7 @@ jobs: build-system: 'cmake' toolchain: ${{ matrix.toolchain }} build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} - one-per-family: true + build-options: '--one-first' upload-metrics: true code-metrics: @@ -153,7 +153,7 @@ jobs: build-system: 'cmake-make' toolchain: 'arm-gcc-${{ matrix.os }}' build-args: '["stm32h7"]' - one-per-family: true + build-options: '--one-random' # --------------------------------------- # Zephyr @@ -196,7 +196,6 @@ jobs: build-system: 'cmake' toolchain: ${{ matrix.toolchain }} build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.hil_json)[matrix.toolchain]) }} - one-per-family: true upload-artifacts: true # --------------------------------------- diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index 2fc0eead0..1cbd02f1b 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -12,10 +12,10 @@ on: build-args: required: true type: string - one-per-family: + build-options: required: false - default: false - type: boolean + default: '' + type: string upload-artifacts: required: false default: false @@ -51,16 +51,6 @@ jobs: with: arg: ${{ matrix.arg }} - - name: Set build one-per-family option - id: set-one-per-family - run: | - if [[ "${{ inputs.one-per-family }}" == "true" ]]; then - BUILD_OPTION="--one-per-family" - fi - echo "build_option=$BUILD_OPTION" - echo "build_option=$BUILD_OPTION" >> $GITHUB_OUTPUT - shell: bash - - name: Build env: IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} @@ -69,10 +59,10 @@ jobs: if [ "$TOOLCHAIN" == "esp-idf" ]; then docker run --rm -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py ${{ matrix.arg }} elif [ "${{ inputs.build-system }}" == "cmake-make" ] || [ "${{ inputs.build-system }}" == "make-cmake" ]; then - python tools/build.py -s make ${{ steps.setup-toolchain.outputs.build_option }} ${{ steps.set-one-per-family.outputs.build_option }} ${{ matrix.arg }} - python tools/build.py -s cmake ${{ steps.setup-toolchain.outputs.build_option }} ${{ steps.set-one-per-family.outputs.build_option }} ${{ matrix.arg }} + python tools/build.py -s make ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} + python tools/build.py -s cmake ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} else - python tools/build.py -s ${{ inputs.build-system }} ${{ steps.setup-toolchain.outputs.build_option }} ${{ steps.set-one-per-family.outputs.build_option }} ${{ matrix.arg }} + python tools/build.py -s ${{ inputs.build-system }} ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} fi shell: bash diff --git a/hw/bsp/rp2040/skip_ci.txt b/hw/bsp/rp2040/skip_ci.txt deleted file mode 100644 index fe99c9f65..000000000 --- a/hw/bsp/rp2040/skip_ci.txt +++ /dev/null @@ -1,7 +0,0 @@ -# boards in this files are skipped when running CI with this family -adafruit_feather_rp2040_usb_host -adafruit_fruit_jam -adafruit_metro_rp2350 -feather_rp2040_max3421 -pico_sdk -raspberry_pi_pico_w diff --git a/tools/build.py b/tools/build.py index c4f1558c0..87064b7a0 100755 --- a/tools/build.py +++ b/tools/build.py @@ -27,6 +27,23 @@ verbose = False clean_build = False parallel_jobs = os.cpu_count() +# CI board control lists (used when running under CI) +ci_skip_boards = { + 'rp2040': [ + 'adafruit_feather_rp2040_usb_host', + 'adafruit_fruit_jam', + 'adafruit_metro_rp2350', + 'feather_rp2040_max3421', + 'pico_sdk', + 'raspberry_pi_pico_w', + ], +} + +ci_preferred_boards = { + 'stm32h7': ['stm32h743eval'], +} + + # ----------------------------- # Helper # ----------------------------- @@ -195,35 +212,40 @@ def build_boards_list(boards, build_defines, build_system, build_flags_on): return ret -def get_family_boards(family, one_per_family, boards): +def get_family_boards(family, one_random, one_first): """Get list of boards for a family. Args: family: Family name - one_per_family: If True, return only one random board - boards: List of boards already specified via -b flag + one_random: If True, return only one random board + one_first: If True, return only the first board (alphabetical) Returns: List of board names """ - skip_ci = [] + skip_list = [] + preferred_list = [] if os.getenv('GITHUB_ACTIONS') or os.getenv('CIRCLECI'): - skip_ci_file = Path(f"hw/bsp/{family}/skip_ci.txt") - if skip_ci_file.exists(): - skip_ci = skip_ci_file.read_text().split() + skip_list = ci_skip_boards.get(family, []) + preferred_list = ci_preferred_boards.get(family, []) + all_boards = [] for entry in os.scandir(f"hw/bsp/{family}/boards"): - if entry.is_dir() and not entry.name in skip_ci: + if entry.is_dir() and entry.name not in skip_list: all_boards.append(entry.name) + if not all_boards: + print(f"No boards found for family '{family}'") + return [] all_boards.sort() - # If only-one flag is set, select one random board - if one_per_family: - for b in boards: - # skip if -b already specify one in this family - if find_family(b) == family: - return [] - all_boards = [random.choice(all_boards)] + # If only-one flags are set, honor select list first, then pick first or random + if one_first or one_random: + if preferred_list: + return [preferred_list[0]] + if one_first: + return [all_boards[0]] + if one_random: + return [random.choice(all_boards)] return all_boards @@ -244,7 +266,10 @@ def main(): parser.add_argument('-s', '--build-system', default='cmake', help='Build system to use, default is cmake') parser.add_argument('-D', '--define-symbol', action='append', default=[], help='Define to pass to build system') parser.add_argument('-f1', '--build-flags-on', action='append', default=[], help='Build flag to pass to build system') - parser.add_argument('-1', '--one-per-family', action='store_true', default=False, help='Build only one random board inside a family') + parser.add_argument('--one-random', action='store_true', default=False, + help='Build only one random board of each specified family') + parser.add_argument('--one-first', action='store_true', default=False, + help='Build only the first board (alphabetical) of each specified family') parser.add_argument('-j', '--jobs', type=int, default=os.cpu_count(), help='Number of jobs to run in parallel') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') args = parser.parse_args() @@ -255,7 +280,8 @@ def main(): build_system = args.build_system build_defines = args.define_symbol build_flags_on = args.build_flags_on - one_per_family = args.one_per_family + one_random = args.one_random + one_first = args.one_first verbose = args.verbose clean_build = args.clean parallel_jobs = args.jobs @@ -283,7 +309,7 @@ def main(): # get boards from families and append to boards list all_boards = list(boards) for f in all_families: - all_boards.extend(get_family_boards(f, one_per_family, boards)) + all_boards.extend(get_family_boards(f, one_random, one_first)) # build all boards result = build_boards_list(all_boards, build_defines, build_system, build_flags_on) diff --git a/tools/metrics.py b/tools/metrics.py index c3b366e42..bdc64fccc 100644 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -245,8 +245,18 @@ def write_compare_markdown(comparison, path, sort_order='size'): header += " Total |" separator += "------:|" - md_lines.append(header) - md_lines.append(separator) + def is_significant(file_row): + for s in sections: + sd = file_row["sections"][s] + diff = abs(sd["diff"]) + base = sd["base"] + if base == 0: + if diff != 0: + return True + else: + if (diff / base) * 100 > 1.0: + return True + return False # Sort files based on sort_order if sort_order == 'size-': @@ -263,38 +273,56 @@ def write_compare_markdown(comparison, path, sort_order='size'): reverse = False sorted_files = sorted(comparison["files"], key=key_func, reverse=reverse) - sum_base = {s: 0 for s in sections} - sum_base["total"] = 0 - sum_new = {s: 0 for s in sections} - sum_new["total"] = 0 - + significant = [] + minor = [] for f in sorted_files: # Skip files with no changes if f["total"]["diff"] == 0 and all(f["sections"][s]["diff"] == 0 for s in sections): continue - - row = f"| {f['file']} |" + (significant if is_significant(f) else minor).append(f) + + def render_table(title, rows): + md_lines.append(f"## {title}") + if not rows: + md_lines.append("No entries.") + md_lines.append("") + return + + md_lines.append(header) + md_lines.append(separator) + + sum_base = {s: 0 for s in sections} + sum_base["total"] = 0 + sum_new = {s: 0 for s in sections} + sum_new["total"] = 0 + + for f in rows: + row = f"| {f['file']} |" + for s in sections: + sd = f["sections"][s] + sum_base[s] += sd["base"] + sum_new[s] += sd["new"] + row += f" {format_diff(sd['base'], sd['new'], sd['diff'])} |" + + td = f["total"] + sum_base["total"] += td["base"] + sum_new["total"] += td["new"] + row += f" {format_diff(td['base'], td['new'], td['diff'])} |" + + md_lines.append(row) + + # Add sum row + sum_row = "| **SUM** |" for s in sections: - sd = f["sections"][s] - sum_base[s] += sd["base"] - sum_new[s] += sd["new"] - row += f" {format_diff(sd['base'], sd['new'], sd['diff'])} |" - - td = f["total"] - sum_base["total"] += td["base"] - sum_new["total"] += td["new"] - row += f" {format_diff(td['base'], td['new'], td['diff'])} |" - - md_lines.append(row) - - # Add sum row - sum_row = "| **SUM** |" - for s in sections: - diff = sum_new[s] - sum_base[s] - sum_row += f" {format_diff(sum_base[s], sum_new[s], diff)} |" - total_diff = sum_new["total"] - sum_base["total"] - sum_row += f" {format_diff(sum_base['total'], sum_new['total'], total_diff)} |" - md_lines.append(sum_row) + diff = sum_new[s] - sum_base[s] + sum_row += f" {format_diff(sum_base[s], sum_new[s], diff)} |" + total_diff = sum_new["total"] - sum_base["total"] + sum_row += f" {format_diff(sum_base['total'], sum_new['total'], total_diff)} |" + md_lines.append(sum_row) + md_lines.append("") + + render_table("Changes >1% in any section", significant) + render_table("Changes <1% in all sections", minor) with open(path, "w", encoding="utf-8") as f: f.write("\n".join(md_lines)) -- cgit v1.3.1 From e73dfde96dcf59e6357d833adbe49c1cf716a1b3 Mon Sep 17 00:00:00 2001 From: Ha Thach Date: Fri, 5 Dec 2025 20:12:45 +0700 Subject: also render unchange table (#3390) * also render unchange table * run metrics with circleci (full all boards build) --- .circleci/config.yml | 19 +++++++- .circleci/config2.yml | 104 +++++++++++++++++++++++++++++++++++++++++--- .github/workflows/build.yml | 12 +++-- tools/metrics.py | 11 +++-- 4 files changed, 126 insertions(+), 20 deletions(-) (limited to '.github/workflows') diff --git a/.circleci/config.yml b/.circleci/config.yml index 42b790c83..c084fc226 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -9,7 +9,7 @@ jobs: executor: continuation/default docker: - image: cimg/base:current - resource_class: small + resource_class: large steps: - checkout - run: @@ -52,8 +52,8 @@ jobs: else echo " - build:" >> .circleci/config2.yml fi - echo " matrix:" >> .circleci/config2.yml + echo " alias: build-${build_system}-${toolchain}" >> .circleci/config2.yml echo " parameters:" >> .circleci/config2.yml echo " build-system: ['$build_system']" >> .circleci/config2.yml echo " toolchain: ['$toolchain']" >> .circleci/config2.yml @@ -62,6 +62,9 @@ jobs: echo " build-args: ['$build_args']" >> .circleci/config2.yml } + # Collect all build aliases for code-metrics requires (cmake only, exclude esp-idf) + BUILD_ALIASES=() + for build_system in "${BUILDSYSTEM_LIST[@]}"; do for toolchain in "${TOOLCHAIN_LIST[@]}"; do # make does not support these toolchains @@ -72,9 +75,21 @@ jobs: FAMILY=$(echo $MATRIX_JSON | jq -r ".\"$toolchain\"") echo "FAMILY_${toolchain}=$FAMILY" gen_build_entry "$build_system" "$toolchain" "$FAMILY" + + # Only add cmake builds: excluding esp-idf or build_args="--one-random" to metrics requirements + if [ "$build_system" == "cmake" ] && [ "$toolchain" != "esp-idf" ] && [ "$toolchain" != "arm-iar" ]; then + BUILD_ALIASES+=("build-${build_system}-${toolchain}") + fi done done + # Add code-metrics job that requires all build jobs + echo " - code-metrics:" >> .circleci/config2.yml + echo " requires:" >> .circleci/config2.yml + for alias in "${BUILD_ALIASES[@]}"; do + echo " - $alias" >> .circleci/config2.yml + done + - continuation/continue: configuration_path: .circleci/config2.yml diff --git a/.circleci/config2.yml b/.circleci/config2.yml index 77bc4f790..a39682067 100644 --- a/.circleci/config2.yml +++ b/.circleci/config2.yml @@ -127,6 +127,35 @@ commands: python tools/build.py -s << parameters.build-system >> $TOOLCHAIN_OPTION -j 4 << parameters.build-args >> << parameters.family >> fi + # Only collect and persist metrics for cmake builds (excluding esp-idf and --one-random) + - when: + condition: + and: + - equal: [ cmake, << parameters.build-system >> ] + - not: + equal: [ esp-idf, << parameters.toolchain >> ] + - not: + equal: [ arm-iar, << parameters.toolchain >> ] + steps: + - run: + name: Collect Metrics + command: | + # Create unique directory per toolchain to avoid workspace conflicts + METRICS_DIR="/tmp/metrics/<< parameters.toolchain >>" + mkdir -p "${METRICS_DIR}" + # Copy all metrics.json files + for f in cmake-build/cmake-build-*/metrics.json; do + if [ -f "$f" ]; then + BOARD_DIR=$(dirname "$f" | xargs basename) + cp "$f" "${METRICS_DIR}/${BOARD_DIR}.json" + fi + done + + - persist_to_workspace: + root: /tmp + paths: + - metrics/<< parameters.toolchain >> + jobs: # Build using docker build: @@ -146,6 +175,7 @@ jobs: docker: - image: cimg/base:current + working_directory: ~/project/tinyusb resource_class: << parameters.resource_class >> steps: @@ -173,6 +203,7 @@ jobs: machine: image: ubuntu-2404:current + working_directory: ~/project/tinyusb resource_class: << parameters.resource_class >> steps: @@ -182,20 +213,79 @@ jobs: family: << parameters.family >> build-args: << parameters.build-args >> + # Aggregate code metrics from all builds + code-metrics: + docker: + - image: cimg/python:3.12 + resource_class: large + steps: + - checkout + - attach_workspace: + at: /tmp + + - run: + name: Aggregate Code Metrics + command: | + python tools/get_deps.py + pip install tools/linkermap/ + # Combine all metrics files from all toolchain subdirectories + ls -R /tmp/metrics + if ls /tmp/metrics/*/*.json 1> /dev/null 2>&1; then + python tools/metrics.py combine -j -m -f tinyusb/src /tmp/metrics/*/*.json + else + echo "No metrics files found" + exit 1 + fi + + - store_artifacts: + path: metrics.json + destination: metrics.json + + # Compare with base master metrics on PR branches + - when: + condition: + not: + equal: [ master, << pipeline.git.branch >> ] + steps: + - run: + name: Download Base Branch Metrics + command: | + # Download metrics.json artifact from the latest successful build on master branch + mkdir -p base-metrics + # Use CircleCI API to get the latest artifact + curl -s -L "https://dl.circleci.com/api/v2/project/gh/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/latest/artifacts?branch=master&filter=successful" \ + -H "Circle-Token: ${CIRCLE_TOKEN:-}" | \ + jq -r '.items[] | select(.path == "metrics.json") | .url' | \ + head -1 | xargs -I {} curl -s -L -o base-metrics/metrics.json {} || true + + - run: + name: Compare with Base Branch + command: | + if [ -f base-metrics/metrics.json ]; then + python tools/metrics.py compare -f tinyusb/src base-metrics/metrics.json metrics.json + cat metrics_compare.md + else + echo "No base metrics found, skipping comparison" + cp metrics.md metrics_compare.md + fi + + - store_artifacts: + path: metrics_compare.md + destination: metrics_compare.md + workflows: build: jobs: +# The jobs below are populated dynamically by config.yml set-matrix job +# Example entries that will be generated: # - build: # matrix: +# alias: build-cmake-arm-gcc # parameters: # toolchain: [ 'arm-gcc' ] # build-system: [ 'cmake' ] # family: [ 'nrf' ] # resource_class: ['large'] -# - build-vm: -# matrix: -# parameters: -# toolchain: ['esp-idf'] -# build-system: ['cmake'] -# family: ['-bespressif_kaluga_1'] -# resource_class: ['large'] +# - code-metrics: +# requires: +# - build-cmake-arm-gcc diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bc2fdac77..5017cb3cd 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -111,7 +111,7 @@ jobs: path: metrics.json - name: Download Base Branch Metrics - if: github.event_name == 'pull_request' + if: github.event_name != 'push' uses: dawidd6/action-download-artifact@v11 with: workflow: build.yml @@ -121,7 +121,7 @@ jobs: continue-on-error: true - name: Compare with Base Branch - if: github.event_name == 'pull_request' + if: github.event_name != 'push' run: | if [ -f base-metrics/metrics.json ]; then python tools/metrics.py compare -f tinyusb/src base-metrics/metrics.json metrics.json @@ -132,7 +132,7 @@ jobs: fi - name: Post Code Metrics as PR Comment - if: github.event_name == 'pull_request' + if: github.event_name != 'push' uses: marocchino/sticky-pull-request-comment@v2 with: header: code-metrics @@ -203,9 +203,7 @@ jobs: # self-hosted on local VM, for attached hardware checkout HIL_JSON # --------------------------------------- hil-tinyusb: - if: | - github.repository_owner == 'hathach' && - (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') + if: github.repository_owner == 'hathach' && github.event_name != 'push' needs: hil-build runs-on: [ self-hosted, X64, hathach, hardware-in-the-loop ] steps: @@ -249,7 +247,7 @@ jobs: if: | github.repository_owner == 'hathach' && github.event.pull_request.head.repo.fork == false && - (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') + github.event_name != 'push' runs-on: [ self-hosted, Linux, X64, hifiphile ] env: IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} diff --git a/tools/metrics.py b/tools/metrics.py index bdc64fccc..2794c7a2a 100644 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -275,11 +275,13 @@ def write_compare_markdown(comparison, path, sort_order='size'): significant = [] minor = [] + unchanged = [] for f in sorted_files: - # Skip files with no changes - if f["total"]["diff"] == 0 and all(f["sections"][s]["diff"] == 0 for s in sections): - continue - (significant if is_significant(f) else minor).append(f) + no_change = f["total"]["diff"] == 0 and all(f["sections"][s]["diff"] == 0 for s in sections) + if no_change: + unchanged.append(f) + else: + (significant if is_significant(f) else minor).append(f) def render_table(title, rows): md_lines.append(f"## {title}") @@ -323,6 +325,7 @@ def write_compare_markdown(comparison, path, sort_order='size'): render_table("Changes >1% in any section", significant) render_table("Changes <1% in all sections", minor) + render_table("No changes", unchanged) with open(path, "w", encoding="utf-8") as f: f.write("\n".join(md_lines)) -- cgit v1.3.1 From 919ee4b1527469e327710cff936366328f97294a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 9 Dec 2025 20:11:18 +0700 Subject: update metrics to support bloaty csv --- .circleci/config2.yml | 1 - .github/workflows/build.yml | 3 +- .../build_system/cmake/toolchain/arm_clang.cmake | 1 - examples/build_system/cmake/toolchain/common.cmake | 41 +++--- hw/bsp/family_support.cmake | 33 ++--- src/common/tusb_compiler.h | 6 +- src/portable/synopsys/dwc2/hcd_dwc2.c | 2 +- tools/get_deps.py | 2 +- tools/metrics.py | 152 ++++++++++++--------- 9 files changed, 124 insertions(+), 117 deletions(-) (limited to '.github/workflows') diff --git a/.circleci/config2.yml b/.circleci/config2.yml index a39682067..352d0f4fa 100644 --- a/.circleci/config2.yml +++ b/.circleci/config2.yml @@ -227,7 +227,6 @@ jobs: name: Aggregate Code Metrics command: | python tools/get_deps.py - pip install tools/linkermap/ # Combine all metrics files from all toolchain subdirectories ls -R /tmp/metrics if ls /tmp/metrics/*/*.json 1> /dev/null 2>&1; then diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5017cb3cd..9d94a3b9b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -100,7 +100,6 @@ jobs: - name: Aggregate Code Metrics run: | python tools/get_deps.py - pip install tools/linkermap/ python tools/metrics.py combine -j -m -f tinyusb/src cmake-build/*/metrics.json - name: Upload Metrics Artifact @@ -124,7 +123,7 @@ jobs: if: github.event_name != 'push' run: | if [ -f base-metrics/metrics.json ]; then - python tools/metrics.py compare -f tinyusb/src base-metrics/metrics.json metrics.json + python tools/metrics.py compare -m -f tinyusb/src base-metrics/metrics.json metrics.json cat metrics_compare.md else echo "No base metrics found, skipping comparison" diff --git a/examples/build_system/cmake/toolchain/arm_clang.cmake b/examples/build_system/cmake/toolchain/arm_clang.cmake index dba637367..e5ca82fab 100644 --- a/examples/build_system/cmake/toolchain/arm_clang.cmake +++ b/examples/build_system/cmake/toolchain/arm_clang.cmake @@ -7,7 +7,6 @@ if (NOT DEFINED CMAKE_CXX_COMPILER) endif () set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER}) -set(TOOLCHAIN_ASM_FLAGS "-x assembler-with-cpp") find_program(CMAKE_SIZE llvm-size) find_program(CMAKE_OBJCOPY llvm-objcopy) diff --git a/examples/build_system/cmake/toolchain/common.cmake b/examples/build_system/cmake/toolchain/common.cmake index 1ef04bc00..e610a349b 100644 --- a/examples/build_system/cmake/toolchain/common.cmake +++ b/examples/build_system/cmake/toolchain/common.cmake @@ -20,41 +20,32 @@ include(${CMAKE_CURRENT_LIST_DIR}/../cpu/${CMAKE_SYSTEM_CPU}.cmake) # ---------------------------------------------------------------------------- # Compile flags # ---------------------------------------------------------------------------- +set(TOOLCHAIN_C_FLAGS) +set(TOOLCHAIN_ASM_FLAGS) +set(TOOLCHAIN_EXE_LINKER_FLAGS) + if (TOOLCHAIN STREQUAL "gcc" OR TOOLCHAIN STREQUAL "clang") list(APPEND TOOLCHAIN_COMMON_FLAGS -fdata-sections -ffunction-sections # -fsingle-precision-constant # not supported by clang -fno-strict-aliasing - -g - ) - list(APPEND TOOLCHAIN_EXE_LINKER_FLAGS - -Wl,--print-memory-usage - -Wl,--gc-sections - -Wl,--cref + -g # include debug info for bloaty ) + set(TOOLCHAIN_EXE_LINKER_FLAGS "-Wl,--print-memory-usage -Wl,--gc-sections -Wl,--cref") + + if (TOOLCHAIN STREQUAL clang) + set(TOOLCHAIN_ASM_FLAGS "-x assembler-with-cpp") + endif () elseif (TOOLCHAIN STREQUAL "iar") - list(APPEND TOOLCHAIN_COMMON_FLAGS - --debug - ) - list(APPEND TOOLCHAIN_EXE_LINKER_FLAGS - --diag_suppress=Li065 - ) + set(TOOLCHAIN_C_FLAGS --debug) + set(TOOLCHAIN_EXE_LINKER_FLAGS --diag_suppress=Li065) endif () # join the toolchain flags into a single string list(JOIN TOOLCHAIN_COMMON_FLAGS " " TOOLCHAIN_COMMON_FLAGS) -foreach (LANG IN ITEMS C CXX ASM) - set(CMAKE_${LANG}_FLAGS_INIT ${TOOLCHAIN_COMMON_FLAGS}) - # optimization flags for LOG, LOGGER ? - #set(CMAKE_${LANG}_FLAGS_RELEASE_INIT "-Os") - #set(CMAKE_${LANG}_FLAGS_DEBUG_INIT "-O0") -endforeach () - -# Assembler -if (DEFINED TOOLCHAIN_ASM_FLAGS) - set(CMAKE_ASM_FLAGS_INIT "${CMAKE_ASM_FLAGS_INIT} ${TOOLCHAIN_ASM_FLAGS}") -endif () -# Linker -list(JOIN TOOLCHAIN_EXE_LINKER_FLAGS " " CMAKE_EXE_LINKER_FLAGS_INIT) +set(CMAKE_C_FLAGS_INIT "${TOOLCHAIN_COMMON_FLAGS} ${TOOLCHAIN_C_FLAGS}") +set(CMAKE_CXX_FLAGS_INIT "${TOOLCHAIN_COMMON_FLAGS} ${TOOLCHAIN_C_FLAGS}") +set(CMAKE_ASM_FLAGS_INIT "${TOOLCHAIN_COMMON_FLAGS} ${TOOLCHAIN_ASM_FLAGS}") +set(CMAKE_EXE_LINKER_FLAGS_INIT ${TOOLCHAIN_EXE_LINKER_FLAGS}) diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake index 62ec412e6..5eadcdaa9 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -232,7 +232,7 @@ function(family_add_bloaty TARGET) return() endif () - set(OPTION "--domain=vm -d compileunits") # add -d symbol if needed + set(OPTION "--domain=vm -d compileunits,sections,symbols") if (DEFINED BLOATY_OPTION) string(APPEND OPTION " ${BLOATY_OPTION}") endif () @@ -240,36 +240,33 @@ function(family_add_bloaty TARGET) add_custom_target(${TARGET}-bloaty DEPENDS ${TARGET} - COMMAND ${BLOATY_EXE} ${OPTION_LIST} $ > $.bloaty.txt - COMMAND cat $.bloaty.txt + COMMAND ${BLOATY_EXE} ${OPTION_LIST} $ VERBATIM) # post build - add_custom_command(TARGET ${TARGET} POST_BUILD - COMMAND ${BLOATY_EXE} ${OPTION_LIST} $ > $.bloaty.txt - COMMAND cat $.bloaty.txt - VERBATIM - ) + # add_custom_command(TARGET ${TARGET} POST_BUILD + # COMMAND ${BLOATY_EXE} --csv ${OPTION_LIST} $ > ${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_bloaty.csv + # VERBATIM + # ) endfunction() # Add linkermap target (https://github.com/hathach/linkermap) function(family_add_linkermap TARGET) - set(LINKERMAP_OPTION_LIST) + set(OPTION "-j") if (DEFINED LINKERMAP_OPTION) - separate_arguments(LINKERMAP_OPTION_LIST UNIX_COMMAND ${LINKERMAP_OPTION}) + string(APPEND OPTION " ${LINKERMAP_OPTION}") endif () + separate_arguments(OPTION_LIST UNIX_COMMAND ${OPTION}) add_custom_target(${TARGET}-linkermap - COMMAND python ${LINKERMAP_PY} ${LINKERMAP_OPTION_LIST} $.map + COMMAND python ${LINKERMAP_PY} ${OPTION_LIST} $.map VERBATIM ) - # post build if bloaty not exist - if (NOT TARGET ${TARGET}-bloaty) - add_custom_command(TARGET ${TARGET} POST_BUILD - COMMAND python ${LINKERMAP_PY} ${LINKERMAP_OPTION_LIST} $.map - VERBATIM) - endif () + # post build + add_custom_command(TARGET ${TARGET} POST_BUILD + COMMAND python ${LINKERMAP_PY} ${OPTION_LIST} $.map + VERBATIM) endfunction() #------------------------------------------------------------- @@ -384,7 +381,7 @@ function(family_configure_common TARGET RTOS) if (NOT RTOS STREQUAL zephyr) # Analyze size with bloaty and linkermap family_add_bloaty(${TARGET}) - family_add_linkermap(${TARGET}) # fall back to linkermap if bloaty not found + family_add_linkermap(${TARGET}) endif () # run size after build diff --git a/src/common/tusb_compiler.h b/src/common/tusb_compiler.h index c8108264f..f20834cea 100644 --- a/src/common/tusb_compiler.h +++ b/src/common/tusb_compiler.h @@ -183,11 +183,11 @@ #define TU_BSWAP32(u32) (__builtin_bswap32(u32)) #endif - #ifndef __ARMCC_VERSION // List of obsolete callback function that is renamed and should not be defined. // Put it here since only gcc support this pragma - #pragma GCC poison tud_vendor_control_request_cb - #endif + #if !defined(__ARMCC_VERSION) && !defined(__ICCARM__) + #pragma GCC poison tud_vendor_control_request_cb + #endif #elif defined(__ICCARM__) #include diff --git a/src/portable/synopsys/dwc2/hcd_dwc2.c b/src/portable/synopsys/dwc2/hcd_dwc2.c index b92448685..fc748c85f 100644 --- a/src/portable/synopsys/dwc2/hcd_dwc2.c +++ b/src/portable/synopsys/dwc2/hcd_dwc2.c @@ -821,7 +821,7 @@ static void channel_xfer_in_retry(dwc2_regs_t* dwc2, uint8_t ch_id, uint32_t hci } } -#if CFG_TUSB_DEBUG +#if CFG_TUSB_DEBUG && 0 TU_ATTR_ALWAYS_INLINE static inline void print_hcint(uint32_t hcint) { const char* str[] = { "XFRC", "HALTED", "AHBERR", "STALL", diff --git a/tools/get_deps.py b/tools/get_deps.py index f11d8d51e..0d9c1a8f1 100755 --- a/tools/get_deps.py +++ b/tools/get_deps.py @@ -15,7 +15,7 @@ deps_mandatory = { '159e31b689577dbf69cf0683bbaffbd71fa5ee10', 'all'], 'tools/linkermap': ['https://github.com/hathach/linkermap.git', - '23d1c4c84c4866b84cb821fb368bb9991633871d', + '8e1f440fa15c567aceb5aa0d14f6d18c329cc67f', 'all'], 'tools/uf2': ['https://github.com/microsoft/uf2.git', 'c594542b2faa01cc33a2b97c9fbebc38549df80a', diff --git a/tools/metrics.py b/tools/metrics.py index f879a0d34..50709d5ba 100644 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Calculate average sizes using bloaty output.""" +"""Calculate average sizes from bloaty CSV or TinyUSB metrics JSON outputs.""" import argparse import csv @@ -85,7 +85,7 @@ def parse_bloaty_csv(csv_text, filters=None): def combine_files(input_files, filters=None): - """Combine multiple bloaty outputs into a single data set.""" + """Combine multiple metrics inputs (bloaty CSV or metrics JSON) into a single data set.""" filters = filters or [] all_json_data = {"file_list": [], "data": []} @@ -168,12 +168,6 @@ def compute_avg(all_json_data): continue file_accumulator[fname]["symbols"][name].append(sym.get("size", 0)) sections_map = f.get("sections") or {} - if isinstance(sections_map, list): - sections_map = { - s.get("name"): s.get("size", 0) - for s in sections_map - if isinstance(s, dict) and s.get("name") - } for sname, ssize in sections_map.items(): file_accumulator[fname]["sections"][sname].append(ssize) @@ -240,6 +234,8 @@ def compare_files(base_file, new_file, filters=None): n = new_files.get(fname, {}) b_size = b.get("size", 0) n_size = n.get("size", 0) + base_sections = b.get("sections") or {} + new_sections = n.get("sections") or {} # Symbol diffs b_syms = {s["name"]: s for s in b.get("symbols", [])} @@ -256,6 +252,14 @@ def compare_files(base_file, new_file, filters=None): "file": fname, "size": {"base": b_size, "new": n_size, "diff": n_size - b_size}, "symbols": symbols, + "sections": { + name: { + "base": base_sections.get(name, 0), + "new": new_sections.get(name, 0), + "diff": new_sections.get(name, 0) - base_sections.get(name, 0), + } + for name in sorted(set(base_sections) | set(new_sections)) + }, }) total = { @@ -299,6 +303,17 @@ def get_sort_key(sort_order): return lambda x: x.get('file', ''), False +def format_diff(base, new, diff): + """Format a diff value with percentage.""" + if diff == 0: + return f"{new}" + if base == 0 or new == 0: + return f"{base} ➙ {new}" + pct = (diff / base) * 100 + sign = "+" if diff > 0 else "" + return f"{base} ➙ {new} ({sign}{diff}, {sign}{pct:.1f}%)" + + def write_json_output(json_data, path): """Write JSON output with indentation.""" @@ -315,7 +330,7 @@ def render_combine_table(json_data, sort_order='name+'): key_func, reverse = get_sort_key(sort_order) files_sorted = sorted(files, key=key_func, reverse=reverse) - total_size = json_data.get("TOTAL") or (sum(f.get("size", 0) for f in files_sorted) or 1) + total_size = json_data.get("TOTAL") or sum(f.get("size", 0) for f in files_sorted) pct_strings = [ f"{(f.get('percent') if f.get('percent') is not None else (f.get('size', 0) / total_size * 100 if total_size else 0)):.1f}%" @@ -357,12 +372,6 @@ def render_combine_table(json_data, sort_order='name+'): parts = [f"| {f.get('file', ''):<{file_width}} |"] if section_names: sections_map = f.get("sections") or {} - if isinstance(sections_map, list): - sections_map = { - s.get("name"): s.get("size", 0) - for s in sections_map - if isinstance(s, dict) and s.get("name") - } for name in section_names: parts.append(f" {sections_map.get(name, 0):>{section_widths[name]}} |") parts.append(f" {size_val:>{size_width}} | {pct_str:>{pct_width}} |") @@ -469,6 +478,7 @@ def _build_rows(files, sort_order): "new": sd['new'], "diff": diff_val, "pct": pct_str, + "sections": f.get("sections", {}), }) return rows @@ -506,59 +516,68 @@ def render_compare_table(rows, include_sum): if not rows: return ["No entries.", ""] + # collect section columns (reverse alpha) + section_names = sorted( + {name for r in rows for name in (r.get("sections") or {})}, + reverse=True, + ) + + def fmt_abs(val_old, val_new): + diff = val_new - val_old + if diff == 0: + return f"{val_new}" + sign = "+" if diff > 0 else "" + return f"{val_old} ➙ {val_new} ({sign}{diff})" + sum_base = sum(r["base"] for r in rows) sum_new = sum(r["new"] for r in rows) total_diff = sum_new - sum_base total_pct = "n/a" if sum_base == 0 else f"{(total_diff / sum_base) * 100:+.1f}%" - base_width = max(len("base"), *(len(str(r["base"])) for r in rows)) - new_width = max(len("new"), *(len(str(r["new"])) for r in rows)) - diff_width = max(len("diff"), *(len(f"{r['diff']:+}") for r in rows)) - pct_width = max(len("% diff"), *(len(r["pct"]) for r in rows)) - name_width = max(len("file"), *(len(r["file"]) for r in rows)) - - if include_sum: - base_width = max(base_width, len(str(sum_base))) - new_width = max(new_width, len(str(sum_new))) - diff_width = max(diff_width, len(f"{total_diff:+}")) - pct_width = max(pct_width, len(total_pct)) - name_width = max(name_width, len("TOTAL")) - - header = ( - f"| {'file':<{name_width}} | " - f"{'base':>{base_width}} | " - f"{'new':>{new_width}} | " - f"{'diff':>{diff_width}} | " - f"{'% diff':>{pct_width}} |" - ) - separator = ( - f"| :{'-' * (name_width - 1)} | " - f"{'-' * base_width}:| " - f"{'-' * new_width}:| " - f"{'-' * diff_width}:| " - f"{'-' * pct_width}:|" + file_width = max(len("file"), *(len(r["file"]) for r in rows), len("TOTAL")) + size_width = max( + len("size"), + *(len(fmt_abs(r["base"], r["new"])) for r in rows), + len(fmt_abs(sum_base, sum_new)), ) + pct_width = max(len("% diff"), *(len(r["pct"]) for r in rows), len(total_pct)) + section_widths = {} + for name in section_names: + max_val_len = 0 + for r in rows: + sec_entry = (r.get("sections") or {}).get(name, {"base": 0, "new": 0}) + max_val_len = max(max_val_len, len(fmt_abs(sec_entry.get("base", 0), sec_entry.get("new", 0)))) + section_widths[name] = max(len(name), max_val_len, 1) + + header_parts = [f"| {'file':<{file_width}} |"] + sep_parts = [f"| :{'-' * (file_width - 1)} |"] + for name in section_names: + header_parts.append(f" {name:>{section_widths[name]}} |") + sep_parts.append(f" {'-' * (section_widths[name] - 1)}: |") + header_parts.append(f" {'size':>{size_width}} | {'% diff':>{pct_width}} |") + sep_parts.append(f" {'-' * (size_width - 1)}: | {'-' * (pct_width - 1)}: |") + header = "".join(header_parts) + separator = "".join(sep_parts) lines = [header, separator] for r in rows: - diff_str = f"{r['diff']:+}" - lines.append( - f"| {r['file']:<{name_width}} | " - f"{str(r['base']):>{base_width}} | " - f"{str(r['new']):>{new_width}} | " - f"{diff_str:>{diff_width}} | " - f"{r['pct']:>{pct_width}} |" - ) + parts = [f"| {r['file']:<{file_width}} |"] + sections_map = r.get("sections") or {} + for name in section_names: + sec_entry = sections_map.get(name, {"base": 0, "new": 0}) + parts.append(f" {fmt_abs(sec_entry.get('base', 0), sec_entry.get('new', 0)):>{section_widths[name]}} |") + parts.append(f" {fmt_abs(r['base'], r['new']):>{size_width}} | {r['pct']:>{pct_width}} |") + lines.append("".join(parts)) if include_sum: - lines.append( - f"| {'TOTAL':<{name_width}} | " - f"{sum_base:>{base_width}} | " - f"{sum_new:>{new_width}} | " - f"{total_diff:+{diff_width}d} | " - f"{total_pct:>{pct_width}} |" - ) + total_parts = [f"| {'TOTAL':<{file_width}} |"] + for name in section_names: + total_base = sum((r.get("sections") or {}).get(name, {}).get("base", 0) for r in rows) + total_new = sum((r.get("sections") or {}).get(name, {}).get("new", 0) for r in rows) + total_parts.append(f" {fmt_abs(total_base, total_new):>{section_widths[name]}} |") + total_parts.append(f" {fmt_abs(sum_base, sum_new):>{size_width}} | {total_pct:>{pct_width}} |") + lines.append("".join(total_parts)) return lines @@ -592,9 +611,10 @@ def cmd_compare(args): if not args.quiet: print_compare_summary(comparison, args.sort) - write_compare_markdown(comparison, args.out + '.md', args.sort) - if not args.quiet: - print(f"Comparison written to {args.out}.md") + if args.markdown_out: + write_compare_markdown(comparison, args.out + '.md', args.sort) + if not args.quiet: + print(f"Comparison written to {args.out}.md") def main(argv=None): @@ -602,9 +622,9 @@ def main(argv=None): subparsers = parser.add_subparsers(dest='command', required=True, help='Available commands') # Combine subcommand - combine_parser = subparsers.add_parser('combine', help='Combine and average multiple bloaty outputs') + combine_parser = subparsers.add_parser('combine', help='Combine and average bloaty CSV outputs or metrics JSON files') combine_parser.add_argument('files', nargs='+', - help='Path to bloaty CSV output or JSON file(s) or glob pattern(s)') + help='Path to bloaty CSV output or TinyUSB metrics JSON file(s) (including linkermap-generated) or glob pattern(s)') combine_parser.add_argument('-f', '--filter', dest='filters', action='append', default=[], help='Only include compile units whose path contains this substring (can be repeated)') combine_parser.add_argument('-o', '--out', dest='out', default='metrics', @@ -620,13 +640,15 @@ def main(argv=None): help='Sort order: size/size- (descending), size+ (ascending), name/name+ (ascending), name- (descending). Default: size-') # Compare subcommand - compare_parser = subparsers.add_parser('compare', help='Compare two bloaty outputs (CSV) or JSON inputs') - compare_parser.add_argument('base', help='Base CSV/JSON file') - compare_parser.add_argument('new', help='New CSV/JSON file') + compare_parser = subparsers.add_parser('compare', help='Compare two metrics inputs (bloaty CSV or metrics JSON)') + compare_parser.add_argument('base', help='Base CSV/metrics JSON file') + compare_parser.add_argument('new', help='New CSV/metrics JSON file') compare_parser.add_argument('-f', '--filter', dest='filters', action='append', default=[], help='Only include compile units whose path contains this substring (can be repeated)') compare_parser.add_argument('-o', '--out', dest='out', default='metrics_compare', - help='Output path basename for Markdown file (default: metrics_compare)') + help='Output path basename for Markdown/JSON files (default: metrics_compare)') + compare_parser.add_argument('-m', '--markdown', dest='markdown_out', action='store_true', + help='Write Markdown output file') compare_parser.add_argument('-S', '--sort', dest='sort', default='name+', choices=['size', 'size-', 'size+', 'name', 'name-', 'name+'], help='Sort order: size/size- (descending), size+ (ascending), name/name+ (ascending), name- (descending). Default: name+') -- cgit v1.3.1 From c7f7dc6ee1f4f56ccb86adabb2ca3f14c212e985 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 11 Dec 2025 19:01:33 +0700 Subject: separate metric comment into its own workflow in order to work with forked PR --- .github/workflows/build.yml | 16 ++++++++++----- .github/workflows/metrics_comment.yml | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/metrics_comment.yml (limited to '.github/workflows') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 9d94a3b9b..ea7f5d74b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -130,12 +130,18 @@ jobs: cp metrics.md metrics_compare.md fi - - name: Post Code Metrics as PR Comment - if: github.event_name != 'push' - uses: marocchino/sticky-pull-request-comment@v2 + - name: Save PR number + if: github.event_name == 'pull_request' + run: echo ${{ github.event.number }} > pr_number.txt + + - name: Upload Metrics Comment Artifact + if: github.event_name == 'pull_request' + uses: actions/upload-artifact@v5 with: - header: code-metrics - path: metrics_compare.md + name: metrics-comment + path: | + metrics_compare.md + pr_number.txt # --------------------------------------- # Build Make/CMake on Windows/MacOS diff --git a/.github/workflows/metrics_comment.yml b/.github/workflows/metrics_comment.yml new file mode 100644 index 000000000..2f1b0d631 --- /dev/null +++ b/.github/workflows/metrics_comment.yml @@ -0,0 +1,38 @@ +name: Metrics Comment + +on: + workflow_run: + workflows: ["Build"] + types: + - completed + +jobs: + post-comment: + runs-on: ubuntu-latest + if: > + github.event.workflow_run.event == 'pull_request' && + github.event.workflow_run.conclusion == 'success' + permissions: + pull-requests: write + steps: + - name: Download Artifacts + uses: actions/download-artifact@v4 + with: + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ secrets.GITHUB_TOKEN }} + name: metrics-comment + + - name: Read PR Number + id: pr_number + run: | + if [ -f pr_number.txt ]; then + echo "number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT + fi + + - name: Post Code Metrics as PR Comment + if: steps.pr_number.outputs.number != '' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: code-metrics + path: metrics_compare.md + number: ${{ steps.pr_number.outputs.number }} -- cgit v1.3.1 From d71e3c9ea61b1d2ab8e60daeb299abfdf828541e Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 12 Dec 2025 00:58:17 +0700 Subject: post pr comment anyway --- .github/workflows/build.yml | 7 +++++++ 1 file changed, 7 insertions(+) (limited to '.github/workflows') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index ea7f5d74b..c062aca46 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -143,6 +143,13 @@ jobs: metrics_compare.md pr_number.txt + - name: Post Code Metrics as PR Comment + if: github.event_name != 'push' + uses: marocchino/sticky-pull-request-comment@v2 + with: + header: code-metrics + path: metrics_compare.md + # --------------------------------------- # Build Make/CMake on Windows/MacOS # --------------------------------------- -- cgit v1.3.1