Skip to content

Add espressif esp-idf-size to the build engine to show firmware metrics #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 46 commits into from
May 21, 2025

Conversation

Jason2866
Copy link

@Jason2866 Jason2866 commented May 21, 2025

Description:

  • new feature: additional infos about flash and RAM usage.

The needed map file for this is default generated in Arduino projects. Only for espidf projects SHOW_METRICS needs to be added to the build_flags to generate the needed map file. Like this build_flags = -D SHOW_METRICS

@h2zero please test, ping me on Discord for questions or suggestions

Checklist:

  • The pull request is done against the latest develop branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR, more changes are allowed when changing boards.json
  • I accept the CLA

Summary by CodeRabbit

  • New Features

    • Added automatic display of firmware size metrics after building ESP-IDF projects when the relevant build flag is enabled.
    • Introduced support for the ESP32C5 MCU in toolchain and debugging configurations.
  • Configuration

    • Enabled the option to show build metrics by default in example ESP-IDF project configurations through a new build flag.
  • Chores

    • Updated Python package requirements to include the latest version of the firmware size analysis tool.

Copy link

coderabbitai bot commented May 21, 2025

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes introduce the esp-idf-size Python package as a build dependency, update build scripts to generate and display firmware size metrics when the SHOW_METRICS flag is set, and extend toolchain support for the esp32c5 MCU. Example project configurations are updated to enable metrics display by adding the SHOW_METRICS flag.

Changes

File(s) Change Summary
builder/frameworks/arduino.py, builder/frameworks/espidf.py Added "esp-idf-size>=1.6.1" to required Python packages; updated build logic to support metrics.
builder/frameworks/espidf.py Refactored CMake argument construction; added map file cleanup; appended linker flag for metrics.
builder/main.py Added firmware_metrics function to display size metrics post-build; added esp32c5 toolchain support.
examples/espidf-blink/platformio.ini, examples/espidf-coap-server/platformio.ini Added -D SHOW_METRICS to build flags for relevant ESP-IDF environments.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant BuildSystem
    participant PythonDeps
    participant Linker
    participant MetricsTool

    User->>BuildSystem: Start ESP-IDF build
    BuildSystem->>PythonDeps: Ensure required packages (incl. esp-idf-size)
    BuildSystem->>Linker: Build firmware (with SHOW_METRICS flag)
    Linker->>BuildSystem: Generate .map file if SHOW_METRICS is set
    BuildSystem->>MetricsTool: Run esp-idf-size on .map file (post-build)
    MetricsTool->>BuildSystem: Output firmware size metrics
Loading

Poem

In the warren of code where the metrics reside,
A new tool hops in, with size stats to provide.
With SHOW_METRICS set, map files appear,
And esp-idf-size makes the numbers clear.
Now every build tells a story in bytes—
A bunny’s delight in these firmware insights!
🐇📏✨


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Jason2866
Copy link
Author

@coderabbitai full review

Copy link

coderabbitai bot commented May 21, 2025

✅ Actions performed

Full review triggered.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
builder/main.py (1)

361-376: Well-implemented firmware metrics function

The firmware_metrics function is well implemented with proper file existence checks and error handling. It attempts to locate the map file in either the build directory or project directory before running the metrics tool.

One small suggestion: consider adding a log message when an exception occurs to help with debugging in case the metrics functionality fails.

        except Exception:
+            print("Warning: Failed to run firmware metrics. Is esp-idf-size installed?")
            pass
builder/frameworks/espidf.py (1)

1792-1810: Well-structured refactoring to support metrics feature.

The CMake arguments construction has been refactored into a list for better organization and to support conditional flags. The new code checks for the SHOW_METRICS define and appropriately adds the map file generation linker flag when needed.

However, I notice a potential optimization opportunity with the map file path definition:

# At the beginning of the file
map_file = os.path.join(env.subst("$PROJECT_DIR"), env.subst("$PROGNAME") + ".map")
# ...
if "CPPDEFINES" in env:
    flatten_cppdefines = env.Flatten(env['CPPDEFINES'])
    if "SHOW_METRICS" in flatten_cppdefines:
