1## @file InfPomAlignmentMisc.py
2# This file contained the routines for InfPomAlignment
3#
4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
5#
6# SPDX-License-Identifier: BSD-2-Clause-Patent
7#
8
9'''
10InfPomAlignmentMisc
11'''
12
13##
14# Import modules
15#
16import Logger.Log as Logger
17from Library import DataType as DT
18from Library.Misc import ConvertArchList
19from Object.POM.ModuleObject import BinaryFileObject
20from Object.POM import CommonObject
21from Library.StringUtils import FORMAT_INVALID
22from Library.Misc import CheckGuidRegFormat
23from Logger import StringTable as ST
24
25
26## GenModuleHeaderUserExt
27#
28#
29def GenModuleHeaderUserExt(DefineObj, ArchString):
30    DefinesDictNew = {}
31    EdkReleaseVersion = DefineObj.GetEdkReleaseVersion()
32    Shadow = DefineObj.GetShadow()
33    DpxSource = DefineObj.GetDpxSource()
34    PciVendorId = DefineObj.GetPciVendorId()
35    PciDeviceId = DefineObj.GetPciDeviceId()
36    PciClassCode = DefineObj.GetPciClassCode()
37    PciRevision = DefineObj.GetPciRevision()
38    PciCompress = DefineObj.GetPciCompress()
39    CustomMakefile = DefineObj.GetCustomMakefile()
40    UefiHiiResourceSection = DefineObj.GetUefiHiiResourceSection()
41
42    if EdkReleaseVersion is not None:
43        Name = DT.TAB_INF_DEFINES_EDK_RELEASE_VERSION
44        Value = EdkReleaseVersion.GetValue()
45        Statement = _GenInfDefineStateMent(EdkReleaseVersion.Comments.GetHeaderComments(),
46                                           Name,
47                                           Value,
48                                           EdkReleaseVersion.Comments.GetTailComments())
49        DefinesDictNew[Statement] = ArchString
50
51    if Shadow is not None:
52        Name = DT.TAB_INF_DEFINES_SHADOW
53        Value = Shadow.GetValue()
54        Statement = _GenInfDefineStateMent(Shadow.Comments.GetHeaderComments(),
55                                           Name,
56                                           Value,
57                                           Shadow.Comments.GetTailComments())
58        DefinesDictNew[Statement] = ArchString
59
60    if DpxSource is not None:
61        Name = DT.TAB_INF_DEFINES_DPX_SOURCE
62        for DpxSourceItem in DpxSource:
63            Value = DpxSourceItem[0]
64            Statement = _GenInfDefineStateMent(DpxSourceItem[1].GetHeaderComments(),
65                                               Name,
66                                               Value,
67                                               DpxSourceItem[1].GetTailComments())
68            DefinesDictNew[Statement] = ArchString
69
70    if PciVendorId is not None:
71        Name = DT.TAB_INF_DEFINES_PCI_VENDOR_ID
72        Value = PciVendorId.GetValue()
73        Statement = _GenInfDefineStateMent(PciVendorId.Comments.GetHeaderComments(),
74                                           Name,
75                                           Value,
76                                           PciVendorId.Comments.GetTailComments())
77        DefinesDictNew[Statement] = ArchString
78
79    if PciDeviceId is not None:
80        Name = DT.TAB_INF_DEFINES_PCI_DEVICE_ID
81        Value = PciDeviceId.GetValue()
82        Statement = _GenInfDefineStateMent(PciDeviceId.Comments.GetHeaderComments(),
83                                           Name,
84                                           Value,
85                                           PciDeviceId.Comments.GetTailComments())
86        DefinesDictNew[Statement] = ArchString
87
88    if PciClassCode is not None:
89        Name = DT.TAB_INF_DEFINES_PCI_CLASS_CODE
90        Value = PciClassCode.GetValue()
91        Statement = _GenInfDefineStateMent(PciClassCode.Comments.GetHeaderComments(),
92                                           Name,
93                                           Value,
94                                           PciClassCode.Comments.GetTailComments())
95        DefinesDictNew[Statement] = ArchString
96
97    if PciRevision is not None:
98        Name = DT.TAB_INF_DEFINES_PCI_REVISION
99        Value = PciRevision.GetValue()
100        Statement = _GenInfDefineStateMent(PciRevision.Comments.GetHeaderComments(),
101                                           Name,
102                                           Value,
103                                           PciRevision.Comments.GetTailComments())
104        DefinesDictNew[Statement] = ArchString
105
106    if PciCompress is not None:
107        Name = DT.TAB_INF_DEFINES_PCI_COMPRESS
108        Value = PciCompress.GetValue()
109        Statement = _GenInfDefineStateMent(PciCompress.Comments.GetHeaderComments(),
110                                           Name,
111                                           Value,
112                                           PciCompress.Comments.GetTailComments())
113        DefinesDictNew[Statement] = ArchString
114
115    if len(CustomMakefile) >= 1:
116        for CustomMakefileItem in CustomMakefile:
117            Name = DT.TAB_INF_DEFINES_CUSTOM_MAKEFILE
118            #
119            # Not with Feature Flag Expression
120            #
121            if len(CustomMakefileItem) == 3:
122                if CustomMakefileItem[0] != '':
123                    Value = CustomMakefileItem[0] + ' | ' + CustomMakefileItem[1]
124                else:
125                    Value = CustomMakefileItem[1]
126
127                Comments = CustomMakefileItem[2]
128                Statement = _GenInfDefineStateMent(Comments.GetHeaderComments(),
129                                                   Name,
130                                                   Value,
131                                                   Comments.GetTailComments())
132
133            DefinesDictNew[Statement] = ArchString
134
135    if UefiHiiResourceSection is not None:
136        Name = DT.TAB_INF_DEFINES_UEFI_HII_RESOURCE_SECTION
137        Value = UefiHiiResourceSection.GetValue()
138        HeaderComment = UefiHiiResourceSection.Comments.GetHeaderComments()
139        TailComment = UefiHiiResourceSection.Comments.GetTailComments()
140        Statement = _GenInfDefineStateMent(HeaderComment,
141                                           Name,
142                                           Value,
143                                           TailComment)
144        DefinesDictNew[Statement] = ""
145
146    return DefinesDictNew
147
148
149## Generate the define statement that will be put into userextension
150#  Not support comments.
151#
152# @param HeaderComment: the original header comment (# not removed)
153# @param Name: the definition keyword, should not be empty or none
154# @param Value: the definition keyword value
155# @param TailComment: the original Tail comment (# not removed)
156#
157# @return: the regenerated define statement
158#
159def _GenInfDefineStateMent(HeaderComment, Name, Value, TailComment):
160    Logger.Debug(5, HeaderComment + TailComment)
161    Statement = '%s = %s' % (Name, Value)
162
163    return Statement
164
165## GenBinaryData
166#
167#
168def GenBinaryData(BinaryData, BinaryObj, BinariesDict, AsBuildIns, BinaryFileObjectList, \
169                  SupArchList, BinaryModule, DecObjList=None):
170    if BinaryModule:
171        pass
172    OriSupArchList = SupArchList
173    for Item in BinaryData:
174        ItemObj = BinaryObj[Item][0][0]
175        if ItemObj.GetType() not in DT.BINARY_FILE_TYPE_UI_LIST + DT.BINARY_FILE_TYPE_VER_LIST:
176            TagName = ItemObj.GetTagName()
177            Family = ItemObj.GetFamily()
178        else:
179            TagName = ''
180            Family = ''
181
182        FFE = ItemObj.GetFeatureFlagExp()
183
184        #
185        # If have architecturie specified, then use the specified architecturie;
186        # If the section tag does not have an architecture modifier or the modifier is "common" (case in-sensitive),
187        # and the VALID_ARCHITECTURES comment exists, the list from the VALID_ARCHITECTURES comment
188        # can be used for the attribute.
189        # If both not have VALID_ARCHITECTURE comment and no architecturie specified, then keep it empty.
190        #
191        SupArchList = sorted(ConvertArchList(ItemObj.GetSupArchList()))
192        if len(SupArchList) == 1 and SupArchList[0] == 'COMMON':
193            if not (len(OriSupArchList) == 1 or OriSupArchList[0] == 'COMMON'):
194                SupArchList = OriSupArchList
195            else:
196                SupArchList = ['COMMON']
197
198        FileNameObj = CommonObject.FileNameObject()
199        FileNameObj.SetFileType(ItemObj.GetType())
200        FileNameObj.SetFilename(ItemObj.GetFileName())
201        FileNameObj.SetFeatureFlag(FFE)
202        #
203        # Get GUID value of the GUID CName in the DEC file
204        #
205        if ItemObj.GetType() == DT.SUBTYPE_GUID_BINARY_FILE_TYPE:
206            if not CheckGuidRegFormat(ItemObj.GetGuidValue()):
207                if not DecObjList:
208                    if DT.TAB_HORIZON_LINE_SPLIT in ItemObj.GetGuidValue() or \
209                        DT.TAB_COMMA_SPLIT in ItemObj.GetGuidValue():
210                        Logger.Error("\nMkPkg",
211                                 FORMAT_INVALID,
212                                 ST.ERR_DECPARSE_DEFINE_PKGGUID,
213                                 ExtraData=ItemObj.GetGuidValue(),
214                                 RaiseError=True)
215                    else:
216                        Logger.Error("\nMkPkg",
217                                     FORMAT_INVALID,
218                                     ST.ERR_UNI_SUBGUID_VALUE_DEFINE_DEC_NOT_FOUND % \
219                                     (ItemObj.GetGuidValue()),
220                                     RaiseError=True)
221                else:
222                    for DecObj in DecObjList:
223                        for GuidObj in DecObj.GetGuidList():
224                            if GuidObj.GetCName() == ItemObj.GetGuidValue():
225                                FileNameObj.SetGuidValue(GuidObj.GetGuid())
226                                break
227
228                    if not FileNameObj.GetGuidValue():
229                        Logger.Error("\nMkPkg",
230                                         FORMAT_INVALID,
231                                         ST.ERR_DECPARSE_CGUID_NOT_FOUND % \
232                                         (ItemObj.GetGuidValue()),
233                                         RaiseError=True)
234            else:
235                FileNameObj.SetGuidValue(ItemObj.GetGuidValue().strip())
236
237        FileNameObj.SetSupArchList(SupArchList)
238        FileNameList = [FileNameObj]
239
240        BinaryFile = BinaryFileObject()
241        BinaryFile.SetFileNameList(FileNameList)
242        BinaryFile.SetAsBuiltList(AsBuildIns)
243        BinaryFileObjectList.append(BinaryFile)
244
245        SupArchStr = ' '.join(SupArchList)
246        Key = (ItemObj.GetFileName(), ItemObj.GetType(), FFE, SupArchStr)
247        ValueItem = (ItemObj.GetTarget(), Family, TagName, '')
248        if Key in BinariesDict:
249            ValueList = BinariesDict[Key]
250            ValueList.append(ValueItem)
251            BinariesDict[Key] = ValueList
252        else:
253            BinariesDict[Key] = [ValueItem]
254
255    return BinariesDict, AsBuildIns, BinaryFileObjectList
256