]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Trim/Trim.py
BaseTools: Replace StringIO.StringIO with io.BytesIO
[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
111dab62 34__copyright__ = "Copyright (c) 2007-2017, 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
169 # The first injetcted file must be the preprocessed file itself\r
170 if PreprocessedFile == "":\r
171 PreprocessedFile = InjectedFile\r
172 LineControlDirectiveFound = True\r
173 continue\r
174 elif PreprocessedFile == "" or InjectedFile != PreprocessedFile:\r
175 continue\r
176\r
4231a819 177 if LineIndexOfOriginalFile is None:\r
f51461c8
LG
178 #\r
179 # Any non-empty lines must be from original preprocessed file.\r
180 # And this must be the first one.\r
181 #\r
182 LineIndexOfOriginalFile = Index\r
183 EdkLogger.verbose("Found original file content starting from line %d"\r
184 % (LineIndexOfOriginalFile + 1))\r
185\r
55668ca2
YL
186 if TrimLong:\r
187 Line = gLongNumberPattern.sub(r"\1", Line)\r
f51461c8
LG
188 # convert HEX number format if indicated\r
189 if ConvertHex:\r
190 Line = gHexNumberPattern.sub(r"0\2h", Line)\r
191 else:\r
192 Line = gHexNumberPattern.sub(r"\1\2", Line)\r
f51461c8
LG
193\r
194 # convert Decimal number format\r
195 Line = gDecNumberPattern.sub(r"\1", Line)\r
196\r
4231a819 197 if LineNumber is not None:\r
f51461c8
LG
198 EdkLogger.verbose("Got line directive: line=%d" % LineNumber)\r
199 # in case preprocessor removed some lines, like blank or comment lines\r
200 if LineNumber <= len(NewLines):\r
201 # possible?\r
202 NewLines[LineNumber - 1] = Line\r
203 else:\r
204 if LineNumber > (len(NewLines) + 1):\r
205 for LineIndex in range(len(NewLines), LineNumber-1):\r
206 NewLines.append(os.linesep)\r
207 NewLines.append(Line)\r
208 LineNumber = None\r
209 EdkLogger.verbose("Now we have lines: %d" % len(NewLines))\r
210 else:\r
211 NewLines.append(Line)\r
212\r
213 # in case there's no line directive or linemarker found\r
214 if (not LineControlDirectiveFound) and NewLines == []:\r
7cf1e91d
YZ
215 MulPatternFlag = False\r
216 SinglePatternFlag = False\r
217 Brace = 0\r
218 for Index in range(len(Lines)):\r
219 Line = Lines[Index]\r
4231a819
CJ
220 if MulPatternFlag == False and gTypedef_MulPattern.search(Line) is None:\r
221 if SinglePatternFlag == False and gTypedef_SinglePattern.search(Line) is None:\r
7cf1e91d 222 # remove "#pragram pack" directive\r
4231a819 223 if gPragmaPattern.search(Line) is None:\r
7cf1e91d
YZ
224 NewLines.append(Line)\r
225 continue\r
226 elif SinglePatternFlag == False:\r
227 SinglePatternFlag = True\r
228 if Line.find(";") >= 0:\r
229 SinglePatternFlag = False\r
230 elif MulPatternFlag == False:\r
231 # found "typedef struct, typedef union, union, struct", keep its position and set a flag\r
232 MulPatternFlag = True\r
233\r
234 # match { and } to find the end of typedef definition\r
235 if Line.find("{") >= 0:\r
236 Brace += 1\r
237 elif Line.find("}") >= 0:\r
238 Brace -= 1\r
239\r
240 # "typedef struct, typedef union, union, struct" must end with a ";"\r
241 if Brace == 0 and Line.find(";") >= 0:\r
242 MulPatternFlag = False\r
f51461c8
LG
243\r
244 # save to file\r
245 try:\r
246 f = open (Target, 'wb')\r
247 except:\r
248 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
249 f.writelines(NewLines)\r
250 f.close()\r
251\r
252## Trim preprocessed VFR file\r
253#\r
254# Remove extra content made by preprocessor. The preprocessor doesn't need to\r
255# enable line number generation option when preprocessing.\r
256#\r
257# @param Source File to be trimmed\r
258# @param Target File to store the trimmed content\r
259#\r
260def TrimPreprocessedVfr(Source, Target):\r
261 CreateDirectory(os.path.dirname(Target))\r
262 \r
263 try:\r
ccaa7754 264 f = open (Source, 'r')\r
f51461c8
LG
265 except:\r
266 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
267 # read whole file\r
268 Lines = f.readlines()\r
269 f.close()\r
270\r
271 FoundTypedef = False\r
272 Brace = 0\r
273 TypedefStart = 0\r
274 TypedefEnd = 0\r
275 for Index in range(len(Lines)):\r
276 Line = Lines[Index]\r
277 # don't trim the lines from "formset" definition to the end of file\r
278 if Line.strip() == 'formset':\r
279 break\r
280\r
281 if FoundTypedef == False and (Line.find('#line') == 0 or Line.find('# ') == 0):\r
282 # empty the line number directive if it's not aomong "typedef struct"\r
283 Lines[Index] = "\n"\r
284 continue\r
285\r
4231a819 286 if FoundTypedef == False and gTypedefPattern.search(Line) is None:\r
f51461c8 287 # keep "#pragram pack" directive\r
4231a819 288 if gPragmaPattern.search(Line) is None:\r
f51461c8
LG
289 Lines[Index] = "\n"\r
290 continue\r
291 elif FoundTypedef == False:\r
292 # found "typedef struct", keept its position and set a flag\r
293 FoundTypedef = True\r
294 TypedefStart = Index\r
295\r
296 # match { and } to find the end of typedef definition\r
297 if Line.find("{") >= 0:\r
298 Brace += 1\r
299 elif Line.find("}") >= 0:\r
300 Brace -= 1\r
301\r
302 # "typedef struct" must end with a ";"\r
303 if Brace == 0 and Line.find(";") >= 0:\r
304 FoundTypedef = False\r
305 TypedefEnd = Index\r
306 # keep all "typedef struct" except to GUID, EFI_PLABEL and PAL_CALL_RETURN\r
91fa33ee 307 if Line.strip("} ;\r\n") in [TAB_GUID, "EFI_PLABEL", "PAL_CALL_RETURN"]:\r
f51461c8
LG
308 for i in range(TypedefStart, TypedefEnd+1):\r
309 Lines[i] = "\n"\r
310\r
311 # save all lines trimmed\r
312 try:\r
ccaa7754 313 f = open (Target, 'w')\r
f51461c8
LG
314 except:\r
315 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
316 f.writelines(Lines)\r
317 f.close()\r
318\r
319## Read the content ASL file, including ASL included, recursively\r
320#\r
321# @param Source File to be read\r
322# @param Indent Spaces before the Include() statement\r
323# @param IncludePathList The list of external include file\r
324# @param LocalSearchPath If LocalSearchPath is specified, this path will be searched\r
325# first for the included file; otherwise, only the path specified\r
326# in the IncludePathList will be searched.\r
327#\r
328def DoInclude(Source, Indent='', IncludePathList=[], LocalSearchPath=None):\r
329 NewFileContent = []\r
330\r
331 try:\r
332 #\r
333 # Search LocalSearchPath first if it is specified.\r
334 #\r
335 if LocalSearchPath:\r
336 SearchPathList = [LocalSearchPath] + IncludePathList\r
337 else:\r
338 SearchPathList = IncludePathList\r
339 \r
340 for IncludePath in SearchPathList:\r
341 IncludeFile = os.path.join(IncludePath, Source)\r
342 if os.path.isfile(IncludeFile):\r
343 F = open(IncludeFile, "r")\r
344 break\r
345 else:\r
346 EdkLogger.error("Trim", "Failed to find include file %s" % Source)\r
347 except:\r
348 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
349\r
350 \r
351 # avoid A "include" B and B "include" A\r
352 IncludeFile = os.path.abspath(os.path.normpath(IncludeFile))\r
353 if IncludeFile in gIncludedAslFile:\r
354 EdkLogger.warn("Trim", "Circular include",\r
355 ExtraData= "%s -> %s" % (" -> ".join(gIncludedAslFile), IncludeFile))\r
356 return []\r
357 gIncludedAslFile.append(IncludeFile)\r
358 \r
359 for Line in F:\r
360 LocalSearchPath = None\r
361 Result = gAslIncludePattern.findall(Line)\r
362 if len(Result) == 0:\r
363 Result = gAslCIncludePattern.findall(Line)\r
364 if len(Result) == 0 or os.path.splitext(Result[0][1])[1].lower() not in [".asl", ".asi"]:\r
365 NewFileContent.append("%s%s" % (Indent, Line))\r
366 continue\r
367 #\r
368 # We should first search the local directory if current file are using pattern #include "XXX" \r
369 #\r
370 if Result[0][2] == '"':\r
371 LocalSearchPath = os.path.dirname(IncludeFile)\r
372 CurrentIndent = Indent + Result[0][0]\r
373 IncludedFile = Result[0][1]\r
374 NewFileContent.extend(DoInclude(IncludedFile, CurrentIndent, IncludePathList, LocalSearchPath))\r
375 NewFileContent.append("\n")\r
376\r
377 gIncludedAslFile.pop()\r
378 F.close()\r
379\r
380 return NewFileContent\r
381\r
382\r
383## Trim ASL file\r
384#\r
385# Replace ASL include statement with the content the included file\r
386#\r
387# @param Source File to be trimmed\r
388# @param Target File to store the trimmed content\r
389# @param IncludePathFile The file to log the external include path \r
390#\r
391def TrimAslFile(Source, Target, IncludePathFile):\r
392 CreateDirectory(os.path.dirname(Target))\r
393 \r
394 SourceDir = os.path.dirname(Source)\r
395 if SourceDir == '':\r
396 SourceDir = '.'\r
397 \r
398 #\r
399 # Add source directory as the first search directory\r
400 #\r
401 IncludePathList = [SourceDir]\r
402 \r
403 #\r
404 # If additional include path file is specified, append them all\r
405 # to the search directory list.\r
406 #\r
407 if IncludePathFile:\r
408 try:\r
409 LineNum = 0\r
ccaa7754 410 for Line in open(IncludePathFile, 'r'):\r
f51461c8
LG
411 LineNum += 1\r
412 if Line.startswith("/I") or Line.startswith ("-I"):\r
413 IncludePathList.append(Line[2:].strip())\r
414 else:\r
415 EdkLogger.warn("Trim", "Invalid include line in include list file.", IncludePathFile, LineNum)\r
416 except:\r
417 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=IncludePathFile)\r
418\r
419 Lines = DoInclude(Source, '', IncludePathList)\r
420\r
421 #\r
422 # Undef MIN and MAX to avoid collision in ASL source code\r
423 #\r
424 Lines.insert(0, "#undef MIN\n#undef MAX\n")\r
425\r
426 # save all lines trimmed\r
427 try:\r
ccaa7754 428 f = open (Target, 'w')\r
f51461c8
LG
429 except:\r
430 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
431\r
432 f.writelines(Lines)\r
433 f.close()\r
434\r
111dab62
YZ
435def GenerateVfrBinSec(ModuleName, DebugDir, OutputFile):\r
436 VfrNameList = []\r
437 if os.path.isdir(DebugDir):\r
438 for CurrentDir, Dirs, Files in os.walk(DebugDir):\r
439 for FileName in Files:\r
440 Name, Ext = os.path.splitext(FileName)\r
a146c532 441 if Ext == '.c' and Name != 'AutoGen':\r
111dab62
YZ
442 VfrNameList.append (Name + 'Bin')\r
443\r
444 VfrNameList.append (ModuleName + 'Strings')\r
445\r
446 EfiFileName = os.path.join(DebugDir, ModuleName + '.efi')\r
447 MapFileName = os.path.join(DebugDir, ModuleName + '.map')\r
448 VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, VfrNameList)\r
449\r
450 if not VfrUniOffsetList:\r
451 return\r
452\r
453 try:\r
454 fInputfile = open(OutputFile, "wb+", 0)\r
455 except:\r
456 EdkLogger.error("Trim", FILE_OPEN_FAILURE, "File open failed for %s" %OutputFile, None)\r
457\r
86379ac4
GL
458 # Use a instance of BytesIO to cache data\r
459 fStringIO = BytesIO('')\r
111dab62
YZ
460\r
461 for Item in VfrUniOffsetList:\r
462 if (Item[0].find("Strings") != -1):\r
463 #\r
464 # UNI offset in image.\r
465 # GUID + Offset\r
466 # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
467 #\r
468 UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]\r
469 UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]\r
470 fStringIO.write(''.join(UniGuid))\r
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
479 VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
480 VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]\r
481 fStringIO.write(''.join(VfrGuid))\r
482 type (Item[1])\r
483 VfrValue = pack ('Q', int (Item[1], 16))\r
484 fStringIO.write (VfrValue)\r
485\r
486 #\r
487 # write data into file.\r
488 #\r
489 try :\r
490 fInputfile.write (fStringIO.getvalue())\r
491 except:\r
492 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
493\r
494 fStringIO.close ()\r
495 fInputfile.close ()\r
496\r
f51461c8
LG
497## Trim EDK source code file(s)\r
498#\r
499#\r
500# @param Source File or directory to be trimmed\r
501# @param Target File or directory to store the trimmed content\r
502#\r
503def TrimEdkSources(Source, Target):\r
504 if os.path.isdir(Source):\r
505 for CurrentDir, Dirs, Files in os.walk(Source):\r
506 if '.svn' in Dirs:\r
507 Dirs.remove('.svn')\r
508 elif "CVS" in Dirs:\r
509 Dirs.remove("CVS")\r
510\r
511 for FileName in Files:\r
512 Dummy, Ext = os.path.splitext(FileName)\r
513 if Ext.upper() not in ['.C', '.H']: continue\r
4231a819 514 if Target is None or Target == '':\r
f51461c8
LG
515 TrimEdkSourceCode(\r
516 os.path.join(CurrentDir, FileName),\r
517 os.path.join(CurrentDir, FileName)\r
518 )\r
519 else:\r
520 TrimEdkSourceCode(\r
521 os.path.join(CurrentDir, FileName),\r
522 os.path.join(Target, CurrentDir[len(Source)+1:], FileName)\r
523 )\r
524 else:\r
525 TrimEdkSourceCode(Source, Target)\r
526\r
527## Trim one EDK source code file\r
528#\r
529# Do following replacement:\r
530#\r
531# (**PeiServices\).PciCfg = <*>;\r
532# => {\r
533# STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {\r
534# (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
535# &gEcpPeiPciCfgPpiGuid,\r
536# <*>\r
537# };\r
538# (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);\r
539#\r
540# <*>Modify(<*>)\r
541# => PeiLibPciCfgModify (<*>)\r
542#\r
543# gRT->ReportStatusCode (<*>)\r
544# => EfiLibReportStatusCode (<*>)\r
545#\r
546# #include <LoadFile\.h>\r
547# => #include <FvLoadFile.h>\r
548#\r
549# CreateEvent (EFI_EVENT_SIGNAL_READY_TO_BOOT, <*>)\r
550# => EfiCreateEventReadyToBoot (<*>)\r
551#\r
552# CreateEvent (EFI_EVENT_SIGNAL_LEGACY_BOOT, <*>)\r
553# => EfiCreateEventLegacyBoot (<*>)\r
554#\r
555# @param Source File to be trimmed\r
556# @param Target File to store the trimmed content\r
557#\r
558def TrimEdkSourceCode(Source, Target):\r
559 EdkLogger.verbose("\t%s -> %s" % (Source, Target))\r
560 CreateDirectory(os.path.dirname(Target))\r
561\r
562 try:\r
ccaa7754 563 f = open (Source, 'rb')\r
f51461c8
LG
564 except:\r
565 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
566 # read whole file\r
567 Lines = f.read()\r
568 f.close()\r
569\r
570 NewLines = None\r
ccaa7754 571 for Re, Repl in gImportCodePatterns:\r
4231a819 572 if NewLines is None:\r
f51461c8
LG
573 NewLines = Re.sub(Repl, Lines)\r
574 else:\r
575 NewLines = Re.sub(Repl, NewLines)\r
576\r
577 # save all lines if trimmed\r
578 if Source == Target and NewLines == Lines:\r
579 return\r
580\r
581 try:\r
ccaa7754 582 f = open (Target, 'wb')\r
f51461c8
LG
583 except:\r
584 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
585 f.write(NewLines)\r
586 f.close()\r
587\r
588\r
589## Parse command line options\r
590#\r
591# Using standard Python module optparse to parse command line option of this tool.\r
592#\r
593# @retval Options A optparse.Values object containing the parsed options\r
594# @retval InputFile Path of file to be trimmed\r
595#\r
596def Options():\r
597 OptionList = [\r
598 make_option("-s", "--source-code", dest="FileType", const="SourceCode", action="store_const",\r
599 help="The input file is preprocessed source code, including C or assembly code"),\r
600 make_option("-r", "--vfr-file", dest="FileType", const="Vfr", action="store_const",\r
601 help="The input file is preprocessed VFR file"),\r
111dab62
YZ
602 make_option("--Vfr-Uni-Offset", dest="FileType", const="VfrOffsetBin", action="store_const",\r
603 help="The input file is EFI image"),\r
f51461c8
LG
604 make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",\r
605 help="The input file is ASL file"),\r
606 make_option("-8", "--Edk-source-code", dest="FileType", const="EdkSourceCode", action="store_const",\r
607 help="The input file is source code for Edk to be trimmed for ECP"),\r
608\r
609 make_option("-c", "--convert-hex", dest="ConvertHex", action="store_true",\r
610 help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),\r
611\r
612 make_option("-l", "--trim-long", dest="TrimLong", action="store_true",\r
613 help="Remove postfix of long number"),\r
614 make_option("-i", "--include-path-file", dest="IncludePathFile",\r
615 help="The input file is include path list to search for ASL include file"),\r
616 make_option("-o", "--output", dest="OutputFile",\r
617 help="File to store the trimmed content"),\r
111dab62
YZ
618 make_option("--ModuleName", dest="ModuleName", help="The module's BASE_NAME"),\r
619 make_option("--DebugDir", dest="DebugDir",\r
620 help="Debug Output directory to store the output files"),\r
f51461c8
LG
621 make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,\r
622 help="Run verbosely"),\r
623 make_option("-d", "--debug", dest="LogLevel", type="int",\r
624 help="Run with debug information"),\r
625 make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,\r
626 help="Run quietly"),\r
627 make_option("-?", action="help", help="show this help message and exit"),\r
628 ]\r
629\r
630 # use clearer usage to override default usage message\r
111dab62 631 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
632\r
633 Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)\r
634 Parser.set_defaults(FileType="Vfr")\r
635 Parser.set_defaults(ConvertHex=False)\r
636 Parser.set_defaults(LogLevel=EdkLogger.INFO)\r
637\r
638 Options, Args = Parser.parse_args()\r
639\r
640 # error check\r
111dab62
YZ
641 if Options.FileType == 'VfrOffsetBin':\r
642 if len(Args) == 0:\r
643 return Options, ''\r
644 elif len(Args) > 1:\r
645 EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
f51461c8
LG
646 if len(Args) == 0:\r
647 EdkLogger.error("Trim", OPTION_MISSING, ExtraData=Parser.get_usage())\r
648 if len(Args) > 1:\r
649 EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
650\r
651 InputFile = Args[0]\r
652 return Options, InputFile\r
653\r
654## Entrance method\r
655#\r
656# This method mainly dispatch specific methods per the command line options.\r
657# If no error found, return zero value so the caller of this tool can know\r
658# if it's executed successfully or not.\r
659#\r
660# @retval 0 Tool was successful\r
661# @retval 1 Tool failed\r
662#\r
663def Main():\r
664 try:\r
665 EdkLogger.Initialize()\r
666 CommandOptions, InputFile = Options()\r
667 if CommandOptions.LogLevel < EdkLogger.DEBUG_9:\r
668 EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
669 else:\r
670 EdkLogger.SetLevel(CommandOptions.LogLevel)\r
5b0671c1 671 except FatalError as X:\r
f51461c8
LG
672 return 1\r
673 \r
674 try:\r
675 if CommandOptions.FileType == "Vfr":\r
4231a819 676 if CommandOptions.OutputFile is None:\r
f51461c8
LG
677 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
678 TrimPreprocessedVfr(InputFile, CommandOptions.OutputFile)\r
679 elif CommandOptions.FileType == "Asl":\r
4231a819 680 if CommandOptions.OutputFile is None:\r
f51461c8
LG
681 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
682 TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)\r
683 elif CommandOptions.FileType == "EdkSourceCode":\r
684 TrimEdkSources(InputFile, CommandOptions.OutputFile)\r
111dab62
YZ
685 elif CommandOptions.FileType == "VfrOffsetBin":\r
686 GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile)\r
f51461c8 687 else :\r
4231a819 688 if CommandOptions.OutputFile is None:\r
f51461c8
LG
689 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
690 TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong)\r
5b0671c1 691 except FatalError as X:\r
f51461c8
LG
692 import platform\r
693 import traceback\r
4231a819 694 if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:\r
f51461c8
LG
695 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
696 return 1\r
697 except:\r
698 import traceback\r
699 import platform\r
700 EdkLogger.error(\r
701 "\nTrim",\r
702 CODE_ERROR,\r
703 "Unknown fatal error when trimming [%s]" % InputFile,\r
3a0f8bde 704 ExtraData="\n(Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!)\n",\r
f51461c8
LG
705 RaiseError=False\r
706 )\r
707 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
708 return 1\r
709\r
710 return 0\r
711\r
712if __name__ == '__main__':\r
713 r = Main()\r
714 ## 0-127 is a safe return range, and 1 is a standard default error\r
715 if r < 0 or r > 127: r = 1\r
716 sys.exit(r)\r
717\r