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