]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/AutoGen.py
Revert BaseTools: PYTHON3 migration
[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 # Copyright (c) 2018, Hewlett Packard Enterprise Development, L.P.<BR>
6 #
7 # This program and the accompanying materials
8 # are licensed and made available under the terms and conditions of the BSD License
9 # which accompanies this distribution. The full text of the license may be found at
10 # http://opensource.org/licenses/bsd-license.php
11 #
12 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 #
15
16 ## Import Modules
17 #
18 from __future__ import print_function
19 from __future__ import absolute_import
20 import Common.LongFilePathOs as os
21 import re
22 import os.path as path
23 import copy
24 import uuid
25
26 from . import GenC
27 from . import GenMake
28 from . import GenDepex
29 from io import BytesIO
30
31 from .StrGather import *
32 from .BuildEngine import BuildRule
33
34 from Common.LongFilePathSupport import CopyLongFilePath
35 from Common.BuildToolError import *
36 from Common.DataType import *
37 from Common.Misc import *
38 from Common.StringUtils import *
39 import Common.GlobalData as GlobalData
40 from GenFds.FdfParser import *
41 from CommonDataClass.CommonClass import SkuInfoClass
42 from GenPatchPcdTable.GenPatchPcdTable import parsePcdInfoFromMapFile
43 import Common.VpdInfoFile as VpdInfoFile
44 from .GenPcdDb import CreatePcdDatabaseCode
45 from Workspace.MetaFileCommentParser import UsageList
46 from Workspace.WorkspaceCommon import GetModuleLibInstances
47 from Common.MultipleWorkspace import MultipleWorkspace as mws
48 from . import InfSectionParser
49 import datetime
50 import hashlib
51 from .GenVar import VariableMgr, var_info
52 from collections import OrderedDict
53 from collections import defaultdict
54 from Workspace.WorkspaceCommon import OrderedListDict
55
56 from Common.caching import cached_property, cached_class_function
57
58 ## Regular expression for splitting Dependency Expression string into tokens
59 gDepexTokenPattern = re.compile("(\(|\)|\w+| \S+\.inf)")
60
61 ## Regular expression for match: PCD(xxxx.yyy)
62 gPCDAsGuidPattern = re.compile(r"^PCD\(.+\..+\)$")
63
64 #
65 # Regular expression for finding Include Directories, the difference between MSFT and INTEL/GCC/RVCT
66 # is the former use /I , the Latter used -I to specify include directories
67 #
68 gBuildOptIncludePatternMsft = re.compile(r"(?:.*?)/I[ \t]*([^ ]*)", re.MULTILINE | re.DOTALL)
69 gBuildOptIncludePatternOther = re.compile(r"(?:.*?)-I[ \t]*([^ ]*)", re.MULTILINE | re.DOTALL)
70
71 #
72 # Match name = variable
73 #
74 gEfiVarStoreNamePattern = re.compile("\s*name\s*=\s*(\w+)")
75 #
76 # The format of guid in efivarstore statement likes following and must be correct:
77 # guid = {0xA04A27f4, 0xDF00, 0x4D42, {0xB5, 0x52, 0x39, 0x51, 0x13, 0x02, 0x11, 0x3D}}
78 #
79 gEfiVarStoreGuidPattern = re.compile("\s*guid\s*=\s*({.*?{.*?}\s*})")
80
81 ## Mapping Makefile type
82 gMakeTypeMap = {TAB_COMPILER_MSFT:"nmake", "GCC":"gmake"}
83
84
85 ## Build rule configuration file
86 gDefaultBuildRuleFile = 'build_rule.txt'
87
88 ## Tools definition configuration file
89 gDefaultToolsDefFile = 'tools_def.txt'
90
91 ## Build rule default version
92 AutoGenReqBuildRuleVerNum = "0.1"
93
94 ## default file name for AutoGen
95 gAutoGenCodeFileName = "AutoGen.c"
96 gAutoGenHeaderFileName = "AutoGen.h"
97 gAutoGenStringFileName = "%(module_name)sStrDefs.h"
98 gAutoGenStringFormFileName = "%(module_name)sStrDefs.hpk"
99 gAutoGenDepexFileName = "%(module_name)s.depex"
100 gAutoGenImageDefFileName = "%(module_name)sImgDefs.h"
101 gAutoGenIdfFileName = "%(module_name)sIdf.hpk"
102 gInfSpecVersion = "0x00010017"
103
104 #
105 # Template string to generic AsBuilt INF
106 #
107 gAsBuiltInfHeaderString = TemplateString("""${header_comments}
108
109 # DO NOT EDIT
110 # FILE auto-generated
111
112 [Defines]
113 INF_VERSION = ${module_inf_version}
114 BASE_NAME = ${module_name}
115 FILE_GUID = ${module_guid}
116 MODULE_TYPE = ${module_module_type}${BEGIN}
117 VERSION_STRING = ${module_version_string}${END}${BEGIN}
118 PCD_IS_DRIVER = ${pcd_is_driver_string}${END}${BEGIN}
119 UEFI_SPECIFICATION_VERSION = ${module_uefi_specification_version}${END}${BEGIN}
120 PI_SPECIFICATION_VERSION = ${module_pi_specification_version}${END}${BEGIN}
121 ENTRY_POINT = ${module_entry_point}${END}${BEGIN}
122 UNLOAD_IMAGE = ${module_unload_image}${END}${BEGIN}
123 CONSTRUCTOR = ${module_constructor}${END}${BEGIN}
124 DESTRUCTOR = ${module_destructor}${END}${BEGIN}
125 SHADOW = ${module_shadow}${END}${BEGIN}
126 PCI_VENDOR_ID = ${module_pci_vendor_id}${END}${BEGIN}
127 PCI_DEVICE_ID = ${module_pci_device_id}${END}${BEGIN}
128 PCI_CLASS_CODE = ${module_pci_class_code}${END}${BEGIN}
129 PCI_REVISION = ${module_pci_revision}${END}${BEGIN}
130 BUILD_NUMBER = ${module_build_number}${END}${BEGIN}
131 SPEC = ${module_spec}${END}${BEGIN}
132 UEFI_HII_RESOURCE_SECTION = ${module_uefi_hii_resource_section}${END}${BEGIN}
133 MODULE_UNI_FILE = ${module_uni_file}${END}
134
135 [Packages.${module_arch}]${BEGIN}
136 ${package_item}${END}
137
138 [Binaries.${module_arch}]${BEGIN}
139 ${binary_item}${END}
140
141 [PatchPcd.${module_arch}]${BEGIN}
142 ${patchablepcd_item}
143 ${END}
144
145 [Protocols.${module_arch}]${BEGIN}
146 ${protocol_item}
147 ${END}
148
149 [Ppis.${module_arch}]${BEGIN}
150 ${ppi_item}
151 ${END}
152
153 [Guids.${module_arch}]${BEGIN}
154 ${guid_item}
155 ${END}
156
157 [PcdEx.${module_arch}]${BEGIN}
158 ${pcd_item}
159 ${END}
160
161 [LibraryClasses.${module_arch}]
162 ## @LIB_INSTANCES${BEGIN}
163 # ${libraryclasses_item}${END}
164
165 ${depexsection_item}
166
167 ${userextension_tianocore_item}
168
169 ${tail_comments}
170
171 [BuildOptions.${module_arch}]
172 ## @AsBuilt${BEGIN}
173 ## ${flags_item}${END}
174 """)
175
176 ## Base class for AutoGen
177 #
178 # This class just implements the cache mechanism of AutoGen objects.
179 #
180 class AutoGen(object):
181 # database to maintain the objects in each child class
182 __ObjectCache = {} # (BuildTarget, ToolChain, ARCH, platform file): AutoGen object
183
184 ## Factory method
185 #
186 # @param Class class object of real AutoGen class
187 # (WorkspaceAutoGen, ModuleAutoGen or PlatformAutoGen)
188 # @param Workspace Workspace directory or WorkspaceAutoGen object
189 # @param MetaFile The path of meta file
190 # @param Target Build target
191 # @param Toolchain Tool chain name
192 # @param Arch Target arch
193 # @param *args The specific class related parameters
194 # @param **kwargs The specific class related dict parameters
195 #
196 def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
197 # check if the object has been created
198 Key = (Target, Toolchain, Arch, MetaFile)
199 if Key in cls.__ObjectCache:
200 # if it exists, just return it directly
201 return cls.__ObjectCache[Key]
202 # it didnt exist. create it, cache it, then return it
203 RetVal = cls.__ObjectCache[Key] = super(AutoGen, cls).__new__(cls)
204 return RetVal
205
206 def __init__ (self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
207 super(AutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
208
209 ## hash() operator
210 #
211 # The file path of platform file will be used to represent hash value of this object
212 #
213 # @retval int Hash value of the file path of platform file
214 #
215 def __hash__(self):
216 return hash(self.MetaFile)
217
218 ## str() operator
219 #
220 # The file path of platform file will be used to represent this object
221 #
222 # @retval string String of platform file path
223 #
224 def __str__(self):
225 return str(self.MetaFile)
226
227 ## "==" operator
228 def __eq__(self, Other):
229 return Other and self.MetaFile == Other
230
231 ## Workspace AutoGen class
232 #
233 # This class is used mainly to control the whole platform build for different
234 # architecture. This class will generate top level makefile.
235 #
236 class WorkspaceAutoGen(AutoGen):
237 # call super().__init__ then call the worker function with different parameter count
238 def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
239 if not hasattr(self, "_Init"):
240 super(WorkspaceAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
241 self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
242 self._Init = True
243
244 ## Initialize WorkspaceAutoGen
245 #
246 # @param WorkspaceDir Root directory of workspace
247 # @param ActivePlatform Meta-file of active platform
248 # @param Target Build target
249 # @param Toolchain Tool chain name
250 # @param ArchList List of architecture of current build
251 # @param MetaFileDb Database containing meta-files
252 # @param BuildConfig Configuration of build
253 # @param ToolDefinition Tool chain definitions
254 # @param FlashDefinitionFile File of flash definition
255 # @param Fds FD list to be generated
256 # @param Fvs FV list to be generated
257 # @param Caps Capsule list to be generated
258 # @param SkuId SKU id from command line
259 #
260 def _InitWorker(self, WorkspaceDir, ActivePlatform, Target, Toolchain, ArchList, MetaFileDb,
261 BuildConfig, ToolDefinition, FlashDefinitionFile='', Fds=None, Fvs=None, Caps=None, SkuId='', UniFlag=None,
262 Progress=None, BuildModule=None):
263 self.BuildDatabase = MetaFileDb
264 self.MetaFile = ActivePlatform
265 self.WorkspaceDir = WorkspaceDir
266 self.Platform = self.BuildDatabase[self.MetaFile, TAB_ARCH_COMMON, Target, Toolchain]
267 GlobalData.gActivePlatform = self.Platform
268 self.BuildTarget = Target
269 self.ToolChain = Toolchain
270 self.ArchList = ArchList
271 self.SkuId = SkuId
272 self.UniFlag = UniFlag
273
274 self.TargetTxt = BuildConfig
275 self.ToolDef = ToolDefinition
276 self.FdfFile = FlashDefinitionFile
277 self.FdTargetList = Fds if Fds else []
278 self.FvTargetList = Fvs if Fvs else []
279 self.CapTargetList = Caps if Caps else []
280 self.AutoGenObjectList = []
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 = defaultdict(set)
359 for Arch in self.ArchList:
360 Current_Platform_cache = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
361 for Pkey in Current_Platform_cache.Modules:
362 MetaFile_cache[Arch].add(Current_Platform_cache.Modules[Pkey].MetaFile)
363 for Inf in self.FdfProfile.InfDict[key]:
364 ModuleFile = PathClass(NormPath(Inf), GlobalData.gWorkspace, Arch)
365 for Arch in self.ArchList:
366 if ModuleFile in MetaFile_cache[Arch]:
367 break
368 else:
369 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]
370 if not ModuleData.IsBinaryModule:
371 EdkLogger.error('build', PARSER_ERROR, "Module %s NOT found in DSC file; Is it really a binary module?" % ModuleFile)
372
373 else:
374 for Arch in self.ArchList:
375 if Arch == key:
376 Platform = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
377 MetaFileList = set()
378 for Pkey in Platform.Modules:
379 MetaFileList.add(Platform.Modules[Pkey].MetaFile)
380 for Inf in self.FdfProfile.InfDict[key]:
381 ModuleFile = PathClass(NormPath(Inf), GlobalData.gWorkspace, Arch)
382 if ModuleFile in MetaFileList:
383 continue
384 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]
385 if not ModuleData.IsBinaryModule:
386 EdkLogger.error('build', PARSER_ERROR, "Module %s NOT found in DSC file; Is it really a binary module?" % ModuleFile)
387
388 else:
389 PcdSet = {}
390 ModuleList = []
391 self.FdfProfile = None
392 if self.FdTargetList:
393 EdkLogger.info("No flash definition file found. FD [%s] will be ignored." % " ".join(self.FdTargetList))
394 self.FdTargetList = []
395 if self.FvTargetList:
396 EdkLogger.info("No flash definition file found. FV [%s] will be ignored." % " ".join(self.FvTargetList))
397 self.FvTargetList = []
398 if self.CapTargetList:
399 EdkLogger.info("No flash definition file found. Capsule [%s] will be ignored." % " ".join(self.CapTargetList))
400 self.CapTargetList = []
401
402 # apply SKU and inject PCDs from Flash Definition file
403 for Arch in self.ArchList:
404 Platform = self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]
405 PlatformPcds = Platform.Pcds
406 self._GuidDict = Platform._GuidDict
407 SourcePcdDict = {TAB_PCDS_DYNAMIC_EX:set(), TAB_PCDS_PATCHABLE_IN_MODULE:set(),TAB_PCDS_DYNAMIC:set(),TAB_PCDS_FIXED_AT_BUILD:set()}
408 BinaryPcdDict = {TAB_PCDS_DYNAMIC_EX:set(), TAB_PCDS_PATCHABLE_IN_MODULE:set()}
409 SourcePcdDict_Keys = SourcePcdDict.keys()
410 BinaryPcdDict_Keys = BinaryPcdDict.keys()
411
412 # generate the SourcePcdDict and BinaryPcdDict
413 PGen = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)
414 for BuildData in PGen.BuildDatabase._CACHE_.values():
415 if BuildData.Arch != Arch:
416 continue
417 if BuildData.MetaFile.Ext == '.inf':
418 for key in BuildData.Pcds:
419 if BuildData.Pcds[key].Pending:
420 if key in Platform.Pcds:
421 PcdInPlatform = Platform.Pcds[key]
422 if PcdInPlatform.Type:
423 BuildData.Pcds[key].Type = PcdInPlatform.Type
424 BuildData.Pcds[key].Pending = False
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:
431 BuildData.Pcds[key].Type = PcdInPlatform.Type
432 BuildData.Pcds[key].Pending = False
433 else:
434 #Pcd used in Library, Pcd Type from reference module if Pcd Type is Pending
435 if BuildData.Pcds[key].Pending:
436 MGen = ModuleAutoGen(self, BuildData.MetaFile, Target, Toolchain, Arch, self.MetaFile)
437 if MGen and MGen.IsLibrary:
438 if MGen in PGen.LibraryAutoGenList:
439 ReferenceModules = MGen.ReferenceModules
440 for ReferenceModule in ReferenceModules:
441 if ReferenceModule.MetaFile in Platform.Modules:
442 RefPlatformModule = Platform.Modules[str(ReferenceModule.MetaFile)]
443 if key in RefPlatformModule.Pcds:
444 PcdInReferenceModule = RefPlatformModule.Pcds[key]
445 if PcdInReferenceModule.Type:
446 BuildData.Pcds[key].Type = PcdInReferenceModule.Type
447 BuildData.Pcds[key].Pending = False
448 break
449
450 if TAB_PCDS_DYNAMIC_EX in BuildData.Pcds[key].Type:
451 if BuildData.IsBinaryModule:
452 BinaryPcdDict[TAB_PCDS_DYNAMIC_EX].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
453 else:
454 SourcePcdDict[TAB_PCDS_DYNAMIC_EX].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
455
456 elif TAB_PCDS_PATCHABLE_IN_MODULE in BuildData.Pcds[key].Type:
457 if BuildData.MetaFile.Ext == '.inf':
458 if BuildData.IsBinaryModule:
459 BinaryPcdDict[TAB_PCDS_PATCHABLE_IN_MODULE].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
460 else:
461 SourcePcdDict[TAB_PCDS_PATCHABLE_IN_MODULE].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
462
463 elif TAB_PCDS_DYNAMIC in BuildData.Pcds[key].Type:
464 SourcePcdDict[TAB_PCDS_DYNAMIC].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
465 elif TAB_PCDS_FIXED_AT_BUILD in BuildData.Pcds[key].Type:
466 SourcePcdDict[TAB_PCDS_FIXED_AT_BUILD].add((BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName))
467 else:
468 pass
469 #
470 # A PCD can only use one type for all source modules
471 #
472 for i in SourcePcdDict_Keys:
473 for j in SourcePcdDict_Keys:
474 if i != j:
475 Intersections = SourcePcdDict[i].intersection(SourcePcdDict[j])
476 if len(Intersections) > 0:
477 EdkLogger.error(
478 'build',
479 FORMAT_INVALID,
480 "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),
481 ExtraData='\n\t'.join(str(P[1]+'.'+P[0]) for P in Intersections)
482 )
483
484 #
485 # intersection the BinaryPCD for Mixed PCD
486 #
487 for i in BinaryPcdDict_Keys:
488 for j in BinaryPcdDict_Keys:
489 if i != j:
490 Intersections = BinaryPcdDict[i].intersection(BinaryPcdDict[j])
491 for item in Intersections:
492 NewPcd1 = (item[0] + '_' + i, item[1])
493 NewPcd2 = (item[0] + '_' + j, item[1])
494 if item not in GlobalData.MixedPcd:
495 GlobalData.MixedPcd[item] = [NewPcd1, NewPcd2]
496 else:
497 if NewPcd1 not in GlobalData.MixedPcd[item]:
498 GlobalData.MixedPcd[item].append(NewPcd1)
499 if NewPcd2 not in GlobalData.MixedPcd[item]:
500 GlobalData.MixedPcd[item].append(NewPcd2)
501
502 #
503 # intersection the SourcePCD and BinaryPCD for Mixed PCD
504 #
505 for i in SourcePcdDict_Keys:
506 for j in BinaryPcdDict_Keys:
507 if i != j:
508 Intersections = SourcePcdDict[i].intersection(BinaryPcdDict[j])
509 for item in Intersections:
510 NewPcd1 = (item[0] + '_' + i, item[1])
511 NewPcd2 = (item[0] + '_' + j, item[1])
512 if item not in GlobalData.MixedPcd:
513 GlobalData.MixedPcd[item] = [NewPcd1, NewPcd2]
514 else:
515 if NewPcd1 not in GlobalData.MixedPcd[item]:
516 GlobalData.MixedPcd[item].append(NewPcd1)
517 if NewPcd2 not in GlobalData.MixedPcd[item]:
518 GlobalData.MixedPcd[item].append(NewPcd2)
519
520 for BuildData in PGen.BuildDatabase._CACHE_.values():
521 if BuildData.Arch != Arch:
522 continue
523 for key in BuildData.Pcds:
524 for SinglePcd in GlobalData.MixedPcd:
525 if (BuildData.Pcds[key].TokenCName, BuildData.Pcds[key].TokenSpaceGuidCName) == SinglePcd:
526 for item in GlobalData.MixedPcd[SinglePcd]:
527 Pcd_Type = item[0].split('_')[-1]
528 if (Pcd_Type == BuildData.Pcds[key].Type) or (Pcd_Type == TAB_PCDS_DYNAMIC_EX and BuildData.Pcds[key].Type in PCD_DYNAMIC_EX_TYPE_SET) or \
529 (Pcd_Type == TAB_PCDS_DYNAMIC and BuildData.Pcds[key].Type in PCD_DYNAMIC_TYPE_SET):
530 Value = BuildData.Pcds[key]
531 Value.TokenCName = BuildData.Pcds[key].TokenCName + '_' + Pcd_Type
532 if len(key) == 2:
533 newkey = (Value.TokenCName, key[1])
534 elif len(key) == 3:
535 newkey = (Value.TokenCName, key[1], key[2])
536 del BuildData.Pcds[key]
537 BuildData.Pcds[newkey] = Value
538 break
539 break
540
541 # handle the mixed pcd in FDF file
542 for key in PcdSet:
543 if key in GlobalData.MixedPcd:
544 Value = PcdSet[key]
545 del PcdSet[key]
546 for item in GlobalData.MixedPcd[key]:
547 PcdSet[item] = Value
548
549 #Collect package set information from INF of FDF
550 PkgSet = set()
551 for Inf in ModuleList:
552 ModuleFile = PathClass(NormPath(Inf), GlobalData.gWorkspace, Arch)
553 if ModuleFile in Platform.Modules:
554 continue
555 ModuleData = self.BuildDatabase[ModuleFile, Arch, Target, Toolchain]
556 PkgSet.update(ModuleData.Packages)
557 Pkgs = list(PkgSet) + list(PGen.PackageList)
558 DecPcds = set()
559 DecPcdsKey = set()
560 for Pkg in Pkgs:
561 for Pcd in Pkg.Pcds:
562 DecPcds.add((Pcd[0], Pcd[1]))
563 DecPcdsKey.add((Pcd[0], Pcd[1], Pcd[2]))
564
565 Platform.SkuName = self.SkuId
566 for Name, Guid,Fileds in PcdSet:
567 if (Name, Guid) not in DecPcds:
568 EdkLogger.error(
569 'build',
570 PARSER_ERROR,
571 "PCD (%s.%s) used in FDF is not declared in DEC files." % (Guid, Name),
572 File = self.FdfProfile.PcdFileLineDict[Name, Guid][0],
573 Line = self.FdfProfile.PcdFileLineDict[Name, Guid][1]
574 )
575 else:
576 # Check whether Dynamic or DynamicEx PCD used in FDF file. If used, build break and give a error message.
577 if (Name, Guid, TAB_PCDS_FIXED_AT_BUILD) in DecPcdsKey \
578 or (Name, Guid, TAB_PCDS_PATCHABLE_IN_MODULE) in DecPcdsKey \
579 or (Name, Guid, TAB_PCDS_FEATURE_FLAG) in DecPcdsKey:
580 continue
581 elif (Name, Guid, TAB_PCDS_DYNAMIC) in DecPcdsKey or (Name, Guid, TAB_PCDS_DYNAMIC_EX) in DecPcdsKey:
582 EdkLogger.error(
583 'build',
584 PARSER_ERROR,
585 "Using Dynamic or DynamicEx type of PCD [%s.%s] in FDF file is not allowed." % (Guid, Name),
586 File = self.FdfProfile.PcdFileLineDict[Name, Guid][0],
587 Line = self.FdfProfile.PcdFileLineDict[Name, Guid][1]
588 )
589
590 Pa = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)
591 #
592 # Explicitly collect platform's dynamic PCDs
593 #
594 Pa.CollectPlatformDynamicPcds()
595 Pa.CollectFixedAtBuildPcds()
596 self.AutoGenObjectList.append(Pa)
597
598 #
599 # Generate Package level hash value
600 #
601 GlobalData.gPackageHash[Arch] = {}
602 if GlobalData.gUseHashCache:
603 for Pkg in Pkgs:
604 self._GenPkgLevelHash(Pkg)
605
606 #
607 # Check PCDs token value conflict in each DEC file.
608 #
609 self._CheckAllPcdsTokenValueConflict()
610
611 #
612 # Check PCD type and definition between DSC and DEC
613 #
614 self._CheckPcdDefineAndType()
615
616 #
617 # Create BuildOptions Macro & PCD metafile, also add the Active Platform and FDF file.
618 #
619 content = 'gCommandLineDefines: '
620 content += str(GlobalData.gCommandLineDefines)
621 content += os.linesep
622 content += 'BuildOptionPcd: '
623 content += str(GlobalData.BuildOptionPcd)
624 content += os.linesep
625 content += 'Active Platform: '
626 content += str(self.Platform)
627 content += os.linesep
628 if self.FdfFile:
629 content += 'Flash Image Definition: '
630 content += str(self.FdfFile)
631 content += os.linesep
632 SaveFileOnChange(os.path.join(self.BuildDir, 'BuildOptions'), content, False)
633
634 #
635 # Create PcdToken Number file for Dynamic/DynamicEx Pcd.
636 #
637 PcdTokenNumber = 'PcdTokenNumber: '
638 if Pa.PcdTokenNumber:
639 if Pa.DynamicPcdList:
640 for Pcd in Pa.DynamicPcdList:
641 PcdTokenNumber += os.linesep
642 PcdTokenNumber += str((Pcd.TokenCName, Pcd.TokenSpaceGuidCName))
643 PcdTokenNumber += ' : '
644 PcdTokenNumber += str(Pa.PcdTokenNumber[Pcd.TokenCName, Pcd.TokenSpaceGuidCName])
645 SaveFileOnChange(os.path.join(self.BuildDir, 'PcdTokenNumber'), PcdTokenNumber, False)
646
647 #
648 # Get set of workspace metafiles
649 #
650 AllWorkSpaceMetaFiles = self._GetMetaFiles(Target, Toolchain, Arch)
651
652 #
653 # Retrieve latest modified time of all metafiles
654 #
655 SrcTimeStamp = 0
656 for f in AllWorkSpaceMetaFiles:
657 if os.stat(f)[8] > SrcTimeStamp:
658 SrcTimeStamp = os.stat(f)[8]
659 self._SrcTimeStamp = SrcTimeStamp
660
661 if GlobalData.gUseHashCache:
662 m = hashlib.md5()
663 for files in AllWorkSpaceMetaFiles:
664 if files.endswith('.dec'):
665 continue
666 f = open(files, 'r')
667 Content = f.read()
668 f.close()
669 m.update(Content)
670 SaveFileOnChange(os.path.join(self.BuildDir, 'AutoGen.hash'), m.hexdigest(), True)
671 GlobalData.gPlatformHash = m.hexdigest()
672
673 #
674 # Write metafile list to build directory
675 #
676 AutoGenFilePath = os.path.join(self.BuildDir, 'AutoGen')
677 if os.path.exists (AutoGenFilePath):
678 os.remove(AutoGenFilePath)
679 if not os.path.exists(self.BuildDir):
680 os.makedirs(self.BuildDir)
681 with open(os.path.join(self.BuildDir, 'AutoGen'), 'w+') as file:
682 for f in AllWorkSpaceMetaFiles:
683 print(f, file=file)
684 return True
685
686 def _GenPkgLevelHash(self, Pkg):
687 if Pkg.PackageName in GlobalData.gPackageHash[Pkg.Arch]:
688 return
689
690 PkgDir = os.path.join(self.BuildDir, Pkg.Arch, Pkg.PackageName)
691 CreateDirectory(PkgDir)
692 HashFile = os.path.join(PkgDir, Pkg.PackageName + '.hash')
693 m = hashlib.md5()
694 # Get .dec file's hash value
695 f = open(Pkg.MetaFile.Path, 'r')
696 Content = f.read()
697 f.close()
698 m.update(Content)
699 # Get include files hash value
700 if Pkg.Includes:
701 for inc in sorted(Pkg.Includes, key=lambda x: str(x)):
702 for Root, Dirs, Files in os.walk(str(inc)):
703 for File in sorted(Files):
704 File_Path = os.path.join(Root, File)
705 f = open(File_Path, 'r')
706 Content = f.read()
707 f.close()
708 m.update(Content)
709 SaveFileOnChange(HashFile, m.hexdigest(), True)
710 GlobalData.gPackageHash[Pkg.Arch][Pkg.PackageName] = m.hexdigest()
711
712 def _GetMetaFiles(self, Target, Toolchain, Arch):
713 AllWorkSpaceMetaFiles = set()
714 #
715 # add fdf
716 #
717 if self.FdfFile:
718 AllWorkSpaceMetaFiles.add (self.FdfFile.Path)
719 for f in GlobalData.gFdfParser.GetAllIncludedFile():
720 AllWorkSpaceMetaFiles.add (f.FileName)
721 #
722 # add dsc
723 #
724 AllWorkSpaceMetaFiles.add(self.MetaFile.Path)
725
726 #
727 # add build_rule.txt & tools_def.txt
728 #
729 AllWorkSpaceMetaFiles.add(os.path.join(GlobalData.gConfDirectory, gDefaultBuildRuleFile))
730 AllWorkSpaceMetaFiles.add(os.path.join(GlobalData.gConfDirectory, gDefaultToolsDefFile))
731
732 # add BuildOption metafile
733 #
734 AllWorkSpaceMetaFiles.add(os.path.join(self.BuildDir, 'BuildOptions'))
735
736 # add PcdToken Number file for Dynamic/DynamicEx Pcd
737 #
738 AllWorkSpaceMetaFiles.add(os.path.join(self.BuildDir, 'PcdTokenNumber'))
739
740 for Arch in self.ArchList:
741 #
742 # add dec
743 #
744 for Package in PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch).PackageList:
745 AllWorkSpaceMetaFiles.add(Package.MetaFile.Path)
746
747 #
748 # add included dsc
749 #
750 for filePath in self.BuildDatabase[self.MetaFile, Arch, Target, Toolchain]._RawData.IncludedFiles:
751 AllWorkSpaceMetaFiles.add(filePath.Path)
752
753 return AllWorkSpaceMetaFiles
754
755 def _CheckPcdDefineAndType(self):
756 PcdTypeSet = {TAB_PCDS_FIXED_AT_BUILD,
757 TAB_PCDS_PATCHABLE_IN_MODULE,
758 TAB_PCDS_FEATURE_FLAG,
759 TAB_PCDS_DYNAMIC,
760 TAB_PCDS_DYNAMIC_EX}
761
762 # This dict store PCDs which are not used by any modules with specified arches
763 UnusedPcd = OrderedDict()
764 for Pa in self.AutoGenObjectList:
765 # Key of DSC's Pcds dictionary is PcdCName, TokenSpaceGuid
766 for Pcd in Pa.Platform.Pcds:
767 PcdType = Pa.Platform.Pcds[Pcd].Type
768
769 # If no PCD type, this PCD comes from FDF
770 if not PcdType:
771 continue
772
773 # Try to remove Hii and Vpd suffix
774 if PcdType.startswith(TAB_PCDS_DYNAMIC_EX):
775 PcdType = TAB_PCDS_DYNAMIC_EX
776 elif PcdType.startswith(TAB_PCDS_DYNAMIC):
777 PcdType = TAB_PCDS_DYNAMIC
778
779 for Package in Pa.PackageList:
780 # Key of DEC's Pcds dictionary is PcdCName, TokenSpaceGuid, PcdType
781 if (Pcd[0], Pcd[1], PcdType) in Package.Pcds:
782 break
783 for Type in PcdTypeSet:
784 if (Pcd[0], Pcd[1], Type) in Package.Pcds:
785 EdkLogger.error(
786 'build',
787 FORMAT_INVALID,
788 "Type [%s] of PCD [%s.%s] in DSC file doesn't match the type [%s] defined in DEC file." \
789 % (Pa.Platform.Pcds[Pcd].Type, Pcd[1], Pcd[0], Type),
790 ExtraData=None
791 )
792 return
793 else:
794 UnusedPcd.setdefault(Pcd, []).append(Pa.Arch)
795
796 for Pcd in UnusedPcd:
797 EdkLogger.warn(
798 'build',
799 "The PCD was not specified by any INF module in the platform for the given architecture.\n"
800 "\tPCD: [%s.%s]\n\tPlatform: [%s]\n\tArch: %s"
801 % (Pcd[1], Pcd[0], os.path.basename(str(self.MetaFile)), str(UnusedPcd[Pcd])),
802 ExtraData=None
803 )
804
805 def __repr__(self):
806 return "%s [%s]" % (self.MetaFile, ", ".join(self.ArchList))
807
808 ## Return the directory to store FV files
809 @cached_property
810 def FvDir(self):
811 return path.join(self.BuildDir, TAB_FV_DIRECTORY)
812
813 ## Return the directory to store all intermediate and final files built
814 @cached_property
815 def BuildDir(self):
816 return self.AutoGenObjectList[0].BuildDir
817
818 ## Return the build output directory platform specifies
819 @cached_property
820 def OutputDir(self):
821 return self.Platform.OutputDirectory
822
823 ## Return platform name
824 @cached_property
825 def Name(self):
826 return self.Platform.PlatformName
827
828 ## Return meta-file GUID
829 @cached_property
830 def Guid(self):
831 return self.Platform.Guid
832
833 ## Return platform version
834 @cached_property
835 def Version(self):
836 return self.Platform.Version
837
838 ## Return paths of tools
839 @cached_property
840 def ToolDefinition(self):
841 return self.AutoGenObjectList[0].ToolDefinition
842
843 ## Return directory of platform makefile
844 #
845 # @retval string Makefile directory
846 #
847 @cached_property
848 def MakeFileDir(self):
849 return self.BuildDir
850
851 ## Return build command string
852 #
853 # @retval string Build command string
854 #
855 @cached_property
856 def BuildCommand(self):
857 # BuildCommand should be all the same. So just get one from platform AutoGen
858 return self.AutoGenObjectList[0].BuildCommand
859
860 ## Check the PCDs token value conflict in each DEC file.
861 #
862 # Will cause build break and raise error message while two PCDs conflict.
863 #
864 # @return None
865 #
866 def _CheckAllPcdsTokenValueConflict(self):
867 for Pa in self.AutoGenObjectList:
868 for Package in Pa.PackageList:
869 PcdList = Package.Pcds.values()
870 PcdList.sort(key=lambda x: int(x.TokenValue, 0))
871 Count = 0
872 while (Count < len(PcdList) - 1) :
873 Item = PcdList[Count]
874 ItemNext = PcdList[Count + 1]
875 #
876 # Make sure in the same token space the TokenValue should be unique
877 #
878 if (int(Item.TokenValue, 0) == int(ItemNext.TokenValue, 0)):
879 SameTokenValuePcdList = []
880 SameTokenValuePcdList.append(Item)
881 SameTokenValuePcdList.append(ItemNext)
882 RemainPcdListLength = len(PcdList) - Count - 2
883 for ValueSameCount in range(RemainPcdListLength):
884 if int(PcdList[len(PcdList) - RemainPcdListLength + ValueSameCount].TokenValue, 0) == int(Item.TokenValue, 0):
885 SameTokenValuePcdList.append(PcdList[len(PcdList) - RemainPcdListLength + ValueSameCount])
886 else:
887 break;
888 #
889 # Sort same token value PCD list with TokenGuid and TokenCName
890 #
891 SameTokenValuePcdList.sort(key=lambda x: "%s.%s" % (x.TokenSpaceGuidCName, x.TokenCName))
892 SameTokenValuePcdListCount = 0
893 while (SameTokenValuePcdListCount < len(SameTokenValuePcdList) - 1):
894 Flag = False
895 TemListItem = SameTokenValuePcdList[SameTokenValuePcdListCount]
896 TemListItemNext = SameTokenValuePcdList[SameTokenValuePcdListCount + 1]
897
898 if (TemListItem.TokenSpaceGuidCName == TemListItemNext.TokenSpaceGuidCName) and (TemListItem.TokenCName != TemListItemNext.TokenCName):
899 for PcdItem in GlobalData.MixedPcd:
900 if (TemListItem.TokenCName, TemListItem.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem] or \
901 (TemListItemNext.TokenCName, TemListItemNext.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
902 Flag = True
903 if not Flag:
904 EdkLogger.error(
905 'build',
906 FORMAT_INVALID,
907 "The TokenValue [%s] of PCD [%s.%s] is conflict with: [%s.%s] in %s"\
908 % (TemListItem.TokenValue, TemListItem.TokenSpaceGuidCName, TemListItem.TokenCName, TemListItemNext.TokenSpaceGuidCName, TemListItemNext.TokenCName, Package),
909 ExtraData=None
910 )
911 SameTokenValuePcdListCount += 1
912 Count += SameTokenValuePcdListCount
913 Count += 1
914
915 PcdList = Package.Pcds.values()
916 PcdList.sort(key=lambda x: "%s.%s" % (x.TokenSpaceGuidCName, x.TokenCName))
917 Count = 0
918 while (Count < len(PcdList) - 1) :
919 Item = PcdList[Count]
920 ItemNext = PcdList[Count + 1]
921 #
922 # Check PCDs with same TokenSpaceGuidCName.TokenCName have same token value as well.
923 #
924 if (Item.TokenSpaceGuidCName == ItemNext.TokenSpaceGuidCName) and (Item.TokenCName == ItemNext.TokenCName) and (int(Item.TokenValue, 0) != int(ItemNext.TokenValue, 0)):
925 EdkLogger.error(
926 'build',
927 FORMAT_INVALID,
928 "The TokenValue [%s] of PCD [%s.%s] in %s defined in two places should be same as well."\
929 % (Item.TokenValue, Item.TokenSpaceGuidCName, Item.TokenCName, Package),
930 ExtraData=None
931 )
932 Count += 1
933 ## Generate fds command
934 @property
935 def GenFdsCommand(self):
936 return (GenMake.TopLevelMakefile(self)._TEMPLATE_.Replace(GenMake.TopLevelMakefile(self)._TemplateDict)).strip()
937
938 ## Create makefile for the platform and modules in it
939 #
940 # @param CreateDepsMakeFile Flag indicating if the makefile for
941 # modules will be created as well
942 #
943 def CreateMakeFile(self, CreateDepsMakeFile=False):
944 if not CreateDepsMakeFile:
945 return
946 for Pa in self.AutoGenObjectList:
947 Pa.CreateMakeFile(True)
948
949 ## Create autogen code for platform and modules
950 #
951 # Since there's no autogen code for platform, this method will do nothing
952 # if CreateModuleCodeFile is set to False.
953 #
954 # @param CreateDepsCodeFile Flag indicating if creating module's
955 # autogen code file or not
956 #
957 def CreateCodeFile(self, CreateDepsCodeFile=False):
958 if not CreateDepsCodeFile:
959 return
960 for Pa in self.AutoGenObjectList:
961 Pa.CreateCodeFile(True)
962
963 ## Create AsBuilt INF file the platform
964 #
965 def CreateAsBuiltInf(self):
966 return
967
968
969 ## AutoGen class for platform
970 #
971 # PlatformAutoGen class will process the original information in platform
972 # file in order to generate makefile for platform.
973 #
974 class PlatformAutoGen(AutoGen):
975 # call super().__init__ then call the worker function with different parameter count
976 def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
977 if not hasattr(self, "_Init"):
978 super(PlatformAutoGen, self).__init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
979 self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch)
980 self._Init = True
981 #
982 # Used to store all PCDs for both PEI and DXE phase, in order to generate
983 # correct PCD database
984 #
985 _DynaPcdList_ = []
986 _NonDynaPcdList_ = []
987 _PlatformPcds = {}
988
989 #
990 # The priority list while override build option
991 #
992 PrioList = {"0x11111" : 16, # TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE (Highest)
993 "0x01111" : 15, # ******_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
994 "0x10111" : 14, # TARGET_*********_ARCH_COMMANDTYPE_ATTRIBUTE
995 "0x00111" : 13, # ******_*********_ARCH_COMMANDTYPE_ATTRIBUTE
996 "0x11011" : 12, # TARGET_TOOLCHAIN_****_COMMANDTYPE_ATTRIBUTE
997 "0x01011" : 11, # ******_TOOLCHAIN_****_COMMANDTYPE_ATTRIBUTE
998 "0x10011" : 10, # TARGET_*********_****_COMMANDTYPE_ATTRIBUTE
999 "0x00011" : 9, # ******_*********_****_COMMANDTYPE_ATTRIBUTE
1000 "0x11101" : 8, # TARGET_TOOLCHAIN_ARCH_***********_ATTRIBUTE
1001 "0x01101" : 7, # ******_TOOLCHAIN_ARCH_***********_ATTRIBUTE
1002 "0x10101" : 6, # TARGET_*********_ARCH_***********_ATTRIBUTE
1003 "0x00101" : 5, # ******_*********_ARCH_***********_ATTRIBUTE
1004 "0x11001" : 4, # TARGET_TOOLCHAIN_****_***********_ATTRIBUTE
1005 "0x01001" : 3, # ******_TOOLCHAIN_****_***********_ATTRIBUTE
1006 "0x10001" : 2, # TARGET_*********_****_***********_ATTRIBUTE
1007 "0x00001" : 1} # ******_*********_****_***********_ATTRIBUTE (Lowest)
1008
1009 ## Initialize PlatformAutoGen
1010 #
1011 #
1012 # @param Workspace WorkspaceAutoGen object
1013 # @param PlatformFile Platform file (DSC file)
1014 # @param Target Build target (DEBUG, RELEASE)
1015 # @param Toolchain Name of tool chain
1016 # @param Arch arch of the platform supports
1017 #
1018 def _InitWorker(self, Workspace, PlatformFile, Target, Toolchain, Arch):
1019 EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen platform [%s] [%s]" % (PlatformFile, Arch))
1020 GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (PlatformFile, Arch, Toolchain, Target)
1021
1022 self.MetaFile = PlatformFile
1023 self.Workspace = Workspace
1024 self.WorkspaceDir = Workspace.WorkspaceDir
1025 self.ToolChain = Toolchain
1026 self.BuildTarget = Target
1027 self.Arch = Arch
1028 self.SourceDir = PlatformFile.SubDir
1029 self.SourceOverrideDir = None
1030 self.FdTargetList = self.Workspace.FdTargetList
1031 self.FvTargetList = self.Workspace.FvTargetList
1032 self.AllPcdList = []
1033 # get the original module/package/platform objects
1034 self.BuildDatabase = Workspace.BuildDatabase
1035 self.DscBuildDataObj = Workspace.Platform
1036
1037 # flag indicating if the makefile/C-code file has been created or not
1038 self.IsMakeFileCreated = False
1039
1040 self._DynamicPcdList = None # [(TokenCName1, TokenSpaceGuidCName1), (TokenCName2, TokenSpaceGuidCName2), ...]
1041 self._NonDynamicPcdList = None # [(TokenCName1, TokenSpaceGuidCName1), (TokenCName2, TokenSpaceGuidCName2), ...]
1042
1043 self._AsBuildInfList = []
1044 self._AsBuildModuleList = []
1045
1046 self.VariableInfo = None
1047
1048 if GlobalData.gFdfParser is not None:
1049 self._AsBuildInfList = GlobalData.gFdfParser.Profile.InfList
1050 for Inf in self._AsBuildInfList:
1051 InfClass = PathClass(NormPath(Inf), GlobalData.gWorkspace, self.Arch)
1052 M = self.BuildDatabase[InfClass, self.Arch, self.BuildTarget, self.ToolChain]
1053 if not M.IsBinaryModule:
1054 continue
1055 self._AsBuildModuleList.append(InfClass)
1056 # get library/modules for build
1057 self.LibraryBuildDirectoryList = []
1058 self.ModuleBuildDirectoryList = []
1059
1060 return True
1061
1062 @cached_class_function
1063 def __repr__(self):
1064 return "%s [%s]" % (self.MetaFile, self.Arch)
1065
1066 ## Create autogen code for platform and modules
1067 #
1068 # Since there's no autogen code for platform, this method will do nothing
1069 # if CreateModuleCodeFile is set to False.
1070 #
1071 # @param CreateModuleCodeFile Flag indicating if creating module's
1072 # autogen code file or not
1073 #
1074 @cached_class_function
1075 def CreateCodeFile(self, CreateModuleCodeFile=False):
1076 # only module has code to be greated, so do nothing if CreateModuleCodeFile is False
1077 if not CreateModuleCodeFile:
1078 return
1079
1080 for Ma in self.ModuleAutoGenList:
1081 Ma.CreateCodeFile(True)
1082
1083 ## Generate Fds Command
1084 @cached_property
1085 def GenFdsCommand(self):
1086 return self.Workspace.GenFdsCommand
1087
1088 ## Create makefile for the platform and modules in it
1089 #
1090 # @param CreateModuleMakeFile Flag indicating if the makefile for
1091 # modules will be created as well
1092 #
1093 def CreateMakeFile(self, CreateModuleMakeFile=False, FfsCommand = {}):
1094 if CreateModuleMakeFile:
1095 for Ma in self._MaList:
1096 key = (Ma.MetaFile.File, self.Arch)
1097 if key in FfsCommand:
1098 Ma.CreateMakeFile(True, FfsCommand[key])
1099 else:
1100 Ma.CreateMakeFile(True)
1101
1102 # no need to create makefile for the platform more than once
1103 if self.IsMakeFileCreated:
1104 return
1105
1106 # create library/module build dirs for platform
1107 Makefile = GenMake.PlatformMakefile(self)
1108 self.LibraryBuildDirectoryList = Makefile.GetLibraryBuildDirectoryList()
1109 self.ModuleBuildDirectoryList = Makefile.GetModuleBuildDirectoryList()
1110
1111 self.IsMakeFileCreated = True
1112
1113 ## Deal with Shared FixedAtBuild Pcds
1114 #
1115 def CollectFixedAtBuildPcds(self):
1116 for LibAuto in self.LibraryAutoGenList:
1117 FixedAtBuildPcds = {}
1118 ShareFixedAtBuildPcdsSameValue = {}
1119 for Module in LibAuto.ReferenceModules:
1120 for Pcd in set(Module.FixedAtBuildPcds + LibAuto.FixedAtBuildPcds):
1121 DefaultValue = Pcd.DefaultValue
1122 # Cover the case: DSC component override the Pcd value and the Pcd only used in one Lib
1123 if Pcd in Module.LibraryPcdList:
1124 Index = Module.LibraryPcdList.index(Pcd)
1125 DefaultValue = Module.LibraryPcdList[Index].DefaultValue
1126 key = ".".join((Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1127 if key not in FixedAtBuildPcds:
1128 ShareFixedAtBuildPcdsSameValue[key] = True
1129 FixedAtBuildPcds[key] = DefaultValue
1130 else:
1131 if FixedAtBuildPcds[key] != DefaultValue:
1132 ShareFixedAtBuildPcdsSameValue[key] = False
1133 for Pcd in LibAuto.FixedAtBuildPcds:
1134 key = ".".join((Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1135 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) not in self.NonDynamicPcdDict:
1136 continue
1137 else:
1138 DscPcd = self.NonDynamicPcdDict[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName)]
1139 if DscPcd.Type != TAB_PCDS_FIXED_AT_BUILD:
1140 continue
1141 if key in ShareFixedAtBuildPcdsSameValue and ShareFixedAtBuildPcdsSameValue[key]:
1142 LibAuto.ConstPcd[key] = FixedAtBuildPcds[key]
1143
1144 def CollectVariables(self, DynamicPcdSet):
1145 VpdRegionSize = 0
1146 VpdRegionBase = 0
1147 if self.Workspace.FdfFile:
1148 FdDict = self.Workspace.FdfProfile.FdDict[GlobalData.gFdfParser.CurrentFdName]
1149 for FdRegion in FdDict.RegionList:
1150 for item in FdRegion.RegionDataList:
1151 if self.Platform.VpdToolGuid.strip() and self.Platform.VpdToolGuid in item:
1152 VpdRegionSize = FdRegion.Size
1153 VpdRegionBase = FdRegion.Offset
1154 break
1155
1156 VariableInfo = VariableMgr(self.DscBuildDataObj._GetDefaultStores(), self.DscBuildDataObj.SkuIds)
1157 VariableInfo.SetVpdRegionMaxSize(VpdRegionSize)
1158 VariableInfo.SetVpdRegionOffset(VpdRegionBase)
1159 Index = 0
1160 for Pcd in DynamicPcdSet:
1161 pcdname = ".".join((Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1162 for SkuName in Pcd.SkuInfoList:
1163 Sku = Pcd.SkuInfoList[SkuName]
1164 SkuId = Sku.SkuId
1165 if SkuId is None or SkuId == '':
1166 continue
1167 if len(Sku.VariableName) > 0:
1168 VariableGuidStructure = Sku.VariableGuidValue
1169 VariableGuid = GuidStructureStringToGuidString(VariableGuidStructure)
1170 for StorageName in Sku.DefaultStoreDict:
1171 VariableInfo.append_variable(var_info(Index, pcdname, StorageName, SkuName, StringToArray(Sku.VariableName), VariableGuid, Sku.VariableOffset, Sku.VariableAttribute, Sku.HiiDefaultValue, Sku.DefaultStoreDict[StorageName], Pcd.DatumType, Pcd.CustomAttribute['DscPosition'], Pcd.CustomAttribute.get('IsStru',False)))
1172 Index += 1
1173 return VariableInfo
1174
1175 def UpdateNVStoreMaxSize(self, OrgVpdFile):
1176 if self.VariableInfo:
1177 VpdMapFilePath = os.path.join(self.BuildDir, TAB_FV_DIRECTORY, "%s.map" % self.Platform.VpdToolGuid)
1178 PcdNvStoreDfBuffer = [item for item in self._DynamicPcdList if item.TokenCName == "PcdNvStoreDefaultValueBuffer" and item.TokenSpaceGuidCName == "gEfiMdeModulePkgTokenSpaceGuid"]
1179
1180 if PcdNvStoreDfBuffer:
1181 if os.path.exists(VpdMapFilePath):
1182 OrgVpdFile.Read(VpdMapFilePath)
1183 PcdItems = OrgVpdFile.GetOffset(PcdNvStoreDfBuffer[0])
1184 NvStoreOffset = PcdItems.values()[0].strip() if PcdItems else '0'
1185 else:
1186 EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)
1187
1188 NvStoreOffset = int(NvStoreOffset, 16) if NvStoreOffset.upper().startswith("0X") else int(NvStoreOffset)
1189 default_skuobj = PcdNvStoreDfBuffer[0].SkuInfoList.get(TAB_DEFAULT)
1190 maxsize = self.VariableInfo.VpdRegionSize - NvStoreOffset if self.VariableInfo.VpdRegionSize else len(default_skuobj.DefaultValue.split(","))
1191 var_data = self.VariableInfo.PatchNVStoreDefaultMaxSize(maxsize)
1192
1193 if var_data and default_skuobj:
1194 default_skuobj.DefaultValue = var_data
1195 PcdNvStoreDfBuffer[0].DefaultValue = var_data
1196 PcdNvStoreDfBuffer[0].SkuInfoList.clear()
1197 PcdNvStoreDfBuffer[0].SkuInfoList[TAB_DEFAULT] = default_skuobj
1198 PcdNvStoreDfBuffer[0].MaxDatumSize = str(len(default_skuobj.DefaultValue.split(",")))
1199
1200 return OrgVpdFile
1201
1202 ## Collect dynamic PCDs
1203 #
1204 # Gather dynamic PCDs list from each module and their settings from platform
1205 # This interface should be invoked explicitly when platform action is created.
1206 #
1207 def CollectPlatformDynamicPcds(self):
1208 for key in self.Platform.Pcds:
1209 for SinglePcd in GlobalData.MixedPcd:
1210 if (self.Platform.Pcds[key].TokenCName, self.Platform.Pcds[key].TokenSpaceGuidCName) == SinglePcd:
1211 for item in GlobalData.MixedPcd[SinglePcd]:
1212 Pcd_Type = item[0].split('_')[-1]
1213 if (Pcd_Type == self.Platform.Pcds[key].Type) or (Pcd_Type == TAB_PCDS_DYNAMIC_EX and self.Platform.Pcds[key].Type in PCD_DYNAMIC_EX_TYPE_SET) or \
1214 (Pcd_Type == TAB_PCDS_DYNAMIC and self.Platform.Pcds[key].Type in PCD_DYNAMIC_TYPE_SET):
1215 Value = self.Platform.Pcds[key]
1216 Value.TokenCName = self.Platform.Pcds[key].TokenCName + '_' + Pcd_Type
1217 if len(key) == 2:
1218 newkey = (Value.TokenCName, key[1])
1219 elif len(key) == 3:
1220 newkey = (Value.TokenCName, key[1], key[2])
1221 del self.Platform.Pcds[key]
1222 self.Platform.Pcds[newkey] = Value
1223 break
1224 break
1225
1226 # for gathering error information
1227 NoDatumTypePcdList = set()
1228 FdfModuleList = []
1229 for InfName in self._AsBuildInfList:
1230 InfName = mws.join(self.WorkspaceDir, InfName)
1231 FdfModuleList.append(os.path.normpath(InfName))
1232 for M in self._MaList:
1233 # F is the Module for which M is the module autogen
1234 for PcdFromModule in M.ModulePcdList + M.LibraryPcdList:
1235 # make sure that the "VOID*" kind of datum has MaxDatumSize set
1236 if PcdFromModule.DatumType == TAB_VOID and not PcdFromModule.MaxDatumSize:
1237 NoDatumTypePcdList.add("%s.%s [%s]" % (PcdFromModule.TokenSpaceGuidCName, PcdFromModule.TokenCName, M.MetaFile))
1238
1239 # Check the PCD from Binary INF or Source INF
1240 if M.IsBinaryModule == True:
1241 PcdFromModule.IsFromBinaryInf = True
1242
1243 # Check the PCD from DSC or not
1244 PcdFromModule.IsFromDsc = (PcdFromModule.TokenCName, PcdFromModule.TokenSpaceGuidCName) in self.Platform.Pcds
1245
1246 if PcdFromModule.Type in PCD_DYNAMIC_TYPE_SET or PcdFromModule.Type in PCD_DYNAMIC_EX_TYPE_SET:
1247 if M.MetaFile.Path not in FdfModuleList:
1248 # If one of the Source built modules listed in the DSC is not listed
1249 # in FDF modules, and the INF lists a PCD can only use the PcdsDynamic
1250 # access method (it is only listed in the DEC file that declares the
1251 # PCD as PcdsDynamic), then build tool will report warning message
1252 # notify the PI that they are attempting to build a module that must
1253 # be included in a flash image in order to be functional. These Dynamic
1254 # PCD will not be added into the Database unless it is used by other
1255 # modules that are included in the FDF file.
1256 if PcdFromModule.Type in PCD_DYNAMIC_TYPE_SET and \
1257 PcdFromModule.IsFromBinaryInf == False:
1258 # Print warning message to let the developer make a determine.
1259 continue
1260 # If one of the Source built modules listed in the DSC is not listed in
1261 # FDF modules, and the INF lists a PCD can only use the PcdsDynamicEx
1262 # access method (it is only listed in the DEC file that declares the
1263 # PCD as PcdsDynamicEx), then DO NOT break the build; DO NOT add the
1264 # PCD to the Platform's PCD Database.
1265 if PcdFromModule.Type in PCD_DYNAMIC_EX_TYPE_SET:
1266 continue
1267 #
1268 # If a dynamic PCD used by a PEM module/PEI module & DXE module,
1269 # it should be stored in Pcd PEI database, If a dynamic only
1270 # used by DXE module, it should be stored in DXE PCD database.
1271 # The default Phase is DXE
1272 #
1273 if M.ModuleType in SUP_MODULE_SET_PEI:
1274 PcdFromModule.Phase = "PEI"
1275 if PcdFromModule not in self._DynaPcdList_:
1276 self._DynaPcdList_.append(PcdFromModule)
1277 elif PcdFromModule.Phase == 'PEI':
1278 # overwrite any the same PCD existing, if Phase is PEI
1279 Index = self._DynaPcdList_.index(PcdFromModule)
1280 self._DynaPcdList_[Index] = PcdFromModule
1281 elif PcdFromModule not in self._NonDynaPcdList_:
1282 self._NonDynaPcdList_.append(PcdFromModule)
1283 elif PcdFromModule in self._NonDynaPcdList_ and PcdFromModule.IsFromBinaryInf == True:
1284 Index = self._NonDynaPcdList_.index(PcdFromModule)
1285 if self._NonDynaPcdList_[Index].IsFromBinaryInf == False:
1286 #The PCD from Binary INF will override the same one from source INF
1287 self._NonDynaPcdList_.remove (self._NonDynaPcdList_[Index])
1288 PcdFromModule.Pending = False
1289 self._NonDynaPcdList_.append (PcdFromModule)
1290 DscModuleSet = {os.path.normpath(ModuleInf.Path) for ModuleInf in self.Platform.Modules}
1291 # add the PCD from modules that listed in FDF but not in DSC to Database
1292 for InfName in FdfModuleList:
1293 if InfName not in DscModuleSet:
1294 InfClass = PathClass(InfName)
1295 M = self.BuildDatabase[InfClass, self.Arch, self.BuildTarget, self.ToolChain]
1296 # If a module INF in FDF but not in current arch's DSC module list, it must be module (either binary or source)
1297 # for different Arch. PCDs in source module for different Arch is already added before, so skip the source module here.
1298 # For binary module, if in current arch, we need to list the PCDs into database.
1299 if not M.IsBinaryModule:
1300 continue
1301 # Override the module PCD setting by platform setting
1302 ModulePcdList = self.ApplyPcdSetting(M, M.Pcds)
1303 for PcdFromModule in ModulePcdList:
1304 PcdFromModule.IsFromBinaryInf = True
1305 PcdFromModule.IsFromDsc = False
1306 # Only allow the DynamicEx and Patchable PCD in AsBuild INF
1307 if PcdFromModule.Type not in PCD_DYNAMIC_EX_TYPE_SET and PcdFromModule.Type not in TAB_PCDS_PATCHABLE_IN_MODULE:
1308 EdkLogger.error("build", AUTOGEN_ERROR, "PCD setting error",
1309 File=self.MetaFile,
1310 ExtraData="\n\tExisted %s PCD %s in:\n\t\t%s\n"
1311 % (PcdFromModule.Type, PcdFromModule.TokenCName, InfName))
1312 # make sure that the "VOID*" kind of datum has MaxDatumSize set
1313 if PcdFromModule.DatumType == TAB_VOID and not PcdFromModule.MaxDatumSize:
1314 NoDatumTypePcdList.add("%s.%s [%s]" % (PcdFromModule.TokenSpaceGuidCName, PcdFromModule.TokenCName, InfName))
1315 if M.ModuleType in SUP_MODULE_SET_PEI:
1316 PcdFromModule.Phase = "PEI"
1317 if PcdFromModule not in self._DynaPcdList_ and PcdFromModule.Type in PCD_DYNAMIC_EX_TYPE_SET:
1318 self._DynaPcdList_.append(PcdFromModule)
1319 elif PcdFromModule not in self._NonDynaPcdList_ and PcdFromModule.Type in TAB_PCDS_PATCHABLE_IN_MODULE:
1320 self._NonDynaPcdList_.append(PcdFromModule)
1321 if PcdFromModule in self._DynaPcdList_ and PcdFromModule.Phase == 'PEI' and PcdFromModule.Type in PCD_DYNAMIC_EX_TYPE_SET:
1322 # Overwrite the phase of any the same PCD existing, if Phase is PEI.
1323 # It is to solve the case that a dynamic PCD used by a PEM module/PEI
1324 # module & DXE module at a same time.
1325 # Overwrite the type of the PCDs in source INF by the type of AsBuild
1326 # INF file as DynamicEx.
1327 Index = self._DynaPcdList_.index(PcdFromModule)
1328 self._DynaPcdList_[Index].Phase = PcdFromModule.Phase
1329 self._DynaPcdList_[Index].Type = PcdFromModule.Type
1330 for PcdFromModule in self._NonDynaPcdList_:
1331 # If a PCD is not listed in the DSC file, but binary INF files used by
1332 # this platform all (that use this PCD) list the PCD in a [PatchPcds]
1333 # section, AND all source INF files used by this platform the build
1334 # that use the PCD list the PCD in either a [Pcds] or [PatchPcds]
1335 # section, then the tools must NOT add the PCD to the Platform's PCD
1336 # Database; the build must assign the access method for this PCD as
1337 # PcdsPatchableInModule.
1338 if PcdFromModule not in self._DynaPcdList_:
1339 continue
1340 Index = self._DynaPcdList_.index(PcdFromModule)
1341 if PcdFromModule.IsFromDsc == False and \
1342 PcdFromModule.Type in TAB_PCDS_PATCHABLE_IN_MODULE and \
1343 PcdFromModule.IsFromBinaryInf == True and \
1344 self._DynaPcdList_[Index].IsFromBinaryInf == False:
1345 Index = self._DynaPcdList_.index(PcdFromModule)
1346 self._DynaPcdList_.remove (self._DynaPcdList_[Index])
1347
1348 # print out error information and break the build, if error found
1349 if len(NoDatumTypePcdList) > 0:
1350 NoDatumTypePcdListString = "\n\t\t".join(NoDatumTypePcdList)
1351 EdkLogger.error("build", AUTOGEN_ERROR, "PCD setting error",
1352 File=self.MetaFile,
1353 ExtraData="\n\tPCD(s) without MaxDatumSize:\n\t\t%s\n"
1354 % NoDatumTypePcdListString)
1355 self._NonDynamicPcdList = self._NonDynaPcdList_
1356 self._DynamicPcdList = self._DynaPcdList_
1357 #
1358 # Sort dynamic PCD list to:
1359 # 1) If PCD's datum type is VOID* and value is unicode string which starts with L, the PCD item should
1360 # try to be put header of dynamicd List
1361 # 2) If PCD is HII type, the PCD item should be put after unicode type PCD
1362 #
1363 # The reason of sorting is make sure the unicode string is in double-byte alignment in string table.
1364 #
1365 UnicodePcdArray = set()
1366 HiiPcdArray = set()
1367 OtherPcdArray = set()
1368 VpdPcdDict = {}
1369 VpdFile = VpdInfoFile.VpdInfoFile()
1370 NeedProcessVpdMapFile = False
1371
1372 for pcd in self.Platform.Pcds:
1373 if pcd not in self._PlatformPcds:
1374 self._PlatformPcds[pcd] = self.Platform.Pcds[pcd]
1375
1376 for item in self._PlatformPcds:
1377 if self._PlatformPcds[item].DatumType and self._PlatformPcds[item].DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1378 self._PlatformPcds[item].DatumType = TAB_VOID
1379
1380 if (self.Workspace.ArchList[-1] == self.Arch):
1381 for Pcd in self._DynamicPcdList:
1382 # just pick the a value to determine whether is unicode string type
1383 Sku = Pcd.SkuInfoList.values()[0]
1384 Sku.VpdOffset = Sku.VpdOffset.strip()
1385
1386 if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1387 Pcd.DatumType = TAB_VOID
1388
1389 # if found PCD which datum value is unicode string the insert to left size of UnicodeIndex
1390 # if found HII type PCD then insert to right of UnicodeIndex
1391 if Pcd.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD]:
1392 VpdPcdDict[(Pcd.TokenCName, Pcd.TokenSpaceGuidCName)] = Pcd
1393
1394 #Collect DynamicHii PCD values and assign it to DynamicExVpd PCD gEfiMdeModulePkgTokenSpaceGuid.PcdNvStoreDefaultValueBuffer
1395 PcdNvStoreDfBuffer = VpdPcdDict.get(("PcdNvStoreDefaultValueBuffer", "gEfiMdeModulePkgTokenSpaceGuid"))
1396 if PcdNvStoreDfBuffer:
1397 self.VariableInfo = self.CollectVariables(self._DynamicPcdList)
1398 vardump = self.VariableInfo.dump()
1399 if vardump:
1400 PcdNvStoreDfBuffer.DefaultValue = vardump
1401 for skuname in PcdNvStoreDfBuffer.SkuInfoList:
1402 PcdNvStoreDfBuffer.SkuInfoList[skuname].DefaultValue = vardump
1403 PcdNvStoreDfBuffer.MaxDatumSize = str(len(vardump.split(",")))
1404 else:
1405 #If the end user define [DefaultStores] and [XXX.Menufacturing] in DSC, but forget to configure PcdNvStoreDefaultValueBuffer to PcdsDynamicVpd
1406 if [Pcd for Pcd in self._DynamicPcdList if Pcd.UserDefinedDefaultStoresFlag]:
1407 EdkLogger.warn("build", "PcdNvStoreDefaultValueBuffer should be defined as PcdsDynamicExVpd in dsc file since the DefaultStores is enabled for this platform.\n%s" %self.Platform.MetaFile.Path)
1408 PlatformPcds = sorted(self._PlatformPcds.keys())
1409 #
1410 # Add VPD type PCD into VpdFile and determine whether the VPD PCD need to be fixed up.
1411 #
1412 VpdSkuMap = {}
1413 for PcdKey in PlatformPcds:
1414 Pcd = self._PlatformPcds[PcdKey]
1415 if Pcd.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD] and \
1416 PcdKey in VpdPcdDict:
1417 Pcd = VpdPcdDict[PcdKey]
1418 SkuValueMap = {}
1419 DefaultSku = Pcd.SkuInfoList.get(TAB_DEFAULT)
1420 if DefaultSku:
1421 PcdValue = DefaultSku.DefaultValue
1422 if PcdValue not in SkuValueMap:
1423 SkuValueMap[PcdValue] = []
1424 VpdFile.Add(Pcd, TAB_DEFAULT, DefaultSku.VpdOffset)
1425 SkuValueMap[PcdValue].append(DefaultSku)
1426
1427 for (SkuName, Sku) in Pcd.SkuInfoList.items():
1428 Sku.VpdOffset = Sku.VpdOffset.strip()
1429 PcdValue = Sku.DefaultValue
1430 if PcdValue == "":
1431 PcdValue = Pcd.DefaultValue
1432 if Sku.VpdOffset != '*':
1433 if PcdValue.startswith("{"):
1434 Alignment = 8
1435 elif PcdValue.startswith("L"):
1436 Alignment = 2
1437 else:
1438 Alignment = 1
1439 try:
1440 VpdOffset = int(Sku.VpdOffset)
1441 except:
1442 try:
1443 VpdOffset = int(Sku.VpdOffset, 16)
1444 except:
1445 EdkLogger.error("build", FORMAT_INVALID, "Invalid offset value %s for PCD %s.%s." % (Sku.VpdOffset, Pcd.TokenSpaceGuidCName, Pcd.TokenCName))
1446 if VpdOffset % Alignment != 0:
1447 if PcdValue.startswith("{"):
1448 EdkLogger.warn("build", "The offset value of PCD %s.%s is not 8-byte aligned!" %(Pcd.TokenSpaceGuidCName, Pcd.TokenCName), File=self.MetaFile)
1449 else:
1450 EdkLogger.error("build", FORMAT_INVALID, 'The offset value of PCD %s.%s should be %s-byte aligned.' % (Pcd.TokenSpaceGuidCName, Pcd.TokenCName, Alignment))
1451 if PcdValue not in SkuValueMap:
1452 SkuValueMap[PcdValue] = []
1453 VpdFile.Add(Pcd, SkuName, Sku.VpdOffset)
1454 SkuValueMap[PcdValue].append(Sku)
1455 # if the offset of a VPD is *, then it need to be fixed up by third party tool.
1456 if not NeedProcessVpdMapFile and Sku.VpdOffset == "*":
1457 NeedProcessVpdMapFile = True
1458 if self.Platform.VpdToolGuid is None or self.Platform.VpdToolGuid == '':
1459 EdkLogger.error("Build", FILE_NOT_FOUND, \
1460 "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.")
1461
1462 VpdSkuMap[PcdKey] = SkuValueMap
1463 #
1464 # Fix the PCDs define in VPD PCD section that never referenced by module.
1465 # An example is PCD for signature usage.
1466 #
1467 for DscPcd in PlatformPcds:
1468 DscPcdEntry = self._PlatformPcds[DscPcd]
1469 if DscPcdEntry.Type in [TAB_PCDS_DYNAMIC_VPD, TAB_PCDS_DYNAMIC_EX_VPD]:
1470 if not (self.Platform.VpdToolGuid is None or self.Platform.VpdToolGuid == ''):
1471 FoundFlag = False
1472 for VpdPcd in VpdFile._VpdArray:
1473 # This PCD has been referenced by module
1474 if (VpdPcd.TokenSpaceGuidCName == DscPcdEntry.TokenSpaceGuidCName) and \
1475 (VpdPcd.TokenCName == DscPcdEntry.TokenCName):
1476 FoundFlag = True
1477
1478 # Not found, it should be signature
1479 if not FoundFlag :
1480 # just pick the a value to determine whether is unicode string type
1481 SkuValueMap = {}
1482 SkuObjList = DscPcdEntry.SkuInfoList.items()
1483 DefaultSku = DscPcdEntry.SkuInfoList.get(TAB_DEFAULT)
1484 if DefaultSku:
1485 defaultindex = SkuObjList.index((TAB_DEFAULT, DefaultSku))
1486 SkuObjList[0], SkuObjList[defaultindex] = SkuObjList[defaultindex], SkuObjList[0]
1487 for (SkuName, Sku) in SkuObjList:
1488 Sku.VpdOffset = Sku.VpdOffset.strip()
1489
1490 # Need to iterate DEC pcd information to get the value & datumtype
1491 for eachDec in self.PackageList:
1492 for DecPcd in eachDec.Pcds:
1493 DecPcdEntry = eachDec.Pcds[DecPcd]
1494 if (DecPcdEntry.TokenSpaceGuidCName == DscPcdEntry.TokenSpaceGuidCName) and \
1495 (DecPcdEntry.TokenCName == DscPcdEntry.TokenCName):
1496 # Print warning message to let the developer make a determine.
1497 EdkLogger.warn("build", "Unreferenced vpd pcd used!",
1498 File=self.MetaFile, \
1499 ExtraData = "PCD: %s.%s used in the DSC file %s is unreferenced." \
1500 %(DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName, self.Platform.MetaFile.Path))
1501
1502 DscPcdEntry.DatumType = DecPcdEntry.DatumType
1503 DscPcdEntry.DefaultValue = DecPcdEntry.DefaultValue
1504 DscPcdEntry.TokenValue = DecPcdEntry.TokenValue
1505 DscPcdEntry.TokenSpaceGuidValue = eachDec.Guids[DecPcdEntry.TokenSpaceGuidCName]
1506 # Only fix the value while no value provided in DSC file.
1507 if not Sku.DefaultValue:
1508 DscPcdEntry.SkuInfoList[DscPcdEntry.SkuInfoList.keys()[0]].DefaultValue = DecPcdEntry.DefaultValue
1509
1510 if DscPcdEntry not in self._DynamicPcdList:
1511 self._DynamicPcdList.append(DscPcdEntry)
1512 Sku.VpdOffset = Sku.VpdOffset.strip()
1513 PcdValue = Sku.DefaultValue
1514 if PcdValue == "":
1515 PcdValue = DscPcdEntry.DefaultValue
1516 if Sku.VpdOffset != '*':
1517 if PcdValue.startswith("{"):
1518 Alignment = 8
1519 elif PcdValue.startswith("L"):
1520 Alignment = 2
1521 else:
1522 Alignment = 1
1523 try:
1524 VpdOffset = int(Sku.VpdOffset)
1525 except:
1526 try:
1527 VpdOffset = int(Sku.VpdOffset, 16)
1528 except:
1529 EdkLogger.error("build", FORMAT_INVALID, "Invalid offset value %s for PCD %s.%s." % (Sku.VpdOffset, DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName))
1530 if VpdOffset % Alignment != 0:
1531 if PcdValue.startswith("{"):
1532 EdkLogger.warn("build", "The offset value of PCD %s.%s is not 8-byte aligned!" %(DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName), File=self.MetaFile)
1533 else:
1534 EdkLogger.error("build", FORMAT_INVALID, 'The offset value of PCD %s.%s should be %s-byte aligned.' % (DscPcdEntry.TokenSpaceGuidCName, DscPcdEntry.TokenCName, Alignment))
1535 if PcdValue not in SkuValueMap:
1536 SkuValueMap[PcdValue] = []
1537 VpdFile.Add(DscPcdEntry, SkuName, Sku.VpdOffset)
1538 SkuValueMap[PcdValue].append(Sku)
1539 if not NeedProcessVpdMapFile and Sku.VpdOffset == "*":
1540 NeedProcessVpdMapFile = True
1541 if DscPcdEntry.DatumType == TAB_VOID and PcdValue.startswith("L"):
1542 UnicodePcdArray.add(DscPcdEntry)
1543 elif len(Sku.VariableName) > 0:
1544 HiiPcdArray.add(DscPcdEntry)
1545 else:
1546 OtherPcdArray.add(DscPcdEntry)
1547
1548 # if the offset of a VPD is *, then it need to be fixed up by third party tool.
1549 VpdSkuMap[DscPcd] = SkuValueMap
1550 if (self.Platform.FlashDefinition is None or self.Platform.FlashDefinition == '') and \
1551 VpdFile.GetCount() != 0:
1552 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE,
1553 "Fail to get FLASH_DEFINITION definition in DSC file %s which is required when DSC contains VPD PCD." % str(self.Platform.MetaFile))
1554
1555 if VpdFile.GetCount() != 0:
1556
1557 self.FixVpdOffset(VpdFile)
1558
1559 self.FixVpdOffset(self.UpdateNVStoreMaxSize(VpdFile))
1560
1561 # Process VPD map file generated by third party BPDG tool
1562 if NeedProcessVpdMapFile:
1563 VpdMapFilePath = os.path.join(self.BuildDir, TAB_FV_DIRECTORY, "%s.map" % self.Platform.VpdToolGuid)
1564 if os.path.exists(VpdMapFilePath):
1565 VpdFile.Read(VpdMapFilePath)
1566
1567 # Fixup "*" offset
1568 for pcd in VpdSkuMap:
1569 vpdinfo = VpdFile.GetVpdInfo(pcd)
1570 if vpdinfo is None:
1571 # just pick the a value to determine whether is unicode string type
1572 continue
1573 for pcdvalue in VpdSkuMap[pcd]:
1574 for sku in VpdSkuMap[pcd][pcdvalue]:
1575 for item in vpdinfo:
1576 if item[2] == pcdvalue:
1577 sku.VpdOffset = item[1]
1578 else:
1579 EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)
1580
1581 # Delete the DynamicPcdList At the last time enter into this function
1582 for Pcd in self._DynamicPcdList:
1583 # just pick the a value to determine whether is unicode string type
1584 Sku = Pcd.SkuInfoList.values()[0]
1585 Sku.VpdOffset = Sku.VpdOffset.strip()
1586
1587 if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:
1588 Pcd.DatumType = TAB_VOID
1589
1590 PcdValue = Sku.DefaultValue
1591 if Pcd.DatumType == TAB_VOID and PcdValue.startswith("L"):
1592 # if found PCD which datum value is unicode string the insert to left size of UnicodeIndex
1593 UnicodePcdArray.add(Pcd)
1594 elif len(Sku.VariableName) > 0:
1595 # if found HII type PCD then insert to right of UnicodeIndex
1596 HiiPcdArray.add(Pcd)
1597 else:
1598 OtherPcdArray.add(Pcd)
1599 del self._DynamicPcdList[:]
1600 self._DynamicPcdList.extend(list(UnicodePcdArray))
1601 self._DynamicPcdList.extend(list(HiiPcdArray))
1602 self._DynamicPcdList.extend(list(OtherPcdArray))
1603 allskuset = [(SkuName, Sku.SkuId) for pcd in self._DynamicPcdList for (SkuName, Sku) in pcd.SkuInfoList.items()]
1604 for pcd in self._DynamicPcdList:
1605 if len(pcd.SkuInfoList) == 1:
1606 for (SkuName, SkuId) in allskuset:
1607 if type(SkuId) in (str, unicode) and eval(SkuId) == 0 or SkuId == 0:
1608 continue
1609 pcd.SkuInfoList[SkuName] = copy.deepcopy(pcd.SkuInfoList[TAB_DEFAULT])
1610 pcd.SkuInfoList[SkuName].SkuId = SkuId
1611 pcd.SkuInfoList[SkuName].SkuIdName = SkuName
1612 self.AllPcdList = self._NonDynamicPcdList + self._DynamicPcdList
1613
1614 def FixVpdOffset(self, VpdFile ):
1615 FvPath = os.path.join(self.BuildDir, TAB_FV_DIRECTORY)
1616 if not os.path.exists(FvPath):
1617 try:
1618 os.makedirs(FvPath)
1619 except:
1620 EdkLogger.error("build", FILE_WRITE_FAILURE, "Fail to create FV folder under %s" % self.BuildDir)
1621
1622 VpdFilePath = os.path.join(FvPath, "%s.txt" % self.Platform.VpdToolGuid)
1623
1624 if VpdFile.Write(VpdFilePath):
1625 # retrieve BPDG tool's path from tool_def.txt according to VPD_TOOL_GUID defined in DSC file.
1626 BPDGToolName = None
1627 for ToolDef in self.ToolDefinition.values():
1628 if TAB_GUID in ToolDef and ToolDef[TAB_GUID] == self.Platform.VpdToolGuid:
1629 if "PATH" not in ToolDef:
1630 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "PATH attribute was not provided for BPDG guid tool %s in tools_def.txt" % self.Platform.VpdToolGuid)
1631 BPDGToolName = ToolDef["PATH"]
1632 break
1633 # Call third party GUID BPDG tool.
1634 if BPDGToolName is not None:
1635 VpdInfoFile.CallExtenalBPDGTool(BPDGToolName, VpdFilePath)
1636 else:
1637 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.")
1638
1639 ## Return the platform build data object
1640 @cached_property
1641 def Platform(self):
1642 return self.BuildDatabase[self.MetaFile, self.Arch, self.BuildTarget, self.ToolChain]
1643
1644 ## Return platform name
1645 @cached_property
1646 def Name(self):
1647 return self.Platform.PlatformName
1648
1649 ## Return the meta file GUID
1650 @cached_property
1651 def Guid(self):
1652 return self.Platform.Guid
1653
1654 ## Return the platform version
1655 @cached_property
1656 def Version(self):
1657 return self.Platform.Version
1658
1659 ## Return the FDF file name
1660 @cached_property
1661 def FdfFile(self):
1662 if self.Workspace.FdfFile:
1663 RetVal= mws.join(self.WorkspaceDir, self.Workspace.FdfFile)
1664 else:
1665 RetVal = ''
1666 return RetVal
1667
1668 ## Return the build output directory platform specifies
1669 @cached_property
1670 def OutputDir(self):
1671 return self.Platform.OutputDirectory
1672
1673 ## Return the directory to store all intermediate and final files built
1674 @cached_property
1675 def BuildDir(self):
1676 if os.path.isabs(self.OutputDir):
1677 GlobalData.gBuildDirectory = RetVal = path.join(
1678 path.abspath(self.OutputDir),
1679 self.BuildTarget + "_" + self.ToolChain,
1680 )
1681 else:
1682 GlobalData.gBuildDirectory = RetVal = path.join(
1683 self.WorkspaceDir,
1684 self.OutputDir,
1685 self.BuildTarget + "_" + self.ToolChain,
1686 )
1687 return RetVal
1688
1689 ## Return directory of platform makefile
1690 #
1691 # @retval string Makefile directory
1692 #
1693 @cached_property
1694 def MakeFileDir(self):
1695 return path.join(self.BuildDir, self.Arch)
1696
1697 ## Return build command string
1698 #
1699 # @retval string Build command string
1700 #
1701 @cached_property
1702 def BuildCommand(self):
1703 RetVal = []
1704 if "MAKE" in self.ToolDefinition and "PATH" in self.ToolDefinition["MAKE"]:
1705 RetVal += SplitOption(self.ToolDefinition["MAKE"]["PATH"])
1706 if "FLAGS" in self.ToolDefinition["MAKE"]:
1707 NewOption = self.ToolDefinition["MAKE"]["FLAGS"].strip()
1708 if NewOption != '':
1709 RetVal += SplitOption(NewOption)
1710 if "MAKE" in self.EdkIIBuildOption:
1711 if "FLAGS" in self.EdkIIBuildOption["MAKE"]:
1712 Flags = self.EdkIIBuildOption["MAKE"]["FLAGS"]
1713 if Flags.startswith('='):
1714 RetVal = [RetVal[0]] + [Flags[1:]]
1715 else:
1716 RetVal.append(Flags)
1717 return RetVal
1718
1719 ## Get tool chain definition
1720 #
1721 # Get each tool defition for given tool chain from tools_def.txt and platform
1722 #
1723 @cached_property
1724 def ToolDefinition(self):
1725 ToolDefinition = self.Workspace.ToolDef.ToolsDefTxtDictionary
1726 if TAB_TOD_DEFINES_COMMAND_TYPE not in self.Workspace.ToolDef.ToolsDefTxtDatabase:
1727 EdkLogger.error('build', RESOURCE_NOT_AVAILABLE, "No tools found in configuration",
1728 ExtraData="[%s]" % self.MetaFile)
1729 RetVal = {}
1730 DllPathList = set()
1731 for Def in ToolDefinition:
1732 Target, Tag, Arch, Tool, Attr = Def.split("_")
1733 if Target != self.BuildTarget or Tag != self.ToolChain or Arch != self.Arch:
1734 continue
1735
1736 Value = ToolDefinition[Def]
1737 # don't record the DLL
1738 if Attr == "DLL":
1739 DllPathList.add(Value)
1740 continue
1741
1742 if Tool not in RetVal:
1743 RetVal[Tool] = {}
1744 RetVal[Tool][Attr] = Value
1745
1746 ToolsDef = ''
1747 if GlobalData.gOptions.SilentMode and "MAKE" in RetVal:
1748 if "FLAGS" not in RetVal["MAKE"]:
1749 RetVal["MAKE"]["FLAGS"] = ""
1750 RetVal["MAKE"]["FLAGS"] += " -s"
1751 MakeFlags = ''
1752 for Tool in RetVal:
1753 for Attr in RetVal[Tool]:
1754 Value = RetVal[Tool][Attr]
1755 if Tool in self._BuildOptionWithToolDef(RetVal) and Attr in self._BuildOptionWithToolDef(RetVal)[Tool]:
1756 # check if override is indicated
1757 if self._BuildOptionWithToolDef(RetVal)[Tool][Attr].startswith('='):
1758 Value = self._BuildOptionWithToolDef(RetVal)[Tool][Attr][1:]
1759 else:
1760 if Attr != 'PATH':
1761 Value += " " + self._BuildOptionWithToolDef(RetVal)[Tool][Attr]
1762 else:
1763 Value = self._BuildOptionWithToolDef(RetVal)[Tool][Attr]
1764
1765 if Attr == "PATH":
1766 # Don't put MAKE definition in the file
1767 if Tool != "MAKE":
1768 ToolsDef += "%s = %s\n" % (Tool, Value)
1769 elif Attr != "DLL":
1770 # Don't put MAKE definition in the file
1771 if Tool == "MAKE":
1772 if Attr == "FLAGS":
1773 MakeFlags = Value
1774 else:
1775 ToolsDef += "%s_%s = %s\n" % (Tool, Attr, Value)
1776 ToolsDef += "\n"
1777
1778 SaveFileOnChange(self.ToolDefinitionFile, ToolsDef)
1779 for DllPath in DllPathList:
1780 os.environ["PATH"] = DllPath + os.pathsep + os.environ["PATH"]
1781 os.environ["MAKE_FLAGS"] = MakeFlags
1782
1783 return RetVal
1784
1785 ## Return the paths of tools
1786 @cached_property
1787 def ToolDefinitionFile(self):
1788 return os.path.join(self.MakeFileDir, "TOOLS_DEF." + self.Arch)
1789
1790 ## Retrieve the toolchain family of given toolchain tag. Default to 'MSFT'.
1791 @cached_property
1792 def ToolChainFamily(self):
1793 ToolDefinition = self.Workspace.ToolDef.ToolsDefTxtDatabase
1794 if TAB_TOD_DEFINES_FAMILY not in ToolDefinition \
1795 or self.ToolChain not in ToolDefinition[TAB_TOD_DEFINES_FAMILY] \
1796 or not ToolDefinition[TAB_TOD_DEFINES_FAMILY][self.ToolChain]:
1797 EdkLogger.verbose("No tool chain family found in configuration for %s. Default to MSFT." \
1798 % self.ToolChain)
1799 RetVal = TAB_COMPILER_MSFT
1800 else:
1801 RetVal = ToolDefinition[TAB_TOD_DEFINES_FAMILY][self.ToolChain]
1802 return RetVal
1803
1804 @cached_property
1805 def BuildRuleFamily(self):
1806 ToolDefinition = self.Workspace.ToolDef.ToolsDefTxtDatabase
1807 if TAB_TOD_DEFINES_BUILDRULEFAMILY not in ToolDefinition \
1808 or self.ToolChain not in ToolDefinition[TAB_TOD_DEFINES_BUILDRULEFAMILY] \
1809 or not ToolDefinition[TAB_TOD_DEFINES_BUILDRULEFAMILY][self.ToolChain]:
1810 EdkLogger.verbose("No tool chain family found in configuration for %s. Default to MSFT." \
1811 % self.ToolChain)
1812 return TAB_COMPILER_MSFT
1813
1814 return ToolDefinition[TAB_TOD_DEFINES_BUILDRULEFAMILY][self.ToolChain]
1815
1816 ## Return the build options specific for all modules in this platform
1817 @cached_property
1818 def BuildOption(self):
1819 return self._ExpandBuildOption(self.Platform.BuildOptions)
1820
1821 def _BuildOptionWithToolDef(self, ToolDef):
1822 return self._ExpandBuildOption(self.Platform.BuildOptions, ToolDef=ToolDef)
1823
1824 ## Return the build options specific for EDK modules in this platform
1825 @cached_property
1826 def EdkBuildOption(self):
1827 return self._ExpandBuildOption(self.Platform.BuildOptions, EDK_NAME)
1828
1829 ## Return the build options specific for EDKII modules in this platform
1830 @cached_property
1831 def EdkIIBuildOption(self):
1832 return self._ExpandBuildOption(self.Platform.BuildOptions, EDKII_NAME)
1833
1834 ## Parse build_rule.txt in Conf Directory.
1835 #
1836 # @retval BuildRule object
1837 #
1838 @cached_property
1839 def BuildRule(self):
1840 BuildRuleFile = None
1841 if TAB_TAT_DEFINES_BUILD_RULE_CONF in self.Workspace.TargetTxt.TargetTxtDictionary:
1842 BuildRuleFile = self.Workspace.TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_BUILD_RULE_CONF]
1843 if not BuildRuleFile:
1844 BuildRuleFile = gDefaultBuildRuleFile
1845 RetVal = BuildRule(BuildRuleFile)
1846 if RetVal._FileVersion == "":
1847 RetVal._FileVersion = AutoGenReqBuildRuleVerNum
1848 else:
1849 if RetVal._FileVersion < AutoGenReqBuildRuleVerNum :
1850 # If Build Rule's version is less than the version number required by the tools, halting the build.
1851 EdkLogger.error("build", AUTOGEN_ERROR,
1852 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])"\
1853 % (RetVal._FileVersion, AutoGenReqBuildRuleVerNum))
1854 return RetVal
1855
1856 ## Summarize the packages used by modules in this platform
1857 @cached_property
1858 def PackageList(self):
1859 RetVal = set()
1860 for La in self.LibraryAutoGenList:
1861 RetVal.update(La.DependentPackageList)
1862 for Ma in self.ModuleAutoGenList:
1863 RetVal.update(Ma.DependentPackageList)
1864 #Collect package set information from INF of FDF
1865 for ModuleFile in self._AsBuildModuleList:
1866 if ModuleFile in self.Platform.Modules:
1867 continue
1868 ModuleData = self.BuildDatabase[ModuleFile, self.Arch, self.BuildTarget, self.ToolChain]
1869 RetVal.update(ModuleData.Packages)
1870 return list(RetVal)
1871
1872 @cached_property
1873 def NonDynamicPcdDict(self):
1874 return {(Pcd.TokenCName, Pcd.TokenSpaceGuidCName):Pcd for Pcd in self.NonDynamicPcdList}
1875
1876 ## Get list of non-dynamic PCDs
1877 @cached_property
1878 def NonDynamicPcdList(self):
1879 self.CollectPlatformDynamicPcds()
1880 return self._NonDynamicPcdList
1881
1882 ## Get list of dynamic PCDs
1883 @cached_property
1884 def DynamicPcdList(self):
1885 self.CollectPlatformDynamicPcds()
1886 return self._DynamicPcdList
1887
1888 ## Generate Token Number for all PCD
1889 @cached_property
1890 def PcdTokenNumber(self):
1891 RetVal = OrderedDict()
1892 TokenNumber = 1
1893 #
1894 # Make the Dynamic and DynamicEx PCD use within different TokenNumber area.
1895 # Such as:
1896 #
1897 # Dynamic PCD:
1898 # TokenNumber 0 ~ 10
1899 # DynamicEx PCD:
1900 # TokeNumber 11 ~ 20
1901 #
1902 for Pcd in self.DynamicPcdList:
1903 if Pcd.Phase == "PEI" and Pcd.Type in PCD_DYNAMIC_TYPE_SET:
1904 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
1905 RetVal[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
1906 TokenNumber += 1
1907
1908 for Pcd in self.DynamicPcdList:
1909 if Pcd.Phase == "PEI" and Pcd.Type in PCD_DYNAMIC_EX_TYPE_SET:
1910 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
1911 RetVal[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
1912 TokenNumber += 1
1913
1914 for Pcd in self.DynamicPcdList:
1915 if Pcd.Phase == "DXE" and Pcd.Type in PCD_DYNAMIC_TYPE_SET:
1916 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
1917 RetVal[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
1918 TokenNumber += 1
1919
1920 for Pcd in self.DynamicPcdList:
1921 if Pcd.Phase == "DXE" and Pcd.Type in PCD_DYNAMIC_EX_TYPE_SET:
1922 EdkLogger.debug(EdkLogger.DEBUG_5, "%s %s (%s) -> %d" % (Pcd.TokenCName, Pcd.TokenSpaceGuidCName, Pcd.Phase, TokenNumber))
1923 RetVal[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
1924 TokenNumber += 1
1925
1926 for Pcd in self.NonDynamicPcdList:
1927 RetVal[Pcd.TokenCName, Pcd.TokenSpaceGuidCName] = TokenNumber
1928 TokenNumber += 1
1929 return RetVal
1930
1931 @cached_property
1932 def _MaList(self):
1933 for ModuleFile in self.Platform.Modules:
1934 Ma = ModuleAutoGen(
1935 self.Workspace,
1936 ModuleFile,
1937 self.BuildTarget,
1938 self.ToolChain,
1939 self.Arch,
1940 self.MetaFile
1941 )
1942 self.Platform.Modules[ModuleFile].M = Ma
1943 return [x.M for x in self.Platform.Modules.values()]
1944
1945 ## Summarize ModuleAutoGen objects of all modules to be built for this platform
1946 @cached_property
1947 def ModuleAutoGenList(self):
1948 RetVal = []
1949 for Ma in self._MaList:
1950 if Ma not in RetVal:
1951 RetVal.append(Ma)
1952 return RetVal
1953
1954 ## Summarize ModuleAutoGen objects of all libraries to be built for this platform
1955 @cached_property
1956 def LibraryAutoGenList(self):
1957 RetVal = []
1958 for Ma in self._MaList:
1959 for La in Ma.LibraryAutoGenList:
1960 if La not in RetVal:
1961 RetVal.append(La)
1962 if Ma not in La.ReferenceModules:
1963 La.ReferenceModules.append(Ma)
1964 return RetVal
1965
1966 ## Test if a module is supported by the platform
1967 #
1968 # An error will be raised directly if the module or its arch is not supported
1969 # by the platform or current configuration
1970 #
1971 def ValidModule(self, Module):
1972 return Module in self.Platform.Modules or Module in self.Platform.LibraryInstances \
1973 or Module in self._AsBuildModuleList
1974
1975 ## Resolve the library classes in a module to library instances
1976 #
1977 # This method will not only resolve library classes but also sort the library
1978 # instances according to the dependency-ship.
1979 #
1980 # @param Module The module from which the library classes will be resolved
1981 #
1982 # @retval library_list List of library instances sorted
1983 #
1984 def ApplyLibraryInstance(self, Module):
1985 # Cover the case that the binary INF file is list in the FDF file but not DSC file, return empty list directly
1986 if str(Module) not in self.Platform.Modules:
1987 return []
1988
1989 return GetModuleLibInstances(Module,
1990 self.Platform,
1991 self.BuildDatabase,
1992 self.Arch,
1993 self.BuildTarget,
1994 self.ToolChain,
1995 self.MetaFile,
1996 EdkLogger)
1997
1998 ## Override PCD setting (type, value, ...)
1999 #
2000 # @param ToPcd The PCD to be overrided
2001 # @param FromPcd The PCD overrideing from
2002 #
2003 def _OverridePcd(self, ToPcd, FromPcd, Module="", Msg="", Library=""):
2004 #
2005 # in case there's PCDs coming from FDF file, which have no type given.
2006 # at this point, ToPcd.Type has the type found from dependent
2007 # package
2008 #
2009 TokenCName = ToPcd.TokenCName
2010 for PcdItem in GlobalData.MixedPcd:
2011 if (ToPcd.TokenCName, ToPcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
2012 TokenCName = PcdItem[0]
2013 break
2014 if FromPcd is not None:
2015 if ToPcd.Pending and FromPcd.Type:
2016 ToPcd.Type = FromPcd.Type
2017 elif ToPcd.Type and FromPcd.Type\
2018 and ToPcd.Type != FromPcd.Type and ToPcd.Type in FromPcd.Type:
2019 if ToPcd.Type.strip() == TAB_PCDS_DYNAMIC_EX:
2020 ToPcd.Type = FromPcd.Type
2021 elif ToPcd.Type and FromPcd.Type \
2022 and ToPcd.Type != FromPcd.Type:
2023 if Library:
2024 Module = str(Module) + " 's library file (" + str(Library) + ")"
2025 EdkLogger.error("build", OPTION_CONFLICT, "Mismatched PCD type",
2026 ExtraData="%s.%s is used as [%s] in module %s, but as [%s] in %s."\
2027 % (ToPcd.TokenSpaceGuidCName, TokenCName,
2028 ToPcd.Type, Module, FromPcd.Type, Msg),
2029 File=self.MetaFile)
2030
2031 if FromPcd.MaxDatumSize:
2032 ToPcd.MaxDatumSize = FromPcd.MaxDatumSize
2033 ToPcd.MaxSizeUserSet = FromPcd.MaxDatumSize
2034 if FromPcd.DefaultValue:
2035 ToPcd.DefaultValue = FromPcd.DefaultValue
2036 if FromPcd.TokenValue:
2037 ToPcd.TokenValue = FromPcd.TokenValue
2038 if FromPcd.DatumType:
2039 ToPcd.DatumType = FromPcd.DatumType
2040 if FromPcd.SkuInfoList:
2041 ToPcd.SkuInfoList = FromPcd.SkuInfoList
2042 if FromPcd.UserDefinedDefaultStoresFlag:
2043 ToPcd.UserDefinedDefaultStoresFlag = FromPcd.UserDefinedDefaultStoresFlag
2044 # Add Flexible PCD format parse
2045 if ToPcd.DefaultValue:
2046 try:
2047 ToPcd.DefaultValue = ValueExpressionEx(ToPcd.DefaultValue, ToPcd.DatumType, self.Workspace._GuidDict)(True)
2048 except BadExpression as Value:
2049 EdkLogger.error('Parser', FORMAT_INVALID, 'PCD [%s.%s] Value "%s", %s' %(ToPcd.TokenSpaceGuidCName, ToPcd.TokenCName, ToPcd.DefaultValue, Value),
2050 File=self.MetaFile)
2051
2052 # check the validation of datum
2053 IsValid, Cause = CheckPcdDatum(ToPcd.DatumType, ToPcd.DefaultValue)
2054 if not IsValid:
2055 EdkLogger.error('build', FORMAT_INVALID, Cause, File=self.MetaFile,
2056 ExtraData="%s.%s" % (ToPcd.TokenSpaceGuidCName, TokenCName))
2057 ToPcd.validateranges = FromPcd.validateranges
2058 ToPcd.validlists = FromPcd.validlists
2059 ToPcd.expressions = FromPcd.expressions
2060 ToPcd.CustomAttribute = FromPcd.CustomAttribute
2061
2062 if FromPcd is not None and ToPcd.DatumType == TAB_VOID and not ToPcd.MaxDatumSize:
2063 EdkLogger.debug(EdkLogger.DEBUG_9, "No MaxDatumSize specified for PCD %s.%s" \
2064 % (ToPcd.TokenSpaceGuidCName, TokenCName))
2065 Value = ToPcd.DefaultValue
2066 if not Value:
2067 ToPcd.MaxDatumSize = '1'
2068 elif Value[0] == 'L':
2069 ToPcd.MaxDatumSize = str((len(Value) - 2) * 2)
2070 elif Value[0] == '{':
2071 ToPcd.MaxDatumSize = str(len(Value.split(',')))
2072 else:
2073 ToPcd.MaxDatumSize = str(len(Value) - 1)
2074
2075 # apply default SKU for dynamic PCDS if specified one is not available
2076 if (ToPcd.Type in PCD_DYNAMIC_TYPE_SET or ToPcd.Type in PCD_DYNAMIC_EX_TYPE_SET) \
2077 and not ToPcd.SkuInfoList:
2078 if self.Platform.SkuName in self.Platform.SkuIds:
2079 SkuName = self.Platform.SkuName
2080 else:
2081 SkuName = TAB_DEFAULT
2082 ToPcd.SkuInfoList = {
2083 SkuName : SkuInfoClass(SkuName, self.Platform.SkuIds[SkuName][0], '', '', '', '', '', ToPcd.DefaultValue)
2084 }
2085
2086 ## Apply PCD setting defined platform to a module
2087 #
2088 # @param Module The module from which the PCD setting will be overrided
2089 #
2090 # @retval PCD_list The list PCDs with settings from platform
2091 #
2092 def ApplyPcdSetting(self, Module, Pcds, Library=""):
2093 # for each PCD in module
2094 for Name, Guid in Pcds:
2095 PcdInModule = Pcds[Name, Guid]
2096 # find out the PCD setting in platform
2097 if (Name, Guid) in self.Platform.Pcds:
2098 PcdInPlatform = self.Platform.Pcds[Name, Guid]
2099 else:
2100 PcdInPlatform = None
2101 # then override the settings if any
2102 self._OverridePcd(PcdInModule, PcdInPlatform, Module, Msg="DSC PCD sections", Library=Library)
2103 # resolve the VariableGuid value
2104 for SkuId in PcdInModule.SkuInfoList:
2105 Sku = PcdInModule.SkuInfoList[SkuId]
2106 if Sku.VariableGuid == '': continue
2107 Sku.VariableGuidValue = GuidValue(Sku.VariableGuid, self.PackageList, self.MetaFile.Path)
2108 if Sku.VariableGuidValue is None:
2109 PackageList = "\n\t".join(str(P) for P in self.PackageList)
2110 EdkLogger.error(
2111 'build',
2112 RESOURCE_NOT_AVAILABLE,
2113 "Value of GUID [%s] is not found in" % Sku.VariableGuid,
2114 ExtraData=PackageList + "\n\t(used with %s.%s from module %s)" \
2115 % (Guid, Name, str(Module)),
2116 File=self.MetaFile
2117 )
2118
2119 # override PCD settings with module specific setting
2120 if Module in self.Platform.Modules:
2121 PlatformModule = self.Platform.Modules[str(Module)]
2122 for Key in PlatformModule.Pcds:
2123 Flag = False
2124 if Key in Pcds:
2125 ToPcd = Pcds[Key]
2126 Flag = True
2127 elif Key in GlobalData.MixedPcd:
2128 for PcdItem in GlobalData.MixedPcd[Key]:
2129 if PcdItem in Pcds:
2130 ToPcd = Pcds[PcdItem]
2131 Flag = True
2132 break
2133 if Flag:
2134 self._OverridePcd(ToPcd, PlatformModule.Pcds[Key], Module, Msg="DSC Components Module scoped PCD section", Library=Library)
2135 # use PCD value to calculate the MaxDatumSize when it is not specified
2136 for Name, Guid in Pcds:
2137 Pcd = Pcds[Name, Guid]
2138 if Pcd.DatumType == TAB_VOID and not Pcd.MaxDatumSize:
2139 Pcd.MaxSizeUserSet = None
2140 Value = Pcd.DefaultValue
2141 if not Value:
2142 Pcd.MaxDatumSize = '1'
2143 elif Value[0] == 'L':
2144 Pcd.MaxDatumSize = str((len(Value) - 2) * 2)
2145 elif Value[0] == '{':
2146 Pcd.MaxDatumSize = str(len(Value.split(',')))
2147 else:
2148 Pcd.MaxDatumSize = str(len(Value) - 1)
2149 return Pcds.values()
2150
2151 ## Resolve library names to library modules
2152 #
2153 # (for Edk.x modules)
2154 #
2155 # @param Module The module from which the library names will be resolved
2156 #
2157 # @retval library_list The list of library modules
2158 #
2159 def ResolveLibraryReference(self, Module):
2160 EdkLogger.verbose("")
2161 EdkLogger.verbose("Library instances of module [%s] [%s]:" % (str(Module), self.Arch))
2162 LibraryConsumerList = [Module]
2163
2164 # "CompilerStub" is a must for Edk modules
2165 if Module.Libraries:
2166 Module.Libraries.append("CompilerStub")
2167 LibraryList = []
2168 while len(LibraryConsumerList) > 0:
2169 M = LibraryConsumerList.pop()
2170 for LibraryName in M.Libraries:
2171 Library = self.Platform.LibraryClasses[LibraryName, ':dummy:']
2172 if Library is None:
2173 for Key in self.Platform.LibraryClasses.data:
2174 if LibraryName.upper() == Key.upper():
2175 Library = self.Platform.LibraryClasses[Key, ':dummy:']
2176 break
2177 if Library is None:
2178 EdkLogger.warn("build", "Library [%s] is not found" % LibraryName, File=str(M),
2179 ExtraData="\t%s [%s]" % (str(Module), self.Arch))
2180 continue
2181
2182 if Library not in LibraryList:
2183 LibraryList.append(Library)
2184 LibraryConsumerList.append(Library)
2185 EdkLogger.verbose("\t" + LibraryName + " : " + str(Library) + ' ' + str(type(Library)))
2186 return LibraryList
2187
2188 ## Calculate the priority value of the build option
2189 #
2190 # @param Key Build option definition contain: TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
2191 #
2192 # @retval Value Priority value based on the priority list.
2193 #
2194 def CalculatePriorityValue(self, Key):
2195 Target, ToolChain, Arch, CommandType, Attr = Key.split('_')
2196 PriorityValue = 0x11111
2197 if Target == "*":
2198 PriorityValue &= 0x01111
2199 if ToolChain == "*":
2200 PriorityValue &= 0x10111
2201 if Arch == "*":
2202 PriorityValue &= 0x11011
2203 if CommandType == "*":
2204 PriorityValue &= 0x11101
2205 if Attr == "*":
2206 PriorityValue &= 0x11110
2207
2208 return self.PrioList["0x%0.5x" % PriorityValue]
2209
2210
2211 ## Expand * in build option key
2212 #
2213 # @param Options Options to be expanded
2214 # @param ToolDef Use specified ToolDef instead of full version.
2215 # This is needed during initialization to prevent
2216 # infinite recursion betweeh BuildOptions,
2217 # ToolDefinition, and this function.
2218 #
2219 # @retval options Options expanded
2220 #
2221 def _ExpandBuildOption(self, Options, ModuleStyle=None, ToolDef=None):
2222 if not ToolDef:
2223 ToolDef = self.ToolDefinition
2224 BuildOptions = {}
2225 FamilyMatch = False
2226 FamilyIsNull = True
2227
2228 OverrideList = {}
2229 #
2230 # Construct a list contain the build options which need override.
2231 #
2232 for Key in Options:
2233 #
2234 # Key[0] -- tool family
2235 # Key[1] -- TARGET_TOOLCHAIN_ARCH_COMMANDTYPE_ATTRIBUTE
2236 #
2237 if (Key[0] == self.BuildRuleFamily and
2238 (ModuleStyle is None or len(Key) < 3 or (len(Key) > 2 and Key[2] == ModuleStyle))):
2239 Target, ToolChain, Arch, CommandType, Attr = Key[1].split('_')
2240 if (Target == self.BuildTarget or Target == "*") and\
2241 (ToolChain == self.ToolChain or ToolChain == "*") and\
2242 (Arch == self.Arch or Arch == "*") and\
2243 Options[Key].startswith("="):
2244
2245 if OverrideList.get(Key[1]) is not None:
2246 OverrideList.pop(Key[1])
2247 OverrideList[Key[1]] = Options[Key]
2248
2249 #
2250 # Use the highest priority value.
2251 #
2252 if (len(OverrideList) >= 2):
2253 KeyList = OverrideList.keys()
2254 for Index in range(len(KeyList)):
2255 NowKey = KeyList[Index]
2256 Target1, ToolChain1, Arch1, CommandType1, Attr1 = NowKey.split("_")
2257 for Index1 in range(len(KeyList) - Index - 1):
2258 NextKey = KeyList[Index1 + Index + 1]
2259 #
2260 # Compare two Key, if one is included by another, choose the higher priority one
2261 #
2262 Target2, ToolChain2, Arch2, CommandType2, Attr2 = NextKey.split("_")
2263 if (Target1 == Target2 or Target1 == "*" or Target2 == "*") and\
2264 (ToolChain1 == ToolChain2 or ToolChain1 == "*" or ToolChain2 == "*") and\
2265 (Arch1 == Arch2 or Arch1 == "*" or Arch2 == "*") and\
2266 (CommandType1 == CommandType2 or CommandType1 == "*" or CommandType2 == "*") and\
2267 (Attr1 == Attr2 or Attr1 == "*" or Attr2 == "*"):
2268
2269 if self.CalculatePriorityValue(NowKey) > self.CalculatePriorityValue(NextKey):
2270 if Options.get((self.BuildRuleFamily, NextKey)) is not None:
2271 Options.pop((self.BuildRuleFamily, NextKey))
2272 else:
2273 if Options.get((self.BuildRuleFamily, NowKey)) is not None:
2274 Options.pop((self.BuildRuleFamily, NowKey))
2275
2276 for Key in Options:
2277 if ModuleStyle is not None and len (Key) > 2:
2278 # Check Module style is EDK or EDKII.
2279 # Only append build option for the matched style module.
2280 if ModuleStyle == EDK_NAME and Key[2] != EDK_NAME:
2281 continue
2282 elif ModuleStyle == EDKII_NAME and Key[2] != EDKII_NAME:
2283 continue
2284 Family = Key[0]
2285 Target, Tag, Arch, Tool, Attr = Key[1].split("_")
2286 # if tool chain family doesn't match, skip it
2287 if Tool in ToolDef and Family != "":
2288 FamilyIsNull = False
2289 if ToolDef[Tool].get(TAB_TOD_DEFINES_BUILDRULEFAMILY, "") != "":
2290 if Family != ToolDef[Tool][TAB_TOD_DEFINES_BUILDRULEFAMILY]:
2291 continue
2292 elif Family != ToolDef[Tool][TAB_TOD_DEFINES_FAMILY]:
2293 continue
2294 FamilyMatch = True
2295 # expand any wildcard
2296 if Target == "*" or Target == self.BuildTarget:
2297 if Tag == "*" or Tag == self.ToolChain:
2298 if Arch == "*" or Arch == self.Arch:
2299 if Tool not in BuildOptions:
2300 BuildOptions[Tool] = {}
2301 if Attr != "FLAGS" or Attr not in BuildOptions[Tool] or Options[Key].startswith('='):
2302 BuildOptions[Tool][Attr] = Options[Key]
2303 else:
2304 # append options for the same tool except PATH
2305 if Attr != 'PATH':
2306 BuildOptions[Tool][Attr] += " " + Options[Key]
2307 else:
2308 BuildOptions[Tool][Attr] = Options[Key]
2309 # Build Option Family has been checked, which need't to be checked again for family.
2310 if FamilyMatch or FamilyIsNull:
2311 return BuildOptions
2312
2313 for Key in Options:
2314 if ModuleStyle is not None and len (Key) > 2:
2315 # Check Module style is EDK or EDKII.
2316 # Only append build option for the matched style module.
2317 if ModuleStyle == EDK_NAME and Key[2] != EDK_NAME:
2318 continue
2319 elif ModuleStyle == EDKII_NAME and Key[2] != EDKII_NAME:
2320 continue
2321 Family = Key[0]
2322 Target, Tag, Arch, Tool, Attr = Key[1].split("_")
2323 # if tool chain family doesn't match, skip it
2324 if Tool not in ToolDef or Family == "":
2325 continue
2326 # option has been added before
2327 if Family != ToolDef[Tool][TAB_TOD_DEFINES_FAMILY]:
2328 continue
2329
2330 # expand any wildcard
2331 if Target == "*" or Target == self.BuildTarget:
2332 if Tag == "*" or Tag == self.ToolChain:
2333 if Arch == "*" or Arch == self.Arch:
2334 if Tool not in BuildOptions:
2335 BuildOptions[Tool] = {}
2336 if Attr != "FLAGS" or Attr not in BuildOptions[Tool] or Options[Key].startswith('='):
2337 BuildOptions[Tool][Attr] = Options[Key]
2338 else:
2339 # append options for the same tool except PATH
2340 if Attr != 'PATH':
2341 BuildOptions[Tool][Attr] += " " + Options[Key]
2342 else:
2343 BuildOptions[Tool][Attr] = Options[Key]
2344 return BuildOptions
2345
2346 ## Append build options in platform to a module
2347 #
2348 # @param Module The module to which the build options will be appened
2349 #
2350 # @retval options The options appended with build options in platform
2351 #
2352 def ApplyBuildOption(self, Module):
2353 # Get the different options for the different style module
2354 if Module.AutoGenVersion < 0x00010005:
2355 PlatformOptions = self.EdkBuildOption
2356 ModuleTypeOptions = self.Platform.GetBuildOptionsByModuleType(EDK_NAME, Module.ModuleType)
2357 else:
2358 PlatformOptions = self.EdkIIBuildOption
2359 ModuleTypeOptions = self.Platform.GetBuildOptionsByModuleType(EDKII_NAME, Module.ModuleType)
2360 ModuleTypeOptions = self._ExpandBuildOption(ModuleTypeOptions)
2361 ModuleOptions = self._ExpandBuildOption(Module.BuildOptions)
2362 if Module in self.Platform.Modules:
2363 PlatformModule = self.Platform.Modules[str(Module)]
2364 PlatformModuleOptions = self._ExpandBuildOption(PlatformModule.BuildOptions)
2365 else:
2366 PlatformModuleOptions = {}
2367
2368 BuildRuleOrder = None
2369 for Options in [self.ToolDefinition, ModuleOptions, PlatformOptions, ModuleTypeOptions, PlatformModuleOptions]:
2370 for Tool in Options:
2371 for Attr in Options[Tool]:
2372 if Attr == TAB_TOD_DEFINES_BUILDRULEORDER:
2373 BuildRuleOrder = Options[Tool][Attr]
2374
2375 AllTools = set(ModuleOptions.keys() + PlatformOptions.keys() +
2376 PlatformModuleOptions.keys() + ModuleTypeOptions.keys() +
2377 self.ToolDefinition.keys())
2378 BuildOptions = defaultdict(lambda: defaultdict(str))
2379 for Tool in AllTools:
2380 for Options in [self.ToolDefinition, ModuleOptions, PlatformOptions, ModuleTypeOptions, PlatformModuleOptions]:
2381 if Tool not in Options:
2382 continue
2383 for Attr in Options[Tool]:
2384 #
2385 # Do not generate it in Makefile
2386 #
2387 if Attr == TAB_TOD_DEFINES_BUILDRULEORDER:
2388 continue
2389 Value = Options[Tool][Attr]
2390 # check if override is indicated
2391 if Value.startswith('='):
2392 BuildOptions[Tool][Attr] = mws.handleWsMacro(Value[1:])
2393 else:
2394 if Attr != 'PATH':
2395 BuildOptions[Tool][Attr] += " " + mws.handleWsMacro(Value)
2396 else:
2397 BuildOptions[Tool][Attr] = mws.handleWsMacro(Value)
2398
2399 if Module.AutoGenVersion < 0x00010005 and self.Workspace.UniFlag is not None:
2400 #
2401 # Override UNI flag only for EDK module.
2402 #
2403 BuildOptions['BUILD']['FLAGS'] = self.Workspace.UniFlag
2404 return BuildOptions, BuildRuleOrder
2405
2406 #
2407 # extend lists contained in a dictionary with lists stored in another dictionary
2408 # if CopyToDict is not derived from DefaultDict(list) then this may raise exception
2409 #
2410 def ExtendCopyDictionaryLists(CopyToDict, CopyFromDict):
2411 for Key in CopyFromDict:
2412 CopyToDict[Key].extend(CopyFromDict[Key])
2413
2414 # Create a directory specified by a set of path elements and return the full path
2415 def _MakeDir(PathList):
2416 RetVal = path.join(*PathList)
2417 CreateDirectory(RetVal)
2418 return RetVal
2419
2420 ## ModuleAutoGen class
2421 #
2422 # This class encapsules the AutoGen behaviors for the build tools. In addition to
2423 # the generation of AutoGen.h and AutoGen.c, it will generate *.depex file according
2424 # to the [depex] section in module's inf file.
2425 #
2426 class ModuleAutoGen(AutoGen):
2427 # call super().__init__ then call the worker function with different parameter count
2428 def __init__(self, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
2429 if not hasattr(self, "_Init"):
2430 super(ModuleAutoGen, self).__init__(Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
2431 self._InitWorker(Workspace, MetaFile, Target, Toolchain, Arch, *args)
2432 self._Init = True
2433
2434 ## Cache the timestamps of metafiles of every module in a class attribute
2435 #
2436 TimeDict = {}
2437
2438 def __new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs):
2439 # check if this module is employed by active platform
2440 if not PlatformAutoGen(Workspace, args[0], Target, Toolchain, Arch).ValidModule(MetaFile):
2441 EdkLogger.verbose("Module [%s] for [%s] is not employed by active platform\n" \
2442 % (MetaFile, Arch))
2443 return None
2444 return super(ModuleAutoGen, cls).__new__(cls, Workspace, MetaFile, Target, Toolchain, Arch, *args, **kwargs)
2445
2446 ## Initialize ModuleAutoGen
2447 #
2448 # @param Workspace EdkIIWorkspaceBuild object
2449 # @param ModuleFile The path of module file
2450 # @param Target Build target (DEBUG, RELEASE)
2451 # @param Toolchain Name of tool chain
2452 # @param Arch The arch the module supports
2453 # @param PlatformFile Platform meta-file
2454 #
2455 def _InitWorker(self, Workspace, ModuleFile, Target, Toolchain, Arch, PlatformFile):
2456 EdkLogger.debug(EdkLogger.DEBUG_9, "AutoGen module [%s] [%s]" % (ModuleFile, Arch))
2457 GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (ModuleFile, Arch, Toolchain, Target)
2458
2459 self.Workspace = Workspace
2460 self.WorkspaceDir = Workspace.WorkspaceDir
2461 self.MetaFile = ModuleFile
2462 self.PlatformInfo = PlatformAutoGen(Workspace, PlatformFile, Target, Toolchain, Arch)
2463
2464 self.SourceDir = self.MetaFile.SubDir
2465 self.SourceDir = mws.relpath(self.SourceDir, self.WorkspaceDir)
2466
2467 self.SourceOverrideDir = None
2468 # use overrided path defined in DSC file
2469 if self.MetaFile.Key in GlobalData.gOverrideDir:
2470 self.SourceOverrideDir = GlobalData.gOverrideDir[self.MetaFile.Key]
2471
2472 self.ToolChain = Toolchain
2473 self.BuildTarget = Target
2474 self.Arch = Arch
2475 self.ToolChainFamily = self.PlatformInfo.ToolChainFamily
2476 self.BuildRuleFamily = self.PlatformInfo.BuildRuleFamily
2477
2478 self.IsCodeFileCreated = False
2479 self.IsAsBuiltInfCreated = False
2480 self.DepexGenerated = False
2481
2482 self.BuildDatabase = self.Workspace.BuildDatabase
2483 self.BuildRuleOrder = None
2484 self.BuildTime = 0
2485
2486 self._PcdComments = OrderedListDict()
2487 self._GuidComments = OrderedListDict()
2488 self._ProtocolComments = OrderedListDict()
2489 self._PpiComments = OrderedListDict()
2490 self._BuildTargets = None
2491 self._IntroBuildTargetList = None
2492 self._FinalBuildTargetList = None
2493 self._FileTypes = None
2494
2495 self.AutoGenDepSet = set()
2496 self.ReferenceModules = []
2497 self.ConstPcd = {}
2498
2499
2500 def __repr__(self):
2501 return "%s [%s]" % (self.MetaFile, self.Arch)
2502
2503 # Get FixedAtBuild Pcds of this Module
2504 @cached_property
2505 def FixedAtBuildPcds(self):
2506 RetVal = []
2507 for Pcd in self.ModulePcdList:
2508 if Pcd.Type != TAB_PCDS_FIXED_AT_BUILD:
2509 continue
2510 if Pcd not in RetVal:
2511 RetVal.append(Pcd)
2512 return RetVal
2513
2514 @cached_property
2515 def FixedVoidTypePcds(self):
2516 RetVal = {}
2517 for Pcd in self.FixedAtBuildPcds:
2518 if Pcd.DatumType == TAB_VOID:
2519 if '{}.{}'.format(Pcd.TokenSpaceGuidCName, Pcd.TokenCName) not in RetVal:
2520 RetVal['{}.{}'.format(Pcd.TokenSpaceGuidCName, Pcd.TokenCName)] = Pcd.DefaultValue
2521 return RetVal
2522
2523 @property
2524 def UniqueBaseName(self):
2525 BaseName = self.Name
2526 for Module in self.PlatformInfo.ModuleAutoGenList:
2527 if Module.MetaFile == self.MetaFile:
2528 continue
2529 if Module.Name == self.Name:
2530 if uuid.UUID(Module.Guid) == uuid.UUID(self.Guid):
2531 EdkLogger.error("build", FILE_DUPLICATED, 'Modules have same BaseName and FILE_GUID:\n'
2532 ' %s\n %s' % (Module.MetaFile, self.MetaFile))
2533 BaseName = '%s_%s' % (self.Name, self.Guid)
2534 return BaseName
2535
2536 # Macros could be used in build_rule.txt (also Makefile)
2537 @cached_property
2538 def Macros(self):
2539 return OrderedDict((
2540 ("WORKSPACE" ,self.WorkspaceDir),
2541 ("MODULE_NAME" ,self.Name),
2542 ("MODULE_NAME_GUID" ,self.UniqueBaseName),
2543 ("MODULE_GUID" ,self.Guid),
2544 ("MODULE_VERSION" ,self.Version),
2545 ("MODULE_TYPE" ,self.ModuleType),
2546 ("MODULE_FILE" ,str(self.MetaFile)),
2547 ("MODULE_FILE_BASE_NAME" ,self.MetaFile.BaseName),
2548 ("MODULE_RELATIVE_DIR" ,self.SourceDir),
2549 ("MODULE_DIR" ,self.SourceDir),
2550 ("BASE_NAME" ,self.Name),
2551 ("ARCH" ,self.Arch),
2552 ("TOOLCHAIN" ,self.ToolChain),
2553 ("TOOLCHAIN_TAG" ,self.ToolChain),
2554 ("TOOL_CHAIN_TAG" ,self.ToolChain),
2555 ("TARGET" ,self.BuildTarget),
2556 ("BUILD_DIR" ,self.PlatformInfo.BuildDir),
2557 ("BIN_DIR" ,os.path.join(self.PlatformInfo.BuildDir, self.Arch)),
2558 ("LIB_DIR" ,os.path.join(self.PlatformInfo.BuildDir, self.Arch)),
2559 ("MODULE_BUILD_DIR" ,self.BuildDir),
2560 ("OUTPUT_DIR" ,self.OutputDir),
2561 ("DEBUG_DIR" ,self.DebugDir),
2562 ("DEST_DIR_OUTPUT" ,self.OutputDir),
2563 ("DEST_DIR_DEBUG" ,self.DebugDir),
2564 ("PLATFORM_NAME" ,self.PlatformInfo.Name),
2565 ("PLATFORM_GUID" ,self.PlatformInfo.Guid),
2566 ("PLATFORM_VERSION" ,self.PlatformInfo.Version),
2567 ("PLATFORM_RELATIVE_DIR" ,self.PlatformInfo.SourceDir),
2568 ("PLATFORM_DIR" ,mws.join(self.WorkspaceDir, self.PlatformInfo.SourceDir)),
2569 ("PLATFORM_OUTPUT_DIR" ,self.PlatformInfo.OutputDir),
2570 ("FFS_OUTPUT_DIR" ,self.FfsOutputDir)
2571 ))
2572
2573 ## Return the module build data object
2574 @cached_property
2575 def Module(self):
2576 return self.BuildDatabase[self.MetaFile, self.Arch, self.BuildTarget, self.ToolChain]
2577
2578 ## Return the module name
2579 @cached_property
2580 def Name(self):
2581 return self.Module.BaseName
2582
2583 ## Return the module DxsFile if exist
2584 @cached_property
2585 def DxsFile(self):
2586 return self.Module.DxsFile
2587
2588 ## Return the module meta-file GUID
2589 @cached_property
2590 def Guid(self):
2591 #
2592 # To build same module more than once, the module path with FILE_GUID overridden has
2593 # the file name FILE_GUIDmodule.inf, but the relative path (self.MetaFile.File) is the realy path
2594 # in DSC. The overridden GUID can be retrieved from file name
2595 #
2596 if os.path.basename(self.MetaFile.File) != os.path.basename(self.MetaFile.Path):
2597 #
2598 # Length of GUID is 36
2599 #
2600 return os.path.basename(self.MetaFile.Path)[:36]
2601 return self.Module.Guid
2602
2603 ## Return the module version
2604 @cached_property
2605 def Version(self):
2606 return self.Module.Version
2607
2608 ## Return the module type
2609 @cached_property
2610 def ModuleType(self):
2611 return self.Module.ModuleType
2612
2613 ## Return the component type (for Edk.x style of module)
2614 @cached_property
2615 def ComponentType(self):
2616 return self.Module.ComponentType
2617
2618 ## Return the build type
2619 @cached_property
2620 def BuildType(self):
2621 return self.Module.BuildType
2622
2623 ## Return the PCD_IS_DRIVER setting
2624 @cached_property
2625 def PcdIsDriver(self):
2626 return self.Module.PcdIsDriver
2627
2628 ## Return the autogen version, i.e. module meta-file version
2629 @cached_property
2630 def AutoGenVersion(self):
2631 return self.Module.AutoGenVersion
2632
2633 ## Check if the module is library or not
2634 @cached_property
2635 def IsLibrary(self):
2636 return bool(self.Module.LibraryClass)
2637
2638 ## Check if the module is binary module or not
2639 @cached_property
2640 def IsBinaryModule(self):
2641 return self.Module.IsBinaryModule
2642
2643 ## Return the directory to store intermediate files of the module
2644 @cached_property
2645 def BuildDir(self):
2646 return _MakeDir((
2647 self.PlatformInfo.BuildDir,
2648 self.Arch,
2649 self.SourceDir,
2650 self.MetaFile.BaseName
2651 ))
2652
2653 ## Return the directory to store the intermediate object files of the mdoule
2654 @cached_property
2655 def OutputDir(self):
2656 return _MakeDir((self.BuildDir, "OUTPUT"))
2657
2658 ## Return the directory path to store ffs file
2659 @cached_property
2660 def FfsOutputDir(self):
2661 if GlobalData.gFdfParser:
2662 return path.join(self.PlatformInfo.BuildDir, TAB_FV_DIRECTORY, "Ffs", self.Guid + self.Name)
2663 return ''
2664
2665 ## Return the directory to store auto-gened source files of the mdoule
2666 @cached_property
2667 def DebugDir(self):
2668 return _MakeDir((self.BuildDir, "DEBUG"))
2669
2670 ## Return the path of custom file
2671 @cached_property
2672 def CustomMakefile(self):
2673 RetVal = {}
2674 for Type in self.Module.CustomMakefile:
2675 MakeType = gMakeTypeMap[Type] if Type in gMakeTypeMap else 'nmake'
2676 if self.SourceOverrideDir is not None:
2677 File = os.path.join(self.SourceOverrideDir, self.Module.CustomMakefile[Type])
2678 if not os.path.exists(File):
2679 File = os.path.join(self.SourceDir, self.Module.CustomMakefile[Type])
2680 else:
2681 File = os.path.join(self.SourceDir, self.Module.CustomMakefile[Type])
2682 RetVal[MakeType] = File
2683 return RetVal
2684
2685 ## Return the directory of the makefile
2686 #
2687 # @retval string The directory string of module's makefile
2688 #
2689 @cached_property
2690 def MakeFileDir(self):
2691 return self.BuildDir
2692
2693 ## Return build command string
2694 #
2695 # @retval string Build command string
2696 #
2697 @cached_property
2698 def BuildCommand(self):
2699 return self.PlatformInfo.BuildCommand
2700
2701 ## Get object list of all packages the module and its dependent libraries belong to
2702 #
2703 # @retval list The list of package object
2704 #
2705 @cached_property
2706 def DerivedPackageList(self):
2707 PackageList = []
2708 for M in [self.Module] + self.DependentLibraryList:
2709 for Package in M.Packages:
2710 if Package in PackageList:
2711 continue
2712 PackageList.append(Package)
2713 return PackageList
2714
2715 ## Get the depex string
2716 #
2717 # @return : a string contain all depex expresion.
2718 def _GetDepexExpresionString(self):
2719 DepexStr = ''
2720 DepexList = []
2721 ## DPX_SOURCE IN Define section.
2722 if self.Module.DxsFile:
2723 return DepexStr
2724 for M in [self.Module] + self.DependentLibraryList:
2725 Filename = M.MetaFile.Path
2726 InfObj = InfSectionParser.InfSectionParser(Filename)
2727 DepexExpresionList = InfObj.GetDepexExpresionList()
2728 for DepexExpresion in DepexExpresionList:
2729 for key in DepexExpresion:
2730 Arch, ModuleType = key
2731 DepexExpr = [x for x in DepexExpresion[key] if not str(x).startswith('#')]
2732 # the type of build module is USER_DEFINED.
2733 # All different DEPEX section tags would be copied into the As Built INF file
2734 # and there would be separate DEPEX section tags
2735 if self.ModuleType.upper() == SUP_MODULE_USER_DEFINED:
2736 if (Arch.upper() == self.Arch.upper()) and (ModuleType.upper() != TAB_ARCH_COMMON):
2737 DepexList.append({(Arch, ModuleType): DepexExpr})
2738 else:
2739 if Arch.upper() == TAB_ARCH_COMMON or \
2740 (Arch.upper() == self.Arch.upper() and \
2741 ModuleType.upper() in [TAB_ARCH_COMMON, self.ModuleType.upper()]):
2742 DepexList.append({(Arch, ModuleType): DepexExpr})
2743
2744 #the type of build module is USER_DEFINED.
2745 if self.ModuleType.upper() == SUP_MODULE_USER_DEFINED:
2746 for Depex in DepexList:
2747 for key in Depex:
2748 DepexStr += '[Depex.%s.%s]\n' % key
2749 DepexStr += '\n'.join('# '+ val for val in Depex[key])
2750 DepexStr += '\n\n'
2751 if not DepexStr:
2752 return '[Depex.%s]\n' % self.Arch
2753 return DepexStr
2754
2755 #the type of build module not is USER_DEFINED.
2756 Count = 0
2757 for Depex in DepexList:
2758 Count += 1
2759 if DepexStr != '':
2760 DepexStr += ' AND '
2761 DepexStr += '('
2762 for D in Depex.values():
2763 DepexStr += ' '.join(val for val in D)
2764 Index = DepexStr.find('END')
2765 if Index > -1 and Index == len(DepexStr) - 3:
2766 DepexStr = DepexStr[:-3]
2767 DepexStr = DepexStr.strip()
2768 DepexStr += ')'
2769 if Count == 1:
2770 DepexStr = DepexStr.lstrip('(').rstrip(')').strip()
2771 if not DepexStr:
2772 return '[Depex.%s]\n' % self.Arch
2773 return '[Depex.%s]\n# ' % self.Arch + DepexStr
2774
2775 ## Merge dependency expression
2776 #
2777 # @retval list The token list of the dependency expression after parsed
2778 #
2779 @cached_property
2780 def DepexList(self):
2781 if self.DxsFile or self.IsLibrary or TAB_DEPENDENCY_EXPRESSION_FILE in self.FileTypes:
2782 return {}
2783
2784 DepexList = []
2785 #
2786 # Append depex from dependent libraries, if not "BEFORE", "AFTER" expresion
2787 #
2788 for M in [self.Module] + self.DependentLibraryList:
2789 Inherited = False
2790 for D in M.Depex[self.Arch, self.ModuleType]:
2791 if DepexList != []:
2792 DepexList.append('AND')
2793 DepexList.append('(')
2794 #replace D with value if D is FixedAtBuild PCD
2795 NewList = []
2796 for item in D:
2797 if '.' not in item:
2798 NewList.append(item)
2799 else:
2800 if item not in self._FixedPcdVoidTypeDict:
2801 EdkLogger.error("build", FORMAT_INVALID, "{} used in [Depex] section should be used as FixedAtBuild type and VOID* datum type in the module.".format(item))
2802 else:
2803 Value = self._FixedPcdVoidTypeDict[item]
2804 if len(Value.split(',')) != 16:
2805 EdkLogger.error("build", FORMAT_INVALID,
2806 "{} used in [Depex] section should be used as FixedAtBuild type and VOID* datum type and 16 bytes in the module.".format(item))
2807 NewList.append(Value)
2808 DepexList.extend(NewList)
2809 if DepexList[-1] == 'END': # no need of a END at this time
2810 DepexList.pop()
2811 DepexList.append(')')
2812 Inherited = True
2813 if Inherited:
2814 EdkLogger.verbose("DEPEX[%s] (+%s) = %s" % (self.Name, M.BaseName, DepexList))
2815 if 'BEFORE' in DepexList or 'AFTER' in DepexList:
2816 break
2817 if len(DepexList) > 0:
2818 EdkLogger.verbose('')
2819 return {self.ModuleType:DepexList}
2820
2821 ## Merge dependency expression
2822 #
2823 # @retval list The token list of the dependency expression after parsed
2824 #
2825 @cached_property
2826 def DepexExpressionDict(self):
2827 if self.DxsFile or self.IsLibrary or TAB_DEPENDENCY_EXPRESSION_FILE in self.FileTypes:
2828 return {}
2829
2830 DepexExpressionString = ''
2831 #
2832 # Append depex from dependent libraries, if not "BEFORE", "AFTER" expresion
2833 #
2834 for M in [self.Module] + self.DependentLibraryList:
2835 Inherited = False
2836 for D in M.DepexExpression[self.Arch, self.ModuleType]:
2837 if DepexExpressionString != '':
2838 DepexExpressionString += ' AND '
2839 DepexExpressionString += '('
2840 DepexExpressionString += D
2841 DepexExpressionString = DepexExpressionString.rstrip('END').strip()
2842 DepexExpressionString += ')'
2843 Inherited = True
2844 if Inherited:
2845 EdkLogger.verbose("DEPEX[%s] (+%s) = %s" % (self.Name, M.BaseName, DepexExpressionString))
2846 if 'BEFORE' in DepexExpressionString or 'AFTER' in DepexExpressionString:
2847 break
2848 if len(DepexExpressionString) > 0:
2849 EdkLogger.verbose('')
2850
2851 return {self.ModuleType:DepexExpressionString}
2852
2853 # Get the tiano core user extension, it is contain dependent library.
2854 # @retval: a list contain tiano core userextension.
2855 #
2856 def _GetTianoCoreUserExtensionList(self):
2857 TianoCoreUserExtentionList = []
2858 for M in [self.Module] + self.DependentLibraryList:
2859 Filename = M.MetaFile.Path
2860 InfObj = InfSectionParser.InfSectionParser(Filename)
2861 TianoCoreUserExtenList = InfObj.GetUserExtensionTianoCore()
2862 for TianoCoreUserExtent in TianoCoreUserExtenList:
2863 for Section in TianoCoreUserExtent:
2864 ItemList = Section.split(TAB_SPLIT)
2865 Arch = self.Arch
2866 if len(ItemList) == 4:
2867 Arch = ItemList[3]
2868 if Arch.upper() == TAB_ARCH_COMMON or Arch.upper() == self.Arch.upper():
2869 TianoCoreList = []
2870 TianoCoreList.extend([TAB_SECTION_START + Section + TAB_SECTION_END])
2871 TianoCoreList.extend(TianoCoreUserExtent[Section][:])
2872 TianoCoreList.append('\n')
2873 TianoCoreUserExtentionList.append(TianoCoreList)
2874
2875 return TianoCoreUserExtentionList
2876
2877 ## Return the list of specification version required for the module
2878 #
2879 # @retval list The list of specification defined in module file
2880 #
2881 @cached_property
2882 def Specification(self):
2883 return self.Module.Specification
2884
2885 ## Tool option for the module build
2886 #
2887 # @param PlatformInfo The object of PlatformBuildInfo
2888 # @retval dict The dict containing valid options
2889 #
2890 @cached_property
2891 def BuildOption(self):
2892 RetVal, self.BuildRuleOrder = self.PlatformInfo.ApplyBuildOption(self.Module)
2893 if self.BuildRuleOrder:
2894 self.BuildRuleOrder = ['.%s' % Ext for Ext in self.BuildRuleOrder.split()]
2895 return RetVal
2896
2897 ## Get include path list from tool option for the module build
2898 #
2899 # @retval list The include path list
2900 #
2901 @cached_property
2902 def BuildOptionIncPathList(self):
2903 #
2904 # Regular expression for finding Include Directories, the difference between MSFT and INTEL/GCC/RVCT
2905 # is the former use /I , the Latter used -I to specify include directories
2906 #
2907 if self.PlatformInfo.ToolChainFamily in (TAB_COMPILER_MSFT):
2908 BuildOptIncludeRegEx = gBuildOptIncludePatternMsft
2909 elif self.PlatformInfo.ToolChainFamily in ('INTEL', 'GCC', 'RVCT'):
2910 BuildOptIncludeRegEx = gBuildOptIncludePatternOther
2911 else:
2912 #
2913 # New ToolChainFamily, don't known whether there is option to specify include directories
2914 #
2915 return []
2916
2917 RetVal = []
2918 for Tool in ('CC', 'PP', 'VFRPP', 'ASLPP', 'ASLCC', 'APP', 'ASM'):
2919 try:
2920 FlagOption = self.BuildOption[Tool]['FLAGS']
2921 except KeyError:
2922 FlagOption = ''
2923
2924 if self.ToolChainFamily != 'RVCT':
2925 IncPathList = [NormPath(Path, self.Macros) for Path in BuildOptIncludeRegEx.findall(FlagOption)]
2926 else:
2927 #
2928 # RVCT may specify a list of directory seperated by commas
2929 #
2930 IncPathList = []
2931 for Path in BuildOptIncludeRegEx.findall(FlagOption):
2932 PathList = GetSplitList(Path, TAB_COMMA_SPLIT)
2933 IncPathList.extend(NormPath(PathEntry, self.Macros) for PathEntry in PathList)
2934
2935 #
2936 # EDK II modules must not reference header files outside of the packages they depend on or
2937 # within the module's directory tree. Report error if violation.
2938 #
2939 if self.AutoGenVersion >= 0x00010005:
2940 for Path in IncPathList:
2941 if (Path not in self.IncludePathList) and (CommonPath([Path, self.MetaFile.Dir]) != self.MetaFile.Dir):
2942 ErrMsg = "The include directory for the EDK II module in this line is invalid %s specified in %s FLAGS '%s'" % (Path, Tool, FlagOption)
2943 EdkLogger.error("build",
2944 PARAMETER_INVALID,
2945 ExtraData=ErrMsg,
2946 File=str(self.MetaFile))
2947 RetVal += IncPathList
2948 return RetVal
2949
2950 ## Return a list of files which can be built from source
2951 #
2952 # What kind of files can be built is determined by build rules in
2953 # $(CONF_DIRECTORY)/build_rule.txt and toolchain family.
2954 #
2955 @cached_property
2956 def SourceFileList(self):
2957 RetVal = []
2958 ToolChainTagSet = {"", "*", self.ToolChain}
2959 ToolChainFamilySet = {"", "*", self.ToolChainFamily, self.BuildRuleFamily}
2960 for F in self.Module.Sources:
2961 # match tool chain
2962 if F.TagName not in ToolChainTagSet:
2963 EdkLogger.debug(EdkLogger.DEBUG_9, "The toolchain [%s] for processing file [%s] is found, "
2964 "but [%s] is currently used" % (F.TagName, str(F), self.ToolChain))
2965 continue
2966 # match tool chain family or build rule family
2967 if F.ToolChainFamily not in ToolChainFamilySet:
2968 EdkLogger.debug(
2969 EdkLogger.DEBUG_0,
2970 "The file [%s] must be built by tools of [%s], " \
2971 "but current toolchain family is [%s], buildrule family is [%s]" \
2972 % (str(F), F.ToolChainFamily, self.ToolChainFamily, self.BuildRuleFamily))
2973 continue
2974
2975 # add the file path into search path list for file including
2976 if F.Dir not in self.IncludePathList and self.AutoGenVersion >= 0x00010005:
2977 self.IncludePathList.insert(0, F.Dir)
2978 RetVal.append(F)
2979
2980 self._MatchBuildRuleOrder(RetVal)
2981
2982 for F in RetVal:
2983 self._ApplyBuildRule(F, TAB_UNKNOWN_FILE)
2984 return RetVal
2985
2986 def _MatchBuildRuleOrder(self, FileList):
2987 Order_Dict = {}
2988 self.BuildOption
2989 for SingleFile in FileList:
2990 if self.BuildRuleOrder and SingleFile.Ext in self.BuildRuleOrder and SingleFile.Ext in self.BuildRules:
2991 key = SingleFile.Path.split(SingleFile.Ext)[0]
2992 if key in Order_Dict:
2993 Order_Dict[key].append(SingleFile.Ext)
2994 else:
2995 Order_Dict[key] = [SingleFile.Ext]
2996
2997 RemoveList = []
2998 for F in Order_Dict:
2999 if len(Order_Dict[F]) > 1:
3000 Order_Dict[F].sort(key=lambda i: self.BuildRuleOrder.index(i))
3001 for Ext in Order_Dict[F][1:]:
3002 RemoveList.append(F + Ext)
3003
3004 for item in RemoveList:
3005 FileList.remove(item)
3006
3007 return FileList
3008
3009 ## Return the list of unicode files
3010 @cached_property
3011 def UnicodeFileList(self):
3012 return self.FileTypes.get(TAB_UNICODE_FILE,[])
3013
3014 ## Return the list of vfr files
3015 @cached_property
3016 def VfrFileList(self):
3017 return self.FileTypes.get(TAB_VFR_FILE, [])
3018
3019 ## Return the list of Image Definition files
3020 @cached_property
3021 def IdfFileList(self):
3022 return self.FileTypes.get(TAB_IMAGE_FILE,[])
3023
3024 ## Return a list of files which can be built from binary
3025 #
3026 # "Build" binary files are just to copy them to build directory.
3027 #
3028 # @retval list The list of files which can be built later
3029 #
3030 @cached_property
3031 def BinaryFileList(self):
3032 RetVal = []
3033 for F in self.Module.Binaries:
3034 if F.Target not in [TAB_ARCH_COMMON, '*'] and F.Target != self.BuildTarget:
3035 continue
3036 RetVal.append(F)
3037 self._ApplyBuildRule(F, F.Type, BinaryFileList=RetVal)
3038 return RetVal
3039
3040 @cached_property
3041 def BuildRules(self):
3042 RetVal = {}
3043 BuildRuleDatabase = self.PlatformInfo.BuildRule
3044 for Type in BuildRuleDatabase.FileTypeList:
3045 #first try getting build rule by BuildRuleFamily
3046 RuleObject = BuildRuleDatabase[Type, self.BuildType, self.Arch, self.BuildRuleFamily]
3047 if not RuleObject:
3048 # build type is always module type, but ...
3049 if self.ModuleType != self.BuildType:
3050 RuleObject = BuildRuleDatabase[Type, self.ModuleType, self.Arch, self.BuildRuleFamily]
3051 #second try getting build rule by ToolChainFamily
3052 if not RuleObject:
3053 RuleObject = BuildRuleDatabase[Type, self.BuildType, self.Arch, self.ToolChainFamily]
3054 if not RuleObject:
3055 # build type is always module type, but ...
3056 if self.ModuleType != self.BuildType:
3057 RuleObject = BuildRuleDatabase[Type, self.ModuleType, self.Arch, self.ToolChainFamily]
3058 if not RuleObject:
3059 continue
3060 RuleObject = RuleObject.Instantiate(self.Macros)
3061 RetVal[Type] = RuleObject
3062 for Ext in RuleObject.SourceFileExtList:
3063 RetVal[Ext] = RuleObject
3064 return RetVal
3065
3066 def _ApplyBuildRule(self, File, FileType, BinaryFileList=None):
3067 if self._BuildTargets is None:
3068 self._IntroBuildTargetList = set()
3069 self._FinalBuildTargetList = set()
3070 self._BuildTargets = defaultdict(set)
3071 self._FileTypes = defaultdict(set)
3072
3073 if not BinaryFileList:
3074 BinaryFileList = self.BinaryFileList
3075
3076 SubDirectory = os.path.join(self.OutputDir, File.SubDir)
3077 if not os.path.exists(SubDirectory):
3078 CreateDirectory(SubDirectory)
3079 LastTarget = None
3080 RuleChain = set()
3081 SourceList = [File]
3082 Index = 0
3083 #
3084 # Make sure to get build rule order value
3085 #
3086 self.BuildOption
3087
3088 while Index < len(SourceList):
3089 Source = SourceList[Index]
3090 Index = Index + 1
3091
3092 if Source != File:
3093 CreateDirectory(Source.Dir)
3094
3095 if File.IsBinary and File == Source and File in BinaryFileList:
3096 # Skip all files that are not binary libraries
3097 if not self.IsLibrary:
3098 continue
3099 RuleObject = self.BuildRules[TAB_DEFAULT_BINARY_FILE]
3100 elif FileType in self.BuildRules:
3101 RuleObject = self.BuildRules[FileType]
3102 elif Source.Ext in self.BuildRules:
3103 RuleObject = self.BuildRules[Source.Ext]
3104 else:
3105 # stop at no more rules
3106 if LastTarget:
3107 self._FinalBuildTargetList.add(LastTarget)
3108 break
3109
3110 FileType = RuleObject.SourceFileType
3111 self._FileTypes[FileType].add(Source)
3112
3113 # stop at STATIC_LIBRARY for library
3114 if self.IsLibrary and FileType == TAB_STATIC_LIBRARY:
3115 if LastTarget:
3116 self._FinalBuildTargetList.add(LastTarget)
3117 break
3118
3119 Target = RuleObject.Apply(Source, self.BuildRuleOrder)
3120 if not Target:
3121 if LastTarget:
3122 self._FinalBuildTargetList.add(LastTarget)
3123 break
3124 elif not Target.Outputs:
3125 # Only do build for target with outputs
3126 self._FinalBuildTargetList.add(Target)
3127
3128 self._BuildTargets[FileType].add(Target)
3129
3130 if not Source.IsBinary and Source == File:
3131 self._IntroBuildTargetList.add(Target)
3132
3133 # to avoid cyclic rule
3134 if FileType in RuleChain:
3135 break
3136
3137 RuleChain.add(FileType)
3138 SourceList.extend(Target.Outputs)
3139 LastTarget = Target
3140 FileType = TAB_UNKNOWN_FILE
3141
3142 @cached_property
3143 def Targets(self):
3144 if self._BuildTargets is None:
3145 self._IntroBuildTargetList = set()
3146 self._FinalBuildTargetList = set()
3147 self._BuildTargets = defaultdict(set)
3148 self._FileTypes = defaultdict(set)
3149
3150 #TRICK: call SourceFileList property to apply build rule for source files
3151 self.SourceFileList
3152
3153 #TRICK: call _GetBinaryFileList to apply build rule for binary files
3154 self.BinaryFileList
3155
3156 return self._BuildTargets
3157
3158 @cached_property
3159 def IntroTargetList(self):
3160 self.Targets
3161 return self._IntroBuildTargetList
3162
3163 @cached_property
3164 def CodaTargetList(self):
3165 self.Targets
3166 return self._FinalBuildTargetList
3167
3168 @cached_property
3169 def FileTypes(self):
3170 self.Targets
3171 return self._FileTypes
3172
3173 ## Get the list of package object the module depends on
3174 #
3175 # @retval list The package object list
3176 #
3177 @cached_property
3178 def DependentPackageList(self):
3179 return self.Module.Packages
3180
3181 ## Return the list of auto-generated code file
3182 #
3183 # @retval list The list of auto-generated file
3184 #
3185 @cached_property
3186 def AutoGenFileList(self):
3187 AutoGenUniIdf = self.BuildType != 'UEFI_HII'
3188 UniStringBinBuffer = BytesIO()
3189 IdfGenBinBuffer = BytesIO()
3190 RetVal = {}
3191 AutoGenC = TemplateString()
3192 AutoGenH = TemplateString()
3193 StringH = TemplateString()
3194 StringIdf = TemplateString()
3195 GenC.CreateCode(self, AutoGenC, AutoGenH, StringH, AutoGenUniIdf, UniStringBinBuffer, StringIdf, AutoGenUniIdf, IdfGenBinBuffer)
3196 #
3197 # AutoGen.c is generated if there are library classes in inf, or there are object files
3198 #
3199 if str(AutoGenC) != "" and (len(self.Module.LibraryClasses) > 0
3200 or TAB_OBJECT_FILE in self.FileTypes):
3201 AutoFile = PathClass(gAutoGenCodeFileName, self.DebugDir)
3202 RetVal[AutoFile] = str(AutoGenC)
3203 self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)
3204 if str(AutoGenH) != "":
3205 AutoFile = PathClass(gAutoGenHeaderFileName, self.DebugDir)
3206 RetVal[AutoFile] = str(AutoGenH)
3207 self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)
3208 if str(StringH) != "":
3209 AutoFile = PathClass(gAutoGenStringFileName % {"module_name":self.Name}, self.DebugDir)
3210 RetVal[AutoFile] = str(StringH)
3211 self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)
3212 if UniStringBinBuffer is not None and UniStringBinBuffer.getvalue() != "":
3213 AutoFile = PathClass(gAutoGenStringFormFileName % {"module_name":self.Name}, self.OutputDir)
3214 RetVal[AutoFile] = UniStringBinBuffer.getvalue()
3215 AutoFile.IsBinary = True
3216 self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)
3217 if UniStringBinBuffer is not None:
3218 UniStringBinBuffer.close()
3219 if str(StringIdf) != "":
3220 AutoFile = PathClass(gAutoGenImageDefFileName % {"module_name":self.Name}, self.DebugDir)
3221 RetVal[AutoFile] = str(StringIdf)
3222 self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)
3223 if IdfGenBinBuffer is not None and IdfGenBinBuffer.getvalue() != "":
3224 AutoFile = PathClass(gAutoGenIdfFileName % {"module_name":self.Name}, self.OutputDir)
3225 RetVal[AutoFile] = IdfGenBinBuffer.getvalue()
3226 AutoFile.IsBinary = True
3227 self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)
3228 if IdfGenBinBuffer is not None:
3229 IdfGenBinBuffer.close()
3230 return RetVal
3231
3232 ## Return the list of library modules explicitly or implicityly used by this module
3233 @cached_property
3234 def DependentLibraryList(self):
3235 # only merge library classes and PCD for non-library module
3236 if self.IsLibrary:
3237 return []
3238 if self.AutoGenVersion < 0x00010005:
3239 return self.PlatformInfo.ResolveLibraryReference(self.Module)
3240 return self.PlatformInfo.ApplyLibraryInstance(self.Module)
3241
3242 ## Get the list of PCDs from current module
3243 #
3244 # @retval list The list of PCD
3245 #
3246 @cached_property
3247 def ModulePcdList(self):
3248 # apply PCD settings from platform
3249 RetVal = self.PlatformInfo.ApplyPcdSetting(self.Module, self.Module.Pcds)
3250 ExtendCopyDictionaryLists(self._PcdComments, self.Module.PcdComments)
3251 return RetVal
3252
3253 ## Get the list of PCDs from dependent libraries
3254 #
3255 # @retval list The list of PCD
3256 #
3257 @cached_property
3258 def LibraryPcdList(self):
3259 if self.IsLibrary:
3260 return []
3261 RetVal = []
3262 Pcds = set()
3263 # get PCDs from dependent libraries
3264 for Library in self.DependentLibraryList:
3265 PcdsInLibrary = OrderedDict()
3266 ExtendCopyDictionaryLists(self._PcdComments, Library.PcdComments)
3267 for Key in Library.Pcds:
3268 # skip duplicated PCDs
3269 if Key in self.Module.Pcds or Key in Pcds:
3270 continue
3271 Pcds.add(Key)
3272 PcdsInLibrary[Key] = copy.copy(Library.Pcds[Key])
3273 RetVal.extend(self.PlatformInfo.ApplyPcdSetting(self.Module, PcdsInLibrary, Library=Library))
3274 return RetVal
3275
3276 ## Get the GUID value mapping
3277 #
3278 # @retval dict The mapping between GUID cname and its value
3279 #
3280 @cached_property
3281 def GuidList(self):
3282 RetVal = OrderedDict(self.Module.Guids)
3283 for Library in self.DependentLibraryList:
3284 RetVal.update(Library.Guids)
3285 ExtendCopyDictionaryLists(self._GuidComments, Library.GuidComments)
3286 ExtendCopyDictionaryLists(self._GuidComments, self.Module.GuidComments)
3287 return RetVal
3288
3289 @cached_property
3290 def GetGuidsUsedByPcd(self):
3291 RetVal = OrderedDict(self.Module.GetGuidsUsedByPcd())
3292 for Library in self.DependentLibraryList:
3293 RetVal.update(Library.GetGuidsUsedByPcd())
3294 return RetVal
3295 ## Get the protocol value mapping
3296 #
3297 # @retval dict The mapping between protocol cname and its value
3298 #
3299 @cached_property
3300 def ProtocolList(self):
3301 RetVal = OrderedDict(self.Module.Protocols)
3302 for Library in self.DependentLibraryList:
3303 RetVal.update(Library.Protocols)
3304 ExtendCopyDictionaryLists(self._ProtocolComments, Library.ProtocolComments)
3305 ExtendCopyDictionaryLists(self._ProtocolComments, self.Module.ProtocolComments)
3306 return RetVal
3307
3308 ## Get the PPI value mapping
3309 #
3310 # @retval dict The mapping between PPI cname and its value
3311 #
3312 @cached_property
3313 def PpiList(self):
3314 RetVal = OrderedDict(self.Module.Ppis)
3315 for Library in self.DependentLibraryList:
3316 RetVal.update(Library.Ppis)
3317 ExtendCopyDictionaryLists(self._PpiComments, Library.PpiComments)
3318 ExtendCopyDictionaryLists(self._PpiComments, self.Module.PpiComments)
3319 return RetVal
3320
3321 ## Get the list of include search path
3322 #
3323 # @retval list The list path
3324 #
3325 @cached_property
3326 def IncludePathList(self):
3327 RetVal = []
3328 if self.AutoGenVersion < 0x00010005:
3329 for Inc in self.Module.Includes:
3330 if Inc not in RetVal:
3331 RetVal.append(Inc)
3332 # for Edk modules
3333 Inc = path.join(Inc, self.Arch.capitalize())
3334 if os.path.exists(Inc) and Inc not in RetVal:
3335 RetVal.append(Inc)
3336 # Edk module needs to put DEBUG_DIR at the end of search path and not to use SOURCE_DIR all the time
3337 RetVal.append(self.DebugDir)
3338 else:
3339 RetVal.append(self.MetaFile.Dir)
3340 RetVal.append(self.DebugDir)
3341
3342 for Package in self.Module.Packages:
3343 PackageDir = mws.join(self.WorkspaceDir, Package.MetaFile.Dir)
3344 if PackageDir not in RetVal:
3345 RetVal.append(PackageDir)
3346 IncludesList = Package.Includes
3347 if Package._PrivateIncludes:
3348 if not self.MetaFile.Path.startswith(PackageDir):
3349 IncludesList = list(set(Package.Includes).difference(set(Package._PrivateIncludes)))
3350 for Inc in IncludesList:
3351 if Inc not in RetVal:
3352 RetVal.append(str(Inc))
3353 return RetVal
3354
3355 @cached_property
3356 def IncludePathLength(self):
3357 return sum(len(inc)+1 for inc in self.IncludePathList)
3358
3359 ## Get HII EX PCDs which maybe used by VFR
3360 #
3361 # efivarstore used by VFR may relate with HII EX PCDs
3362 # Get the variable name and GUID from efivarstore and HII EX PCD
3363 # List the HII EX PCDs in As Built INF if both name and GUID match.
3364 #
3365 # @retval list HII EX PCDs
3366 #
3367 def _GetPcdsMaybeUsedByVfr(self):
3368 if not self.SourceFileList:
3369 return []
3370
3371 NameGuids = set()
3372 for SrcFile in self.SourceFileList:
3373 if SrcFile.Ext.lower() != '.vfr':
3374 continue
3375 Vfri = os.path.join(self.OutputDir, SrcFile.BaseName + '.i')
3376 if not os.path.exists(Vfri):
3377 continue
3378 VfriFile = open(Vfri, 'r')
3379 Content = VfriFile.read()
3380 VfriFile.close()
3381 Pos = Content.find('efivarstore')
3382 while Pos != -1:
3383 #
3384 # Make sure 'efivarstore' is the start of efivarstore statement
3385 # In case of the value of 'name' (name = efivarstore) is equal to 'efivarstore'
3386 #
3387 Index = Pos - 1
3388 while Index >= 0 and Content[Index] in ' \t\r\n':
3389 Index -= 1
3390 if Index >= 0 and Content[Index] != ';':
3391 Pos = Content.find('efivarstore', Pos + len('efivarstore'))
3392 continue
3393 #
3394 # 'efivarstore' must be followed by name and guid
3395 #
3396 Name = gEfiVarStoreNamePattern.search(Content, Pos)
3397 if not Name:
3398 break
3399 Guid = gEfiVarStoreGuidPattern.search(Content, Pos)
3400 if not Guid:
3401 break
3402 NameArray = ConvertStringToByteArray('L"' + Name.group(1) + '"')
3403 NameGuids.add((NameArray, GuidStructureStringToGuidString(Guid.group(1))))
3404 Pos = Content.find('efivarstore', Name.end())
3405 if not NameGuids:
3406 return []
3407 HiiExPcds = []
3408 for Pcd in self.PlatformInfo.Platform.Pcds.values():
3409 if Pcd.Type != TAB_PCDS_DYNAMIC_EX_HII:
3410 continue
3411 for SkuInfo in Pcd.SkuInfoList.values():
3412 Value = GuidValue(SkuInfo.VariableGuid, self.PlatformInfo.PackageList, self.MetaFile.Path)
3413 if not Value:
3414 continue
3415 Name = ConvertStringToByteArray(SkuInfo.VariableName)
3416 Guid = GuidStructureStringToGuidString(Value)
3417 if (Name, Guid) in NameGuids and Pcd not in HiiExPcds:
3418 HiiExPcds.append(Pcd)
3419 break
3420
3421 return HiiExPcds
3422
3423 def _GenOffsetBin(self):
3424 VfrUniBaseName = {}
3425 for SourceFile in self.Module.Sources:
3426 if SourceFile.Type.upper() == ".VFR" :
3427 #
3428 # search the .map file to find the offset of vfr binary in the PE32+/TE file.
3429 #
3430 VfrUniBaseName[SourceFile.BaseName] = (SourceFile.BaseName + "Bin")
3431 elif SourceFile.Type.upper() == ".UNI" :
3432 #
3433 # search the .map file to find the offset of Uni strings binary in the PE32+/TE file.
3434 #
3435 VfrUniBaseName["UniOffsetName"] = (self.Name + "Strings")
3436
3437 if not VfrUniBaseName:
3438 return None
3439 MapFileName = os.path.join(self.OutputDir, self.Name + ".map")
3440 EfiFileName = os.path.join(self.OutputDir, self.Name + ".efi")
3441 VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, VfrUniBaseName.values())
3442 if not VfrUniOffsetList:
3443 return None
3444
3445 OutputName = '%sOffset.bin' % self.Name
3446 UniVfrOffsetFileName = os.path.join( self.OutputDir, OutputName)
3447
3448 try:
3449 fInputfile = open(UniVfrOffsetFileName, "wb+", 0)
3450 except:
3451 EdkLogger.error("build", FILE_OPEN_FAILURE, "File open failed for %s" % UniVfrOffsetFileName, None)
3452
3453 # Use a instance of BytesIO to cache data
3454 fStringIO = BytesIO('')
3455
3456 for Item in VfrUniOffsetList:
3457 if (Item[0].find("Strings") != -1):
3458 #
3459 # UNI offset in image.
3460 # GUID + Offset
3461 # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }
3462 #
3463 UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]
3464 UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]
3465 fStringIO.write(''.join(UniGuid))
3466 UniValue = pack ('Q', int (Item[1], 16))
3467 fStringIO.write (UniValue)
3468 else:
3469 #
3470 # VFR binary offset in image.
3471 # GUID + Offset
3472 # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };
3473 #
3474 VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]
3475 VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]
3476 fStringIO.write(''.join(VfrGuid))
3477 VfrValue = pack ('Q', int (Item[1], 16))
3478 fStringIO.write (VfrValue)
3479 #
3480 # write data into file.
3481 #
3482 try :
3483 fInputfile.write (fStringIO.getvalue())
3484 except:
3485 EdkLogger.error("build", FILE_WRITE_FAILURE, "Write data to file %s failed, please check whether the "
3486 "file been locked or using by other applications." %UniVfrOffsetFileName, None)
3487
3488 fStringIO.close ()
3489 fInputfile.close ()
3490 return OutputName
3491
3492 ## Create AsBuilt INF file the module
3493 #
3494 def CreateAsBuiltInf(self, IsOnlyCopy = False):
3495 self.OutputFile = set()
3496 if IsOnlyCopy and GlobalData.gBinCacheDest:
3497 self.CopyModuleToCache()
3498 return
3499
3500 if self.IsAsBuiltInfCreated:
3501 return
3502
3503 # Skip the following code for EDK I inf
3504 if self.AutoGenVersion < 0x00010005:
3505 return
3506
3507 # Skip the following code for libraries
3508 if self.IsLibrary:
3509 return
3510
3511 # Skip the following code for modules with no source files
3512 if not self.SourceFileList:
3513 return
3514
3515 # Skip the following code for modules without any binary files
3516 if self.BinaryFileList:
3517 return
3518
3519 ### TODO: How to handles mixed source and binary modules
3520
3521 # Find all DynamicEx and PatchableInModule PCDs used by this module and dependent libraries
3522 # Also find all packages that the DynamicEx PCDs depend on
3523 Pcds = []
3524 PatchablePcds = []
3525 Packages = []
3526 PcdCheckList = []
3527 PcdTokenSpaceList = []
3528 for Pcd in self.ModulePcdList + self.LibraryPcdList:
3529 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE:
3530 PatchablePcds.append(Pcd)
3531 PcdCheckList.append((Pcd.TokenCName, Pcd.TokenSpaceGuidCName, TAB_PCDS_PATCHABLE_IN_MODULE))
3532 elif Pcd.Type in PCD_DYNAMIC_EX_TYPE_SET:
3533 if Pcd not in Pcds:
3534 Pcds.append(Pcd)
3535 PcdCheckList.append((Pcd.TokenCName, Pcd.TokenSpaceGuidCName, TAB_PCDS_DYNAMIC_EX))
3536 PcdCheckList.append((Pcd.TokenCName, Pcd.TokenSpaceGuidCName, TAB_PCDS_DYNAMIC))
3537 PcdTokenSpaceList.append(Pcd.TokenSpaceGuidCName)
3538 GuidList = OrderedDict(self.GuidList)
3539 for TokenSpace in self.GetGuidsUsedByPcd:
3540 # If token space is not referred by patch PCD or Ex PCD, remove the GUID from GUID list
3541 # The GUIDs in GUIDs section should really be the GUIDs in source INF or referred by Ex an patch PCDs
3542 if TokenSpace not in PcdTokenSpaceList and TokenSpace in GuidList:
3543 GuidList.pop(TokenSpace)
3544 CheckList = (GuidList, self.PpiList, self.ProtocolList, PcdCheckList)
3545 for Package in self.DerivedPackageList:
3546 if Package in Packages:
3547 continue
3548 BeChecked = (Package.Guids, Package.Ppis, Package.Protocols, Package.Pcds)
3549 Found = False
3550 for Index in range(len(BeChecked)):
3551 for Item in CheckList[Index]:
3552 if Item in BeChecked[Index]:
3553 Packages.append(Package)
3554 Found = True
3555 break
3556 if Found:
3557 break
3558
3559 VfrPcds = self._GetPcdsMaybeUsedByVfr()
3560 for Pkg in self.PlatformInfo.PackageList:
3561 if Pkg in Packages:
3562 continue
3563 for VfrPcd in VfrPcds:
3564 if ((VfrPcd.TokenCName, VfrPcd.TokenSpaceGuidCName, TAB_PCDS_DYNAMIC_EX) in Pkg.Pcds or
3565 (VfrPcd.TokenCName, VfrPcd.TokenSpaceGuidCName, TAB_PCDS_DYNAMIC) in Pkg.Pcds):
3566 Packages.append(Pkg)
3567 break
3568
3569 ModuleType = SUP_MODULE_DXE_DRIVER if self.ModuleType == SUP_MODULE_UEFI_DRIVER and self.DepexGenerated else self.ModuleType
3570 DriverType = self.PcdIsDriver if self.PcdIsDriver else ''
3571 Guid = self.Guid
3572 MDefs = self.Module.Defines
3573
3574 AsBuiltInfDict = {
3575 'module_name' : self.Name,
3576 'module_guid' : Guid,
3577 'module_module_type' : ModuleType,
3578 'module_version_string' : [MDefs['VERSION_STRING']] if 'VERSION_STRING' in MDefs else [],
3579 'pcd_is_driver_string' : [],
3580 'module_uefi_specification_version' : [],
3581 'module_pi_specification_version' : [],
3582 'module_entry_point' : self.Module.ModuleEntryPointList,
3583 'module_unload_image' : self.Module.ModuleUnloadImageList,
3584 'module_constructor' : self.Module.ConstructorList,
3585 'module_destructor' : self.Module.DestructorList,
3586 'module_shadow' : [MDefs['SHADOW']] if 'SHADOW' in MDefs else [],
3587 'module_pci_vendor_id' : [MDefs['PCI_VENDOR_ID']] if 'PCI_VENDOR_ID' in MDefs else [],
3588 'module_pci_device_id' : [MDefs['PCI_DEVICE_ID']] if 'PCI_DEVICE_ID' in MDefs else [],
3589 'module_pci_class_code' : [MDefs['PCI_CLASS_CODE']] if 'PCI_CLASS_CODE' in MDefs else [],
3590 'module_pci_revision' : [MDefs['PCI_REVISION']] if 'PCI_REVISION' in MDefs else [],
3591 'module_build_number' : [MDefs['BUILD_NUMBER']] if 'BUILD_NUMBER' in MDefs else [],
3592 'module_spec' : [MDefs['SPEC']] if 'SPEC' in MDefs else [],
3593 'module_uefi_hii_resource_section' : [MDefs['UEFI_HII_RESOURCE_SECTION']] if 'UEFI_HII_RESOURCE_SECTION' in MDefs else [],
3594 'module_uni_file' : [MDefs['MODULE_UNI_FILE']] if 'MODULE_UNI_FILE' in MDefs else [],
3595 'module_arch' : self.Arch,
3596 'package_item' : [Package.MetaFile.File.replace('\\', '/') for Package in Packages],
3597 'binary_item' : [],
3598 'patchablepcd_item' : [],
3599 'pcd_item' : [],
3600 'protocol_item' : [],
3601 'ppi_item' : [],
3602 'guid_item' : [],
3603 'flags_item' : [],
3604 'libraryclasses_item' : []
3605 }
3606
3607 if 'MODULE_UNI_FILE' in MDefs:
3608 UNIFile = os.path.join(self.MetaFile.Dir, MDefs['MODULE_UNI_FILE'])
3609 if os.path.isfile(UNIFile):
3610 shutil.copy2(UNIFile, self.OutputDir)
3611
3612 if self.AutoGenVersion > int(gInfSpecVersion, 0):
3613 AsBuiltInfDict['module_inf_version'] = '0x%08x' % self.AutoGenVersion
3614 else:
3615 AsBuiltInfDict['module_inf_version'] = gInfSpecVersion
3616
3617 if DriverType:
3618 AsBuiltInfDict['pcd_is_driver_string'].append(DriverType)
3619
3620 if 'UEFI_SPECIFICATION_VERSION' in self.Specification:
3621 AsBuiltInfDict['module_uefi_specification_version'].append(self.Specification['UEFI_SPECIFICATION_VERSION'])
3622 if 'PI_SPECIFICATION_VERSION' in self.Specification:
3623 AsBuiltInfDict['module_pi_specification_version'].append(self.Specification['PI_SPECIFICATION_VERSION'])
3624
3625 OutputDir = self.OutputDir.replace('\\', '/').strip('/')
3626 DebugDir = self.DebugDir.replace('\\', '/').strip('/')
3627 for Item in self.CodaTargetList:
3628 File = Item.Target.Path.replace('\\', '/').strip('/').replace(DebugDir, '').replace(OutputDir, '').strip('/')
3629 self.OutputFile.add(File)
3630 if os.path.isabs(File):
3631 File = File.replace('\\', '/').strip('/').replace(OutputDir, '').strip('/')
3632 if Item.Target.Ext.lower() == '.aml':
3633 AsBuiltInfDict['binary_item'].append('ASL|' + File)
3634 elif Item.Target.Ext.lower() == '.acpi':
3635 AsBuiltInfDict['binary_item'].append('ACPI|' + File)
3636 elif Item.Target.Ext.lower() == '.efi':
3637 AsBuiltInfDict['binary_item'].append('PE32|' + self.Name + '.efi')
3638 else:
3639 AsBuiltInfDict['binary_item'].append('BIN|' + File)
3640 if self.DepexGenerated:
3641 self.OutputFile.add(self.Name + '.depex')
3642 if self.ModuleType in [SUP_MODULE_PEIM]:
3643 AsBuiltInfDict['binary_item'].append('PEI_DEPEX|' + self.Name + '.depex')
3644 elif self.ModuleType in [SUP_MODULE_DXE_DRIVER, SUP_MODULE_DXE_RUNTIME_DRIVER, SUP_MODULE_DXE_SAL_DRIVER, SUP_MODULE_UEFI_DRIVER]:
3645 AsBuiltInfDict['binary_item'].append('DXE_DEPEX|' + self.Name + '.depex')
3646 elif self.ModuleType in [SUP_MODULE_DXE_SMM_DRIVER]:
3647 AsBuiltInfDict['binary_item'].append('SMM_DEPEX|' + self.Name + '.depex')
3648
3649 Bin = self._GenOffsetBin()
3650 if Bin:
3651 AsBuiltInfDict['binary_item'].append('BIN|%s' % Bin)
3652 self.OutputFile.add(Bin)
3653
3654 for Root, Dirs, Files in os.walk(OutputDir):
3655 for File in Files:
3656 if File.lower().endswith('.pdb'):
3657 AsBuiltInfDict['binary_item'].append('DISPOSABLE|' + File)
3658 self.OutputFile.add(File)
3659 HeaderComments = self.Module.HeaderComments
3660 StartPos = 0
3661 for Index in range(len(HeaderComments)):
3662 if HeaderComments[Index].find('@BinaryHeader') != -1:
3663 HeaderComments[Index] = HeaderComments[Index].replace('@BinaryHeader', '@file')
3664 StartPos = Index
3665 break
3666 AsBuiltInfDict['header_comments'] = '\n'.join(HeaderComments[StartPos:]).replace(':#', '://')
3667 AsBuiltInfDict['tail_comments'] = '\n'.join(self.Module.TailComments)
3668
3669 GenList = [
3670 (self.ProtocolList, self._ProtocolComments, 'protocol_item'),
3671 (self.PpiList, self._PpiComments, 'ppi_item'),
3672 (GuidList, self._GuidComments, 'guid_item')
3673 ]
3674 for Item in GenList:
3675 for CName in Item[0]:
3676 Comments = '\n '.join(Item[1][CName]) if CName in Item[1] else ''
3677 Entry = Comments + '\n ' + CName if Comments else CName
3678 AsBuiltInfDict[Item[2]].append(Entry)
3679 PatchList = parsePcdInfoFromMapFile(
3680 os.path.join(self.OutputDir, self.Name + '.map'),
3681 os.path.join(self.OutputDir, self.Name + '.efi')
3682 )
3683 if PatchList:
3684 for Pcd in PatchablePcds:
3685 TokenCName = Pcd.TokenCName
3686 for PcdItem in GlobalData.MixedPcd:
3687 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
3688 TokenCName = PcdItem[0]
3689 break
3690 for PatchPcd in PatchList:
3691 if TokenCName == PatchPcd[0]:
3692 break
3693 else:
3694 continue
3695 PcdValue = ''
3696 if Pcd.DatumType == 'BOOLEAN':
3697 BoolValue = Pcd.DefaultValue.upper()
3698 if BoolValue == 'TRUE':
3699 Pcd.DefaultValue = '1'
3700 elif BoolValue == 'FALSE':
3701 Pcd.DefaultValue = '0'
3702
3703 if Pcd.DatumType in TAB_PCD_NUMERIC_TYPES:
3704 HexFormat = '0x%02x'
3705 if Pcd.DatumType == TAB_UINT16:
3706 HexFormat = '0x%04x'
3707 elif Pcd.DatumType == TAB_UINT32:
3708 HexFormat = '0x%08x'
3709 elif Pcd.DatumType == TAB_UINT64:
3710 HexFormat = '0x%016x'
3711 PcdValue = HexFormat % int(Pcd.DefaultValue, 0)
3712 else:
3713 if Pcd.MaxDatumSize is None or Pcd.MaxDatumSize == '':
3714 EdkLogger.error("build", AUTOGEN_ERROR,
3715 "Unknown [MaxDatumSize] of PCD [%s.%s]" % (Pcd.TokenSpaceGuidCName, TokenCName)
3716 )
3717 ArraySize = int(Pcd.MaxDatumSize, 0)
3718 PcdValue = Pcd.DefaultValue
3719 if PcdValue[0] != '{':
3720 Unicode = False
3721 if PcdValue[0] == 'L':
3722 Unicode = True
3723 PcdValue = PcdValue.lstrip('L')
3724 PcdValue = eval(PcdValue)
3725 NewValue = '{'
3726 for Index in range(0, len(PcdValue)):
3727 if Unicode:
3728 CharVal = ord(PcdValue[Index])
3729 NewValue = NewValue + '0x%02x' % (CharVal & 0x00FF) + ', ' \
3730 + '0x%02x' % (CharVal >> 8) + ', '
3731 else:
3732 NewValue = NewValue + '0x%02x' % (ord(PcdValue[Index]) % 0x100) + ', '
3733 Padding = '0x00, '
3734 if Unicode:
3735 Padding = Padding * 2
3736 ArraySize = ArraySize / 2
3737 if ArraySize < (len(PcdValue) + 1):
3738 if Pcd.MaxSizeUserSet:
3739 EdkLogger.error("build", AUTOGEN_ERROR,
3740 "The maximum size of VOID* type PCD '%s.%s' is less than its actual size occupied." % (Pcd.TokenSpaceGuidCName, TokenCName)
3741 )
3742 else:
3743 ArraySize = len(PcdValue) + 1
3744 if ArraySize > len(PcdValue) + 1:
3745 NewValue = NewValue + Padding * (ArraySize - len(PcdValue) - 1)
3746 PcdValue = NewValue + Padding.strip().rstrip(',') + '}'
3747 elif len(PcdValue.split(',')) <= ArraySize:
3748 PcdValue = PcdValue.rstrip('}') + ', 0x00' * (ArraySize - len(PcdValue.split(',')))
3749 PcdValue += '}'
3750 else:
3751 if Pcd.MaxSizeUserSet:
3752 EdkLogger.error("build", AUTOGEN_ERROR,
3753 "The maximum size of VOID* type PCD '%s.%s' is less than its actual size occupied." % (Pcd.TokenSpaceGuidCName, TokenCName)
3754 )
3755 else:
3756 ArraySize = len(PcdValue) + 1
3757 PcdItem = '%s.%s|%s|0x%X' % \
3758 (Pcd.TokenSpaceGuidCName, TokenCName, PcdValue, PatchPcd[1])
3759 PcdComments = ''
3760 if (Pcd.TokenSpaceGuidCName, Pcd.TokenCName) in self._PcdComments:
3761 PcdComments = '\n '.join(self._PcdComments[Pcd.TokenSpaceGuidCName, Pcd.TokenCName])
3762 if PcdComments:
3763 PcdItem = PcdComments + '\n ' + PcdItem
3764 AsBuiltInfDict['patchablepcd_item'].append(PcdItem)
3765
3766 for Pcd in Pcds + VfrPcds:
3767 PcdCommentList = []
3768 HiiInfo = ''
3769 TokenCName = Pcd.TokenCName
3770 for PcdItem in GlobalData.MixedPcd:
3771 if (Pcd.TokenCName, Pcd.TokenSpaceGuidCName) in GlobalData.MixedPcd[PcdItem]:
3772 TokenCName = PcdItem[0]
3773 break
3774 if Pcd.Type == TAB_PCDS_DYNAMIC_EX_HII:
3775 for SkuName in Pcd.SkuInfoList:
3776 SkuInfo = Pcd.SkuInfoList[SkuName]
3777 HiiInfo = '## %s|%s|%s' % (SkuInfo.VariableName, SkuInfo.VariableGuid, SkuInfo.VariableOffset)
3778 break
3779 if (Pcd.TokenSpaceGuidCName, Pcd.TokenCName) in self._PcdComments:
3780 PcdCommentList = self._PcdComments[Pcd.TokenSpaceGuidCName, Pcd.TokenCName][:]
3781 if HiiInfo:
3782 UsageIndex = -1
3783 UsageStr = ''
3784 for Index, Comment in enumerate(PcdCommentList):
3785 for Usage in UsageList:
3786 if Comment.find(Usage) != -1:
3787 UsageStr = Usage
3788 UsageIndex = Index
3789 break
3790 if UsageIndex != -1:
3791 PcdCommentList[UsageIndex] = '## %s %s %s' % (UsageStr, HiiInfo, PcdCommentList[UsageIndex].replace(UsageStr, ''))
3792 else:
3793 PcdCommentList.append('## UNDEFINED ' + HiiInfo)
3794 PcdComments = '\n '.join(PcdCommentList)
3795 PcdEntry = Pcd.TokenSpaceGuidCName + '.' + TokenCName
3796 if PcdComments:
3797 PcdEntry = PcdComments + '\n ' + PcdEntry
3798 AsBuiltInfDict['pcd_item'].append(PcdEntry)
3799 for Item in self.BuildOption:
3800 if 'FLAGS' in self.BuildOption[Item]:
3801 AsBuiltInfDict['flags_item'].append('%s:%s_%s_%s_%s_FLAGS = %s' % (self.ToolChainFamily, self.BuildTarget, self.ToolChain, self.Arch, Item, self.BuildOption[Item]['FLAGS'].strip()))
3802
3803 # Generated LibraryClasses section in comments.
3804 for Library in self.LibraryAutoGenList:
3805 AsBuiltInfDict['libraryclasses_item'].append(Library.MetaFile.File.replace('\\', '/'))
3806
3807 # Generated UserExtensions TianoCore section.
3808 # All tianocore user extensions are copied.
3809 UserExtStr = ''
3810 for TianoCore in self._GetTianoCoreUserExtensionList():
3811 UserExtStr += '\n'.join(TianoCore)
3812 ExtensionFile = os.path.join(self.MetaFile.Dir, TianoCore[1])
3813 if os.path.isfile(ExtensionFile):
3814 shutil.copy2(ExtensionFile, self.OutputDir)
3815 AsBuiltInfDict['userextension_tianocore_item'] = UserExtStr
3816
3817 # Generated depex expression section in comments.
3818 DepexExpresion = self._GetDepexExpresionString()
3819 AsBuiltInfDict['depexsection_item'] = DepexExpresion if DepexExpresion else ''
3820
3821 AsBuiltInf = TemplateString()
3822 AsBuiltInf.Append(gAsBuiltInfHeaderString.Replace(AsBuiltInfDict))
3823
3824 SaveFileOnChange(os.path.join(self.OutputDir, self.Name + '.inf'), str(AsBuiltInf), False)
3825
3826 self.IsAsBuiltInfCreated = True
3827 if GlobalData.gBinCacheDest:
3828 self.CopyModuleToCache()
3829
3830 def CopyModuleToCache(self):
3831 FileDir = path.join(GlobalData.gBinCacheDest, self.Arch, self.SourceDir, self.MetaFile.BaseName)
3832 CreateDirectory (FileDir)
3833 HashFile = path.join(self.BuildDir, self.Name + '.hash')
3834 ModuleFile = path.join(self.OutputDir, self.Name + '.inf')
3835 if os.path.exists(HashFile):
3836 shutil.copy2(HashFile, FileDir)
3837 if os.path.exists(ModuleFile):
3838 shutil.copy2(ModuleFile, FileDir)
3839 if not self.OutputFile:
3840 Ma = self.BuildDatabase[PathClass(ModuleFile), self.Arch, self.BuildTarget, self.ToolChain]
3841 self.OutputFile = Ma.Binaries
3842 if self.OutputFile:
3843 for File in self.OutputFile:
3844 File = str(File)
3845 if not os.path.isabs(File):
3846 File = os.path.join(self.OutputDir, File)
3847 if os.path.exists(File):
3848 shutil.copy2(File, FileDir)
3849
3850 def AttemptModuleCacheCopy(self):
3851 if self.IsBinaryModule:
3852 return False
3853 FileDir = path.join(GlobalData.gBinCacheSource, self.Arch, self.SourceDir, self.MetaFile.BaseName)
3854 HashFile = path.join(FileDir, self.Name + '.hash')
3855 if os.path.exists(HashFile):
3856 f = open(HashFile, 'r')
3857 CacheHash = f.read()
3858 f.close()
3859 if GlobalData.gModuleHash[self.Arch][self.Name]:
3860 if CacheHash == GlobalData.gModuleHash[self.Arch][self.Name]:
3861 for root, dir, files in os.walk(FileDir):
3862 for f in files:
3863 if self.Name + '.hash' in f:
3864 shutil.copy2(HashFile, self.BuildDir)
3865 else:
3866 File = path.join(root, f)
3867 shutil.copy2(File, self.OutputDir)
3868 if self.Name == "PcdPeim" or self.Name == "PcdDxe":
3869 CreatePcdDatabaseCode(self, TemplateString(), TemplateString())
3870 return True
3871 return False
3872
3873 ## Create makefile for the module and its dependent libraries
3874 #
3875 # @param CreateLibraryMakeFile Flag indicating if or not the makefiles of
3876 # dependent libraries will be created
3877 #
3878 @cached_class_function
3879 def CreateMakeFile(self, CreateLibraryMakeFile=True, GenFfsList = []):
3880 # nest this function inside it's only caller.
3881 def CreateTimeStamp():
3882 FileSet = {self.MetaFile.Path}
3883
3884 for SourceFile in self.Module.Sources:
3885 FileSet.add (SourceFile.Path)
3886
3887 for Lib in self.DependentLibraryList:
3888 FileSet.add (Lib.MetaFile.Path)
3889
3890 for f in self.AutoGenDepSet:
3891 FileSet.add (f.Path)
3892
3893 if os.path.exists (self.TimeStampPath):
3894 os.remove (self.TimeStampPath)
3895 with open(self.TimeStampPath, 'w+') as file:
3896 for f in FileSet:
3897 print(f, file=file)
3898
3899 # Ignore generating makefile when it is a binary module
3900 if self.IsBinaryModule:
3901 return
3902
3903 self.GenFfsList = GenFfsList
3904 if not self.IsLibrary and CreateLibraryMakeFile:
3905 for LibraryAutoGen in self.LibraryAutoGenList:
3906 LibraryAutoGen.CreateMakeFile()
3907
3908 if self.CanSkip():
3909 return
3910
3911 if len(self.CustomMakefile) == 0:
3912 Makefile = GenMake.ModuleMakefile(self)
3913 else:
3914 Makefile = GenMake.CustomMakefile(self)
3915 if Makefile.Generate():
3916 EdkLogger.debug(EdkLogger.DEBUG_9, "Generated makefile for module %s [%s]" %
3917 (self.Name, self.Arch))
3918 else:
3919 EdkLogger.debug(EdkLogger.DEBUG_9, "Skipped the generation of makefile for module %s [%s]" %
3920 (self.Name, self.Arch))
3921
3922 CreateTimeStamp()
3923
3924 def CopyBinaryFiles(self):
3925 for File in self.Module.Binaries:
3926 SrcPath = File.Path
3927 DstPath = os.path.join(self.OutputDir, os.path.basename(SrcPath))
3928 CopyLongFilePath(SrcPath, DstPath)
3929 ## Create autogen code for the module and its dependent libraries
3930 #
3931 # @param CreateLibraryCodeFile Flag indicating if or not the code of
3932 # dependent libraries will be created
3933 #
3934 def CreateCodeFile(self, CreateLibraryCodeFile=True):
3935 if self.IsCodeFileCreated:
3936 return
3937
3938 # Need to generate PcdDatabase even PcdDriver is binarymodule
3939 if self.IsBinaryModule and self.PcdIsDriver != '':
3940 CreatePcdDatabaseCode(self, TemplateString(), TemplateString())
3941 return
3942 if self.IsBinaryModule:
3943 if self.IsLibrary:
3944 self.CopyBinaryFiles()
3945 return
3946
3947 if not self.IsLibrary and CreateLibraryCodeFile:
3948 for LibraryAutoGen in self.LibraryAutoGenList:
3949 LibraryAutoGen.CreateCodeFile()
3950
3951 if self.CanSkip():
3952 return
3953
3954 AutoGenList = []
3955 IgoredAutoGenList = []
3956
3957 for File in self.AutoGenFileList:
3958 if GenC.Generate(File.Path, self.AutoGenFileList[File], File.IsBinary):
3959 #Ignore Edk AutoGen.c
3960 if self.AutoGenVersion < 0x00010005 and File.Name == 'AutoGen.c':
3961 continue
3962
3963 AutoGenList.append(str(File))
3964 else:
3965 IgoredAutoGenList.append(str(File))
3966
3967 # Skip the following code for EDK I inf
3968 if self.AutoGenVersion < 0x00010005:
3969 return
3970
3971 for ModuleType in self.DepexList:
3972 # Ignore empty [depex] section or [depex] section for SUP_MODULE_USER_DEFINED module
3973 if len(self.DepexList[ModuleType]) == 0 or ModuleType == SUP_MODULE_USER_DEFINED:
3974 continue
3975
3976 Dpx = GenDepex.DependencyExpression(self.DepexList[ModuleType], ModuleType, True)
3977 DpxFile = gAutoGenDepexFileName % {"module_name" : self.Name}
3978
3979 if len(Dpx.PostfixNotation) != 0:
3980 self.DepexGenerated = True
3981
3982 if Dpx.Generate(path.join(self.OutputDir, DpxFile)):
3983 AutoGenList.append(str(DpxFile))
3984 else:
3985 IgoredAutoGenList.append(str(DpxFile))
3986
3987 if IgoredAutoGenList == []:
3988 EdkLogger.debug(EdkLogger.DEBUG_9, "Generated [%s] files for module %s [%s]" %
3989 (" ".join(AutoGenList), self.Name, self.Arch))
3990 elif AutoGenList == []:
3991 EdkLogger.debug(EdkLogger.DEBUG_9, "Skipped the generation of [%s] files for module %s [%s]" %
3992 (" ".join(IgoredAutoGenList), self.Name, self.Arch))
3993 else:
3994 EdkLogger.debug(EdkLogger.DEBUG_9, "Generated [%s] (skipped %s) files for module %s [%s]" %
3995 (" ".join(AutoGenList), " ".join(IgoredAutoGenList), self.Name, self.Arch))
3996
3997 self.IsCodeFileCreated = True
3998 return AutoGenList
3999
4000 ## Summarize the ModuleAutoGen objects of all libraries used by this module
4001 @cached_property
4002 def LibraryAutoGenList(self):
4003 RetVal = []
4004 for Library in self.DependentLibraryList:
4005 La = ModuleAutoGen(
4006 self.Workspace,
4007 Library.MetaFile,
4008 self.BuildTarget,
4009 self.ToolChain,
4010 self.Arch,
4011 self.PlatformInfo.MetaFile
4012 )
4013 if La not in RetVal:
4014 RetVal.append(La)
4015 for Lib in La.CodaTargetList:
4016 self._ApplyBuildRule(Lib.Target, TAB_UNKNOWN_FILE)
4017 return RetVal
4018
4019 def GenModuleHash(self):
4020 if self.Arch not in GlobalData.gModuleHash:
4021 GlobalData.gModuleHash[self.Arch] = {}
4022 m = hashlib.md5()
4023 # Add Platform level hash
4024 m.update(GlobalData.gPlatformHash)
4025 # Add Package level hash
4026 if self.DependentPackageList:
4027 for Pkg in sorted(self.DependentPackageList, key=lambda x: x.PackageName):
4028 if Pkg.PackageName in GlobalData.gPackageHash[self.Arch]:
4029 m.update(GlobalData.gPackageHash[self.Arch][Pkg.PackageName])
4030
4031 # Add Library hash
4032 if self.LibraryAutoGenList:
4033 for Lib in sorted(self.LibraryAutoGenList, key=lambda x: x.Name):
4034 if Lib.Name not in GlobalData.gModuleHash[self.Arch]:
4035 Lib.GenModuleHash()
4036 m.update(GlobalData.gModuleHash[self.Arch][Lib.Name])
4037
4038 # Add Module self
4039 f = open(str(self.MetaFile), 'r')
4040 Content = f.read()
4041 f.close()
4042 m.update(Content)
4043 # Add Module's source files
4044 if self.SourceFileList:
4045 for File in sorted(self.SourceFileList, key=lambda x: str(x)):
4046 f = open(str(File), 'r')
4047 Content = f.read()
4048 f.close()
4049 m.update(Content)
4050
4051 ModuleHashFile = path.join(self.BuildDir, self.Name + ".hash")
4052 if self.Name not in GlobalData.gModuleHash[self.Arch]:
4053 GlobalData.gModuleHash[self.Arch][self.Name] = m.hexdigest()
4054 if GlobalData.gBinCacheSource:
4055 if self.AttemptModuleCacheCopy():
4056 return False
4057 return SaveFileOnChange(ModuleHashFile, m.hexdigest(), True)
4058
4059 ## Decide whether we can skip the ModuleAutoGen process
4060 def CanSkipbyHash(self):
4061 if GlobalData.gUseHashCache:
4062 return not self.GenModuleHash()
4063 return False
4064
4065 ## Decide whether we can skip the ModuleAutoGen process
4066 # If any source file is newer than the module than we cannot skip
4067 #
4068 def CanSkip(self):
4069 if self.MakeFileDir in GlobalData.gSikpAutoGenCache:
4070 return True
4071 if not os.path.exists(self.TimeStampPath):
4072 return False
4073 #last creation time of the module
4074 DstTimeStamp = os.stat(self.TimeStampPath)[8]
4075
4076 SrcTimeStamp = self.Workspace._SrcTimeStamp
4077 if SrcTimeStamp > DstTimeStamp:
4078 return False
4079
4080 with open(self.TimeStampPath,'r') as f:
4081 for source in f:
4082 source = source.rstrip('\n')
4083 if not os.path.exists(source):
4084 return False
4085 if source not in ModuleAutoGen.TimeDict :
4086 ModuleAutoGen.TimeDict[source] = os.stat(source)[8]
4087 if ModuleAutoGen.TimeDict[source] > DstTimeStamp:
4088 return False
4089 GlobalData.gSikpAutoGenCache.add(self.MakeFileDir)
4090 return True
4091
4092 @cached_property
4093 def TimeStampPath(self):
4094 return os.path.join(self.MakeFileDir, 'AutoGenTimeStamp')