From a337a6d337c0cdd50981ba2040aee99966ae3152 Mon Sep 17 00:00:00 2001 From: hathach Date: Mon, 1 Dec 2025 17:31:43 +0700 Subject: run linkermap as post build for size analyze --- tools/build.py | 58 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) (limited to 'tools/build.py') diff --git a/tools/build.py b/tools/build.py index ce4d0ef1a..5328a987f 100755 --- a/tools/build.py +++ b/tools/build.py @@ -5,6 +5,7 @@ import os import sys import time import subprocess +import shlex from pathlib import Path from multiprocessing import Pool @@ -29,9 +30,12 @@ parallel_jobs = os.cpu_count() # Helper # ----------------------------- def run_cmd(cmd): - #print(cmd) - r = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - title = f'Command Error: {cmd}' + if isinstance(cmd, str): + raise TypeError("run_cmd expects a list/tuple of args, not a string") + args = cmd + cmd_display = " ".join(args) + r = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) + title = f'Command Error: {cmd_display}' if r.returncode != 0: # print build output if failed if os.getenv('GITHUB_ACTIONS'): @@ -42,7 +46,7 @@ def run_cmd(cmd): print(title) print(r.stdout.decode("utf-8")) elif verbose: - print(cmd) + print(cmd_display) print(r.stdout.decode("utf-8")) return r @@ -87,10 +91,10 @@ def cmake_board(board, build_args, build_flags_on): start_time = time.monotonic() build_dir = f'cmake-build/cmake-build-{board}' - build_flags = '' + build_flags = [] if len(build_flags_on) > 0: - build_flags = ' '.join(f'-D{flag}=1' for flag in build_flags_on) - build_flags = f'-DCFLAGS_CLI="{build_flags}"' + cli_flags = ' '.join(f'-D{flag}=1' for flag in build_flags_on) + build_flags.append(f'-DCFLAGS_CLI={cli_flags}') build_dir += '-f1_' + '_'.join(build_flags_on) family = find_family(board) @@ -101,25 +105,22 @@ def cmake_board(board, build_args, build_flags_on): if build_utils.skip_example(example, board): ret[2] += 1 else: - rcmd = run_cmd(f'idf.py -C examples/{example} -B {build_dir}/{example} -G Ninja ' - f'-DBOARD={board} {build_flags} build') + rcmd = run_cmd([ + 'idf.py', '-C', f'examples/{example}', '-B', f'{build_dir}/{example}', '-GNinja', + f'-DBOARD={board}', *build_flags, 'build' + ]) ret[0 if rcmd.returncode == 0 else 1] += 1 else: - rcmd = run_cmd(f'cmake examples -B {build_dir} -G Ninja -DBOARD={board} -DCMAKE_BUILD_TYPE=MinSizeRel ' - f'{build_args} {build_flags}') + rcmd = run_cmd([ + 'cmake', 'examples', '-B', build_dir, '-GNinja', + f'-DBOARD={board}', '-DCMAKE_BUILD_TYPE=MinSizeRel', + '-DLINKERMAP_OPTION=-q -f tinyusb/src', *build_args, *build_flags + ]) if rcmd.returncode == 0: - cmd = f"cmake --build {build_dir}" - njobs = parallel_jobs - - # circleci docker return $nproc as 36 core, limit parallel according to resource class. - # Required for IAR, also prevent crashed/killed by docker - if os.getenv('CIRCLECI'): - resource_class = { 'small': 1, 'medium': 2, 'medium+': 3, 'large': 4 } - for rc in resource_class: - if rc in os.getenv('CIRCLE_JOB'): - njobs = resource_class[rc] - break - cmd += f' --parallel {njobs}' + cmd = [ + "cmake", "--build", build_dir, + '--parallel', str(parallel_jobs) + ] rcmd = run_cmd(cmd) ret[0 if rcmd.returncode == 0 else 1] += 1 @@ -141,9 +142,12 @@ def make_one_example(example, board, make_option): # skip -j for circleci if not os.getenv('CIRCLECI'): make_option += ' -j' - make_cmd = f"make -C examples/{example} BOARD={board} {make_option}" - # run_cmd(f"{make_cmd} clean") - build_result = run_cmd(f"{make_cmd} all") + make_args = ["make", "-C", f"examples/{example}", f"BOARD={board}"] + if make_option: + make_args += shlex.split(make_option) + make_args.append("all") + # 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) @@ -180,7 +184,7 @@ def build_boards_list(boards, build_defines, build_system, build_flags_on): for b in boards: r = [0, 0, 0] if build_system == 'cmake': - build_args = ' '.join(f'-D{d}' for d in build_defines) + build_args = [f'-D{d}' for d in build_defines] r = cmake_board(b, build_args, build_flags_on) elif build_system == 'make': build_args = ' '.join(f'{d}' for d in build_defines) -- cgit v1.3.1 From c859744784cc396ae0993a16a1935b10fbd9b797 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 2 Dec 2025 12:50:31 +0700 Subject: adding metrics for computing average compiled size --- examples/device/CMakeLists.txt | 63 ++++++++++--------- examples/dual/CMakeLists.txt | 10 ++- examples/host/CMakeLists.txt | 20 +++--- hw/bsp/family_support.cmake | 6 -- tools/build.py | 4 +- tools/get_deps.py | 2 +- tools/metrics.py | 134 +++++++++++++++++++++++++++++++++++++++++ 7 files changed, 193 insertions(+), 46 deletions(-) create mode 100644 tools/metrics.py (limited to 'tools/build.py') diff --git a/examples/device/CMakeLists.txt b/examples/device/CMakeLists.txt index eb625ea51..660df67cb 100644 --- a/examples/device/CMakeLists.txt +++ b/examples/device/CMakeLists.txt @@ -6,31 +6,38 @@ project(tinyusb_device_examples C CXX ASM) family_initialize_project(tinyusb_device_examples ${CMAKE_CURRENT_LIST_DIR}) # family_add_subdirectory will filter what to actually add based on selected FAMILY -family_add_subdirectory(audio_4_channel_mic) -family_add_subdirectory(audio_test) -family_add_subdirectory(audio_4_channel_mic_freertos) -family_add_subdirectory(audio_test_freertos) -family_add_subdirectory(audio_test_multi_rate) -family_add_subdirectory(board_test) -family_add_subdirectory(cdc_dual_ports) -family_add_subdirectory(cdc_msc) -family_add_subdirectory(cdc_msc_freertos) -family_add_subdirectory(cdc_uac2) -family_add_subdirectory(dfu) -family_add_subdirectory(dfu_runtime) -family_add_subdirectory(dynamic_configuration) -family_add_subdirectory(hid_boot_interface) -family_add_subdirectory(hid_composite) -family_add_subdirectory(hid_composite_freertos) -family_add_subdirectory(hid_generic_inout) -family_add_subdirectory(hid_multiple_interface) -family_add_subdirectory(midi_test) -family_add_subdirectory(msc_dual_lun) -family_add_subdirectory(mtp) -family_add_subdirectory(net_lwip_webserver) -family_add_subdirectory(uac2_headset) -family_add_subdirectory(uac2_speaker_fb) -family_add_subdirectory(usbtmc) -family_add_subdirectory(video_capture) -family_add_subdirectory(video_capture_2ch) -family_add_subdirectory(webusb_serial) +set(EXAMPLE_LIST + audio_4_channel_mic + audio_4_channel_mic_freertos + audio_test + audio_test_freertos + audio_test_multi_rate + board_test + cdc_dual_ports + cdc_msc + cdc_msc_freertos + cdc_uac2 + dfu + dfu_runtime + dynamic_configuration + hid_boot_interface + hid_composite + hid_composite_freertos + hid_generic_inout + hid_multiple_interface + midi_test + midi_test_freertos + msc_dual_lun + mtp + net_lwip_webserver + uac2_headset + uac2_speaker_fb + usbtmc + video_capture + video_capture_2ch + webusb_serial + ) + +foreach (example ${EXAMPLE_LIST}) + family_add_subdirectory(${example}) +endforeach () diff --git a/examples/dual/CMakeLists.txt b/examples/dual/CMakeLists.txt index c5e3ffce4..4978f1fab 100644 --- a/examples/dual/CMakeLists.txt +++ b/examples/dual/CMakeLists.txt @@ -9,6 +9,12 @@ if (FAMILY STREQUAL "rp2040" AND NOT TARGET tinyusb_pico_pio_usb) message("Skipping dual host/device mode examples as Pico-PIO-USB is not available") else () # family_add_subdirectory will filter what to actually add based on selected FAMILY - family_add_subdirectory(host_hid_to_device_cdc) - family_add_subdirectory(host_info_to_device_cdc) + set(EXAMPLE_LIST + host_hid_to_device_cdc + host_info_to_device_cdc + ) + + foreach (example ${EXAMPLE_LIST}) + family_add_subdirectory(${example}) + endforeach () endif () diff --git a/examples/host/CMakeLists.txt b/examples/host/CMakeLists.txt index 2783dd84e..f8e0ce692 100644 --- a/examples/host/CMakeLists.txt +++ b/examples/host/CMakeLists.txt @@ -6,10 +6,16 @@ project(tinyusb_host_examples C CXX ASM) family_initialize_project(tinyusb_host_examples ${CMAKE_CURRENT_LIST_DIR}) # family_add_subdirectory will filter what to actually add based on selected FAMILY -family_add_subdirectory(bare_api) -family_add_subdirectory(cdc_msc_hid) -family_add_subdirectory(cdc_msc_hid_freertos) -family_add_subdirectory(device_info) -family_add_subdirectory(hid_controller) -family_add_subdirectory(midi_rx) -family_add_subdirectory(msc_file_explorer) +set(EXAMPLE_LIST + bare_api + cdc_msc_hid + cdc_msc_hid_freertos + device_info + hid_controller + midi_rx + msc_file_explorer + ) + +foreach (example ${EXAMPLE_LIST}) + family_add_subdirectory(${example}) +endforeach () diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake index 1f91d0910..e7dfc19c8 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -231,12 +231,6 @@ function(family_add_linkermap TARGET) separate_arguments(LINKERMAP_OPTION_LIST UNIX_COMMAND ${LINKERMAP_OPTION}) endif () - if (ARGC GREATER 1) - separate_arguments(ARG_OPTION_LIST UNIX_COMMAND ${ARGV1}) - list(APPEND LINKERMAP_OPTION_LIST ${ARG_OPTION_LIST}) - endif () - - # target add_custom_target(${TARGET}-linkermap COMMAND python ${LINKERMAP_PY} -j -m ${LINKERMAP_OPTION_LIST} $.map VERBATIM diff --git a/tools/build.py b/tools/build.py index 5328a987f..692853297 100755 --- a/tools/build.py +++ b/tools/build.py @@ -113,8 +113,8 @@ def cmake_board(board, build_args, build_flags_on): else: rcmd = run_cmd([ 'cmake', 'examples', '-B', build_dir, '-GNinja', - f'-DBOARD={board}', '-DCMAKE_BUILD_TYPE=MinSizeRel', - '-DLINKERMAP_OPTION=-q -f tinyusb/src', *build_args, *build_flags + f'-DBOARD={board}', '-DCMAKE_BUILD_TYPE=MinSizeRel', '-DLINKERMAP_OPTION=-q -f tinyusb/src', + *build_args, *build_flags ]) if rcmd.returncode == 0: cmd = [ diff --git a/tools/get_deps.py b/tools/get_deps.py index 47cc5c7dd..5fb7e022c 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', - '1f47651142646398c7746e109ae0481732aeb564', + 'ac1228d5bbde1e54cb2e17e928662094ae19c51d', 'all'], 'tools/uf2': ['https://github.com/microsoft/uf2.git', 'c594542b2faa01cc33a2b97c9fbebc38549df80a', diff --git a/tools/metrics.py b/tools/metrics.py new file mode 100644 index 000000000..d972d3681 --- /dev/null +++ b/tools/metrics.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +"""Calculate average size from multiple linker map files.""" + +import argparse +import sys +import os + +# Add linkermap module to path +sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'linkermap')) +import linkermap + + +def combine_maps(map_files, filters=None): + """Combine multiple map files into a list of json_data. + + Args: + map_files: List of paths to linker map files or JSON files + filters: List of path substrings to filter object files (default: []) + + Returns: + all_json_data: Dictionary with mapfiles list and data from each map file + """ + import json + + filters = filters or [] + all_json_data = {"mapfiles": [], "data": []} + + for map_file in map_files: + if not os.path.exists(map_file): + print(f"Warning: {map_file} not found, skipping", file=sys.stderr) + continue + + try: + if map_file.endswith('.json'): + with open(map_file, 'r', encoding='utf-8') as f: + json_data = json.load(f) + # Apply path filters to JSON data + if filters: + filtered_files = [ + f for f in json_data["files"] + if f.get("path") and any(filt in f["path"] for filt in filters) + ] + json_data["files"] = filtered_files + else: + json_data = linkermap.analyze_map(map_file, filters=filters) + all_json_data["mapfiles"].append(map_file) + all_json_data["data"].append(json_data) + except Exception as e: + print(f"Warning: Failed to analyze {map_file}: {e}", file=sys.stderr) + continue + + return all_json_data + + +def compute_avg(all_json_data): + """Compute average sizes from combined json_data. + + Args: + all_json_data: Dictionary with mapfiles and data from combine_maps() + + Returns: + json_average: Dictionary with averaged size data + """ + if not all_json_data["data"]: + return None + + # Collect all sections preserving order + all_sections = [] + for json_data in all_json_data["data"]: + for s in json_data["sections"]: + if s not in all_sections: + all_sections.append(s) + + # Merge files with the same 'file' value and compute averages + file_accumulator = {} # key: file name, value: {"sections": {section: [sizes]}, "totals": [totals]} + + for json_data in all_json_data["data"]: + for f in json_data["files"]: + fname = f["file"] + if fname not in file_accumulator: + file_accumulator[fname] = {"sections": {}, "totals": [], "path": f.get("path")} + file_accumulator[fname]["totals"].append(f["total"]) + for section, size in f["sections"].items(): + if section in file_accumulator[fname]["sections"]: + file_accumulator[fname]["sections"][section].append(size) + else: + file_accumulator[fname]["sections"][section] = [size] + + # Build json_average with averaged values + files_average = [] + for fname, data in file_accumulator.items(): + avg_total = round(sum(data["totals"]) / len(data["totals"])) + avg_sections = {} + for section, sizes in data["sections"].items(): + avg_sections[section] = round(sum(sizes) / len(sizes)) + files_average.append({ + "file": fname, + "path": data["path"], + "sections": avg_sections, + "total": avg_total + }) + + json_average = { + "mapfiles": all_json_data["mapfiles"], + "sections": all_sections, + "files": files_average + } + + return json_average + + +def main(): + parser = argparse.ArgumentParser(description='Calculate average size from linker map files') + parser.add_argument('files', nargs='+', help='Path to map file(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)') + args = parser.parse_args() + + all_json_data = combine_maps(args.files, args.filters) + json_average = compute_avg(all_json_data) + + if json_average is None: + print("No valid map files found", file=sys.stderr) + sys.exit(1) + + linkermap.print_summary(json_average, False) + linkermap.write_json(json_average, args.out + '.json') + linkermap.write_markdown(json_average, args.out + '.md') + + +if __name__ == '__main__': + main() -- cgit v1.3.1 From 09e1113aaf1b2618ffe42e9638d68e6047b6f1ef Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 2 Dec 2025 14:22:52 +0700 Subject: adding metrics for computing average compiled size --- examples/CMakeLists.txt | 25 ++++++++++++++++--- hw/bsp/family_support.cmake | 4 +-- tools/build.py | 61 ++++++++++++++++++++++++--------------------- tools/get_deps.py | 2 +- tools/metrics.py | 47 ++++++++++++++++++++++++++++------ 5 files changed, 96 insertions(+), 43 deletions(-) (limited to 'tools/build.py') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d34c6ed5d..d9f97d598 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -5,7 +5,24 @@ include(${CMAKE_CURRENT_SOURCE_DIR}/../hw/bsp/family_support.cmake) project(tinyusb_examples C CXX ASM) -add_subdirectory(device) -add_subdirectory(dual) -add_subdirectory(host) -add_subdirectory(typec) +set(EXAMPLES_LIST + device + dual + host + typec + ) +set(MAPJSON_PATTERNS "") + +foreach (example ${EXAMPLES_LIST}) + add_subdirectory(${example}) + list(APPEND MAPJSON_PATTERNS "${CMAKE_BINARY_DIR}/${example}/*/*.map.json") +endforeach () + +# Post-build: run metrics.py on all map.json files +add_custom_target(tinyusb_examples_metrics + COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/../tools/metrics.py + -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 e7dfc19c8..3ede95e3f 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -232,13 +232,13 @@ function(family_add_linkermap TARGET) endif () add_custom_target(${TARGET}-linkermap - COMMAND python ${LINKERMAP_PY} -j -m ${LINKERMAP_OPTION_LIST} $.map + COMMAND python ${LINKERMAP_PY} -j ${LINKERMAP_OPTION_LIST} $.map VERBATIM ) # post build add_custom_command(TARGET ${TARGET} POST_BUILD - COMMAND python ${LINKERMAP_PY} -j -m ${LINKERMAP_OPTION_LIST} $.map + COMMAND python ${LINKERMAP_PY} -j ${LINKERMAP_OPTION_LIST} $.map VERBATIM) endfunction() diff --git a/tools/build.py b/tools/build.py index 692853297..5392a9aa4 100755 --- a/tools/build.py +++ b/tools/build.py @@ -6,6 +6,8 @@ import sys import time import subprocess import shlex +import glob +import metrics from pathlib import Path from multiprocessing import Pool @@ -111,18 +113,18 @@ def cmake_board(board, build_args, build_flags_on): ]) ret[0 if rcmd.returncode == 0 else 1] += 1 else: - rcmd = run_cmd([ - 'cmake', 'examples', '-B', build_dir, '-GNinja', - f'-DBOARD={board}', '-DCMAKE_BUILD_TYPE=MinSizeRel', '-DLINKERMAP_OPTION=-q -f tinyusb/src', - *build_args, *build_flags - ]) + rcmd = run_cmd(['cmake', 'examples', '-B', build_dir, '-GNinja', + f'-DBOARD={board}', '-DCMAKE_BUILD_TYPE=MinSizeRel', '-DLINKERMAP_OPTION=-q -f tinyusb/src', + *build_args, *build_flags]) if rcmd.returncode == 0: - cmd = [ - "cmake", "--build", build_dir, - '--parallel', str(parallel_jobs) - ] + cmd = ["cmake", "--build", build_dir, '--parallel', str(parallel_jobs)] rcmd = run_cmd(cmd) - ret[0 if rcmd.returncode == 0 else 1] += 1 + if rcmd.returncode == 0: + ret[0] += 1 + rcmd = run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_examples_metrics']) + # print(rcmd.stdout.decode("utf-8")) + else: + ret[1] += 1 example = 'all' print_build_result(board, example, 0 if ret[1] == 0 else 1, time.monotonic() - start_time) @@ -195,8 +197,18 @@ def build_boards_list(boards, build_defines, build_system, build_flags_on): return ret -def build_family(family, build_defines, build_system, build_flags_on, one_per_family, boards): - skip_ci = ['pico_sdk'] +def get_family_boards(family, one_per_family, boards): + """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 + + Returns: + List of board names + """ + skip_ci = [] 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(): @@ -207,17 +219,15 @@ def build_family(family, build_defines, build_system, build_flags_on, one_per_fa all_boards.append(entry.name) all_boards.sort() - ret = [0, 0, 0] # 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 ret + return [] all_boards = [random.choice(all_boards)] - ret = build_boards_list(all_boards, build_defines, build_system, build_flags_on) - return ret + return all_boards # ----------------------------- @@ -258,9 +268,8 @@ def main(): print(build_separator) print(build_format.format('Board', 'Example', '\033[39mResult\033[0m', 'Time')) total_time = time.monotonic() - result = [0, 0, 0] - # build families + # get all families all_families = [] if 'all' in families: for entry in os.scandir("hw/bsp"): @@ -270,23 +279,19 @@ def main(): all_families = list(families) all_families.sort() - # succeeded, failed, skipped + # get boards from families and append to boards list + all_boards = list(boards) for f in all_families: - r = build_family(f, build_defines, build_system, build_flags_on, one_per_family, boards) - result[0] += r[0] - result[1] += r[1] - result[2] += r[2] + all_boards.extend(get_family_boards(f, one_per_family, boards)) - # build boards - r = build_boards_list(boards, build_defines, build_system, build_flags_on) - result[0] += r[0] - result[1] += r[1] - result[2] += r[2] + # build all boards + result = build_boards_list(all_boards, build_defines, build_system, build_flags_on) total_time = time.monotonic() - total_time print(build_separator) print(f"Build Summary: {result[0]} {STATUS_OK}, {result[1]} {STATUS_FAILED} and took {total_time:.2f}s") print(build_separator) + return result[1] diff --git a/tools/get_deps.py b/tools/get_deps.py index 5fb7e022c..029c33607 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', - 'ac1228d5bbde1e54cb2e17e928662094ae19c51d', + '75d9d2c9e0f83297ddbc0da899f6cc0ab21076f0', 'all'], 'tools/uf2': ['https://github.com/microsoft/uf2.git', 'c594542b2faa01cc33a2b97c9fbebc38549df80a', diff --git a/tools/metrics.py b/tools/metrics.py index d972d3681..c6cd49d57 100644 --- a/tools/metrics.py +++ b/tools/metrics.py @@ -2,6 +2,7 @@ """Calculate average size from multiple linker map files.""" import argparse +import glob import sys import os @@ -10,6 +11,24 @@ sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'linkermap')) import linkermap +def expand_files(file_patterns): + """Expand file patterns (globs) to list of files. + + Args: + file_patterns: List of file paths or glob patterns + + Returns: + List of expanded file paths + """ + expanded = [] + for pattern in file_patterns: + if '*' in pattern or '?' in pattern: + expanded.extend(glob.glob(pattern)) + else: + expanded.append(pattern) + return expanded + + def combine_maps(map_files, filters=None): """Combine multiple map files into a list of json_data. @@ -109,25 +128,37 @@ def compute_avg(all_json_data): return json_average -def main(): +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)') + 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)') - args = parser.parse_args() - - all_json_data = combine_maps(args.files, args.filters) + 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) + + # Expand glob patterns + map_files = expand_files(args.files) + + all_json_data = combine_maps(map_files, args.filters) json_average = compute_avg(all_json_data) if json_average is None: print("No valid map files found", file=sys.stderr) sys.exit(1) - linkermap.print_summary(json_average, False) - linkermap.write_json(json_average, args.out + '.json') - linkermap.write_markdown(json_average, args.out + '.md') + if not args.quiet: + linkermap.print_summary(json_average, False) + if args.json_out: + linkermap.write_json(json_average, args.out + '.json') + if args.markdown_out: + linkermap.write_markdown(json_average, args.out + '.md') if __name__ == '__main__': -- 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 'tools/build.py') 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 'tools/build.py') 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 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 'tools/build.py') 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 'tools/build.py') 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 9465ce985bcf5732795d2aea5df4fd6315896cf9 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 9 Jan 2026 11:01:58 +0700 Subject: apply copilot suggestion --- hw/bsp/at32f45x/FreeRTOSConfig/FreeRTOSConfig.h | 2 +- hw/bsp/at32f45x/at32f45x_clock.c | 2 +- hw/bsp/at32f45x/at32f45x_int.c | 4 ++-- hw/bsp/at32f45x/family.c | 4 ++-- tools/build.py | 4 +++- 5 files changed, 9 insertions(+), 7 deletions(-) (limited to 'tools/build.py') diff --git a/hw/bsp/at32f45x/FreeRTOSConfig/FreeRTOSConfig.h b/hw/bsp/at32f45x/FreeRTOSConfig/FreeRTOSConfig.h index 8a9906d39..7a4393ad7 100644 --- a/hw/bsp/at32f45x/FreeRTOSConfig/FreeRTOSConfig.h +++ b/hw/bsp/at32f45x/FreeRTOSConfig/FreeRTOSConfig.h @@ -49,7 +49,7 @@ #endif -/* Cortex M23/M33 port configuration. */ +/* Cortex-M4 port configuration. */ #define configENABLE_MPU 0 #define configENABLE_FPU 1 #define configENABLE_TRUSTZONE 0 diff --git a/hw/bsp/at32f45x/at32f45x_clock.c b/hw/bsp/at32f45x/at32f45x_clock.c index a6724e23f..a66a8b1aa 100644 --- a/hw/bsp/at32f45x/at32f45x_clock.c +++ b/hw/bsp/at32f45x/at32f45x_clock.c @@ -53,7 +53,7 @@ void system_clock_config(void) /* set the flash clock divider */ flash_psr_set(FLASH_WAIT_CYCLE_5); - /* enable pwc periph clock */ + /* enable pwc periph clock */ crm_periph_clock_enable(CRM_PWC_PERIPH_CLOCK, TRUE); /* config ldo voltage */ diff --git a/hw/bsp/at32f45x/at32f45x_int.c b/hw/bsp/at32f45x/at32f45x_int.c index 15ff90c64..b3ba6ad9c 100644 --- a/hw/bsp/at32f45x/at32f45x_int.c +++ b/hw/bsp/at32f45x/at32f45x_int.c @@ -26,11 +26,11 @@ /* includes ------------------------------------------------------------------*/ #include "at32f45x_int.h" -/** @addtogroup AT32F455_periph_examples +/** @addtogroup AT32F45X_BSP * @{ */ -/** @addtogroup 455_USB_device_keyboard +/** @addtogroup AT32F45X_USB_Device_Keyboard * @{ */ diff --git a/hw/bsp/at32f45x/family.c b/hw/bsp/at32f45x/family.c index 32bae96af..79da6aec2 100644 --- a/hw/bsp/at32f45x/family.c +++ b/hw/bsp/at32f45x/family.c @@ -202,7 +202,7 @@ int board_uart_read(uint8_t *buf, int len) { int board_uart_write(void const *buf, int len) { #if CFG_TUSB_OS == OPT_OS_NONE int txsize = len; - u16 timeout = 0xffff; + uint16_t timeout = 0xffff; while (txsize--) { while (usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET) { timeout--; @@ -252,7 +252,7 @@ void _init(void) { void assert_failed(const char *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, - tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + e.g.: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/tools/build.py b/tools/build.py index 87064b7a0..d22d06a0b 100755 --- a/tools/build.py +++ b/tools/build.py @@ -40,7 +40,9 @@ ci_skip_boards = { } ci_preferred_boards = { - 'stm32h7': ['stm32h743eval'], + 'samd2x_l2x': ['metro_m0_express'], + 'samd5x_e5x': ['metro_m4_express'], + 'stm32h7': ['stm32h743eval'] } -- cgit v1.3.1 From 39b157d22f8a82264f07cdd31396c15b7a9c9e6d Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 Feb 2026 23:45:24 +0700 Subject: add membrowse-upload target and use it in ci after build --- .github/actions/get_deps/action.yml | 2 + .github/workflows/build.yml | 92 +++++++++++++++++----------------- .github/workflows/build_util.yml | 14 ++++-- .github/workflows/membrowse-report.yml | 1 - hw/bsp/family_support.cmake | 5 ++ tools/build.py | 13 +++-- 6 files changed, 73 insertions(+), 54 deletions(-) (limited to 'tools/build.py') diff --git a/.github/actions/get_deps/action.yml b/.github/actions/get_deps/action.yml index a84db893b..8ea36ce78 100644 --- a/.github/actions/get_deps/action.yml +++ b/.github/actions/get_deps/action.yml @@ -22,6 +22,8 @@ runs: NINJA_URL=https://github.com/ninja-build/ninja/releases/download/v1.13.1/ninja-linux.zip wget $NINJA_URL -O ninja-linux.zip unzip ninja-linux.zip -d ninja-bin + pip install membrowse + #echo >> $GITHUB_PATH "$HOME/.local/bin" echo >> $GITHUB_PATH "${{ github.workspace }}/ninja-bin" shell: bash diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 352875a9d..2b0c38c4f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -93,7 +93,9 @@ jobs: build-args: ${{ toJSON(fromJSON(needs.set-matrix.outputs.json)[matrix.toolchain]) }} build-options: '--one-first' upload-metrics: true - upload-artifacts: true + upload-artifacts: false + upload-membrowse: true + secrets: inherit code-metrics: needs: cmake @@ -337,47 +339,47 @@ jobs: # Push: always runs (uses identical for doc-only to maintain commit chain) # PR: only runs if code changed (doc-only PRs skip entirely) # --------------------------------------- - membrowse: - needs: [check-paths, cmake] - if: | - always() && !cancelled() && ( - github.event_name == 'push' || - github.event_name == 'release' || - github.event_name == 'workflow_dispatch' || - (github.event_name == 'pull_request' && needs.check-paths.outputs.code_changed == 'true') - ) - permissions: - contents: read - actions: read - uses: ./.github/workflows/membrowse-report.yml - with: - code_changed: ${{ needs.check-paths.outputs.code_changed == 'true' || github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} - secrets: inherit - - membrowse-comment: - needs: membrowse - # skip membrowse comment since it is too verbal - if: false && github.event_name == 'pull_request' - runs-on: ubuntu-latest - permissions: - contents: read - actions: read - steps: - - name: Checkout repository - uses: actions/checkout@v6 - - - name: Download report artifacts - id: download - uses: actions/download-artifact@v5 - with: - pattern: membrowse-report-* - path: reports - merge-multiple: true - continue-on-error: true - - - name: Upload Membrowse Comment Artifact - if: steps.download.outcome == 'success' - uses: actions/upload-artifact@v5 - with: - name: membrowse-comment - path: reports/ +# membrowse: +# needs: [check-paths, cmake] +# if: | +# always() && !cancelled() && ( +# github.event_name == 'push' || +# github.event_name == 'release' || +# github.event_name == 'workflow_dispatch' || +# (github.event_name == 'pull_request' && needs.check-paths.outputs.code_changed == 'true') +# ) +# permissions: +# contents: read +# actions: read +# uses: ./.github/workflows/membrowse-report.yml +# with: +# code_changed: ${{ needs.check-paths.outputs.code_changed == 'true' || github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} +# secrets: inherit +# +# membrowse-comment: +# needs: membrowse +# # skip membrowse comment since it is too verbal +# if: false && github.event_name == 'pull_request' +# runs-on: ubuntu-latest +# permissions: +# contents: read +# actions: read +# steps: +# - name: Checkout repository +# uses: actions/checkout@v6 +# +# - name: Download report artifacts +# id: download +# uses: actions/download-artifact@v5 +# with: +# pattern: membrowse-report-* +# path: reports +# merge-multiple: true +# continue-on-error: true +# +# - name: Upload Membrowse Comment Artifact +# if: steps.download.outcome == 'success' +# uses: actions/upload-artifact@v5 +# with: +# name: membrowse-comment +# path: reports/ diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index e62c10ca1..8a3dd8f91 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -24,6 +24,10 @@ on: required: false default: false type: boolean + upload-membrowse: + required: false + default: false + type: boolean os: required: false type: string @@ -54,15 +58,17 @@ jobs: - name: Build env: IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} + MEMBROWSE_API_KEY: ${{ secrets.MEMBROWSE_API_KEY }} + MEMBROWSE_UPLOAD_OPTION: ${{ inputs.upload-membrowse && '--membrowse-upload' || '' }} TOOLCHAIN: ${{ inputs.toolchain }} run: | if [ "$TOOLCHAIN" == "esp-idf" ]; then - docker run --rm -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py ${{ matrix.arg }} + docker run --rm -e MEMBROWSE_API_KEY="$MEMBROWSE_API_KEY" -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py $MEMBROWSE_UPLOAD_OPTION ${{ 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 }} ${{ inputs.build-options }} ${{ matrix.arg }} - python tools/build.py -s cmake ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} + python tools/build.py -s make $MEMBROWSE_UPLOAD_OPTION ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} + python tools/build.py -s cmake $MEMBROWSE_UPLOAD_OPTION ${{ 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 }} ${{ inputs.build-options }} ${{ matrix.arg }} + python tools/build.py -s ${{ inputs.build-system }} $MEMBROWSE_UPLOAD_OPTION ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} fi shell: bash diff --git a/.github/workflows/membrowse-report.yml b/.github/workflows/membrowse-report.yml index 0667418e6..f86b047df 100644 --- a/.github/workflows/membrowse-report.yml +++ b/.github/workflows/membrowse-report.yml @@ -38,7 +38,6 @@ jobs: uses: actions/checkout@v6 with: fetch-depth: 0 - submodules: recursive # Download artifacts when code changed (build artifacts available) - name: Download build artifacts diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake index 335b56c72..57a323c4a 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -298,6 +298,11 @@ ${MEMBROWSE_EXE} report ${OPTION} $ \"$ld_scripts\"") VERBATIM ) + if (NOT TARGET examples-membrowse-upload) + add_custom_target(examples-membrowse-upload) + endif () + add_dependencies(examples-membrowse-upload ${TARGET}-membrowse-upload) + set_property(TARGET ${TARGET}-membrowse PROPERTY FOLDER ${TARGET}) set_property(TARGET ${TARGET}-membrowse-upload PROPERTY FOLDER ${TARGET}) endif () diff --git a/tools/build.py b/tools/build.py index d22d06a0b..bc032fb91 100755 --- a/tools/build.py +++ b/tools/build.py @@ -106,7 +106,7 @@ def print_build_result(board, example, status, duration): # ----------------------------- # CMake # ----------------------------- -def cmake_board(board, build_args, build_flags_on): +def cmake_board(board, build_args, build_flags_on, membrowse_upload): ret = [0, 0, 0] start_time = time.monotonic() @@ -142,6 +142,8 @@ def cmake_board(board, build_args, build_flags_on): if rcmd.returncode == 0: ret[0] += 1 run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_metrics']) + if membrowse_upload: + run_cmd(["cmake", "--build", build_dir, '--target', 'examples-membrowse-upload']) # print(rcmd.stdout.decode("utf-8")) else: ret[1] += 1 @@ -198,13 +200,13 @@ def make_board(board, build_args): # ----------------------------- # Build Family # ----------------------------- -def build_boards_list(boards, build_defines, build_system, build_flags_on): +def build_boards_list(boards, build_defines, build_system, build_flags_on, membrowse_upload): ret = [0, 0, 0] for b in boards: r = [0, 0, 0] if build_system == 'cmake': build_args = [f'-D{d}' for d in build_defines] - r = cmake_board(b, build_args, build_flags_on) + r = cmake_board(b, build_args, build_flags_on, membrowse_upload) elif build_system == 'make': build_args = ' '.join(f'{d}' for d in build_defines) r = make_board(b, build_args) @@ -273,6 +275,8 @@ def main(): 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('--membrowse-upload', action='store_true', default=False, + help='Run examples-membrowse-upload target after successful CMake build') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') args = parser.parse_args() @@ -284,6 +288,7 @@ def main(): build_flags_on = args.build_flags_on one_random = args.one_random one_first = args.one_first + membrowse_upload = args.membrowse_upload verbose = args.verbose clean_build = args.clean parallel_jobs = args.jobs @@ -314,7 +319,7 @@ def main(): 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) + result = build_boards_list(all_boards, build_defines, build_system, build_flags_on, membrowse_upload) total_time = time.monotonic() - total_time print(build_separator) -- cgit v1.3.1 From 2e8e33f28494307276d7a5417569461fe6584b80 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 11 Feb 2026 16:34:28 +0700 Subject: add build target argument to improve flexibility of build scripts and workflows membrowse-upload upload with --identical if elf file does not exist --- .github/workflows/build.yml | 23 +++++++++-------- .github/workflows/build_util.yml | 38 +++++++++++++++++++--------- hw/bsp/family_support.cmake | 54 ++++++++++++++++++++++++++++++++++------ tools/build.py | 43 +++++++++++++------------------- 4 files changed, 103 insertions(+), 55 deletions(-) (limited to 'tools/build.py') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2b0c38c4f..412d52bb8 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,8 +47,6 @@ jobs: - '.github/workflows/ci_set_matrix.py' set-matrix: - needs: [ check-paths ] - if: needs.check-paths.outputs.code_changed == 'true' runs-on: ubuntu-latest outputs: json: ${{ steps.set-matrix-json.outputs.matrix }} @@ -75,7 +73,7 @@ jobs: # For Make and IAR build: will be done on CircleCI only (one random per family as well) # ------------------------------------------------------------------------------ cmake: - needs: set-matrix + needs: [ check-paths, set-matrix ] uses: ./.github/workflows/build_util.yml strategy: fail-fast: false @@ -95,10 +93,12 @@ jobs: upload-metrics: true upload-artifacts: false upload-membrowse: true + code-changed: ${{ needs.check-paths.outputs.code_changed == 'true' }} secrets: inherit code-metrics: - needs: cmake + needs: [ check-paths, cmake ] + if: needs.check-paths.outputs.code_changed == true runs-on: ubuntu-latest permissions: pull-requests: write @@ -196,17 +196,18 @@ jobs: # --------------------------------------- build-os: needs: [ check-paths ] - if: needs.check-paths.outputs.code_changed == 'true' + if: needs.check-paths.outputs.code_changed == true 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: 'cmake-make' + build-system: ${{ matrix.build-system }} toolchain: 'arm-gcc-${{ matrix.os }}' - build-args: '["stm32h7"]' + build-args: '["stm32h7rs"]' build-options: '--one-random' # --------------------------------------- @@ -215,7 +216,8 @@ jobs: zephyr: needs: [ check-paths ] # skip zephyr build due to failed build, fix later - if: false && needs.check-paths.outputs.code_changed == 'true' + if: false + #if: needs.check-paths.outputs.code_changed == 'true' runs-on: ubuntu-latest steps: - name: Checkout TinyUSB @@ -237,8 +239,8 @@ jobs: # Run on PR only (hil-tinyusb), hil-hfp only run on non-forked PR # --------------------------------------- hil-build: - needs: set-matrix - if: github.repository_owner == 'hathach' + needs: [ check-paths, set-matrix ] + if: needs.check-paths.outputs.code_changed == true && github.repository_owner == 'hathach' uses: ./.github/workflows/build_util.yml strategy: fail-fast: false @@ -299,6 +301,7 @@ jobs: hil-hfp: needs: [ check-paths ] if: | + needs.check-paths.outputs.code_changed == true && github.repository_owner == 'hathach' && !(github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == true) runs-on: [ self-hosted, Linux, X64, hifiphile ] diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index eb3c4df89..4200c11bd 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -3,6 +3,10 @@ name: Reusable build util on: workflow_call: inputs: + os: + required: false + type: string + default: 'ubuntu-latest' build-system: required: true type: string @@ -28,10 +32,10 @@ on: required: false default: false type: boolean - os: + code-changed: required: false - type: string - default: 'ubuntu-latest' + default: false + type: boolean jobs: family: @@ -58,19 +62,29 @@ jobs: arg: ${{ matrix.arg }} - name: Build + if: ${{ inputs.code-changed }} env: IAR_LMS_BEARER_TOKEN: ${{ secrets.IAR_LMS_BEARER_TOKEN }} - MEMBROWSE_API_KEY: ${{ secrets.MEMBROWSE_API_KEY }} - MEMBROWSE_UPLOAD_OPTION: ${{ inputs.upload-membrowse && '--membrowse-upload' || '' }} - TOOLCHAIN: ${{ inputs.toolchain }} run: | - if [ "$TOOLCHAIN" == "esp-idf" ]; then - docker run --rm -e MEMBROWSE_API_KEY="$MEMBROWSE_API_KEY" -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py $MEMBROWSE_UPLOAD_OPTION ${{ matrix.arg }} - elif [ "${{ inputs.build-system }}" == "cmake-make" ] || [ "${{ inputs.build-system }}" == "make-cmake" ]; then - python tools/build.py -s make $MEMBROWSE_UPLOAD_OPTION ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} - python tools/build.py -s cmake $MEMBROWSE_UPLOAD_OPTION ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} + if [ "${{ inputs.toolchain }}" == "esp-idf" ]; then + docker run --rm -e MEMBROWSE_API_KEY="$MEMBROWSE_API_KEY" -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py -T all ${{ matrix.arg }} else - python tools/build.py -s ${{ inputs.build-system }} $MEMBROWSE_UPLOAD_OPTION ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} ${{ matrix.arg }} + BUILD_PY_ARGS="-s ${{ inputs.build-system }} ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }}" + python tools/build.py $BUILD_PY_ARGS --target all ${{ matrix.arg }} + + if [ "${{ inputs.upload-metrics }}" = "true" ]; then + python tools/build.py $BUILD_PY_ARGS --target tinyusb_metrics ${{ matrix.arg }} + fi + fi + shell: bash + + - name: Membrowse Upload + if: inputs.toolchain != 'esp-idf' + env: + MEMBROWSE_API_KEY: ${{ secrets.MEMBROWSE_API_KEY }} + run: | + if [ "${{ inputs.upload-membrowse }}" = "true" ]; then + python tools/build.py $BUILD_PY_ARGS --target examples-membrowse-upload ${{ matrix.arg }} fi shell: bash diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake index 57a323c4a..fdf7b78ed 100644 --- a/hw/bsp/family_support.cmake +++ b/hw/bsp/family_support.cmake @@ -280,21 +280,62 @@ function(family_add_membrowse TARGET) string(APPEND OPTION " ${MEMBROWSE_OPTION}") endif () - # For Ninja generator, extract all linker scripts from Ninja commands and pass them to membrowse. + # For Ninja generator, extract all linker scripts from Ninja commands (with INCLUDE) and pass them to membrowse. if (CMAKE_GENERATOR MATCHES "Ninja") + set(TARGET_ELF_PATH "$/$") + set(MEMBROWSE_LD_SCRIPTS_CMD + "ld_scripts=\"$(${CMAKE_MAKE_PROGRAM} -C ${CMAKE_BINARY_DIR} -t commands ${TARGET} | grep -oP '(?:-Wl,--script=|-T\\s*)\\K[A-Za-z0-9_./-]+\\.ld' | xargs)\"; \ +all_ld_scripts=\"\"; \ +pending_ld_scripts=\"$ld_scripts\"; \ +while [ -n \"$pending_ld_scripts\" ]; do \ + next_pending=\"\"; \ + for script in $pending_ld_scripts; do \ + case \" $all_ld_scripts \" in *\" $script \"*) continue ;; esac; \ + all_ld_scripts=\"$all_ld_scripts $script\"; \ + script_dir=$(dirname \"$script\"); \ + include_scripts=$(grep -hoP '^\\s*INCLUDE\\s+[<\"]?\\K[^\">[:space:]]+\\.ld' \"$script\" 2>/dev/null | xargs); \ + for include_script in $include_scripts; do \ + resolved_script=\"\"; \ + if [ -f \"$include_script\" ]; then \ + resolved_script=\"$include_script\"; \ + elif [ -f \"$script_dir/$include_script\" ]; then \ + resolved_script=\"$script_dir/$include_script\"; \ + fi; \ + if [ -n \"$resolved_script\" ]; then \ + case \" $all_ld_scripts $next_pending \" in *\" $resolved_script \"*) ;; *) next_pending=\"$next_pending $resolved_script\" ;; esac; \ + fi; \ + done; \ + done; \ + pending_ld_scripts=\"$(echo \"$next_pending\" | xargs)\"; \ +done; \ +ld_scripts=\"$(echo \"$all_ld_scripts\" | xargs)\"") + set(MEMBROWSE_CMD - "ld_scripts=\"$(${CMAKE_MAKE_PROGRAM} -C ${CMAKE_BINARY_DIR} -t commands ${TARGET} | grep -oP '(?<=-Wl,--script=)[A-Za-z0-9_./-]+\\.ld' | xargs)\"; \ -${MEMBROWSE_EXE} report ${OPTION} $ \"$ld_scripts\"") + "if [ -f \"${TARGET_ELF_PATH}\" ]; then \ + ${MEMBROWSE_LD_SCRIPTS_CMD}; \ + echo ld_scripts=\"$ld_scripts\"; \ + if [ \"$MEMBROWSE_UPLOAD\" = \"1\" ]; then \ + ${MEMBROWSE_EXE} report ${OPTION} \"${TARGET_ELF_PATH}\" \"$ld_scripts\" --upload --github --target-name ${BOARD}-${TARGET} --api-key $ENV{MEMBROWSE_API_KEY}; \ + else \ + ${MEMBROWSE_EXE} report ${OPTION} \"${TARGET_ELF_PATH}\" \"$ld_scripts\"; \ + fi; \ +else \ + if [ \"$MEMBROWSE_UPLOAD\" = \"1\" ]; then \ + ${MEMBROWSE_EXE} report ${OPTION} --identical --upload --github --target-name ${BOARD}-${TARGET} --api-key $ENV{MEMBROWSE_API_KEY}; \ + else \ + ${MEMBROWSE_EXE} report ${OPTION} --identical; \ + fi; \ +fi") add_custom_target(${TARGET}-membrowse DEPENDS ${TARGET} - COMMAND bash -lc "${MEMBROWSE_CMD}" + COMMAND ${CMAKE_COMMAND} -E env MEMBROWSE_UPLOAD=0 bash -lc "${MEMBROWSE_CMD}" VERBATIM ) + set_property(TARGET ${TARGET}-membrowse PROPERTY FOLDER ${TARGET}) add_custom_target(${TARGET}-membrowse-upload - DEPENDS ${TARGET} - COMMAND bash -lc "${MEMBROWSE_CMD} --upload --github --target-name ${BOARD}-${TARGET} --api-key $ENV{MEMBROWSE_API_KEY}" + COMMAND ${CMAKE_COMMAND} -E env MEMBROWSE_UPLOAD=1 bash -lc "${MEMBROWSE_CMD}" VERBATIM ) @@ -303,7 +344,6 @@ ${MEMBROWSE_EXE} report ${OPTION} $ \"$ld_scripts\"") endif () add_dependencies(examples-membrowse-upload ${TARGET}-membrowse-upload) - set_property(TARGET ${TARGET}-membrowse PROPERTY FOLDER ${TARGET}) set_property(TARGET ${TARGET}-membrowse-upload PROPERTY FOLDER ${TARGET}) endif () endfunction() diff --git a/tools/build.py b/tools/build.py index bc032fb91..d26028c51 100755 --- a/tools/build.py +++ b/tools/build.py @@ -98,15 +98,15 @@ def get_examples(family): return all_examples -def print_build_result(board, example, status, duration): +def print_build_result(board, build_target, status, duration): if isinstance(duration, (int, float)): duration = "{:.2f}s".format(duration) - print(build_format.format(board, example, build_status[status], duration)) + print(build_format.format(board, build_target, build_status[status], duration)) # ----------------------------- # CMake # ----------------------------- -def cmake_board(board, build_args, build_flags_on, membrowse_upload): +def cmake_board(board, build_args, build_flags_on, build_target): ret = [0, 0, 0] start_time = time.monotonic() @@ -137,26 +137,18 @@ def cmake_board(board, build_args, build_flags_on, membrowse_upload): if rcmd.returncode == 0: if clean_build: run_cmd(["cmake", "--build", build_dir, '--target', 'clean']) - cmd = ["cmake", "--build", build_dir, '--parallel', str(parallel_jobs)] + cmd = ["cmake", "--build", build_dir, '--target', build_target, '--parallel', str(parallel_jobs)] rcmd = run_cmd(cmd) - if rcmd.returncode == 0: - ret[0] += 1 - run_cmd(["cmake", "--build", build_dir, '--target', 'tinyusb_metrics']) - if membrowse_upload: - run_cmd(["cmake", "--build", build_dir, '--target', 'examples-membrowse-upload']) - # print(rcmd.stdout.decode("utf-8")) - else: - ret[1] += 1 + ret[0 if rcmd.returncode == 0 else 1] += 1 - example = 'all' - print_build_result(board, example, 0 if ret[1] == 0 else 1, time.monotonic() - start_time) + print_build_result(board, build_target, 0 if ret[1] == 0 else 1, time.monotonic() - start_time) return ret # ----------------------------- # Make # ----------------------------- -def make_one_example(example, board, make_option): +def make_one_example(example, board, make_option, build_target): # Check if board is skipped if build_utils.skip_example(example, board): print_build_result(board, example, 2, '-') @@ -168,7 +160,7 @@ def make_one_example(example, board, make_option): make_args += shlex.split(make_option) if clean_build: run_cmd(make_args + ["clean"]) - build_result = run_cmd(make_args + ['all']) + build_result = run_cmd(make_args + [build_target]) r = 0 if build_result.returncode == 0 else 1 print_build_result(board, example, r, time.monotonic() - start_time) @@ -177,7 +169,7 @@ def make_one_example(example, board, make_option): return ret -def make_board(board, build_args): +def make_board(board, build_args, build_target): print(build_separator) family = find_family(board); all_examples = get_examples(family) @@ -188,7 +180,7 @@ def make_board(board, build_args): final_status = 2 else: with Pool(processes=os.cpu_count()) as pool: - pool_args = list((map(lambda e, b=board, o=f"{build_args}": [e, b, o], all_examples))) + pool_args = list((map(lambda e, b=board, o=f"{build_args}", t=build_target: [e, b, o, t], all_examples))) r = pool.starmap(make_one_example, pool_args) # sum all element of same index (column sum) ret = list(map(sum, list(zip(*r)))) @@ -200,16 +192,16 @@ def make_board(board, build_args): # ----------------------------- # Build Family # ----------------------------- -def build_boards_list(boards, build_defines, build_system, build_flags_on, membrowse_upload): +def build_boards_list(boards, build_defines, build_system, build_flags_on, build_target): ret = [0, 0, 0] for b in boards: r = [0, 0, 0] if build_system == 'cmake': build_args = [f'-D{d}' for d in build_defines] - r = cmake_board(b, build_args, build_flags_on, membrowse_upload) + r = cmake_board(b, build_args, build_flags_on, build_target) elif build_system == 'make': build_args = ' '.join(f'{d}' for d in build_defines) - r = make_board(b, build_args) + r = make_board(b, build_args, build_target) ret[0] += r[0] ret[1] += r[1] ret[2] += r[2] @@ -275,8 +267,7 @@ def main(): 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('--membrowse-upload', action='store_true', default=False, - help='Run examples-membrowse-upload target after successful CMake build') + parser.add_argument('-T', '--target', default='all', help='Build target to use, default is all') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') args = parser.parse_args() @@ -288,7 +279,7 @@ def main(): build_flags_on = args.build_flags_on one_random = args.one_random one_first = args.one_first - membrowse_upload = args.membrowse_upload + build_target = args.target verbose = args.verbose clean_build = args.clean parallel_jobs = args.jobs @@ -300,7 +291,7 @@ def main(): return 1 print(build_separator) - print(build_format.format('Board', 'Example', '\033[39mResult\033[0m', 'Time')) + print(build_format.format('Board', 'Target', '\033[39mResult\033[0m', 'Time')) total_time = time.monotonic() # get all families @@ -319,7 +310,7 @@ def main(): 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, membrowse_upload) + result = build_boards_list(all_boards, build_defines, build_system, build_flags_on, build_target) total_time = time.monotonic() - total_time print(build_separator) -- cgit v1.3.1 From 8a6012b0096cb71bb4cfaaa36813de4ac5003c18 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 12 Feb 2026 13:32:00 +0700 Subject: refactor build scripts to support multiple build targets and improve argument handling --- .circleci/config2.yml | 7 +++--- .github/workflows/build_util.yml | 7 +++--- tools/build.py | 50 ++++++++++++++++++++-------------------- 3 files changed, 31 insertions(+), 33 deletions(-) (limited to 'tools/build.py') diff --git a/.circleci/config2.yml b/.circleci/config2.yml index 900994a73..a31b0a818 100644 --- a/.circleci/config2.yml +++ b/.circleci/config2.yml @@ -124,12 +124,11 @@ commands: # circleci docker return $nproc as 36 core, limit parallel to 4 (resource-class = large) # Required for IAR, also prevent crashed/killed by docker - BUILD_PY_ARGS="-s << parameters.build-system >> $TOOLCHAIN_OPTION -j 4 << parameters.build-args >>" - python tools/build.py $BUILD_PY_ARGS --target all << parameters.family >> - + BUILD_PY_ARGS="-s << parameters.build-system >> $TOOLCHAIN_OPTION -j 4 << parameters.build-args >> --target all" if [ << parameters.build-system >> == "cmake" ]; then - python tools/build.py $BUILD_PY_ARGS --target tinyusb_metrics << parameters.family >> + BUILD_PY_ARGS="$BUILD_PY_ARGS --target tinyusb_metrics" fi + python tools/build.py $BUILD_PY_ARGS << parameters.family >> fi # Only collect and persist metrics for cmake builds (excluding esp-idf and --one-random) diff --git a/.github/workflows/build_util.yml b/.github/workflows/build_util.yml index b0adec979..6863ebdf2 100644 --- a/.github/workflows/build_util.yml +++ b/.github/workflows/build_util.yml @@ -69,12 +69,11 @@ jobs: if [ "${{ inputs.toolchain }}" == "esp-idf" ]; then docker run --rm -e MEMBROWSE_API_KEY="$MEMBROWSE_API_KEY" -v $PWD:/project -w /project espressif/idf:tinyusb python tools/build.py --target all ${{ matrix.arg }} else - BUILD_PY_ARGS="-s ${{ inputs.build-system }} ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }}" - python tools/build.py $BUILD_PY_ARGS --target all ${{ matrix.arg }} - + BUILD_PY_ARGS="-s ${{ inputs.build-system }} ${{ steps.setup-toolchain.outputs.build_option }} ${{ inputs.build-options }} --target all" if [ "${{ inputs.upload-metrics }}" = "true" ]; then - python tools/build.py $BUILD_PY_ARGS --target tinyusb_metrics ${{ matrix.arg }} + BUILD_PY_ARGS="$BUILD_PY_ARGS --target tinyusb_metrics" fi + python tools/build.py $BUILD_PY_ARGS ${{ matrix.arg }} fi shell: bash diff --git a/tools/build.py b/tools/build.py index d26028c51..3c5c3c077 100755 --- a/tools/build.py +++ b/tools/build.py @@ -24,7 +24,6 @@ build_separator = '-' * 95 build_status = [STATUS_OK, STATUS_FAILED, STATUS_SKIPPED] verbose = False -clean_build = False parallel_jobs = os.cpu_count() # CI board control lists (used when running under CI) @@ -106,7 +105,7 @@ def print_build_result(board, build_target, status, duration): # ----------------------------- # CMake # ----------------------------- -def cmake_board(board, build_args, build_flags_on, build_target): +def cmake_board(board, build_args, build_flags_on, build_targets): ret = [0, 0, 0] start_time = time.monotonic() @@ -135,33 +134,36 @@ def cmake_board(board, build_args, build_flags_on, build_target): 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, '--target', build_target, '--parallel', str(parallel_jobs)] - rcmd = run_cmd(cmd) + cmd = ["cmake", "--build", build_dir, '--parallel', str(parallel_jobs)] + for target in build_targets: + rcmd = run_cmd(cmd + ['--target', target]) + if rcmd.returncode != 0: + break ret[0 if rcmd.returncode == 0 else 1] += 1 - print_build_result(board, build_target, 0 if ret[1] == 0 else 1, time.monotonic() - start_time) + print_build_result(board, ','.join(build_targets), 0 if ret[1] == 0 else 1, time.monotonic() - start_time) return ret # ----------------------------- # Make # ----------------------------- -def make_one_example(example, board, make_option, build_target): +def make_one_example(example, board, make_option, build_targets): # Check if board is skipped if build_utils.skip_example(example, board): print_build_result(board, example, 2, '-') r = 2 else: start_time = time.monotonic() - make_args = ["make", "-C", f"examples/{example}", f"BOARD={board}", '-j', str(parallel_jobs)] + make_cmd = ["make", "-C", f"examples/{example}", f"BOARD={board}", '-j', str(parallel_jobs)] if make_option: - make_args += shlex.split(make_option) - if clean_build: - run_cmd(make_args + ["clean"]) - build_result = run_cmd(make_args + [build_target]) - r = 0 if build_result.returncode == 0 else 1 + make_cmd += shlex.split(make_option) + r = 0 + for target in build_targets: + build_result = run_cmd(make_cmd + [target]) + if build_result.returncode != 0: + r = 1 + break print_build_result(board, example, r, time.monotonic() - start_time) ret = [0, 0, 0] @@ -169,7 +171,7 @@ def make_one_example(example, board, make_option, build_target): return ret -def make_board(board, build_args, build_target): +def make_board(board, build_args, build_targets): print(build_separator) family = find_family(board); all_examples = get_examples(family) @@ -180,7 +182,7 @@ def make_board(board, build_args, build_target): final_status = 2 else: with Pool(processes=os.cpu_count()) as pool: - pool_args = list((map(lambda e, b=board, o=f"{build_args}", t=build_target: [e, b, o, t], all_examples))) + pool_args = list((map(lambda e, b=board, o=f"{build_args}", t=build_targets: [e, b, o, t], all_examples))) r = pool.starmap(make_one_example, pool_args) # sum all element of same index (column sum) ret = list(map(sum, list(zip(*r)))) @@ -192,16 +194,16 @@ def make_board(board, build_args, build_target): # ----------------------------- # Build Family # ----------------------------- -def build_boards_list(boards, build_defines, build_system, build_flags_on, build_target): +def build_boards_list(boards, build_defines, build_system, build_flags_on, build_targets): ret = [0, 0, 0] for b in boards: r = [0, 0, 0] if build_system == 'cmake': build_args = [f'-D{d}' for d in build_defines] - r = cmake_board(b, build_args, build_flags_on, build_target) + r = cmake_board(b, build_args, build_flags_on, build_targets) elif build_system == 'make': build_args = ' '.join(f'{d}' for d in build_defines) - r = make_board(b, build_args, build_target) + r = make_board(b, build_args, build_targets) ret[0] += r[0] ret[1] += r[1] ret[2] += r[2] @@ -251,13 +253,11 @@ def get_family_boards(family, one_random, one_first): # ----------------------------- 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') @@ -267,7 +267,8 @@ def main(): 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('-T', '--target', default='all', help='Build target to use, default is all') + parser.add_argument('-T', '--target', action='append', default=[], + help='Build target to use, may be specified multiple times (default: all)') parser.add_argument('-v', '--verbose', action='store_true', help='Verbose output') args = parser.parse_args() @@ -279,9 +280,8 @@ def main(): build_flags_on = args.build_flags_on one_random = args.one_random one_first = args.one_first - build_target = args.target + build_targets = args.target if args.target else ['all'] verbose = args.verbose - clean_build = args.clean parallel_jobs = args.jobs build_defines.append(f'TOOLCHAIN={toolchain}') @@ -310,7 +310,7 @@ def main(): 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, build_target) + result = build_boards_list(all_boards, build_defines, build_system, build_flags_on, build_targets) total_time = time.monotonic() - total_time print(build_separator) -- cgit v1.3.1 From 6f35e76667f4015ef429ace5730e20cc0037e042 Mon Sep 17 00:00:00 2001 From: Ha Thach Date: Thu, 11 Jun 2026 08:16:43 +0700 Subject: HIL: replace build.flags_on with named build variants (#3687) * test/hil: replace build.flags_on with named variant schema Boards declare build variants as `variant: [{name, flags}]` instead of `build.flags_on`. The variant `name` is the build dir (cmake-build-) and the HIL report row; `flags` is the raw CFLAGS string (-D...=1) injected via CFLAGS_CLI. No `variant` => a single build named after the board. - build.py: --build-name (dir) + --cflag= (raw CFLAGS, repeatable, =form survives the matrix's shell word-splitting); drop -f1/CFLAGS wrapping. - hil_ci_set_matrix.py: emit one build arg per variant. - hil_test.py: iterate variants; report row + build dir = variant name. - hil_ci.sh: copy all cmake-build-* dirs for -b runs. - get_deps.py: accept (ignore) --build-name/--cflag from matrix args. - tinyusb.json: migrate all 6 flags_on boards to variant. * board_test: park CI build with busy spin instead of wfe --- .github/workflows/build.yml | 10 ++++- examples/device/board_test/src/main.c | 50 ++++++++++--------------- test/hil/hfp.json | 4 ++ test/hil/hil_ci.sh | 39 +++++++++++++++++--- test/hil/hil_ci_set_matrix.py | 26 ++++++------- test/hil/hil_test.py | 69 ++++++++++++++++++----------------- test/hil/tinyusb.json | 58 ++++++++++++----------------- tools/build.py | 30 +++++++++------ tools/get_deps.py | 2 + 9 files changed, 157 insertions(+), 131 deletions(-) (limited to 'tools/build.py') diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e22ba909c..a7c7cf99a 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -397,7 +397,15 @@ jobs: run: python3 tools/get_deps.py $BUILD_ARGS - name: Build - run: python3 tools/build.py --toolchain iar $BUILD_ARGS + run: | + # Each variant carries its own --build-name/--cflag, which are global to a + # single build.py invocation — so build one matrix entry at a time rather + # than joining them (joining would leak a variant's flags onto every board). + readarray -t ENTRIES < <(python test/hil/hil_ci_set_matrix.py test/hil/hfp.json | jq -r '.["arm-gcc"][]') + for entry in "${ENTRIES[@]}"; do + echo "+ tools/build.py --toolchain iar $entry" + python3 tools/build.py --toolchain iar $entry + done - name: Test on actual hardware (hardware in the loop) run: | diff --git a/examples/device/board_test/src/main.c b/examples/device/board_test/src/main.c index 3d8cf9979..96dc1bd30 100644 --- a/examples/device/board_test/src/main.c +++ b/examples/device/board_test/src/main.c @@ -57,8 +57,16 @@ void tusb_time_delay_ms_api(uint32_t ms) { // CI_BUILD (defined for all CI builds, see hw/bsp/family_support.cmake) skips the // blink/echo loop below: after HIL tests, this firmware is flashed to park the // board in a quiet, low-power idle state (no USB, LED, or UART activity). -#ifndef CI_BUILD +#ifdef CI_BUILD +int main(void) { + while (1) { + #if defined(ESP_PLATFORM) + vTaskDelay(portMAX_DELAY); + #endif + } +} +#else // Task parameter type: ULONG for ThreadX, void* for FreeRTOS and noos #if CFG_TUSB_OS == OPT_OS_THREADX #define RTOS_PARAM ULONG @@ -112,46 +120,21 @@ static void board_test_loop(RTOS_PARAM param) { } } -#endif // CI_BUILD - int main(void) { -#ifdef CI_BUILD - // Park the board in a quiet, low-power idle loop. board_init() is intentionally - // skipped: no clocks, peripherals, USB, LED, or UART are brought up, so the MCU - // just idles after CI flashes this over a board's previous test firmware. - while (1) { - #if defined(ESP_PLATFORM) - vTaskDelay(portMAX_DELAY); // ESP runs FreeRTOS: yield this task indefinitely - #elif defined(__ARM_ARCH) || defined(__arm__) - __asm volatile("wfe"); // Cortex-M: sleep until an event - #else - // other architectures (e.g. RISC-V): spin - #endif - } - // no return: the loop never exits (an unreachable return trips IAR's Pe111) -#else board_init(); board_led_write(true); - #if CFG_TUSB_OS == OPT_OS_FREERTOS +#if CFG_TUSB_OS == OPT_OS_FREERTOS freertos_init(); - #elif CFG_TUSB_OS == OPT_OS_THREADX +#elif CFG_TUSB_OS == OPT_OS_THREADX tx_kernel_enter(); - #else +#else board_test_loop(NULL); - #endif - - return 0; #endif -} -#ifdef ESP_PLATFORM -void app_main(void) { - main(); + return 0; } -#endif -#ifndef CI_BUILD //--------------------------------------------------------------------+ // FreeRTOS //--------------------------------------------------------------------+ @@ -197,5 +180,10 @@ void tx_application_define(void *first_unused_memory) { 1, 1, TX_NO_TIME_SLICE, TX_AUTO_START); } #endif - #endif // CI_BUILD + +#ifdef ESP_PLATFORM +void app_main(void) { + main(); +} +#endif diff --git a/test/hil/hfp.json b/test/hil/hfp.json index 8ba7a8f44..bb146d2fc 100644 --- a/test/hil/hfp.json +++ b/test/hil/hfp.json @@ -15,6 +15,10 @@ { "name": "stm32f746disco", "uid": "210041000C51343237303334", + "variant": [ + { "name": "stm32f746disco", "flags": "" }, + { "name": "stm32f746disco-DMA", "flags": "-DCFG_TUD_DWC2_DMA_ENABLE=1 -DCFG_TUH_DWC2_DMA_ENABLE=1" } + ], "tests": { "device": true, "host": false, "dual": false }, diff --git a/test/hil/hil_ci.sh b/test/hil/hil_ci.sh index 4f68ed067..3ec907979 100644 --- a/test/hil/hil_ci.sh +++ b/test/hil/hil_ci.sh @@ -66,14 +66,41 @@ copy_board_binaries() { } if [ -n "$BOARD" ]; then - BUILD_DIR="$ROOT_DIR/examples/cmake-build-$BOARD" - if [ ! -d "$BUILD_DIR" ]; then - echo "Error: build directory not found: $BUILD_DIR" - echo "Build first with: cd examples && cmake -DBOARD=$BOARD -G Ninja -B cmake-build-$BOARD . && cmake --build cmake-build-$BOARD" + # Copy the board's build dir plus its variant dirs. Variant names come from + # $CONFIG (they are not required to be prefixed with the board name); the + # cmake-build--* glob is kept as a fallback for ad-hoc local builds. + # Collect only dirs that actually exist, deduplicated. + declare -A SEEN_DIRS=() + BUILD_DIRS=() + add_build_dir() { + [[ -d "$1" && -z "${SEEN_DIRS[$1]:-}" ]] || return 0 + SEEN_DIRS[$1]=1 + BUILD_DIRS+=("$1") + } + shopt -s nullglob + for d in "$ROOT_DIR"/examples/cmake-build-"$BOARD" "$ROOT_DIR"/examples/cmake-build-"$BOARD"-*; do + add_build_dir "$d" + done + shopt -u nullglob + while IFS= read -r v; do + add_build_dir "$ROOT_DIR/examples/cmake-build-$v" + done < <(python3 -c ' +import json, sys +cfg = json.load(open(sys.argv[1])) +for b in cfg.get("boards", []): + if b["name"] == sys.argv[2]: + for v in b.get("variant") or []: + print(v["name"]) +' "$CONFIG" "$BOARD") + if [ ${#BUILD_DIRS[@]} -eq 0 ]; then + echo "Error: no build directory found for $BOARD under $ROOT_DIR/examples/" + echo "Build first with: cd examples && cmake --preset $BOARD && cmake --build --preset $BOARD" exit 1 fi - echo "==> Copying binaries for $BOARD" - copy_board_binaries "$BUILD_DIR" + echo "==> Copying binaries for $BOARD (${#BUILD_DIRS[@]} build dir(s))" + for d in "${BUILD_DIRS[@]}"; do + copy_board_binaries "$d" + done else echo "==> Copying all built binaries" # Use `%/` parameter expansion to strip the trailing slash from the glob — diff --git a/test/hil/hil_ci_set_matrix.py b/test/hil/hil_ci_set_matrix.py index 2cce35ae2..baa24afb1 100644 --- a/test/hil/hil_ci_set_matrix.py +++ b/test/hil/hil_ci_set_matrix.py @@ -44,19 +44,19 @@ def main(): toolchain = 'arm-gcc' build_board = f'-b {name}' - if 'build' in board: - if 'args' in board['build']: - build_board += ' ' + ' '.join(f'-D{a}' for a in board['build']['args']) - if 'flags_on' in board['build']: - for f in board['build']['flags_on']: - if f == '': - append_build_arg(toolchain, build_board) - else: - append_build_arg(toolchain, f'{build_board} -f1 {f.replace(" ", " -f1 ")}') - else: - append_build_arg(toolchain, build_board) - else: - append_build_arg(toolchain, build_board) + if 'build' in board and 'args' in board['build']: + build_board += ' ' + ' '.join(f'-D{a}' for a in board['build']['args']) + + # Each variant builds into cmake-build- with its raw CFLAGS. + # No 'variant' -> a single build named after the board. + variants = board.get('variant') or [{'name': name, 'flags': ''}] + for v in variants: + arg = build_board + if v['name'] != name: + arg += f' --build-name {v["name"]}' + for tok in v.get('flags', '').split(): + arg += f' --cflag={tok}' + append_build_arg(toolchain, arg) print(json.dumps(matrix)) diff --git a/test/hil/hil_test.py b/test/hil/hil_test.py index 45bad7a45..da13fcbaf 100755 --- a/test/hil/hil_test.py +++ b/test/hil/hil_test.py @@ -122,16 +122,21 @@ class TestsCfg(TypedDict, total=False): class BuildCfg(TypedDict, total=False): - flags_on: list[str] args: list[str] +class VariantCfg(TypedDict, total=False): + name: str # build dir (cmake-build-) and HIL report row + flags: str # raw CFLAGS, e.g. "-DCFG_TUD_DWC2_DMA_ENABLE=1" + + class Board(TypedDict): name: str uid: str tests: TestsCfg flasher: FlasherCfg build: NotRequired[BuildCfg] + variant: NotRequired[list[VariantCfg]] class HilConfig(TypedDict): @@ -223,7 +228,9 @@ def open_serial_dev(port: str): while timeout > 0: if os.path.exists(port): try: - ser = serial.Serial(port, baudrate=115200, timeout=5) + # write_timeout: a wedged device otherwise blocks ser.write() forever, + # hanging the worker until the pool/job timeout kills the whole run + ser = serial.Serial(port, baudrate=115200, timeout=5, write_timeout=5) break except serial.SerialException: print(f'serial {port} not reaady {timeout} sec') @@ -976,9 +983,9 @@ def test_device_cdc_msc_throughput(board): pass print(f' CDC read {cdc_r} write {cdc_w}, MSC read {msc_r} write {msc_w} ', end='') - # compact read/write speed for the report cell, e.g. "C 652k/422k M 1.1M/783k" + # compact read/write speed for the report cell, e.g. "✅ CDC 652k/422k MSC 1.1M/783k" short = lambda s: (s.split()[0].rstrip('0').rstrip('.') + s.split()[-1][0]) if ' ' in s else s - return f'C {short(cdc_r)}/{short(cdc_w)} M {short(msc_r)}/{short(msc_w)}' + return f'{REPORT_CELL["pass"]} CDC {short(cdc_r)}/{short(cdc_w)} MSC {short(msc_r)}/{short(msc_w)}' def test_device_dfu(board): @@ -1502,17 +1509,12 @@ host_test = [ ] -def f1_suffix(f1: str) -> str: - """Build dir / row-label suffix for a flags-on variant ('' for the default).""" - return '-f1_' + f1.replace(' ', '_') if f1 else '' - - -def find_firmware(name: str, f1: str, example: str): +def find_firmware(variant: str, example: str): """Locate a built example's firmware base path (no extension) under - cmake-build-[-f1_...]//. Accepts the single-config layout - (firmware directly in the example dir) or Ninja Multi-Config (a per-config - subdir like RelWithDebInfo/). Returns the base Path, or None if not built.""" - fw_dir = TINYUSB_ROOT / build_dir / f'cmake-build-{name}{f1_suffix(f1)}' / example + cmake-build-//. Accepts the single-config layout (firmware + directly in the example dir) or Ninja Multi-Config (a per-config subdir like + RelWithDebInfo/). Returns the base Path, or None if not built.""" + fw_dir = TINYUSB_ROOT / build_dir / f'cmake-build-{variant}' / example base = Path(example).name if fw_dir.is_dir(): for cand in [fw_dir / base, fw_dir / 'RelWithDebInfo' / base, @@ -1522,25 +1524,24 @@ def find_firmware(name: str, f1: str, example: str): return None -def test_example(board: Board, f1: str, example: str) -> tuple[int, str]: +def test_example(board: Board, variant: str, example: str) -> tuple[int, str]: """ Test example firmware :param board: board dict - :param f1: flags on + :param variant: build variant name = build dir (cmake-build-) and report row :param example: example name :return: (err_count, status, metric) where err_count is 0 on success/skip or 1 on failure, status is one of 'pass'/'fail'/'skip' (a missing binary counts as 'skip'), and metric is an optional string a test returns to show in its report cell instead of the pass symbol (e.g. speed) """ - name = board['name'] err_count = 0 result_status = 'fail' metric = None - test_name = f'{name + f1_suffix(f1):40} {example:30} ...' + test_name = f'{variant:40} {example:30} ...' - fw_name = find_firmware(name, f1, example) + fw_name = find_firmware(variant, example) if fw_name is None: log_line(f'{test_name} Skip (no binary)') return 0, 'skip', None @@ -1619,21 +1620,22 @@ def test_example(board: Board, f1: str, example: str) -> tuple[int, str]: def build_board(board: Board) -> tuple[str, int]: """Build firmware for this board via tools/build.py. - Honors board config's build.flags_on variants and build.args defines. - Output goes to cmake-build/cmake-build-BOARD[-f1_...]/ (tools/build.py layout).""" + Honors board config's variant list and build.args defines. + Output goes to cmake-build/cmake-build-/ (tools/build.py layout).""" name = board['name'] bcfg = cast(BuildCfg, board.get('build', {})) - flags_on_list = bcfg.get('flags_on', ['']) extra_defs = bcfg.get('args', []) + variants = board.get('variant') or [{'name': name, 'flags': ''}] failed = 0 - for f1 in flags_on_list: + for v in variants: cmd = [sys.executable, str(TINYUSB_ROOT / 'tools' / 'build.py'), '-b', name] for d in extra_defs: cmd += ['-D', d] - if f1: - for flag in f1.split(): - cmd += ['-f1', flag] + if v['name'] != name: + cmd += ['--build-name', v['name']] + for tok in v.get('flags', '').split(): + cmd += [f'--cflag={tok}'] if verbose: cmd.append('-v') print(f' + {" ".join(cmd)}') @@ -1684,25 +1686,24 @@ def test_board(board: Board) -> tuple[str, int, list[str], list]: err_count = 0 failed_tests = [] - rows = [] # list of (row_label, {example: status}) — one row per board[-f1] variant - flags_on_list = [""] - if 'build' in board and 'flags_on' in board['build']: - flags_on_list = board['build']['flags_on'] + rows = [] # list of (row_label, {example: status}) — one row per build variant + variants = board.get('variant') or [{'name': name, 'flags': ''}] - for f1 in flags_on_list: + for v in variants: + vname = v['name'] cells = {} for test in test_list: - ec, status, metric = test_example(board, f1, test) + ec, status, metric = test_example(board, vname, test) err_count += ec cells[test] = metric if metric else status if ec > 0: failed_tests.append(test) - rows.append((name + f1_suffix(f1), cells)) + rows.append((vname, cells)) # flash board_test last to disable board's usb (skipped when --skip-flash is set); # this is teardown/park, not a test — not recorded in the report if not skip_flash: - test_example(board, flags_on_list[0], 'device/board_test') + test_example(board, variants[0]['name'], 'device/board_test') return name, err_count, sorted(set(failed_tests)), rows diff --git a/test/hil/tinyusb.json b/test/hil/tinyusb.json index 319ee9a79..afe3c4d03 100644 --- a/test/hil/tinyusb.json +++ b/test/hil/tinyusb.json @@ -17,12 +17,10 @@ { "name": "espressif_p4_function_ev", "uid": "6055F9F98715", - "build": { - "flags_on": [ - "", - "CFG_TUD_DWC2_DMA_ENABLE CFG_TUH_DWC2_DMA_ENABLE" - ] - }, + "variant": [ + { "name": "espressif_p4_function_ev", "flags": "" }, + { "name": "espressif_p4_function_ev-DMA", "flags": "-DCFG_TUD_DWC2_DMA_ENABLE=1 -DCFG_TUH_DWC2_DMA_ENABLE=1" } + ], "tests": { "only": [ "device/cdc_msc_freertos", @@ -58,12 +56,10 @@ { "name": "espressif_s3_devkitm", "uid": "84F703C084E4", - "build": { - "flags_on": [ - "", - "CFG_TUD_DWC2_DMA_ENABLE CFG_TUH_DWC2_DMA_ENABLE" - ] - }, + "variant": [ + { "name": "espressif_s3_devkitm", "flags": "" }, + { "name": "espressif_s3_devkitm-DMA", "flags": "-DCFG_TUD_DWC2_DMA_ENABLE=1 -DCFG_TUH_DWC2_DMA_ENABLE=1" } + ], "tests": { "only": [ "device/cdc_msc_freertos", @@ -226,11 +222,9 @@ { "name": "raspberry_pi_pico", "uid": "E6614C311B764A37", - "build": { - "flags_on": [ - "CFG_TUH_RPI_PIO_USB" - ] - }, + "variant": [ + { "name": "raspberry_pi_pico", "flags": "-DCFG_TUH_RPI_PIO_USB=1" } + ], "tests": { "device": true, "host": true, @@ -374,12 +368,10 @@ { "name": "stm32f723disco", "uid": "460029001951373031313335", - "build": { - "flags_on": [ - "", - "CFG_TUH_DWC2_DMA_ENABLE" - ] - }, + "variant": [ + { "name": "stm32f723disco", "flags": "" }, + { "name": "stm32f723disco-DMA", "flags": "-DCFG_TUH_DWC2_DMA_ENABLE=1" } + ], "tests": { "device": true, "host": true, @@ -410,12 +402,10 @@ { "name": "stm32h743nucleo", "uid": "110018000951383432343236", - "build": { - "flags_on": [ - "", - "CFG_TUD_DWC2_DMA_ENABLE" - ] - }, + "variant": [ + { "name": "stm32h743nucleo", "flags": "" }, + { "name": "stm32h743nucleo-DMA", "flags": "-DCFG_TUD_DWC2_DMA_ENABLE=1 -DCFG_TUH_DWC2_DMA_ENABLE=1" } + ], "tests": { "device": true, "host": false, @@ -474,12 +464,10 @@ { "name": "stm32f769disco", "uid": "21002F000F51363531383437", - "build": { - "flags_on": [ - "", - "CFG_TUD_DWC2_DMA_ENABLE" - ] - }, + "variant": [ + { "name": "stm32f769disco", "flags": "" }, + { "name": "stm32f769disco-DMA", "flags": "-DCFG_TUD_DWC2_DMA_ENABLE=1 -DCFG_TUH_DWC2_DMA_ENABLE=1" } + ], "tests": { "device": true, "host": false, diff --git a/tools/build.py b/tools/build.py index 3c5c3c077..86bc30d28 100755 --- a/tools/build.py +++ b/tools/build.py @@ -105,16 +105,14 @@ def print_build_result(board, build_target, status, duration): # ----------------------------- # CMake # ----------------------------- -def cmake_board(board, build_args, build_flags_on, build_targets): +def cmake_board(board, build_args, build_name, build_cflags, build_targets): ret = [0, 0, 0] start_time = time.monotonic() - build_dir = f'cmake-build/cmake-build-{board}' + build_dir = f'cmake-build/cmake-build-{build_name or board}' build_flags = [] - if len(build_flags_on) > 0: - cli_flags = ' '.join(f'-D{flag}=1' for flag in build_flags_on) - build_flags.append(f'-DCFLAGS_CLI={cli_flags}') - build_dir += '-f1_' + '_'.join(build_flags_on) + if build_cflags: + build_flags.append('-DCFLAGS_CLI=' + ' '.join(build_cflags)) family = find_family(board) if family == 'espressif': @@ -194,13 +192,13 @@ def make_board(board, build_args, build_targets): # ----------------------------- # Build Family # ----------------------------- -def build_boards_list(boards, build_defines, build_system, build_flags_on, build_targets): +def build_boards_list(boards, build_defines, build_system, build_name, build_cflags, build_targets): ret = [0, 0, 0] for b in boards: r = [0, 0, 0] if build_system == 'cmake': build_args = [f'-D{d}' for d in build_defines] - r = cmake_board(b, build_args, build_flags_on, build_targets) + r = cmake_board(b, build_args, build_name, build_cflags, build_targets) elif build_system == 'make': build_args = ' '.join(f'{d}' for d in build_defines) r = make_board(b, build_args, build_targets) @@ -261,7 +259,10 @@ def main(): 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') - parser.add_argument('-f1', '--build-flags-on', action='append', default=[], help='Build flag to pass to build system') + parser.add_argument('--build-name', default=None, + help='Override build dir name (cmake-build-); default is the board name. Used for HIL variants.') + parser.add_argument('--cflag', action='append', default=[], + help='Raw compiler flag appended to CFLAGS_CLI, e.g. --cflag=-DCFG_TUD_DWC2_DMA_ENABLE=1 (repeatable)') 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, @@ -277,7 +278,8 @@ def main(): toolchain = args.toolchain build_system = args.build_system build_defines = args.define_symbol - build_flags_on = args.build_flags_on + build_name = args.build_name + build_cflags = args.cflag one_random = args.one_random one_first = args.one_first build_targets = args.target if args.target else ['all'] @@ -290,6 +292,12 @@ def main(): print("Please specify families or board to build") return 1 + # --build-name renames the single shared build dir, so building more than one + # board with it would clobber/mix artifacts + if build_name and (len(families) > 0 or len(boards) != 1): + print("--build-name requires exactly one board (-b) and no families") + return 1 + print(build_separator) print(build_format.format('Board', 'Target', '\033[39mResult\033[0m', 'Time')) total_time = time.monotonic() @@ -310,7 +318,7 @@ def main(): 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, build_targets) + result = build_boards_list(all_boards, build_defines, build_system, build_name, build_cflags, build_targets) total_time = time.monotonic() - total_time print(build_separator) diff --git a/tools/get_deps.py b/tools/get_deps.py index eb87abf6e..abe5750f1 100755 --- a/tools/get_deps.py +++ b/tools/get_deps.py @@ -366,6 +366,8 @@ def main(): parser.add_argument('-b', '--board', action='append', default=[], help='Boards to fetch') parser.add_argument('-D', '--define', action='append', default=[], help='Have no effect') parser.add_argument('-f1', '--build-flags-on', action='append', default=[], help='Have no effect') + parser.add_argument('--build-name', default=None, help='Have no effect') + parser.add_argument('--cflag', action='append', default=[], help='Have no effect') args = parser.parse_args() families = args.families -- cgit v1.3.1