eb0e1c2f8c40c53ffb4de47271d29144f611a598
[people/mcb30/basetools.git] / Source / Python / Workspace / WorkspaceDatabase.py
1 ## @file\r
2 # This file is used to create a database used by build tool\r
3 #\r
4 # Copyright (c) 2008, Intel Corporation\r
5 # All rights reserved. This program and the accompanying materials\r
6 # are licensed and made available under the terms and conditions of the BSD License\r
7 # which accompanies this distribution.  The full text of the license may be found at\r
8 # http://opensource.org/licenses/bsd-license.php\r
9 #\r
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12 #\r
13 \r
14 ##\r
15 # Import Modules\r
16 #\r
17 import sqlite3\r
18 import os\r
19 import os.path\r
20 \r
21 import Common.EdkLogger as EdkLogger\r
22 from CommonDataClass.DataClass import *\r
23 from CommonDataClass.ModuleClass import *\r
24 from Common.String import *\r
25 from Common.DataType import *\r
26 from Common.Misc import tdict\r
27 from Common.Misc import sdict\r
28 \r
29 from MetaDataTable import *\r
30 from MetaFileTable import *\r
31 from MetaFileParser import *\r
32 from BuildClassObject import *\r
33 \r
34 ## Check if gvien file exists or not\r
35\r
36 #   @param      File    File name or path to be checked\r
37 #   @param      Dir     The directory the file is relative to\r
38\r
39 #   @retval     True    if file exists\r
40 #   @retval     False   if file doesn't exists\r
41\r
42 def ValidFile(File, Dir='.'):\r
43     Wd = os.getcwd()\r
44     os.chdir(Dir)\r
45     if not os.path.exists(File):\r
46         os.chdir(Wd)\r
47         return False\r
48     os.chdir(Wd)\r
49     return True\r
50 \r
51 ## Get GUID value from given packages\r
52\r
53 #   @param      CName           The CName of the GUID\r
54 #   @param      PackageList     List of packages looking-up in\r
55\r
56 #   @retval     GuidValue   if the CName is found in any given package\r
57 #   @retval     None        if the CName is not found in all given packages\r
58\r
59 def GuidValue(CName, PackageList):\r
60     for P in PackageList:\r
61         if CName in P.Guids:\r
62             return P.Guids[CName]\r
63         if CName in P.Protocols:\r
64             return P.Protocols[CName]\r
65         if CName in P.Ppis:\r
66             return P.Ppis[CName]\r
67     return None\r
68 \r
69 ## Platform build information from DSC file\r
70 #\r
71 #  This class is used to retrieve information stored in database and convert them\r
72 # into PlatformBuildClassObject form for easier use for AutoGen.\r
73 #\r
74 class DscBuildData(PlatformBuildClassObject):\r
75     # dict used to convert PCD type in database to string used by build tool\r
76     _PCD_TYPE_STRING_ = {\r
77         MODEL_PCD_FIXED_AT_BUILD        :   "FixedAtBuild",\r
78         MODEL_PCD_PATCHABLE_IN_MODULE   :   "PatchableInModule",\r
79         MODEL_PCD_FEATURE_FLAG          :   "FeatureFlag",\r
80         MODEL_PCD_DYNAMIC               :   "Dynamic",\r
81         MODEL_PCD_DYNAMIC_DEFAULT       :   "Dynamic",\r
82         MODEL_PCD_DYNAMIC_HII           :   "DynamicHii",\r
83         MODEL_PCD_DYNAMIC_VPD           :   "DynamicVpd",\r
84         MODEL_PCD_DYNAMIC_EX            :   "DynamicEx",\r
85         MODEL_PCD_DYNAMIC_EX_DEFAULT    :   "DynamicEx",\r
86         MODEL_PCD_DYNAMIC_EX_HII        :   "DynamicExHii",\r
87         MODEL_PCD_DYNAMIC_EX_VPD        :   "DynamicExVpd",\r
88     }\r
89 \r
90     # used to compose dummy library class name for those forced library instances\r
91     _NullLibraryNumber = 0\r
92     _NullLibraryClass = {}  # file : class\r
93 \r
94     def __init__(self, FilePath, Table, Db, Arch='COMMON', Macros={}):\r
95         self.DescFilePath = FilePath\r
96         self._Table = Table\r
97         self._Db = Db\r
98         self._Arch = Arch\r
99         self._Macros = Macros\r
100         self._Clear()\r
101 \r
102     def __repr__(self):\r
103         S = '[Platform.%s]\n' % self.Arch\r
104         S += "\tName = %s\n" % self.PlatformName\r
105         S += "\tGuid = %s\n" % self.Guid\r
106         S += "\tVer = %s\n" % self.Version\r
107         S += "\n"\r
108         S += "\tSpecification = %s\n" % self.DscSpecification\r
109         S += "\tOutputDirectory = %s\n" % self.OutputDirectory \r
110         S += "\tSupArchList = %s\n" % self.SupArchList     \r
111         S += "\tBuildTargets = %s\n" % self.BuildTargets    \r
112         S += "\tSkuName = %s\n" % self.SkuName           \r
113         S += "\tFlashDefinition = %s\n" % self.FlashDefinition \r
114         S += "\tBuildNumber = %s\n" % self.BuildNumber     \r
115         S += "\tMakefileName = %s\n" % self.MakefileName    \r
116         S += "\tBsBaseAddress = %s\n" % self.BsBaseAddress   \r
117         S += "\tRtBaseAddress = %s\n" % self.RtBaseAddress   \r
118 \r
119         S += '  <SkuId>\n'\r
120         for SkuName in self.SkuIds:\r
121             S += "\t%s = %s\n" % (SkuName, self.SkuIds[SkuName])\r
122 \r
123         #S += '  <LibraryClass>\n'\r
124         #ModuleTypeList = set()\r
125         #LibraryClassList = set()\r
126         #for LibraryClass,ModuleType in self.LibraryClasses:\r
127         #    LibraryClassList.add(LibraryClass)\r
128         #    ModuleTypeList.add(ModuleType)\r
129         #LibraryClassList = list(LibraryClassList)\r
130         #ModuleTypeList = list(ModuleTypeList)\r
131         #LibraryClassList.sort()\r
132         #ModuleTypeList.sort()\r
133         #for LibraryClass in LibraryClassList:\r
134         #    for ModuleType in ModuleTypeList:\r
135         #        if not (LibraryClass,ModuleType) in self.LibraryClasses:\r
136         #            continue \r
137         #        S += "\t%32s, %-24s = %s\n" % (LibraryClass, ModuleType, self.LibraryClasses[LibraryClass,ModuleType])\r
138         \r
139         S += '  <PCD>\n'\r
140         for Name, Guid in self.Pcds:\r
141             S += "\t%s.%s\n\t\t%s\n" % (Guid, Name, str(self.Pcds[Name, Guid]))\r
142         \r
143         S += '  <BuildOption>\n'\r
144         for ToolChainFamily,ToolChain in self.BuildOptions:\r
145             S += "\t%s:%s = %s\n" % (ToolChainFamily, ToolChain, self.BuildOptions[ToolChainFamily, ToolChain])\r
146 \r
147         S += '  <Module>\n'\r
148         S += "\t" + "\n\t".join([str(M) for M in self.Modules]) + '\n'\r
149         return S\r
150 \r
151     def _Clear(self):\r
152         self._PlatformName      = None\r
153         self._Guid              = None\r
154         self._Version           = None\r
155         self._DscSpecification  = None\r
156         self._OutputDirectory   = None\r
157         self._SupArchList       = None\r
158         self._BuildTargets      = None\r
159         self._SkuName           = None\r
160         self._FlashDefinition   = None\r
161         self._BuildNumber       = None\r
162         self._MakefileName      = None\r
163         self._BsBaseAddress     = None\r
164         self._RtBaseAddress     = None\r
165         self._SkuIds            = None\r
166         self._Modules           = None\r
167         self._LibraryInstances  = None\r
168         self._LibraryClasses    = None\r
169         self._Libraries         = None\r
170         self._Pcds              = None\r
171         self._BuildOptions      = None\r
172 \r
173     def _GetArch(self):\r
174         return self._Arch\r
175 \r
176     def _SetArch(self, Value):\r
177         if self._Arch == Value:\r
178             return\r
179         self._Arch = Value\r
180         self._Clear()\r
181 \r
182     def _GetPlatformName(self):\r
183         if self._PlatformName == None:\r
184             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_PLATFORM_NAME, self.Arch)\r
185             if len(RecordList) == 0 or RecordList[0][0] == None:\r
186                 EdkLogger.error('build', ATTRIBUTE_NOT_AVAILABLE, "No PLATFORM_NAME", File=self.DescFilePath)\r
187             self._PlatformName = RecordList[0][0]\r
188         return self._PlatformName\r
189 \r
190     def _GetFileGuid(self):\r
191         if self._Guid == None:\r
192             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_PLATFORM_GUID, self.Arch)\r
193             if len(RecordList) == 0 or RecordList[0][0] == None:\r
194                 EdkLogger.error('build', ATTRIBUTE_NOT_AVAILABLE, "No FILE_GUID", File=self.DescFilePath)\r
195             self._Guid = RecordList[0][0]\r
196         return self._Guid\r
197 \r
198     def _GetVersion(self):\r
199         if self._Version == None:\r
200             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_PLATFORM_VERSION, self.Arch)\r
201             if len(RecordList) == 0 or RecordList[0][0] == None:\r
202                 self._Version = 0.0\r
203             else:\r
204                 self._Version = RecordList[0][0]\r
205         return self._Version\r
206 \r
207     def _GetDscSpec(self):\r
208         if self._DscSpecification == None:\r
209             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_DSC_SPECIFICATION, self.Arch)\r
210             if len(RecordList) == 0 or RecordList[0][0] == None:\r
211                 self._DscSpecification = ''\r
212             else:\r
213                 self._DscSpecification = RecordList[0][0]\r
214         return self._DscSpecification\r
215 \r
216     def _GetOutpuDir(self):\r
217         if self._OutputDirectory == None:\r
218             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_OUTPUT_DIRECTORY, self.Arch)\r
219             if len(RecordList) == 0 or RecordList[0][0] == None:\r
220                 self._OutputDirectory = os.path.join("Build", self.PlatformName)\r
221             else:\r
222                 File = NormPath(RecordList[0][0], self._Macros)\r
223                 LineNo = RecordList[0][-1]\r
224                 self._OutputDirectory = File\r
225         return self._OutputDirectory\r
226 \r
227     def _GetSupArch(self):\r
228         if self._SupArchList == None:\r
229             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_SUPPORTED_ARCHITECTURES, self.Arch)\r
230             if len(RecordList) == 0 or RecordList[0][0] == None:\r
231                 self._SupArchList = ARCH_LIST\r
232             else:\r
233                 self._SupArchList = GetSplitValueList(RecordList[0][0], TAB_VALUE_SPLIT)\r
234         return self._SupArchList\r
235 \r
236     def _GetBuildTarget(self):\r
237         if self._BuildTargets == None:\r
238             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_BUILD_TARGETS, self.Arch)\r
239             if len(RecordList) == 0 or RecordList[0][0] == None:\r
240                 self._BuildTargets = ['DEBUG', 'RELEASE']\r
241             else:\r
242                 self._BuildTargets = GetSplitValueList(RecordList[0][0])\r
243         return self._BuildTargets\r
244 \r
245     def _GetSkuName(self):\r
246         if self._SkuName == None:\r
247             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_SKUID_IDENTIFIER, self.Arch)\r
248             if len(RecordList) > 0:\r
249                 self._SkuName = RecordList[0][0]\r
250                 if self._SkuName not in self.SkuIds:\r
251                     self._SkuName = 'DEFAULT'\r
252             else:\r
253                 self._SkuName = 'DEFAULT'\r
254         return self._SkuName\r
255 \r
256     def _SetSkuName(self, Value):\r
257         if Value in self.SkuIds:\r
258             self._SkuName = Value\r
259 \r
260     def _GetFdfFile(self):\r
261         if self._FlashDefinition == None:\r
262             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_FLASH_DEFINITION, self.Arch)\r
263             if len(RecordList) > 0:\r
264                 self._FlashDefinition = NormPath(RecordList[0][0])\r
265             else:\r
266                 self._FlashDefinition = ''\r
267         return self._FlashDefinition\r
268 \r
269     def _GetBuildNumber(self):\r
270         if self._BuildNumber == None:\r
271             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_BUILD_NUMBER, self.Arch)\r
272             if len(RecordList) > 0 and RecordList[0][0] != None:\r
273                 self._BuildNumber = RecordList[0][0]\r
274             else:\r
275                 self._BuildNumber = ''\r
276         return self._BuildNumber\r
277 \r
278     def _GetMakefileName(self):\r
279         if self._MakefileName == None:\r
280             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_MAKEFILE_NAME, self.Arch)\r
281             if len(RecordList) > 0 and RecordList[0][0] != None:\r
282                 self._MakefileName = RecordList[0][0]\r
283             else:\r
284                 self._MakefileName = ''\r
285         return self._MakefileName\r
286 \r
287     def _GetBsBaseAddress(self):\r
288         if self._BsBaseAddress == None:\r
289             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_BS_BASE_ADDRESS, self.Arch)\r
290             if len(RecordList) > 0 and RecordList[0][0] != None:\r
291                 self._BsBaseAddress = RecordList[0][0]\r
292             else:\r
293                 self._BsBaseAddress = ''\r
294         return self._BsBaseAddress\r
295 \r
296     def _GetRtBaseAddress(self):\r
297         if self._RtBaseAddress == None:\r
298             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DSC_DEFINES_RT_BASE_ADDRESS, self.Arch)\r
299             if len(RecordList) > 0 and RecordList[0][0] != None:\r
300                 self._RtBaseAddress = RecordList[0][0]\r
301             else:\r
302                 self._RtBaseAddress = ''\r
303         return self._RtBaseAddress\r
304 \r
305     def _GetSkuIds(self):\r
306         if self._SkuIds == None:\r
307             self._SkuIds = {}\r
308             RecordList = self._Table.Query(MODEL_EFI_SKU_ID)\r
309             for Record in RecordList:\r
310                 if Record[0] in [None, '']:\r
311                     EdkLogger.error('build', FORMAT_INVALID, 'No Sku ID number',\r
312                                     File=self.DescFilePath, Line=Record[-1])\r
313                 if Record[1] in [None, '']:\r
314                     EdkLogger.error('build', FORMAT_INVALID, 'No Sku ID name',\r
315                                     File=self.DescFilePath, Line=Record[-1])\r
316                 self._SkuIds[Record[1]] = Record[0]\r
317             if 'DEFAULT' not in self._SkuIds:\r
318                 self._SkuIds['DEFAULT'] = 0\r
319         return self._SkuIds\r
320 \r
321     def _GetModules(self):\r
322         if self._Modules == None:\r
323             self._Modules = []\r
324             RecordList = self._Table.Query(MODEL_META_DATA_COMPONENT, Scope1=self.Arch)\r
325             for Record in RecordList:\r
326                 ModuleFile = NormPath(Record[0], self._Macros)\r
327                 ModuleId = Record[5]\r
328                 LineNo = Record[6]\r
329                 if not ValidFile(ModuleFile):\r
330                     EdkLogger.error('build', FILE_NOT_FOUND, File=self.DescFilePath, \r
331                                     ExtraData=ModuleFile, Line=LineNo)\r
332                 if ModuleFile in self._Modules:\r
333                     continue\r
334                 Module = self._Db.BuildObject[ModuleFile, MODEL_FILE_INF, self._Arch]\r
335                 # only merge library classes and PCD for non-library module\r
336                 if Module.LibraryClass == None or Module.LibraryClass == []:\r
337                     if Module.AutoGenVersion < 0x00010005:\r
338                         self._ResolveLibraryReference(Module)\r
339                     else:\r
340                         self._MergeModuleInfo(Module, ModuleId)\r
341                 self._UpdateModulePcd(Module, ModuleId)\r
342                 self._MergeModuleBuildOption(Module, ModuleId)\r
343                 self._Modules.append(Module)\r
344         return self._Modules\r
345 \r
346     def _MergeModuleInfo(self, Module, ModuleId):\r
347         ModuleType = Module.ModuleType\r
348         # merge library class/instance information\r
349         for LibraryClass in Module.LibraryClasses:\r
350             if self.LibraryClasses[LibraryClass, ModuleType] == None: continue\r
351             Module.LibraryClasses[LibraryClass] = self.LibraryClasses[LibraryClass, ModuleType]\r
352         RecordList = self._Table.Query(MODEL_EFI_LIBRARY_CLASS, Scope1=self.Arch, BelongsToItem=ModuleId)\r
353         for Record in RecordList:\r
354             LibraryClass = Record[0]\r
355             LibraryPath = NormPath(Record[1], self._Macros)\r
356             LineNo = Record[-1]\r
357             if not ValidFile(LibraryPath):\r
358                 EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=LibraryPath,\r
359                                 File=self.DescFilePath, Line=LineNo)\r
360             if LibraryClass == '' or LibraryClass == 'NULL':\r
361                 if LibraryPath not in self._NullLibraryClass:\r
362                     self._NullLibraryNumber += 1\r
363                     LibraryClass = LibraryClassObject('NULL%d' % self._NullLibraryNumber, [ModuleType])\r
364                     LibraryInstance = self._Db.BuildObject[LibraryPath, MODEL_FILE_INF, self._Arch]\r
365                     LibraryInstance.LibraryClass.append(LibraryClass)\r
366                     self._NullLibraryClass[LibraryPath] = LibraryClass\r
367                 else:\r
368                     LibraryClass = self._NullLibraryClass[LibraryPath]\r
369                     # add new supported module type\r
370                     if ModuleType not in LibraryClass.SupModList:\r
371                         LibraryClass.SupModList.append(ModuleType)\r
372                     LibraryInstance = self._Db.BuildObject[LibraryPath, MODEL_FILE_INF, self._Arch]\r
373                     LibraryInstance.LibraryClass.append(LibraryClass)\r
374 \r
375             Module.LibraryClasses[LibraryClass] = LibraryPath\r
376 \r
377         # R9 module\r
378         LibraryConsumerList = [Module]\r
379         Constructor         = []\r
380         ConsumedByList      = sdict()\r
381         LibraryInstance     = sdict()\r
382 \r
383         EdkLogger.verbose("")\r
384         EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), self.Arch))\r
385         while len(LibraryConsumerList) > 0:\r
386             M = LibraryConsumerList.pop()\r
387             for LibraryClassName in M.LibraryClasses:\r
388                 LibraryPath = M.LibraryClasses[LibraryClassName]\r
389                 if LibraryPath == None or LibraryPath == "":\r
390                     LibraryPath = self.LibraryClasses[LibraryClassName, ModuleType]\r
391                     if LibraryPath == None and LibraryClassName not in LibraryInstance:\r
392                         LibraryInstance[LibraryClassName] = None\r
393                         continue\r
394                 if LibraryClassName not in LibraryInstance:\r
395                     LibraryModule = self._Db.BuildObject[LibraryPath, MODEL_FILE_INF, self._Arch]\r
396                     LibraryInstance[LibraryClassName] = LibraryModule\r
397                     LibraryConsumerList.append(LibraryModule)\r
398                     EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))\r
399                 else:\r
400                     LibraryModule = LibraryInstance[LibraryClassName]\r
401 \r
402                 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:\r
403                     Constructor.append(LibraryModule)\r
404 \r
405                 if LibraryModule not in ConsumedByList:\r
406                     ConsumedByList[LibraryModule] = []\r
407                 if M != Module:\r
408                     if M in ConsumedByList[LibraryModule]:\r
409                         continue\r
410                     ConsumedByList[LibraryModule].append(M)\r
411         #\r
412         # Initialize the sorted output list to the empty set\r
413         #\r
414         SortedLibraryList = []\r
415         #\r
416         # Q <- Set of all nodes with no incoming edges\r
417         #\r
418         LibraryList = [] #LibraryInstance.values()\r
419         Q = []\r
420         for LibraryClassName in LibraryInstance:\r
421             M = LibraryInstance[LibraryClassName]\r
422             if M == None:\r
423                 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,\r
424                                 "\tLibrary instance of library class [%s] is not found" % LibraryClassName,\r
425                                 File=self.DescFilePath, ExtraData="consumed by [%s] [%s]" % (str(Module), self._Arch))\r
426             LibraryList.append(M)\r
427             #\r
428             # check if there're duplicate library classes\r
429             #\r
430             for Lc in M.LibraryClass:\r
431                 if Lc.SupModList != None and ModuleType not in Lc.SupModList:\r
432                     EdkLogger.error("build", OPTION_MISSING,\r
433                                     "Module type [%s] is not supported by library instance [%s]" % (ModuleType, str(M)),\r
434                                     File=self.DescFilePath, ExtraData="\tconsumed by [%s]" % str(Module))\r
435 \r
436                 if Lc.LibraryClass in LibraryInstance and str(M) != str(LibraryInstance[Lc.LibraryClass]):\r
437                     EdkLogger.error("build", OPTION_CONFLICT,\r
438                                     "More than one library instance found for library class [%s] in module [%s]" % (Lc.LibraryClass, str(Module)),\r
439                                     ExtraData="\t%s\n\t%s" % (LibraryInstance[Lc.LibraryClass], str(M))\r
440                                     )\r
441             if ConsumedByList[M] == []:\r
442                 Q.insert(0, M)\r
443 \r
444         #\r
445         # start the  DAG algorithm\r
446         #\r
447         while True:\r
448             EdgeRemoved = True\r
449             while Q == [] and EdgeRemoved:\r
450                 EdgeRemoved = False\r
451                 # for each node Item with a Constructor\r
452                 for Item in LibraryList:\r
453                     if Item not in Constructor:\r
454                         continue\r
455                     # for each Node without a constructor with an edge e from Item to Node\r
456                     for Node in ConsumedByList[Item]:\r
457                         if Node in Constructor:\r
458                             continue\r
459                         # remove edge e from the graph if Node has no constructor\r
460                         ConsumedByList[Item].remove(Node)\r
461                         EdgeRemoved = True\r
462                         if ConsumedByList[Item] == []:\r
463                             # insert Item into Q\r
464                             Q.insert(0, Item)\r
465                             break\r
466                     if Q != []:\r
467                         break\r
468             # DAG is done if there's no more incoming edge for all nodes\r
469             if Q == []:\r
470                 break\r
471 \r
472             # remove node from Q\r
473             Node = Q.pop()\r
474             # output Node\r
475             SortedLibraryList.append(Node)\r
476 \r
477             # for each node Item with an edge e from Node to Item do\r
478             for Item in LibraryList:\r
479                 if Node not in ConsumedByList[Item]:\r
480                     continue\r
481                 # remove edge e from the graph\r
482                 ConsumedByList[Item].remove(Node)\r
483 \r
484                 if ConsumedByList[Item] != []:\r
485                     continue\r
486                 # insert Item into Q, if Item has no other incoming edges\r
487                 Q.insert(0, Item)\r
488 \r
489         #\r
490         # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle\r
491         #\r
492         for Item in LibraryList:\r
493             if ConsumedByList[Item] != [] and Item in Constructor and len(Constructor) > 1:\r
494                 ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join([str(L) for L in ConsumedByList[Item]])\r
495                 EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),\r
496                                 ExtraData=ErrorMessage, File=self.DescFilePath)\r
497             if Item not in SortedLibraryList:\r
498                 SortedLibraryList.append(Item)\r
499 \r
500         #\r
501         # Build the list of constructor and destructir names\r
502         # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order\r
503         #\r
504         SortedLibraryList.reverse()\r
505         Module.LibraryClasses = sdict()\r
506         for L in SortedLibraryList:\r
507             Module.LibraryClasses[L.LibraryClass[0].LibraryClass, ModuleType] = L\r
508             #\r
509             # Merge PCDs from library instance\r
510             #\r
511             for Key in L.Pcds:\r
512                 if Key not in Module.Pcds:\r
513                     Module.Pcds[Key] = L.Pcds[Key]\r
514             #\r
515             # Merge GUIDs from library instance\r
516             #\r
517             for CName in L.Guids:\r
518                 if CName not in Module.Guids:\r
519                     Module.Guids[CName] = L.Guids[CName]\r
520             #\r
521             # Merge Protocols from library instance\r
522             #\r
523             for CName in L.Protocols:\r
524                 if CName not in Module.Protocols:\r
525                     Module.Protocols[CName] = L.Protocols[CName]\r
526             #\r
527             # Merge Ppis from library instance\r
528             #\r
529             for CName in L.Ppis:\r
530                 if CName not in Module.Ppis:\r
531                     Module.Ppis[CName] = L.Ppis[CName]\r
532 \r
533     ##\r
534     # for R8.x modules\r
535     # \r
536     def _ResolveLibraryReference(self, Module):\r
537         EdkLogger.verbose("")\r
538         EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), self._Arch))\r
539         LibraryConsumerList = [Module]\r
540 \r
541         # "CompilerStub" is a must for R8 modules\r
542         Module.Libraries.append("CompilerStub")\r
543         while len(LibraryConsumerList) > 0:\r
544             M = LibraryConsumerList.pop()\r
545             for LibraryName in M.Libraries:\r
546                 if LibraryName not in self.Libraries:\r
547                     EdkLogger.warn("build", "Library [%s] is not found" % LibraryName, File=str(M),\r
548                                     ExtraData="\t%s [%s]" % (str(Module), self._Arch))\r
549                     continue\r
550 \r
551                 Library = self.Libraries[LibraryName]\r
552                 if (LibraryName, Module.ModuleType) not in Module.LibraryClasses:\r
553                     Module.LibraryClasses[LibraryName, Module.ModuleType] = Library\r
554                     LibraryConsumerList.append(Library)\r
555                     EdkLogger.verbose("\t" + LibraryName + " : " + str(Library))\r
556 \r
557     def _UpdateModulePcd(self, Module, ModuleId):\r
558         for Name,Guid in Module.Pcds:\r
559             PcdInModule = Module.Pcds[Name,Guid]\r
560             if (Name,Guid) in self.Pcds:\r
561                 PcdInPlatform = self.Pcds[Name,Guid]\r
562                 # \r
563                 # in case there's PCDs coming from FDF file, which have no type given.\r
564                 # at this point, PcdInModule.Type has the type found from dependent\r
565                 # package\r
566                 # \r
567                 if PcdInPlatform.Type != None and PcdInPlatform.Type != '':\r
568                     PcdInModule.Type = PcdInPlatform.Type\r
569                 PcdInModule.MaxDatumSize = PcdInPlatform.MaxDatumSize\r
570                 PcdInModule.SkuInfoList = PcdInPlatform.SkuInfoList\r
571                 if PcdInPlatform.DefaultValue not in [None, '']:\r
572                     PcdInModule.DefaultValue = PcdInPlatform.DefaultValue\r
573                 if PcdInPlatform.TokenValue not in [None, '']:\r
574                     PcdInModule.TokenValue = PcdInPlatform.TokenValue\r
575                 if PcdInPlatform.MaxDatumSize not in [None, '']:\r
576                     PcdInModule.MaxDatumSize = PcdInPlatform.MaxDatumSize\r
577                 if PcdInPlatform.DatumType not in [None, '']:\r
578                     PcdInModule.DatumType = PcdInPlatform.DatumType\r
579 \r
580             if PcdInModule.DatumType == "VOID*" and PcdInModule.MaxDatumSize in ['', None]:\r
581                 EdkLogger.verbose("No MaxDatumSize specified for PCD %s.%s in module [%s]" % (Guid, Name, str(Module)))\r
582                 Value = PcdInModule.DefaultValue\r
583                 if Value[0] == 'L':\r
584                     PcdInModule.MaxDatumSize = str(len(Value) * 2)\r
585                 elif Value[0] == '{':\r
586                     PcdInModule.MaxDatumSize = str(len(Value.split(',')))\r
587                 else:\r
588                     PcdInModule.MaxDatumSize = str(len(Value))\r
589 \r
590             # apply default SKU for dynamic PCDS if specified one is not available\r
591             if (PcdInModule.Type in PCD_DYNAMIC_TYPE_LIST or PcdInModule.Type in PCD_DYNAMIC_EX_TYPE_LIST) \\r
592                 and PcdInModule.SkuInfoList in [None, {}, '']:\r
593                 if self.SkuName in self.SkuIds:\r
594                     SkuName = self.SkuName\r
595                 else:\r
596                     SkuName = 'DEFAULT'\r
597                 PcdInModule.SkuInfoList = {\r
598                     SkuName : SkuInfoClass(SkuName, self.SkuIds[SkuName], '', '', '', '', '', PcdInModule.DefaultValue)\r
599                 }\r
600 \r
601         RecordList = self._Table.Query(MODEL_PCD_FIXED_AT_BUILD, Scope1=self.Arch, BelongsToItem=ModuleId)\r
602         for TokenSpaceGuid, PcdCName, Setting, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
603             if (PcdCName, TokenSpaceGuid) not in Module.Pcds:\r
604                 continue\r
605             TokenList = GetSplitValueList(Setting)\r
606             Pcd = Module.Pcds[PcdCName, TokenSpaceGuid]\r
607             Pcd.DefaultValue = TokenList[0]\r
608             if len(TokenList) > 1:\r
609                 Pcd.MaxDatumSize = TokenList[1]\r
610             Pcd.Type = self._PCD_TYPE_STRING_[MODEL_PCD_FIXED_AT_BUILD]\r
611 \r
612         RecordList = self._Table.Query(MODEL_PCD_PATCHABLE_IN_MODULE, Scope1=self.Arch, BelongsToItem=ModuleId)\r
613         for TokenSpaceGuid, PcdCName, Setting, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
614             if (PcdCName, TokenSpaceGuid) not in Module.Pcds:\r
615                 continue\r
616             TokenList = GetSplitValueList(Setting)\r
617             Pcd = Module.Pcds[PcdCName, TokenSpaceGuid]\r
618             Pcd.DefaultValue = TokenList[0]\r
619             if len(TokenList) > 1:\r
620                 Pcd.MaxDatumSize = TokenList[1]\r
621             Pcd.Type = self._PCD_TYPE_STRING_[MODEL_PCD_PATCHABLE_IN_MODULE]\r
622 \r
623         RecordList = self._Table.Query(MODEL_PCD_FEATURE_FLAG, Scope1=self.Arch, BelongsToItem=ModuleId)\r
624         for TokenSpaceGuid, PcdCName, Setting, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
625             if (PcdCName, TokenSpaceGuid) not in Module.Pcds:\r
626                 continue\r
627             TokenList = GetSplitValueList(Setting)\r
628             Pcd = Module.Pcds[PcdCName, TokenSpaceGuid]\r
629             Pcd.DefaultValue = TokenList[0]\r
630             if len(TokenList) > 1:\r
631                 Pcd.MaxDatumSize = TokenList[1]\r
632             Pcd.Type = self._PCD_TYPE_STRING_[MODEL_PCD_FEATURE_FLAG]\r
633 \r
634         RecordList = self._Table.Query(MODEL_PCD_DYNAMIC, Scope1=self.Arch, BelongsToItem=ModuleId)\r
635         for TokenSpaceGuid, PcdCName, Setting, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
636             if (PcdCName, TokenSpaceGuid) not in Module.Pcds:\r
637                 continue\r
638             Pcd.DefaultValue = Setting\r
639             Pcd.Type = self._PCD_TYPE_STRING_[MODEL_PCD_DYNAMIC]\r
640 \r
641         RecordList = self._Table.Query(MODEL_PCD_DYNAMIC_EX, Scope1=self.Arch, BelongsToItem=ModuleId)\r
642         for TokenSpaceGuid, PcdCName, Setting, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
643             if (PcdCName, TokenSpaceGuid) not in Module.Pcds:\r
644                 continue\r
645             Pcd.DefaultValue = Setting\r
646             Pcd.Type = self._PCD_TYPE_STRING_[MODEL_PCD_DYNAMIC_EX]\r
647 \r
648     def _MergeModuleBuildOption(self, Module, ModuleId):\r
649         RecordList = self._Table.Query(MODEL_META_DATA_BUILD_OPTION, Scope1=self.Arch, BelongsToItem=ModuleId)\r
650         for ToolChainFamily, ToolChain, Option, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
651             if (ToolChainFamily, ToolChain) not in Module.BuildOptions:\r
652                 Module.BuildOptions[ToolChainFamily, ToolChain] = Option\r
653             else:\r
654                 OptionString = Module.BuildOptions[ToolChainFamily, ToolChain]\r
655                 Module.BuildOptions[ToolChainFamily, ToolChain] = OptionString + " " + Option\r
656 \r
657     def _GetLibraryInstances(self):\r
658         if self._LibraryInstances == None:\r
659             self._LibraryInstances = []\r
660             for Module in self.Modules:\r
661                 for Key in Module.LibraryClasses:\r
662                     Lib = Module.LibraryClasses[Key]\r
663                     if Lib in [None, ''] or Lib in self._LibraryInstances:\r
664                         continue\r
665                     self._LibraryInstances.append(Lib)\r
666         return self._LibraryInstances\r
667 \r
668     def _GetLibraryClasses(self):\r
669         if self._LibraryClasses == None:\r
670             LibraryClassDict = tdict(True, 3)\r
671             LibraryClassSet = set()\r
672             ModuleTypeSet = set()\r
673             RecordList = self._Table.Query(MODEL_EFI_LIBRARY_CLASS, Scope1=self.Arch)\r
674             for LibraryClass, LibraryInstance, Dummy, Arch, ModuleType, Dummy, LineNo in RecordList:\r
675                 LibraryClassSet.add(LibraryClass)\r
676                 ModuleTypeSet.add(ModuleType)\r
677                 LibraryInstance = NormPath(LibraryInstance, self._Macros)\r
678                 if not ValidFile(LibraryInstance):\r
679                     EdkLogger.error('build', FILE_NOT_FOUND, File=self.DescFilePath, \r
680                                     ExtraData=LibraryInstance, Line=LineNo)\r
681                 LibraryClassDict[Arch, ModuleType, LibraryClass] = LibraryInstance\r
682 \r
683             self._LibraryClasses = tdict(True)\r
684             for LibraryClass in LibraryClassSet:\r
685                 for ModuleType in ModuleTypeSet:\r
686                     LibraryInstance = LibraryClassDict[self.Arch, ModuleType, LibraryClass]\r
687                     if LibraryInstance == None:\r
688                         continue\r
689                     self._LibraryClasses[LibraryClass, ModuleType] = LibraryInstance\r
690         return self._LibraryClasses\r
691 \r
692     def _GetLibraries(self):\r
693         if self._Libraries == None:\r
694             self._Libraries = sdict()\r
695             RecordList = self._Table.Query(MODEL_EFI_LIBRARY_INSTANCE, Scope1=self.Arch)\r
696             for Record in RecordList:\r
697                 File = NormPath(Record[0], self._Macros)\r
698                 LineNo = Record[-1]\r
699                 if not ValidFile(File):\r
700                     EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
701                                     File=self.DescFilePath, Line=LineNo)\r
702                 Library = self._Db.BuildObject[File, MODEL_FILE_INF, self._Arch]\r
703                 self._Libraries[Library.BaseName] = Library\r
704         return self._Libraries\r
705 \r
706     def _GetPcds(self):\r
707         if self._Pcds == None:\r
708             self._Pcds = {}\r
709             self._Pcds.update(self._GetPcd(MODEL_PCD_FIXED_AT_BUILD))\r
710             self._Pcds.update(self._GetPcd(MODEL_PCD_PATCHABLE_IN_MODULE))\r
711             self._Pcds.update(self._GetPcd(MODEL_PCD_FEATURE_FLAG))\r
712             self._Pcds.update(self._GetDynamicPcd(MODEL_PCD_DYNAMIC_DEFAULT))\r
713             self._Pcds.update(self._GetDynamicHiiPcd(MODEL_PCD_DYNAMIC_HII))\r
714             self._Pcds.update(self._GetDynamicVpdPcd(MODEL_PCD_DYNAMIC_VPD))\r
715             self._Pcds.update(self._GetDynamicPcd(MODEL_PCD_DYNAMIC_EX_DEFAULT))\r
716             self._Pcds.update(self._GetDynamicHiiPcd(MODEL_PCD_DYNAMIC_EX_HII))\r
717             self._Pcds.update(self._GetDynamicVpdPcd(MODEL_PCD_DYNAMIC_EX_VPD))\r
718         return self._Pcds\r
719 \r
720     def _GetBuildOptions(self):\r
721         if self._BuildOptions == None:\r
722             self._BuildOptions = {}\r
723             RecordList = self._Table.Query(MODEL_META_DATA_BUILD_OPTION)\r
724             for ToolChainFamily, ToolChain, Option, Dummy1, Dummy2, Dummy3, Dummy4 in RecordList:\r
725                 self._BuildOptions[ToolChainFamily, ToolChain] = Option\r
726         return self._BuildOptions\r
727 \r
728     def _GetPcd(self, Type):\r
729         Pcds = {}\r
730         PcdDict = tdict(True, 3)\r
731         PcdSet = set()\r
732         # Find out all possible PCD candidates for self.Arch\r
733         RecordList = self._Table.Query(Type, Scope1=self.Arch)\r
734         for TokenSpaceGuid, PcdCName, Setting, Arch, SkuName, Dummy3, Dummy4 in RecordList:\r
735             PcdSet.add((PcdCName, TokenSpaceGuid))\r
736             PcdDict[Arch, PcdCName, TokenSpaceGuid] = Setting\r
737         # Remove redundant PCD candidates\r
738         for PcdCName, TokenSpaceGuid in PcdSet:\r
739             ValueList = ['', '', '']\r
740             Setting = PcdDict[self.Arch, PcdCName, TokenSpaceGuid]\r
741             if Setting == None:\r
742                 continue\r
743             TokenList = Setting.split(TAB_VALUE_SPLIT)\r
744             ValueList[0:len(TokenList)] = TokenList\r
745             PcdValue, DatumType, MaxDatumSize = ValueList\r
746             Pcds[PcdCName, TokenSpaceGuid] = PcdClassObject(\r
747                                                 PcdCName,\r
748                                                 TokenSpaceGuid,\r
749                                                 self._PCD_TYPE_STRING_[Type],\r
750                                                 DatumType,\r
751                                                 PcdValue,\r
752                                                 '',\r
753                                                 MaxDatumSize,\r
754                                                 {},\r
755                                                 True\r
756                                                 )\r
757         return Pcds\r
758 \r
759     def _GetDynamicPcd(self, Type):\r
760         Pcds = {}\r
761         PcdDict = tdict(True, 4)\r
762         PcdSet = set()\r
763         RecordList = self._Table.Query(Type, Scope1=self.Arch)\r
764         for TokenSpaceGuid, PcdCName, Setting, Arch, SkuName, Dummy3, Dummy4 in RecordList:\r
765             PcdSet.add((PcdCName, TokenSpaceGuid))\r
766             PcdDict[Arch, SkuName, PcdCName, TokenSpaceGuid] = Setting\r
767 \r
768         for PcdCName, TokenSpaceGuid in PcdSet:\r
769             ValueList = ['', '', '']\r
770             Setting = PcdDict[self.Arch, self.SkuName, PcdCName, TokenSpaceGuid]\r
771             if Setting == None:\r
772                 continue\r
773             TokenList = Setting.split(TAB_VALUE_SPLIT)\r
774             ValueList[0:len(TokenList)] = TokenList\r
775             PcdValue, DatumType, MaxDatumSize = ValueList\r
776 \r
777             SkuInfo = SkuInfoClass(self.SkuName, self.SkuIds[self.SkuName], '', '', '', '', '', PcdValue)\r
778             Pcds[PcdCName, TokenSpaceGuid] = PcdClassObject(\r
779                                                 PcdCName,\r
780                                                 TokenSpaceGuid,\r
781                                                 self._PCD_TYPE_STRING_[Type],\r
782                                                 DatumType,\r
783                                                 PcdValue,\r
784                                                 '',\r
785                                                 MaxDatumSize,\r
786                                                 {self.SkuName : SkuInfo},\r
787                                                 True\r
788                                                 )\r
789         return Pcds\r
790 \r
791     def _GetDynamicHiiPcd(self, Type):\r
792         Pcds = {}\r
793         PcdDict = tdict(True, 4)\r
794         PcdSet = set()\r
795         RecordList = self._Table.Query(Type, Scope1=self.Arch)\r
796         for TokenSpaceGuid, PcdCName, Setting, Arch, SkuName, Dummy3, Dummy4 in RecordList:\r
797             PcdSet.add((PcdCName, TokenSpaceGuid))\r
798             PcdDict[Arch, SkuName, PcdCName, TokenSpaceGuid] = Setting\r
799 \r
800         for PcdCName, TokenSpaceGuid in PcdSet:\r
801             ValueList = ['', '', '', '']\r
802             Setting = PcdDict[self.Arch, self.SkuName, PcdCName, TokenSpaceGuid]\r
803             if Setting == None:\r
804                 continue\r
805             TokenList = Setting.split(TAB_VALUE_SPLIT)\r
806             ValueList[0:len(TokenList)] = TokenList\r
807             VariableName, VariableGuid, VariableOffset, DefaultValue = ValueList\r
808             SkuInfo = SkuInfoClass(self.SkuName, self.SkuIds[self.SkuName], VariableName, VariableGuid, VariableOffset, DefaultValue)\r
809             Pcds[PcdCName, TokenSpaceGuid] = PcdClassObject(\r
810                                                 PcdCName,\r
811                                                 TokenSpaceGuid,\r
812                                                 self._PCD_TYPE_STRING_[Type],\r
813                                                 '',\r
814                                                 DefaultValue,\r
815                                                 '',\r
816                                                 '',\r
817                                                 {self.SkuName : SkuInfo},\r
818                                                 True\r
819                                                 )\r
820         return Pcds\r
821 \r
822     def _GetDynamicVpdPcd(self, Type):\r
823         Pcds = {}\r
824         PcdDict = tdict(True, 4)\r
825         PcdSet = set()\r
826         RecordList = self._Table.Query(Type, Scope1=self.Arch)\r
827         for TokenSpaceGuid, PcdCName, Setting, Arch, SkuName, Dummy3, Dummy4 in RecordList:\r
828             PcdSet.add((PcdCName, TokenSpaceGuid))\r
829             PcdDict[Arch, SkuName, PcdCName, TokenSpaceGuid] = Setting\r
830 \r
831         for PcdCName, TokenSpaceGuid in PcdSet:\r
832             ValueList = ['', '']\r
833             Setting = PcdDict[self.Arch, self.SkuName, PcdCName, TokenSpaceGuid]\r
834             if Setting == None:\r
835                 continue\r
836             TokenList = Setting.split(TAB_VALUE_SPLIT)\r
837             ValueList[0:len(TokenList)] = TokenList\r
838             VpdOffset, MaxDatumSize = ValueList\r
839 \r
840             SkuInfo = SkuInfoClass(self.SkuName, self.SkuIds[self.SkuName], '', '', '', '', VpdOffset)\r
841             Pcds[PcdCName, TokenSpaceGuid] = PcdClassObject(\r
842                                                 PcdCName,\r
843                                                 TokenSpaceGuid,\r
844                                                 self._PCD_TYPE_STRING_[Type],\r
845                                                 '',\r
846                                                 '',\r
847                                                 '',\r
848                                                 MaxDatumSize,\r
849                                                 {self.SkuName : SkuInfo},\r
850                                                 True\r
851                                                 )\r
852         return Pcds\r
853 \r
854     def AddModule(self, FilePath):\r
855         Module = self._Db.BuildObject[FilePath, MODEL_FILE_INF, self._Arch]\r
856         self._Modules.append(NormPath(Module))\r
857 \r
858     def AddPcd(self, Name, Guid, Value):\r
859         if (Name, Guid) not in self.Pcds:\r
860             self.Pcds[Name, Guid] = PcdClassObject(\r
861                                         Name,\r
862                                         Guid,\r
863                                         '',\r
864                                         '',\r
865                                         '',\r
866                                         '',\r
867                                         '',\r
868                                         {},\r
869                                         True\r
870                                         )\r
871         self.Pcds[Name, Guid].DefaultValue = Value\r
872 \r
873     Arch                = property(_GetArch, _SetArch)\r
874     Platform            = property(_GetPlatformName)\r
875     PlatformName        = property(_GetPlatformName)\r
876     Guid                = property(_GetFileGuid)\r
877     Version             = property(_GetVersion)\r
878     DscSpecification    = property(_GetDscSpec)\r
879     OutputDirectory     = property(_GetOutpuDir)\r
880     SupArchList         = property(_GetSupArch)\r
881     BuildTargets        = property(_GetBuildTarget)\r
882     SkuName             = property(_GetSkuName, _SetSkuName)\r
883     FlashDefinition     = property(_GetFdfFile)\r
884     BuildNumber         = property(_GetBuildNumber)\r
885     MakefileName        = property(_GetMakefileName)\r
886     BsBaseAddress       = property(_GetBsBaseAddress)\r
887     RtBaseAddress       = property(_GetRtBaseAddress)\r
888 \r
889     SkuIds              = property(_GetSkuIds)\r
890     Modules             = property(_GetModules)\r
891     LibraryInstances    = property(_GetLibraryInstances)\r
892     LibraryClasses      = property(_GetLibraryClasses)\r
893     Libraries           = property(_GetLibraries)\r
894     Pcds                = property(_GetPcds)\r
895     BuildOptions        = property(_GetBuildOptions)\r
896 \r
897 class DecBuildData(PackageBuildClassObject):\r
898     _PCD_TYPE_STRING_ = {\r
899         MODEL_PCD_FIXED_AT_BUILD        :   "FixedAtBuild",\r
900         MODEL_PCD_PATCHABLE_IN_MODULE   :   "PatchableInModule",\r
901         MODEL_PCD_FEATURE_FLAG          :   "FeatureFlag",\r
902         MODEL_PCD_DYNAMIC               :   "Dynamic",\r
903         MODEL_PCD_DYNAMIC_DEFAULT       :   "Dynamic",\r
904         MODEL_PCD_DYNAMIC_HII           :   "DynamicHii",\r
905         MODEL_PCD_DYNAMIC_VPD           :   "DynamicVpd",\r
906         MODEL_PCD_DYNAMIC_EX            :   "DynamicEx",\r
907         MODEL_PCD_DYNAMIC_EX_DEFAULT    :   "DynamicEx",\r
908         MODEL_PCD_DYNAMIC_EX_HII        :   "DynamicExHii",\r
909         MODEL_PCD_DYNAMIC_EX_VPD        :   "DynamicExVpd",\r
910     }\r
911 \r
912     def __init__(self, FilePath, Table, Db, Arch='COMMON', Macros={}):\r
913         self.DescFilePath = FilePath\r
914         self._PackageDir = os.path.dirname(FilePath)\r
915         self._Table = Table\r
916         self._Arch = Arch\r
917         self._Macros = Macros\r
918         self._Clear()\r
919 \r
920     def __repr__(self):\r
921         S = "[Package]\n"\r
922         S += "\tNAME = %s\n" % self.PackageName\r
923         S += "\tGUID = %s\n" % self.Guid\r
924         S += "\tVER  = %s\n" % self.Version\r
925 \r
926         S += '  <Protocol>\n'\r
927         for Name in self.Protocols:\r
928             S += "\t%s = %s\n" % (Name, self.Protocols[Name])\r
929 \r
930         S += '  <Ppi>\n'\r
931         for Name in self.Ppis:\r
932             S += "\t%s = %s\n" % (Name, self.Ppis[Name])\r
933 \r
934         S += '  <Guid>\n'\r
935         for Name in self.Guids:\r
936             S += "\t%s = %s\n" % (Name, self.Guids[Name])\r
937 \r
938         S += '  <Include>\n\t'\r
939         S += "\n\t".join(self.Includes) + '\n'\r
940 \r
941         S += '  <LibraryClass>\n'\r
942         for LibraryClass in self.LibraryClasses:\r
943             S += "\t%s = %s\n" % (LibraryClass, self.LibraryClasses[LibraryClass])\r
944 \r
945         S += '  <PCD>\n'\r
946         for Name,Guid,Type in self.Pcds:\r
947             S += "\t%s.%s-%s\n\t\t%s\n" % (Guid, Name, Type, str(self.Pcds[Name, Guid, Type]))\r
948         return S\r
949 \r
950     def _Clear(self):\r
951         self._PackageName       = None\r
952         self._Guid              = None\r
953         self._Version           = None\r
954         self._Protocols         = None\r
955         self._Ppis              = None\r
956         self._Guids             = None\r
957         self._Includes          = None\r
958         self._LibraryClasses    = None\r
959         self._Pcds              = None\r
960 \r
961     def _GetArch(self):\r
962         return self._Arch\r
963 \r
964     def _SetArch(self, Value):\r
965         if self._Arch == Value:\r
966             return\r
967         self._Arch = Value\r
968         self._Clear()\r
969 \r
970     def _GetPackageName(self):\r
971         if self._PackageName == None:\r
972             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DEC_DEFINES_PACKAGE_NAME)\r
973             if len(RecordList) == 0:\r
974                 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "No PACKAGE_NAME", File=self.DescFilePath)\r
975             self._PackageName = RecordList[0][0]\r
976         return self._PackageName\r
977 \r
978     def _GetFileGuid(self):\r
979         if self._Guid == None:\r
980             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DEC_DEFINES_PACKAGE_GUID)\r
981             if len(RecordList) == 0:\r
982                 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "No PACKAGE_GUID", File=self.DescFilePath)\r
983             self._Guid = RecordList[0][0]\r
984         return self._Guid\r
985 \r
986     def _GetVersion(self):\r
987         if self._Version == None:\r
988             RecordList = self._Table.Query(MODEL_META_DATA_HEADER, TAB_DEC_DEFINES_PACKAGE_VERSION)\r
989             if len(RecordList) == 0:\r
990                 self._Version = ''\r
991             self._Version = RecordList[0][0]\r
992         return self._Version\r
993 \r
994     def _GetProtocol(self):\r
995         if self._Protocols == None:\r
996             ProtocolDict = tdict(True)\r
997             NameList = []\r
998             RecordList = self._Table.Query(MODEL_EFI_PROTOCOL, Arch=self.Arch)\r
999             for Name, Guid, Dummy, Arch, ID, LineNo in RecordList:\r
1000                 if Name not in NameList:\r
1001                     NameList.append(Name)\r
1002                 ProtocolDict[Arch, Name] = Guid\r
1003             self._Protocols = sdict()\r
1004             for Name in NameList:\r
1005                 self._Protocols[Name] = ProtocolDict[self.Arch, Name]\r
1006         return self._Protocols\r
1007 \r
1008     def _GetPpi(self):\r
1009         if self._Ppis == None:\r
1010             PpiDict = tdict(True)\r
1011             NameList = []\r
1012             RecordList = self._Table.Query(MODEL_EFI_PPI, Arch=self.Arch)\r
1013             for Name, Guid, Dummy, Arch, ID, LineNo in RecordList:\r
1014                 if Name not in NameList:\r
1015                     NameList.append(Name)\r
1016                 PpiDict[Arch, Name] = Guid\r
1017             self._Ppis = sdict()\r
1018             for Name in NameList:\r
1019                 self._Ppis[Name] = PpiDict[self.Arch, Name]\r
1020         return self._Ppis\r
1021 \r
1022     def _GetGuid(self):\r
1023         if self._Guids == None:\r
1024             GuidDict = tdict(True)\r
1025             NameList = []\r
1026             RecordList = self._Table.Query(MODEL_EFI_GUID, Arch=self.Arch)\r
1027             for Name, Guid, Dummy, Arch, ID, LineNo in RecordList:\r
1028                 if Name not in NameList:\r
1029                     NameList.append(Name)\r
1030                 GuidDict[Arch, Name] = Guid\r
1031             self._Guids = sdict()\r
1032             for Name in NameList:\r
1033                 self._Guids[Name] = GuidDict[self.Arch, Name]\r
1034         return self._Guids\r
1035 \r
1036     def _GetInclude(self):\r
1037         if self._Includes == None:\r
1038             self._Includes = []\r
1039             RecordList = self._Table.Query(MODEL_EFI_INCLUDE, Arch=self.Arch)\r
1040             for Record in RecordList:\r
1041                 File = NormPath(Record[0], self._Macros)\r
1042                 LineNo = Record[-1]\r
1043                 if not ValidFile(File, self._PackageDir):\r
1044                     EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1045                                     File=self.DescFilePath, Line=LineNo)\r
1046                 if File not in self._Includes:\r
1047                     self._Includes.append(File)\r
1048         return self._Includes\r
1049 \r
1050     def _GetLibraryClass(self):\r
1051         if self._LibraryClasses == None:\r
1052             LibraryClassDict = tdict(True)\r
1053             LibraryClassSet = set()\r
1054             RecordList = self._Table.Query(MODEL_EFI_LIBRARY_CLASS, Arch=self.Arch)\r
1055             for LibraryClass, File, Dummy, Arch, ID, LineNo in RecordList:\r
1056                 File = NormPath(File, self._Macros)\r
1057                 if not ValidFile(File, self._PackageDir):\r
1058                     EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1059                                     File=self.DescFilePath, Line=LineNo)\r
1060                 LibraryClassSet.add(LibraryClass)\r
1061                 LibraryClassDict[Arch, LibraryClass] = File\r
1062             self._LibraryClasses = sdict()\r
1063             for LibraryClass in LibraryClassSet:\r
1064                 self._LibraryClasses[LibraryClass] = LibraryClassDict[self.Arch, LibraryClass]\r
1065         return self._LibraryClasses\r
1066 \r
1067     def _GetPcds(self):\r
1068         if self._Pcds == None:\r
1069             self._Pcds = {}\r
1070             self._Pcds.update(self._GetPcd(MODEL_PCD_FIXED_AT_BUILD))\r
1071             self._Pcds.update(self._GetPcd(MODEL_PCD_PATCHABLE_IN_MODULE))\r
1072             self._Pcds.update(self._GetPcd(MODEL_PCD_FEATURE_FLAG))\r
1073             self._Pcds.update(self._GetPcd(MODEL_PCD_DYNAMIC))\r
1074             self._Pcds.update(self._GetPcd(MODEL_PCD_DYNAMIC_EX))\r
1075         return self._Pcds\r
1076 \r
1077     def _GetPcd(self, Type):\r
1078         Pcds = {}\r
1079         PcdDict = tdict(True, 3)\r
1080         PcdSet = set()\r
1081         RecordList = self._Table.Query(Type, Arch=self._Arch)\r
1082         for TokenSpaceGuid, PcdCName, Setting, Arch, Dummy1, Dummy2 in RecordList:\r
1083             PcdDict[Arch, PcdCName, TokenSpaceGuid] = Setting\r
1084             PcdSet.add((PcdCName, TokenSpaceGuid))\r
1085 \r
1086         for PcdCName, TokenSpaceGuid in PcdSet:\r
1087             ValueList = ['', '', '']\r
1088             Setting = PcdDict[self.Arch, PcdCName, TokenSpaceGuid]\r
1089             if Setting == None:\r
1090                 continue\r
1091             TokenList = Setting.split(TAB_VALUE_SPLIT)\r
1092             ValueList[0:len(TokenList)] = TokenList\r
1093             DefaultValue, DatumType, TokenNumber = ValueList\r
1094             Pcds[PcdCName, TokenSpaceGuid, self._PCD_TYPE_STRING_[Type]] = PcdClassObject(\r
1095                                                                             PcdCName,\r
1096                                                                             TokenSpaceGuid,\r
1097                                                                             self._PCD_TYPE_STRING_[Type],\r
1098                                                                             DatumType,\r
1099                                                                             DefaultValue,\r
1100                                                                             TokenNumber,\r
1101                                                                             '',\r
1102                                                                             {},\r
1103                                                                             True\r
1104                                                                             )\r
1105         return Pcds\r
1106 \r
1107 \r
1108     Arch            = property(_GetArch, _SetArch)\r
1109     PackageName     = property(_GetPackageName)\r
1110     Guid            = property(_GetFileGuid)\r
1111     Version         = property(_GetVersion)\r
1112 \r
1113     Protocols       = property(_GetProtocol)\r
1114     Ppis            = property(_GetPpi)\r
1115     Guids           = property(_GetGuid)\r
1116     Includes        = property(_GetInclude)\r
1117     LibraryClasses  = property(_GetLibraryClass)\r
1118     Pcds            = property(_GetPcds)\r
1119 \r
1120 class InfBuildData(ModuleBuildClassObject):\r
1121     _PCD_TYPE_STRING_ = {\r
1122         MODEL_PCD_FIXED_AT_BUILD        :   "FixedAtBuild",\r
1123         MODEL_PCD_PATCHABLE_IN_MODULE   :   "PatchableInModule",\r
1124         MODEL_PCD_FEATURE_FLAG          :   "FeatureFlag",\r
1125         MODEL_PCD_DYNAMIC               :   "Dynamic",\r
1126         MODEL_PCD_DYNAMIC_DEFAULT       :   "Dynamic",\r
1127         MODEL_PCD_DYNAMIC_HII           :   "DynamicHii",\r
1128         MODEL_PCD_DYNAMIC_VPD           :   "DynamicVpd",\r
1129         MODEL_PCD_DYNAMIC_EX            :   "DynamicEx",\r
1130         MODEL_PCD_DYNAMIC_EX_DEFAULT    :   "DynamicEx",\r
1131         MODEL_PCD_DYNAMIC_EX_HII        :   "DynamicExHii",\r
1132         MODEL_PCD_DYNAMIC_EX_VPD        :   "DynamicExVpd",\r
1133     }\r
1134 \r
1135     _PROPERTY_ = {\r
1136         #\r
1137         # Required Fields\r
1138         #\r
1139         TAB_INF_DEFINES_BASE_NAME                   : "_BaseName",\r
1140         TAB_INF_DEFINES_FILE_GUID                   : "_Guid",\r
1141         TAB_INF_DEFINES_MODULE_TYPE                 : "_ModuleType",\r
1142         #\r
1143         # Optional Fields\r
1144         #\r
1145         TAB_INF_DEFINES_INF_VERSION                 : "_AutoGenVersion",\r
1146         TAB_INF_DEFINES_COMPONENT_TYPE              : "_ComponentType",\r
1147         TAB_INF_DEFINES_MAKEFILE_NAME               : "_MakefileName",\r
1148         #TAB_INF_DEFINES_CUSTOM_MAKEFILE             : "_CustomMakefile",\r
1149         TAB_INF_DEFINES_VERSION_NUMBER              : "_Version",\r
1150         TAB_INF_DEFINES_VERSION_STRING              : "_Version",\r
1151         TAB_INF_DEFINES_VERSION                     : "_Version",\r
1152         TAB_INF_DEFINES_PCD_IS_DRIVER               : "_PcdIsDriver",\r
1153         TAB_INF_DEFINES_SHADOW                      : "_Shadow",\r
1154     }\r
1155 \r
1156     _MODULE_TYPE_ = {\r
1157         "LIBRARY"               :   "BASE",\r
1158         "SECURITY_CORE"         :   "SEC",\r
1159         "PEI_CORE"              :   "PEI_CORE",\r
1160         "COMBINED_PEIM_DRIVER"  :   "PEIM",\r
1161         "PIC_PEIM"              :   "PEIM",\r
1162         "RELOCATABLE_PEIM"      :   "PEIM",\r
1163         "PE32_PEIM"             :   "PEIM",\r
1164         "BS_DRIVER"             :   "DXE_DRIVER",\r
1165         "RT_DRIVER"             :   "DXE_RUNTIME_DRIVER",\r
1166         "SAL_RT_DRIVER"         :   "DXE_SAL_DRIVER",\r
1167     #    "BS_DRIVER"             :   "DXE_SMM_DRIVER",\r
1168     #    "BS_DRIVER"             :   "UEFI_DRIVER",\r
1169         "APPLICATION"           :   "UEFI_APPLICATION",\r
1170         "LOGO"                  :   "BASE",\r
1171     }\r
1172     \r
1173     _NMAKE_FLAG_PATTERN_ = re.compile("(?:EBC_)?([A-Z]+)_(?:STD_|PROJ_|ARCH_)?FLAGS(?:_DLL|_ASL|_EXE)?", re.UNICODE)\r
1174     _TOOL_CODE_ = {\r
1175         "C"         :   "CC",\r
1176         "LIB"       :   "SLINK",\r
1177         "LINK"      :   "DLINK",\r
1178     }\r
1179     \r
1180 \r
1181     def __init__(self, FilePath, Table, Db, Arch='COMMON', Macros={}):\r
1182         self.DescFilePath = FilePath\r
1183         self._ModuleDir = os.path.dirname(FilePath)\r
1184         self._Table = Table\r
1185         self._Db = Db\r
1186         self._Arch = Arch\r
1187         self._Platform = 'COMMON'\r
1188         self._Macros = Macros\r
1189         self._Clear()\r
1190 \r
1191     def Print(self):\r
1192         S = '[%s.%s]\n' % (self.DescFilePath, self.Arch)\r
1193         S += '\tName = ' + self.BaseName + '\n'\r
1194         S += '\tGuid = ' + self.Guid + '\n'\r
1195         S += '\tVer  = ' + self.Version + '\n'\r
1196         S += '\tInfVersion = ' + self.AutoGenVersion + '\n'\r
1197         S += '\tModuleType = ' + self.ModuleType + '\n'\r
1198         S += '\tComponentType = ' + self.ComponentType + '\n'\r
1199         S += '\tPcdIsDriver = ' + str(self.PcdIsDriver) + '\n'\r
1200         S += '\tCustomMakefile = ' + self.CustomMakefile + '\n'\r
1201         S += '\tSpecification = ' + str(self.Specification) + '\n'\r
1202         S += '\tShadow = ' + str(self.Shadow) + '\n'\r
1203         S += '\tPcdIsDriver = ' + str(self.PcdIsDriver) + '\n'\r
1204         for Lib in self.LibraryClass:\r
1205             S += '\tLibraryClassDefinition = ' + str(Lib.LibraryClass) + ' SupModList = ' + str(Lib.SupModList) + '\n'\r
1206         S += '\tModuleEntryPointList = ' + str(self.ModuleEntryPointList) + '\n'\r
1207         S += '\tModuleUnloadImageList = ' + str(self.ModuleUnloadImageList) + '\n'\r
1208         S += '\tConstructorList = ' + str(self.ConstructorList) + '\n'\r
1209         S += '\tDestructorList = ' + str(self.DestructorList) + '\n'\r
1210 \r
1211         S += '  <Binaries>\n'\r
1212         for item in self.Binaries:\r
1213             S += "\t" + item.BinaryFile + item.FeatureFlag + item.SupArchList + '\n'\r
1214 \r
1215         S += '  <Sources>\n'\r
1216         for item in self.Sources:\r
1217             S += "\t" + item.SourceFile + '\n'\r
1218 \r
1219         S += '  <LibraryClasses>\n'\r
1220         S += '\t' + '\n\t'.join([Key for Key in self.LibraryClasses]) + '\n'\r
1221 \r
1222         S += '  <Protocols>\n'\r
1223         S += '\t' + '\n\t'.join(self.Protocols) + '\n'\r
1224 \r
1225         S += '  <Ppis>\n'\r
1226         S += '\t' + '\n\t'.join(self.Ppis) + '\n'\r
1227 \r
1228         S += '  <Guids>\n'\r
1229         S += '\t' + '\n\t'.join(self.Guids) + '\n'\r
1230 \r
1231         S += '  <Includes>\n'\r
1232         S += '\t' + '\n\t'.join(self.Includes) + '\n'\r
1233 \r
1234         S += '  <Packages>\n'\r
1235         S += '\t' + '\n\t'.join([str(P) for P in self.Packages]) + '\n'\r
1236 \r
1237         S += '  <Pcds>\n'\r
1238         for Name,Guid in self.Pcds.keys():\r
1239             S += "\t%s.%s\n\t\t%s\n" % (Guid, Name, str(self.Pcds[Name,Guid]))\r
1240 \r
1241         S += '  <BuildOptions\n'\r
1242         S += '\t' + '\n\t'.join(self.BuildOptions.values()) + '\n'\r
1243 \r
1244         S += '  <Depex>\n'\r
1245         S += '\t' + str(self.Depex) + '\n'\r
1246 \r
1247         S += '\n'\r
1248         return S\r
1249 \r
1250     ## XXX[key] = value\r
1251     def __setitem__(self, key, value):\r
1252         self.__dict__[self._PROPERTY_[key]] = value\r
1253     ## value = XXX[key]\r
1254     def __getitem__(self, key):\r
1255         return self.__dict__[self._PROPERTY_[key]]\r
1256     ## "in" test support\r
1257     def __contains__(self, key):\r
1258         return key in self._PROPERTY_\r
1259 \r
1260     def _Clear(self):\r
1261         self._Header_               = None\r
1262         self._AutoGenVersion        = None\r
1263         self._DescFilePath          = None\r
1264         self._BaseName              = None\r
1265         self._ModuleType            = None\r
1266         self._ComponentType         = None\r
1267         self._Guid                  = None\r
1268         self._Version               = None\r
1269         self._PcdIsDriver           = None\r
1270         self._BinaryModule          = None\r
1271         self._Shadow                = None\r
1272         self._MakefileName          = None\r
1273         self._CustomMakefile        = None\r
1274         self._Specification         = None\r
1275         self._LibraryClass          = None\r
1276         self._ModuleEntryPointList  = None\r
1277         self._ModuleUnloadImageList = None\r
1278         self._ConstructorList       = None\r
1279         self._DestructorList        = None\r
1280         self._Binaries              = None\r
1281         self._Sources               = None\r
1282         self._LibraryClasses        = None\r
1283         self._Libraries             = None\r
1284         self._Protocols             = None\r
1285         self._Ppis                  = None\r
1286         self._Guids                 = None\r
1287         self._Includes              = None\r
1288         self._Packages              = None\r
1289         self._Pcds                  = None\r
1290         self._BuildOptions          = None\r
1291         self._Depex                 = None\r
1292 \r
1293     def _GetArch(self):\r
1294         return self._Arch\r
1295 \r
1296     def _SetArch(self, Value):\r
1297         if self._Arch == Value:\r
1298             return\r
1299         self._Arch = Value\r
1300         self._Clear()\r
1301 \r
1302     def _GetPlatform(self):\r
1303         return self._Platform\r
1304 \r
1305     def _SetPlatform(self, Value):\r
1306         self._Platform = Value\r
1307 \r
1308     def _GetHeaderInfo(self):\r
1309         RecordList = self._Table.Query(MODEL_META_DATA_HEADER, Arch=self._Arch, Platform=self._Platform)\r
1310         for Record in RecordList:\r
1311             Name = Record[0]\r
1312             if Name in self:\r
1313                 self[Name] = Record[1]\r
1314             elif Name == 'EFI_SPECIFICATION_VERSION':\r
1315                 if self._Specification == None:\r
1316                     self._Specification = sdict()\r
1317                 self._Specification[Name] = Record[1]\r
1318             elif Name == 'EDK_RELEASE_VERSION':\r
1319                 if self._Specification == None:\r
1320                     self._Specification = sdict()\r
1321                 self._Specification[Name] = Record[1]\r
1322             elif Name == 'LIBRARY_CLASS':\r
1323                 if self._LibraryClass == None:\r
1324                     self._LibraryClass = []\r
1325                 ValueList = GetSplitValueList(Record[1])\r
1326                 LibraryClass = ValueList[0]\r
1327                 if len(ValueList) > 1:\r
1328                     SupModuleList = GetSplitValueList(ValueList[1], ' ')\r
1329                 else:\r
1330                     SupModuleList = SUP_MODULE_LIST\r
1331                 self._LibraryClass.append(LibraryClassObject(LibraryClass, SupModuleList))\r
1332             elif Name == 'ENTRY_POINT':\r
1333                 if self._ModuleEntryPointList == None:\r
1334                     self._ModuleEntryPointList = []\r
1335                 self._ModuleEntryPointList.append(Record[1])\r
1336             elif Name == 'UNLOAD_IMAGE':\r
1337                 if self._ModuleUnloadImageList == None:\r
1338                     self._ModuleUnloadImageList = []\r
1339                 if Record[1] == '':\r
1340                     continue\r
1341                 self._ModuleUnloadImageList.append(Record[1])\r
1342             elif Name == 'CONSTRUCTOR':\r
1343                 if self._ConstructorList == None:\r
1344                     self._ConstructorList = []\r
1345                 if Record[1] == '':\r
1346                     continue\r
1347                 self._ConstructorList.append(Record[1])\r
1348             elif Name == 'DESTRUCTOR':\r
1349                 if self._DestructorList == None:\r
1350                     self._DestructorList = []\r
1351                 if Record[1] == '':\r
1352                     continue\r
1353                 self._DestructorList.append(Record[1])\r
1354             elif Name == TAB_INF_DEFINES_CUSTOM_MAKEFILE:\r
1355                 TokenList = GetSplitValueList(Record[1])\r
1356                 self._CustomMakefile = {}\r
1357                 if len(TokenList) < 2:\r
1358                     self._CustomMakefile['MSFT'] = TokenList[0]\r
1359                     self._CustomMakefile['GCC'] = TokenList[0]\r
1360                 else:\r
1361                     if TokenList[0] not in ['MSFT', 'GCC']:\r
1362                         EdkLogger.error("build", FORMAT_NOT_SUPPORTED,\r
1363                                         "No supported family [%s]" % TokenList[0], \r
1364                                         File=self.DescFilePath, Line=Record[-1])\r
1365                     self._CustomMakefile[TokenList[0]] = TokenList[1]\r
1366         # \r
1367         # R8.x modules\r
1368         # \r
1369         if self._AutoGenVersion < 0x00010005:   # _AutoGenVersion may be None, which is less than anything\r
1370             if self._ComponentType in self._MODULE_TYPE_:\r
1371                 self._ModuleType = self._MODULE_TYPE_[self._ComponentType]\r
1372             if self._ComponentType == 'LIBRARY':\r
1373                 self._LibraryClass = [LibraryClassObject(self._BaseName, SUP_MODULE_LIST)]\r
1374             # make use some [nmake] section macros\r
1375             RecordList = self._Table.Query(MODEL_META_DATA_NMAKE, Arch=self._Arch, Platform=self._Platform)\r
1376             for Name,Value,Dummy,Arch,Platform,ID,LineNo in RecordList:\r
1377                 if Name == "IMAGE_ENTRY_POINT":\r
1378                     if self._ModuleEntryPointList == None:\r
1379                         self._ModuleEntryPointList = []\r
1380                     self._ModuleEntryPointList.append(Value)\r
1381                 elif Name == "DPX_SOURCE":\r
1382                     File = NormPath(Value, self._Macros)\r
1383                     if not ValidFile(File, self._ModuleDir):\r
1384                         EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1385                                         File=self.DescFilePath, Line=LineNo)\r
1386                     if self.Sources == None:\r
1387                         self._Sources = []\r
1388                     self._Sources.append(ModuleSourceFileClass(File, "", "", "", ""))\r
1389                 else:\r
1390                     ToolList = self._NMAKE_FLAG_PATTERN_.findall(Name)\r
1391                     if len(ToolList) == 0 or len(ToolList) != 1:\r
1392                         EdkLogger.warn("build", "Don't know how to do with macro [%s]" % Name, \r
1393                                        File=self.DescFilePath, Line=LineNo)\r
1394                     else:\r
1395                         if self._BuildOptions == None:\r
1396                             self._BuildOptions = sdict()\r
1397 \r
1398                         if ToolList[0] in self._TOOL_CODE_:\r
1399                             Tool = self._TOOL_CODE_[ToolList[0]]\r
1400                         else:\r
1401                             Tool = ToolList[0]\r
1402                         ToolChain = "*_*_*_%s_FLAGS" % Tool\r
1403                         ToolChainFamily = 'MSFT'    # R8.x only support MSFT tool chain\r
1404                         if (ToolChainFamily, ToolChain) not in self._BuildOptions:\r
1405                             self._BuildOptions[ToolChainFamily, ToolChain] = Value\r
1406                         else:\r
1407                             OptionString = self._BuildOptions[ToolChainFamily, ToolChain]\r
1408                             self._BuildOptions[ToolChainFamily, ToolChain] = OptionString + " " + Value\r
1409 \r
1410         self._Header_ = 'DUMMY'\r
1411 \r
1412     def _GetInfVersion(self):\r
1413         if self._AutoGenVersion == None:\r
1414             if self._Header_ == None:\r
1415                 self._GetHeaderInfo()\r
1416             if self._AutoGenVersion == None:\r
1417                 self._AutoGenVersion = 0x00010000\r
1418         return self._AutoGenVersion\r
1419 \r
1420     def _GetBaseName(self):\r
1421         if self._BaseName == None:\r
1422             if self._Header_ == None:\r
1423                 self._GetHeaderInfo()\r
1424             if self._BaseName == None:\r
1425                 EdkLogger.error('build', ATTRIBUTE_NOT_AVAILABLE, "No BASE_NAME name", File=self.DescFilePath)\r
1426         return self._BaseName\r
1427 \r
1428     def _GetModuleType(self):\r
1429         if self._ModuleType == None:\r
1430             if self._Header_ == None:\r
1431                 self._GetHeaderInfo()\r
1432             if self._ModuleType == None:\r
1433                 self._ModuleType = 'BASE'\r
1434         return self._ModuleType\r
1435 \r
1436     def _GetComponentType(self):\r
1437         if self._ComponentType == None:\r
1438             if self._Header_ == None:\r
1439                 self._GetHeaderInfo()\r
1440             if self._ComponentType == None:\r
1441                 self._ComponentType = ''\r
1442         return self._ComponentType\r
1443 \r
1444     def _GetFileGuid(self):\r
1445         if self._Guid == None:\r
1446             if self._Header_ == None:\r
1447                 self._GetHeaderInfo()\r
1448             if self._Guid == None:\r
1449                 self._Guid = '00000000-0000-0000-000000000000'\r
1450         return self._Guid\r
1451 \r
1452     def _GetVersion(self):\r
1453         if self._Version == None:\r
1454             if self._Header_ == None:\r
1455                 self._GetHeaderInfo()\r
1456             if self._Version == None:\r
1457                 self._Version = '0.0'\r
1458         return self._Version\r
1459 \r
1460     def _GetPcdIsDriver(self):\r
1461         if self._PcdIsDriver == None:\r
1462             if self._Header_ == None:\r
1463                 self._GetHeaderInfo()\r
1464             if self._PcdIsDriver == None:\r
1465                 self._PcdIsDriver = ''\r
1466         return self._PcdIsDriver\r
1467 \r
1468     def _GetShadow(self):\r
1469         if self._Shadow == None:\r
1470             if self._Header_ == None:\r
1471                 self._GetHeaderInfo()\r
1472             if self._Shadow != None and self._Shadow.upper() == 'TRUE':\r
1473                 self._Shadow = True\r
1474             else:\r
1475                 self._Shadow = False\r
1476         return self._Shadow\r
1477 \r
1478     def _GetMakefile(self):\r
1479         if self._CustomMakefile == None:\r
1480             if self._Header_ == None:\r
1481                 self._GetHeaderInfo()\r
1482             if self._CustomMakefile == None:\r
1483                 self._CustomMakefile = {}\r
1484         return self._CustomMakefile\r
1485 \r
1486     def _GetSpec(self):\r
1487         if self._Specification == None:\r
1488             if self._Header_ == None:\r
1489                 self._GetHeaderInfo()\r
1490             if self._Specification == None:\r
1491                 self._Specification = {}\r
1492         return self._Specification\r
1493 \r
1494     def _GetLibraryClass(self):\r
1495         if self._LibraryClass == None:\r
1496             if self._Header_ == None:\r
1497                 self._GetHeaderInfo()\r
1498             if self._LibraryClass == None:\r
1499                 self._LibraryClass = []\r
1500         return self._LibraryClass\r
1501 \r
1502     def _GetEntryPoint(self):\r
1503         if self._ModuleEntryPointList == None:\r
1504             if self._Header_ == None:\r
1505                 self._GetHeaderInfo()\r
1506             if self._ModuleEntryPointList == None:\r
1507                 self._ModuleEntryPointList = []\r
1508         return self._ModuleEntryPointList\r
1509 \r
1510     def _GetUnloadImage(self):\r
1511         if self._ModuleUnloadImageList == None:\r
1512             if self._Header_ == None:\r
1513                 self._GetHeaderInfo()\r
1514             if self._ModuleUnloadImageList == None:\r
1515                 self._ModuleUnloadImageList = []\r
1516         return self._ModuleUnloadImageList\r
1517 \r
1518     def _GetConstructor(self):\r
1519         if self._ConstructorList == None:\r
1520             if self._Header_ == None:\r
1521                 self._GetHeaderInfo()\r
1522             if self._ConstructorList == None:\r
1523                 self._ConstructorList = []\r
1524         return self._ConstructorList\r
1525 \r
1526     def _GetDestructor(self):\r
1527         if self._DestructorList == None:\r
1528             if self._Header_ == None:\r
1529                 self._GetHeaderInfo()\r
1530             if self._DestructorList == None:\r
1531                 self._DestructorList = []\r
1532         return self._DestructorList\r
1533                         \r
1534     def _GetBinaryFiles(self):\r
1535         if self._Binaries == None:\r
1536             self._Binaries = []\r
1537             RecordList = self._Table.Query(MODEL_EFI_BINARY_FILE, Arch=self._Arch, Platform=self._Platform)\r
1538             for Record in RecordList:\r
1539                 FileType = Record[0]\r
1540                 File = NormPath(Record[1], self._Macros)\r
1541                 LineNo = Record[-1]\r
1542                 if not ValidFile(File, self._ModuleDir):\r
1543                     EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1544                                     File=self.DescFilePath, Line=LineNo)\r
1545                 Target = Record[2]\r
1546                 FeatureFlag = Record[3]\r
1547                 self._Binaries.append(ModuleBinaryFileClass(File, FileType, Target, FeatureFlag, self._Arch))\r
1548         return self._Binaries\r
1549 \r
1550     def _GetSourceFiles(self):\r
1551         if self._Sources == None:\r
1552             self._Sources = []\r
1553             RecordList = self._Table.Query(MODEL_EFI_SOURCE_FILE, Arch=self._Arch, Platform=self._Platform)\r
1554             for Record in RecordList:\r
1555                 File = NormPath(Record[0], self._Macros)\r
1556                 LineNo = Record[-1]\r
1557                 if not ValidFile(File, self._ModuleDir):\r
1558                     EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1559                                     File=self.DescFilePath, Line=LineNo)\r
1560                 ToolChainFamily = Record[1]\r
1561                 TagName = Record[2]\r
1562                 ToolCode = Record[3]\r
1563                 FeatureFlag = Record[4]\r
1564                 self._Sources.append(ModuleSourceFileClass(File, TagName, ToolCode, ToolChainFamily, FeatureFlag))\r
1565         return self._Sources\r
1566 \r
1567     def _GetLibraryClassUses(self):\r
1568         if self._LibraryClasses == None:\r
1569             self._LibraryClasses = sdict()\r
1570             RecordList = self._Table.Query(MODEL_EFI_LIBRARY_CLASS, Arch=self._Arch, Platform=self._Platform)\r
1571             for Record in RecordList:\r
1572                 Lib = Record[0]\r
1573                 Instance = Record[1]\r
1574                 if Instance != None and Instance != '':\r
1575                     Instance = NormPath(Instance, self._Macros)\r
1576                 self._LibraryClasses[Lib] = Instance\r
1577         return self._LibraryClasses\r
1578 \r
1579     def _SetLibraryClassUses(self, Value):\r
1580         self._LibraryClasses = Value\r
1581 \r
1582     def _GetLibraryNames(self):\r
1583         if self._Libraries == None:\r
1584             self._Libraries = []\r
1585             RecordList = self._Table.Query(MODEL_EFI_LIBRARY_INSTANCE, Arch=self._Arch, Platform=self._Platform)\r
1586             for Record in RecordList:\r
1587                 # in case of name with '.lib' extension, which is unusual in R8.x inf\r
1588                 LibraryName = os.path.splitext(Record[0])[0]\r
1589                 if LibraryName not in self._Libraries:\r
1590                     self._Libraries.append(LibraryName)\r
1591         return self._Libraries\r
1592 \r
1593     def _GetProtocols(self):\r
1594         if self._Protocols == None:\r
1595             self._Protocols = sdict()\r
1596             RecordList = self._Table.Query(MODEL_EFI_PROTOCOL, Arch=self._Arch, Platform=self._Platform)\r
1597             for Record in RecordList:\r
1598                 CName = Record[0]\r
1599                 Value = GuidValue(CName, self.Packages)\r
1600                 if Value == None:\r
1601                     PackageList = '\t' + "\n\t".join([str(P) for P in self.Packages])\r
1602                     EdkLogger.error('build', RESOURCE_NOT_AVAILABLE, "Value of [%s] is not found in" % CName,\r
1603                                     ExtraData=PackageList, File=self.DescFilePath, Line=Record[-1])\r
1604                 self._Protocols[CName] = Value\r
1605         return self._Protocols\r
1606 \r
1607     def _GetPpis(self):\r
1608         if self._Ppis == None:\r
1609             self._Ppis = sdict()\r
1610             RecordList = self._Table.Query(MODEL_EFI_PPI, Arch=self._Arch, Platform=self._Platform)\r
1611             for Record in RecordList:\r
1612                 CName = Record[0]\r
1613                 Value = GuidValue(CName, self.Packages)\r
1614                 if Value == None:\r
1615                     PackageList = '\t' + "\n\t".join([str(P) for P in self.Packages])\r
1616                     EdkLogger.error('build', RESOURCE_NOT_AVAILABLE, "Value of [%s] is not found in " % CName,\r
1617                                     ExtraData=PackageList, File=self.DescFilePath, Line=Record[-1])\r
1618                 self._Ppis[CName] = Value\r
1619         return self._Ppis\r
1620 \r
1621     def _GetGuids(self):\r
1622         if self._Guids == None:\r
1623             self._Guids = sdict()\r
1624             RecordList = self._Table.Query(MODEL_EFI_GUID, Arch=self._Arch, Platform=self._Platform)\r
1625             for Record in RecordList:\r
1626                 CName = Record[0]\r
1627                 Value = GuidValue(CName, self.Packages)\r
1628                 if Value == None:\r
1629                     PackageList = '\t' + "\n\t".join([str(P) for P in self.Packages])\r
1630                     EdkLogger.error('build', RESOURCE_NOT_AVAILABLE, "Value of [%s] is not found in" % CName,\r
1631                                     ExtraData=PackageList, File=self.DescFilePath, Line=Record[-1])\r
1632                 self._Guids[CName] = Value\r
1633         return self._Guids\r
1634 \r
1635     def _GetIncludes(self):\r
1636         if self._Includes == None:\r
1637             self._Includes = []\r
1638             RecordList = self._Table.Query(MODEL_EFI_INCLUDE, Arch=self._Arch, Platform=self._Platform)\r
1639             # [includes] section must be used only in old (R8.x) inf file\r
1640             if self.AutoGenVersion >= 0x00010005 and len(RecordList) > 0:\r
1641                 EdkLogger.error('build', FORMAT_NOT_SUPPORTED, "No [include] section allowed",\r
1642                                 File=self.DescFilePath, Line=RecordList[0][-1]-1)\r
1643             for Record in RecordList:\r
1644                 File = NormPath(Record[0], self._Macros)\r
1645                 LineNo = Record[-1]\r
1646                 #if File[0] == '.':\r
1647                 #    if not ValidFile(File, self._ModuleDir):\r
1648                 #        EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1649                 #                        File=self.DescFilePath, Line=LineNo)\r
1650                 #else:\r
1651                 #    if not ValidFile(File):\r
1652                 #        EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1653                 #                        File=self.DescFilePath, Line=LineNo)\r
1654                 if File in self._Includes:\r
1655                     continue\r
1656                 self._Includes.append(File)\r
1657         return self._Includes\r
1658 \r
1659     def _GetPackages(self):\r
1660         if self._Packages == None:\r
1661             self._Packages = []\r
1662             RecordList = self._Table.Query(MODEL_META_DATA_PACKAGE, Arch=self._Arch, Platform=self._Platform)\r
1663             for Record in RecordList:\r
1664                 File = NormPath(Record[0], self._Macros)\r
1665                 LineNo = Record[-1]\r
1666                 if not ValidFile(File):\r
1667                     EdkLogger.error('build', FILE_NOT_FOUND, ExtraData=File,\r
1668                                     File=self.DescFilePath, Line=LineNo)\r
1669                 Package = self._Db.BuildObject[File, MODEL_FILE_DEC, self._Arch]\r
1670                 self._Packages.append(Package)\r
1671         return self._Packages\r
1672 \r
1673     def _GetPcds(self):\r
1674         if self._Pcds == None:\r
1675             self._Pcds = {}\r
1676             self._Pcds.update(self._GetPcd(MODEL_PCD_FIXED_AT_BUILD))\r
1677             self._Pcds.update(self._GetPcd(MODEL_PCD_PATCHABLE_IN_MODULE))\r
1678             self._Pcds.update(self._GetPcd(MODEL_PCD_FEATURE_FLAG))\r
1679             self._Pcds.update(self._GetPcd(MODEL_PCD_DYNAMIC))\r
1680             self._Pcds.update(self._GetPcd(MODEL_PCD_DYNAMIC_EX))\r
1681         return self._Pcds\r
1682 \r
1683     def _GetBuildOptions(self):\r
1684         if self._BuildOptions == None:\r
1685             self._BuildOptions = sdict()\r
1686             RecordList = self._Table.Query(MODEL_META_DATA_BUILD_OPTION, Arch=self._Arch, Platform=self._Platform)\r
1687             for Record in RecordList:\r
1688                 ToolChainFamily = Record[0]\r
1689                 ToolChain = Record[1]\r
1690                 Option = Record[2]\r
1691                 if (ToolChainFamily, ToolChain) not in self._BuildOptions:\r
1692                     self._BuildOptions[ToolChainFamily, ToolChain] = Option\r
1693                 else:\r
1694                     OptionString = self._BuildOptions[ToolChainFamily, ToolChain]\r
1695                     self._BuildOptions[ToolChainFamily, ToolChain] = OptionString + " " + Option\r
1696         return self._BuildOptions\r
1697 \r
1698     def _GetDepex(self):\r
1699         if self._Depex == None:\r
1700             self._Depex = ''\r
1701             RecordList = self._Table.Query(MODEL_EFI_DEPEX, Arch=self._Arch, Platform=self._Platform)\r
1702             for Record in RecordList:\r
1703                 self._Depex += ' ' + Record[0]\r
1704         return self._Depex\r
1705 \r
1706     def _GetPcd(self, Type):\r
1707         Pcds = {}\r
1708         PcdDict = tdict(True, 4)\r
1709         PcdSet = set()\r
1710         RecordList = self._Table.Query(Type, Arch=self.Arch, Platform=self.Platform)\r
1711         for TokenSpaceGuid, PcdCName, Setting, Arch, Platform, Dummy1, LineNo in RecordList:\r
1712             PcdDict[Arch, Platform, PcdCName, TokenSpaceGuid] = Setting\r
1713             PcdSet.add((PcdCName, TokenSpaceGuid))\r
1714             if TokenSpaceGuid not in self.Guids:\r
1715                 Value = GuidValue(TokenSpaceGuid, self.Packages)\r
1716                 if Value == None:\r
1717                     PackageList = '\t' + "\n\t".join([str(P) for P in self.Packages])\r
1718                     EdkLogger.error('build', RESOURCE_NOT_AVAILABLE, "Value of [%s] is not found in" % PcdCName,\r
1719                                     ExtraData=PackageList, File=self.DescFilePath, Line=LineNo)\r
1720                 self._Guids[TokenSpaceGuid] = Value\r
1721 \r
1722         for PcdCName, TokenSpaceGuid in PcdSet:\r
1723             ValueList = ['', '']\r
1724             Setting = PcdDict[self.Arch, self.Platform, PcdCName, TokenSpaceGuid]\r
1725             if Setting == None:\r
1726                 continue\r
1727             TokenList = Setting.split(TAB_VALUE_SPLIT)\r
1728             ValueList[0:len(TokenList)] = TokenList\r
1729             DefaultValue = ValueList[0]\r
1730             Pcd = PcdClassObject(\r
1731                     PcdCName,\r
1732                     TokenSpaceGuid,\r
1733                     '',\r
1734                     '',\r
1735                     DefaultValue,\r
1736                     '',\r
1737                     '',\r
1738                     {},\r
1739                     True\r
1740                     )\r
1741 \r
1742             # get necessary info from package declaring this PCD\r
1743             for Package in self.Packages:\r
1744                 # \r
1745                 # 'dynamic' in INF means its type is determined by platform;\r
1746                 # if platform doesn't give its type, use 'lowest' one in the \r
1747                 # following order, if any\r
1748                 # \r
1749                 #   "FixedAtBuild", "PatchableInModule", "FeatureFlag", "Dynamic", "DynamicEx"\r
1750                 # \r
1751                 PcdType = self._PCD_TYPE_STRING_[Type]\r
1752                 if Type in [MODEL_PCD_DYNAMIC, MODEL_PCD_DYNAMIC_EX]:\r
1753                     for T in ["FixedAtBuild", "PatchableInModule", "FeatureFlag", "Dynamic", "DynamicEx"]:\r
1754                         if (PcdCName, TokenSpaceGuid, T) in Package.Pcds:\r
1755                             PcdType = T\r
1756                             break\r
1757 \r
1758                 if (PcdCName, TokenSpaceGuid, PcdType) in Package.Pcds:\r
1759                     PcdInPackage = Package.Pcds[PcdCName, TokenSpaceGuid, PcdType]\r
1760                     Pcd.Type = PcdType\r
1761                     Pcd.TokenValue = PcdInPackage.TokenValue\r
1762                     Pcd.DatumType = PcdInPackage.DatumType\r
1763                     Pcd.MaxDatumSize = PcdInPackage.MaxDatumSize\r
1764                     if Pcd.DefaultValue in [None, '']:\r
1765                         Pcd.DefaultValue = PcdInPackage.DefaultValue\r
1766                     break\r
1767             else:\r
1768                 EdkLogger.error(\r
1769                             'build', \r
1770                             PARSER_ERROR,\r
1771                             "PCD [%s.%s] in [%s] is not found in dependent packages:" % (TokenSpaceGuid, PcdCName, self.DescFilePath),\r
1772                             ExtraData="\t%s" % '\n\t'.join([str(P) for P in self.Packages])\r
1773                             )\r
1774             Pcds[PcdCName, TokenSpaceGuid] = Pcd\r
1775         return Pcds\r
1776 \r
1777     Arch                    = property(_GetArch, _SetArch)\r
1778     Platform                = property(_GetPlatform, _SetPlatform)\r
1779 \r
1780     AutoGenVersion          = property(_GetInfVersion)\r
1781     BaseName                = property(_GetBaseName)\r
1782     ModuleType              = property(_GetModuleType)\r
1783     ComponentType           = property(_GetComponentType)\r
1784     Guid                    = property(_GetFileGuid)\r
1785     Version                 = property(_GetVersion)\r
1786     PcdIsDriver             = property(_GetPcdIsDriver)\r
1787     Shadow                  = property(_GetShadow)\r
1788     CustomMakefile          = property(_GetMakefile)\r
1789     Specification           = property(_GetSpec)\r
1790     LibraryClass            = property(_GetLibraryClass)\r
1791     ModuleEntryPointList    = property(_GetEntryPoint)\r
1792     ModuleUnloadImageList   = property(_GetUnloadImage)\r
1793     ConstructorList         = property(_GetConstructor)\r
1794     DestructorList          = property(_GetDestructor)\r
1795 \r
1796     Binaries                = property(_GetBinaryFiles)\r
1797     Sources                 = property(_GetSourceFiles)\r
1798     LibraryClasses          = property(_GetLibraryClassUses, _SetLibraryClassUses)\r
1799     Libraries               = property(_GetLibraryNames)\r
1800     Protocols               = property(_GetProtocols)\r
1801     Ppis                    = property(_GetPpis)\r
1802     Guids                   = property(_GetGuids)\r
1803     Includes                = property(_GetIncludes)\r
1804     Packages                = property(_GetPackages)\r
1805     Pcds                    = property(_GetPcds)\r
1806     BuildOptions            = property(_GetBuildOptions)\r
1807     Depex                   = property(_GetDepex)\r
1808 \r
1809 \r
1810 ## Database\r
1811 #\r
1812 # This class defined the build databse\r
1813 # During the phase of initialization, the database will create all tables and\r
1814 # insert all records of table DataModel\r
1815\r
1816 # @param object:      Inherited from object class\r
1817 # @param DbPath:      A string for the path of the ECC database\r
1818 #\r
1819 # @var Conn:          Connection of the ECC database\r
1820 # @var Cur:           Cursor of the connection\r
1821 # @var TblDataModel:  Local instance for TableDataModel\r
1822 #\r
1823 class WorkspaceDatabase(object):\r
1824     _FILE_TYPE_ = {\r
1825         ".INF"  : MODEL_FILE_INF,\r
1826         ".DEC"  : MODEL_FILE_DEC,\r
1827         ".DSC"  : MODEL_FILE_DSC,\r
1828         ".FDF"  : MODEL_FILE_FDF,\r
1829     }\r
1830 \r
1831     _FILE_PARSER_ = {\r
1832         MODEL_FILE_INF  :   InfParser,\r
1833         MODEL_FILE_DEC  :   DecParser,\r
1834         MODEL_FILE_DSC  :   DscParser,\r
1835         MODEL_FILE_FDF  :   None, #FdfParser,\r
1836     }\r
1837 \r
1838     _FILE_TABLE_ = {\r
1839         MODEL_FILE_INF  :   ModuleTable,\r
1840         MODEL_FILE_DEC  :   PackageTable,\r
1841         MODEL_FILE_DSC  :   PlatformTable,\r
1842     }\r
1843 \r
1844     class BuildObjectFactory(object):\r
1845         _GENERATOR_ = {\r
1846             MODEL_FILE_INF  :   InfBuildData,\r
1847             MODEL_FILE_DEC  :   DecBuildData,\r
1848             MODEL_FILE_DSC  :   DscBuildData,\r
1849             MODEL_FILE_FDF  :   None #FlashDefTable,\r
1850         }\r
1851 \r
1852         _CACHE_ = {}    # FilePath  : <object>\r
1853 \r
1854         def __init__(self, WorkspaceDb):\r
1855             self.WorkspaceDb = WorkspaceDb\r
1856 \r
1857         # key = (FilePath, FileType, Arch='COMMON', Platform='COMMON')\r
1858         def __getitem__(self, Key):\r
1859             FilePath = Key[0]\r
1860             FileType = Key[1]\r
1861             if len(Key) > 2:\r
1862                 Arch = Key[2]\r
1863             else:\r
1864                 Arch = 'COMMON'\r
1865             Key = (FilePath, Arch)\r
1866             if Key in self._CACHE_:\r
1867                 return self._CACHE_[Key]\r
1868 \r
1869             self.WorkspaceDb[FilePath] = FileType\r
1870             if FileType not in self._GENERATOR_:\r
1871                 return None\r
1872 \r
1873             # create temp table for current file\r
1874             Table = self.WorkspaceDb[FilePath]\r
1875             BuildObject = self._GENERATOR_[FileType](\r
1876                                     FilePath, \r
1877                                     Table, \r
1878                                     self.WorkspaceDb, \r
1879                                     Arch, \r
1880                                     self.WorkspaceDb._GlobalMacros\r
1881                                     )\r
1882             self._CACHE_[Key] = BuildObject\r
1883             return BuildObject\r
1884 \r
1885     class TransformObjectFactory:\r
1886         def __init__(self, WorkspaceDb):\r
1887             self.WorkspaceDb = WorkspaceDb\r
1888 \r
1889         # key = FilePath\r
1890         def __getitem__(self, Key):\r
1891             pass\r
1892 \r
1893     def __init__(self, DbPath, GlobalMacros={}, RenewDb=False):\r
1894         self._GlobalMacros = GlobalMacros\r
1895 \r
1896         DbDir = os.path.split(DbPath)[0]\r
1897         if not os.path.exists(DbDir):\r
1898             os.makedirs(DbDir)\r
1899 \r
1900         if RenewDb and os.path.exists(DbPath):\r
1901             os.remove(DbPath)\r
1902 \r
1903         self.Conn = sqlite3.connect(DbPath, isolation_level='DEFERRED')\r
1904         self.Conn.execute("PRAGMA synchronous=OFF")\r
1905         self.Conn.execute("PRAGMA temp_store=MEMORY")\r
1906         self.Conn.execute("PRAGMA count_changes=OFF")\r
1907         self.Conn.execute("PRAGMA cache_size=8192")\r
1908         #self.Conn.execute("PRAGMA page_size=8192")\r
1909 \r
1910         # to avoid non-ascii character conversion issue\r
1911         self.Conn.text_factory = str\r
1912         self.Cur = self.Conn.cursor()\r
1913 \r
1914         self.TblDataModel = TableDataModel(self.Cur)\r
1915         self.TblFile = TableFile(self.Cur)\r
1916 \r
1917         self.BuildObject = WorkspaceDatabase.BuildObjectFactory(self)\r
1918         self.TransformObject = WorkspaceDatabase.TransformObjectFactory(self)\r
1919     \r
1920     ## Initialize build database\r
1921     #\r
1922     # 1. Delete all old existing tables\r
1923     # 2. Create new tables\r
1924     # 3. Initialize table DataModel\r
1925     #\r
1926     def InitDatabase(self):\r
1927         EdkLogger.verbose("\nInitialize ECC database started ...")\r
1928         \r
1929         #\r
1930         # Create new tables\r
1931         #\r
1932         self.TblDataModel.Create(False)\r
1933         self.TblFile.Create(False)\r
1934         \r
1935         #\r
1936         # Initialize table DataModel\r
1937         #\r
1938         self.TblDataModel.InitTable()\r
1939         EdkLogger.verbose("Initialize build database ... DONE!")\r
1940 \r
1941     ## Query a table\r
1942     #\r
1943     # @param Table:  The instance of the table to be queried\r
1944     #\r
1945     def QueryTable(self, Table):\r
1946         Table.Query()\r
1947     \r
1948     ## Close entire database\r
1949     #\r
1950     # Commit all first \r
1951     # Close the connection and cursor\r
1952     #\r
1953     def Close(self):\r
1954         self.Conn.commit()\r
1955         self.Cur.close()\r
1956         self.Conn.close()\r
1957 \r
1958     def GetFileId(self, FilePath):\r
1959         return self.TblFile.GetFileId(FilePath)\r
1960 \r
1961     def GetFileType(self, FileId):\r
1962         return self.TblFile.GetFileType(FileId)\r
1963 \r
1964     def GetTimeStamp(self, FileId):\r
1965         return self.TblFile.GetFileTimeStamp(FileId)\r
1966 \r
1967     def SetTimeStamp(self, FileId, TimeStamp):\r
1968         return self.TblFile.SetFileTimeStamp(FileId, TimeStamp)\r
1969 \r
1970     def CheckIntegrity(self, TableName):\r
1971         Result = self.Cur.execute("select min(ID) from %s" % (TableName)).fetchall()\r
1972         if Result[0][0] != -1:\r
1973             return False\r
1974         return True\r
1975 \r
1976     def GetTableName(self, FileType, FileId):\r
1977         return "_%s_%s" % (FileType, FileId)\r
1978 \r
1979     ## TRICK: \r
1980     # Key = FilePath\r
1981     # Value = FileType\r
1982     def __setitem__(self, FilePath, FileType):\r
1983         FileId = self.GetFileId(FilePath)\r
1984         if FileId != None:\r
1985             TimeStamp = os.stat(FilePath)[8]\r
1986             TableName = self.GetTableName(FileType, FileId)\r
1987             if TimeStamp != self.GetTimeStamp(FileId):\r
1988                 self.SetTimeStamp(FileId, TimeStamp)\r
1989             else:\r
1990                 if self.CheckIntegrity(TableName) == True:\r
1991                     return\r
1992         else:\r
1993             FileId = self.TblFile.InsertFile(FilePath, FileType)\r
1994             TableName = self.GetTableName(FileType, FileId)\r
1995 \r
1996         FileTable = self._FILE_TABLE_[FileType](self.Cur, TableName, FileId)\r
1997         FileTable.Create()\r
1998         Parser = self._FILE_PARSER_[FileType](FilePath, FileId, FileType, FileTable)\r
1999         Parser.Start()\r
2000 \r
2001     ## Return a temp table containing all content of the given file\r
2002     # \r
2003     def __getitem__(self, FileInfo):\r
2004         if type(FileInfo) == type(''):\r
2005             FileId = self.GetFileId(FileInfo)\r
2006         else:\r
2007             FileId = FileInfo\r
2008         if FileId == None:\r
2009             return None\r
2010         FileType = self.GetFileType(FileId)\r
2011         if FileType not in self._FILE_TABLE_:\r
2012             return None\r
2013 \r
2014         TableName = "_%s_%s" % (FileType, FileId)\r
2015         FileTable = self._FILE_TABLE_[FileType](self.Cur, TableName, FileId)\r
2016         return FileTable\r
2017 \r
2018     ## Resolve cross-references between platfor, packages and modules\r
2019     def _PostFix(self):\r
2020         pass\r
2021 \r
2022 ##\r
2023 #\r
2024 # This acts like the main() function for the script, unless it is 'import'ed into another\r
2025 # script.\r
2026 #\r
2027 if __name__ == '__main__':\r
2028     EdkLogger.Initialize()\r
2029     EdkLogger.SetLevel(EdkLogger.DEBUG_0)\r
2030     \r
2031     Db = WorkspaceDatabase(DATABASE_PATH)\r
2032     Db.InitDatabase()\r
2033     Db.QueryTable(Db.TblDataModel)   \r
2034     Db.QueryTable(Db.TblFile)\r
2035     Db.QueryTable(Db.TblDsc)\r
2036     Db.Close()\r
2037