summaryrefslogtreecommitdiff
path: root/BaseTools/Source/Python/spd2dec/StoreDec.py
blob: 67cbd11e9b4883782c815f08960beb0e0b7ba7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
## @file
# Store a Package class object to a DEC file.
#
# Copyright (c) 2007, Intel Corporation
# All rights reserved. This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution.  The full text of the license may be found at
# http://opensource.org/licenses/bsd-license.php
#
# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#

##
# Import Modules
#
import os
from Common.MigrationUtilities import *
from LoadSpd import LoadSpd
from CommonDataClass.PackageClass import *


## Store Defines section.
#
# Write [Defines] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile            The output DEC file to store the Defines section.
# @param  Package            An input Package class object.
#
def StorePackageDefinesSection(DecFile, Package):
    DefinesTupleList = []
    DefinesTupleList.append(("DEC_VERSION", Package.Header.DecSpecification))
    DefinesTupleList.append(("PACKAGE_NAME", Package.Header.Name))
    DefinesTupleList.append(("PACKAGE_GUID", Package.Header.Guid))
    
    StoreDefinesSection(DecFile, DefinesTupleList)


## Return a Package Include Class Item.
#
# Read the input Include class object and return one Include Class Item.
#
# @param  Include            An input Include class object.
#
# @retval IncludeClassItem   A Package Include Class Item.
#
def GetPackageIncludeClassItem(Include):
    return Include.FilePath


## Store Includes section.
#
# Write [Includes] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile            The output DEC file to store the Includes section.
# @param  Package            An input Package class object.
#
def StorePackageIncludesSection(DecFile, Package):
    Includes = Package.Includes
    Section = GetSection("Includes", GetPackageIncludeClassItem, Includes)
    StoreTextFile(DecFile, Section)
    

## Return a Package Library Class Item.
#
# Read the input LibraryClass class object and return one Library Class Item.
#
# @param  LibraryClass       An input LibraryClass class object.
#
# @retval LibraryClassItem   A Package Library Class Item.
#
def GetPackageLibraryClassItem(LibraryClass):
    return "|".join((LibraryClass.LibraryClass, LibraryClass.IncludeHeader))


## Store Library Classes section.
#
# Write [LibraryClasses] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile            The output DEC file to store the Library Classes
#                            section.
# @param  Package            An input Package class object.
#
def StorePackageLibraryClassesSection(DecFile, Package):
    LibraryClasses = Package.LibraryClassDeclarations
    Section = GetSection("LibraryClasses", GetPackageLibraryClassItem, LibraryClasses)
    StoreTextFile(DecFile, Section)


## Return a Package Guid Declaration Item.
#
# Read the input Guid class object and return one line of Guid Declaration Item.
#
# @param  Guid                An input Guid class object.
#
# @retval GuidDeclarationItem A Package Guid Declaration Item.
#
def GetPackageGuidDeclarationItem(Guid):
    GuidCName = Guid.CName
    GuidValue = Guid.Guid.replace("-", "")
    GuidValueList = [GuidValue[0:8]]
    GuidValueList += [GuidValue[i : i + 4] for i in range(8, 16, 4)]
    GuidValueList += [GuidValue[i : i + 2] for i in range(16, 32, 2)]

    GuidCFormat = "{0x%s" + ", 0x%s" * 2 + ", {0x%s" + ", 0x%s" * 7 + "}}"
    GuidCValue = GuidCFormat % tuple(GuidValueList)
    return "%-30s = %s" % (GuidCName, GuidCValue)


## Store Protocols section.
#
# Write [Protocols] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile            The output DEC file to store the Protocols section.
# @param  Package            An input Package class object.
#
def StorePackageProtocolsSection(DecFile, Package):
    Protocols = Package.ProtocolDeclarations
    Section = GetSection("Protocols", GetPackageGuidDeclarationItem, Protocols)
    StoreTextFile(DecFile, Section)
    

