1## @file
2# This file is used to define class objects of INF file [Sources] section.
3# It will consumed by InfParser.
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'''
16InfSourcesObject
17'''
18
19import os
20
21from Logger import StringTable as ST
22from Logger import ToolError
23import Logger.Log as Logger
24from Library import GlobalData
25
26from Library.Misc import Sdict
27from Library.ExpressionValidate import IsValidFeatureFlagExp
28from Object.Parser.InfCommonObject import InfSectionCommonDef
29from Library.Misc import ValidFile
30from Library.ParserValidate import IsValidFamily
31from Library.ParserValidate import IsValidPath
32
33## __GenSourceInstance
34#
35#
36def GenSourceInstance(Item, CurrentLineOfItem, ItemObj):
37
38    IsValidFileFlag = False
39
40    if len(Item) < 6 and len(Item) >= 1:
41        #
42        # File | Family | TagName | ToolCode | FeatureFlagExpr
43        #
44        if len(Item) == 5:
45            #
46            # Validate Feature Flag Express
47            #
48            if Item[4].strip() == '':
49                Logger.Error("InfParser",
50                             ToolError.FORMAT_INVALID,
51                             ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_MISSING,
52                             File=CurrentLineOfItem[2],
53                             Line=CurrentLineOfItem[1],
54                             ExtraData=CurrentLineOfItem[0])
55            #
56            # Validate FFE
57            #
58            FeatureFlagRtv = IsValidFeatureFlagExp(Item[4].strip())
59            if not FeatureFlagRtv[0]:
60                Logger.Error("InfParser",
61                             ToolError.FORMAT_INVALID,
62                             ST.ERR_INF_PARSER_FEATURE_FLAG_EXP_SYNTAX_INVLID%(FeatureFlagRtv[1]),
63                             File=CurrentLineOfItem[2],
64                             Line=CurrentLineOfItem[1],
65                             ExtraData=CurrentLineOfItem[0])
66            ItemObj.SetFeatureFlagExp(Item[4])
67        if len(Item) >= 4:
68            if Item[3].strip() == '':
69                ItemObj.SetToolCode(Item[3])
70            else:
71                Logger.Error("InfParser",
72                             ToolError.FORMAT_INVALID,
73                             ST.ERR_INF_PARSER_TOOLCODE_NOT_PERMITTED%(Item[2]),
74                             File=CurrentLineOfItem[2],
75                             Line=CurrentLineOfItem[1],
76                             ExtraData=CurrentLineOfItem[0])
77        if len(Item) >= 3:
78            if Item[2].strip() == '':
79                ItemObj.SetTagName(Item[2])
80            else:
81                Logger.Error("InfParser",
82                             ToolError.FORMAT_INVALID,
83                             ST.ERR_INF_PARSER_TAGNAME_NOT_PERMITTED%(Item[2]),
84                             File=CurrentLineOfItem[2],
85                             Line=CurrentLineOfItem[1],
86                             ExtraData=CurrentLineOfItem[0])
87        if len(Item) >= 2:
88            if IsValidFamily(Item[1].strip()):
89                #
90                # To align with UDP specification. "*" is not permitted in UDP specification
91                #
92                if Item[1].strip() == "*":
93                    Item[1] = ""
94                ItemObj.SetFamily(Item[1])
95            else:
96                Logger.Error("InfParser",
97                             ToolError.FORMAT_INVALID,
98                             ST.ERR_INF_PARSER_SOURCE_SECTION_FAMILY_INVALID%(Item[1]),
99                             File=CurrentLineOfItem[2],
100                             Line=CurrentLineOfItem[1],
101                             ExtraData=CurrentLineOfItem[0])
102        if len(Item) >= 1:
103            #
104            # Validate file name exist.
105            #
106            FullFileName = os.path.normpath(os.path.realpath(os.path.join(GlobalData.gINF_MODULE_DIR, Item[0])))
107            if not (ValidFile(FullFileName) or ValidFile(Item[0])):
108                Logger.Error("InfParser",
109                             ToolError.FORMAT_INVALID,
110                             ST.ERR_FILELIST_EXIST%(Item[0]),
111                             File=CurrentLineOfItem[2],
112                             Line=CurrentLineOfItem[1],
113                             ExtraData=CurrentLineOfItem[0])
114
115            #
116            # Validate file exist/format.
117            #
118
119            if IsValidPath(Item[0], GlobalData.gINF_MODULE_DIR):
120                IsValidFileFlag = True
121            else:
122                Logger.Error("InfParser",
123                             ToolError.FORMAT_INVALID,
124                             ST.ERR_INF_PARSER_FILE_NOT_EXIST_OR_NAME_INVALID%(Item[0]),
125                             File=CurrentLineOfItem[2],
126                             Line=CurrentLineOfItem[1],
127                             ExtraData=CurrentLineOfItem[0])
128                return False
129            if IsValidFileFlag:
130                ItemObj.SetSourceFileName(Item[0])
131    else:
132        Logger.Error("InfParser",
133                     ToolError.FORMAT_INVALID,
134                     ST.ERR_INF_PARSER_SOURCES_SECTION_CONTENT_ERROR,
135                     File=CurrentLineOfItem[2],
136                     Line=CurrentLineOfItem[1],
137                     ExtraData=CurrentLineOfItem[0])
138
139    return ItemObj
140
141## InfSourcesItemObject()
142#
143#
144class InfSourcesItemObject():
145    def __init__(self, \
146                 SourceFileName = '', \
147                 Family = '', \
148                 TagName = '', \
149                 ToolCode = '', \
150                 FeatureFlagExp = ''):
151        self.SourceFileName = SourceFileName
152        self.Family         = Family
153        self.TagName        = TagName
154        self.ToolCode       = ToolCode
155        self.FeatureFlagExp = FeatureFlagExp
156        self.HeaderString   = ''
157        self.TailString     = ''
158        self.SupArchList    = []
159
160    def SetSourceFileName(self, SourceFilename):
161        self.SourceFileName = SourceFilename
162    def GetSourceFileName(self):
163        return self.SourceFileName
164
165    def SetFamily(self, Family):
166        self.Family = Family
167    def GetFamily(self):
168        return self.Family
169
170    def SetTagName(self, TagName):
171        self.TagName = TagName
172    def GetTagName(self):
173        return self.TagName
174
175    def SetToolCode(self, ToolCode):
176        self.ToolCode = ToolCode
177    def GetToolCode(self):
178        return self.ToolCode
179
180    def SetFeatureFlagExp(self, FeatureFlagExp):
181        self.FeatureFlagExp = FeatureFlagExp
182    def GetFeatureFlagExp(self):
183        return self.FeatureFlagExp
184
185    def SetHeaderString(self, HeaderString):
186        self.HeaderString = HeaderString
187    def GetHeaderString(self):
188        return self.HeaderString
189
190    def SetTailString(self, TailString):
191        self.TailString = TailString
192    def GetTailString(self):
193        return self.TailString
194
195    def SetSupArchList(self, SupArchList):
196        self.SupArchList = SupArchList
197    def GetSupArchList(self):
198        return self.SupArchList
199##
200#
201#
202#
203class InfSourcesObject(InfSectionCommonDef):
204    def __init__(self):
205        self.Sources = Sdict()
206        InfSectionCommonDef.__init__(self)
207
208    def SetSources(self, SourceList, Arch = None):
209        __SupArchList = []
210        for ArchItem in Arch:
211            #
212            # Validate Arch
213            #
214            if (ArchItem == '' or ArchItem == None):
215                ArchItem = 'COMMON'
216            __SupArchList.append(ArchItem)
217
218        for Item in SourceList:
219            ItemObj = InfSourcesItemObject()
220            CurrentLineOfItem = Item[2]
221            Item = Item[0]
222
223            ItemObj = GenSourceInstance(Item, CurrentLineOfItem, ItemObj)
224
225            ItemObj.SetSupArchList(__SupArchList)
226
227            if self.Sources.has_key((ItemObj)):
228                SourceContent = self.Sources[ItemObj]
229                SourceContent.append(ItemObj)
230                self.Sources[ItemObj] = SourceContent
231            else:
232                SourceContent = []
233                SourceContent.append(ItemObj)
234                self.Sources[ItemObj] = SourceContent
235
236        return True
237
238    def GetSources(self):
239        return self.Sources
240