]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Share RegEx between files
authorCarsey, Jaben <jaben.carsey@intel.com>
Fri, 20 Apr 2018 15:51:23 +0000 (23:51 +0800)
committerYonghong Zhu <yonghong.zhu@intel.com>
Thu, 26 Apr 2018 06:27:59 +0000 (14:27 +0800)
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jaben Carsey <jaben.carsey@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/Python/Common/Misc.py
BaseTools/Source/Python/GenPatchPcdTable/GenPatchPcdTable.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
index 59748763a553d2711470f4b06879767a95055330..2a039480a00ae22839a226e7e4b9687d42ec8c13 100644 (file)
@@ -24,7 +24,7 @@ import array
 \r
 from Common.BuildToolError import *\r
 import Common.EdkLogger as EdkLogger\r
-from Common.Misc import PeImageClass\r
+from Common.Misc import PeImageClass, startPatternGeneral, addressPatternGeneral, valuePatternGcc, pcdPatternGcc, secReGeneral\r
 from Common.BuildVersion import gBUILD_VERSION\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
 \r
@@ -36,7 +36,6 @@ __copyright__ = "Copyright (c) 2008 - 2010, Intel Corporation. All rights reserv
 #======================================  Internal Libraries ========================================\r
 \r
 #============================================== Code ===============================================\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
 \r
 def parsePcdInfoFromMapFile(mapfilepath, efifilepath):\r
@@ -80,9 +79,7 @@ def _parseForXcode(lines, efifilepath):
 \r
 def _parseForGCC(lines, efifilepath):\r
     """ Parse map file generated by GCC linker """\r
-    valuePattern = re.compile('^([\w_\.]+) +([\da-fA-Fx]+) +([\da-fA-Fx]+)$')\r
     dataPattern = re.compile('^.data._gPcd_BinaryPatch_([\w_\d]+)$')\r
-    pcdPattern = re.compile('^([\da-fA-Fx]+) +([\da-fA-Fx]+)')\r
     status = 0\r
     imageBase = -1\r
     sections = []\r
@@ -102,7 +99,7 @@ def _parseForGCC(lines, efifilepath):
 \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
         if status == 3:\r
@@ -110,7 +107,7 @@ def _parseForGCC(lines, efifilepath):
             if m is not None:\r
                 if lines[index + 1]:\r
                     PcdName = m.groups(0)[0]\r
-                    m = pcdPattern.match(lines[index + 1].strip())\r
+                    m = pcdPatternGcc.match(lines[index + 1].strip())\r
                     if m is not None:\r
                         bpcds.append((PcdName, int(m.groups(0)[0], 16) , int(sections[-1][1], 16), sections[-1][0]))\r
                 \r
@@ -141,16 +138,14 @@ def _parseGeneral(lines, efifilepath):
     status = 0    #0 - beginning of file; 1 - PE section definition; 2 - symbol table\r
     secs = []    # key = section name\r
     bPcds = []\r
-    startPattern = re.compile("^Start[' ']+Length[' ']+Name[' ']+Class")\r
-    addressPattern = re.compile("^Address[' ']+Publics by Value[' ']+Rva\+Base")\r
     symPattern = re.compile('^[_]+gPcd_BinaryPatch_([\w]+)')\r
 \r
     for line in lines:\r
         line = line.strip()\r
-        if startPattern.match(line):\r
+        if startPatternGeneral.match(line):\r
             status = 1\r
             continue\r
-        if addressPattern.match(line):\r
+        if addressPatternGeneral.match(line):\r
             status = 2\r
             continue\r
         if line.startswith("entry point at"):\r