Bläddra i källkod

tools: Autodetect the target of MAP files in idf_size.py

Roland Dobai 5 år sedan
förälder
incheckning
511135989c
3 ändrade filer med 107 tillägg och 8 borttagningar
  1. 44 8
      tools/idf_size.py
  2. 55 0
      tools/test_idf_size/expected_output
  3. 8 0
      tools/test_idf_size/test.sh

+ 44 - 8
tools/idf_size.py

@@ -179,8 +179,9 @@ def format_json(json_object):
 
 def load_map_data(map_file):
     memory_config = load_memory_config(map_file)
-    sections  = load_sections(map_file)
-    return memory_config, sections
+    detected_chip = detect_target_chip(map_file)
+    sections = load_sections(map_file)
+    return detected_chip, memory_config, sections
 
 
 def load_memory_config(map_file):
@@ -206,6 +207,26 @@ def load_memory_config(map_file):
     raise RuntimeError("End of file while scanning memory configuration?")
 
 
+def detect_target_chip(map_file):
+    ''' Detect target chip based on the xtensa toolchain name in in the linker script part of the MAP file '''
+    scan_to_header(map_file, 'Linker script and memory map')
+
+    RE_TARGET = re.compile(r'^LOAD .*?/xtensa-([^-]+)-elf/')
+
+    for line in map_file:
+        m = RE_TARGET.search(line)
+        if m:
+            return m.group(1)
+        line = line.strip()
+        # There could be empty line(s) between the "Linker script and memory map" header and "LOAD lines". Therefore,
+        # line stripping and length is checked as well. The "LOAD lines" are between START GROUP and END GROUP for
+        # older MAP files.
+        if not line.startswith(('LOAD', 'START GROUP')) and len(line) > 0:
+            # This break is a failsafe to not process anything load_sections() might want to analyze.
+            break
+    return None
+
+
 def load_sections(map_file):
     """ Load section size information from the MAP file.
 
@@ -213,8 +234,6 @@ def load_sections(map_file):
     is a dict with details about this section, including a "sources" key which holds a list of source file line
     information for each symbol linked into the section.
     """
-    scan_to_header(map_file, "Linker script and memory map")
-
     # output section header, ie '.iram0.text     0x0000000040080400    0x129a5'
     RE_SECTION_HEADER = re.compile(r"(?P<name>[^ ]+) +0x(?P<address>[\da-f]+) +0x(?P<size>[\da-f]+)$")
 
