]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Trim/Trim.py
BaseTools/Tests: Update GNUmakefile to use python3 variable
[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
a09f4c91 248 f = open (Target, 'w')\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
86e6cf98 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
86e6cf98 471 fStringIO.write(bytes(UniGuid))\r
111dab62
YZ
472 UniValue = pack ('Q', int (Item[1], 16))\r
473 fStringIO.write (UniValue)\r
474 else:\r
475 #\r
476 # VFR binary offset in image.\r
477 # GUID + Offset\r
478 # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
479 #\r
480 VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
86e6cf98 481 fStringIO.write(bytes(VfrGuid))\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## Trim EDK source code file(s)\r
497#\r
498#\r
499# @param Source File or directory to be trimmed\r
500# @param Target File or directory to store the trimmed content\r
501#\r
502def TrimEdkSources(Source, Target):\r
503 if os.path.isdir(Source):\r
504 for CurrentDir, Dirs, Files in os.walk(Source):\r
505 if '.svn' in Dirs:\r
506 Dirs.remove('.svn')\r
507 elif "CVS" in Dirs:\r
508 Dirs.remove("CVS")\r
509\r
510 for FileName in Files:\r
511 Dummy, Ext = os.path.splitext(FileName)\r
512 if Ext.upper() not in ['.C', '.H']: continue\r
4231a819 513 if Target is None or Target == '':\r
f51461c8
LG
514 TrimEdkSourceCode(\r
515 os.path.join(CurrentDir, FileName),\r
516 os.path.join(CurrentDir, FileName)\r
517 )\r
518 else:\r
519 TrimEdkSourceCode(\r
520 os.path.join(CurrentDir, FileName),\r
521 os.path.join(Target, CurrentDir[len(Source)+1:], FileName)\r
522 )\r
523 else:\r
524 TrimEdkSourceCode(Source, Target)\r
525\r
526## Trim one EDK source code file\r
527#\r
528# Do following replacement:\r
529#\r
530# (**PeiServices\).PciCfg = <*>;\r
531# => {\r
532# STATIC EFI_PEI_PPI_DESCRIPTOR gEcpPeiPciCfgPpiList = {\r
533# (EFI_PEI_PPI_DESCRIPTOR_PPI | EFI_PEI_PPI_DESCRIPTOR_TERMINATE_LIST),\r
534# &gEcpPeiPciCfgPpiGuid,\r
535# <*>\r
536# };\r
537# (**PeiServices).InstallPpi (PeiServices, &gEcpPeiPciCfgPpiList);\r
538#\r
539# <*>Modify(<*>)\r
540# => PeiLibPciCfgModify (<*>)\r
541#\r
542# gRT->ReportStatusCode (<*>)\r
543# => EfiLibReportStatusCode (<*>)\r
544#\r
545# #include <LoadFile\.h>\r
546# => #include <FvLoadFile.h>\r
547#\r
548# CreateEvent (EFI_EVENT_SIGNAL_READY_TO_BOOT, <*>)\r
549# => EfiCreateEventReadyToBoot (<*>)\r
550#\r
551# CreateEvent (EFI_EVENT_SIGNAL_LEGACY_BOOT, <*>)\r
552# => EfiCreateEventLegacyBoot (<*>)\r
553#\r
554# @param Source File to be trimmed\r
555# @param Target File to store the trimmed content\r
556#\r
557def TrimEdkSourceCode(Source, Target):\r
558 EdkLogger.verbose("\t%s -> %s" % (Source, Target))\r
559 CreateDirectory(os.path.dirname(Target))\r
560\r
561 try:\r
a09f4c91 562 f = open (Source, 'r')\r
f51461c8
LG
563 except:\r
564 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Source)\r
565 # read whole file\r
566 Lines = f.read()\r
567 f.close()\r
568\r
569 NewLines = None\r
ccaa7754 570 for Re, Repl in gImportCodePatterns:\r
4231a819 571 if NewLines is None:\r
f51461c8
LG
572 NewLines = Re.sub(Repl, Lines)\r
573 else:\r
574 NewLines = Re.sub(Repl, NewLines)\r
575\r
576 # save all lines if trimmed\r
577 if Source == Target and NewLines == Lines:\r
578 return\r
579\r
580 try:\r
a09f4c91 581 f = open (Target, 'w')\r
f51461c8
LG
582 except:\r
583 EdkLogger.error("Trim", FILE_OPEN_FAILURE, ExtraData=Target)\r
584 f.write(NewLines)\r
585 f.close()\r
586\r
587\r
588## Parse command line options\r
589#\r
590# Using standard Python module optparse to parse command line option of this tool.\r
591#\r
592# @retval Options A optparse.Values object containing the parsed options\r
593# @retval InputFile Path of file to be trimmed\r
594#\r
595def Options():\r
596 OptionList = [\r
597 make_option("-s", "--source-code", dest="FileType", const="SourceCode", action="store_const",\r
598 help="The input file is preprocessed source code, including C or assembly code"),\r
599 make_option("-r", "--vfr-file", dest="FileType", const="Vfr", action="store_const",\r
600 help="The input file is preprocessed VFR file"),\r
111dab62
YZ
601 make_option("--Vfr-Uni-Offset", dest="FileType", const="VfrOffsetBin", action="store_const",\r
602 help="The input file is EFI image"),\r
f51461c8
LG
603 make_option("-a", "--asl-file", dest="FileType", const="Asl", action="store_const",\r
604 help="The input file is ASL file"),\r
605 make_option("-8", "--Edk-source-code", dest="FileType", const="EdkSourceCode", action="store_const",\r
606 help="The input file is source code for Edk to be trimmed for ECP"),\r
607\r
608 make_option("-c", "--convert-hex", dest="ConvertHex", action="store_true",\r
609 help="Convert standard hex format (0xabcd) to MASM format (abcdh)"),\r
610\r
611 make_option("-l", "--trim-long", dest="TrimLong", action="store_true",\r
612 help="Remove postfix of long number"),\r
613 make_option("-i", "--include-path-file", dest="IncludePathFile",\r
614 help="The input file is include path list to search for ASL include file"),\r
615 make_option("-o", "--output", dest="OutputFile",\r
616 help="File to store the trimmed content"),\r
111dab62
YZ
617 make_option("--ModuleName", dest="ModuleName", help="The module's BASE_NAME"),\r
618 make_option("--DebugDir", dest="DebugDir",\r
619 help="Debug Output directory to store the output files"),\r
f51461c8
LG
620 make_option("-v", "--verbose", dest="LogLevel", action="store_const", const=EdkLogger.VERBOSE,\r
621 help="Run verbosely"),\r
622 make_option("-d", "--debug", dest="LogLevel", type="int",\r
623 help="Run with debug information"),\r
624 make_option("-q", "--quiet", dest="LogLevel", action="store_const", const=EdkLogger.QUIET,\r
625 help="Run quietly"),\r
626 make_option("-?", action="help", help="show this help message and exit"),\r
627 ]\r
628\r
629 # use clearer usage to override default usage message\r
111dab62 630 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
631\r
632 Parser = OptionParser(description=__copyright__, version=__version__, option_list=OptionList, usage=UsageString)\r
633 Parser.set_defaults(FileType="Vfr")\r
634 Parser.set_defaults(ConvertHex=False)\r
635 Parser.set_defaults(LogLevel=EdkLogger.INFO)\r
636\r
637 Options, Args = Parser.parse_args()\r
638\r
639 # error check\r
111dab62
YZ
640 if Options.FileType == 'VfrOffsetBin':\r
641 if len(Args) == 0:\r
642 return Options, ''\r
643 elif len(Args) > 1:\r
644 EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
f51461c8
LG
645 if len(Args) == 0:\r
646 EdkLogger.error("Trim", OPTION_MISSING, ExtraData=Parser.get_usage())\r
647 if len(Args) > 1:\r
648 EdkLogger.error("Trim", OPTION_NOT_SUPPORTED, ExtraData=Parser.get_usage())\r
649\r
650 InputFile = Args[0]\r
651 return Options, InputFile\r
652\r
653## Entrance method\r
654#\r
655# This method mainly dispatch specific methods per the command line options.\r
656# If no error found, return zero value so the caller of this tool can know\r
657# if it's executed successfully or not.\r
658#\r
659# @retval 0 Tool was successful\r
660# @retval 1 Tool failed\r
661#\r
662def Main():\r
663 try:\r
664 EdkLogger.Initialize()\r
665 CommandOptions, InputFile = Options()\r
666 if CommandOptions.LogLevel < EdkLogger.DEBUG_9:\r
667 EdkLogger.SetLevel(CommandOptions.LogLevel + 1)\r
668 else:\r
669 EdkLogger.SetLevel(CommandOptions.LogLevel)\r
5b0671c1 670 except FatalError as X:\r
f51461c8 671 return 1\r
f7496d71 672\r
f51461c8
LG
673 try:\r
674 if CommandOptions.FileType == "Vfr":\r
4231a819 675 if CommandOptions.OutputFile is None:\r
f51461c8
LG
676 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
677 TrimPreprocessedVfr(InputFile, CommandOptions.OutputFile)\r
678 elif CommandOptions.FileType == "Asl":\r
4231a819 679 if CommandOptions.OutputFile is None:\r
f51461c8
LG
680 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
681 TrimAslFile(InputFile, CommandOptions.OutputFile, CommandOptions.IncludePathFile)\r
682 elif CommandOptions.FileType == "EdkSourceCode":\r
683 TrimEdkSources(InputFile, CommandOptions.OutputFile)\r
111dab62
YZ
684 elif CommandOptions.FileType == "VfrOffsetBin":\r
685 GenerateVfrBinSec(CommandOptions.ModuleName, CommandOptions.DebugDir, CommandOptions.OutputFile)\r
f51461c8 686 else :\r
4231a819 687 if CommandOptions.OutputFile is None:\r
f51461c8
LG
688 CommandOptions.OutputFile = os.path.splitext(InputFile)[0] + '.iii'\r
689 TrimPreprocessedFile(InputFile, CommandOptions.OutputFile, CommandOptions.ConvertHex, CommandOptions.TrimLong)\r
5b0671c1 690 except FatalError as X:\r
f51461c8
LG
691 import platform\r
692 import traceback\r
4231a819 693 if CommandOptions is not None and CommandOptions.LogLevel <= EdkLogger.DEBUG_9:\r
f51461c8
LG
694 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
695 return 1\r
696 except:\r
697 import traceback\r
698 import platform\r
699 EdkLogger.error(\r
700 "\nTrim",\r
701 CODE_ERROR,\r
702 "Unknown fatal error when trimming [%s]" % InputFile,\r
3a0f8bde 703 ExtraData="\n(Please send email to edk2-devel@lists.01.org for help, attaching following call stack trace!)\n",\r
f51461c8
LG
704 RaiseError=False\r
705 )\r
706 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())\r
707 return 1\r
708\r
709 return 0\r
710\r
711if __name__ == '__main__':\r
712 r = Main()\r
713 ## 0-127 is a safe return range, and 1 is a standard default error\r
714 if r < 0 or r > 127: r = 1\r
715 sys.exit(r)\r
716\r