1## @file GenMetaFileMisc.py
2#
3# This file contained the miscellaneous routines for GenMetaFile usage.
4#
5# Copyright (c) 2011 - 2014, 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'''
17GenMetaFileMisc
18'''
19
20from Library import DataType as DT
21from Library import GlobalData
22from Parser.DecParser import Dec
23
24# AddExternToDefineSec
25#
26#  @param SectionDict: string of source file path/name
27#  @param Arch:     string of source file family field
28#  @param ExternList:  string of source file FeatureFlag field
29#
30def AddExternToDefineSec(SectionDict, Arch, ExternList):
31    LeftOffset = 31
32    for ArchList, EntryPoint, UnloadImage, Constructor, Destructor, FFE, HelpStringList in ExternList:
33        if Arch or ArchList:
34            if EntryPoint:
35                Statement = (u'%s ' % DT.TAB_INF_DEFINES_ENTRY_POINT).ljust(LeftOffset) + u'= %s' % EntryPoint
36                if FFE:
37                    Statement += ' | %s' % FFE
38                if len(HelpStringList) > 0:
39                    Statement = HelpStringList[0].GetString() + '\n' + Statement
40                if len(HelpStringList) > 1:
41                    Statement = Statement + HelpStringList[1].GetString()
42                SectionDict[Arch] = SectionDict[Arch] + [Statement]
43
44            if UnloadImage:
45                Statement = (u'%s ' % DT.TAB_INF_DEFINES_UNLOAD_IMAGE).ljust(LeftOffset) + u'= %s' % UnloadImage
46                if FFE:
47                    Statement += ' | %s' % FFE
48
49                if len(HelpStringList) > 0:
50                    Statement = HelpStringList[0].GetString() + '\n' + Statement
51                if len(HelpStringList) > 1:
52                    Statement = Statement + HelpStringList[1].GetString()
53                SectionDict[Arch] = SectionDict[Arch] + [Statement]
54
55            if Constructor:
56                Statement = (u'%s ' % DT.TAB_INF_DEFINES_CONSTRUCTOR).ljust(LeftOffset) + u'= %s' % Constructor
57                if FFE:
58                    Statement += ' | %s' % FFE
59
60                if len(HelpStringList) > 0:
61                    Statement = HelpStringList[0].GetString() + '\n' + Statement
62                if len(HelpStringList) > 1:
63                    Statement = Statement + HelpStringList[1].GetString()
64                SectionDict[Arch] = SectionDict[Arch] + [Statement]
65
66            if Destructor:
67                Statement = (u'%s ' % DT.TAB_INF_DEFINES_DESTRUCTOR).ljust(LeftOffset) + u'= %s' % Destructor
68                if FFE:
69                    Statement += ' | %s' % FFE
70
71                if len(HelpStringList) > 0:
72                    Statement = HelpStringList[0].GetString() + '\n' + Statement
73                if len(HelpStringList) > 1:
74                    Statement = Statement + HelpStringList[1].GetString()
75                SectionDict[Arch] = SectionDict[Arch] + [Statement]
76
77## ObtainPcdName
78#
79# Using TokenSpaceGuidValue and Token to obtain PcdName from DEC file
80#
81def ObtainPcdName(Packages, TokenSpaceGuidValue, Token):
82    for PackageDependency in Packages:
83        #
84        # Generate generic comment
85        #
86        Guid = PackageDependency.GetGuid()
87        Version = PackageDependency.GetVersion()
88
89        #
90        # find package path/name
91        #
92        for PkgInfo in GlobalData.gWSPKG_LIST:
93            if Guid == PkgInfo[1]:
94                if (not Version) or (Version == PkgInfo[2]):
95                    Path = PkgInfo[3]
96                    break
97
98        DecFile = None
99        if Path not in GlobalData.gPackageDict:
100            DecFile = Dec(Path)
101            GlobalData.gPackageDict[Path] = DecFile
102        else:
103            DecFile = GlobalData.gPackageDict[Path]
104
105        DecGuidsDict = DecFile.GetGuidSectionObject().ValueDict
106        DecPcdsDict = DecFile.GetPcdSectionObject().ValueDict
107
108        TokenSpaceGuidName = ''
109        PcdCName = ''
110        TokenSpaceGuidNameFound = False
111        PcdCNameFound = False
112
113        #
114        # Get TokenSpaceGuidCName from Guids section
115        #
116        for GuidKey in DecGuidsDict:
117            GuidList = DecGuidsDict[GuidKey]
118            if TokenSpaceGuidNameFound:
119                break
120            for GuidItem in GuidList:
121                if TokenSpaceGuidValue.upper() == GuidItem.GuidString.upper():
122                    TokenSpaceGuidName = GuidItem.GuidCName
123                    TokenSpaceGuidNameFound = True
124                    break
125
126        #
127        # Retrieve PcdCName from Pcds Section
128        #
129        for PcdKey in DecPcdsDict:
130            PcdList = DecPcdsDict[PcdKey]
131            if PcdCNameFound:
132                return TokenSpaceGuidName, PcdCName
133            for PcdItem in PcdList:
134                if TokenSpaceGuidName == PcdItem.TokenSpaceGuidCName and Token == PcdItem.TokenValue:
135                    PcdCName = PcdItem.TokenCName
136                    PcdCNameFound = True
137                    break
138
139    return TokenSpaceGuidName, PcdCName
140
141## _TransferDict
142#  transfer dict that using (Statement, SortedArch) as key,
143#  (GenericComment, UsageComment) as value into a dict that using SortedArch as
144#  key and NewStatement as value
145#
146def TransferDict(OrigDict, Type=None):
147    NewDict = {}
148    LeftOffset = 0
149    if Type in ['INF_GUID', 'INF_PPI_PROTOCOL']:
150        LeftOffset = 45
151    if Type in ['INF_PCD']:
152        LeftOffset = 75
153    if LeftOffset > 0:
154        for Statement, SortedArch in OrigDict:
155            if len(Statement) > LeftOffset:
156                LeftOffset = len(Statement)
157
158    for Statement, SortedArch in OrigDict:
159        Comment = OrigDict[Statement, SortedArch]
160        #
161        # apply the NComment/1Comment rule
162        #
163        if Comment.find('\n') != len(Comment) - 1:
164            NewStateMent = Comment + Statement
165        else:
166            if LeftOffset:
167                NewStateMent = Statement.ljust(LeftOffset) + ' ' + Comment.rstrip('\n')
168            else:
169                NewStateMent = Statement + ' ' + Comment.rstrip('\n')
170
171        if SortedArch in NewDict:
172            NewDict[SortedArch] = NewDict[SortedArch] + [NewStateMent]
173        else:
174            NewDict[SortedArch] = [NewStateMent]
175
176    return NewDict
177
178