]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Trim/Trim.py
Sync BaseTools Branch (version r2321) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / Trim / Trim.py
index b3ad16715ae03cbf9185e5996e3ea1f070a97360..c5e9ee2f0d243370cdf89582eed3d004e9398e5a 100644 (file)
@@ -22,11 +22,11 @@ from optparse import OptionParser
 from optparse import make_option
 from Common.BuildToolError import *
 from Common.Misc import *
-
+from Common.BuildVersion import gBUILD_VERSION
 import Common.EdkLogger as EdkLogger
 
 # Version and Copyright
-__version_number__ = "0.10"
+__version_number__ = ("0.10" + " " + gBUILD_VERSION)
 __version__ = "%prog Version " + __version_number__
 __copyright__ = "Copyright (c) 2007-2010, Intel Corporation. All rights reserved."
 
@@ -41,7 +41,7 @@ gHexNumberPattern = re.compile("0[xX]([0-9a-fA-F]+)")
 ## Regular expression for matching "Include ()" in asl file
 gAslIncludePattern = re.compile("^(\s*)[iI]nclude\s*\(\"?([^\"\(\)]+)\"\)", re.MULTILINE)
 ## Regular expression for matching C style #include "XXX.asl" in asl file
-gAslCIncludePattern = re.compile(r'^(\s*)#include\s*[<"]\s*([-\\/\w.]+)\s*[>"]', re.MULTILINE)
+gAslCIncludePattern = re.compile(r'^(\s*)#include\s*[<"]\s*([-\\/\w.]+)\s*([>"])', re.MULTILINE)
 ## Regular expression for matching constant with 'ULL' and 'UL', 'LL', 'L' postfix
 gLongNumberPattern = re.compile("(0[xX][0-9a-fA-F]+|[0-9]+)U?LL", re.MULTILINE)
 ## Patterns used to convert EDK conventions to EDK2 ECP conventions
@@ -273,12 +273,23 @@ def TrimPreprocessedVfr(Source, Target):
 # @param  Source            File to be read
 # @param  Indent            Spaces before the Include() statement
 # @param  IncludePathList   The list of external include file
+# @param  LocalSearchPath   If LocalSearchPath is specified, this path will be searched
+#                           first for the included file; otherwise, only the path specified
+#                           in the IncludePathList will be searched.
 #
-def DoInclude(Source, Indent='', IncludePathList=[]):
+def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
     NewFileContent = []
 
     try:
-        for IncludePath in IncludePathList:
+        #
+        # Search LocalSearchPath first if it is specified.
+        #
+        if LocalSearchPath:
+            SearchPathList = [LocalSearchPath] + IncludePathList
+        else:
+            SearchPathList = IncludePathList
+  
+        for IncludePath in SearchPathList:
             IncludeFile = os.path.join(IncludePath, Source)
             if os.path.isfile(IncludeFile):
                 F = open(IncludeFile, "r")
@@ -298,15 +309,21 @@ def DoInclude(Source, Indent='', IncludePathList=[]):
     gIncludedAslFile.append(IncludeFile)
     
     for Line in F:
+        LocalSearchPath = None
         Result = gAslIncludePattern.findall(Line)
         if len(Result) == 0:
             Result = gAslCIncludePattern.findall(Line)
             if len(Result) == 0 or os.path.splitext(Result[0][1])[1].lower() not in [".asl", ".asi"]:
                 NewFileContent.append("%s%s" % (Indent, Line))
                 continue
+            #
+            # We should first search the local directory if current file are using pattern #include "XXX" 
+            #
+            if Result[0][2] == '"':
+                LocalSearchPath = os.path.dirname(IncludeFile)
         CurrentIndent = Indent + Result[0][0]
         IncludedFile = Result[0][1]
-        NewFileContent.extend(DoInclude(IncludedFile, CurrentIndent, IncludePathList))
+        NewFileContent.extend(DoInclude(IncludedFile, CurrentIndent, IncludePathList, LocalSearchPath))
         NewFileContent.append("\n")
 
     gIncludedAslFile.pop()
@@ -373,7 +390,7 @@ def TrimAslFile(Source, Target, IncludePathFile):
 # @param  Source    File or directory to be trimmed
 # @param  Target    File or directory to store the trimmed content
 #
-def TrimR8Sources(Source, Target):
+def TrimEdkSources(Source, Target):
     if os.path.isdir(Source):
         for CurrentDir, Dirs, Files in os.walk(Source):
             if '.svn' in Dirs:
@@ -385,17 +402,17 @@ def TrimR8Sources(Source, Target):
                 Dummy, Ext = os.path.splitext(FileName)
                 if Ext.upper() not in ['.C', '.H']: continue
                 if Target == None or Target == '':
-                    TrimR8SourceCode(
+                    TrimEdkSourceCode(
                         os.path.join(CurrentDir, FileName),
                         os.path.join(CurrentDir, FileName)
                         )
                 else:
-                    TrimR8SourceCode(
+                    TrimEdkSourceCode(
                         os.path.join(CurrentDir, FileName),
                         os.path.join(Target, CurrentDir[len(Source)+1:], FileName)
                         )
     else:
-        TrimR8SourceCode(Source, Target)
+        TrimEdkSourceCode(Source, Target)
 
 ## Trim one EDK source code file
 #
@@ -428,7 +445,7 @@ def TrimR8Sources(Source, Target):
 # @param  Source    File to be trimmed
 # @param  Target    File to store the trimmed content
 #
-def TrimR8SourceCode(Source, Target):
+def TrimEdkSourceCode(Source, Target):
     EdkLogger.verbose("\t%s -> %s" % (Source, Target))
     CreateDirectory(os.path.dirname(Target))
 
@@ -474,8 +491,8 @@ def Options():
                           help="The input file is preprocessed VFR file"),
         make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",
                           help="The input file is ASL file"),
-        make_option("-8", "--r8-source-code", dest="FileType", const="R8SourceCode", action="store_const",
-                          help="The input file is source code for R8 to be trimmed for ECP"),
+        make_option("-8", "--Edk-source-code", dest="FileType", const="EdkSourceCode", action="store_const",
+                          help="The input file is source code for Edk to be trimmed for ECP"),
 
         make_option("-c", "--convert-hex", dest="ConvertHex", action="store_true",
                           help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),
@@ -543,8 +560,8 @@ def Main():
             if CommandOptions.OutputFile == None:
                 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'
             TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)
-        elif CommandOptions.FileType == "R8SourceCode":
-            TrimR8Sources(InputFile, CommandOptions.OutputFile)
+        elif CommandOptions.FileType == "EdkSourceCode":
+            TrimEdkSources(InputFile, CommandOptions.OutputFile)
         else :
             if CommandOptions.OutputFile == None:
                 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'