@@ -318,7 +337,7 @@ def main():
         '--files', help='Print per-file sizes', action='store_true')
 
     parser.add_argument(
-        '--target', help='Set target chip', default='esp32')
+        '--target', help='Set target chip', default=None)
 
     parser.add_argument(
         '--diff', help='Show the differences in comparison with another MAP file',
@@ -335,16 +354,33 @@ def main():
 
     args = parser.parse_args()
 
-    memory_config, sections = load_map_data(args.map_file)
+    detected_target, memory_config, sections = load_map_data(args.map_file)
     args.map_file.close()
 
+    def check_target(target, map_file):
+        if target is None:
+            raise RuntimeError('The target chip cannot be detected for {}. '
+                               'Please report the issue.'.format(map_file.name))
+
+    check_target(detected_target, args.map_file)
+
+    if args.target is not None:
+        if args.target != detected_target:
+            print('WARNING: The detected chip target is {} but command line argument overwrites it to '
+                  '{}!'.format(detected_target, args.target))
+        detected_target = args.target
+
     if args.another_map_file:
         with open(args.another_map_file, 'r') as f:
-            memory_config_diff, sections_diff = load_map_data(f)
+            detected_target_diff, memory_config_diff, sections_diff = load_map_data(f)
+            check_target(detected_target_diff, f)
+            if detected_target_diff != detected_target:
+                print('WARNING: The target of the reference and other MAP files is {} and {}, respectively.'
+                      ''.format(detected_target, detected_target_diff))
     else:
         memory_config_diff, sections_diff = None, None
 
-    mem_regions = MemRegions(args.target)
+    mem_regions = MemRegions(detected_target)
     mem_reg = MemRegNames.get(mem_regions, memory_config, sections)
     mem_reg_diff = MemRegNames.get(mem_regions, memory_config_diff, sections_diff) if args.another_map_file else None
 

+ 55 - 0
tools/test_idf_size/expected_output

@@ -3042,6 +3042,18 @@ Used stat D/IRAM:   43020 bytes ( 350196 available, 10.9% used)
     Flash rodata:   18580 bytes
 Total image size:~ 136039 bytes (.bin may be padded larger)
 
+***
+Running idf_size.py for esp32s2 (target autodetected)...
+Total sizes:
+ DRAM .data size:    7152 bytes
+ DRAM .bss  size:    1936 bytes
+Used static DRAM:       0 bytes (      0 available, nan% used)
+Used static IRAM:       0 bytes (      0 available, nan% used)
+Used stat D/IRAM:   43020 bytes ( 350196 available, 10.9% used)
+      Flash code:   74439 bytes
+    Flash rodata:   18580 bytes
+Total image size:~ 136039 bytes (.bin may be padded larger)
+
 ***
 Running idf_size.py on bootloader for esp32s2...
 Total sizes:
@@ -3054,6 +3066,18 @@ Used stat D/IRAM:   12094 bytes (  30914 available, 28.1% used)
     Flash rodata:       0 bytes
 Total image size:~  12094 bytes (.bin may be padded larger)
 
+***
+Running idf_size.py on bootloader for esp32s2 (target autodetected)...
+Total sizes:
+ DRAM .data size:       4 bytes
+ DRAM .bss  size:     264 bytes
+Used static DRAM:       0 bytes (      0 available, nan% used)
+Used static IRAM:       0 bytes (      0 available, nan% used)
+Used stat D/IRAM:   12094 bytes (  30914 available, 28.1% used)
+      Flash code:       0 bytes
+    Flash rodata:       0 bytes
+Total image size:~  12094 bytes (.bin may be padded larger)
+
 ***
 Running idf_size.py --archives for esp32s2...
 Total sizes:
@@ -3361,6 +3385,37 @@ Section total: 0
 Symbols from section: .rtc.text
 Section total: 0
 
+***
+Running idf_size.py diff with another app (different target)...
+WARNING: The target of the reference and other MAP files is esp32 and esp32s2, respectively.
+<CURRENT> MAP file: app.map
+<REFERENCE> MAP file: app_esp32s2.map
+Difference is counted as <CURRENT> - <REFERENCE>, i.e. a positive number means that <CURRENT> is larger.
+Total sizes of <CURRENT>:                                                 <REFERENCE>     Difference
+ DRAM .data size:    9324 bytes                                                  7152          +2172
+ DRAM .bss  size:    8296 bytes                                                  1936          +6360
+Used static DRAM:   17620 bytes ( 163116 available, 9.7% used)                   9088          +8532 ( -24404 available,  -15872 total)
+Used static IRAM:   38932 bytes (  92140 available, 29.7% used)                 74439         -35507 (+158387 available, +122880 total)
+      Flash code:  146944 bytes                                                 74439         +72505
+    Flash rodata:   39580 bytes                                                 18580         +21000
+Total image size:~ 243076 bytes (.bin may be padded larger)                    176546         +66530
+
+***
+Running idf_size.py diff with another app (wrong target)...
+WARNING: The detected chip target is esp32 but command line argument overwrites it to esp32s2!
+WARNING: The target of the reference and other MAP files is esp32s2 and esp32, respectively.
+<CURRENT> MAP file: app.map
+<REFERENCE> MAP file: app2.map
+Difference is counted as <CURRENT> - <REFERENCE>, i.e. a positive number means that <CURRENT> is larger.
+Total sizes of <CURRENT>:                                                 <REFERENCE>     Difference
+ DRAM .data size:       0 bytes                                                     0
+ DRAM .bss  size:    8296 bytes                                                  2024          +6272
+Used static DRAM:       0 bytes (      0 available, nan% used)                      0                (     +0 available,      +0 total)
+Used static IRAM:       0 bytes (      0 available, nan% used)                      0                (     +0 available,      +0 total)
+      Flash code:  146944 bytes                                                 77191         +69753
+    Flash rodata:   39580 bytes                                                 22360         +17220
+Total image size:~ 194820 bytes (.bin may be padded larger)                    101575         +93245
+
 ***
 Producing JSON output...
 {

+ 8 - 0
tools/test_idf_size/test.sh

@@ -50,14 +50,22 @@
     && coverage run -a $IDF_PATH/tools/idf_size.py app.map --archive_details libfreertos.a --diff app2.map &>> output \
     && echo -e "\n***\nRunning idf_size.py for esp32s2..." &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --target esp32s2 app_esp32s2.map &>> output \
+    && echo -e "\n***\nRunning idf_size.py for esp32s2 (target autodetected)..." &>> output \
+    && coverage run -a $IDF_PATH/tools/idf_size.py app_esp32s2.map &>> output \
     && echo -e "\n***\nRunning idf_size.py on bootloader for esp32s2..." &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --target esp32s2 bootloader_esp32s2.map &>> output \
+    && echo -e "\n***\nRunning idf_size.py on bootloader for esp32s2 (target autodetected)..." &>> output \
+    && coverage run -a $IDF_PATH/tools/idf_size.py bootloader_esp32s2.map &>> output \
     && echo -e "\n***\nRunning idf_size.py --archives for esp32s2..." &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --target esp32s2 --archives app_esp32s2.map &>> output \
     && echo -e "\n***\nRunning idf_size.py --files for esp32s2..." &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --target esp32s2 --files app_esp32s2.map &>> output \
     && echo -e "\n***\nRunning idf_size.py --archive_details for esp32s2..." &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --target esp32s2 --archive_details libdriver.a app_esp32s2.map &>> output \
+    && echo -e "\n***\nRunning idf_size.py diff with another app (different target)..." &>> output \
+    && coverage run -a $IDF_PATH/tools/idf_size.py app.map --diff app_esp32s2.map &>> output \
+    && echo -e "\n***\nRunning idf_size.py diff with another app (wrong target)..." &>> output \
+    && coverage run -a $IDF_PATH/tools/idf_size.py --target esp32s2 app.map --diff app2.map &>> output \
     && echo -e "\n***\nProducing JSON output..." &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --json app.map &>> output \
     && coverage run -a $IDF_PATH/tools/idf_size.py --json --archives app.map &>> output \