]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/UPT/GenMetaFile/GenDecFile.py
Sync BaseTools Branch (version r2271) to EDKII main trunk.
[mirror_edk2.git] / BaseTools / Source / Python / UPT / GenMetaFile / GenDecFile.py
1 ## @file GenDecFile.py
2 #
3 # This file contained the logical of transfer package object to DEC files.
4 #
5 # Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>
6 #
7 # This program and the accompanying materials are licensed and made available
8 # under the terms and conditions of the BSD License which accompanies this
9 # distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
11 #
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #
15
16 '''
17 GenDEC
18 '''
19
20 from Library.Parsing import GenSection
21 from Library.CommentGenerating import GenHeaderCommentSection
22 from Library.CommentGenerating import GenGenericCommentF
23 from Library.CommentGenerating import GenDecTailComment
24 from Library.CommentGenerating import _GetHelpStr
25 from Library.Misc import GuidStringToGuidStructureString
26 from Library.Misc import SaveFileOnChange
27 from Library.Misc import ConvertPath
28 from Library.DataType import TAB_SPACE_SPLIT
29 from Library.DataType import TAB_COMMA_SPLIT
30 from Library.DataType import TAB_ARCH_COMMON
31 from Library.DataType import TAB_DEC_DEFINES_DEC_SPECIFICATION
32 from Library.DataType import TAB_DEC_DEFINES_PACKAGE_NAME
33 from Library.DataType import TAB_DEC_DEFINES_PACKAGE_GUID
34 from Library.DataType import TAB_DEC_DEFINES_PACKAGE_VERSION
35
36
37 def GenPcd(Package, Content):
38 #
39 # generate [Pcd] section
40 # <TokenSpcCName>.<TokenCName>|<Value>|<DatumType>|<Token>
41 #
42 ValidUsageDict = {}
43 for Pcd in Package.GetPcdList():
44 #
45 # Generate generic comment
46 #
47 HelpTextList = Pcd.GetHelpTextList()
48 HelpStr = _GetHelpStr(HelpTextList)
49 CommentStr = GenGenericCommentF(HelpStr, 2)
50
51 PcdErrList = Pcd.GetPcdErrorsList()
52 if PcdErrList:
53 CommentStr += GenPcdErrComment(PcdErrList[0])
54 Statement = CommentStr
55
56 CName = Pcd.GetCName()
57 TokenSpaceGuidCName = Pcd.GetTokenSpaceGuidCName()
58 DefaultValue = Pcd.GetDefaultValue()
59 DatumType = Pcd.GetDatumType()
60 Token = Pcd.GetToken()
61 ValidUsage = Pcd.GetValidUsage()
62
63 if ValidUsage == 'FeaturePcd':
64 ValidUsage = 'PcdsFeatureFlag'
65 elif ValidUsage == 'PatchPcd':
66 ValidUsage = 'PcdsPatchableInModule'
67 elif ValidUsage == 'FixedPcd':
68 ValidUsage = 'PcdsFixedAtBuild'
69 elif ValidUsage == 'Pcd':
70 ValidUsage = 'PcdsDynamic'
71 elif ValidUsage == 'PcdEx':
72 ValidUsage = 'PcdsDynamicEx'
73
74 if ValidUsage in ValidUsageDict:
75 NewSectionDict = ValidUsageDict[ValidUsage]
76 else:
77 NewSectionDict = {}
78 ValidUsageDict[ValidUsage] = NewSectionDict
79 Statement += TokenSpaceGuidCName + '.' + CName
80 Statement += '|' + DefaultValue
81 Statement += '|' + DatumType
82 Statement += '|' + Token
83 #
84 # generate tail comment
85 #
86 if Pcd.GetSupModuleList():
87 Statement += GenDecTailComment(Pcd.GetSupModuleList())
88
89 ArchList = Pcd.GetSupArchList()
90 ArchList.sort()
91 SortedArch = ' '.join(ArchList)
92 if SortedArch in NewSectionDict:
93 NewSectionDict[SortedArch] = \
94 NewSectionDict[SortedArch] + [Statement]
95 else:
96 NewSectionDict[SortedArch] = [Statement]
97
98 for ValidUsage in ValidUsageDict:
99 Content += GenSection(ValidUsage, ValidUsageDict[ValidUsage])
100
101 return Content
102
103 def GenGuidProtocolPpi(Package, Content):
104 #
105 # generate [Guids] section
106 #
107 NewSectionDict = {}
108 for Guid in Package.GetGuidList():
109 #
110 # Generate generic comment
111 #
112 HelpTextList = Guid.GetHelpTextList()
113 HelpStr = _GetHelpStr(HelpTextList)
114 CommentStr = GenGenericCommentF(HelpStr, 2)
115
116 Statement = CommentStr
117 CName = Guid.GetCName()
118 Value = GuidStringToGuidStructureString(Guid.GetGuid())
119 Statement += CName + ' = ' + Value
120 #
121 # generate tail comment
122 #
123 if Guid.GetSupModuleList():
124 Statement += GenDecTailComment(Guid.GetSupModuleList())
125 ArchList = Guid.GetSupArchList()
126 ArchList.sort()
127 SortedArch = ' '.join(ArchList)
128 if SortedArch in NewSectionDict:
129 NewSectionDict[SortedArch] = \
130 NewSectionDict[SortedArch] + [Statement]
131 else:
132 NewSectionDict[SortedArch] = [Statement]
133
134 Content += GenSection('Guids', NewSectionDict)
135
136 #
137 # generate [Protocols] section
138 #
139 NewSectionDict = {}
140 for Protocol in Package.GetProtocolList():
141 #
142 # Generate generic comment
143 #
144 HelpTextList = Protocol.GetHelpTextList()
145 HelpStr = _GetHelpStr(HelpTextList)
146 CommentStr = GenGenericCommentF(HelpStr, 2)
147
148 Statement = CommentStr
149 CName = Protocol.GetCName()
150 Value = GuidStringToGuidStructureString(Protocol.GetGuid())
151 Statement += CName + ' = ' + Value
152
153 #
154 # generate tail comment
155 #
156 if Protocol.GetSupModuleList():
157 Statement += GenDecTailComment(Protocol.GetSupModuleList())
158 ArchList = Protocol.GetSupArchList()
159 ArchList.sort()
160 SortedArch = ' '.join(ArchList)
161 if SortedArch in NewSectionDict:
162 NewSectionDict[SortedArch] = \
163 NewSectionDict[SortedArch] + [Statement]
164 else:
165 NewSectionDict[SortedArch] = [Statement]
166
167 Content += GenSection('Protocols', NewSectionDict)
168
169 #
170 # generate [Ppis] section
171 #
172 NewSectionDict = {}
173 for Ppi in Package.GetPpiList():
174 #
175 # Generate generic comment
176 #
177 HelpTextList = Ppi.GetHelpTextList()
178 HelpStr = _GetHelpStr(HelpTextList)
179 CommentStr = GenGenericCommentF(HelpStr, 2)
180
181 Statement = CommentStr
182 CName = Ppi.GetCName()
183 Value = GuidStringToGuidStructureString(Ppi.GetGuid())
184 Statement += CName + ' = ' + Value
185
186 #
187 # generate tail comment
188 #
189 if Ppi.GetSupModuleList():
190 Statement += GenDecTailComment(Ppi.GetSupModuleList())
191 ArchList = Ppi.GetSupArchList()
192 ArchList.sort()
193 SortedArch = ' '.join(ArchList)
194 if SortedArch in NewSectionDict:
195 NewSectionDict[SortedArch] = \
196 NewSectionDict[SortedArch] + [Statement]
197 else:
198 NewSectionDict[SortedArch] = [Statement]
199
200 Content += GenSection('Ppis', NewSectionDict)
201
202 return Content
203
204 ## Transfer Package Object to Dec files
205 #
206 # Transfer all contents of a standard Package Object to a Dec file
207 #
208 # @param Package: A Package
209 #
210 def PackageToDec(Package):
211 #
212 # Init global information for the file
213 #
214 ContainerFile = Package.GetFullPath()
215
216 Content = ''
217 #
218 # generate header comment section
219 #
220 Content += GenHeaderCommentSection(Package.GetAbstract(), \
221 Package.GetDescription(), \
222 Package.GetCopyright(), \
223 Package.GetLicense())
224
225 #
226 # for each section, maintain a dict, sorted arch will be its key,
227 #statement list will be its data
228 # { 'Arch1 Arch2 Arch3': [statement1, statement2],
229 # 'Arch1' : [statement1, statement3]
230 # }
231 #
232
233 #
234 # generate [Defines] section
235 #
236 NewSectionDict = {TAB_ARCH_COMMON : []}
237 SpecialItemList = []
238
239 Statement = '%s = %s' % (TAB_DEC_DEFINES_DEC_SPECIFICATION, '0x00010017')
240 SpecialItemList.append(Statement)
241
242 BaseName = Package.GetBaseName()
243 if BaseName.startswith('.') or BaseName.startswith('-'):
244 BaseName = '_' + BaseName
245 Statement = '%s = %s' % (TAB_DEC_DEFINES_PACKAGE_NAME, BaseName)
246 SpecialItemList.append(Statement)
247 Statement = '%s = %s' % (TAB_DEC_DEFINES_PACKAGE_VERSION, Package.GetVersion())
248 SpecialItemList.append(Statement)
249 Statement = '%s = %s' % (TAB_DEC_DEFINES_PACKAGE_GUID, Package.GetGuid())
250 SpecialItemList.append(Statement)
251 for SortedArch in NewSectionDict:
252 NewSectionDict[SortedArch] = \
253 NewSectionDict[SortedArch] + SpecialItemList
254 Content += GenSection('Defines', NewSectionDict)
255
256 #
257 # generate [Includes] section
258 #
259 NewSectionDict = {}
260 IncludeArchList = Package.GetIncludeArchList()
261 if IncludeArchList:
262 for Path, ArchList in IncludeArchList:
263 Statement = Path
264 ArchList.sort()
265 SortedArch = ' '.join(ArchList)
266 if SortedArch in NewSectionDict:
267 NewSectionDict[SortedArch] = \
268 NewSectionDict[SortedArch] + [ConvertPath(Statement)]
269 else:
270 NewSectionDict[SortedArch] = [ConvertPath(Statement)]
271
272 Content += GenSection('Includes', NewSectionDict)
273
274 Content = GenGuidProtocolPpi(Package, Content)
275
276 #
277 # generate [LibraryClasses] section
278 #
279 NewSectionDict = {}
280 for LibraryClass in Package.GetLibraryClassList():
281 #
282 # Generate generic comment
283 #
284 HelpTextList = LibraryClass.GetHelpTextList()
285 HelpStr = _GetHelpStr(HelpTextList)
286 if HelpStr:
287 HelpStr = '@libraryclass ' + HelpStr
288 CommentStr = GenGenericCommentF(HelpStr, 2)
289
290 Statement = CommentStr
291 Name = LibraryClass.GetLibraryClass()
292 IncludeHeader = LibraryClass.GetIncludeHeader()
293 Statement += Name + '|' + ConvertPath(IncludeHeader)
294 #
295 # generate tail comment
296 #
297 if LibraryClass.GetSupModuleList():
298 Statement += \
299 GenDecTailComment(LibraryClass.GetSupModuleList())
300 ArchList = LibraryClass.GetSupArchList()
301 ArchList.sort()
302 SortedArch = ' '.join(ArchList)
303 if SortedArch in NewSectionDict:
304 NewSectionDict[SortedArch] = \
305 NewSectionDict[SortedArch] + [Statement]
306 else:
307 NewSectionDict[SortedArch] = [Statement]
308
309 Content += GenSection('LibraryClasses', NewSectionDict)
310
311 Content = GenPcd(Package, Content)
312
313 #
314 # generate [UserExtensions] section
315 #
316 NewSectionDict = {}
317 for UserExtension in Package.GetUserExtensionList():
318 Statement = UserExtension.GetStatement()
319 if not Statement:
320 continue
321
322 SectionList = []
323 SectionName = 'UserExtensions'
324 UserId = UserExtension.GetUserID()
325 if UserId:
326 if '.' in UserId:
327 UserId = '"' + UserId + '"'
328 SectionName += '.' + UserId
329 if UserExtension.GetIdentifier():
330 SectionName += '.' + '"' + UserExtension.GetIdentifier() + '"'
331 if not UserExtension.GetSupArchList():
332 SectionList.append(SectionName)
333 else:
334 for Arch in UserExtension.GetSupArchList():
335 SectionList.append(SectionName + '.' + Arch)
336 SectionName = ', '.join(SectionList)
337 SectionName = ''.join(['[', SectionName, ']\n'])
338 Content += '\n\n' + SectionName + Statement
339
340 SaveFileOnChange(ContainerFile, Content, False)
341 return ContainerFile
342
343 ## GenPcdErrComment
344 #
345 # @param PcdErrObject: PcdErrorObject
346 #
347 # @retval CommentStr: Generated comment lines, with prefix "#"
348 #
349 def GenPcdErrComment (PcdErrObject):
350 EndOfLine = "\n"
351 ValidValueRange = PcdErrObject.GetValidValueRange()
352 if ValidValueRange:
353 CommentStr = "# @ValidRange " + ValidValueRange + EndOfLine
354
355 ValidValue = PcdErrObject.GetValidValue()
356 if ValidValue:
357 ValidValueList = \
358 [Value for Value in ValidValue.split(TAB_SPACE_SPLIT) if Value]
359 CommentStr = \
360 "# @ValidList " + TAB_COMMA_SPLIT.join(ValidValueList) + EndOfLine
361
362 Expression = PcdErrObject.GetExpression()
363 if Expression:
364 CommentStr = "# @Expression " + Expression + EndOfLine
365
366 return CommentStr
367