]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Generate dependent files for ASL and ASM files
authorBob Feng <bob.c.feng@intel.com>
Wed, 20 Nov 2019 02:58:28 +0000 (10:58 +0800)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Tue, 10 Dec 2019 01:31:55 +0000 (01:31 +0000)
BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=2311

Implement the function in Trim tool to get the included
file list for ASL and ASM file.

Signed-off-by: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Steven Shi <steven.shi@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
BaseTools/Source/Python/Trim/Trim.py

index 24c3fafa76f9db94f388057e3d37f2f77e428ae3..c5638376e41a4cf88c269752ad398dfe38f85c8d 100644 (file)
@@ -56,6 +56,10 @@ gAslIncludePattern = re.compile("^(\s*)[iI]nclude\s*\(\"?([^\"\(\)]+)\"\)", re.M
 gAslCIncludePattern = re.compile(r'^(\s*)#include\s*[<"]\s*([-\\/\w.]+)\s*([>"])', re.MULTILINE)\r
 ## Patterns used to convert EDK conventions to EDK2 ECP conventions\r
 \r
+## Regular expression for finding header file inclusions\r
+gIncludePattern = re.compile(r"^[ \t]*[%]?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE | re.UNICODE | re.IGNORECASE)\r
+\r
+\r
 ## file cache to avoid circular include in ASL file\r
 gIncludedAslFile = []\r
 \r
@@ -253,9 +257,10 @@ def TrimPreprocessedVfr(Source, Target):
 #                           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
+def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None, IncludeFileList = None, filetype=None):\r
     NewFileContent = []\r
-\r
+    if IncludeFileList is None:\r
+        IncludeFileList = []\r
     try:\r
         #\r
         # Search LocalSearchPath first if it is specified.\r
@@ -288,24 +293,37 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
                        ExtraData= "%s -> %s" % (" -> ".join(gIncludedAslFile), IncludeFile))\r
         return []\r
     gIncludedAslFile.append(IncludeFile)\r
-\r
+    IncludeFileList.append(IncludeFile.strip())\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
+        if filetype == "ASL":\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,IncludeFileList,filetype))\r
+            NewFileContent.append("\n")\r
+        elif filetype == "ASM":\r
+            Result = gIncludePattern.findall(Line)\r
+            if len(Result) == 0:\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
+            IncludedFile = Result[0]\r
+\r
+            IncludedFile = IncludedFile.strip()\r
+            IncludedFile = os.path.normpath(IncludedFile)\r
+            NewFileContent.extend(DoInclude(IncludedFile, '', IncludePathList, LocalSearchPath,IncludeFileList,filetype))\r
+            NewFileContent.append("\n")\r
 \r
     gIncludedAslFile.pop()\r
 \r
@@ -320,7 +338,7 @@ def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):
 # @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
+def TrimAslFile(Source, Target, IncludePathFile,AslDeps = False):\r
     CreateDirectory(os.path.dirname(Target))\r
 \r
     SourceDir = os.path.dirname(Source)\r
@@ -349,8 +367,11 @@ def TrimAslFile(Source, Target, IncludePathFile):
                     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
+    AslIncludes = []\r
+    Lines = DoInclude(Source, '', IncludePathList,IncludeFileList=AslIncludes,filetype='ASL')\r
+    AslIncludes = [item for item in AslIncludes if item !=Source]\r
+    if AslDeps and AslIncludes:\r
+        SaveFileOnChange(os.path.join(os.path.dirname(Target),os.path.basename(Source))+".trim.deps", " \\\n".join([Source+":"] +AslIncludes),False)\r
 \r
     #\r
     # Undef MIN and MAX to avoid collision in ASL source code\r
@@ -364,6 +385,54 @@ def TrimAslFile(Source, Target, IncludePathFile):
     except:\r
         EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
 \r
+## Trim ASM file\r
+#\r
+# Output ASM 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 TrimAsmFile(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
+    # 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
+            with open(IncludePathFile, 'r') as File:\r
+                FileLines = File.readlines()\r
+            for Line in FileLines:\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
+    AsmIncludes = []\r
+    Lines = DoInclude(Source, '', IncludePathList,IncludeFileList=AsmIncludes,filetype='ASM')\r
+    AsmIncludes = [item for item in AsmIncludes if item != Source]\r
+    if AsmIncludes:\r
+        SaveFileOnChange(os.path.join(os.path.dirname(Target),os.path.basename(Source))+".trim.deps", " \\\n".join([Source+":"] +AsmIncludes),False)\r
+    # save all lines trimmed\r
+    try:\r
+        with open(Target, 'w') as File:\r
+            File.writelines(Lines)\r
+    except:\r
+        EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
+\r
 def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):\r
     VfrNameList = []\r
     if os.path.isdir(DebugDir):\r
@@ -440,8 +509,12 @@ def Options():
                           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("--asl-deps", dest="AslDeps", const="True", action="store_const",\r
+                          help="Generate Asl dependent files."),\r
         make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",\r
                           help="The input file is ASL file"),\r
+        make_option( "--asm-file", dest="FileType", const="Asm", action="store_const",\r
+                          help="The input file is asm file"),\r
         make_option("-c", "--convert-hex", dest="ConvertHex", action="store_true",\r
                           help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),\r
 \r
@@ -515,9 +588,11 @@ def Main():
         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
+            TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile,CommandOptions.AslDeps)\r
         elif CommandOptions.FileType == "VfrOffsetBin":\r
             GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile)\r
+        elif CommandOptions.FileType == "Asm":\r
+            TrimAsmFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)\r
         else :\r
             if CommandOptions.OutputFile is None:\r
                 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r