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
try:\r
with open(Source, "r") as File:\r
Lines = File.readlines()\r
- except:\r
+ except IOError:\r
EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
+ except:\r
+ EdkLogger.error("Trim", AUTOGEN_ERROR, "TrimPreprocessedFile: Error while processing file", File=Source)\r
\r
PreprocessedFile = ""\r
InjectedFile = ""\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
+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
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
# @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
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
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
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
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
"\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
+ ExtraData="\n(Please send email to %s for help, attaching following call stack trace!)\n" % MSG_EDKII_MAIL_ADDR,\r
RaiseError=False\r
)\r
EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r