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