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