]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Trim/Trim.py
BaseTools: Generate dependent files for ASL and ASM files
[mirror_edk2.git] / BaseTools / Source / Python / Trim / Trim.py
CommitLineData
f51461c8
LG
1## @file\r
2# Trim files preprocessed by compiler\r
3#\r
a146c532 4# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
2e351cbe 5# SPDX-License-Identifier: BSD-2-Clause-Patent\r
f51461c8
LG
6#\r
7\r
8##\r
9# Import Modules\r
10#\r
1be2ed90 11import Common.LongFilePathOs as os\r
f51461c8
LG
12import sys\r
13import re\r
86379ac4 14from io import BytesIO\r
58742d79 15import codecs\r
f51461c8
LG
16from optparse import OptionParser\r
17from optparse import make_option\r
18from Common.BuildToolError import *\r
19from Common.Misc import *\r
91fa33ee 20from Common.DataType import *\r
f51461c8
LG
21from Common.BuildVersion import gBUILD_VERSION\r
22import Common.EdkLogger as EdkLogger\r
1be2ed90 23from Common.LongFilePathSupport import OpenLongFilePath as open\r
f51461c8
LG
24\r
25# Version and Copyright\r
26__version_number__ = ("0.10" + " " + gBUILD_VERSION)\r
27__version__ = "%prog Version " + __version_number__\r
f7496d71 28__copyright__ = "Copyright (c) 2007-2018, Intel Corporation. All rights reserved."\r
f51461c8
LG
29\r
30## Regular expression for matching Line Control directive like "#line xxx"\r
31gLineControlDirective = re.compile('^\s*#(?:line)?\s+([0-9]+)\s+"*([^"]*)"')\r
32## Regular expression for matching "typedef struct"\r
33gTypedefPattern = re.compile("^\s*typedef\s+struct(\s+\w+)?\s*[{]*$", re.MULTILINE)\r
34## Regular expression for matching "#pragma pack"\r
35gPragmaPattern = re.compile("^\s*#pragma\s+pack", re.MULTILINE)\r
7cf1e91d
YZ
36## Regular expression for matching "typedef"\r
37gTypedef_SinglePattern = re.compile("^\s*typedef", re.MULTILINE)\r
38## Regular expression for matching "typedef struct, typedef union, struct, union"\r
39gTypedef_MulPattern = re.compile("^\s*(typedef)?\s+(struct|union)(\s+\w+)?\s*[{]*$", re.MULTILINE)\r
f51461c8
LG
40\r
41#\r
42# The following number pattern match will only match if following criteria is met:\r
43# There is leading non-(alphanumeric or _) character, and no following alphanumeric or _\r
44# as the pattern is greedily match, so it is ok for the gDecNumberPattern or gHexNumberPattern to grab the maximum match\r
45#\r
46## Regular expression for matching HEX number\r
47gHexNumberPattern = re.compile("(?<=[^a-zA-Z0-9_])(0[xX])([0-9a-fA-F]+)(U(?=$|[^a-zA-Z0-9_]))?")\r
48## Regular expression for matching decimal number with 'U' postfix\r
49gDecNumberPattern = re.compile("(?<=[^a-zA-Z0-9_])([0-9]+)U(?=$|[^a-zA-Z0-9_])")\r
50## Regular expression for matching constant with 'ULL' 'LL' postfix\r
51gLongNumberPattern = re.compile("(?<=[^a-zA-Z0-9_])(0[xX][0-9a-fA-F]+|[0-9]+)U?LL(?=$|[^a-zA-Z0-9_])")\r
52\r
53## Regular expression for matching "Include ()" in asl file\r
54gAslIncludePattern = re.compile("^(\s*)[iI]nclude\s*\(\"?([^\"\(\)]+)\"\)", re.MULTILINE)\r
55## Regular expression for matching C style #include "XXX.asl" in asl file\r
56gAslCIncludePattern = re.compile(r'^(\s*)#include\s*[<"]\s*([-\\/\w.]+)\s*([>"])', re.MULTILINE)\r
57## Patterns used to convert EDK conventions to EDK2 ECP conventions\r
f51461c8 58\r
e6edbe31
BF
59## Regular expression for finding header file inclusions\r
60gIncludePattern = re.compile(r"^[ \t]*[%]?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE | re.UNICODE | re.IGNORECASE)\r
61\r
62\r
f51461c8
LG
63## file cache to avoid circular include in ASL file\r
64gIncludedAslFile = []\r
65\r
66## Trim preprocessed source code\r
67#\r
68# Remove extra content made by preprocessor. The preprocessor must enable the\r
69# line number generation option when preprocessing.\r
70#\r
71# @param Source File to be trimmed\r
72# @param Target File to store the trimmed content\r
73# @param Convert If True, convert standard HEX format to MASM format\r
74#\r
75def TrimPreprocessedFile(Source, Target, ConvertHex, TrimLong):\r
76 CreateDirectory(os.path.dirname(Target))\r
77 try:\r
58742d79
FZ
78 with open(Source, "r") as File:\r
79 Lines = File.readlines()\r
307e1650 80 except IOError:\r
f51461c8 81 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
83d6207f 82 except:\r
307e1650 83 EdkLogger.error("Trim", AUTOGEN_ERROR, "TrimPreprocessedFile: Error while processing file", File=Source)\r
f51461c8 84\r
f51461c8
LG
85 PreprocessedFile = ""\r
86 InjectedFile = ""\r
87 LineIndexOfOriginalFile = None\r
88 NewLines = []\r
89 LineControlDirectiveFound = False\r
90 for Index in range(len(Lines)):\r
91 Line = Lines[Index]\r
92 #\r
93 # Find out the name of files injected by preprocessor from the lines\r
94 # with Line Control directive\r
95 #\r
96 MatchList = gLineControlDirective.findall(Line)\r
97 if MatchList != []:\r
98 MatchList = MatchList[0]\r
99 if len(MatchList) == 2:\r
100 LineNumber = int(MatchList[0], 0)\r
101 InjectedFile = MatchList[1]\r
5895956d
CC
102 InjectedFile = os.path.normpath(InjectedFile)\r
103 InjectedFile = os.path.normcase(InjectedFile)\r
fb0b35e0 104 # The first injected file must be the preprocessed file itself\r
f51461c8
LG
105 if PreprocessedFile == "":\r
106 PreprocessedFile = InjectedFile\r
107 LineControlDirectiveFound = True\r
108 continue\r
109 elif PreprocessedFile == "" or InjectedFile != PreprocessedFile:\r
110 continue\r
111\r
4231a819 112 if LineIndexOfOriginalFile is None:\r
f51461c8
LG
113 #\r
114 # Any non-empty lines must be from original preprocessed file.\r
115 # And this must be the first one.\r
116 #\r
117 LineIndexOfOriginalFile = Index\r
118 EdkLogger.verbose("Found original file content starting from line %d"\r
119 % (LineIndexOfOriginalFile + 1))\r
120\r
55668ca2
YL
121 if TrimLong:\r
122 Line = gLongNumberPattern.sub(r"\1", Line)\r
f51461c8
LG
123 # convert HEX number format if indicated\r
124 if ConvertHex:\r
125 Line = gHexNumberPattern.sub(r"0\2h", Line)\r
126 else:\r
127 Line = gHexNumberPattern.sub(r"\1\2", Line)\r
f51461c8
LG
128\r
129 # convert Decimal number format\r
130 Line = gDecNumberPattern.sub(r"\1", Line)\r
131\r
4231a819 132 if LineNumber is not None:\r
f51461c8
LG
133 EdkLogger.verbose("Got line directive: line=%d" % LineNumber)\r
134 # in case preprocessor removed some lines, like blank or comment lines\r
135 if LineNumber <= len(NewLines):\r
136 # possible?\r
137 NewLines[LineNumber - 1] = Line\r
138 else:\r
139 if LineNumber > (len(NewLines) + 1):\r
140 for LineIndex in range(len(NewLines), LineNumber-1):\r
62cb98c2 141 NewLines.append(TAB_LINE_BREAK)\r
f51461c8
LG
142 NewLines.append(Line)\r
143 LineNumber = None\r
144 EdkLogger.verbose("Now we have lines: %d" % len(NewLines))\r
145 else:\r
146 NewLines.append(Line)\r
147\r
148 # in case there's no line directive or linemarker found\r
149 if (not LineControlDirectiveFound) and NewLines == []:\r
7cf1e91d
YZ
150 MulPatternFlag = False\r
151 SinglePatternFlag = False\r
152 Brace = 0\r
153 for Index in range(len(Lines)):\r
154 Line = Lines[Index]\r
4231a819
CJ
155 if MulPatternFlag == False and gTypedef_MulPattern.search(Line) is None:\r
156 if SinglePatternFlag == False and gTypedef_SinglePattern.search(Line) is None:\r
7cf1e91d 157 # remove "#pragram pack" directive\r
4231a819 158 if gPragmaPattern.search(Line) is None:\r
7cf1e91d
YZ
159 NewLines.append(Line)\r
160 continue\r
161 elif SinglePatternFlag == False:\r
162 SinglePatternFlag = True\r
163 if Line.find(";") >= 0:\r
164 SinglePatternFlag = False\r
165 elif MulPatternFlag == False:\r
166 # found "typedef struct, typedef union, union, struct", keep its position and set a flag\r
167 MulPatternFlag = True\r
168\r
169 # match { and } to find the end of typedef definition\r
170 if Line.find("{") >= 0:\r
171 Brace += 1\r
172 elif Line.find("}") >= 0:\r
173 Brace -= 1\r
174\r
175 # "typedef struct, typedef union, union, struct" must end with a ";"\r
176 if Brace == 0 and Line.find(";") >= 0:\r
177 MulPatternFlag = False\r
f51461c8
LG
178\r
179 # save to file\r
180 try:\r
58742d79
FZ
181 with open(Target, 'w') as File:\r
182 File.writelines(NewLines)\r
f51461c8
LG
183 except:\r
184 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
f51461c8
LG
185\r
186## Trim preprocessed VFR file\r
187#\r
188# Remove extra content made by preprocessor. The preprocessor doesn't need to\r
189# enable line number generation option when preprocessing.\r
190#\r
191# @param Source File to be trimmed\r
192# @param Target File to store the trimmed content\r
193#\r
194def TrimPreprocessedVfr(Source, Target):\r
195 CreateDirectory(os.path.dirname(Target))\r
f7496d71 196\r
f51461c8 197 try:\r
58742d79
FZ
198 with open(Source, "r") as File:\r
199 Lines = File.readlines()\r
f51461c8
LG
200 except:\r
201 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
202 # read whole file\r
f51461c8
LG
203\r
204 FoundTypedef = False\r
205 Brace = 0\r
206 TypedefStart = 0\r
207 TypedefEnd = 0\r
208 for Index in range(len(Lines)):\r
209 Line = Lines[Index]\r
210 # don't trim the lines from "formset" definition to the end of file\r
211 if Line.strip() == 'formset':\r
212 break\r
213\r
214 if FoundTypedef == False and (Line.find('#line') == 0 or Line.find('# ') == 0):\r
215 # empty the line number directive if it's not aomong "typedef struct"\r
216 Lines[Index] = "\n"\r
217 continue\r
218\r
4231a819 219 if FoundTypedef == False and gTypedefPattern.search(Line) is None:\r
f51461c8 220 # keep "#pragram pack" directive\r
4231a819 221 if gPragmaPattern.search(Line) is None:\r
f51461c8
LG
222 Lines[Index] = "\n"\r
223 continue\r
224 elif FoundTypedef == False:\r
225 # found "typedef struct", keept its position and set a flag\r
226 FoundTypedef = True\r
227 TypedefStart = Index\r
228\r
229 # match { and } to find the end of typedef definition\r
230 if Line.find("{") >= 0:\r
231 Brace += 1\r
232 elif Line.find("}") >= 0:\r
233 Brace -= 1\r
234\r
235 # "typedef struct" must end with a ";"\r
236 if Brace == 0 and Line.find(";") >= 0:\r
237 FoundTypedef = False\r
238 TypedefEnd = Index\r
239 # keep all "typedef struct" except to GUID, EFI_PLABEL and PAL_CALL_RETURN\r
91fa33ee 240 if Line.strip("} ;\r\n") in [TAB_GUID, "EFI_PLABEL", "PAL_CALL_RETURN"]:\r
f51461c8
LG
241 for i in range(TypedefStart, TypedefEnd+1):\r
242 Lines[i] = "\n"\r
243\r
244 # save all lines trimmed\r
245 try:\r
58742d79
FZ
246 with open(Target, 'w') as File:\r
247 File.writelines(Lines)\r
f51461c8
LG
248 except:\r
249 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
f51461c8
LG
250\r
251## Read the content ASL file, including ASL included, recursively\r
252#\r
253# @param Source File to be read\r
254# @param Indent Spaces before the Include() statement\r
255# @param IncludePathList The list of external include file\r
256# @param LocalSearchPath If LocalSearchPath is specified, this path will be searched\r
257# first for the included file; otherwise, only the path specified\r
258# in the IncludePathList will be searched.\r
259#\r
e6edbe31 260def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None, IncludeFileList = None, filetype=None):\r
f51461c8 261 NewFileContent = []\r
e6edbe31
BF
262 if IncludeFileList is None:\r
263 IncludeFileList = []\r
f51461c8
LG
264 try:\r
265 #\r
266 # Search LocalSearchPath first if it is specified.\r
267 #\r
268 if LocalSearchPath:\r
269 SearchPathList = [LocalSearchPath] + IncludePathList\r
270 else:\r
271 SearchPathList = IncludePathList\r
f7496d71 272\r
f51461c8
LG
273 for IncludePath in SearchPathList:\r
274 IncludeFile = os.path.join(IncludePath, Source)\r
275 if os.path.isfile(IncludeFile):\r
58742d79
FZ
276 try:\r
277 with open(IncludeFile, "r") as File:\r
278 F = File.readlines()\r
279 except:\r
280 with codecs.open(IncludeFile, "r", encoding='utf-8') as File:\r
281 F = File.readlines()\r
f51461c8
LG
282 break\r
283 else:\r
284 EdkLogger.error("Trim", "Failed to find include file %s" % Source)\r
285 except:\r
286 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
287\r
f7496d71 288\r
f51461c8
LG
289 # avoid A "include" B and B "include" A\r
290 IncludeFile = os.path.abspath(os.path.normpath(IncludeFile))\r
291 if IncludeFile in gIncludedAslFile:\r
292 EdkLogger.warn("Trim", "Circular include",\r
293 ExtraData= "%s -> %s" % (" -> ".join(gIncludedAslFile), IncludeFile))\r
294 return []\r
295 gIncludedAslFile.append(IncludeFile)\r
e6edbe31 296 IncludeFileList.append(IncludeFile.strip())\r
f51461c8
LG
297 for Line in F:\r
298 LocalSearchPath = None\r
e6edbe31
BF
299 if filetype == "ASL":\r
300 Result = gAslIncludePattern.findall(Line)\r
301 if len(Result) == 0:\r
302 Result = gAslCIncludePattern.findall(Line)\r
303 if len(Result) == 0 or os.path.splitext(Result[0][1])[1].lower() not in [".asl", ".asi"]:\r
304 NewFileContent.append("%s%s" % (Indent, Line))\r
305 continue\r
306 #\r
307 # We should first search the local directory if current file are using pattern #include "XXX"\r
308 #\r
309 if Result[0][2] == '"':\r
310 LocalSearchPath = os.path.dirname(IncludeFile)\r
311 CurrentIndent = Indent + Result[0][0]\r
312 IncludedFile = Result[0][1]\r
313 NewFileContent.extend(DoInclude(IncludedFile, CurrentIndent, IncludePathList, LocalSearchPath,IncludeFileList,filetype))\r
314 NewFileContent.append("\n")\r
315 elif filetype == "ASM":\r
316 Result = gIncludePattern.findall(Line)\r
317 if len(Result) == 0:\r
f51461c8
LG
318 NewFileContent.append("%s%s" % (Indent, Line))\r
319 continue\r
e6edbe31
BF
320\r
321 IncludedFile = Result[0]\r
322\r
323 IncludedFile = IncludedFile.strip()\r
324 IncludedFile = os.path.normpath(IncludedFile)\r
325 NewFileContent.extend(DoInclude(IncludedFile, '', IncludePathList, LocalSearchPath,IncludeFileList,filetype))\r
326 NewFileContent.append("\n")\r
f51461c8
LG
327\r
328 gIncludedAslFile.pop()\r
f51461c8
LG
329\r
330 return NewFileContent\r
331\r
332\r
333## Trim ASL file\r
334#\r
335# Replace ASL include statement with the content the included file\r
336#\r
337# @param Source File to be trimmed\r
338# @param Target File to store the trimmed content\r
f7496d71 339# @param IncludePathFile The file to log the external include path\r
f51461c8 340#\r
e6edbe31 341def TrimAslFile(Source, Target, IncludePathFile,AslDeps = False):\r
f51461c8 342 CreateDirectory(os.path.dirname(Target))\r
f7496d71 343\r
f51461c8
LG
344 SourceDir = os.path.dirname(Source)\r
345 if SourceDir == '':\r
346 SourceDir = '.'\r
f7496d71 347\r
f51461c8
LG
348 #\r
349 # Add source directory as the first search directory\r
350 #\r
351 IncludePathList = [SourceDir]\r
f7496d71 352\r
f51461c8
LG
353 #\r
354 # If additional include path file is specified, append them all\r
355 # to the search directory list.\r
356 #\r
357 if IncludePathFile:\r
358 try:\r
359 LineNum = 0\r
58742d79
FZ
360 with open(IncludePathFile, 'r') as File:\r
361 FileLines = File.readlines()\r
362 for Line in FileLines:\r
f51461c8
LG
363 LineNum += 1\r
364 if Line.startswith("/I") or Line.startswith ("-I"):\r
365 IncludePathList.append(Line[2:].strip())\r
366 else:\r
367 EdkLogger.warn("Trim", "Invalid include line in include list file.", IncludePathFile, LineNum)\r
368 except:\r
369 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=IncludePathFile)\r
e6edbe31
BF
370 AslIncludes = []\r
371 Lines = DoInclude(Source, '', IncludePathList,IncludeFileList=AslIncludes,filetype='ASL')\r
372 AslIncludes = [item for item in AslIncludes if item !=Source]\r
373 if AslDeps and AslIncludes:\r
374 SaveFileOnChange(os.path.join(os.path.dirname(Target),os.path.basename(Source))+".trim.deps", " \\\n".join([Source+":"] +AslIncludes),False)\r
f51461c8
LG
375\r
376 #\r
377 # Undef MIN and MAX to avoid collision in ASL source code\r
378 #\r
379 Lines.insert(0, "#undef MIN\n#undef MAX\n")\r
380\r
381 # save all lines trimmed\r
382 try:\r
58742d79
FZ
383 with open(Target, 'w') as File:\r
384 File.writelines(Lines)\r
f51461c8
LG
385 except:\r
386 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
387\r
e6edbe31
BF
388## Trim ASM file\r
389#\r
390# Output ASM include statement with the content the included file\r
391#\r
392# @param Source File to be trimmed\r
393# @param Target File to store the trimmed content\r
394# @param IncludePathFile The file to log the external include path\r
395#\r
396def TrimAsmFile(Source, Target, IncludePathFile):\r
397 CreateDirectory(os.path.dirname(Target))\r
398\r
399 SourceDir = os.path.dirname(Source)\r
400 if SourceDir == '':\r
401 SourceDir = '.'\r
402\r
403 #\r
404 # Add source directory as the first search directory\r
405 #\r
406 IncludePathList = [SourceDir]\r
407 #\r
408 # If additional include path file is specified, append them all\r
409 # to the search directory list.\r
410 #\r
411 if IncludePathFile:\r
412 try:\r
413 LineNum = 0\r
414 with open(IncludePathFile, 'r') as File:\r
415 FileLines = File.readlines()\r
416 for Line in FileLines:\r
417 LineNum += 1\r
418 if Line.startswith("/I") or Line.startswith ("-I"):\r
419 IncludePathList.append(Line[2:].strip())\r
420 else:\r
421 EdkLogger.warn("Trim", "Invalid include line in include list file.", IncludePathFile, LineNum)\r
422 except:\r
423 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=IncludePathFile)\r
424 AsmIncludes = []\r
425 Lines = DoInclude(Source, '', IncludePathList,IncludeFileList=AsmIncludes,filetype='ASM')\r
426 AsmIncludes = [item for item in AsmIncludes if item != Source]\r
427 if AsmIncludes:\r
428 SaveFileOnChange(os.path.join(os.path.dirname(Target),os.path.basename(Source))+".trim.deps", " \\\n".join([Source+":"] +AsmIncludes),False)\r
429 # save all lines trimmed\r
430 try:\r
431 with open(Target, 'w') as File:\r
432 File.writelines(Lines)\r
433 except:\r
434 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
435\r
111dab62
YZ
436def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):\r
437 VfrNameList = []\r
438 if os.path.isdir(DebugDir):\r
439 for CurrentDir, Dirs, Files in os.walk(DebugDir):\r
440 for FileName in Files:\r
441 Name, Ext = os.path.splitext(FileName)\r
a146c532 442 if Ext == '.c' and Name != 'AutoGen':\r
111dab62
YZ
443 VfrNameList.append (Name + 'Bin')\r
444\r
445 VfrNameList.append (ModuleName + 'Strings')\r
446\r
447 EfiFileName = os.path.join(DebugDir, ModuleName + '.efi')\r
448 MapFileName = os.path.join(DebugDir, ModuleName + '.map')\r
449 VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, VfrNameList)\r
450\r
451 if not VfrUniOffsetList:\r
452 return\r
453\r
454 try:\r
58742d79 455 fInputfile = open(OutputFile, "wb+")\r
111dab62
YZ
456 except:\r
457 EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)\r
458\r
86379ac4 459 # Use a instance of BytesIO to cache data\r
d943b0c3 460 fStringIO = BytesIO()\r
111dab62
YZ
461\r
462 for Item in VfrUniOffsetList:\r
463 if (Item[0].find("Strings") != -1):\r
464 #\r
465 # UNI offset in image.\r
466 # GUID + Offset\r
467 # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
468 #\r
d943b0c3
FB
469 UniGuid = b'\xe0\xc5\x13\x89\xf63\x86M\x9b\xf1C\xef\x89\xfc\x06f'\r
470 fStringIO.write(UniGuid)\r
111dab62
YZ
471 UniValue = pack ('Q', int (Item[1], 16))\r
472 fStringIO.write (UniValue)\r
473 else:\r
474 #\r
475 # VFR binary offset in image.\r
476 # GUID + Offset\r
477 # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
478 #\r
d943b0c3
FB
479 VfrGuid = b'\xb4|\xbc\xd0Gj_I\xaa\x11q\x07F\xda\x06\xa2'\r
480 fStringIO.write(VfrGuid)\r
1ccc4d89 481 type (Item[1])\r
111dab62
YZ
482 VfrValue = pack ('Q', int (Item[1], 16))\r
483 fStringIO.write (VfrValue)\r
484\r
485 #\r
486 # write data into file.\r
487 #\r
488 try :\r
489 fInputfile.write (fStringIO.getvalue())\r
490 except:\r
491 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
492\r
493 fStringIO.close ()\r
494 fInputfile.close ()\r
495\r
f51461c8
LG
496\r
497## Parse command line options\r
498#\r
499# Using standard Python module optparse to parse command line option of this tool.\r
500#\r
501# @retval Options A optparse.Values object containing the parsed options\r
502# @retval InputFile Path of file to be trimmed\r
503#\r
504def Options():\r
505 OptionList = [\r
506 make_option("-s", "--source-code", dest="FileType", const="SourceCode", action="store_const",\r
507 help="The input file is preprocessed source code, including C or assembly code"),\r
508 make_option("-r", "--vfr-file", dest="FileType", const="Vfr", action="store_const",\r
509 help="The input file is preprocessed VFR file"),\r
111dab62
YZ
510 make_option("--Vfr-Uni-Offset", dest="FileType", const="VfrOffsetBin", action="store_const",\r
511 help="The input file is EFI image"),\r
e6edbe31
BF
512 make_option("--asl-deps", dest="AslDeps", const="True", action="store_const",\r
513 help="Generate Asl dependent files."),\r
f51461c8
LG
514 make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",\r
515 help="The input file is ASL file"),\r
e6edbe31
BF
516 make_option( "--asm-file", dest="FileType", const="Asm", action="store_const",\r
517 help="The input file is asm file"),\r
f51461c8
LG
518 make_option("-c", "--convert-hex", dest="ConvertHex", action="store_true",\r
519 help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),\r
520\r
521 make_option("-l", "--trim-long", dest="TrimLong", action="store_true",\r
522 help="Remove postfix of long number"),\r
523 make_option("-i", "--include-path-file", dest="IncludePathFile",\r
524 help="The input file is include path list to search for ASL include file"),\r
525 make_option("-o", "--output", dest="OutputFile",\r
526 help="File to store the trimmed content"),\r
111dab62
YZ
527 make_option("--ModuleName", dest="ModuleName", help="The module's BASE_NAME"),\r
528 make_option("--DebugDir", dest="DebugDir",\r
529 help="Debug Output directory to store the output files"),\r
f51461c8
LG
530 make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,\r
531 help="Run verbosely"),\r
532 make_option("-d", "--debug", dest="LogLevel", type="int",\r
533 help="Run with debug information"),\r
534 make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,\r
535 help="Run quietly"),\r
536 make_option("-?", action="help", help="show this help message and exit"),\r
537 ]\r
538\r
539 # use clearer usage to override default usage message\r
111dab62 540 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
f51461c8
LG
541\r
542 Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)\r
543 Parser.set_defaults(FileType="Vfr")\r
544 Parser.set_defaults(ConvertHex=False)\r
545 Parser.set_defaults(LogLevel=EdkLogger.INFO)\r
546\r
547 Options, Args = Parser.parse_args()\r
548\r
549 # error check\r
111dab62
YZ
550 if Options.FileType == 'VfrOffsetBin':\r
551 if len(Args) == 0:\r
552 return Options, ''\r
553 elif len(Args) > 1:\r
554 EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
f51461c8
LG
555 if len(Args) == 0:\r
556 EdkLogger.error("Trim", OPTION_MISSING, ExtraData=Parser.get_usage())\r
557 if len(Args) > 1:\r
558 EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
559\r
560 InputFile = Args[0]\r
561 return Options, InputFile\r
562\r
563## Entrance method\r
564#\r
565# This method mainly dispatch specific methods per the command line options.\r
566# If no error found, return zero value so the caller of this tool can know\r
567# if it's executed successfully or not.\r
568#\r
569# @retval 0 Tool was successful\r
570# @retval 1 Tool failed\r
571#\r
572def Main():\r
573 try:\r
574 EdkLogger.Initialize()\r
575 CommandOptions, InputFile = Options()\r
576 if CommandOptions.LogLevel < EdkLogger.DEBUG_9:\r
577 EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
578 else:\r
579 EdkLogger.SetLevel(CommandOptions.LogLevel)\r
5b0671c1 580 except FatalError as X:\r
f51461c8 581 return 1\r
f7496d71 582\r
f51461c8
LG
583 try:\r
584 if CommandOptions.FileType == "Vfr":\r
4231a819 585 if CommandOptions.OutputFile is None:\r
f51461c8
LG
586 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
587 TrimPreprocessedVfr(InputFile, CommandOptions.OutputFile)\r
588 elif CommandOptions.FileType == "Asl":\r
4231a819 589 if CommandOptions.OutputFile is None:\r
f51461c8 590 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
e6edbe31 591 TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile,CommandOptions.AslDeps)\r
111dab62
YZ
592 elif CommandOptions.FileType == "VfrOffsetBin":\r
593 GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile)\r
e6edbe31
BF
594 elif CommandOptions.FileType == "Asm":\r
595 TrimAsmFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)\r
f51461c8 596 else :\r
4231a819 597 if CommandOptions.OutputFile is None:\r
f51461c8
LG
598 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
599 TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong)\r
5b0671c1 600 except FatalError as X:\r
f51461c8
LG
601 import platform\r
602 import traceback\r
4231a819 603 if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:\r
f51461c8
LG
604 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
605 return 1\r
606 except:\r
607 import traceback\r
608 import platform\r
609 EdkLogger.error(\r
610 "\nTrim",\r
611 CODE_ERROR,\r
612 "Unknown fatal error when trimming [%s]" % InputFile,\r
c1387446 613 ExtraData="\n(Please send email to %s for help, attaching following call stack trace!)\n" % MSG_EDKII_MAIL_ADDR,\r
f51461c8
LG
614 RaiseError=False\r
615 )\r
616 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
617 return 1\r
618\r
619 return 0\r
620\r
621if __name__ == '__main__':\r
622 r = Main()\r
623 ## 0-127 is a safe return range, and 1 is a standard default error\r
624 if r < 0 or r > 127: r = 1\r
625 sys.exit(r)\r
626\r