Skip to content

Commit 0d2a0a0

Browse files
committed
Parse filenames from IAR map file
Instead of scanning. Is a 8ms/15% speedup.
1 parent 2114ccd commit 0d2a0a0

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

tools/memap.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
RE_IS_TEST = re.compile(r'^(.+)\/.*TESTS\/.+\.map$')
2424

25+
RE_CMDLINE_FILE_IAR = re.compile(r'^#\s+(.+\.o)')
2526
RE_LIBRARY_IAR = re.compile(r'^(.+\.a)\:.+$')
2627
RE_OBJECT_LIBRARY_IAR = re.compile(r'^\s+(.+\.o)\s.*')
2728

@@ -72,6 +73,10 @@ def __init__(self):
7273

7374
self.misc_flash_mem = 0
7475

76+
# Modules passed to the linker on the command line
77+
# this is a dict because modules are looked up by their basename
78+
self.cmd_modules = {}
79+
7580

7681
def module_add(self, object_name, size, section):
7782
""" Adds a module / section to the list
@@ -286,18 +291,19 @@ def parse_section_armcc(self, line):
286291
else:
287292
return ["", 0, ""]
288293

289-
def parse_object_name_iar(self, line):
294+
def parse_object_name_iar(self, object_name):
290295
""" Parse object file
291296
292297
Positional arguments:
293298
line - the line containing the object or library
294299
"""
295300

296301
# simple object (not library)
297-
if line.endswith(".o"):
298-
object_name = line
299-
return object_name
300-
302+
if object_name.endswith(".o"):
303+
try:
304+
return self.cmd_modules[object_name]
305+
except KeyError:
306+
return object_name
301307
else:
302308
return '[misc]'
303309

@@ -344,8 +350,7 @@ def parse_section_iar(self, line):
344350
print "Malformed input found when parsing IAR map: %s" % line
345351

346352
# lookup object in dictionary and return module name
347-
temp = test_re_iar.group(5)
348-
object_name = self.parse_object_name_iar(temp)
353+
object_name = self.parse_object_name_iar(test_re_iar.group(5))
349354

350355
return [object_name, size, section]
351356

@@ -409,34 +414,47 @@ def check_new_object_lib_iar(self, line):
409414
else:
410415
return ""
411416

417+
def parse_iar_command_line(self, lines):
418+
"""Parse the files passed on the command line to the iar linker
419+
420+
Positional arguments:
421+
lines -- an iterator over the lines within a file
422+
"""
423+
for line in lines:
424+
if line.startswith("*"):
425+
break
426+
is_cmdline_file = RE_CMDLINE_FILE_IAR.match(line)
427+
if is_cmdline_file:
428+
full_path = is_cmdline_file.group(1)
429+
self.cmd_modules[os.path.basename(full_path)] = full_path
430+
431+
common_prefix = os.path.dirname(os.path.commonprefix(self.cmd_modules.values()))
432+
self.cmd_modules = {s: os.path.relpath(f, common_prefix)
433+
for s, f in self.cmd_modules.items()}
434+
435+
412436
def parse_map_file_iar(self, file_desc):
413437
""" Main logic to decode IAR map files
414438
415439
Positional arguments:
416440
file_desc - a file like object to parse as an IAR map file
417441
"""
418442

419-
# first round, search for objects
420443
with file_desc as infile:
421-
# Search area to parse
444+
self.parse_iar_command_line(infile)
445+
422446
for line in infile:
423447
if line.startswith(' Section '):
424448
break
425449

426-
# Start decoding the map file
427450
for line in infile:
428-
429-
[name, size, section] = self.parse_section_iar(line)
430-
431-
if size == 0 or name == "" or section == "":
432-
pass
433-
else:
451+
name, size, section = self.parse_section_iar(line)
452+
if size and name and section:
434453
self.module_add(name, size, section)
435454

436455
if line.startswith('*** MODULE SUMMARY'): # finish section
437456
break
438457

439-
# Start decoding the map file
440458
current_library = ""
441459
for line in infile:
442460

@@ -450,7 +468,6 @@ def parse_map_file_iar(self, file_desc):
450468
if object_name and current_library:
451469
temp = '[lib]' + '/'+ current_library + '/'+ object_name
452470
self.module_replace(object_name, temp)
453-
self.rename_modules_from_fs(infile.name)
454471

455472
def _rename_from_path(self, path, to_find, to_update, skip):
456473
for root, subdirs, obj_files in os.walk(path):

0 commit comments

Comments
 (0)