a4e36d1a2fb4f3a47157f932157d7a7e8a86f222
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / AutoGen.py
1 ## @file
2 # Generate AutoGen.h, AutoGen.c and *.depex files
3 #
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
9 #
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 #
13
14 ## Import Modules
15 #
16 import Common.LongFilePathOs as os
17 import re
18 import os.path as path
19 import copy
20 import uuid
21
22 import GenC
23 import GenMake
24 import GenDepex
25 from StringIO import StringIO
26
27 from StrGather import *
28 from BuildEngine import BuildRule
29
30 from Common.LongFilePathSupport import CopyLongFilePath
31 from Common.BuildToolError import *
32 from Common.DataType import *
33 from Common.Misc import *
34 from Common.String import *
35 import Common.GlobalData as GlobalData
36 from GenFds.FdfParser import *
37 from CommonDataClass.CommonClass import SkuInfoClass
38 from Workspace.BuildClassObject import *
39 from GenPatchPcdTable.GenPatchPcdTable import parsePcdInfoFromMapFile
40 import Common.VpdInfoFile as VpdInfoFile
41 from GenPcdDb import CreatePcdDatabaseCode
42 from Workspace.MetaFileCommentParser import UsageList
43 from Common.MultipleWorkspace import MultipleWorkspace as mws
44 import InfSectionParser
45 import datetime
46 import hashlib
47 from GenVar import VariableMgr,var_info
48 from collections import OrderedDict
49 from collections import defaultdict
50
51 ## Regular expression for splitting Dependency Expression string into tokens
52 gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)")
53
54 ## Regular expression for match: PCD(xxxx.yyy)
55 gPCDAsGuidPattern = re.compile(r"^PCD\(.+\..+\)$")
56
57 #
58 # Regular expression for finding Include Directories, the difference between MSFT and INTEL/GCC/RVCT
59 # is the former use /I , the Latter used -I to specify include directories
60 #
61 gBuildOptIncludePatternMsft = re.compile(r"(?:.*?)/I[ \t]*([^ ]*)", re.MULTILINE | re.DOTALL)
62 gBuildOptIncludePatternOther = re.compile(r"(?:.*?)-I[ \t]*([^ ]*)", re.MULTILINE | re.DOTALL)
63
64 #
65 # Match name = variable
66 #
67 gEfiVarStoreNamePattern = re.compile("\s*name\s*=\s*(\w+)")
68 #
69 # The format of guid in efivarstore statement likes following and must be correct:
70 # guid = {0xA04A27f4, 0xDF00, 0x4D42, {0xB5, 0x52, 0x39, 0x51, 0x13, 0x02, 0x11, 0x3D}}
71 #
72 gEfiVarStoreGuidPattern = re.compile("\s*guid\s*=\s*({.*?{.*?}\s*})")
73
74 ## Mapping Makefile type
75 gMakeTypeMap = {"MSFT":"nmake", "GCC":"gmake"}
76
77
78 ## Build rule configuration file
79 gDefaultBuildRuleFile = 'build_rule.txt'
80
81 ## Tools definition configuration file
82 gDefaultToolsDefFile = 'tools_def.txt'
83
84 ## Build rule default version
85 AutoGenReqBuildRuleVerNum = "0.1"
86
87 ## default file name for AutoGen
88 gAutoGenCodeFileName = "AutoGen.c"
89 gAutoGenHeaderFileName = "AutoGen.h"
90 gAutoGenStringFileName = "%(module_name)sStrDefs.h"
91 gAutoGenStringFormFileName = "%(module_name)sStrDefs.hpk"
92 gAutoGenDepexFileName = "%(module_name)s.depex"
93 gAutoGenImageDefFileName = "%(module_name)sImgDefs.h"
94 gAutoGenIdfFileName = "%(module_name)sIdf.hpk"
95 gInfSpecVersion = "0x00010017"
96
97 #
98 # Template string to generic AsBuilt INF
99 #
100 gAsBuiltInfHeaderString = TemplateString("""${header_comments}
101
102 # DO NOT EDIT
103 # FILE auto-generated
104
105 [Defines]
106 INF_VERSION = ${module_inf_version}
107 BASE_NAME = ${module_name}
108 FILE_GUID = ${module_guid}
109 MODULE_TYPE = ${module_module_type}${BEGIN}
110 VERSION_STRING = ${module_version_string}${END}${BEGIN}
111 PCD_IS_DRIVER = ${pcd_is_driver_string}${END}${BEGIN}
112 UEFI_SPECIFICATION_VERSION = ${module_uefi_specification_version}${END}${BEGIN}
113 PI_SPECIFICATION_VERSION = ${module_pi_specification_version}${END}${BEGIN}
114 ENTRY_POINT = ${module_entry_point}${END}${BEGIN}
115 UNLOAD_IMAGE = ${module_unload_image}${END}${BEGIN}
116 CONSTRUCTOR = ${module_constructor}${END}${BEGIN}
117 DESTRUCTOR = ${module_destructor}${END}${BEGIN}
118 SHADOW = ${module_shadow}${END}${BEGIN}
119 PCI_VENDOR_ID = ${module_pci_vendor_id}${END}${BEGIN}
120 PCI_DEVICE_ID = ${module_pci_device_id}${END}${BEGIN}
121 PCI_CLASS_CODE = ${module_pci_class_code}${END}${BEGIN}
122 PCI_REVISION = ${module_pci_revision}${END}${BEGIN}
123 BUILD_NUMBER = ${module_build_number}${END}${BEGIN}
124 SPEC = ${module_spec}${END}${BEGIN}
125 UEFI_HII_RESOURCE_SECTION = ${module_uefi_hii_resource_section}${END}${BEGIN}
126 MODULE_UNI_FILE = ${module_uni_file}${END}
127
128 [Packages.${module_arch}]${BEGIN}
129 ${package_item}${END}
130
131 [Binaries.${module_arch}]${BEGIN}
132 ${binary_item}${END}
133
134 [PatchPcd.${module_arch}]${BEGIN}
135 ${patchablepcd_item}
136 ${END}
137
138 [Protocols.${module_arch}]${BEGIN}
139 ${protocol_item}
140 ${END}
141
142 [Ppis.${module_arch}]${BEGIN}
143 ${ppi_item}
144 ${END}
145
146 [Guids.${module_arch}]${BEGIN}
147 ${guid_item}
148 ${END}
149
150 [PcdEx.${module_arch}]${BEGIN}
151 ${pcd_item}
152 ${END}
153
154 [LibraryClasses.${module_arch}]
155 ## @LIB_INSTANCES${BEGIN}
156 # ${libraryclasses_item}${END}
157
158 ${depexsection_item}
159
160 ${userextension_tianocore_item}
161
162 ${tail_comments}
163
164 [BuildOptions.${module_arch}]
165 ## @AsBuilt${BEGIN}
166 ## ${flags_item}${END}
167 """)
168
169 ## Base class for AutoGen
170 #
171 # This class just implements the cache mechanism of AutoGen objects.
172 #
173 class AutoGen(object):
174 # database to maintain the objects in each child class
175 __ObjectCache = {} # (BuildTarget, ToolChain, ARCH, platform file): AutoGen object
176
177 ## Factory method
178 #
179 # @param Class class object of real AutoGen class
180 # (WorkspaceAutoGen, ModuleAutoGen or PlatformAutoGen)
181 # @param Workspace Workspace directory or WorkspaceAutoGen object
182 # @param MetaFile The path of meta file
183 # @param Target Build target
184 # @param Toolchain Tool chain name
185 # @param Arch Target arch
186 # @param *args The specific class related parameters
187 # @param **kwargs The specific class related dict parameters
188 #
189 def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
190 # check if the object has been created
191 Key = (Target, Toolchain, Arch, MetaFile)
192 try:
193 # if it exists, just return it directly
194 return cls.__ObjectCache[Key]
195 except:
196 # it didnt exist. create it, cache it, then return it
197 cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls)
198 return cls.__ObjectCache[Key]
199
200 def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
201 super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
202
203 ## hash() operator
204 #
205 # The file path of platform file will be used to represent hash value of this object
206 #
207 # @retval int Hash value of the file path of platform file
208 #
209 def __hash__(self):
210 return hash(self.MetaFile)
211
212 ## str() operator
213 #
214 # The file path of platform file will be used to represent this object
215 #
216 # @retval string String of platform file path
217 #
218 def __str__(self):
219 return str(self.MetaFile)
220
221 ## "==" operator
222 def __eq__(self, Other):
223 return Other and self.MetaFile == Other
224
225 ## Workspace AutoGen class
226 #
227 # This class is used mainly to control the whole platform build for different
228 # architecture. This class will generate top level makefile.
229 #
230 class WorkspaceAutoGen(AutoGen):
231 # call super().__init__ then call the worker function with different parameter count
232 def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
233 try:
234 self._Init
235 except:
236 super(WorkspaceAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
237 self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
238 self._Init = True
239
240 ## Initialize WorkspaceAutoGen
241 #
242 # @param WorkspaceDir Root directory of workspace
243 # @param ActivePlatform Meta-file of active platform
244 # @param Target Build target
245 # @param Toolchain Tool chain name
246 # @param ArchList List of architecture of current build
247 # @param MetaFileDb Database containing meta-files
248 # @param BuildConfig Configuration of build
249 # @param ToolDefinition Tool chain definitions
250 # @param FlashDefinitionFile File of flash definition
251 # @param Fds FD list to be generated
252 # @param Fvs FV list to be generated
253 # @param Caps Capsule list to be generated
254 # @param SkuId SKU id from command line
255 #
256 def _InitWorker(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchList, MetaFileDb,
257 BuildConfig, ToolDefinition, FlashDefinitionFile='', Fds=None, Fvs=None, Caps=None, SkuId='', UniFlag=None,
258 Progress=None, BuildModule=None):
259 self.BuildDatabase = MetaFileDb
260 self.MetaFile = ActivePlatform
261 self.WorkspaceDir = WorkspaceDir
262 self.Platform = self.BuildDatabase[self.MetaFile, 'COMMON', Target, Toolchain]
263 GlobalData.gActivePlatform = self.Platform
264 self.BuildTarget = Target
265 self.ToolChain = Toolchain
266 self.ArchList = ArchList
267 self.SkuId = SkuId
268 self.UniFlag = UniFlag
269
270 self.TargetTxt = BuildConfig
271 self.ToolDef = ToolDefinition
272 self.FdfFile = FlashDefinitionFile
273 self.FdTargetList = Fds if Fds else []
274 self.FvTargetList = Fvs if Fvs else []
275 self.CapTargetList = Caps if Caps else []
276 self.AutoGenObjectList = []
277 self._BuildDir = None
278 self._FvDir = None
279 self._MakeFileDir = None
280 self._BuildCommand = None
281 self._GuidDict = {}
282
283 # there's many relative directory operations, so ...
284 os.chdir(self.WorkspaceDir)
285
286 #
287 # Merge Arch
288 #
289 if not self.ArchList:
290 ArchList = set(self.Platform.SupArchList)
291 else:
292 ArchList = set(self.ArchList) & set(self.Platform.SupArchList)
293 if not ArchList:
294 EdkLogger.error("build", PARAMETER_INVALID,
295 ExtraData = "Invalid ARCH specified. [Valid ARCH: %s]" % (" ".join(self.Platform.SupArchList)))
296 elif self.ArchList and len(ArchList) != len(self.ArchList):
297 SkippedArchList = set(self.ArchList).symmetric_difference(set(self.Platform.SupArchList))
298 EdkLogger.verbose("\nArch [%s] is ignored because the platform supports [%s] only!"
299 % (" ".join(SkippedArchList), " ".join(self.Platform.SupArchList)))
300 self.ArchList = tuple(ArchList)
301
302 # Validate build target
303 if self.BuildTarget not in self.Platform.BuildTargets:
304 EdkLogger.error("build", PARAMETER_INVALID,
305 ExtraData="Build target [%s] is not supported by the platform. [Valid target: %s]"
306 % (self.BuildTarget, " ".join(self.Platform.BuildTargets)))
307
308
309 # parse FDF file to get PCDs in it, if any
310 if not self.FdfFile:
311 self.FdfFile = self.Platform.FlashDefinition
312
313 EdkLogger.info("")
314 if self.ArchList:
315 EdkLogger.info('%-16s = %s' % ("Architecture(s)", ' '.join(self.ArchList)))
316 EdkLogger.info('%-16s = %s' % ("Build target", self.BuildTarget))
317 EdkLogger.info('%-16s = %s' % ("Toolchain", self.ToolChain))
318
319 EdkLogger.info('\n%-24s = %s' % ("Active Platform", self.Platform))
320 if BuildModule:
321 EdkLogger.info('%-24s = %s' % ("Active Module", BuildModule))
322
323 if self.FdfFile:
324 EdkLogger.info('%-24s = %s' % ("Flash Image Definition", self.FdfFile))
325
326 EdkLogger.verbose("\nFLASH_DEFINITION = %s" % self.FdfFile)
327
328 if Progress:
329 Progress.Start("\nProcessing meta-data")
330
331 if self.FdfFile:
332 #
333 # Mark now build in AutoGen Phase
334 #
335 GlobalData.gAutoGenPhase = True
336 Fdf = FdfParser(self.FdfFile.Path)
337 Fdf.ParseFile()
338 GlobalData.gFdfParser = Fdf
339 GlobalData.gAutoGenPhase = False
340 PcdSet = Fdf.Profile.PcdDict
341 if Fdf.CurrentFdName and Fdf.CurrentFdName in Fdf.Profile.FdDict:
342 FdDict = Fdf.Profile.FdDict[Fdf.CurrentFdName]
343 for FdRegion in FdDict.RegionList:
344 if str(FdRegion.RegionType) is 'FILE' and self.Platform.VpdToolGuid in str(FdRegion.RegionDataList):
345 if int(FdRegion.Offset) % 8 != 0:
346 EdkLogger.error("build", FORMAT_INVALID, 'The VPD Base Address %s must be 8-byte aligned.' % (FdRegion.Offset))
347 ModuleList = Fdf.Profile.InfList
348 self.FdfProfile = Fdf.Profile
349 for fvname in self.FvTargetList:
350 if fvname.upper() not in self.FdfProfile.FvDict:
351 EdkLogger.error("build", OPTION_VALUE_INVALID,
352 "No such an FV in FDF file: %s" % fvname)
353
354 # In DSC file may use FILE_GUID to override the module, then in the Platform.Modules use FILE_GUIDmodule.inf as key,
355 # but the path (self.MetaFile.Path) is the real path
356 for key in self.FdfProfile.InfDict:
357 if key == 'ArchTBD':
358 MetaFile_cache = {}
359 for Arch in self.ArchList:
360 Current_Platform_cache = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
361 MetaFile_cache[Arch] = set()
362 for Pkey in Current_Platform_cache.Modules:
363 MetaFile_cache[Arch].add(Current_Platform_cache.Modules[Pkey].MetaFile)
364 for Inf in self.FdfProfile.InfDict[key]:
365 ModuleFile = PathClass(NormPath(Inf), GlobalData.gWorkspace, Arch)
366 for Arch in self.ArchList:
367 if ModuleFile in MetaFile_cache[Arch]:
368 break
369 else:
370 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]
371 if not ModuleData.IsBinaryModule:
372 EdkLogger.error('build', PARSER_ERROR, "Module %s NOT found in DSC file; Is it really a binary module?" % ModuleFile)
373
374 else:
375 for Arch in self.ArchList:
376 if Arch == key:
377 Platform = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
378 MetaFileList = set()
379 for Pkey in Platform.Modules:
380 MetaFileList.add(Platform.Modules[Pkey].MetaFile)
381 for Inf in self.FdfProfile.InfDict[key]:
382 ModuleFile = PathClass(NormPath(Inf), GlobalData.gWorkspace, Arch)
383 if ModuleFile in MetaFileList:
384 continue
385 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]
386 if not ModuleData.IsBinaryModule:
387 EdkLogger.error('build', PARSER_ERROR, "Module %s NOT found in DSC file; Is it really a binary module?" % ModuleFile)
388
389 else:
390 PcdSet = {}
391 ModuleList = []
392 self.FdfProfile = None
393 if self.FdTargetList:
394 EdkLogger.info("No flash definition file found. FD [%s] will be ignored." % " ".join(self.FdTargetList))
395 self.FdTargetList = []
396 if self.FvTargetList:
397 EdkLogger.info("No flash definition file found. FV [%s] will be ignored." % " ".join(self.FvTargetList))
398 self.FvTargetList = []
399 if self.CapTargetList:
400 EdkLogger.info("No flash definition file found. Capsule [%s] will be ignored." % " ".join(self.CapTargetList))
401 self.CapTargetList = []
402
403 # apply SKU and inject PCDs from Flash Definition file
404 for Arch in self.ArchList:
405 Platform = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
406 PlatformPcds = Platform.Pcds
407 self._GuidDict = Platform._GuidDict
408 SourcePcdDict = {'DynamicEx':set(), 'PatchableInModule':set(),'Dynamic':set(),'FixedAtBuild':set()}
409 BinaryPcdDict = {'DynamicEx':set(), 'PatchableInModule':set()}
410 SourcePcdDict_Keys = SourcePcdDict.keys()
411 BinaryPcdDict_Keys = BinaryPcdDict.keys()
412
413 # generate the SourcePcdDict and BinaryPcdDict
414 PGen = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)
415 for BuildData in PGen.BuildDatabase._CACHE_.values():
416 if BuildData.Arch != Arch:
417 continue
418 if BuildData.MetaFile.Ext == '.inf':
419 for key in BuildData.Pcds:
420 if BuildData.Pcds[key].Pending:
421 if key in Platform.Pcds:
422 PcdInPlatform = Platform.Pcds[key]
423 if PcdInPlatform.Type not in [None, '']:
424 BuildData.Pcds[key].Type = PcdInPlatform.Type
425
426 if BuildData.MetaFile in Platform.Modules:
427 PlatformModule = Platform.Modules[str(BuildData.MetaFile)]
428 if key in PlatformModule.Pcds:
429 PcdInPlatform = PlatformModule.Pcds[key]
430 if PcdInPlatform.Type not in [None, '']:
431 BuildData.Pcds[key].Type = PcdInPlatform.Type
432
433 if 'DynamicEx' in BuildData.Pcds[key].Type:
434 if BuildData.IsBinaryModule:
435 BinaryPcdDict['DynamicEx'].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
436 else:
437 SourcePcdDict['DynamicEx'].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
438
439 elif 'PatchableInModule' in BuildData.Pcds[key].Type:
440 if BuildData.MetaFile.Ext == '.inf':
441 if BuildData.IsBinaryModule:
442 BinaryPcdDict['PatchableInModule'].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
443 else:
444 SourcePcdDict['PatchableInModule'].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
445
446 elif 'Dynamic' in BuildData.Pcds[key].Type:
447 SourcePcdDict['Dynamic'].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
448 elif 'FixedAtBuild' in BuildData.Pcds[key].Type:
449 SourcePcdDict['FixedAtBuild'].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
450 else:
451 pass
452 #
453 # A PCD can only use one type for all source modules
454 #
455 for i in SourcePcdDict_Keys:
456 for j in SourcePcdDict_Keys:
457 if i != j:
458 Intersections = SourcePcdDict[i].intersection(SourcePcdDict[j])
459 if len(Intersections) > 0:
460 EdkLogger.error(
461 'build',
462 FORMAT_INVALID,
463 "Building modules from source INFs, following PCD use %s and %s access method. It must be corrected to use only one access method." % (i, j),
464 ExtraData="%s" % '\n\t'.join([str(P[1]+'.'+P[0]) for P in Intersections])
465 )
466
467 #
468 # intersection the BinaryPCD for Mixed PCD
469 #
470 for i in BinaryPcdDict_Keys:
471 for j in BinaryPcdDict_Keys:
472 if i != j:
473 Intersections = BinaryPcdDict[i].intersection(BinaryPcdDict[j])
474 for item in Intersections:
475 NewPcd1 = (item[0] + '_' + i, item[1])
476 NewPcd2 = (item[0] + '_' + j, item[1])
477 if item not in GlobalData.MixedPcd:
478 GlobalData.MixedPcd[item] = [NewPcd1, NewPcd2]
479 else:
480 if NewPcd1 not in GlobalData.MixedPcd[item]:
481 GlobalData.MixedPcd[item].append(NewPcd1)
482 if NewPcd2 not in GlobalData.MixedPcd[item]:
483 GlobalData.MixedPcd[item].append(NewPcd2)
484
485 #
486 # intersection the SourcePCD and BinaryPCD for Mixed PCD
487 #
488 for i in SourcePcdDict_Keys:
489 for j in BinaryPcdDict_Keys:
490 if i != j:
491 Intersections = SourcePcdDict[i].intersection(BinaryPcdDict[j])
492 for item in Intersections:
493 NewPcd1 = (item[0] + '_' + i, item[1])
494 NewPcd2 = (item[0] + '_' + j, item[1])
495 if item not in GlobalData.MixedPcd:
496 GlobalData.MixedPcd[item] = [NewPcd1, NewPcd2]
497 else:
498 if NewPcd1 not in GlobalData.MixedPcd[item]:
499 GlobalData.MixedPcd[item].append(NewPcd1)
500 if NewPcd2 not in GlobalData.MixedPcd[item]:
501 GlobalData.MixedPcd[item].append(NewPcd2)
502
503 for BuildData in PGen.BuildDatabase._CACHE_.values():
504 if BuildData.Arch != Arch:
505 continue
506 for key in BuildData.Pcds:
507 for SinglePcd in GlobalData.MixedPcd:
508 if (BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName) == SinglePcd:
509 for item in GlobalData.MixedPcd[SinglePcd]:
510 Pcd_Type = item[0].split('_')[-1]
511 if (Pcd_Type == BuildData.Pcds[key].Type) or (Pcd_Type == TAB_PCDS_DYNAMIC_EX and BuildData.Pcds[key].Type in GenC.gDynamicExPcd) or \
512 (Pcd_Type == TAB_PCDS_DYNAMIC and BuildData.Pcds[key].Type in GenC.gDynamicPcd):
513 Value = BuildData.Pcds[key]
514 Value.TokenCName = BuildData.Pcds[key].TokenCName + '_' + Pcd_Type
515 if len(key) == 2:
516 newkey = (Value.TokenCName, key[1])
517 elif len(key) == 3:
518 newkey = (Value.TokenCName, key[1], key[2])
519 del BuildData.Pcds[key]
520 BuildData.Pcds[newkey] = Value
521 break
522 break
523
524 # handle the mixed pcd in FDF file
525 for key in PcdSet:
526 if key in GlobalData.MixedPcd:
527 Value = PcdSet[key]
528 del PcdSet[key]
529 for item in GlobalData.MixedPcd[key]:
530 PcdSet[item] = Value
531
532 #Collect package set information from INF of FDF
533 PkgSet = set()
534 for Inf in ModuleList:
535 ModuleFile = PathClass(NormPath(Inf), GlobalData.gWorkspace, Arch)
536 if ModuleFile in Platform.Modules:
537 continue
538 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]
539 PkgSet.update(ModuleData.Packages)
540 Pkgs = list(PkgSet) + list(PGen.PackageList)
541 DecPcds = set()
542 DecPcdsKey = set()
543 for Pkg in Pkgs:
544 for Pcd in Pkg.Pcds:
545 DecPcds.add((Pcd[0], Pcd[1]))
546 DecPcdsKey.add((Pcd[0], Pcd[1], Pcd[2]))
547
548 Platform.SkuName = self.SkuId
549 for Name, Guid in PcdSet:
550 if (Name, Guid) not in DecPcds:
551 EdkLogger.error(
552 'build',
553 PARSER_ERROR,
554 "PCD (%s.%s) used in FDF is not declared in DEC files." % (Guid, Name),
555 File = self.FdfProfile.PcdFileLineDict[Name, Guid][0],
556 Line = self.FdfProfile.PcdFileLineDict[Name, Guid][1]
557 )
558 else:
559 # Check whether Dynamic or DynamicEx PCD used in FDF file. If used, build break and give a error message.
560 if (Name, Guid, TAB_PCDS_FIXED_AT_BUILD) in DecPcdsKey \
561 or (Name, Guid, TAB_PCDS_PATCHABLE_IN_MODULE) in DecPcdsKey \
562 or (Name, Guid, TAB_PCDS_FEATURE_FLAG) in DecPcdsKey:
563 Platform.AddPcd(Name, Guid, PcdSet[Name, Guid])
564 continue
565 elif (Name, Guid, TAB_PCDS_DYNAMIC) in DecPcdsKey or (Name, Guid, TAB_PCDS_DYNAMIC_EX) in DecPcdsKey:
566 EdkLogger.error(
567 'build',
568 PARSER_ERROR,
569 "Using Dynamic or DynamicEx type of PCD [%s.%s] in FDF file is not allowed." % (Guid, Name),
570 File = self.FdfProfile.PcdFileLineDict[Name, Guid][0],
571 Line = self.FdfProfile.PcdFileLineDict[Name, Guid][1]
572 )
573
574 Pa = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)
575 #
576 # Explicitly collect platform's dynamic PCDs
577 #
578 Pa.CollectPlatformDynamicPcds()
579 Pa.CollectFixedAtBuildPcds()
580 self.AutoGenObjectList.append(Pa)
581
582 #
583 # Generate Package level hash value
584 #
585 GlobalData.gPackageHash[Arch] = {}
586 if GlobalData.gUseHashCache:
587 for Pkg in Pkgs:
588 self._GenPkgLevelHash(Pkg)
589
590 #
591 # Check PCDs token value conflict in each DEC file.
592 #
593 self._CheckAllPcdsTokenValueConflict()
594
595 #
596 # Check PCD type and definition between DSC and DEC
597 #
598 self._CheckPcdDefineAndType()
599
600 # if self.FdfFile:
601 # self._CheckDuplicateInFV(Fdf)
602
603 #
604 # Create BuildOptions Macro & PCD metafile, also add the Active Platform and FDF file.
605 #
606 content = 'gCommandLineDefines: '
607 content += str(GlobalData.gCommandLineDefines)
608 content += os.linesep
609 content += 'BuildOptionPcd: '
610 content += str(GlobalData.BuildOptionPcd)
611 content += os.linesep
612 content += 'Active Platform: '
613 content += str(self.Platform)
614 content += os.linesep
615 if self.FdfFile:
616 content += 'Flash Image Definition: '
617 content += str(self.FdfFile)
618 content += os.linesep
619 SaveFileOnChange(os.path.join(self.BuildDir, 'BuildOptions'), content, False)
620
621 #
622 # Create PcdToken Number file for Dynamic/DynamicEx Pcd.
623 #
624 PcdTokenNumber = 'PcdTokenNumber: '
625 if Pa.PcdTokenNumber:
626 if Pa.DynamicPcdList:
627 for Pcd in Pa.DynamicPcdList:
628 PcdTokenNumber += os.linesep
629 PcdTokenNumber += str((Pcd.TokenCName, Pcd.TokenSpaceGuidCName))
630 PcdTokenNumber += ' : '
631 PcdTokenNumber += str(Pa.PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName])
632 SaveFileOnChange(os.path.join(self.BuildDir, 'PcdTokenNumber'), PcdTokenNumber, False)
633
634 #
635 # Get set of workspace metafiles
636 #
637 AllWorkSpaceMetaFiles = self._GetMetaFiles(Target, Toolchain, Arch)
638
639 #
640 # Retrieve latest modified time of all metafiles
641 #
642 SrcTimeStamp = 0
643 for f in AllWorkSpaceMetaFiles:
644 if os.stat(f)[8] > SrcTimeStamp:
645 SrcTimeStamp = os.stat(f)[8]
646 self._SrcTimeStamp = SrcTimeStamp
647
648 if GlobalData.gUseHashCache:
649 m = hashlib.md5()
650 for files in AllWorkSpaceMetaFiles:
651 if files.endswith('.dec'):
652 continue
653 f = open(files, 'r')
654 Content = f.read()
655 f.close()
656 m.update(Content)
657 SaveFileOnChange(os.path.join(self.BuildDir, 'AutoGen.hash'), m.hexdigest(), True)
658 GlobalData.gPlatformHash = m.hexdigest()
659
660 #
661 # Write metafile list to build directory
662 #
663 AutoGenFilePath = os.path.join(self.BuildDir, 'AutoGen')
664 if os.path.exists (AutoGenFilePath):
665 os.remove(AutoGenFilePath)
666 if not os.path.exists(self.BuildDir):
667 os.makedirs(self.BuildDir)
668 with open(os.path.join(self.BuildDir, 'AutoGen'), 'w+') as file:
669 for f in AllWorkSpaceMetaFiles:
670 print >> file, f
671 return True
672
673 def _GenPkgLevelHash(self, Pkg):
674 PkgDir = os.path.join(self.BuildDir, Pkg.Arch, Pkg.PackageName)
675 CreateDirectory(PkgDir)
676 HashFile = os.path.join(PkgDir, Pkg.PackageName + '.hash')
677 m = hashlib.md5()
678 # Get .dec file's hash value
679 f = open(Pkg.MetaFile.Path, 'r')
680 Content = f.read()
681 f.close()
682 m.update(Content)
683 # Get include files hash value
684 if Pkg.Includes:
685 for inc in Pkg.Includes:
686 for Root, Dirs, Files in os.walk(str(inc)):
687 for File in Files:
688 File_Path = os.path.join(Root, File)
689 f = open(File_Path, 'r')
690 Content = f.read()
691 f.close()
692 m.update(Content)
693 SaveFileOnChange(HashFile, m.hexdigest(), True)
694 if Pkg.PackageName not in GlobalData.gPackageHash[Pkg.Arch]:
695 GlobalData.gPackageHash[Pkg.Arch][Pkg.PackageName] = m.hexdigest()
696
697 def _GetMetaFiles(self, Target, Toolchain, Arch):
698 AllWorkSpaceMetaFiles = set()
699 #
700 # add fdf
701 #
702 if self.FdfFile:
703 AllWorkSpaceMetaFiles.add (self.FdfFile.Path)
704 if self.FdfFile:
705 FdfFiles = GlobalData.gFdfParser.GetAllIncludedFile()
706 for f in FdfFiles:
707 AllWorkSpaceMetaFiles.add (f.FileName)
708 #
709 # add dsc
710 #
711 AllWorkSpaceMetaFiles.add(self.MetaFile.Path)
712
713 #
714 # add build_rule.txt & tools_def.txt
715 #
716 AllWorkSpaceMetaFiles.add(os.path.join(GlobalData.gConfDirectory, gDefaultBuildRuleFile))
717 AllWorkSpaceMetaFiles.add(os.path.join(GlobalData.gConfDirectory, gDefaultToolsDefFile))
718
719 # add BuildOption metafile
720 #
721 AllWorkSpaceMetaFiles.add(os.path.join(self.BuildDir, 'BuildOptions'))
722
723 # add PcdToken Number file for Dynamic/DynamicEx Pcd
724 #
725 AllWorkSpaceMetaFiles.add(os.path.join(self.BuildDir, 'PcdTokenNumber'))
726
727 for Arch in self.ArchList:
728 Platform = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
729 PGen = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)
730
731 #
732 # add dec
733 #
734 for Package in PGen.PackageList:
735 AllWorkSpaceMetaFiles.add(Package.MetaFile.Path)
736
737 #
738 # add included dsc
739 #
740 for filePath in Platform._RawData.IncludedFiles:
741 AllWorkSpaceMetaFiles.add(filePath.Path)
742
743 return AllWorkSpaceMetaFiles
744
745 ## _CheckDuplicateInFV() method
746 #
747 # Check whether there is duplicate modules/files exist in FV section.
748 # The check base on the file GUID;
749 #
750 def _CheckDuplicateInFV(self, Fdf):
751 for Fv in Fdf.Profile.FvDict:
752 _GuidDict = {}
753 for FfsFile in Fdf.Profile.FvDict[Fv].FfsList:
754 if FfsFile.InfFileName and FfsFile.NameGuid is None:
755 #
756 # Get INF file GUID
757 #
758 InfFoundFlag = False
759 for Pa in self.AutoGenObjectList:
760 if InfFoundFlag:
761 break
762 for Module in Pa.ModuleAutoGenList:
763 if path.normpath(Module.MetaFile.File) == path.normpath(FfsFile.InfFileName):
764 InfFoundFlag = True
765 if not Module.Guid.upper() in _GuidDict.keys():
766 _GuidDict[Module.Guid.upper()] = FfsFile
767 break
768 else:
769 EdkLogger.error("build",
770 FORMAT_INVALID,
771 "Duplicate GUID found for these lines: Line %d: %s and Line %d: %s. GUID: %s" % (FfsFile.CurrentLineNum,
772 FfsFile.CurrentLineContent,
773 _GuidDict[Module.Guid.upper()].CurrentLineNum,
774 _GuidDict[Module.Guid.upper()].CurrentLineContent,
775 Module.Guid.upper()),
776 ExtraData=self.FdfFile)
777 #
778 # Some INF files not have entity in DSC file.
779 #
780 if not InfFoundFlag:
781 if FfsFile.InfFileName.find('$') == -1:
782 InfPath = NormPath(FfsFile.InfFileName)
783 if not os.path.exists(InfPath):
784 EdkLogger.error('build', GENFDS_ERROR, "Non-existant Module %s !" % (FfsFile.InfFileName))
785
786 PathClassObj = PathClass(FfsFile.InfFileName, self.WorkspaceDir)
787 #
788 # Here we just need to get FILE_GUID from INF file, use 'COMMON' as ARCH attribute. and use
789 # BuildObject from one of AutoGenObjectList is enough.
790 #
791 InfObj = self.AutoGenObjectList[0].BuildDatabase.WorkspaceDb.BuildObject[PathClassObj, 'COMMON', self.BuildTarget, self.ToolChain]
792 if not InfObj.Guid.upper() in _GuidDict.keys():
793 _GuidDict[InfObj.Guid.upper()] = FfsFile
794 else:
795 EdkLogger.error("build",
796 FORMAT_INVALID,
797 "Duplicate GUID found for these lines: Line %d: %s and Line %d: %s. GUID: %s" % (FfsFile.CurrentLineNum,
798 FfsFile.CurrentLineContent,
799 _GuidDict[InfObj.Guid.upper()].CurrentLineNum,
800 _GuidDict[InfObj.Guid.upper()].CurrentLineContent,
801 InfObj.Guid.upper()),
802 ExtraData=self.FdfFile)
803 InfFoundFlag = False
804
805 if FfsFile.NameGuid is not None:
806 #
807 # If the NameGuid reference a PCD name.
808 # The style must match: PCD(xxxx.yyy)
809 #
810 if gPCDAsGuidPattern.match(FfsFile.NameGuid):
811 #
812 # Replace the PCD value.
813 #
814 _PcdName = FfsFile.NameGuid.lstrip("PCD(").rstrip(")")
815 PcdFoundFlag = False
816 for Pa in self.AutoGenObjectList:
817 if not PcdFoundFlag:
818 for PcdItem in Pa.AllPcdList:
819 if (PcdItem.TokenSpaceGuidCName + "." + PcdItem.TokenCName) == _PcdName:
820 #
821 # First convert from CFormatGuid to GUID string
822 #
823 _PcdGuidString = GuidStructureStringToGuidString(PcdItem.DefaultValue)
824
825 if not _PcdGuidString:
826 #
827 # Then try Byte array.
828 #
829 _PcdGuidString = GuidStructureByteArrayToGuidString(PcdItem.DefaultValue)
830
831 if not _PcdGuidString:
832 #
833 # Not Byte array or CFormat GUID, raise error.
834 #
835 EdkLogger.error("build",
836 FORMAT_INVALID,
837 "The format of PCD value is incorrect. PCD: %s , Value: %s\n" % (_PcdName, PcdItem.DefaultValue),
838 ExtraData=self.FdfFile)
839
840 if not _PcdGuidString.upper() in _GuidDict.keys():
841 _GuidDict[_PcdGuidString.upper()] = FfsFile
842 PcdFoundFlag = True
843 break
844 else:
845 EdkLogger.error("build",
846 FORMAT_INVALID,
847 "Duplicate GUID found for these lines: Line %d: %s and Line %d: %s. GUID: %s" % (FfsFile.CurrentLineNum,
848 FfsFile.CurrentLineContent,
849 _GuidDict[_PcdGuidString.upper()].CurrentLineNum,
850 _GuidDict[_PcdGuidString.upper()].CurrentLineContent,
851 FfsFile.NameGuid.upper()),
852 ExtraData=self.FdfFile)
853
854 if not FfsFile.NameGuid.upper() in _GuidDict.keys():
855 _GuidDict[FfsFile.NameGuid.upper()] = FfsFile
856 else:
857 #
858 # Two raw file GUID conflict.
859 #
860 EdkLogger.error("build",
861 FORMAT_INVALID,
862 "Duplicate GUID found for these lines: Line %d: %s and Line %d: %s. GUID: %s" % (FfsFile.CurrentLineNum,
863 FfsFile.CurrentLineContent,
864 _GuidDict[FfsFile.NameGuid.upper()].CurrentLineNum,
865 _GuidDict[FfsFile.NameGuid.upper()].CurrentLineContent,
866 FfsFile.NameGuid.upper()),
867 ExtraData=self.FdfFile)
868
869
870 def _CheckPcdDefineAndType(self):
871 PcdTypeList = [
872 "FixedAtBuild", "PatchableInModule", "FeatureFlag",
873 "Dynamic", #"DynamicHii", "DynamicVpd",
874 "DynamicEx", # "DynamicExHii", "DynamicExVpd"
875 ]
876
877 # This dict store PCDs which are not used by any modules with specified arches
878 UnusedPcd = OrderedDict()
879 for Pa in self.AutoGenObjectList:
880 # Key of DSC's Pcds dictionary is PcdCName, TokenSpaceGuid
881 for Pcd in Pa.Platform.Pcds:
882 PcdType = Pa.Platform.Pcds[Pcd].Type
883
884 # If no PCD type, this PCD comes from FDF
885 if not PcdType:
886 continue
887
888 # Try to remove Hii and Vpd suffix
889 if PcdType.startswith("DynamicEx"):
890 PcdType = "DynamicEx"
891 elif PcdType.startswith("Dynamic"):
892 PcdType = "Dynamic"
893
894 for Package in Pa.PackageList:
895 # Key of DEC's Pcds dictionary is PcdCName, TokenSpaceGuid, PcdType
896 if (Pcd[0], Pcd[1], PcdType) in Package.Pcds:
897 break
898 for Type in PcdTypeList:
899 if (Pcd[0], Pcd[1], Type) in Package.Pcds:
900 EdkLogger.error(
901 'build',
902 FORMAT_INVALID,
903 "Type [%s] of PCD [%s.%s] in DSC file doesn't match the type [%s] defined in DEC file." \
904 % (Pa.Platform.Pcds[Pcd].Type, Pcd[1], Pcd[0], Type),
905 ExtraData=None
906 )
907 return
908 else:
909 UnusedPcd.setdefault(Pcd, []).append(Pa.Arch)
910
911 for Pcd in UnusedPcd:
912 EdkLogger.warn(
913 'build',
914 "The PCD was not specified by any INF module in the platform for the given architecture.\n"
915 "\tPCD: [%s.%s]\n\tPlatform: [%s]\n\tArch: %s"
916 % (Pcd[1], Pcd[0], os.path.basename(str(self.MetaFile)), str(UnusedPcd[Pcd])),
917 ExtraData=None
918 )
919
920 def __repr__(self):
921 return "%s [%s]" % (self.MetaFile, ", ".join(self.ArchList))
922
923 ## Return the directory to store FV files
924 def _GetFvDir(self):
925 if self._FvDir is None:
926 self._FvDir = path.join(self.BuildDir, 'FV')
927 return self._FvDir
928
929 ## Return the directory to store all intermediate and final files built
930 def _GetBuildDir(self):
931 if self._BuildDir is None:
932 return self.AutoGenObjectList[0].BuildDir
933
934 ## Return the build output directory platform specifies
935 def _GetOutputDir(self):
936 return self.Platform.OutputDirectory
937
938 ## Return platform name
939 def _GetName(self):
940 return self.Platform.PlatformName
941
942 ## Return meta-file GUID
943 def _GetGuid(self):
944 return self.Platform.Guid
945
946 ## Return platform version
947 def _GetVersion(self):
948 return self.Platform.Version
949
950 ## Return paths of tools
951 def _GetToolDefinition(self):
952 return self.AutoGenObjectList[0].ToolDefinition
953
954 ## Return directory of platform makefile
955 #
956 # @retval string Makefile directory
957 #
958 def _GetMakeFileDir(self):
959 if self._MakeFileDir is None:
960 self._MakeFileDir = self.BuildDir
961 return self._MakeFileDir
962
963 ## Return build command string
964 #
965 # @retval string Build command string
966 #
967 def _GetBuildCommand(self):
968 if self._BuildCommand is None:
969 # BuildCommand should be all the same. So just get one from platform AutoGen
970 self._BuildCommand = self.AutoGenObjectList[0].BuildCommand
971 return self._BuildCommand
972
973 ## Check the PCDs token value conflict in each DEC file.
974 #
975 # Will cause build break and raise error message while two PCDs conflict.
976 #
977 # @return None
978 #
979 def _CheckAllPcdsTokenValueConflict(self):
980 for Pa in self.AutoGenObjectList:
981 for Package in Pa.PackageList:
982 PcdList = Package.Pcds.values()
983 PcdList.sort(lambda x, y: cmp(int(x.TokenValue, 0), int(y.TokenValue, 0)))
984 Count = 0
985 while (Count < len(PcdList) - 1) :
986 Item = PcdList[Count]
987 ItemNext = PcdList[Count + 1]
988 #
989 # Make sure in the same token space the TokenValue should be unique
990 #
991 if (int(Item.TokenValue, 0) == int(ItemNext.TokenValue, 0)):
992 SameTokenValuePcdList = []
993 SameTokenValuePcdList.append(Item)
994 SameTokenValuePcdList.append(ItemNext)
995 RemainPcdListLength = len(PcdList) - Count - 2
996 for ValueSameCount in range(RemainPcdListLength):
997 if int(PcdList[len(PcdList) - RemainPcdListLength + ValueSameCount].TokenValue, 0) == int(Item.TokenValue, 0):
998 SameTokenValuePcdList.append(PcdList[len(PcdList) - RemainPcdListLength + ValueSameCount])
999 else:
1000 break;
1001 #
1002 # Sort same token value PCD list with TokenGuid and TokenCName
1003 #
1004 SameTokenValuePcdList.sort(lambda x, y: cmp("%s.%s" % (x.TokenSpaceGuidCName, x.TokenCName), "%s.%s" % (y.TokenSpaceGuidCName, y.TokenCName)))
1005 SameTokenValuePcdListCount = 0
1006 while (SameTokenValuePcdListCount < len(SameTokenValuePcdList) - 1):
1007 Flag = False
1008 TemListItem = SameTokenValuePcdList[SameTokenValuePcdListCount]
1009 TemListItemNext = SameTokenValuePcdList[SameTokenValuePcdListCount + 1]
1010
1011 if (TemListItem.TokenSpaceGuidCName == TemListItemNext.TokenSpaceGuidCName) and (TemListItem.TokenCName != TemListItemNext.TokenCName):
1012 for PcdItem in GlobalData.MixedPcd:
1013 if (TemListItem.TokenCName, TemListItem.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem] or \
1014 (TemListItemNext.TokenCName, TemListItemNext.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
1015 Flag = True
1016 if not Flag:
1017 EdkLogger.error(
1018 'build',
1019 FORMAT_INVALID,
1020 "The TokenValue [%s] of PCD [%s.%s] is conflict with: [%s.%s] in %s"\
1021 % (TemListItem.TokenValue, TemListItem.TokenSpaceGuidCName, TemListItem.TokenCName, TemListItemNext.TokenSpaceGuidCName, TemListItemNext.TokenCName, Package),
1022 ExtraData=None
1023 )
1024 SameTokenValuePcdListCount += 1
1025 Count += SameTokenValuePcdListCount
1026 Count += 1
1027
1028 PcdList = Package.Pcds.values()
1029 PcdList.sort(lambda x, y: cmp("%s.%s" % (x.TokenSpaceGuidCName, x.TokenCName), "%s.%s" % (y.TokenSpaceGuidCName, y.TokenCName)))
1030 Count = 0
1031 while (Count < len(PcdList) - 1) :
1032 Item = PcdList[Count]
1033 ItemNext = PcdList[Count + 1]
1034 #
1035 # Check PCDs with same TokenSpaceGuidCName.TokenCName have same token value as well.
1036 #
1037 if (Item.TokenSpaceGuidCName == ItemNext.TokenSpaceGuidCName) and (Item.TokenCName == ItemNext.TokenCName) and (int(Item.TokenValue, 0) != int(ItemNext.TokenValue, 0)):
1038 EdkLogger.error(
1039 'build',
1040 FORMAT_INVALID,
1041 "The TokenValue [%s] of PCD [%s.%s] in %s defined in two places should be same as well."\
1042 % (Item.TokenValue, Item.TokenSpaceGuidCName, Item.TokenCName, Package),
1043 ExtraData=None
1044 )
1045 Count += 1
1046 ## Generate fds command
1047 def _GenFdsCommand(self):
1048 return (GenMake.TopLevelMakefile(self)._TEMPLATE_.Replace(GenMake.TopLevelMakefile(self)._TemplateDict)).strip()
1049
1050 ## Create makefile for the platform and modules in it
1051 #
1052 # @param CreateDepsMakeFile Flag indicating if the makefile for
1053 # modules will be created as well
1054 #
1055 def CreateMakeFile(self, CreateDepsMakeFile=False):
1056 if CreateDepsMakeFile:
1057 for Pa in self.AutoGenObjectList:
1058 Pa.CreateMakeFile(CreateDepsMakeFile)
1059
1060 ## Create autogen code for platform and modules
1061 #
1062 # Since there's no autogen code for platform, this method will do nothing
1063 # if CreateModuleCodeFile is set to False.
1064 #
1065 # @param CreateDepsCodeFile Flag indicating if creating module's
1066 # autogen code file or not
1067 #
1068 def CreateCodeFile(self, CreateDepsCodeFile=False):
1069 if not CreateDepsCodeFile:
1070 return
1071 for Pa in self.AutoGenObjectList:
1072 Pa.CreateCodeFile(CreateDepsCodeFile)
1073
1074 ## Create AsBuilt INF file the platform
1075 #
1076 def CreateAsBuiltInf(self):
1077 return
1078
1079 Name = property(_GetName)
1080 Guid = property(_GetGuid)
1081 Version = property(_GetVersion)
1082 OutputDir = property(_GetOutputDir)
1083
1084 ToolDefinition = property(_GetToolDefinition) # toolcode : tool path
1085
1086 BuildDir = property(_GetBuildDir)
1087 FvDir = property(_GetFvDir)
1088 MakeFileDir = property(_GetMakeFileDir)
1089 BuildCommand = property(_GetBuildCommand)
1090 GenFdsCommand = property(_GenFdsCommand)
1091
1092 ## AutoGen class for platform
1093 #
1094 # PlatformAutoGen class will process the original information in platform
1095 # file in order to generate makefile for platform.
1096 #
1097 class PlatformAutoGen(AutoGen):
1098 # call super().__init__ then call the worker function with different parameter count
1099 def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
1100 try:
1101 self._Init
1102 except:
1103 super(PlatformAutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
1104 self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch)
1105 self._Init = True
1106 #
1107 # Used to store all PCDs for both PEI and DXE phase, in order to generate
1108 # correct PCD database
1109 #
1110 _DynaPcdList_ = []
1111 _NonDynaPcdList_ = []
1112 _PlatformPcds = {}
1113
1114 #
1115 # The priority list while override build option
1116 #
1117 PrioList = {"0x11111" : 16, # TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE (Highest)
1118 "0x01111" : 15, # ******_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
1119 "0x10111" : 14, # TARGET_*********_ARCH_COMMANDTYPE_ATTRIBUTE
1120 "0x00111" : 13, # ******_*********_ARCH_COMMANDTYPE_ATTRIBUTE
1121 "0x11011" : 12, # TARGET_TOOLCHAIN_****_COMMANDTYPE_ATTRIBUTE
1122 "0x01011" : 11, # ******_TOOLCHAIN_****_COMMANDTYPE_ATTRIBUTE
1123 "0x10011" : 10, # TARGET_*********_****_COMMANDTYPE_ATTRIBUTE
1124 "0x00011" : 9, # ******_*********_****_COMMANDTYPE_ATTRIBUTE
1125 "0x11101" : 8, # TARGET_TOOLCHAIN_ARCH_***********_ATTRIBUTE
1126 "0x01101" : 7, # ******_TOOLCHAIN_ARCH_***********_ATTRIBUTE
1127 "0x10101" : 6, # TARGET_*********_ARCH_***********_ATTRIBUTE
1128 "0x00101" : 5, # ******_*********_ARCH_***********_ATTRIBUTE
1129 "0x11001" : 4, # TARGET_TOOLCHAIN_****_***********_ATTRIBUTE
1130 "0x01001" : 3, # ******_TOOLCHAIN_****_***********_ATTRIBUTE
1131 "0x10001" : 2, # TARGET_*********_****_***********_ATTRIBUTE
1132 "0x00001" : 1} # ******_*********_****_***********_ATTRIBUTE (Lowest)
1133
1134 ## Initialize PlatformAutoGen
1135 #
1136 #
1137 # @param Workspace WorkspaceAutoGen object
1138 # @param PlatformFile Platform file (DSC file)
1139 # @param Target Build target (DEBUG, RELEASE)
1140 # @param Toolchain Name of tool chain
1141 # @param Arch arch of the platform supports
1142 #
1143 def _InitWorker(self, Workspace, PlatformFile, Target, Toolchain, Arch):
1144 EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen platform [%s] [%s]" % (PlatformFile, Arch))
1145 GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (PlatformFile, Arch, Toolchain, Target)
1146
1147 self.MetaFile = PlatformFile
1148 self.Workspace = Workspace
1149 self.WorkspaceDir = Workspace.WorkspaceDir
1150 self.ToolChain = Toolchain
1151 self.BuildTarget = Target
1152 self.Arch = Arch
1153 self.SourceDir = PlatformFile.SubDir
1154 self.SourceOverrideDir = None
1155 self.FdTargetList = self.Workspace.FdTargetList
1156 self.FvTargetList = self.Workspace.FvTargetList
1157 self.AllPcdList = []
1158 # get the original module/package/platform objects
1159 self.BuildDatabase = Workspace.BuildDatabase
1160 self.DscBuildDataObj = Workspace.Platform
1161 self._GuidDict = Workspace._GuidDict
1162
1163 # flag indicating if the makefile/C-code file has been created or not
1164 self.IsMakeFileCreated = False
1165 self.IsCodeFileCreated = False
1166
1167 self._Platform = None
1168 self._Name = None
1169 self._Guid = None
1170 self._Version = None
1171
1172 self._BuildRule = None
1173 self._SourceDir = None
1174 self._BuildDir = None
1175 self._OutputDir = None
1176 self._FvDir = None
1177 self._MakeFileDir = None
1178 self._FdfFile = None
1179
1180 self._PcdTokenNumber = None # (TokenCName, TokenSpaceGuidCName) : GeneratedTokenNumber
1181 self._DynamicPcdList = None # [(TokenCName1, TokenSpaceGuidCName1), (TokenCName2, TokenSpaceGuidCName2), ...]
1182 self._NonDynamicPcdList = None # [(TokenCName1, TokenSpaceGuidCName1), (TokenCName2, TokenSpaceGuidCName2), ...]
1183 self._NonDynamicPcdDict = {}
1184
1185 self._ToolDefinitions = None
1186 self._ToolDefFile = None # toolcode : tool path
1187 self._ToolChainFamily = None
1188 self._BuildRuleFamily = None
1189 self._BuildOption = None # toolcode : option
1190 self._EdkBuildOption = None # edktoolcode : option
1191 self._EdkIIBuildOption = None # edkiitoolcode : option
1192 self._PackageList = None
1193 self._ModuleAutoGenList = None
1194 self._LibraryAutoGenList = None
1195 self._BuildCommand = None
1196 self._AsBuildInfList = []
1197 self._AsBuildModuleList = []
1198
1199 self.VariableInfo = None
1200
1201 if GlobalData.gFdfParser is not None:
1202 self._AsBuildInfList = GlobalData.gFdfParser.Profile.InfList
1203 for Inf in self._AsBuildInfList:
1204 InfClass = PathClass(NormPath(Inf), GlobalData.gWorkspace, self.Arch)
1205 M = self.BuildDatabase[InfClass, self.Arch, self.BuildTarget, self.ToolChain]
1206 if not M.IsSupportedArch:
1207 continue
1208 self._AsBuildModuleList.append(InfClass)
1209 # get library/modules for build
1210 self.LibraryBuildDirectoryList = []
1211 self.ModuleBuildDirectoryList = []
1212
1213 return True
1214
1215 def __repr__(self):
1216 return "%s [%s]" % (self.MetaFile, self.Arch)
1217
1218 ## Create autogen code for platform and modules
1219 #
1220 # Since there's no autogen code for platform, this method will do nothing
1221 # if CreateModuleCodeFile is set to False.
1222 #
1223 # @param CreateModuleCodeFile Flag indicating if creating module's
1224 # autogen code file or not
1225 #
1226 def CreateCodeFile(self, CreateModuleCodeFile=False):
1227 # only module has code to be greated, so do nothing if CreateModuleCodeFile is False
1228 if self.IsCodeFileCreated or not CreateModuleCodeFile:
1229 return
1230
1231 for Ma in self.ModuleAutoGenList:
1232 Ma.CreateCodeFile(True)
1233
1234 # don't do this twice
1235 self.IsCodeFileCreated = True
1236
1237 ## Generate Fds Command
1238 def _GenFdsCommand(self):
1239 return self.Workspace.GenFdsCommand
1240
1241 ## Create makefile for the platform and mdoules in it
1242 #
1243 # @param CreateModuleMakeFile Flag indicating if the makefile for
1244 # modules will be created as well
1245 #
1246 def CreateMakeFile(self, CreateModuleMakeFile=False, FfsCommand = {}):
1247 if CreateModuleMakeFile:
1248 for ModuleFile in self.Platform.Modules:
1249 Ma = ModuleAutoGen(self.Workspace, ModuleFile, self.BuildTarget,
1250 self.ToolChain, self.Arch, self.MetaFile)
1251 if (ModuleFile.File, self.Arch) in FfsCommand:
1252 Ma.CreateMakeFile(True, FfsCommand[ModuleFile.File, self.Arch])
1253 else:
1254 Ma.CreateMakeFile(True)
1255 #Ma.CreateAsBuiltInf()
1256
1257 # no need to create makefile for the platform more than once
1258 if self.IsMakeFileCreated:
1259 return
1260
1261 # create library/module build dirs for platform
1262 Makefile = GenMake.PlatformMakefile(self)
1263 self.LibraryBuildDirectoryList = Makefile.GetLibraryBuildDirectoryList()
1264 self.ModuleBuildDirectoryList = Makefile.GetModuleBuildDirectoryList()
1265
1266 self.IsMakeFileCreated = True
1267
1268 ## Deal with Shared FixedAtBuild Pcds
1269 #
1270 def CollectFixedAtBuildPcds(self):
1271 for LibAuto in self.LibraryAutoGenList:
1272 FixedAtBuildPcds = {}
1273 ShareFixedAtBuildPcdsSameValue = {}
1274 for Module in LibAuto._ReferenceModules:
1275 for Pcd in Module.FixedAtBuildPcds + LibAuto.FixedAtBuildPcds:
1276 key = ".".join((Pcd.TokenSpaceGuidCName,Pcd.TokenCName))
1277 if key not in FixedAtBuildPcds:
1278 ShareFixedAtBuildPcdsSameValue[key] = True
1279 FixedAtBuildPcds[key] = Pcd.DefaultValue
1280 else:
1281 if FixedAtBuildPcds[key] != Pcd.DefaultValue:
1282 ShareFixedAtBuildPcdsSameValue[key] = False
1283 for Pcd in LibAuto.FixedAtBuildPcds:
1284 key = ".".join((Pcd.TokenSpaceGuidCName,Pcd.TokenCName))
1285 if (Pcd.TokenCName,Pcd.TokenSpaceGuidCName) not in self.NonDynamicPcdDict:
1286 continue
1287 else:
1288 DscPcd = self.NonDynamicPcdDict[(Pcd.TokenCName,Pcd.TokenSpaceGuidCName)]
1289 if DscPcd.Type != "FixedAtBuild":
1290 continue
1291 if key in ShareFixedAtBuildPcdsSameValue and ShareFixedAtBuildPcdsSameValue[key]:
1292 LibAuto.ConstPcd[key] = FixedAtBuildPcds[key]
1293
1294 def CollectVariables(self, DynamicPcdSet):
1295
1296 VpdRegionSize = 0
1297 VpdRegionBase = 0
1298 if self.Workspace.FdfFile:
1299 FdDict = self.Workspace.FdfProfile.FdDict[GlobalData.gFdfParser.CurrentFdName]
1300 for FdRegion in FdDict.RegionList:
1301 for item in FdRegion.RegionDataList:
1302 if self.Platform.VpdToolGuid.strip() and self.Platform.VpdToolGuid in item:
1303 VpdRegionSize = FdRegion.Size
1304 VpdRegionBase = FdRegion.Offset
1305 break
1306
1307
1308 VariableInfo = VariableMgr(self.DscBuildDataObj._GetDefaultStores(),self.DscBuildDataObj._GetSkuIds())
1309 VariableInfo.SetVpdRegionMaxSize(VpdRegionSize)
1310 VariableInfo.SetVpdRegionOffset(VpdRegionBase)
1311 Index = 0
1312 for Pcd in DynamicPcdSet:
1313 pcdname = ".".join((Pcd.TokenSpaceGuidCName,Pcd.TokenCName))
1314 for SkuName in Pcd.SkuInfoList:
1315 Sku = Pcd.SkuInfoList[SkuName]
1316 SkuId = Sku.SkuId
1317 if SkuId is None or SkuId == '':
1318 continue
1319 if len(Sku.VariableName) > 0:
1320 VariableGuidStructure = Sku.VariableGuidValue
1321 VariableGuid = GuidStructureStringToGuidString(VariableGuidStructure)
1322 for StorageName in Sku.DefaultStoreDict:
1323 VariableInfo.append_variable(var_info(Index,pcdname,StorageName,SkuName, StringToArray(Sku.VariableName),VariableGuid, Sku.VariableOffset, Sku.VariableAttribute , Sku.HiiDefaultValue,Sku.DefaultStoreDict[StorageName],Pcd.DatumType))
1324 Index += 1
1325 return VariableInfo
1326
1327 def UpdateNVStoreMaxSize(self,OrgVpdFile):
1328 if self.VariableInfo:
1329 VpdMapFilePath = os.path.join(self.BuildDir, "FV", "%s.map" % self.Platform.VpdToolGuid)
1330 PcdNvStoreDfBuffer = [item for item in self._DynamicPcdList if item.TokenCName == "PcdNvStoreDefaultValueBuffer" and item.TokenSpaceGuidCName == "gEfiMdeModulePkgTokenSpaceGuid"]
1331
1332 if PcdNvStoreDfBuffer:
1333 if os.path.exists(VpdMapFilePath):
1334 OrgVpdFile.Read(VpdMapFilePath)
1335 PcdItems = OrgVpdFile.GetOffset(PcdNvStoreDfBuffer[0])
1336 NvStoreOffset = PcdItems.values()[0].strip() if PcdItems else '0'
1337 else:
1338 EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)
1339
1340 NvStoreOffset = int(NvStoreOffset,16) if NvStoreOffset.upper().startswith("0X") else int(NvStoreOffset)
1341 default_skuobj = PcdNvStoreDfBuffer[0].SkuInfoList.get("DEFAULT")
1342 maxsize = self.VariableInfo.VpdRegionSize - NvStoreOffset if self.VariableInfo.VpdRegionSize else len(default_skuobj.DefaultValue.split(","))
1343 var_data = self.VariableInfo.PatchNVStoreDefaultMaxSize(maxsize)
1344
1345 if var_data and default_skuobj:
1346 default_skuobj.DefaultValue = var_data
1347 PcdNvStoreDfBuffer[0].DefaultValue = var_data
1348 PcdNvStoreDfBuffer[0].SkuInfoList.clear()
1349 PcdNvStoreDfBuffer[0].SkuInfoList['DEFAULT'] = default_skuobj
1350 PcdNvStoreDfBuffer[0].MaxDatumSize = str(len(default_skuobj.DefaultValue.split(",")))
1351
1352 return OrgVpdFile
1353
1354 ## Collect dynamic PCDs
1355 #
1356 # Gather dynamic PCDs list from each module and their settings from platform
1357 # This interface should be invoked explicitly when platform action is created.
1358 #
1359 def CollectPlatformDynamicPcds(self):
1360
1361 for key in self.Platform.Pcds:
1362 for SinglePcd in GlobalData.MixedPcd:
1363 if (self.Platform.Pcds[key].TokenCName, self.Platform.Pcds[key].TokenSpaceGuidCName) == SinglePcd:
1364 for item in GlobalData.MixedPcd[SinglePcd]:
1365 Pcd_Type = item[0].split('_')[-1]
1366 if (Pcd_Type == self.Platform.Pcds[key].Type) or (Pcd_Type == TAB_PCDS_DYNAMIC_EX and self.Platform.Pcds[key].Type in GenC.gDynamicExPcd) or \
1367 (Pcd_Type == TAB_PCDS_DYNAMIC and self.Platform.Pcds[key].Type in GenC.gDynamicPcd):
1368 Value = self.Platform.Pcds[key]
1369 Value.TokenCName = self.Platform.Pcds[key].TokenCName + '_' + Pcd_Type
1370 if len(key) == 2:
1371 newkey = (Value.TokenCName, key[1])
1372 elif len(key) == 3:
1373 newkey = (Value.TokenCName, key[1], key[2])
1374 del self.Platform.Pcds[key]
1375 self.Platform.Pcds[newkey] = Value
1376 break
1377 else:
1378 pass
1379 break
1380 else:
1381 pass
1382
1383 # for gathering error information
1384 NoDatumTypePcdList = set()
1385 FdfModuleList = []
1386 for InfName in self._AsBuildInfList:
1387 InfName = mws.join(self.WorkspaceDir, InfName)
1388 FdfModuleList.append(os.path.normpath(InfName))
1389 for F in self.Platform.Modules.keys():
1390 M = ModuleAutoGen(self.Workspace, F, self.BuildTarget, self.ToolChain, self.Arch, self.MetaFile)
1391 #GuidValue.update(M.Guids)
1392
1393 self.Platform.Modules[F].M = M
1394
1395 for PcdFromModule in M.ModulePcdList + M.LibraryPcdList:
1396 # make sure that the "VOID*" kind of datum has MaxDatumSize set
1397 if PcdFromModule.DatumType == "VOID*" and PcdFromModule.MaxDatumSize in [None, '']:
1398 NoDatumTypePcdList.add("%s.%s [%s]" % (PcdFromModule.TokenSpaceGuidCName, PcdFromModule.TokenCName, F))
1399
1400 # Check the PCD from Binary INF or Source INF
1401 if M.IsBinaryModule == True:
1402 PcdFromModule.IsFromBinaryInf = True
1403
1404 # Check the PCD from DSC or not
1405 if (PcdFromModule.TokenCName, PcdFromModule.TokenSpaceGuidCName) in self.Platform.Pcds.keys():
1406 PcdFromModule.IsFromDsc = True
1407 else:
1408 PcdFromModule.IsFromDsc = False
1409 if PcdFromModule.Type in GenC.gDynamicPcd or PcdFromModule.Type in GenC.gDynamicExPcd:
1410 if F.Path not in FdfModuleList:
1411 # If one of the Source built modules listed in the DSC is not listed
1412 # in FDF modules, and the INF lists a PCD can only use the PcdsDynamic
1413 # access method (it is only listed in the DEC file that declares the
1414 # PCD as PcdsDynamic), then build tool will report warning message
1415 # notify the PI that they are attempting to build a module that must
1416 # be included in a flash image in order to be functional. These Dynamic
1417 # PCD will not be added into the Database unless it is used by other
1418 # modules that are included in the FDF file.
1419 if PcdFromModule.Type in GenC.gDynamicPcd and \
1420 PcdFromModule.IsFromBinaryInf == False:
1421 # Print warning message to let the developer make a determine.
1422 continue
1423 # If one of the Source built modules listed in the DSC is not listed in
1424 # FDF modules, and the INF lists a PCD can only use the PcdsDynamicEx
1425 # access method (it is only listed in the DEC file that declares the
1426 # PCD as PcdsDynamicEx), then DO NOT break the build; DO NOT add the
1427 # PCD to the Platform's PCD Database.
1428 if PcdFromModule.Type in GenC.gDynamicExPcd:
1429 continue
1430 #
1431 # If a dynamic PCD used by a PEM module/PEI module & DXE module,
1432 # it should be stored in Pcd PEI database, If a dynamic only
1433 # used by DXE module, it should be stored in DXE PCD database.
1434 # The default Phase is DXE
1435 #
1436 if M.ModuleType in ["PEIM", "PEI_CORE"]:
1437 PcdFromModule.Phase = "PEI"
1438 if PcdFromModule not in self._DynaPcdList_:
1439 self._DynaPcdList_.append(PcdFromModule)
1440 elif PcdFromModule.Phase == 'PEI':
1441 # overwrite any the same PCD existing, if Phase is PEI
1442 Index = self._DynaPcdList_.index(PcdFromModule)
1443 self._DynaPcdList_[Index] = PcdFromModule
1444 elif PcdFromModule not in self._NonDynaPcdList_:
1445 self._NonDynaPcdList_.append(PcdFromModule)
1446 elif PcdFromModule in self._NonDynaPcdList_ and PcdFromModule.IsFromBinaryInf == True:
1447 Index = self._NonDynaPcdList_.index(PcdFromModule)
1448 if self._NonDynaPcdList_[Index].IsFromBinaryInf == False:
1449 #The PCD from Binary INF will override the same one from source INF
1450 self._NonDynaPcdList_.remove (self._NonDynaPcdList_[Index])
1451 PcdFromModule.Pending = False
1452 self._NonDynaPcdList_.append (PcdFromModule)
1453 DscModuleSet = {os.path.normpath(ModuleInf.Path) for ModuleInf in self.Platform.Modules}
1454 # add the PCD from modules that listed in FDF but not in DSC to Database
1455 for InfName in FdfModuleList:
1456 if InfName not in DscModuleSet:
1457 InfClass = PathClass(InfName)
1458 M = self.BuildDatabase[InfClass, self.Arch, self.BuildTarget, self.ToolChain]
1459 # If a module INF in FDF but not in current arch's DSC module list, it must be module (either binary or source)
1460 # for different Arch. PCDs in source module for different Arch is already added before, so skip the source module here.
1461 # For binary module, if in current arch, we need to list the PCDs into database.
1462 if not M.IsSupportedArch:
1463 continue
1464 # Override the module PCD setting by platform setting
1465 ModulePcdList = self.ApplyPcdSetting(M, M.Pcds)
1466 for PcdFromModule in ModulePcdList:
1467 PcdFromModule.IsFromBinaryInf = True
1468 PcdFromModule.IsFromDsc = False
1469 # Only allow the DynamicEx and Patchable PCD in AsBuild INF
1470 if PcdFromModule.Type not in GenC.gDynamicExPcd and PcdFromModule.Type not in TAB_PCDS_PATCHABLE_IN_MODULE:
1471 EdkLogger.error("build", AUTOGEN_ERROR, "PCD setting error",
1472 File=self.MetaFile,
1473 ExtraData="\n\tExisted %s PCD %s in:\n\t\t%s\n"
1474 % (PcdFromModule.Type, PcdFromModule.TokenCName, InfName))
1475 # make sure that the "VOID*" kind of datum has MaxDatumSize set
1476 if PcdFromModule.DatumType == "VOID*" and PcdFromModule.MaxDatumSize in [None, '']:
1477 NoDatumTypePcdList.add("%s.%s [%s]" % (PcdFromModule.TokenSpaceGuidCName, PcdFromModule.TokenCName, InfName))
1478 if M.ModuleType in ["PEIM", "PEI_CORE"]:
1479 PcdFromModule.Phase = "PEI"
1480 if PcdFromModule not in self._DynaPcdList_ and PcdFromModule.Type in GenC.gDynamicExPcd:
1481 self._DynaPcdList_.append(PcdFromModule)
1482 elif PcdFromModule not in self._NonDynaPcdList_ and PcdFromModule.Type in TAB_PCDS_PATCHABLE_IN_MODULE:
1483 self._NonDynaPcdList_.append(PcdFromModule)
1484 if PcdFromModule in self._DynaPcdList_ and PcdFromModule.Phase == 'PEI' and PcdFromModule.Type in GenC.gDynamicExPcd:
1485 # Overwrite the phase of any the same PCD existing, if Phase is PEI.
1486 # It is to solve the case that a dynamic PCD used by a PEM module/PEI
1487 # module & DXE module at a same time.
1488 # Overwrite the type of the PCDs in source INF by the type of AsBuild
1489 # INF file as DynamicEx.
1490 Index = self._DynaPcdList_.index(PcdFromModule)
1491 self._DynaPcdList_[Index].Phase = PcdFromModule.Phase
1492 self._DynaPcdList_[Index].Type = PcdFromModule.Type
1493 for PcdFromModule in self._NonDynaPcdList_:
1494 # If a PCD is not listed in the DSC file, but binary INF files used by
1495 # this platform all (that use this PCD) list the PCD in a [PatchPcds]
1496 # section, AND all source INF files used by this platform the build
1497 # that use the PCD list the PCD in either a [Pcds] or [PatchPcds]
1498 # section, then the tools must NOT add the PCD to the Platform's PCD
1499 # Database; the build must assign the access method for this PCD as
1500 # PcdsPatchableInModule.
1501 if PcdFromModule not in self._DynaPcdList_:
1502 continue
1503 Index = self._DynaPcdList_.index(PcdFromModule)
1504 if PcdFromModule.IsFromDsc == False and \
1505 PcdFromModule.Type in TAB_PCDS_PATCHABLE_IN_MODULE and \
1506 PcdFromModule.IsFromBinaryInf == True and \
1507 self._DynaPcdList_[Index].IsFromBinaryInf == False:
1508 Index = self._DynaPcdList_.index(PcdFromModule)
1509 self._DynaPcdList_.remove (self._DynaPcdList_[Index])
1510
1511 # print out error information and break the build, if error found
1512 if len(NoDatumTypePcdList) > 0:
1513 NoDatumTypePcdListString = "\n\t\t".join(NoDatumTypePcdList)
1514 EdkLogger.error("build", AUTOGEN_ERROR, "PCD setting error",
1515 File=self.MetaFile,
1516 ExtraData="\n\tPCD(s) without MaxDatumSize:\n\t\t%s\n"
1517 % NoDatumTypePcdListString)
1518 self._NonDynamicPcdList = self._NonDynaPcdList_
1519 self._DynamicPcdList = self._DynaPcdList_
1520 #
1521 # Sort dynamic PCD list to:
1522 # 1) If PCD's datum type is VOID* and value is unicode string which starts with L, the PCD item should
1523 # try to be put header of dynamicd List
1524 # 2) If PCD is HII type, the PCD item should be put after unicode type PCD
1525 #
1526 # The reason of sorting is make sure the unicode string is in double-byte alignment in string table.
1527 #
1528 UnicodePcdArray = set()
1529 HiiPcdArray = set()
1530 OtherPcdArray = set()
1531 VpdPcdDict = {}
1532 VpdFile = VpdInfoFile.VpdInfoFile()
1533 NeedProcessVpdMapFile = False
1534
1535 for pcd in self.Platform.Pcds.keys():
1536 if pcd not in self._PlatformPcds.keys():
1537 self._PlatformPcds[pcd] = self.Platform.Pcds[pcd]
1538
1539 for item in self._PlatformPcds:
1540 if self._PlatformPcds[item].DatumType and self._PlatformPcds[item].DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1541 self._PlatformPcds[item].DatumType = "VOID*"
1542
1543 if (self.Workspace.ArchList[-1] == self.Arch):
1544 for Pcd in self._DynamicPcdList:
1545 # just pick the a value to determine whether is unicode string type
1546 Sku = Pcd.SkuInfoList[Pcd.SkuInfoList.keys()[0]]
1547 Sku.VpdOffset = Sku.VpdOffset.strip()
1548
1549 if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1550 Pcd.DatumType = "VOID*"
1551
1552 # if found PCD which datum value is unicode string the insert to left size of UnicodeIndex
1553 # if found HII type PCD then insert to right of UnicodeIndex
1554 if Pcd.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD]:
1555 VpdPcdDict[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName)] = Pcd
1556
1557 #Collect DynamicHii PCD values and assign it to DynamicExVpd PCD gEfiMdeModulePkgTokenSpaceGuid.PcdNvStoreDefaultValueBuffer
1558 PcdNvStoreDfBuffer = VpdPcdDict.get(("PcdNvStoreDefaultValueBuffer","gEfiMdeModulePkgTokenSpaceGuid"))
1559 if PcdNvStoreDfBuffer:
1560 self.VariableInfo = self.CollectVariables(self._DynamicPcdList)
1561 vardump = self.VariableInfo.dump()
1562 if vardump:
1563 PcdNvStoreDfBuffer.DefaultValue = vardump
1564 for skuname in PcdNvStoreDfBuffer.SkuInfoList:
1565 PcdNvStoreDfBuffer.SkuInfoList[skuname].DefaultValue = vardump
1566 PcdNvStoreDfBuffer.MaxDatumSize = str(len(vardump.split(",")))
1567
1568 PlatformPcds = self._PlatformPcds.keys()
1569 PlatformPcds.sort()
1570 #
1571 # Add VPD type PCD into VpdFile and determine whether the VPD PCD need to be fixed up.
1572 #
1573 VpdSkuMap = {}
1574 for PcdKey in PlatformPcds:
1575 Pcd = self._PlatformPcds[PcdKey]
1576 if Pcd.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD] and \
1577 PcdKey in VpdPcdDict:
1578 Pcd = VpdPcdDict[PcdKey]
1579 SkuValueMap = {}
1580 DefaultSku = Pcd.SkuInfoList.get('DEFAULT')
1581 if DefaultSku:
1582 PcdValue = DefaultSku.DefaultValue
1583 if PcdValue not in SkuValueMap:
1584 SkuValueMap[PcdValue] = []
1585 VpdFile.Add(Pcd, 'DEFAULT',DefaultSku.VpdOffset)
1586 SkuValueMap[PcdValue].append(DefaultSku)
1587
1588 for (SkuName,Sku) in Pcd.SkuInfoList.items():
1589 Sku.VpdOffset = Sku.VpdOffset.strip()
1590 PcdValue = Sku.DefaultValue
1591 if PcdValue == "":
1592 PcdValue = Pcd.DefaultValue
1593 if Sku.VpdOffset != '*':
1594 if PcdValue.startswith("{"):
1595 Alignment = 8
1596 elif PcdValue.startswith("L"):
1597 Alignment = 2
1598 else:
1599 Alignment = 1
1600 try:
1601 VpdOffset = int(Sku.VpdOffset)
1602 except:
1603 try:
1604 VpdOffset = int(Sku.VpdOffset, 16)
1605 except:
1606 EdkLogger.error("build", FORMAT_INVALID, "Invalid offset value %s for PCD %s.%s." % (Sku.VpdOffset, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1607 if VpdOffset % Alignment != 0:
1608 if PcdValue.startswith("{"):
1609 EdkLogger.warn("build", "The offset value of PCD %s.%s is not 8-byte aligned!" %(Pcd.TokenSpaceGuidCName, Pcd.TokenCName), File=self.MetaFile)
1610 else:
1611 EdkLogger.error("build", FORMAT_INVALID, 'The offset value of PCD %s.%s should be %s-byte aligned.' % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName, Alignment))
1612 if PcdValue not in SkuValueMap:
1613 SkuValueMap[PcdValue] = []
1614 VpdFile.Add(Pcd, SkuName,Sku.VpdOffset)
1615 SkuValueMap[PcdValue].append(Sku)
1616 # if the offset of a VPD is *, then it need to be fixed up by third party tool.
1617 if not NeedProcessVpdMapFile and Sku.VpdOffset == "*":
1618 NeedProcessVpdMapFile = True
1619 if self.Platform.VpdToolGuid is None or self.Platform.VpdToolGuid == '':
1620 EdkLogger.error("Build", FILE_NOT_FOUND, \
1621 "Fail to find third-party BPDG tool to process VPD PCDs. BPDG Guid tool need to be defined in tools_def.txt and VPD_TOOL_GUID need to be provided in DSC file.")
1622
1623 VpdSkuMap[PcdKey] = SkuValueMap
1624 #
1625 # Fix the PCDs define in VPD PCD section that never referenced by module.
1626 # An example is PCD for signature usage.
1627 #
1628 for DscPcd in PlatformPcds:
1629 DscPcdEntry = self._PlatformPcds[DscPcd]
1630 if DscPcdEntry.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD]:
1631 if not (self.Platform.VpdToolGuid is None or self.Platform.VpdToolGuid == ''):
1632 FoundFlag = False
1633 for VpdPcd in VpdFile._VpdArray.keys():
1634 # This PCD has been referenced by module
1635 if (VpdPcd.TokenSpaceGuidCName == DscPcdEntry.TokenSpaceGuidCName) and \
1636 (VpdPcd.TokenCName == DscPcdEntry.TokenCName):
1637 FoundFlag = True
1638
1639 # Not found, it should be signature
1640 if not FoundFlag :
1641 # just pick the a value to determine whether is unicode string type
1642 SkuValueMap = {}
1643 SkuObjList = DscPcdEntry.SkuInfoList.items()
1644 DefaultSku = DscPcdEntry.SkuInfoList.get('DEFAULT')
1645 if DefaultSku:
1646 defaultindex = SkuObjList.index(('DEFAULT',DefaultSku))
1647 SkuObjList[0],SkuObjList[defaultindex] = SkuObjList[defaultindex],SkuObjList[0]
1648 for (SkuName,Sku) in SkuObjList:
1649 Sku.VpdOffset = Sku.VpdOffset.strip()
1650
1651 # Need to iterate DEC pcd information to get the value & datumtype
1652 for eachDec in self.PackageList:
1653 for DecPcd in eachDec.Pcds:
1654 DecPcdEntry = eachDec.Pcds[DecPcd]
1655 if (DecPcdEntry.TokenSpaceGuidCName == DscPcdEntry.TokenSpaceGuidCName) and \
1656 (DecPcdEntry.TokenCName == DscPcdEntry.TokenCName):
1657 # Print warning message to let the developer make a determine.
1658 EdkLogger.warn("build", "Unreferenced vpd pcd used!",
1659 File=self.MetaFile, \
1660 ExtraData = "PCD: %s.%s used in the DSC file %s is unreferenced." \
1661 %(DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName, self.Platform.MetaFile.Path))
1662
1663 DscPcdEntry.DatumType = DecPcdEntry.DatumType
1664 DscPcdEntry.DefaultValue = DecPcdEntry.DefaultValue
1665 DscPcdEntry.TokenValue = DecPcdEntry.TokenValue
1666 DscPcdEntry.TokenSpaceGuidValue = eachDec.Guids[DecPcdEntry.TokenSpaceGuidCName]
1667 # Only fix the value while no value provided in DSC file.
1668 if (Sku.DefaultValue == "" or Sku.DefaultValue==None):
1669 DscPcdEntry.SkuInfoList[DscPcdEntry.SkuInfoList.keys()[0]].DefaultValue = DecPcdEntry.DefaultValue
1670
1671 if DscPcdEntry not in self._DynamicPcdList:
1672 self._DynamicPcdList.append(DscPcdEntry)
1673 Sku.VpdOffset = Sku.VpdOffset.strip()
1674 PcdValue = Sku.DefaultValue
1675 if PcdValue == "":
1676 PcdValue = DscPcdEntry.DefaultValue
1677 if Sku.VpdOffset != '*':
1678 if PcdValue.startswith("{"):
1679 Alignment = 8
1680 elif PcdValue.startswith("L"):
1681 Alignment = 2
1682 else:
1683 Alignment = 1
1684 try:
1685 VpdOffset = int(Sku.VpdOffset)
1686 except:
1687 try:
1688 VpdOffset = int(Sku.VpdOffset, 16)
1689 except:
1690 EdkLogger.error("build", FORMAT_INVALID, "Invalid offset value %s for PCD %s.%s." % (Sku.VpdOffset, DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName))
1691 if VpdOffset % Alignment != 0:
1692 if PcdValue.startswith("{"):
1693 EdkLogger.warn("build", "The offset value of PCD %s.%s is not 8-byte aligned!" %(DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName), File=self.MetaFile)
1694 else:
1695 EdkLogger.error("build", FORMAT_INVALID, 'The offset value of PCD %s.%s should be %s-byte aligned.' % (DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName, Alignment))
1696 if PcdValue not in SkuValueMap:
1697 SkuValueMap[PcdValue] = []
1698 VpdFile.Add(DscPcdEntry, SkuName,Sku.VpdOffset)
1699 SkuValueMap[PcdValue].append(Sku)
1700 if not NeedProcessVpdMapFile and Sku.VpdOffset == "*":
1701 NeedProcessVpdMapFile = True
1702 if DscPcdEntry.DatumType == 'VOID*' and PcdValue.startswith("L"):
1703 UnicodePcdArray.add(DscPcdEntry)
1704 elif len(Sku.VariableName) > 0:
1705 HiiPcdArray.add(DscPcdEntry)
1706 else:
1707 OtherPcdArray.add(DscPcdEntry)
1708
1709 # if the offset of a VPD is *, then it need to be fixed up by third party tool.
1710 VpdSkuMap[DscPcd] = SkuValueMap
1711 if (self.Platform.FlashDefinition is None or self.Platform.FlashDefinition == '') and \
1712 VpdFile.GetCount() != 0:
1713 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE,
1714 "Fail to get FLASH_DEFINITION definition in DSC file %s which is required when DSC contains VPD PCD." % str(self.Platform.MetaFile))
1715
1716 if VpdFile.GetCount() != 0:
1717
1718 self.FixVpdOffset(VpdFile)
1719
1720 self.FixVpdOffset(self.UpdateNVStoreMaxSize(VpdFile))
1721
1722 # Process VPD map file generated by third party BPDG tool
1723 if NeedProcessVpdMapFile:
1724 VpdMapFilePath = os.path.join(self.BuildDir, "FV", "%s.map" % self.Platform.VpdToolGuid)
1725 if os.path.exists(VpdMapFilePath):
1726 VpdFile.Read(VpdMapFilePath)
1727
1728 # Fixup "*" offset
1729 for pcd in VpdSkuMap:
1730 vpdinfo = VpdFile.GetVpdInfo(pcd)
1731 if vpdinfo is None:
1732 # just pick the a value to determine whether is unicode string type
1733 continue
1734 for pcdvalue in VpdSkuMap[pcd]:
1735 for sku in VpdSkuMap[pcd][pcdvalue]:
1736 for item in vpdinfo:
1737 if item[2] == pcdvalue:
1738 sku.VpdOffset = item[1]
1739 else:
1740 EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)
1741
1742 # Delete the DynamicPcdList At the last time enter into this function
1743 for Pcd in self._DynamicPcdList:
1744 # just pick the a value to determine whether is unicode string type
1745 Sku = Pcd.SkuInfoList[Pcd.SkuInfoList.keys()[0]]
1746 Sku.VpdOffset = Sku.VpdOffset.strip()
1747
1748 if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1749 Pcd.DatumType = "VOID*"
1750
1751 PcdValue = Sku.DefaultValue
1752 if Pcd.DatumType == 'VOID*' and PcdValue.startswith("L"):
1753 # if found PCD which datum value is unicode string the insert to left size of UnicodeIndex
1754 UnicodePcdArray.add(Pcd)
1755 elif len(Sku.VariableName) > 0:
1756 # if found HII type PCD then insert to right of UnicodeIndex
1757 HiiPcdArray.add(Pcd)
1758 else:
1759 OtherPcdArray.add(Pcd)
1760 del self._DynamicPcdList[:]
1761 self._DynamicPcdList.extend(list(UnicodePcdArray))
1762 self._DynamicPcdList.extend(list(HiiPcdArray))
1763 self._DynamicPcdList.extend(list(OtherPcdArray))
1764 allskuset = [(SkuName,Sku.SkuId) for pcd in self._DynamicPcdList for (SkuName,Sku) in pcd.SkuInfoList.items()]
1765 for pcd in self._DynamicPcdList:
1766 if len(pcd.SkuInfoList) == 1:
1767 for (SkuName,SkuId) in allskuset:
1768 if type(SkuId) in (str,unicode) and eval(SkuId) == 0 or SkuId == 0:
1769 continue
1770 pcd.SkuInfoList[SkuName] = copy.deepcopy(pcd.SkuInfoList['DEFAULT'])
1771 pcd.SkuInfoList[SkuName].SkuId = SkuId
1772 self.AllPcdList = self._NonDynamicPcdList + self._DynamicPcdList
1773
1774 def FixVpdOffset(self,VpdFile ):
1775 FvPath = os.path.join(self.BuildDir, "FV")
1776 if not os.path.exists(FvPath):
1777 try:
1778 os.makedirs(FvPath)
1779 except:
1780 EdkLogger.error("build", FILE_WRITE_FAILURE, "Fail to create FV folder under %s" % self.BuildDir)
1781
1782 VpdFilePath = os.path.join(FvPath, "%s.txt" % self.Platform.VpdToolGuid)
1783
1784 if VpdFile.Write(VpdFilePath):
1785 # retrieve BPDG tool's path from tool_def.txt according to VPD_TOOL_GUID defined in DSC file.
1786 BPDGToolName = None
1787 for ToolDef in self.ToolDefinition.values():
1788 if ToolDef.has_key("GUID") and ToolDef["GUID"] == self.Platform.VpdToolGuid:
1789 if not ToolDef.has_key("PATH"):
1790 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "PATH attribute was not provided for BPDG guid tool %s in tools_def.txt" % self.Platform.VpdToolGuid)
1791 BPDGToolName = ToolDef["PATH"]
1792 break
1793 # Call third party GUID BPDG tool.
1794 if BPDGToolName is not None:
1795 VpdInfoFile.CallExtenalBPDGTool(BPDGToolName, VpdFilePath)
1796 else:
1797 EdkLogger.error("Build", FILE_NOT_FOUND, "Fail to find third-party BPDG tool to process VPD PCDs. BPDG Guid tool need to be defined in tools_def.txt and VPD_TOOL_GUID need to be provided in DSC file.")
1798
1799 ## Return the platform build data object
1800 def _GetPlatform(self):
1801 if self._Platform is None:
1802 self._Platform = self.BuildDatabase[self.MetaFile, self.Arch, self.BuildTarget, self.ToolChain]
1803 return self._Platform
1804
1805 ## Return platform name
1806 def _GetName(self):
1807 return self.Platform.PlatformName
1808
1809 ## Return the meta file GUID
1810 def _GetGuid(self):
1811 return self.Platform.Guid
1812
1813 ## Return the platform version
1814 def _GetVersion(self):
1815 return self.Platform.Version
1816
1817 ## Return the FDF file name
1818 def _GetFdfFile(self):
1819 if self._FdfFile is None:
1820 if self.Workspace.FdfFile != "":
1821 self._FdfFile= mws.join(self.WorkspaceDir, self.Workspace.FdfFile)
1822 else:
1823 self._FdfFile = ''
1824 return self._FdfFile
1825
1826 ## Return the build output directory platform specifies
1827 def _GetOutputDir(self):
1828 return self.Platform.OutputDirectory
1829
1830 ## Return the directory to store all intermediate and final files built
1831 def _GetBuildDir(self):
1832 if self._BuildDir is None:
1833 if os.path.isabs(self.OutputDir):
1834 self._BuildDir = path.join(
1835 path.abspath(self.OutputDir),
1836 self.BuildTarget + "_" + self.ToolChain,
1837 )
1838 else:
1839 self._BuildDir = path.join(
1840 self.WorkspaceDir,
1841 self.OutputDir,
1842 self.BuildTarget + "_" + self.ToolChain,
1843 )
1844 GlobalData.gBuildDirectory = self._BuildDir
1845 return self._BuildDir
1846
1847 ## Return directory of platform makefile
1848 #
1849 # @retval string Makefile directory
1850 #
1851 def _GetMakeFileDir(self):
1852 if self._MakeFileDir is None:
1853 self._MakeFileDir = path.join(self.BuildDir, self.Arch)
1854 return self._MakeFileDir
1855
1856 ## Return build command string
1857 #
1858 # @retval string Build command string
1859 #
1860 def _GetBuildCommand(self):
1861 if self._BuildCommand is None:
1862 self._BuildCommand = []
1863 if "MAKE" in self.ToolDefinition and "PATH" in self.ToolDefinition["MAKE"]:
1864 self._BuildCommand += SplitOption(self.ToolDefinition["MAKE"]["PATH"])
1865 if "FLAGS" in self.ToolDefinition["MAKE"]:
1866 NewOption = self.ToolDefinition["MAKE"]["FLAGS"].strip()
1867 if NewOption != '':
1868 self._BuildCommand += SplitOption(NewOption)
1869 if "MAKE" in self.EdkIIBuildOption:
1870 if "FLAGS" in self.EdkIIBuildOption["MAKE"]:
1871 Flags = self.EdkIIBuildOption["MAKE"]["FLAGS"]
1872 if Flags.startswith('='):
1873 self._BuildCommand = [self._BuildCommand[0]] + [Flags[1:]]
1874 else:
1875 self._BuildCommand += [Flags]
1876 return self._BuildCommand
1877
1878 ## Get tool chain definition
1879 #
1880 # Get each tool defition for given tool chain from tools_def.txt and platform
1881 #
1882 def _GetToolDefinition(self):
1883 if self._ToolDefinitions is None:
1884 ToolDefinition = self.Workspace.ToolDef.ToolsDefTxtDictionary
1885 if TAB_TOD_DEFINES_COMMAND_TYPE not in self.Workspace.ToolDef.ToolsDefTxtDatabase:
1886 EdkLogger.error('build', RESOURCE_NOT_AVAILABLE, "No tools found in configuration",
1887 ExtraData="[%s]" % self.MetaFile)
1888 self._ToolDefinitions = {}
1889 DllPathList = set()
1890 for Def in ToolDefinition:
1891 Target, Tag, Arch, Tool, Attr = Def.split("_")
1892 if Target != self.BuildTarget or Tag != self.ToolChain or Arch != self.Arch:
1893 continue
1894
1895 Value = ToolDefinition[Def]
1896 # don't record the DLL
1897 if Attr == "DLL":
1898 DllPathList.add(Value)
1899 continue
1900
1901 if Tool not in self._ToolDefinitions:
1902 self._ToolDefinitions[Tool] = {}
1903 self._ToolDefinitions[Tool][Attr] = Value
1904
1905 ToolsDef = ''
1906 if GlobalData.gOptions.SilentMode and "MAKE" in self._ToolDefinitions:
1907 if "FLAGS" not in self._ToolDefinitions["MAKE"]:
1908 self._ToolDefinitions["MAKE"]["FLAGS"] = ""
1909 self._ToolDefinitions["MAKE"]["FLAGS"] += " -s"
1910 MakeFlags = ''
1911 for Tool in self._ToolDefinitions:
1912 for Attr in self._ToolDefinitions[Tool]:
1913 Value = self._ToolDefinitions[Tool][Attr]
1914 if Tool in self.BuildOption and Attr in self.BuildOption[Tool]:
1915 # check if override is indicated
1916 if self.BuildOption[Tool][Attr].startswith('='):
1917 Value = self.BuildOption[Tool][Attr][1:]
1918 else:
1919 if Attr != 'PATH':
1920 Value += " " + self.BuildOption[Tool][Attr]
1921 else:
1922 Value = self.BuildOption[Tool][Attr]
1923
1924 if Attr == "PATH":
1925 # Don't put MAKE definition in the file
1926 if Tool != "MAKE":
1927 ToolsDef += "%s = %s\n" % (Tool, Value)
1928 elif Attr != "DLL":
1929 # Don't put MAKE definition in the file
1930 if Tool == "MAKE":
1931 if Attr == "FLAGS":
1932 MakeFlags = Value
1933 else:
1934 ToolsDef += "%s_%s = %s\n" % (Tool, Attr, Value)
1935 ToolsDef += "\n"
1936
1937 SaveFileOnChange(self.ToolDefinitionFile, ToolsDef)
1938 for DllPath in DllPathList:
1939 os.environ["PATH"] = DllPath + os.pathsep + os.environ["PATH"]
1940 os.environ["MAKE_FLAGS"] = MakeFlags
1941
1942 return self._ToolDefinitions
1943
1944 ## Return the paths of tools
1945 def _GetToolDefFile(self):
1946 if self._ToolDefFile is None:
1947 self._ToolDefFile = os.path.join(self.MakeFileDir, "TOOLS_DEF." + self.Arch)
1948 return self._ToolDefFile
1949
1950 ## Retrieve the toolchain family of given toolchain tag. Default to 'MSFT'.
1951 def _GetToolChainFamily(self):
1952 if self._ToolChainFamily is None:
1953 ToolDefinition = self.Workspace.ToolDef.ToolsDefTxtDatabase
1954 if TAB_TOD_DEFINES_FAMILY not in ToolDefinition \
1955 or self.ToolChain not in ToolDefinition[TAB_TOD_DEFINES_FAMILY] \
1956 or not ToolDefinition[TAB_TOD_DEFINES_FAMILY][self.ToolChain]:
1957 EdkLogger.verbose("No tool chain family found in configuration for %s. Default to MSFT." \
1958 % self.ToolChain)
1959 self._ToolChainFamily = "MSFT"
1960 else:
1961 self._ToolChainFamily = ToolDefinition[TAB_TOD_DEFINES_FAMILY][self.ToolChain]
1962 return self._ToolChainFamily
1963
1964 def _GetBuildRuleFamily(self):
1965 if self._BuildRuleFamily is None:
1966 ToolDefinition = self.Workspace.ToolDef.ToolsDefTxtDatabase
1967 if TAB_TOD_DEFINES_BUILDRULEFAMILY not in ToolDefinition \
1968 or self.ToolChain not in ToolDefinition[TAB_TOD_DEFINES_BUILDRULEFAMILY] \
1969 or not ToolDefinition[TAB_TOD_DEFINES_BUILDRULEFAMILY][self.ToolChain]:
1970 EdkLogger.verbose("No tool chain family found in configuration for %s. Default to MSFT." \
1971 % self.ToolChain)
1972 self._BuildRuleFamily = "MSFT"
1973 else:
1974 self._BuildRuleFamily = ToolDefinition[TAB_TOD_DEFINES_BUILDRULEFAMILY][self.ToolChain]
1975 return self._BuildRuleFamily
1976
1977 ## Return the build options specific for all modules in this platform
1978 def _GetBuildOptions(self):
1979 if self._BuildOption is None:
1980 self._BuildOption = self._ExpandBuildOption(self.Platform.BuildOptions)
1981 return self._BuildOption
1982
1983 ## Return the build options specific for EDK modules in this platform
1984 def _GetEdkBuildOptions(self):
1985 if self._EdkBuildOption is None:
1986 self._EdkBuildOption = self._ExpandBuildOption(self.Platform.BuildOptions, EDK_NAME)
1987 return self._EdkBuildOption
1988
1989 ## Return the build options specific for EDKII modules in this platform
1990 def _GetEdkIIBuildOptions(self):
1991 if self._EdkIIBuildOption is None:
1992 self._EdkIIBuildOption = self._ExpandBuildOption(self.Platform.BuildOptions, EDKII_NAME)
1993 return self._EdkIIBuildOption
1994
1995 ## Parse build_rule.txt in Conf Directory.
1996 #
1997 # @retval BuildRule object
1998 #
1999 def _GetBuildRule(self):
2000 if self._BuildRule is None:
2001 BuildRuleFile = None
2002 if TAB_TAT_DEFINES_BUILD_RULE_CONF in self.Workspace.TargetTxt.TargetTxtDictionary:
2003 BuildRuleFile = self.Workspace.TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_BUILD_RULE_CONF]
2004 if BuildRuleFile in [None, '']:
2005 BuildRuleFile = gDefaultBuildRuleFile
2006 self._BuildRule = BuildRule(BuildRuleFile)
2007 if self._BuildRule._FileVersion == "":
2008 self._BuildRule._FileVersion = AutoGenReqBuildRuleVerNum
2009 else:
2010 if self._BuildRule._FileVersion < AutoGenReqBuildRuleVerNum :
2011 # If Build Rule's version is less than the version number required by the tools, halting the build.
2012 EdkLogger.error("build", AUTOGEN_ERROR,
2013 ExtraData="The version number [%s] of build_rule.txt is less than the version number required by the AutoGen.(the minimum required version number is [%s])"\
2014 % (self._BuildRule._FileVersion, AutoGenReqBuildRuleVerNum))
2015
2016 return self._BuildRule
2017
2018 ## Summarize the packages used by modules in this platform
2019 def _GetPackageList(self):
2020 if self._PackageList is None:
2021 self._PackageList = set()
2022 for La in self.LibraryAutoGenList:
2023 self._PackageList.update(La.DependentPackageList)
2024 for Ma in self.ModuleAutoGenList:
2025 self._PackageList.update(Ma.DependentPackageList)
2026 #Collect package set information from INF of FDF
2027 PkgSet = set()
2028 for ModuleFile in self._AsBuildModuleList:
2029 if ModuleFile in self.Platform.Modules:
2030 continue
2031 ModuleData = self.BuildDatabase[ModuleFile, self.Arch, self.BuildTarget, self.ToolChain]
2032 PkgSet.update(ModuleData.Packages)
2033 self._PackageList = list(self._PackageList) + list (PkgSet)
2034 return self._PackageList
2035
2036 def _GetNonDynamicPcdDict(self):
2037 if self._NonDynamicPcdDict:
2038 return self._NonDynamicPcdDict
2039 for Pcd in self.NonDynamicPcdList:
2040 self._NonDynamicPcdDict[(Pcd.TokenCName,Pcd.TokenSpaceGuidCName)] = Pcd
2041 return self._NonDynamicPcdDict
2042
2043 ## Get list of non-dynamic PCDs
2044 def _GetNonDynamicPcdList(self):
2045 if self._NonDynamicPcdList is None:
2046 self.CollectPlatformDynamicPcds()
2047 return self._NonDynamicPcdList
2048
2049 ## Get list of dynamic PCDs
2050 def _GetDynamicPcdList(self):
2051 if self._DynamicPcdList is None:
2052 self.CollectPlatformDynamicPcds()
2053 return self._DynamicPcdList
2054
2055 ## Generate Token Number for all PCD
2056 def _GetPcdTokenNumbers(self):
2057 if self._PcdTokenNumber is None:
2058 self._PcdTokenNumber = OrderedDict()
2059 TokenNumber = 1
2060 #
2061 # Make the Dynamic and DynamicEx PCD use within different TokenNumber area.
2062 # Such as:
2063 #
2064 # Dynamic PCD:
2065 # TokenNumber 0 ~ 10
2066 # DynamicEx PCD:
2067 # TokeNumber 11 ~ 20
2068 #
2069 for Pcd in self.DynamicPcdList:
2070 if Pcd.Phase == "PEI":
2071 if Pcd.Type in ["Dynamic", "DynamicDefault", "DynamicVpd", "DynamicHii"]:
2072 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
2073 self._PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
2074 TokenNumber += 1
2075
2076 for Pcd in self.DynamicPcdList:
2077 if Pcd.Phase == "PEI":
2078 if Pcd.Type in ["DynamicEx", "DynamicExDefault", "DynamicExVpd", "DynamicExHii"]:
2079 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
2080 self._PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
2081 TokenNumber += 1
2082
2083 for Pcd in self.DynamicPcdList:
2084 if Pcd.Phase == "DXE":
2085 if Pcd.Type in ["Dynamic", "DynamicDefault", "DynamicVpd", "DynamicHii"]:
2086 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
2087 self._PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
2088 TokenNumber += 1
2089
2090 for Pcd in self.DynamicPcdList:
2091 if Pcd.Phase == "DXE":
2092 if Pcd.Type in ["DynamicEx", "DynamicExDefault", "DynamicExVpd", "DynamicExHii"]:
2093 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
2094 self._PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
2095 TokenNumber += 1
2096
2097 for Pcd in self.NonDynamicPcdList:
2098 self._PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
2099 TokenNumber += 1
2100 return self._PcdTokenNumber
2101
2102 ## Summarize ModuleAutoGen objects of all modules/libraries to be built for this platform
2103 def _GetAutoGenObjectList(self):
2104 self._ModuleAutoGenList = []
2105 self._LibraryAutoGenList = []
2106 for ModuleFile in self.Platform.Modules:
2107 Ma = ModuleAutoGen(
2108 self.Workspace,
2109 ModuleFile,
2110 self.BuildTarget,
2111 self.ToolChain,
2112 self.Arch,
2113 self.MetaFile
2114 )
2115 if Ma not in self._ModuleAutoGenList:
2116 self._ModuleAutoGenList.append(Ma)
2117 for La in Ma.LibraryAutoGenList:
2118 if La not in self._LibraryAutoGenList:
2119 self._LibraryAutoGenList.append(La)
2120 if Ma not in La._ReferenceModules:
2121 La._ReferenceModules.append(Ma)
2122
2123 ## Summarize ModuleAutoGen objects of all modules to be built for this platform
2124 def _GetModuleAutoGenList(self):
2125 if self._ModuleAutoGenList is None:
2126 self._GetAutoGenObjectList()
2127 return self._ModuleAutoGenList
2128
2129 ## Summarize ModuleAutoGen objects of all libraries to be built for this platform
2130 def _GetLibraryAutoGenList(self):
2131 if self._LibraryAutoGenList is None:
2132 self._GetAutoGenObjectList()
2133 return self._LibraryAutoGenList
2134
2135 ## Test if a module is supported by the platform
2136 #
2137 # An error will be raised directly if the module or its arch is not supported
2138 # by the platform or current configuration
2139 #
2140 def ValidModule(self, Module):
2141 return Module in self.Platform.Modules or Module in self.Platform.LibraryInstances \
2142 or Module in self._AsBuildModuleList
2143
2144 ## Resolve the library classes in a module to library instances
2145 #
2146 # This method will not only resolve library classes but also sort the library
2147 # instances according to the dependency-ship.
2148 #
2149 # @param Module The module from which the library classes will be resolved
2150 #
2151 # @retval library_list List of library instances sorted
2152 #
2153 def ApplyLibraryInstance(self, Module):
2154 # Cover the case that the binary INF file is list in the FDF file but not DSC file, return empty list directly
2155 if str(Module) not in self.Platform.Modules:
2156 return []
2157
2158 ModuleType = Module.ModuleType
2159
2160 # for overridding library instances with module specific setting
2161 PlatformModule = self.Platform.Modules[str(Module)]
2162
2163 # add forced library instances (specified under LibraryClasses sections)
2164 #
2165 # If a module has a MODULE_TYPE of USER_DEFINED,
2166 # do not link in NULL library class instances from the global [LibraryClasses.*] sections.
2167 #
2168 if Module.ModuleType != SUP_MODULE_USER_DEFINED:
2169 for LibraryClass in self.Platform.LibraryClasses.GetKeys():
2170 if LibraryClass.startswith("NULL") and self.Platform.LibraryClasses[LibraryClass, Module.ModuleType]:
2171 Module.LibraryClasses[LibraryClass] = self.Platform.LibraryClasses[LibraryClass, Module.ModuleType]
2172
2173 # add forced library instances (specified in module overrides)
2174 for LibraryClass in PlatformModule.LibraryClasses:
2175 if LibraryClass.startswith("NULL"):
2176 Module.LibraryClasses[LibraryClass] = PlatformModule.LibraryClasses[LibraryClass]
2177
2178 # EdkII module
2179 LibraryConsumerList = [Module]
2180 Constructor = []
2181 ConsumedByList = OrderedDict()
2182 LibraryInstance = OrderedDict()
2183
2184 EdkLogger.verbose("")
2185 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), self.Arch))
2186 while len(LibraryConsumerList) > 0:
2187 M = LibraryConsumerList.pop()
2188 for LibraryClassName in M.LibraryClasses:
2189 if LibraryClassName not in LibraryInstance:
2190 # override library instance for this module
2191 if LibraryClassName in PlatformModule.LibraryClasses:
2192 LibraryPath = PlatformModule.LibraryClasses[LibraryClassName]
2193 else:
2194 LibraryPath = self.Platform.LibraryClasses[LibraryClassName, ModuleType]
2195 if LibraryPath is None or LibraryPath == "":
2196 LibraryPath = M.LibraryClasses[LibraryClassName]
2197 if LibraryPath is None or LibraryPath == "":
2198 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
2199 "Instance of library class [%s] is not found" % LibraryClassName,
2200 File=self.MetaFile,
2201 ExtraData="in [%s] [%s]\n\tconsumed by module [%s]" % (str(M), self.Arch, str(Module)))
2202
2203 LibraryModule = self.BuildDatabase[LibraryPath, self.Arch, self.BuildTarget, self.ToolChain]
2204 # for those forced library instance (NULL library), add a fake library class
2205 if LibraryClassName.startswith("NULL"):
2206 LibraryModule.LibraryClass.append(LibraryClassObject(LibraryClassName, [ModuleType]))
2207 elif LibraryModule.LibraryClass is None \
2208 or len(LibraryModule.LibraryClass) == 0 \
2209 or (ModuleType != 'USER_DEFINED'
2210 and ModuleType not in LibraryModule.LibraryClass[0].SupModList):
2211 # only USER_DEFINED can link against any library instance despite of its SupModList
2212 EdkLogger.error("build", OPTION_MISSING,
2213 "Module type [%s] is not supported by library instance [%s]" \
2214 % (ModuleType, LibraryPath), File=self.MetaFile,
2215 ExtraData="consumed by [%s]" % str(Module))
2216
2217 LibraryInstance[LibraryClassName] = LibraryModule
2218 LibraryConsumerList.append(LibraryModule)
2219 EdkLogger.verbose("\t" + str(LibraryClassName) + " : " + str(LibraryModule))
2220 else:
2221 LibraryModule = LibraryInstance[LibraryClassName]
2222
2223 if LibraryModule is None:
2224 continue
2225
2226 if LibraryModule.ConstructorList != [] and LibraryModule not in Constructor:
2227 Constructor.append(LibraryModule)
2228
2229 if LibraryModule not in ConsumedByList:
2230 ConsumedByList[LibraryModule] = []
2231 # don't add current module itself to consumer list
2232 if M != Module:
2233 if M in ConsumedByList[LibraryModule]:
2234 continue
2235 ConsumedByList[LibraryModule].append(M)
2236 #
2237 # Initialize the sorted output list to the empty set
2238 #
2239 SortedLibraryList = []
2240 #
2241 # Q <- Set of all nodes with no incoming edges
2242 #
2243 LibraryList = [] #LibraryInstance.values()
2244 Q = []
2245 for LibraryClassName in LibraryInstance:
2246 M = LibraryInstance[LibraryClassName]
2247 LibraryList.append(M)
2248 if ConsumedByList[M] == []:
2249 Q.append(M)
2250
2251 #
2252 # start the DAG algorithm
2253 #
2254 while True:
2255 EdgeRemoved = True
2256 while Q == [] and EdgeRemoved:
2257 EdgeRemoved = False
2258 # for each node Item with a Constructor
2259 for Item in LibraryList:
2260 if Item not in Constructor:
2261 continue
2262 # for each Node without a constructor with an edge e from Item to Node
2263 for Node in ConsumedByList[Item]:
2264 if Node in Constructor:
2265 continue
2266 # remove edge e from the graph if Node has no constructor
2267 ConsumedByList[Item].remove(Node)
2268 EdgeRemoved = True
2269 if ConsumedByList[Item] == []:
2270 # insert Item into Q
2271 Q.insert(0, Item)
2272 break
2273 if Q != []:
2274 break
2275 # DAG is done if there's no more incoming edge for all nodes
2276 if Q == []:
2277 break
2278
2279 # remove node from Q
2280 Node = Q.pop()
2281 # output Node
2282 SortedLibraryList.append(Node)
2283
2284 # for each node Item with an edge e from Node to Item do
2285 for Item in LibraryList:
2286 if Node not in ConsumedByList[Item]:
2287 continue
2288 # remove edge e from the graph
2289 ConsumedByList[Item].remove(Node)
2290
2291 if ConsumedByList[Item] != []:
2292 continue
2293 # insert Item into Q, if Item has no other incoming edges
2294 Q.insert(0, Item)
2295
2296 #
2297 # if any remaining node Item in the graph has a constructor and an incoming edge, then the graph has a cycle
2298 #
2299 for Item in LibraryList:
2300 if ConsumedByList[Item] != [] and Item in Constructor and len(Constructor) > 1:
2301 ErrorMessage = "\tconsumed by " + "\n\tconsumed by ".join([str(L) for L in ConsumedByList[Item]])
2302 EdkLogger.error("build", BUILD_ERROR, 'Library [%s] with constructors has a cycle' % str(Item),
2303 ExtraData=ErrorMessage, File=self.MetaFile)
2304 if Item not in SortedLibraryList:
2305 SortedLibraryList.append(Item)
2306
2307 #
2308 # Build the list of constructor and destructir names
2309 # The DAG Topo sort produces the destructor order, so the list of constructors must generated in the reverse order
2310 #
2311 SortedLibraryList.reverse()
2312 return SortedLibraryList
2313
2314
2315 ## Override PCD setting (type, value, ...)
2316 #
2317 # @param ToPcd The PCD to be overrided
2318 # @param FromPcd The PCD overrideing from
2319 #
2320 def _OverridePcd(self, ToPcd, FromPcd, Module=""):
2321 #
2322 # in case there's PCDs coming from FDF file, which have no type given.
2323 # at this point, ToPcd.Type has the type found from dependent
2324 # package
2325 #
2326 TokenCName = ToPcd.TokenCName
2327 for PcdItem in GlobalData.MixedPcd:
2328 if (ToPcd.TokenCName, ToPcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
2329 TokenCName = PcdItem[0]
2330 break
2331 if FromPcd is not None:
2332 if ToPcd.Pending and FromPcd.Type not in [None, '']:
2333 ToPcd.Type = FromPcd.Type
2334 elif (ToPcd.Type not in [None, '']) and (FromPcd.Type not in [None, ''])\
2335 and (ToPcd.Type != FromPcd.Type) and (ToPcd.Type in FromPcd.Type):
2336 if ToPcd.Type.strip() == "DynamicEx":
2337 ToPcd.Type = FromPcd.Type
2338 elif ToPcd.Type not in [None, ''] and FromPcd.Type not in [None, ''] \
2339 and ToPcd.Type != FromPcd.Type:
2340 EdkLogger.error("build", OPTION_CONFLICT, "Mismatched PCD type",
2341 ExtraData="%s.%s is defined as [%s] in module %s, but as [%s] in platform."\
2342 % (ToPcd.TokenSpaceGuidCName, TokenCName,
2343 ToPcd.Type, Module, FromPcd.Type),
2344 File=self.MetaFile)
2345
2346 if FromPcd.MaxDatumSize:
2347 ToPcd.MaxDatumSize = FromPcd.MaxDatumSize
2348 ToPcd.MaxSizeUserSet = FromPcd.MaxDatumSize
2349 if FromPcd.DefaultValue:
2350 ToPcd.DefaultValue = FromPcd.DefaultValue
2351 if FromPcd.TokenValue:
2352 ToPcd.TokenValue = FromPcd.TokenValue
2353 if FromPcd.DatumType:
2354 ToPcd.DatumType = FromPcd.DatumType
2355 if FromPcd.SkuInfoList:
2356 ToPcd.SkuInfoList = FromPcd.SkuInfoList
2357 # Add Flexible PCD format parse
2358 if ToPcd.DefaultValue:
2359 try:
2360 ToPcd.DefaultValue = ValueExpressionEx(ToPcd.DefaultValue, ToPcd.DatumType, self._GuidDict)(True)
2361 except BadExpression, Value:
2362 EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(ToPcd.TokenSpaceGuidCName, ToPcd.TokenCName, ToPcd.DefaultValue, Value),
2363 File=self.MetaFile)
2364
2365 # check the validation of datum
2366 IsValid, Cause = CheckPcdDatum(ToPcd.DatumType, ToPcd.DefaultValue)
2367 if not IsValid:
2368 EdkLogger.error('build', FORMAT_INVALID, Cause, File=self.MetaFile,
2369 ExtraData="%s.%s" % (ToPcd.TokenSpaceGuidCName, TokenCName))
2370 ToPcd.validateranges = FromPcd.validateranges
2371 ToPcd.validlists = FromPcd.validlists
2372 ToPcd.expressions = FromPcd.expressions
2373
2374 if FromPcd is not None and ToPcd.DatumType == "VOID*" and ToPcd.MaxDatumSize in ['', None]:
2375 EdkLogger.debug(EdkLogger.DEBUG_9, "No MaxDatumSize specified for PCD %s.%s" \
2376 % (ToPcd.TokenSpaceGuidCName, TokenCName))
2377 Value = ToPcd.DefaultValue
2378 if Value in [None, '']:
2379 ToPcd.MaxDatumSize = '1'
2380 elif Value[0] == 'L':
2381 ToPcd.MaxDatumSize = str((len(Value) - 2) * 2)
2382 elif Value[0] == '{':
2383 ToPcd.MaxDatumSize = str(len(Value.split(',')))
2384 else:
2385 ToPcd.MaxDatumSize = str(len(Value) - 1)
2386
2387 # apply default SKU for dynamic PCDS if specified one is not available
2388 if (ToPcd.Type in PCD_DYNAMIC_TYPE_LIST or ToPcd.Type in PCD_DYNAMIC_EX_TYPE_LIST) \
2389 and ToPcd.SkuInfoList in [None, {}, '']:
2390 if self.Platform.SkuName in self.Platform.SkuIds:
2391 SkuName = self.Platform.SkuName
2392 else:
2393 SkuName = 'DEFAULT'
2394 ToPcd.SkuInfoList = {
2395 SkuName : SkuInfoClass(SkuName, self.Platform.SkuIds[SkuName][0], '', '', '', '', '', ToPcd.DefaultValue)
2396 }
2397
2398 ## Apply PCD setting defined platform to a module
2399 #
2400 # @param Module The module from which the PCD setting will be overrided
2401 #
2402 # @retval PCD_list The list PCDs with settings from platform
2403 #
2404 def ApplyPcdSetting(self, Module, Pcds):
2405 # for each PCD in module
2406 for Name, Guid in Pcds:
2407 PcdInModule = Pcds[Name, Guid]
2408 # find out the PCD setting in platform
2409 if (Name, Guid) in self.Platform.Pcds:
2410 PcdInPlatform = self.Platform.Pcds[Name, Guid]
2411 else:
2412 PcdInPlatform = None
2413 # then override the settings if any
2414 self._OverridePcd(PcdInModule, PcdInPlatform, Module)
2415 # resolve the VariableGuid value
2416 for SkuId in PcdInModule.SkuInfoList:
2417 Sku = PcdInModule.SkuInfoList[SkuId]
2418 if Sku.VariableGuid == '': continue
2419 Sku.VariableGuidValue = GuidValue(Sku.VariableGuid, self.PackageList, self.MetaFile.Path)
2420 if Sku.VariableGuidValue is None:
2421 PackageList = "\n\t".join([str(P) for P in self.PackageList])
2422 EdkLogger.error(
2423 'build',
2424 RESOURCE_NOT_AVAILABLE,
2425 "Value of GUID [%s] is not found in" % Sku.VariableGuid,
2426 ExtraData=PackageList + "\n\t(used with %s.%s from module %s)" \
2427 % (Guid, Name, str(Module)),
2428 File=self.MetaFile
2429 )
2430
2431 # override PCD settings with module specific setting
2432 if Module in self.Platform.Modules:
2433 PlatformModule = self.Platform.Modules[str(Module)]
2434 for Key in PlatformModule.Pcds:
2435 Flag = False
2436 if Key in Pcds:
2437 ToPcd = Pcds[Key]
2438 Flag = True
2439 elif Key in GlobalData.MixedPcd:
2440 for PcdItem in GlobalData.MixedPcd[Key]:
2441 if PcdItem in Pcds:
2442 ToPcd = Pcds[PcdItem]
2443 Flag = True
2444 break
2445 if Flag:
2446 self._OverridePcd(ToPcd, PlatformModule.Pcds[Key], Module)
2447 # use PCD value to calculate the MaxDatumSize when it is not specified
2448 for Name, Guid in Pcds:
2449 Pcd = Pcds[Name, Guid]
2450 if Pcd.DatumType == "VOID*" and Pcd.MaxDatumSize in ['', None]:
2451 Pcd.MaxSizeUserSet = None
2452 Value = Pcd.DefaultValue
2453 if Value in [None, '']:
2454 Pcd.MaxDatumSize = '1'
2455 elif Value[0] == 'L':
2456 Pcd.MaxDatumSize = str((len(Value) - 2) * 2)
2457 elif Value[0] == '{':
2458 Pcd.MaxDatumSize = str(len(Value.split(',')))
2459 else:
2460 Pcd.MaxDatumSize = str(len(Value) - 1)
2461 return Pcds.values()
2462
2463 ## Resolve library names to library modules
2464 #
2465 # (for Edk.x modules)
2466 #
2467 # @param Module The module from which the library names will be resolved
2468 #
2469 # @retval library_list The list of library modules
2470 #
2471 def ResolveLibraryReference(self, Module):
2472 EdkLogger.verbose("")
2473 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), self.Arch))
2474 LibraryConsumerList = [Module]
2475
2476 # "CompilerStub" is a must for Edk modules
2477 if Module.Libraries:
2478 Module.Libraries.append("CompilerStub")
2479 LibraryList = []
2480 while len(LibraryConsumerList) > 0:
2481 M = LibraryConsumerList.pop()
2482 for LibraryName in M.Libraries:
2483 Library = self.Platform.LibraryClasses[LibraryName, ':dummy:']
2484 if Library is None:
2485 for Key in self.Platform.LibraryClasses.data.keys():
2486 if LibraryName.upper() == Key.upper():
2487 Library = self.Platform.LibraryClasses[Key, ':dummy:']
2488 break
2489 if Library is None:
2490 EdkLogger.warn("build", "Library [%s] is not found" % LibraryName, File=str(M),
2491 ExtraData="\t%s [%s]" % (str(Module), self.Arch))
2492 continue
2493
2494 if Library not in LibraryList:
2495 LibraryList.append(Library)
2496 LibraryConsumerList.append(Library)
2497 EdkLogger.verbose("\t" + LibraryName + " : " + str(Library) + ' ' + str(type(Library)))
2498 return LibraryList
2499
2500 ## Calculate the priority value of the build option
2501 #
2502 # @param Key Build option definition contain: TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
2503 #
2504 # @retval Value Priority value based on the priority list.
2505 #
2506 def CalculatePriorityValue(self, Key):
2507 Target, ToolChain, Arch, CommandType, Attr = Key.split('_')
2508 PriorityValue = 0x11111
2509 if Target == "*":
2510 PriorityValue &= 0x01111
2511 if ToolChain == "*":
2512 PriorityValue &= 0x10111
2513 if Arch == "*":
2514 PriorityValue &= 0x11011
2515 if CommandType == "*":
2516 PriorityValue &= 0x11101
2517 if Attr == "*":
2518 PriorityValue &= 0x11110
2519
2520 return self.PrioList["0x%0.5x" % PriorityValue]
2521
2522
2523 ## Expand * in build option key
2524 #
2525 # @param Options Options to be expanded
2526 #
2527 # @retval options Options expanded
2528 #
2529 def _ExpandBuildOption(self, Options, ModuleStyle=None):
2530 BuildOptions = {}
2531 FamilyMatch = False
2532 FamilyIsNull = True
2533
2534 OverrideList = {}
2535 #
2536 # Construct a list contain the build options which need override.
2537 #
2538 for Key in Options:
2539 #
2540 # Key[0] -- tool family
2541 # Key[1] -- TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
2542 #
2543 if (Key[0] == self.BuildRuleFamily and
2544 (ModuleStyle is None or len(Key) < 3 or (len(Key) > 2 and Key[2] == ModuleStyle))):
2545 Target, ToolChain, Arch, CommandType, Attr = Key[1].split('_')
2546 if Target == self.BuildTarget or Target == "*":
2547 if ToolChain == self.ToolChain or ToolChain == "*":
2548 if Arch == self.Arch or Arch == "*":
2549 if Options[Key].startswith("="):
2550 if OverrideList.get(Key[1]) is not None:
2551 OverrideList.pop(Key[1])
2552 OverrideList[Key[1]] = Options[Key]
2553
2554 #
2555 # Use the highest priority value.
2556 #
2557 if (len(OverrideList) >= 2):
2558 KeyList = OverrideList.keys()
2559 for Index in range(len(KeyList)):
2560 NowKey = KeyList[Index]
2561 Target1, ToolChain1, Arch1, CommandType1, Attr1 = NowKey.split("_")
2562 for Index1 in range(len(KeyList) - Index - 1):
2563 NextKey = KeyList[Index1 + Index + 1]
2564 #
2565 # Compare two Key, if one is included by another, choose the higher priority one
2566 #
2567 Target2, ToolChain2, Arch2, CommandType2, Attr2 = NextKey.split("_")
2568 if Target1 == Target2 or Target1 == "*" or Target2 == "*":
2569 if ToolChain1 == ToolChain2 or ToolChain1 == "*" or ToolChain2 == "*":
2570 if Arch1 == Arch2 or Arch1 == "*" or Arch2 == "*":
2571 if CommandType1 == CommandType2 or CommandType1 == "*" or CommandType2 == "*":
2572 if Attr1 == Attr2 or Attr1 == "*" or Attr2 == "*":
2573 if self.CalculatePriorityValue(NowKey) > self.CalculatePriorityValue(NextKey):
2574 if Options.get((self.BuildRuleFamily, NextKey)) is not None:
2575 Options.pop((self.BuildRuleFamily, NextKey))
2576 else:
2577 if Options.get((self.BuildRuleFamily, NowKey)) is not None:
2578 Options.pop((self.BuildRuleFamily, NowKey))
2579
2580 for Key in Options:
2581 if ModuleStyle is not None and len (Key) > 2:
2582 # Check Module style is EDK or EDKII.
2583 # Only append build option for the matched style module.
2584 if ModuleStyle == EDK_NAME and Key[2] != EDK_NAME:
2585 continue
2586 elif ModuleStyle == EDKII_NAME and Key[2] != EDKII_NAME:
2587 continue
2588 Family = Key[0]
2589 Target, Tag, Arch, Tool, Attr = Key[1].split("_")
2590 # if tool chain family doesn't match, skip it
2591 if Tool in self.ToolDefinition and Family != "":
2592 FamilyIsNull = False
2593 if self.ToolDefinition[Tool].get(TAB_TOD_DEFINES_BUILDRULEFAMILY, "") != "":
2594 if Family != self.ToolDefinition[Tool][TAB_TOD_DEFINES_BUILDRULEFAMILY]:
2595 continue
2596 elif Family != self.ToolDefinition[Tool][TAB_TOD_DEFINES_FAMILY]:
2597 continue
2598 FamilyMatch = True
2599 # expand any wildcard
2600 if Target == "*" or Target == self.BuildTarget:
2601 if Tag == "*" or Tag == self.ToolChain:
2602 if Arch == "*" or Arch == self.Arch:
2603 if Tool not in BuildOptions:
2604 BuildOptions[Tool] = {}
2605 if Attr != "FLAGS" or Attr not in BuildOptions[Tool] or Options[Key].startswith('='):
2606 BuildOptions[Tool][Attr] = Options[Key]
2607 else:
2608 # append options for the same tool except PATH
2609 if Attr != 'PATH':
2610 BuildOptions[Tool][Attr] += " " + Options[Key]
2611 else:
2612 BuildOptions[Tool][Attr] = Options[Key]
2613 # Build Option Family has been checked, which need't to be checked again for family.
2614 if FamilyMatch or FamilyIsNull:
2615 return BuildOptions
2616
2617 for Key in Options:
2618 if ModuleStyle is not None and len (Key) > 2:
2619 # Check Module style is EDK or EDKII.
2620 # Only append build option for the matched style module.
2621 if ModuleStyle == EDK_NAME and Key[2] != EDK_NAME:
2622 continue
2623 elif ModuleStyle == EDKII_NAME and Key[2] != EDKII_NAME:
2624 continue
2625 Family = Key[0]
2626 Target, Tag, Arch, Tool, Attr = Key[1].split("_")
2627 # if tool chain family doesn't match, skip it
2628 if Tool not in self.ToolDefinition or Family == "":
2629 continue
2630 # option has been added before
2631 if Family != self.ToolDefinition[Tool][TAB_TOD_DEFINES_FAMILY]:
2632 continue
2633
2634 # expand any wildcard
2635 if Target == "*" or Target == self.BuildTarget:
2636 if Tag == "*" or Tag == self.ToolChain:
2637 if Arch == "*" or Arch == self.Arch:
2638 if Tool not in BuildOptions:
2639 BuildOptions[Tool] = {}
2640 if Attr != "FLAGS" or Attr not in BuildOptions[Tool] or Options[Key].startswith('='):
2641 BuildOptions[Tool][Attr] = Options[Key]
2642 else:
2643 # append options for the same tool except PATH
2644 if Attr != 'PATH':
2645 BuildOptions[Tool][Attr] += " " + Options[Key]
2646 else:
2647 BuildOptions[Tool][Attr] = Options[Key]
2648 return BuildOptions
2649
2650 ## Append build options in platform to a module
2651 #
2652 # @param Module The module to which the build options will be appened
2653 #
2654 # @retval options The options appended with build options in platform
2655 #
2656 def ApplyBuildOption(self, Module):
2657 # Get the different options for the different style module
2658 if Module.AutoGenVersion < 0x00010005:
2659 PlatformOptions = self.EdkBuildOption
2660 ModuleTypeOptions = self.Platform.GetBuildOptionsByModuleType(EDK_NAME, Module.ModuleType)
2661 else:
2662 PlatformOptions = self.EdkIIBuildOption
2663 ModuleTypeOptions = self.Platform.GetBuildOptionsByModuleType(EDKII_NAME, Module.ModuleType)
2664 ModuleTypeOptions = self._ExpandBuildOption(ModuleTypeOptions)
2665 ModuleOptions = self._ExpandBuildOption(Module.BuildOptions)
2666 if Module in self.Platform.Modules:
2667 PlatformModule = self.Platform.Modules[str(Module)]
2668 PlatformModuleOptions = self._ExpandBuildOption(PlatformModule.BuildOptions)
2669 else:
2670 PlatformModuleOptions = {}
2671
2672 BuildRuleOrder = None
2673 for Options in [self.ToolDefinition, ModuleOptions, PlatformOptions, ModuleTypeOptions, PlatformModuleOptions]:
2674 for Tool in Options:
2675 for Attr in Options[Tool]:
2676 if Attr == TAB_TOD_DEFINES_BUILDRULEORDER:
2677 BuildRuleOrder = Options[Tool][Attr]
2678
2679 AllTools = set(ModuleOptions.keys() + PlatformOptions.keys() +