]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/Trim/Trim.py
BaseTools: use set instead of list for a variable to be used with in
[mirror_edk2.git] / BaseTools / Source / Python / Trim / Trim.py
index dbfa84a5da0ab9697482d9846b72b77f97e0dde1..3eb7fa39209d74b6872d66c5fd4b63b84f03f8ca 100644 (file)
-## @file
-# Trim files preprocessed by compiler
-#
-# Copyright (c) 2007 - 2010, Intel Corporation
-# All rights reserved. This program and the accompanying materials
-# are licensed and made available under the terms and conditions of the BSD License
-# which accompanies this distribution.  The full text of the license may be found at
-# http://opensource.org/licenses/bsd-license.php
-#
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-#
-
-##
-# Import Modules
-#
-import os
-import sys
-import re
-
-from optparse import OptionParser
-from optparse import make_option
-from Common.BuildToolError import *
-from Common.Misc import *
-
-import Common.EdkLogger as EdkLogger
-
-# Version and Copyright
-__version_number__ = "0.10"
-__version__ = "%prog Version " + __version_number__
-__copyright__ = "Copyright (c) 2007-2010, Intel Corporation. All rights reserved."
-
-## Regular expression for matching Line Control directive like "#line xxx"
-gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')
-## Regular expression for matching "typedef struct"
-gTypedefPattern = re.compile("^\s*typedef\s+struct\s*[{]*$", re.MULTILINE)
-## Regular expression for matching "#pragma pack"
-gPragmaPattern = re.compile("^\s*#pragma\s+pack", re.MULTILINE)
-## Regular expression for matching HEX number
-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)
-## Patterns used to convert EDK conventions to EDK2 ECP conventions
-gImportCodePatterns = [
-    [
-        re.compile('^(\s*)\(\*\*PeiServices\)\.PciCfg\s*=\s*([^;\s]+);', re.MULTILINE),
-        '''\\1{
-\\1  STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {
-\\1    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
-\\1    &gEcpPeiPciCfgPpiGuid,
-\\1    \\2
-\\1  };
-\\1  (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);
-\\1}'''
-    ],
-
-    [
-        re.compile('^(\s*)\(\*PeiServices\)->PciCfg\s*=\s*([^;\s]+);', re.MULTILINE),
-        '''\\1{
-\\1  STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {
-\\1    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
-\\1    &gEcpPeiPciCfgPpiGuid,
-\\1    \\2
-\\1  };
-\\1  (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);
-\\1}'''
-    ],
-
-    [
-        re.compile("(\s*).+->Modify[\s\n]*\(", re.MULTILINE),
-        '\\1PeiLibPciCfgModify ('
-    ],
-
-    [
-        re.compile("(\W*)gRT->ReportStatusCode[\s\n]*\(", re.MULTILINE),
-        '\\1EfiLibReportStatusCode ('
-    ],
-
-    [
-        re.compile('#include\s+["<]LoadFile\.h[">]', re.MULTILINE),
-        '#include <FvLoadFile.h>'
-    ],
-
-    [
-        re.compile('#include\s+EFI_GUID_DEFINITION\s*\(FirmwareFileSystem\)', re.MULTILINE),
-        '#include EFI_GUID_DEFINITION (FirmwareFileSystem)\n#include EFI_GUID_DEFINITION (FirmwareFileSystem2)'
-    ],
-
-    [
-        re.compile('gEfiFirmwareFileSystemGuid', re.MULTILINE),
-        'gEfiFirmwareFileSystem2Guid'
-    ],
-
-    [
-        re.compile('EFI_FVH_REVISION', re.MULTILINE),
-        'EFI_FVH_PI_REVISION'
-    ],
-
-    [
-        re.compile("(\s*)\S*CreateEvent\s*\([\s\n]*EFI_EVENT_SIGNAL_READY_TO_BOOT[^,]*,((?:[^;]+\n)+)(\s*\));", re.MULTILINE),
-        '\\1EfiCreateEventReadyToBoot (\\2\\3;'
-    ],
-
-    [
-        re.compile("(\s*)\S*CreateEvent\s*\([\s\n]*EFI_EVENT_SIGNAL_LEGACY_BOOT[^,]*,((?:[^;]+\n)+)(\s*\));", re.MULTILINE),
-        '\\1EfiCreateEventLegacyBoot (\\2\\3;'
-    ],
-#    [
-#        re.compile("(\W)(PEI_PCI_CFG_PPI)(\W)", re.MULTILINE),
-#        '\\1ECP_\\2\\3'
-#    ]
-]
-
-## file cache to avoid circular include in ASL file
-gIncludedAslFile = []
-
-## Trim preprocessed source code
-#
-# Remove extra content made by preprocessor. The preprocessor must enable the
-# line number generation option when preprocessing.
-#
-# @param  Source    File to be trimmed
-# @param  Target    File to store the trimmed content
-# @param  Convert   If True, convert standard HEX format to MASM format
-#
-def TrimPreprocessedFile(Source, Target, Convert):
-    CreateDirectory(os.path.dirname(Target))
-    try:
-        f = open (Source, 'r')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
-
-    # read whole file
-    Lines = f.readlines()
-    f.close()
-
-    PreprocessedFile = ""
-    InjectedFile = ""
-    LineIndexOfOriginalFile = None
-    NewLines = []
-    LineControlDirectiveFound = False
-    for Index in range(len(Lines)):
-        Line = Lines[Index]
-        #
-        # Find out the name of files injected by preprocessor from the lines
-        # with Line Control directive
-        #
-        MatchList = gLineControlDirective.findall(Line)
-        if MatchList != []:
-            MatchList = MatchList[0]
-            if len(MatchList) == 2:
-                LineNumber = int(MatchList[0], 0)
-                InjectedFile = MatchList[1]
-                # The first injetcted file must be the preprocessed file itself
-                if PreprocessedFile == "":
-                    PreprocessedFile = InjectedFile
-            LineControlDirectiveFound = True
-            continue
-        elif PreprocessedFile == "" or InjectedFile != PreprocessedFile:
-            continue
-
-        if LineIndexOfOriginalFile == None:
-            #
-            # Any non-empty lines must be from original preprocessed file.
-            # And this must be the first one.
-            #
-            LineIndexOfOriginalFile = Index
-            EdkLogger.verbose("Found original file content starting from line %d"
-                              % (LineIndexOfOriginalFile + 1))
-
-        # convert HEX number format if indicated
-        if Convert:
-            Line = gHexNumberPattern.sub(r"0\1h", Line)
-
-        if LineNumber != None:
-            EdkLogger.verbose("Got line directive: line=%d" % LineNumber)
-            # in case preprocessor removed some lines, like blank or comment lines
-            if LineNumber <= len(NewLines):
-                # possible?
-                NewLines[LineNumber - 1] = Line
-            else:
-                if LineNumber > (len(NewLines) + 1):
-                    for LineIndex in range(len(NewLines), LineNumber-1):
-                        NewLines.append(os.linesep)
-                NewLines.append(Line)
-            LineNumber = None
-            EdkLogger.verbose("Now we have lines: %d" % len(NewLines))
-        else:
-            NewLines.append(Line)
-
-    # in case there's no line directive or linemarker found
-    if (not LineControlDirectiveFound) and NewLines == []:
-        NewLines = Lines
-
-    # save to file
-    try:
-        f = open (Target, 'wb')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
-    f.writelines(NewLines)
-    f.close()
-
-## Trim preprocessed VFR file
-#
-# Remove extra content made by preprocessor. The preprocessor doesn't need to
-# enable line number generation option when preprocessing.
-#
-# @param  Source    File to be trimmed
-# @param  Target    File to store the trimmed content
-#
-def TrimPreprocessedVfr(Source, Target):
-    CreateDirectory(os.path.dirname(Target))
-    
-    try:
-        f = open (Source,'r')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
-    # read whole file
-    Lines = f.readlines()
-    f.close()
-
-    FoundTypedef = False
-    Brace = 0
-    TypedefStart = 0
-    TypedefEnd = 0
-    for Index in range(len(Lines)):
-        Line = Lines[Index]
-        # don't trim the lines from "formset" definition to the end of file
-        if Line.strip() == 'formset':
-            break
-
-        if FoundTypedef == False and (Line.find('#line') == 0 or Line.find('# ') == 0):
-            # empty the line number directive if it's not aomong "typedef struct"
-            Lines[Index] = "\n"
-            continue
-
-        if FoundTypedef == False and gTypedefPattern.search(Line) == None:
-            # keep "#pragram pack" directive
-            if gPragmaPattern.search(Line) == None:
-                Lines[Index] = "\n"
-            continue
-        elif FoundTypedef == False:
-            # found "typedef struct", keept its position and set a flag
-            FoundTypedef = True
-            TypedefStart = Index
-
-        # match { and } to find the end of typedef definition
-        if Line.find("{") >= 0:
-            Brace += 1
-        elif Line.find("}") >= 0:
-            Brace -= 1
-
-        # "typedef struct" must end with a ";"
-        if Brace == 0 and Line.find(";") >= 0:
-            FoundTypedef = False
-            TypedefEnd = Index
-            # keep all "typedef struct" except to GUID, EFI_PLABEL and PAL_CALL_RETURN
-            if Line.strip("} ;\r\n") in ["GUID", "EFI_PLABEL", "PAL_CALL_RETURN"]:
-                for i in range(TypedefStart, TypedefEnd+1):
-                    Lines[i] = "\n"
-
-    # save all lines trimmed
-    try:
-        f = open (Target,'w')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
-    f.writelines(Lines)
-    f.close()
-
-## Read the content  ASL file, including ASL included, recursively
-#
-# @param  Source    File to be read
-# @param  Indent    Spaces before the Include() statement
-#
-def DoInclude(Source, Indent=''):
-    NewFileContent = []
-    # avoid A "include" B and B "include" A
-    if Source in gIncludedAslFile:
-        EdkLogger.warn("Trim", "Circular include",
-                       ExtraData= "%s -> %s" % (" -> ".join(gIncludedAslFile), Source))
-        return []
-    gIncludedAslFile.append(Source)
-
-    try:
-        F = open(Source,'r')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
-
-    for Line in F:
-        Result = gAslIncludePattern.findall(Line)
-        if len(Result) == 0:
-            NewFileContent.append("%s%s" % (Indent, Line))
-            continue
-        CurrentIndent = Indent + Result[0][0]
-        IncludedFile = Result[0][1]
-        NewFileContent.extend(DoInclude(IncludedFile, CurrentIndent))
-
-    gIncludedAslFile.pop()
-    F.close()
-
-    return NewFileContent
-
-
-## Trim ASL file
-#
-# Replace ASL include statement with the content the included file
-#
-# @param  Source    File to be trimmed
-# @param  Target    File to store the trimmed content
-#
-def TrimAslFile(Source, Target):
-    CreateDirectory(os.path.dirname(Target))
-    
-    Cwd = os.getcwd()
-    SourceDir = os.path.dirname(Source)
-    if SourceDir == '':
-        SourceDir = '.'
-    os.chdir(SourceDir)
-    Lines = DoInclude(Source)
-    os.chdir(Cwd)
-
-    # save all lines trimmed
-    try:
-        f = open (Target,'w')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
-
-    f.writelines(Lines)
-    f.close()
-
-## Trim EDK source code file(s)
-#
-#
-# @param  Source    File or directory to be trimmed
-# @param  Target    File or directory to store the trimmed content
-#
-def TrimR8Sources(Source, Target):
-    if os.path.isdir(Source):
-        for CurrentDir, Dirs, Files in os.walk(Source):
-            if '.svn' in Dirs:
-                Dirs.remove('.svn')
-            elif "CVS" in Dirs:
-                Dirs.remove("CVS")
-
-            for FileName in Files:
-                Dummy, Ext = os.path.splitext(FileName)
-                if Ext.upper() not in ['.C', '.H']: continue
-                if Target == None or Target == '':
-                    TrimR8SourceCode(
-                        os.path.join(CurrentDir, FileName),
-                        os.path.join(CurrentDir, FileName)
-                        )
-                else:
-                    TrimR8SourceCode(
-                        os.path.join(CurrentDir, FileName),
-                        os.path.join(Target, CurrentDir[len(Source)+1:], FileName)
-                        )
-    else:
-        TrimR8SourceCode(Source, Target)
-
-## Trim one EDK source code file
-#
-# Do following replacement:
-#
-#   (**PeiServices\).PciCfg = <*>;
-#   =>  {
-#         STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {
-#         (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),
-#         &gEcpPeiPciCfgPpiGuid,
-#         <*>
-#       };
-#       (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);
-#
-#   <*>Modify(<*>)
-#   =>  PeiLibPciCfgModify (<*>)
-#
-#   gRT->ReportStatusCode (<*>)
-#   => EfiLibReportStatusCode (<*>)
-#
-#   #include <LoadFile\.h>
-#   =>  #include <FvLoadFile.h>
-#
-#   CreateEvent (EFI_EVENT_SIGNAL_READY_TO_BOOT, <*>)
-#   => EfiCreateEventReadyToBoot (<*>)
-#
-#   CreateEvent (EFI_EVENT_SIGNAL_LEGACY_BOOT, <*>)
-#   =>  EfiCreateEventLegacyBoot (<*>)
-#
-# @param  Source    File to be trimmed
-# @param  Target    File to store the trimmed content
-#
-def TrimR8SourceCode(Source, Target):
-    EdkLogger.verbose("\t%s -> %s" % (Source, Target))
-    CreateDirectory(os.path.dirname(Target))
-
-    try:
-        f = open (Source,'rb')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)
-    # read whole file
-    Lines = f.read()
-    f.close()
-
-    NewLines = None
-    for Re,Repl in gImportCodePatterns:
-        if NewLines == None:
-            NewLines = Re.sub(Repl, Lines)
-        else:
-            NewLines = Re.sub(Repl, NewLines)
-
-    # save all lines if trimmed
-    if Source == Target and NewLines == Lines:
-        return
-
-    try:
-        f = open (Target,'wb')
-    except:
-        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)
-    f.write(NewLines)
-    f.close()
-
-
-## Parse command line options
-#
-# Using standard Python module optparse to parse command line option of this tool.
-#
-# @retval Options   A optparse.Values object containing the parsed options
-# @retval InputFile Path of file to be trimmed
-#
-def Options():
-    OptionList = [
-        make_option("-s", "--source-code", dest="FileType", const="SourceCode", action="store_const",
-                          help="The input file is preprocessed source code, including C or assembly code"),
-        make_option("-r", "--vfr-file", dest="FileType", const="Vfr", action="store_const",
-                          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("-c", "--convert-hex", dest="ConvertHex", action="store_true",
-                          help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),
-
-        make_option("-o", "--output", dest="OutputFile",
-                          help="File to store the trimmed content"),
-        make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,
-                          help="Run verbosely"),
-        make_option("-d", "--debug", dest="LogLevel", type="int",
-                          help="Run with debug information"),
-        make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,
-                          help="Run quietly"),
-        make_option("-?", action="help", help="show this help message and exit"),
-    ]
-
-    # use clearer usage to override default usage message
-    UsageString = "%prog [-s|-r|-a] [-c] [-v|-d <debug_level>|-q] [-o <output_file>] <input_file>"
-
-    Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)
-    Parser.set_defaults(FileType="Vfr")
-    Parser.set_defaults(ConvertHex=False)
-    Parser.set_defaults(LogLevel=EdkLogger.INFO)
-
-    Options, Args = Parser.parse_args()
-
-    # error check
-    if len(Args) == 0:
-        EdkLogger.error("Trim", OPTION_MISSING, ExtraData=Parser.get_usage())
-    if len(Args) > 1:
-        EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())
-
-    InputFile = Args[0]
-    return Options, InputFile
-
-## Entrance method
-#
-# This method mainly dispatch specific methods per the command line options.
-# If no error found, return zero value so the caller of this tool can know
-# if it's executed successfully or not.
-#
-# @retval 0     Tool was successful
-# @retval 1     Tool failed
-#
-def Main():
-    try:
-        EdkLogger.Initialize()
-        CommandOptions, InputFile = Options()
-        if CommandOptions.LogLevel < EdkLogger.DEBUG_9:
-            EdkLogger.SetLevel(CommandOptions.LogLevel + 1)
-        else:
-            EdkLogger.SetLevel(CommandOptions.LogLevel)
-    except FatalError, X:
-        return 1
-    
-    try:
-        if CommandOptions.FileType == "Vfr":
-            if CommandOptions.OutputFile == None:
-                CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'
-            TrimPreprocessedVfr(InputFile, CommandOptions.OutputFile)
-        elif CommandOptions.FileType == "Asl":
-            if CommandOptions.OutputFile == None:
-                CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'
-            TrimAslFile(InputFile, CommandOptions.OutputFile)
-        elif CommandOptions.FileType == "R8SourceCode":
-            TrimR8Sources(InputFile, CommandOptions.OutputFile)
-        else :
-            if CommandOptions.OutputFile == None:
-                CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'
-            TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex)
-    except FatalError, X:
-        import platform
-        import traceback
-        if CommandOptions != None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:
-            EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
-        return 1
-    except:
-        import traceback
-        import platform
-        EdkLogger.error(
-                    "\nTrim",
-                    CODE_ERROR,
-                    "Unknown fatal error when trimming [%s]" % InputFile,
-                    ExtraData="\n(Please send email to edk2-buildtools-devel@lists.sourceforge.net for help, attaching following call stack trace!)\n",
-                    RaiseError=False
-                    )
-        EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
-        return 1
-
-    return 0
-
-if __name__ == '__main__':
-    r = Main()
-    ## 0-127 is a safe return range, and 1 is a standard default error
-    if r < 0 or r > 127: r = 1
-    sys.exit(r)
-
+## @file\r
+# Trim files preprocessed by compiler\r
+#\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
+# http://opensource.org/licenses/bsd-license.php\r
+#\r
+# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+#\r
+\r
+##\r
+# Import Modules\r
+#\r
+import Common.LongFilePathOs as os\r
+import sys\r
+import re\r
+import StringIO\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.BuildVersion import gBUILD_VERSION\r
+import Common.EdkLogger as EdkLogger\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\r
+\r
+# Version and Copyright\r
+__version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
+__version__ = "%prog Version " + __version_number__\r
+__copyright__ = "Copyright (c) 2007-2017, Intel Corporation. All rights reserved."\r
+\r
+## Regular expression for matching Line Control directive like "#line xxx"\r
+gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')\r
+## Regular expression for matching "typedef struct"\r
+gTypedefPattern = re.compile("^\s*typedef\s+struct(\s+\w+)?\s*[{]*$", re.MULTILINE)\r
+## Regular expression for matching "#pragma pack"\r
+gPragmaPattern = re.compile("^\s*#pragma\s+pack", re.MULTILINE)\r
+## Regular expression for matching "typedef"\r
+gTypedef_SinglePattern = re.compile("^\s*typedef", re.MULTILINE)\r
+## Regular expression for matching "typedef struct, typedef union, struct, union"\r
+gTypedef_MulPattern = re.compile("^\s*(typedef)?\s+(struct|union)(\s+\w+)?\s*[{]*$", re.MULTILINE)\r
+\r
+#\r
+# The following number pattern match will only match if following criteria is met:\r
+# There is leading non-(alphanumeric or _) character, and no following alphanumeric or _\r
+# as the pattern is greedily match, so it is ok for the gDecNumberPattern or gHexNumberPattern to grab the maximum match\r
+#\r
+## Regular expression for matching HEX number\r
+gHexNumberPattern = re.compile("(?<=[^a-zA-Z0-9_])(0[xX])([0-9a-fA-F]+)(U(?=$|[^a-zA-Z0-9_]))?")\r
+## Regular expression for matching decimal number with 'U' postfix\r
+gDecNumberPattern = re.compile("(?<=[^a-zA-Z0-9_])([0-9]+)U(?=$|[^a-zA-Z0-9_])")\r
+## Regular expression for matching constant with 'ULL' 'LL' postfix\r
+gLongNumberPattern = re.compile("(?<=[^a-zA-Z0-9_])(0[xX][0-9a-fA-F]+|[0-9]+)U?LL(?=$|[^a-zA-Z0-9_])")\r
+\r
+## Regular expression for matching "Include ()" in asl file\r
+gAslIncludePattern = re.compile("^(\s*)[iI]nclude\s*\(\"?([^\"\(\)]+)\"\)", re.MULTILINE)\r
+## Regular expression for matching C style #include "XXX.asl" in asl file\r
+gAslCIncludePattern = re.compile(r'^(\s*)#include\s*[<"]\s*([-\\/\w.]+)\s*([>"])', re.MULTILINE)\r
+## Patterns used to convert EDK conventions to EDK2 ECP conventions\r
+gImportCodePatterns = [\r
+    [\r
+        re.compile('^(\s*)\(\*\*PeiServices\)\.PciCfg\s*=\s*([^;\s]+);', re.MULTILINE),\r
+        '''\\1{\r
+\\1  STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {\r
+\\1    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+\\1    &gEcpPeiPciCfgPpiGuid,\r
+\\1    \\2\r
+\\1  };\r
+\\1  (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);\r
+\\1}'''\r
+    ],\r
+\r
+    [\r
+        re.compile('^(\s*)\(\*PeiServices\)->PciCfg\s*=\s*([^;\s]+);', re.MULTILINE),\r
+        '''\\1{\r
+\\1  STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {\r
+\\1    (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+\\1    &gEcpPeiPciCfgPpiGuid,\r
+\\1    \\2\r
+\\1  };\r
+\\1  (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);\r
+\\1}'''\r
+    ],\r
+\r
+    [\r
+        re.compile("(\s*).+->Modify[\s\n]*\(", re.MULTILINE),\r
+        '\\1PeiLibPciCfgModify ('\r
+    ],\r
+\r
+    [\r
+        re.compile("(\W*)gRT->ReportStatusCode[\s\n]*\(", re.MULTILINE),\r
+        '\\1EfiLibReportStatusCode ('\r
+    ],\r
+\r
+    [\r
+        re.compile('#include\s+EFI_GUID_DEFINITION\s*\(FirmwareFileSystem\)', re.MULTILINE),\r
+        '#include EFI_GUID_DEFINITION (FirmwareFileSystem)\n#include EFI_GUID_DEFINITION (FirmwareFileSystem2)'\r
+    ],\r
+\r
+    [\r
+        re.compile('gEfiFirmwareFileSystemGuid', re.MULTILINE),\r
+        'gEfiFirmwareFileSystem2Guid'\r
+    ],\r
+\r
+    [\r
+        re.compile('EFI_FVH_REVISION', re.MULTILINE),\r
+        'EFI_FVH_PI_REVISION'\r
+    ],\r
+\r
+    [\r
+        re.compile("(\s*)\S*CreateEvent\s*\([\s\n]*EFI_EVENT_SIGNAL_READY_TO_BOOT[^,]*,((?:[^;]+\n)+)(\s*\));", re.MULTILINE),\r
+        '\\1EfiCreateEventReadyToBoot (\\2\\3;'\r
+    ],\r
+\r
+    [\r
+        re.compile("(\s*)\S*CreateEvent\s*\([\s\n]*EFI_EVENT_SIGNAL_LEGACY_BOOT[^,]*,((?:[^;]+\n)+)(\s*\));", re.MULTILINE),\r
+        '\\1EfiCreateEventLegacyBoot (\\2\\3;'\r
+    ],\r
+#    [\r
+#        re.compile("(\W)(PEI_PCI_CFG_PPI)(\W)", re.MULTILINE),\r
+#        '\\1ECP_\\2\\3'\r
+#    ]\r
+]\r
+\r
+## file cache to avoid circular include in ASL file\r
+gIncludedAslFile = []\r
+\r
+## Trim preprocessed source code\r
+#\r
+# Remove extra content made by preprocessor. The preprocessor must enable the\r
+# line number generation option when preprocessing.\r
+#\r
+# @param  Source    File to be trimmed\r
+# @param  Target    File to store the trimmed content\r
+# @param  Convert   If True, convert standard HEX format to MASM format\r
+#\r
+def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):\r
+    CreateDirectory(os.path.dirname(Target))\r
+    try:\r
+        f = open (Source, 'r')\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
+\r
+    # read whole file\r
+    Lines = f.readlines()\r
+    f.close()\r
+\r
+    PreprocessedFile = ""\r
+    InjectedFile = ""\r
+    LineIndexOfOriginalFile = None\r
+    NewLines = []\r
+    LineControlDirectiveFound = False\r
+    for Index in range(len(Lines)):\r
+        Line = Lines[Index]\r
+        #\r
+        # Find out the name of files injected by preprocessor from the lines\r
+        # with Line Control directive\r
+        #\r
+        MatchList = gLineControlDirective.findall(Line)\r
+        if MatchList != []:\r
+            MatchList = MatchList[0]\r
+            if len(MatchList) == 2:\r
+                LineNumber = int(MatchList[0], 0)\r
+                InjectedFile = MatchList[1]\r
+                # The first injetcted file must be the preprocessed file itself\r
+                if PreprocessedFile == "":\r
+                    PreprocessedFile = InjectedFile\r
+            LineControlDirectiveFound = True\r
+            continue\r
+        elif PreprocessedFile == "" or InjectedFile != PreprocessedFile:\r
+            continue\r
+\r
+        if LineIndexOfOriginalFile is None:\r
+            #\r
+            # Any non-empty lines must be from original preprocessed file.\r
+            # And this must be the first one.\r
+            #\r
+            LineIndexOfOriginalFile = Index\r
+            EdkLogger.verbose("Found original file content starting from line %d"\r
+                              % (LineIndexOfOriginalFile + 1))\r
+\r
+        if TrimLong:\r
+            Line = gLongNumberPattern.sub(r"\1", Line)\r
+        # convert HEX number format if indicated\r
+        if ConvertHex:\r
+            Line = gHexNumberPattern.sub(r"0\2h", Line)\r
+        else:\r
+            Line = gHexNumberPattern.sub(r"\1\2", Line)\r
+\r
+        # convert Decimal number format\r
+        Line = gDecNumberPattern.sub(r"\1", Line)\r
+\r
+        if LineNumber is not None:\r
+            EdkLogger.verbose("Got line directive: line=%d" % LineNumber)\r
+            # in case preprocessor removed some lines, like blank or comment lines\r
+            if LineNumber <= len(NewLines):\r
+                # possible?\r
+                NewLines[LineNumber - 1] = Line\r
+            else:\r
+                if LineNumber > (len(NewLines) + 1):\r
+                    for LineIndex in range(len(NewLines), LineNumber-1):\r
+                        NewLines.append(os.linesep)\r
+                NewLines.append(Line)\r
+            LineNumber = None\r
+            EdkLogger.verbose("Now we have lines: %d" % len(NewLines))\r
+        else:\r
+            NewLines.append(Line)\r
+\r
+    # in case there's no line directive or linemarker found\r
+    if (not LineControlDirectiveFound) and NewLines == []:\r
+        MulPatternFlag = False\r
+        SinglePatternFlag = False\r
+        Brace = 0\r
+        for Index in range(len(Lines)):\r
+            Line = Lines[Index]\r
+            if MulPatternFlag == False and gTypedef_MulPattern.search(Line) is None:\r
+                if SinglePatternFlag == False and gTypedef_SinglePattern.search(Line) is None:\r
+                    # remove "#pragram pack" directive\r
+                    if gPragmaPattern.search(Line) is None:\r
+                        NewLines.append(Line)\r
+                    continue\r
+                elif SinglePatternFlag == False:\r
+                    SinglePatternFlag = True\r
+                if Line.find(";") >= 0:\r
+                    SinglePatternFlag = False\r
+            elif MulPatternFlag == False:\r
+                # found "typedef struct, typedef union, union, struct", keep its position and set a flag\r
+                MulPatternFlag = True\r
+\r
+            # match { and } to find the end of typedef definition\r
+            if Line.find("{") >= 0:\r
+                Brace += 1\r
+            elif Line.find("}") >= 0:\r
+                Brace -= 1\r
+\r
+            # "typedef struct, typedef union, union, struct" must end with a ";"\r
+            if Brace == 0 and Line.find(";") >= 0:\r
+                MulPatternFlag = False\r
+\r
+    # save to file\r
+    try:\r
+        f = open (Target, 'wb')\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
+    f.writelines(NewLines)\r
+    f.close()\r
+\r
+## Trim preprocessed VFR file\r
+#\r
+# Remove extra content made by preprocessor. The preprocessor doesn't need to\r
+# enable line number generation option when preprocessing.\r
+#\r
+# @param  Source    File to be trimmed\r
+# @param  Target    File to store the trimmed content\r
+#\r
+def TrimPreprocessedVfr(Source, Target):\r
+    CreateDirectory(os.path.dirname(Target))\r
+    \r
+    try:\r
+        f = open (Source,'r')\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
+    # read whole file\r
+    Lines = f.readlines()\r
+    f.close()\r
+\r
+    FoundTypedef = False\r
+    Brace = 0\r
+    TypedefStart = 0\r
+    TypedefEnd = 0\r
+    for Index in range(len(Lines)):\r
+        Line = Lines[Index]\r
+        # don't trim the lines from "formset" definition to the end of file\r
+        if Line.strip() == 'formset':\r
+            break\r
+\r
+        if FoundTypedef == False and (Line.find('#line') == 0 or Line.find('# ') == 0):\r
+            # empty the line number directive if it's not aomong "typedef struct"\r
+            Lines[Index] = "\n"\r
+            continue\r
+\r
+        if FoundTypedef == False and gTypedefPattern.search(Line) is None:\r
+            # keep "#pragram pack" directive\r
+            if gPragmaPattern.search(Line) is None:\r
+                Lines[Index] = "\n"\r
+            continue\r
+        elif FoundTypedef == False:\r
+            # found "typedef struct", keept its position and set a flag\r
+            FoundTypedef = True\r
+            TypedefStart = Index\r
+\r
+        # match { and } to find the end of typedef definition\r
+        if Line.find("{") >= 0:\r
+            Brace += 1\r
+        elif Line.find("}") >= 0:\r
+            Brace -= 1\r
+\r
+        # "typedef struct" must end with a ";"\r
+        if Brace == 0 and Line.find(";") >= 0:\r
+            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
+                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
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
+    f.writelines(Lines)\r
+    f.close()\r
+\r
+## Read the content  ASL file, including ASL included, recursively\r
+#\r
+# @param  Source            File to be read\r
+# @param  Indent            Spaces before the Include() statement\r
+# @param  IncludePathList   The list of external include file\r
+# @param  LocalSearchPath   If LocalSearchPath is specified, this path will be searched\r
+#                           first for the included file; otherwise, only the path specified\r
+#                           in the IncludePathList will be searched.\r
+#\r
+def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):\r
+    NewFileContent = []\r
+\r
+    try:\r
+        #\r
+        # Search LocalSearchPath first if it is specified.\r
+        #\r
+        if LocalSearchPath:\r
+            SearchPathList = [LocalSearchPath] + IncludePathList\r
+        else:\r
+            SearchPathList = IncludePathList\r
+  \r
+        for IncludePath in SearchPathList:\r
+            IncludeFile = os.path.join(IncludePath, Source)\r
+            if os.path.isfile(IncludeFile):\r
+                F = open(IncludeFile, "r")\r
+                break\r
+        else:\r
+            EdkLogger.error("Trim", "Failed to find include file %s" % Source)\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
+\r
+    \r
+    # avoid A "include" B and B "include" A\r
+    IncludeFile = os.path.abspath(os.path.normpath(IncludeFile))\r
+    if IncludeFile in gIncludedAslFile:\r
+        EdkLogger.warn("Trim", "Circular include",\r
+                       ExtraData= "%s -> %s" % (" -> ".join(gIncludedAslFile), IncludeFile))\r
+        return []\r
+    gIncludedAslFile.append(IncludeFile)\r
+    \r
+    for Line in F:\r
+        LocalSearchPath = None\r
+        Result = gAslIncludePattern.findall(Line)\r
+        if len(Result) == 0:\r
+            Result = gAslCIncludePattern.findall(Line)\r
+            if len(Result) == 0 or os.path.splitext(Result[0][1])[1].lower() not in [".asl", ".asi"]:\r
+                NewFileContent.append("%s%s" % (Indent, Line))\r
+                continue\r
+            #\r
+            # We should first search the local directory if current file are using pattern #include "XXX" \r
+            #\r
+            if Result[0][2] == '"':\r
+                LocalSearchPath = os.path.dirname(IncludeFile)\r
+        CurrentIndent = Indent + Result[0][0]\r
+        IncludedFile = Result[0][1]\r
+        NewFileContent.extend(DoInclude(IncludedFile, CurrentIndent, IncludePathList, LocalSearchPath))\r
+        NewFileContent.append("\n")\r
+\r
+    gIncludedAslFile.pop()\r
+    F.close()\r
+\r
+    return NewFileContent\r
+\r
+\r
+## Trim ASL file\r
+#\r
+# Replace ASL include statement with the content the included file\r
+#\r
+# @param  Source          File to be trimmed\r
+# @param  Target          File to store the trimmed content\r
+# @param  IncludePathFile The file to log the external include path \r
+#\r
+def TrimAslFile(Source, Target, IncludePathFile):\r
+    CreateDirectory(os.path.dirname(Target))\r
+    \r
+    SourceDir = os.path.dirname(Source)\r
+    if SourceDir == '':\r
+        SourceDir = '.'\r
+    \r
+    #\r
+    # Add source directory as the first search directory\r
+    #\r
+    IncludePathList = [SourceDir]\r
+    \r
+    #\r
+    # If additional include path file is specified, append them all\r
+    # to the search directory list.\r
+    #\r
+    if IncludePathFile:\r
+        try:\r
+            LineNum = 0\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
+                else:\r
+                    EdkLogger.warn("Trim", "Invalid include line in include list file.", IncludePathFile, LineNum)\r
+        except:\r
+            EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=IncludePathFile)\r
+\r
+    Lines = DoInclude(Source, '', IncludePathList)\r
+\r
+    #\r
+    # Undef MIN and MAX to avoid collision in ASL source code\r
+    #\r
+    Lines.insert(0, "#undef MIN\n#undef MAX\n")\r
+\r
+    # save all lines trimmed\r
+    try:\r
+        f = open (Target,'w')\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
+\r
+    f.writelines(Lines)\r
+    f.close()\r
+\r
+def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):\r
+    VfrNameList = []\r
+    if os.path.isdir(DebugDir):\r
+        for CurrentDir, Dirs, Files in os.walk(DebugDir):\r
+            for FileName in Files:\r
+                Name, Ext = os.path.splitext(FileName)\r
+                if Ext == '.c' and Name != 'AutoGen':\r
+                    VfrNameList.append (Name + 'Bin')\r
+\r
+    VfrNameList.append (ModuleName + 'Strings')\r
+\r
+    EfiFileName = os.path.join(DebugDir, ModuleName + '.efi')\r
+    MapFileName = os.path.join(DebugDir, ModuleName + '.map')\r
+    VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, VfrNameList)\r
+\r
+    if not VfrUniOffsetList:\r
+        return\r
+\r
+    try:\r
+        fInputfile = open(OutputFile, "wb+", 0)\r
+    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
+\r
+    for Item in VfrUniOffsetList:\r
+        if (Item[0].find("Strings") != -1):\r
+            #\r
+            # UNI offset in image.\r
+            # GUID + Offset\r
+            # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
+            #\r
+            UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]\r
+            UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]\r
+            fStringIO.write(''.join(UniGuid))\r
+            UniValue = pack ('Q', int (Item[1], 16))\r
+            fStringIO.write (UniValue)\r
+        else:\r
+            #\r
+            # VFR binary offset in image.\r
+            # GUID + Offset\r
+            # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
+            #\r
+            VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
+            VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]\r
+            fStringIO.write(''.join(VfrGuid))\r
+            type (Item[1])\r
+            VfrValue = pack ('Q', int (Item[1], 16))\r
+            fStringIO.write (VfrValue)\r
+\r
+    #\r
+    # write data into file.\r
+    #\r
+    try :\r
+        fInputfile.write (fStringIO.getvalue())\r
+    except:\r
+        EdkLogger.error("Trim", FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the file been locked or using by other applications." %OutputFile, None)\r
+\r
+    fStringIO.close ()\r
+    fInputfile.close ()\r
+\r
+## Trim EDK source code file(s)\r
+#\r
+#\r
+# @param  Source    File or directory to be trimmed\r
+# @param  Target    File or directory to store the trimmed content\r
+#\r
+def TrimEdkSources(Source, Target):\r
+    if os.path.isdir(Source):\r
+        for CurrentDir, Dirs, Files in os.walk(Source):\r
+            if '.svn' in Dirs:\r
+                Dirs.remove('.svn')\r
+            elif "CVS" in Dirs:\r
+                Dirs.remove("CVS")\r
+\r
+            for FileName in Files:\r
+                Dummy, Ext = os.path.splitext(FileName)\r
+                if Ext.upper() not in ['.C', '.H']: continue\r
+                if Target is None or Target == '':\r
+                    TrimEdkSourceCode(\r
+                        os.path.join(CurrentDir, FileName),\r
+                        os.path.join(CurrentDir, FileName)\r
+                        )\r
+                else:\r
+                    TrimEdkSourceCode(\r
+                        os.path.join(CurrentDir, FileName),\r
+                        os.path.join(Target, CurrentDir[len(Source)+1:], FileName)\r
+                        )\r
+    else:\r
+        TrimEdkSourceCode(Source, Target)\r
+\r
+## Trim one EDK source code file\r
+#\r
+# Do following replacement:\r
+#\r
+#   (**PeiServices\).PciCfg = <*>;\r
+#   =>  {\r
+#         STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {\r
+#         (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
+#         &gEcpPeiPciCfgPpiGuid,\r
+#         <*>\r
+#       };\r
+#       (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);\r
+#\r
+#   <*>Modify(<*>)\r
+#   =>  PeiLibPciCfgModify (<*>)\r
+#\r
+#   gRT->ReportStatusCode (<*>)\r
+#   => EfiLibReportStatusCode (<*>)\r
+#\r
+#   #include <LoadFile\.h>\r
+#   =>  #include <FvLoadFile.h>\r
+#\r
+#   CreateEvent (EFI_EVENT_SIGNAL_READY_TO_BOOT, <*>)\r
+#   => EfiCreateEventReadyToBoot (<*>)\r
+#\r
+#   CreateEvent (EFI_EVENT_SIGNAL_LEGACY_BOOT, <*>)\r
+#   =>  EfiCreateEventLegacyBoot (<*>)\r
+#\r
+# @param  Source    File to be trimmed\r
+# @param  Target    File to store the trimmed content\r
+#\r
+def TrimEdkSourceCode(Source, Target):\r
+    EdkLogger.verbose("\t%s -> %s" % (Source, Target))\r
+    CreateDirectory(os.path.dirname(Target))\r
+\r
+    try:\r
+        f = open (Source,'rb')\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
+    # read whole file\r
+    Lines = f.read()\r
+    f.close()\r
+\r
+    NewLines = None\r
+    for Re,Repl in gImportCodePatterns:\r
+        if NewLines is None:\r
+            NewLines = Re.sub(Repl, Lines)\r
+        else:\r
+            NewLines = Re.sub(Repl, NewLines)\r
+\r
+    # save all lines if trimmed\r
+    if Source == Target and NewLines == Lines:\r
+        return\r
+\r
+    try:\r
+        f = open (Target,'wb')\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
+    f.write(NewLines)\r
+    f.close()\r
+\r
+\r
+## Parse command line options\r
+#\r
+# Using standard Python module optparse to parse command line option of this tool.\r
+#\r
+# @retval Options   A optparse.Values object containing the parsed options\r
+# @retval InputFile Path of file to be trimmed\r
+#\r
+def Options():\r
+    OptionList = [\r
+        make_option("-s", "--source-code", dest="FileType", const="SourceCode", action="store_const",\r
+                          help="The input file is preprocessed source code, including C or assembly code"),\r
+        make_option("-r", "--vfr-file", dest="FileType", const="Vfr", action="store_const",\r
+                          help="The input file is preprocessed VFR file"),\r
+        make_option("--Vfr-Uni-Offset", dest="FileType", const="VfrOffsetBin", action="store_const",\r
+                          help="The input file is EFI image"),\r
+        make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",\r
+                          help="The input file is ASL file"),\r
+        make_option("-8", "--Edk-source-code", dest="FileType", const="EdkSourceCode", action="store_const",\r
+                          help="The input file is source code for Edk to be trimmed for ECP"),\r
+\r
+        make_option("-c", "--convert-hex", dest="ConvertHex", action="store_true",\r
+                          help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),\r
+\r
+        make_option("-l", "--trim-long", dest="TrimLong", action="store_true",\r
+                          help="Remove postfix of long number"),\r
+        make_option("-i", "--include-path-file", dest="IncludePathFile",\r
+                          help="The input file is include path list to search for ASL include file"),\r
+        make_option("-o", "--output", dest="OutputFile",\r
+                          help="File to store the trimmed content"),\r
+        make_option("--ModuleName", dest="ModuleName", help="The module's BASE_NAME"),\r
+        make_option("--DebugDir", dest="DebugDir",\r
+                          help="Debug Output directory to store the output files"),\r
+        make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,\r
+                          help="Run verbosely"),\r
+        make_option("-d", "--debug", dest="LogLevel", type="int",\r
+                          help="Run with debug information"),\r
+        make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,\r
+                          help="Run quietly"),\r
+        make_option("-?", action="help", help="show this help message and exit"),\r
+    ]\r
+\r
+    # use clearer usage to override default usage message\r
+    UsageString = "%prog [-s|-r|-a|--Vfr-Uni-Offset] [-c] [-v|-d <debug_level>|-q] [-i <include_path_file>] [-o <output_file>] [--ModuleName <ModuleName>] [--DebugDir <DebugDir>] [<input_file>]"\r
+\r
+    Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)\r
+    Parser.set_defaults(FileType="Vfr")\r
+    Parser.set_defaults(ConvertHex=False)\r
+    Parser.set_defaults(LogLevel=EdkLogger.INFO)\r
+\r
+    Options, Args = Parser.parse_args()\r
+\r
+    # error check\r
+    if Options.FileType == 'VfrOffsetBin':\r
+        if len(Args) == 0:\r
+            return Options, ''\r
+        elif len(Args) > 1:\r
+            EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
+    if len(Args) == 0:\r
+        EdkLogger.error("Trim", OPTION_MISSING, ExtraData=Parser.get_usage())\r
+    if len(Args) > 1:\r
+        EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
+\r
+    InputFile = Args[0]\r
+    return Options, InputFile\r
+\r
+## Entrance method\r
+#\r
+# This method mainly dispatch specific methods per the command line options.\r
+# If no error found, return zero value so the caller of this tool can know\r
+# if it's executed successfully or not.\r
+#\r
+# @retval 0     Tool was successful\r
+# @retval 1     Tool failed\r
+#\r
+def Main():\r
+    try:\r
+        EdkLogger.Initialize()\r
+        CommandOptions, InputFile = Options()\r
+        if CommandOptions.LogLevel < EdkLogger.DEBUG_9:\r
+            EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
+        else:\r
+            EdkLogger.SetLevel(CommandOptions.LogLevel)\r
+    except FatalError, X:\r
+        return 1\r
+    \r
+    try:\r
+        if CommandOptions.FileType == "Vfr":\r
+            if CommandOptions.OutputFile is None:\r
+                CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
+            TrimPreprocessedVfr(InputFile, CommandOptions.OutputFile)\r
+        elif CommandOptions.FileType == "Asl":\r
+            if CommandOptions.OutputFile is None:\r
+                CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
+            TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)\r
+        elif CommandOptions.FileType == "EdkSourceCode":\r
+            TrimEdkSources(InputFile, CommandOptions.OutputFile)\r
+        elif CommandOptions.FileType == "VfrOffsetBin":\r
+            GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile)\r
+        else :\r
+            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
+        import platform\r
+        import traceback\r
+        if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:\r
+            EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
+        return 1\r
+    except:\r
+        import traceback\r
+        import platform\r
+        EdkLogger.error(\r
+                    "\nTrim",\r
+                    CODE_ERROR,\r
+                    "Unknown fatal error when trimming [%s]" % InputFile,\r
+                    ExtraData="\n(Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!)\n",\r
+                    RaiseError=False\r
+                    )\r
+        EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
+        return 1\r
+\r
+    return 0\r
+\r
+if __name__ == '__main__':\r
+    r = Main()\r
+    ## 0-127 is a safe return range, and 1 is a standard default error\r
+    if r < 0 or r > 127: r = 1\r
+    sys.exit(r)\r
+\r