diff --git a/esphome/analyze_memory/cli.py b/esphome/analyze_memory/cli.py index 1695a00c19..2986922ac2 100644 --- a/esphome/analyze_memory/cli.py +++ b/esphome/analyze_memory/cli.py @@ -1,6 +1,7 @@ """CLI interface for memory analysis with report generation.""" from collections import defaultdict +import json import sys from . import ( @@ -270,6 +271,28 @@ class MemoryAnalyzerCLI(MemoryAnalyzer): return "\n".join(lines) + def to_json(self) -> str: + """Export analysis results as JSON.""" + data = { + "components": { + name: { + "text": mem.text_size, + "rodata": mem.rodata_size, + "data": mem.data_size, + "bss": mem.bss_size, + "flash_total": mem.flash_total, + "ram_total": mem.ram_total, + "symbol_count": mem.symbol_count, + } + for name, mem in self.components.items() + }, + "totals": { + "flash": sum(c.flash_total for c in self.components.values()), + "ram": sum(c.ram_total for c in self.components.values()), + }, + } + return json.dumps(data, indent=2) + def dump_uncategorized_symbols(self, output_file: str | None = None) -> None: """Dump uncategorized symbols for analysis.""" # Sort by size descending diff --git a/esphome/platformio_api.py b/esphome/platformio_api.py index b7b6cf399d..19d355efd3 100644 --- a/esphome/platformio_api.py +++ b/esphome/platformio_api.py @@ -408,7 +408,8 @@ class IDEData: def analyze_memory_usage(config: dict[str, Any]) -> None: """Analyze memory usage by component after compilation.""" # Lazy import to avoid overhead when not needed - from esphome.analyze_memory import MemoryAnalyzer + from esphome.analyze_memory.cli import MemoryAnalyzerCLI + from esphome.analyze_memory.helpers import get_esphome_components idedata = get_idedata(config) @@ -435,8 +436,6 @@ def analyze_memory_usage(config: dict[str, Any]) -> None: external_components = set() # Get the list of built-in ESPHome components - from esphome.analyze_memory import get_esphome_components - builtin_components = get_esphome_components() # Special non-component keys that appear in configs @@ -457,7 +456,9 @@ def analyze_memory_usage(config: dict[str, Any]) -> None: _LOGGER.debug("Detected external components: %s", external_components) # Create analyzer and run analysis - analyzer = MemoryAnalyzer(elf_path, objdump_path, readelf_path, external_components) + analyzer = MemoryAnalyzerCLI( + elf_path, objdump_path, readelf_path, external_components + ) analyzer.analyze() # Generate and print report