]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Trim/Trim.py
BaseTools/Trim: Normalize filepaths to fix comparisons on Windows
[mirror_edk2.git] / BaseTools / Source / Python / Trim / Trim.py
index d07edbd5d8722ff7783a6c6c42e4182d622d687e..b46d507b4e55e503712ecf0a205cb3d940f206b4 100644 (file)
@@ -1,7 +1,7 @@
 ## @file\r
 # Trim files preprocessed by compiler\r
 #\r
-# Copyright (c) 2007 - 2017, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
 # This program and the accompanying materials\r
 # are licensed and made available under the terms and conditions of the BSD License\r
 # which accompanies this distribution.  The full text of the license may be found at\r
 import Common.LongFilePathOs as os\r
 import sys\r
 import re\r
-import StringIO\r
+from io import BytesIO\r
 \r
 from optparse import OptionParser\r
 from optparse import make_option\r
 from Common.BuildToolError import *\r
 from Common.Misc import *\r
+from Common.DataType import *\r
 from Common.BuildVersion import gBUILD_VERSION\r
 import Common.EdkLogger as EdkLogger\r
 from Common.LongFilePathSupport import OpenLongFilePath as open\r
@@ -165,6 +166,8 @@ def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):
             if len(MatchList) == 2:\r
                 LineNumber = int(MatchList[0], 0)\r
                 InjectedFile = MatchList[1]\r
+                InjectedFile = os.path.normpath(InjectedFile)\r
+                InjectedFile = os.path.normcase(InjectedFile)\r
                 # The first injetcted file must be the preprocessed file itself\r
                 if PreprocessedFile == "":\r
                     PreprocessedFile = InjectedFile\r
@@ -260,7 +263,7 @@ def TrimPreprocessedVfr(Source, Target):
     CreateDirectory(os.path.dirname(Target))\r
     \r
     try:\r
-        f = open (Source,'r')\r
+        f = open (Source, 'r')\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
     # read whole file\r
@@ -303,13 +306,13 @@ def TrimPreprocessedVfr(Source, Target):
             FoundTypedef = False\r
             TypedefEnd = Index\r
             # keep all "typedef struct" except to GUID, EFI_PLABEL and PAL_CALL_RETURN\r
-            if Line.strip("} ;\r\n") in ["GUID", "EFI_PLABEL", "PAL_CALL_RETURN"]:\r
+            if Line.strip("} ;\r\n") in [TAB_GUID, "EFI_PLABEL", "PAL_CALL_RETURN"]:\r
                 for i in range(TypedefStart, TypedefEnd+1):\r
                     Lines[i] = "\n"\r
 \r
     # save all lines trimmed\r
     try:\r
-        f = open (Target,'w')\r
+        f = open (Target, 'w')\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
     f.writelines(Lines)\r
@@ -406,7 +409,7 @@ def TrimAslFile(Source, Target, IncludePathFile):
     if IncludePathFile:\r
         try:\r
             LineNum = 0\r
-            for Line in open(IncludePathFile,'r'):\r
+            for Line in open(IncludePathFile, 'r'):\r
                 LineNum += 1\r
                 if Line.startswith("/I") or Line.startswith ("-I"):\r
                     IncludePathList.append(Line[2:].strip())\r
@@ -424,7 +427,7 @@ def TrimAslFile(Source, Target, IncludePathFile):
 \r
     # save all lines trimmed\r
     try:\r
-        f = open (Target,'w')\r
+        f = open (Target, 'w')\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
 \r
@@ -437,7 +440,7 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
         for CurrentDir, Dirs, Files in os.walk(DebugDir):\r
             for FileName in Files:\r
                 Name, Ext = os.path.splitext(FileName)\r
-                if Ext == '.lst':\r
+                if Ext == '.c' and Name != 'AutoGen':\r
                     VfrNameList.append (Name + 'Bin')\r
 \r
     VfrNameList.append (ModuleName + 'Strings')\r
@@ -454,8 +457,8 @@ def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)\r
 \r
-    # Use a instance of StringIO to cache data\r
-    fStringIO = StringIO.StringIO('')\r
+    # Use a instance of BytesIO to cache data\r
+    fStringIO = BytesIO('')\r
 \r
     for Item in VfrUniOffsetList:\r
         if (Item[0].find("Strings") != -1):\r
@@ -559,7 +562,7 @@ def TrimEdkSourceCode(Source, Target):
     CreateDirectory(os.path.dirname(Target))\r
 \r
     try:\r
-        f = open (Source,'rb')\r
+        f = open (Source, 'rb')\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
     # read whole file\r
@@ -567,7 +570,7 @@ def TrimEdkSourceCode(Source, Target):
     f.close()\r
 \r
     NewLines = None\r
-    for Re,Repl in gImportCodePatterns:\r
+    for Re, Repl in gImportCodePatterns:\r
         if NewLines is None:\r
             NewLines = Re.sub(Repl, Lines)\r
         else:\r
@@ -578,7 +581,7 @@ def TrimEdkSourceCode(Source, Target):
         return\r
 \r
     try:\r
-        f = open (Target,'wb')\r
+        f = open (Target, 'wb')\r
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
     f.write(NewLines)\r
@@ -667,7 +670,7 @@ def Main():
             EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
         else:\r
             EdkLogger.SetLevel(CommandOptions.LogLevel)\r
-    except FatalError, X:\r
+    except FatalError as X:\r
         return 1\r
     \r
     try:\r
@@ -687,7 +690,7 @@ def Main():
             if CommandOptions.OutputFile is None:\r
                 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
             TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong)\r
-    except FatalError, X:\r
+    except FatalError as X:\r
         import platform\r
         import traceback\r
         if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:\r