]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/spd2dec/StoreDec.py
Sync EDKII BaseTools to BaseTools project r1971
[mirror_edk2.git] / BaseTools / Source / Python / spd2dec / StoreDec.py
1 ## @file
2 # Store a Package class object to a DEC file.
3 #
4 # Copyright (c) 2007, 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 from Common.MigrationUtilities import *
19 from LoadSpd import LoadSpd
20 from CommonDataClass.PackageClass import *
21
22
23 ## Store Defines section.
24 #
25 # Write [Defines] section to the DecFile based on Package class object.
26 # Different CPU architectures are specified in the subsection if possible.
27 #
28 # @param DecFile The output DEC file to store the Defines section.
29 # @param Package An input Package class object.
30 #
31 def StorePackageDefinesSection(DecFile, Package):
32 DefinesTupleList = []
33 DefinesTupleList.append(("DEC_VERSION", Package.Header.DecSpecification))
34 DefinesTupleList.append(("PACKAGE_NAME", Package.Header.Name))
35 DefinesTupleList.append(("PACKAGE_GUID", Package.Header.Guid))
36
37 StoreDefinesSection(DecFile, DefinesTupleList)
38
39
40 ## Return a Package Include Class Item.
41 #
42 # Read the input Include class object and return one Include Class Item.
43 #
44 # @param Include An input Include class object.
45 #
46 # @retval IncludeClassItem A Package Include Class Item.
47 #
48 def GetPackageIncludeClassItem(Include):
49 return Include.FilePath
50
51
52 ## Store Includes section.
53 #
54 # Write [Includes] section to the DecFile based on Package class object.
55 # Different CPU architectures are specified in the subsection if possible.
56 #
57 # @param DecFile The output DEC file to store the Includes section.
58 # @param Package An input Package class object.
59 #
60 def StorePackageIncludesSection(DecFile, Package):
61 Includes = Package.Includes
62 Section = GetSection("Includes", GetPackageIncludeClassItem, Includes)
63 StoreTextFile(DecFile, Section)
64
65
66 ## Return a Package Library Class Item.
67 #
68 # Read the input LibraryClass class object and return one Library Class Item.
69 #
70 # @param LibraryClass An input LibraryClass class object.
71 #
72 # @retval LibraryClassItem A Package Library Class Item.
73 #
74 def GetPackageLibraryClassItem(LibraryClass):
75 return "|".join((LibraryClass.LibraryClass, LibraryClass.IncludeHeader))
76
77
78 ## Store Library Classes section.
79 #
80 # Write [LibraryClasses] section to the DecFile based on Package class object.
81 # Different CPU architectures are specified in the subsection if possible.
82 #
83 # @param DecFile The output DEC file to store the Library Classes
84 # section.
85 # @param Package An input Package class object.
86 #
87 def StorePackageLibraryClassesSection(DecFile, Package):
88 LibraryClasses = Package.LibraryClassDeclarations
89 Section = GetSection("LibraryClasses", GetPackageLibraryClassItem, LibraryClasses)
90 StoreTextFile(DecFile, Section)
91
92
93 ## Return a Package Guid Declaration Item.
94 #
95 # Read the input Guid class object and return one line of Guid Declaration Item.
96 #
97 # @param Guid An input Guid class object.
98 #
99 # @retval GuidDeclarationItem A Package Guid Declaration Item.
100 #
101 def GetPackageGuidDeclarationItem(Guid):
102 GuidCName = Guid.CName
103 GuidValue = Guid.Guid.replace("-", "")
104 GuidValueList = [GuidValue[0:8]]
105 GuidValueList += [GuidValue[i : i + 4] for i in range(8, 16, 4)]
106 GuidValueList += [GuidValue[i : i + 2] for i in range(16, 32, 2)]
107
108 GuidCFormat = "{0x%s" + ", 0x%s" * 2 + ", {0x%s" + ", 0x%s" * 7 + "}}"
109 GuidCValue = GuidCFormat % tuple(GuidValueList)
110 return "%-30s = %s" % (GuidCName, GuidCValue)
111
112
113 ## Store Protocols section.
114 #
115 # Write [Protocols] section to the DecFile based on Package class object.
116 # Different CPU architectures are specified in the subsection if possible.
117 #
118 # @param DecFile The output DEC file to store the Protocols section.
119 # @param Package An input Package class object.
120 #
121 def StorePackageProtocolsSection(DecFile, Package):
122 Protocols = Package.ProtocolDeclarations
123 Section = GetSection("Protocols", GetPackageGuidDeclarationItem, Protocols)
124 StoreTextFile(DecFile, Section)
125
126
127 ## Store Ppis section.
128 #
129 # Write [Ppis] section to the DecFile based on Package class object.
130 # Different CPU architectures are specified in the subsection if possible.
131 #
132 # @param DecFile The output DEC file to store the Ppis section.
133 # @param Package An input Package class object.
134 #
135 def StorePackagePpisSection(DecFile, Package):
136 Ppis = Package.PpiDeclarations
137 Section = GetSection("Ppis", GetPackageGuidDeclarationItem, Ppis)
138 StoreTextFile(DecFile, Section)
139
140
141 ## Store Guids section.
142 #
143 # Write [Guids] section to the DecFile based on Package class object.
144 # Different CPU architectures are specified in the subsection if possible.
145 #
146 # @param DecFile The output DEC file to store the Guids section.
147 # @param Package An input Package class object.
148 #
149 def StorePackageGuidsSection(DecFile, Package):
150 Guids = Package.GuidDeclarations
151 Section = GetSection("Guids", GetPackageGuidDeclarationItem, Guids)
152 StoreTextFile(DecFile, Section)
153
154
155 ## Return a Package Pcd Item.
156 #
157 # Read the input Pcd class object and return one line of Pcd Item.
158 #
159 # @param Pcd An input Pcd class object.
160 #
161 # @retval PcdItem A Package Pcd Item.
162 #
163 def GetPackagePcdItem(Pcd):
164 PcdPair = "%s.%s" % (Pcd.TokenSpaceGuidCName, Pcd.CName)
165 DatumType = Pcd.DatumType
166 DefaultValue = Pcd.DefaultValue
167 Token = Pcd.Token
168 PcdList = [PcdPair, DefaultValue, DatumType, Token]
169 return "|".join(PcdList)
170
171
172 ## DEC Pcd Section Name dictionary indexed by PCD Item Type.
173 mDecPcdSectionNameDict = {
174 "FEATURE_FLAG" : "PcdsFeatureFlag",
175 "FIXED_AT_BUILD" : "PcdsFixedAtBuild",
176 "PATCHABLE_IN_MODULE" : "PcdsPatchableInModule",
177 "DYNAMIC" : "PcdsDynamic",
178 "DYNAMIC_EX" : "PcdsDynamicEx"
179 }
180
181 ## Store Pcds section.
182 #
183 # Write [Pcds*] section to the DecFile based on Package class object.
184 # Different CPU architectures are specified in the subsection if possible.
185 #
186 # @param DecFile The output DEC file to store the Pcds section.
187 # @param Package An input Package class object.
188 #
189 def StorePackagePcdsSection(DecFile, Package):
190 PcdsDict = {}
191 for Pcd in Package.PcdDeclarations:
192 for PcdItemType in Pcd.ValidUsage:
193 PcdSectionName = mDecPcdSectionNameDict.get(PcdItemType)
194 if PcdSectionName:
195 PcdsDict.setdefault(PcdSectionName, []).append(Pcd)
196 else:
197 EdkLogger.info("Unknown Pcd Item Type: %s" % PcdItemType)
198
199 Section = ""
200 for PcdSectionName in PcdsDict:
201 Pcds = PcdsDict[PcdSectionName]
202 Section += GetSection(PcdSectionName, GetPackagePcdItem, Pcds)
203
204 StoreTextFile(DecFile, Section)
205
206
207 ## Store User Extensions section.
208 #
209 # Write [UserExtensions] section to the DecFile based on Package class object.
210 #
211 # @param DecFile The output DEC file to store the User Extensions section.
212 # @param Package An input Package class object.
213 #
214 def StorePackageUserExtensionsSection(DecFile, Package):
215 Section = "".join(map(GetUserExtensions, Package.UserExtensions))
216 StoreTextFile(DecFile, Section)
217
218
219 ## Store a Package class object to a new DEC file.
220 #
221 # Read an input Package class object and ave the contents to a new DEC file.
222 #
223 # @param DecFileName The output DEC file.
224 # @param Package An input Package class object.
225 #
226 def StoreDec(DecFileName, Package):
227 DecFile = open(DecFileName, "w+")
228 EdkLogger.info("Save file to %s" % DecFileName)
229
230 StoreHeader(DecFile, Package.Header)
231 StorePackageDefinesSection(DecFile, Package)
232 StorePackageIncludesSection(DecFile, Package)
233 StorePackageLibraryClassesSection(DecFile, Package)
234 StorePackageProtocolsSection(DecFile, Package)
235 StorePackagePpisSection(DecFile, Package)
236 StorePackageGuidsSection(DecFile, Package)
237 StorePackagePcdsSection(DecFile, Package)
238 StorePackageUserExtensionsSection(DecFile, Package)
239
240 DecFile.close()
241
242
243 # This acts like the main() function for the script, unless it is 'import'ed
244 # into another script.
245 if __name__ == '__main__':
246 pass
247