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