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