]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Common/Misc.py
BaseTools: Share RegEx between files
[mirror_edk2.git] / BaseTools / Source / Python / Common / Misc.py
index 641506a07d4ec8f95903820c650ebfb7cb8d4fa1..f05ae39ebb29d8067596de9dbc9426bd143f796b 100644 (file)
@@ -42,6 +42,13 @@ import subprocess
 ## Regular expression used to find out place holders in string template\r
 gPlaceholderPattern = re.compile("\$\{([^$()\s]+)\}", re.MULTILINE | re.UNICODE)\r
 \r
+## regular expressions for map file processing\r
+startPatternGeneral = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
+addressPatternGeneral = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
+valuePatternGcc = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
+pcdPatternGcc = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
+secReGeneral = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
+\r
 ## Dictionary used to store file time stamp for quick re-access\r
 gFileTimeStampCache = {}    # {file path : file time stamp}\r
 \r
@@ -92,8 +99,6 @@ def _parseForXcode(lines, efifilepath, varnames):
 \r
 def _parseForGCC(lines, efifilepath, varnames):\r
     """ Parse map file generated by GCC linker """\r
-    valuePattern = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
-    pcdPattern = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
     status = 0\r
     sections = []\r
     varoffset = []\r
@@ -112,7 +117,7 @@ def _parseForGCC(lines, efifilepath, varnames):
 \r
         # status handler\r
         if status == 3:\r
-            m = valuePattern.match(line)\r
+            m = valuePatternGcc.match(line)\r
             if m is not None:\r
                 sections.append(m.groups(0))\r
             for varname in varnames:\r
@@ -125,7 +130,7 @@ def _parseForGCC(lines, efifilepath, varnames):
                     else:\r
                         Str = line[len(".data.%s" % varname):]\r
                     if Str:\r
-                        m = pcdPattern.match(Str.strip())\r
+                        m = pcdPatternGcc.match(Str.strip())\r
                         if m is not None:\r
                             varoffset.append((varname, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
 \r
@@ -153,24 +158,21 @@ def _parseGeneral(lines, efifilepath, varnames):
     status = 0    #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
     secs  = []    # key = section name\r
     varoffset = []\r
-    secRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\da-fA-F]+)[Hh]? +([.\w\$]+) +(\w+)', re.UNICODE)\r
     symRe = re.compile('^([\da-fA-F]+):([\da-fA-F]+) +([\.:\\\\\w\?@\$]+) +([\da-fA-F]+)', re.UNICODE)\r
-    startRe = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
-    addressRe = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
 \r
     for line in lines:\r
         line = line.strip()\r
-        if startRe.match(line):\r
+        if startPatternGeneral.match(line):\r
             status = 1\r
             continue\r
-        if addressRe.match(line):\r
+        if addressPatternGeneral.match(line):\r
             status = 2\r
             continue\r
         if line.startswith("entry point at"):\r
             status = 3\r
             continue        \r
         if status == 1 and len(line) != 0:\r
-            m =  secRe.match(line)\r
+            m =  secReGeneral.match(line)\r
             assert m is not None, "Fail to parse the section in map file , line is %s" % line\r
             sec_no, sec_start, sec_length, sec_name, sec_class = m.groups(0)\r
             secs.append([int(sec_no, 16), int(sec_start, 16), int(sec_length, 16), sec_name, sec_class])\r