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