## Store Ppis section.
#
# Write [Ppis] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile            The output DEC file to store the Ppis section.
# @param  Package            An input Package class object.
#
def StorePackagePpisSection(DecFile, Package):
    Ppis = Package.PpiDeclarations
    Section = GetSection("Ppis", GetPackageGuidDeclarationItem, Ppis)
    StoreTextFile(DecFile, Section)


## Store Guids section.
#
# Write [Guids] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile            The output DEC file to store the Guids section.
# @param  Package            An input Package class object.
#
def StorePackageGuidsSection(DecFile, Package):
    Guids = Package.GuidDeclarations
    Section = GetSection("Guids", GetPackageGuidDeclarationItem, Guids)
    StoreTextFile(DecFile, Section)


## Return a Package Pcd Item.
#
# Read the input Pcd class object and return one line of Pcd Item.
#
# @param  Pcd                An input Pcd class object.
#
# @retval PcdItem            A Package Pcd Item.
#
def GetPackagePcdItem(Pcd):
    PcdPair = "%s.%s" % (Pcd.TokenSpaceGuidCName, Pcd.CName)
    DatumType = Pcd.DatumType
    DefaultValue = Pcd.DefaultValue
    Token = Pcd.Token
    PcdList = [PcdPair, DefaultValue, DatumType, Token]
    return "|".join(PcdList)


## DEC Pcd Section Name dictionary indexed by PCD Item Type.
mDecPcdSectionNameDict = {
    "FEATURE_FLAG" : "PcdsFeatureFlag",
    "FIXED_AT_BUILD" : "PcdsFixedAtBuild",
    "PATCHABLE_IN_MODULE" : "PcdsPatchableInModule",
    "DYNAMIC" : "PcdsDynamic",
    "DYNAMIC_EX" : "PcdsDynamicEx"
    }

## Store Pcds section.
#
# Write [Pcds*] section to the DecFile based on Package class object.
# Different CPU architectures are specified in the subsection if possible.
#
# @param  DecFile              The output DEC file to store the Pcds section.
# @param  Package              An input Package class object.
#
def StorePackagePcdsSection(DecFile, Package):
    PcdsDict = {}
    for Pcd in Package.PcdDeclarations:
        for PcdItemType in Pcd.ValidUsage:
            PcdSectionName = mDecPcdSectionNameDict.get(PcdItemType)
            if PcdSectionName:
                PcdsDict.setdefault(PcdSectionName, []).append(Pcd)
            else:
                EdkLogger.info("Unknown Pcd Item Type: %s" % PcdItemType)

    Section = ""
    for PcdSectionName in PcdsDict:
        Pcds = PcdsDict[PcdSectionName]
        Section += GetSection(PcdSectionName, GetPackagePcdItem, Pcds)

    StoreTextFile(DecFile, Section)


## Store User Extensions section.
#
# Write [UserExtensions] section to the DecFile based on Package class object.
#
# @param  DecFile              The output DEC file to store the User Extensions section.
# @param  Package              An input Package class object.
#
def StorePackageUserExtensionsSection(DecFile, Package):
    Section = "".join(map(GetUserExtensions, Package.UserExtensions))
    StoreTextFile(DecFile, Section)


## Store a Package class object to a new DEC file.
#
# Read an input Package class object and ave the contents to a new DEC file.
#
# @param  DecFileName          The output DEC file.
# @param  Package              An input Package class object.
#
def StoreDec(DecFileName, Package):
    DecFile = open(DecFileName, "w+")
    EdkLogger.info("Save file to %s" % DecFileName)
    
    StoreHeader(DecFile, Package.Header)
    StorePackageDefinesSection(DecFile, Package)
    StorePackageIncludesSection(DecFile, Package)
    StorePackageLibraryClassesSection(DecFile, Package)
    StorePackageProtocolsSection(DecFile, Package)
    StorePackagePpisSection(DecFile, Package)
    StorePackageGuidsSection(DecFile, Package)
    StorePackagePcdsSection(DecFile, Package)
    StorePackageUserExtensionsSection(DecFile, Package)
    
    DecFile.close()

    
# This acts like the main() function for the script, unless it is 'import'ed
# into another script.
if __name__ == '__main__':
    pass