]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/Parser/InfAsBuiltProcess.py
Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Parser / InfAsBuiltProcess.py
1 ## @file
2 # This file is used to provide method for process AsBuilt INF file. It will consumed by InfParser
3 #
4 # Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 '''
14 InfAsBuiltProcess
15 '''
16 ## Import modules
17 #
18
19 import os
20 import re
21 from Library import GlobalData
22
23 import Logger.Log as Logger
24 from Logger import StringTable as ST
25 from Logger import ToolError
26
27 from Library.String import GetSplitValueList
28 from Library.Misc import GetHelpStringByRemoveHashKey
29 from Library.Misc import ValidFile
30 from Library.Misc import ProcessLineExtender
31 from Library.ParserValidate import IsValidPath
32 from Library.Parsing import MacroParser
33 from Parser.InfParserMisc import InfExpandMacro
34
35 from Library import DataType as DT
36
37 ## GetLibInstanceInfo
38 #
39 # Get the information from Library Instance INF file.
40 #
41 # @param string. A string start with # and followed by INF file path
42 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
43 #
44 # @return GUID, Version
45 def GetLibInstanceInfo(String, WorkSpace, LineNo):
46
47 FileGuidString = ""
48 VerString = ""
49
50 OrignalString = String
51 String = String.strip()
52 if not String:
53 return None, None
54 #
55 # Remove "#" characters at the beginning
56 #
57 String = GetHelpStringByRemoveHashKey(String)
58 String = String.strip()
59
60 FileLinesList = GetFileLineContent(String, WorkSpace, LineNo, OrignalString)
61
62
63 ReFindFileGuidPattern = re.compile("^\s*FILE_GUID\s*=.*$")
64 ReFindVerStringPattern = re.compile("^\s*VERSION_STRING\s*=.*$")
65
66 FileLinesList = ProcessLineExtender(FileLinesList)
67
68 for Line in FileLinesList:
69 if ReFindFileGuidPattern.match(Line):
70 FileGuidString = Line
71 if ReFindVerStringPattern.match(Line):
72 VerString = Line
73
74 if FileGuidString:
75 FileGuidString = GetSplitValueList(FileGuidString, '=', 1)[1]
76 if VerString:
77 VerString = GetSplitValueList(VerString, '=', 1)[1]
78
79 return FileGuidString, VerString
80
81 ## GetPackageListInfo
82 #
83 # Get the package information from INF file.
84 #
85 # @param string. A string start with # and followed by INF file path
86 # @param WorkSpace. The WorkSpace directory used to combined with INF file path.
87 #
88 # @return GUID, Version
89 def GetPackageListInfo(FileNameString, WorkSpace, LineNo):
90 PackageInfoList = []
91 DefineSectionMacros = {}
92 PackageSectionMacros = {}
93
94 FileLinesList = GetFileLineContent(FileNameString, WorkSpace, LineNo, '')
95
96 RePackageHeader = re.compile('^\s*\[Packages.*\].*$')
97 ReDefineHeader = re.compile('^\s*\[Defines].*$')
98
99 PackageHederFlag = False
100 DefineHeaderFlag = False
101 LineNo = -1
102 for Line in FileLinesList:
103 LineNo += 1
104 Line = Line.strip()
105
106 if Line.startswith('['):
107 PackageHederFlag = False
108 DefineHeaderFlag = False
109
110 if Line.startswith("#"):
111 continue
112
113 if not Line:
114 continue
115
116 #
117 # Found [Packages] section
118 #
119 if RePackageHeader.match(Line):
120 PackageHederFlag = True
121 continue
122
123 #
124 # Found [Define] section
125 #
126 if ReDefineHeader.match(Line):
127 DefineHeaderFlag = True
128 continue
129
130 if DefineHeaderFlag:
131 #
132 # Find Macro
133 #
134 Name, Value = MacroParser((Line, LineNo),
135 FileNameString,
136 DT.MODEL_META_DATA_HEADER,
137 DefineSectionMacros)
138
139 if Name != None:
140 DefineSectionMacros[Name] = Value
141 continue
142
143 if PackageHederFlag:
144
145 #
146 # Find Macro
147 #
148 Name, Value = MacroParser((Line, LineNo),
149 FileNameString,
150 DT.MODEL_META_DATA_PACKAGE,
151 DefineSectionMacros)
152 if Name != None:
153 PackageSectionMacros[Name] = Value
154 continue
155
156 #
157 # Replace with Local section Macro and [Defines] section Macro.
158 #
159 Line = InfExpandMacro(Line, (FileNameString, Line, LineNo), DefineSectionMacros, PackageSectionMacros, True)
160
161 Line = GetSplitValueList(Line, "#", 1)[0]
162 Line = GetSplitValueList(Line, "|", 1)[0]
163 PackageInfoList.append(Line)
164
165 return PackageInfoList
166
167 def GetFileLineContent(FileName, WorkSpace, LineNo, OriginalString):
168
169 if not LineNo:
170 LineNo = -1
171
172 #
173 # Validate file name exist.
174 #
175 FullFileName = os.path.normpath(os.path.realpath(os.path.join(WorkSpace, FileName)))
176 if not (ValidFile(FullFileName)):
177 Logger.Error("InfParser",
178 ToolError.FORMAT_INVALID,
179 ST.ERR_FILELIST_EXIST%(FileName),
180 File=GlobalData.gINF_MODULE_NAME,
181 Line=LineNo,
182 ExtraData=OriginalString)
183
184 #
185 # Validate file exist/format.
186 #
187 if IsValidPath(FileName, WorkSpace):
188 IsValidFileFlag = True
189 else:
190 Logger.Error("InfParser",
191 ToolError.FORMAT_INVALID,
192 ST.ERR_INF_PARSER_FILE_NOT_EXIST_OR_NAME_INVALID%(FileName),
193 File=GlobalData.gINF_MODULE_NAME,
194 Line=LineNo,
195 ExtraData=OriginalString)
196 return False
197
198 FileLinesList = []
199
200 if IsValidFileFlag:
201 try:
202 FullFileName = FullFileName.replace('\\', '/')
203 Inputfile = open(FullFileName, "rb", 0)
204 try:
205 FileLinesList = Inputfile.readlines()
206 except BaseException:
207 Logger.Error("InfParser", ToolError.FILE_READ_FAILURE, ST.ERR_FILE_OPEN_FAILURE, File=FullFileName)
208 finally:
209 Inputfile.close()
210 except BaseException:
211 Logger.Error("InfParser",
212 ToolError.FILE_READ_FAILURE,
213 ST.ERR_FILE_OPEN_FAILURE,
214 File=FullFileName)
215
216 FileLinesList = ProcessLineExtender(FileLinesList)
217
218 return FileLinesList
219