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