-        # This will add the linker flag for the map file
-        extra_cmake_args.append(
-            f'-DCMAKE_EXE_LINKER_FLAGS=-Wl,-Map={os.path.join(BUILD_DIR, env.subst("$PROGNAME") + ".map")}'
-        )
+        # Use the same map file path defined earlier
+        extra_cmake_args.append(
+            f'-DCMAKE_EXE_LINKER_FLAGS=-Wl,-Map={os.path.join(BUILD_DIR, env.subst("$PROGNAME") + ".map")}'
+        )
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 1b7215a and 3b0ecda.

📒 Files selected for processing (5)
  • builder/frameworks/arduino.py (1 hunks)
  • builder/frameworks/espidf.py (3 hunks)
  • builder/main.py (6 hunks)
  • examples/espidf-blink/platformio.ini (3 hunks)
  • examples/espidf-coap-server/platformio.ini (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: build (macos-15, examples/espidf-ulp-lp)
  • GitHub Check: build (macos-15, examples/arduino-zigbee-switch)
  • GitHub Check: build (macos-15, examples/espidf-hello-world)
  • GitHub Check: build (macos-15, examples/espidf-arduino-blink)
  • GitHub Check: build (windows-latest, examples/espidf-coap-server)
  • GitHub Check: build (windows-latest, examples/espidf-ulp-riscv)
  • GitHub Check: build (windows-latest, examples/espidf-blink)
  • GitHub Check: build (windows-latest, examples/espidf-arduino-blink)
  • GitHub Check: build (windows-latest, examples/arduino-NimBLE-ext_client)
  • GitHub Check: build (windows-latest, examples/tasmota)
  • GitHub Check: build (windows-latest, examples/arduino-wifiscan)
  • GitHub Check: build (windows-latest, examples/arduino-blink)
🔇 Additional comments (9)
builder/frameworks/arduino.py (1)

106-108: New dependency added correctly

The addition of the esp-idf-size Python package dependency looks good. This aligns with the PR objective to show firmware metrics.

examples/espidf-blink/platformio.ini (1)

20-20: SHOW_METRICS flag added to enable firmware size reporting

Good addition of the SHOW_METRICS flag to all environments. This preprocessor definition enables the map file generation required for the firmware size metrics feature.

Also applies to: 31-31, 42-42

examples/espidf-coap-server/platformio.ini (1)

21-22: SHOW_METRICS flag added correctly

Good addition of the build_flags section with the SHOW_METRICS flag to enable firmware size reporting for this example too.

builder/main.py (3)

24-26: New imports added for firmware metrics functionality

The new imports are correctly added to support the firmware metrics functionality.


229-229: Added support for esp32c5 MCU

Good addition of "esp32c5" to the MCU lists for both toolchain architecture and GDB tool selection.

Also applies to: 250-250


391-393: Clean integration as a silent post-build action

Good approach to add the firmware metrics as a silent post-build action to avoid cluttering the output. The hack to silence the scons command output works well.

builder/frameworks/espidf.py (3)

59-63: Good preparation for the metrics feature.

The code properly removes any existing map file in the project root before building, ensuring clean metrics generation when the feature is enabled.


91-92: Added necessary dependencies for metrics feature.

The addition of esp-idf-size as a dependency is required for the new firmware metrics functionality, and the PyYAML version has been appropriately updated.


1815-1815: Completed the refactoring.

The changes correctly pass the constructed extra_cmake_args list to the get_cmake_code_model function, completing the argument refactoring.

@Jason2866 Jason2866 merged commit 8ff398b into develop May 21, 2025
1 check passed
@Jason2866 Jason2866 deleted the develop_firm_size branch May 21, 2025 15:29
@Jason2866 Jason2866 restored the develop_firm_size branch May 21, 2025 15:30
@h2zero
Copy link

h2zero commented May 21, 2025

Awesome!, I will test this today 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants