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