]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/build/build.py
BaseTools: Remove Makefile/MakefileName fields
[mirror_edk2.git] / BaseTools / Source / Python / build / build.py
1 ## @file
2 # build a platform or a module
3 #
4 # Copyright (c) 2014, Hewlett-Packard Development Company, L.P.<BR>
5 # Copyright (c) 2007 - 2021, Intel Corporation. All rights reserved.<BR>
6 # Copyright (c) 2018, Hewlett Packard Enterprise Development, L.P.<BR>
7 # Copyright (c) 2020 - 2021, ARM Limited. All rights reserved.<BR>
8 #
9 # SPDX-License-Identifier: BSD-2-Clause-Patent
10 #
11
12 ##
13 # Import Modules
14 #
15 from __future__ import print_function
16 from __future__ import absolute_import
17 import os.path as path
18 import sys
19 import os
20 import re
21 import glob
22 import time
23 import platform
24 import traceback
25 import multiprocessing
26 from threading import Thread,Event,BoundedSemaphore
27 import threading
28 from linecache import getlines
29 from subprocess import Popen,PIPE, STDOUT
30 from collections import OrderedDict, defaultdict
31
32 from AutoGen.PlatformAutoGen import PlatformAutoGen
33 from AutoGen.ModuleAutoGen import ModuleAutoGen
34 from AutoGen.WorkspaceAutoGen import WorkspaceAutoGen
35 from AutoGen.AutoGenWorker import AutoGenWorkerInProcess,AutoGenManager,\
36 LogAgent
37 from AutoGen import GenMake
38 from Common import Misc as Utils
39
40 from Common.TargetTxtClassObject import TargetTxtDict
41 from Common.ToolDefClassObject import ToolDefDict
42 from buildoptions import MyOptionParser
43 from Common.Misc import PathClass,SaveFileOnChange,RemoveDirectory
44 from Common.StringUtils import NormPath
45 from Common.MultipleWorkspace import MultipleWorkspace as mws
46 from Common.BuildToolError import *
47 from Common.DataType import *
48 import Common.EdkLogger as EdkLogger
49
50 from Workspace.WorkspaceDatabase import BuildDB
51
52 from BuildReport import BuildReport
53 from GenPatchPcdTable.GenPatchPcdTable import PeImageClass,parsePcdInfoFromMapFile
54 from PatchPcdValue.PatchPcdValue import PatchBinaryFile
55
56 import Common.GlobalData as GlobalData
57 from GenFds.GenFds import GenFds, GenFdsApi
58 import multiprocessing as mp
59 from multiprocessing import Manager
60 from AutoGen.DataPipe import MemoryDataPipe
61 from AutoGen.ModuleAutoGenHelper import WorkSpaceInfo, PlatformInfo
62 from GenFds.FdfParser import FdfParser
63 from AutoGen.IncludesAutoGen import IncludesAutoGen
64 from GenFds.GenFds import resetFdsGlobalVariable
65 from AutoGen.AutoGen import CalculatePriorityValue
66
67 ## standard targets of build command
68 gSupportedTarget = ['all', 'genc', 'genmake', 'modules', 'libraries', 'fds', 'clean', 'cleanall', 'cleanlib', 'run']
69
70 ## build configuration file
71 gBuildConfiguration = "target.txt"
72 gToolsDefinition = "tools_def.txt"
73
74 TemporaryTablePattern = re.compile(r'^_\d+_\d+_[a-fA-F0-9]+$')
75 TmpTableDict = {}
76
77 ## Check environment PATH variable to make sure the specified tool is found
78 #
79 # If the tool is found in the PATH, then True is returned
80 # Otherwise, False is returned
81 #
82 def IsToolInPath(tool):
83 if 'PATHEXT' in os.environ:
84 extns = os.environ['PATHEXT'].split(os.path.pathsep)
85 else:
86 extns = ('',)
87 for pathDir in os.environ['PATH'].split(os.path.pathsep):
88 for ext in extns:
89 if os.path.exists(os.path.join(pathDir, tool + ext)):
90 return True
91 return False
92
93 ## Check environment variables
94 #
95 # Check environment variables that must be set for build. Currently they are
96 #
97 # WORKSPACE The directory all packages/platforms start from
98 # EDK_TOOLS_PATH The directory contains all tools needed by the build
99 # PATH $(EDK_TOOLS_PATH)/Bin/<sys> must be set in PATH
100 #
101 # If any of above environment variable is not set or has error, the build
102 # will be broken.
103 #
104 def CheckEnvVariable():
105 # check WORKSPACE
106 if "WORKSPACE" not in os.environ:
107 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
108 ExtraData="WORKSPACE")
109
110 WorkspaceDir = os.path.normcase(os.path.normpath(os.environ["WORKSPACE"]))
111 if not os.path.exists(WorkspaceDir):
112 EdkLogger.error("build", FILE_NOT_FOUND, "WORKSPACE doesn't exist", ExtraData=WorkspaceDir)
113 elif ' ' in WorkspaceDir:
114 EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in WORKSPACE path",
115 ExtraData=WorkspaceDir)
116 os.environ["WORKSPACE"] = WorkspaceDir
117
118 # set multiple workspace
119 PackagesPath = os.getenv("PACKAGES_PATH")
120 mws.setWs(WorkspaceDir, PackagesPath)
121 if mws.PACKAGES_PATH:
122 for Path in mws.PACKAGES_PATH:
123 if not os.path.exists(Path):
124 EdkLogger.error("build", FILE_NOT_FOUND, "One Path in PACKAGES_PATH doesn't exist", ExtraData=Path)
125 elif ' ' in Path:
126 EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in PACKAGES_PATH", ExtraData=Path)
127
128
129 os.environ["EDK_TOOLS_PATH"] = os.path.normcase(os.environ["EDK_TOOLS_PATH"])
130
131 # check EDK_TOOLS_PATH
132 if "EDK_TOOLS_PATH" not in os.environ:
133 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
134 ExtraData="EDK_TOOLS_PATH")
135
136 # check PATH
137 if "PATH" not in os.environ:
138 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
139 ExtraData="PATH")
140
141 GlobalData.gWorkspace = WorkspaceDir
142
143 GlobalData.gGlobalDefines["WORKSPACE"] = WorkspaceDir
144 GlobalData.gGlobalDefines["EDK_TOOLS_PATH"] = os.environ["EDK_TOOLS_PATH"]
145
146 ## Get normalized file path
147 #
148 # Convert the path to be local format, and remove the WORKSPACE path at the
149 # beginning if the file path is given in full path.
150 #
151 # @param FilePath File path to be normalized
152 # @param Workspace Workspace path which the FilePath will be checked against
153 #
154 # @retval string The normalized file path
155 #
156 def NormFile(FilePath, Workspace):
157 # check if the path is absolute or relative
158 if os.path.isabs(FilePath):
159 FileFullPath = os.path.normpath(FilePath)
160 else:
161 FileFullPath = os.path.normpath(mws.join(Workspace, FilePath))
162 Workspace = mws.getWs(Workspace, FilePath)
163
164 # check if the file path exists or not
165 if not os.path.isfile(FileFullPath):
166 EdkLogger.error("build", FILE_NOT_FOUND, ExtraData="\t%s (Please give file in absolute path or relative to WORKSPACE)" % FileFullPath)
167
168 # remove workspace directory from the beginning part of the file path
169 if Workspace[-1] in ["\\", "/"]:
170 return FileFullPath[len(Workspace):]
171 else:
172 return FileFullPath[(len(Workspace) + 1):]
173
174 ## Get the output of an external program
175 #
176 # This is the entrance method of thread reading output of an external program and
177 # putting them in STDOUT/STDERR of current program.
178 #
179 # @param From The stream message read from
180 # @param To The stream message put on
181 # @param ExitFlag The flag used to indicate stopping reading
182 #
183 def ReadMessage(From, To, ExitFlag,MemTo=None):
184 while True:
185 # read one line a time
186 Line = From.readline()
187 # empty string means "end"
188 if Line is not None and Line != b"":
189 LineStr = Line.rstrip().decode(encoding='utf-8', errors='ignore')
190 if MemTo is not None:
191 if "Note: including file:" == LineStr.lstrip()[:21]:
192 MemTo.append(LineStr)
193 else:
194 To(LineStr)
195 MemTo.append(LineStr)
196 else:
197 To(LineStr)
198 else:
199 break
200 if ExitFlag.is_set():
201 break
202
203 class MakeSubProc(Popen):
204 def __init__(self,*args, **argv):
205 super(MakeSubProc,self).__init__(*args, **argv)
206 self.ProcOut = []
207
208 ## Launch an external program
209 #
210 # This method will call subprocess.Popen to execute an external program with
211 # given options in specified directory. Because of the dead-lock issue during
212 # redirecting output of the external program, threads are used to to do the
213 # redirection work.
214 #
215 # @param Command A list or string containing the call of the program
216 # @param WorkingDir The directory in which the program will be running
217 #
218 def LaunchCommand(Command, WorkingDir,ModuleAuto = None):
219 BeginTime = time.time()
220 # if working directory doesn't exist, Popen() will raise an exception
221 if not os.path.isdir(WorkingDir):
222 EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=WorkingDir)
223
224 # Command is used as the first Argument in following Popen().
225 # It could be a string or sequence. We find that if command is a string in following Popen(),
226 # ubuntu may fail with an error message that the command is not found.
227 # So here we may need convert command from string to list instance.
228 if platform.system() != 'Windows':
229 if not isinstance(Command, list):
230 Command = Command.split()
231 Command = ' '.join(Command)
232
233 Proc = None
234 EndOfProcedure = None
235 try:
236 # launch the command
237 Proc = MakeSubProc(Command, stdout=PIPE, stderr=STDOUT, env=os.environ, cwd=WorkingDir, bufsize=-1, shell=True)
238
239 # launch two threads to read the STDOUT and STDERR
240 EndOfProcedure = Event()
241 EndOfProcedure.clear()
242 if Proc.stdout:
243 StdOutThread = Thread(target=ReadMessage, args=(Proc.stdout, EdkLogger.info, EndOfProcedure,Proc.ProcOut))
244 StdOutThread.name = "STDOUT-Redirector"
245 StdOutThread.daemon = False
246 StdOutThread.start()
247
248
249 # waiting for program exit
250 Proc.wait()
251 except: # in case of aborting
252 # terminate the threads redirecting the program output
253 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
254 if EndOfProcedure is not None:
255 EndOfProcedure.set()
256 if Proc is None:
257 if not isinstance(Command, type("")):
258 Command = " ".join(Command)
259 EdkLogger.error("build", COMMAND_FAILURE, "Failed to start command", ExtraData="%s [%s]" % (Command, WorkingDir))
260
261 if Proc.stdout:
262 StdOutThread.join()
263
264 # check the return code of the program
265 if Proc.returncode != 0:
266 if not isinstance(Command, type("")):
267 Command = " ".join(Command)
268 # print out the Response file and its content when make failure
269 RespFile = os.path.join(WorkingDir, 'OUTPUT', 'respfilelist.txt')
270 if os.path.isfile(RespFile):
271 f = open(RespFile)
272 RespContent = f.read()
273 f.close()
274 EdkLogger.info(RespContent)
275
276 EdkLogger.error("build", COMMAND_FAILURE, ExtraData="%s [%s]" % (Command, WorkingDir))
277 if ModuleAuto:
278 iau = IncludesAutoGen(WorkingDir,ModuleAuto)
279 if ModuleAuto.ToolChainFamily == TAB_COMPILER_MSFT:
280 iau.CreateDepsFileForMsvc(Proc.ProcOut)
281 else:
282 iau.UpdateDepsFileforNonMsvc()
283 iau.UpdateDepsFileforTrim()
284 iau.CreateModuleDeps()
285 iau.CreateDepsInclude()
286 iau.CreateDepsTarget()
287 return "%dms" % (int(round((time.time() - BeginTime) * 1000)))
288
289 ## The smallest unit that can be built in multi-thread build mode
290 #
291 # This is the base class of build unit. The "Obj" parameter must provide
292 # __str__(), __eq__() and __hash__() methods. Otherwise there could be build units
293 # missing build.
294 #
295 # Currently the "Obj" should be only ModuleAutoGen or PlatformAutoGen objects.
296 #
297 class BuildUnit:
298 ## The constructor
299 #
300 # @param self The object pointer
301 # @param Obj The object the build is working on
302 # @param Target The build target name, one of gSupportedTarget
303 # @param Dependency The BuildUnit(s) which must be completed in advance
304 # @param WorkingDir The directory build command starts in
305 #
306 def __init__(self, Obj, BuildCommand, Target, Dependency, WorkingDir="."):
307 self.BuildObject = Obj
308 self.Dependency = Dependency
309 self.WorkingDir = WorkingDir
310 self.Target = Target
311 self.BuildCommand = BuildCommand
312 if not BuildCommand:
313 EdkLogger.error("build", OPTION_MISSING,
314 "No build command found for this module. "
315 "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %
316 (Obj.BuildTarget, Obj.ToolChain, Obj.Arch),
317 ExtraData=str(Obj))
318
319
320 ## str() method
321 #
322 # It just returns the string representation of self.BuildObject
323 #
324 # @param self The object pointer
325 #
326 def __str__(self):
327 return str(self.BuildObject)
328
329 ## "==" operator method
330 #
331 # It just compares self.BuildObject with "Other". So self.BuildObject must
332 # provide its own __eq__() method.
333 #
334 # @param self The object pointer
335 # @param Other The other BuildUnit object compared to
336 #
337 def __eq__(self, Other):
338 return Other and self.BuildObject == Other.BuildObject \
339 and Other.BuildObject \
340 and self.BuildObject.Arch == Other.BuildObject.Arch
341
342 ## hash() method
343 #
344 # It just returns the hash value of self.BuildObject which must be hashable.
345 #
346 # @param self The object pointer
347 #
348 def __hash__(self):
349 return hash(self.BuildObject) + hash(self.BuildObject.Arch)
350
351 def __repr__(self):
352 return repr(self.BuildObject)
353
354 ## The smallest module unit that can be built by nmake/make command in multi-thread build mode
355 #
356 # This class is for module build by nmake/make build system. The "Obj" parameter
357 # must provide __str__(), __eq__() and __hash__() methods. Otherwise there could
358 # be make units missing build.
359 #
360 # Currently the "Obj" should be only ModuleAutoGen object.
361 #
362 class ModuleMakeUnit(BuildUnit):
363 ## The constructor
364 #
365 # @param self The object pointer
366 # @param Obj The ModuleAutoGen object the build is working on
367 # @param Target The build target name, one of gSupportedTarget
368 #
369 def __init__(self, Obj, BuildCommand,Target):
370 Dependency = [ModuleMakeUnit(La, BuildCommand,Target) for La in Obj.LibraryAutoGenList]
371 BuildUnit.__init__(self, Obj, BuildCommand, Target, Dependency, Obj.MakeFileDir)
372 if Target in [None, "", "all"]:
373 self.Target = "tbuild"
374
375 ## The smallest platform unit that can be built by nmake/make command in multi-thread build mode
376 #
377 # This class is for platform build by nmake/make build system. The "Obj" parameter
378 # must provide __str__(), __eq__() and __hash__() methods. Otherwise there could
379 # be make units missing build.
380 #
381 # Currently the "Obj" should be only PlatformAutoGen object.
382 #
383 class PlatformMakeUnit(BuildUnit):
384 ## The constructor
385 #
386 # @param self The object pointer
387 # @param Obj The PlatformAutoGen object the build is working on
388 # @param Target The build target name, one of gSupportedTarget
389 #
390 def __init__(self, Obj, BuildCommand, Target):
391 Dependency = [ModuleMakeUnit(Lib, BuildCommand, Target) for Lib in self.BuildObject.LibraryAutoGenList]
392 Dependency.extend([ModuleMakeUnit(Mod, BuildCommand,Target) for Mod in self.BuildObject.ModuleAutoGenList])
393 BuildUnit.__init__(self, Obj, BuildCommand, Target, Dependency, Obj.MakeFileDir)
394
395 ## The class representing the task of a module build or platform build
396 #
397 # This class manages the build tasks in multi-thread build mode. Its jobs include
398 # scheduling thread running, catching thread error, monitor the thread status, etc.
399 #
400 class BuildTask:
401 # queue for tasks waiting for schedule
402 _PendingQueue = OrderedDict()
403 _PendingQueueLock = threading.Lock()
404
405 # queue for tasks ready for running
406 _ReadyQueue = OrderedDict()
407 _ReadyQueueLock = threading.Lock()
408
409 # queue for run tasks
410 _RunningQueue = OrderedDict()
411 _RunningQueueLock = threading.Lock()
412
413 # queue containing all build tasks, in case duplicate build
414 _TaskQueue = OrderedDict()
415
416 # flag indicating error occurs in a running thread
417 _ErrorFlag = threading.Event()
418 _ErrorFlag.clear()
419 _ErrorMessage = ""
420
421 # BoundedSemaphore object used to control the number of running threads
422 _Thread = None
423
424 # flag indicating if the scheduler is started or not
425 _SchedulerStopped = threading.Event()
426 _SchedulerStopped.set()
427
428 ## Start the task scheduler thread
429 #
430 # @param MaxThreadNumber The maximum thread number
431 # @param ExitFlag Flag used to end the scheduler
432 #
433 @staticmethod
434 def StartScheduler(MaxThreadNumber, ExitFlag):
435 SchedulerThread = Thread(target=BuildTask.Scheduler, args=(MaxThreadNumber, ExitFlag))
436 SchedulerThread.name = "Build-Task-Scheduler"
437 SchedulerThread.daemon = False
438 SchedulerThread.start()
439 # wait for the scheduler to be started, especially useful in Linux
440 while not BuildTask.IsOnGoing():
441 time.sleep(0.01)
442
443 ## Scheduler method
444 #
445 # @param MaxThreadNumber The maximum thread number
446 # @param ExitFlag Flag used to end the scheduler
447 #
448 @staticmethod
449 def Scheduler(MaxThreadNumber, ExitFlag):
450 BuildTask._SchedulerStopped.clear()
451 try:
452 # use BoundedSemaphore to control the maximum running threads
453 BuildTask._Thread = BoundedSemaphore(MaxThreadNumber)
454 #
455 # scheduling loop, which will exits when no pending/ready task and
456 # indicated to do so, or there's error in running thread
457 #
458 while (len(BuildTask._PendingQueue) > 0 or len(BuildTask._ReadyQueue) > 0 \
459 or not ExitFlag.is_set()) and not BuildTask._ErrorFlag.is_set():
460 EdkLogger.debug(EdkLogger.DEBUG_8, "Pending Queue (%d), Ready Queue (%d)"
461 % (len(BuildTask._PendingQueue), len(BuildTask._ReadyQueue)))
462
463 # get all pending tasks
464 BuildTask._PendingQueueLock.acquire()
465 BuildObjectList = list(BuildTask._PendingQueue.keys())
466 #
467 # check if their dependency is resolved, and if true, move them
468 # into ready queue
469 #
470 for BuildObject in BuildObjectList:
471 Bt = BuildTask._PendingQueue[BuildObject]
472 if Bt.IsReady():
473 BuildTask._ReadyQueue[BuildObject] = BuildTask._PendingQueue.pop(BuildObject)
474 BuildTask._PendingQueueLock.release()
475
476 # launch build thread until the maximum number of threads is reached
477 while not BuildTask._ErrorFlag.is_set():
478 # empty ready queue, do nothing further
479 if len(BuildTask._ReadyQueue) == 0:
480 break
481
482 # wait for active thread(s) exit
483 BuildTask._Thread.acquire(True)
484
485 # start a new build thread
486 Bo, Bt = BuildTask._ReadyQueue.popitem()
487
488 # move into running queue
489 BuildTask._RunningQueueLock.acquire()
490 BuildTask._RunningQueue[Bo] = Bt
491 BuildTask._RunningQueueLock.release()
492
493 Bt.Start()
494 # avoid tense loop
495 time.sleep(0.01)
496
497 # avoid tense loop
498 time.sleep(0.01)
499
500 # wait for all running threads exit
501 if BuildTask._ErrorFlag.is_set():
502 EdkLogger.quiet("\nWaiting for all build threads exit...")
503 # while not BuildTask._ErrorFlag.is_set() and \
504 while len(BuildTask._RunningQueue) > 0:
505 EdkLogger.verbose("Waiting for thread ending...(%d)" % len(BuildTask._RunningQueue))
506 EdkLogger.debug(EdkLogger.DEBUG_8, "Threads [%s]" % ", ".join(Th.name for Th in threading.enumerate()))
507 # avoid tense loop
508 time.sleep(0.1)
509 except BaseException as X:
510 #
511 # TRICK: hide the output of threads left running, so that the user can
512 # catch the error message easily
513 #
514 EdkLogger.SetLevel(EdkLogger.ERROR)
515 BuildTask._ErrorFlag.set()
516 BuildTask._ErrorMessage = "build thread scheduler error\n\t%s" % str(X)
517
518 BuildTask._PendingQueue.clear()
519 BuildTask._ReadyQueue.clear()
520 BuildTask._RunningQueue.clear()
521 BuildTask._TaskQueue.clear()
522 BuildTask._SchedulerStopped.set()
523
524 ## Wait for all running method exit
525 #
526 @staticmethod
527 def WaitForComplete():
528 BuildTask._SchedulerStopped.wait()
529
530 ## Check if the scheduler is running or not
531 #
532 @staticmethod
533 def IsOnGoing():
534 return not BuildTask._SchedulerStopped.is_set()
535
536 ## Abort the build
537 @staticmethod
538 def Abort():
539 if BuildTask.IsOnGoing():
540 BuildTask._ErrorFlag.set()
541 BuildTask.WaitForComplete()
542
543 ## Check if there's error in running thread
544 #
545 # Since the main thread cannot catch exceptions in other thread, we have to
546 # use threading.Event to communicate this formation to main thread.
547 #
548 @staticmethod
549 def HasError():
550 return BuildTask._ErrorFlag.is_set()
551
552 ## Get error message in running thread
553 #
554 # Since the main thread cannot catch exceptions in other thread, we have to
555 # use a static variable to communicate this message to main thread.
556 #
557 @staticmethod
558 def GetErrorMessage():
559 return BuildTask._ErrorMessage
560
561 ## Factory method to create a BuildTask object
562 #
563 # This method will check if a module is building or has been built. And if
564 # true, just return the associated BuildTask object in the _TaskQueue. If
565 # not, create and return a new BuildTask object. The new BuildTask object
566 # will be appended to the _PendingQueue for scheduling later.
567 #
568 # @param BuildItem A BuildUnit object representing a build object
569 # @param Dependency The dependent build object of BuildItem
570 #
571 @staticmethod
572 def New(BuildItem, Dependency=None):
573 if BuildItem in BuildTask._TaskQueue:
574 Bt = BuildTask._TaskQueue[BuildItem]
575 return Bt
576
577 Bt = BuildTask()
578 Bt._Init(BuildItem, Dependency)
579 BuildTask._TaskQueue[BuildItem] = Bt
580
581 BuildTask._PendingQueueLock.acquire()
582 BuildTask._PendingQueue[BuildItem] = Bt
583 BuildTask._PendingQueueLock.release()
584
585 return Bt
586
587 ## The real constructor of BuildTask
588 #
589 # @param BuildItem A BuildUnit object representing a build object
590 # @param Dependency The dependent build object of BuildItem
591 #
592 def _Init(self, BuildItem, Dependency=None):
593 self.BuildItem = BuildItem
594
595 self.DependencyList = []
596 if Dependency is None:
597 Dependency = BuildItem.Dependency
598 else:
599 Dependency.extend(BuildItem.Dependency)
600 self.AddDependency(Dependency)
601 # flag indicating build completes, used to avoid unnecessary re-build
602 self.CompleteFlag = False
603
604 ## Check if all dependent build tasks are completed or not
605 #
606 def IsReady(self):
607 ReadyFlag = True
608 for Dep in self.DependencyList:
609 if Dep.CompleteFlag == True:
610 continue
611 ReadyFlag = False
612 break
613
614 return ReadyFlag
615
616 ## Add dependent build task
617 #
618 # @param Dependency The list of dependent build objects
619 #
620 def AddDependency(self, Dependency):
621 for Dep in Dependency:
622 if not Dep.BuildObject.IsBinaryModule and not Dep.BuildObject.CanSkipbyCache(GlobalData.gModuleCacheHit):
623 self.DependencyList.append(BuildTask.New(Dep)) # BuildTask list
624
625 ## The thread wrapper of LaunchCommand function
626 #
627 # @param Command A list or string contains the call of the command
628 # @param WorkingDir The directory in which the program will be running
629 #
630 def _CommandThread(self, Command, WorkingDir):
631 try:
632 self.BuildItem.BuildObject.BuildTime = LaunchCommand(Command, WorkingDir,self.BuildItem.BuildObject)
633 self.CompleteFlag = True
634
635 # Run hash operation post dependency to account for libs
636 # Run if --hash or --binary-destination
637 if GlobalData.gUseHashCache and not GlobalData.gBinCacheSource:
638 self.BuildItem.BuildObject.GenModuleHash()
639 if GlobalData.gBinCacheDest:
640 self.BuildItem.BuildObject.GenCMakeHash()
641
642 except:
643 #
644 # TRICK: hide the output of threads left running, so that the user can
645 # catch the error message easily
646 #
647 if not BuildTask._ErrorFlag.is_set():
648 GlobalData.gBuildingModule = "%s [%s, %s, %s]" % (str(self.BuildItem.BuildObject),
649 self.BuildItem.BuildObject.Arch,
650 self.BuildItem.BuildObject.ToolChain,
651 self.BuildItem.BuildObject.BuildTarget
652 )
653 EdkLogger.SetLevel(EdkLogger.ERROR)
654 BuildTask._ErrorFlag.set()
655 BuildTask._ErrorMessage = "%s broken\n %s [%s]" % \
656 (threading.current_thread().name, Command, WorkingDir)
657
658 # indicate there's a thread is available for another build task
659 BuildTask._RunningQueueLock.acquire()
660 BuildTask._RunningQueue.pop(self.BuildItem)
661 BuildTask._RunningQueueLock.release()
662 BuildTask._Thread.release()
663
664 ## Start build task thread
665 #
666 def Start(self):
667 EdkLogger.quiet("Building ... %s" % repr(self.BuildItem))
668 Command = self.BuildItem.BuildCommand + [self.BuildItem.Target]
669 self.BuildTread = Thread(target=self._CommandThread, args=(Command, self.BuildItem.WorkingDir))
670 self.BuildTread.name = "build thread"
671 self.BuildTread.daemon = False
672 self.BuildTread.start()
673
674 ## The class contains the information related to EFI image
675 #
676 class PeImageInfo():
677 ## Constructor
678 #
679 # Constructor will load all required image information.
680 #
681 # @param BaseName The full file path of image.
682 # @param Guid The GUID for image.
683 # @param Arch Arch of this image.
684 # @param OutputDir The output directory for image.
685 # @param DebugDir The debug directory for image.
686 # @param ImageClass PeImage Information
687 #
688 def __init__(self, BaseName, Guid, Arch, OutputDir, DebugDir, ImageClass):
689 self.BaseName = BaseName
690 self.Guid = Guid
691 self.Arch = Arch
692 self.OutputDir = OutputDir
693 self.DebugDir = DebugDir
694 self.Image = ImageClass
695 self.Image.Size = (self.Image.Size // 0x1000 + 1) * 0x1000
696
697 ## The class implementing the EDK2 build process
698 #
699 # The build process includes:
700 # 1. Load configuration from target.txt and tools_def.txt in $(WORKSPACE)/Conf
701 # 2. Parse DSC file of active platform
702 # 3. Parse FDF file if any
703 # 4. Establish build database, including parse all other files (module, package)
704 # 5. Create AutoGen files (C code file, depex file, makefile) if necessary
705 # 6. Call build command
706 #
707 class Build():
708 ## Constructor
709 #
710 # Constructor will load all necessary configurations, parse platform, modules
711 # and packages and the establish a database for AutoGen.
712 #
713 # @param Target The build command target, one of gSupportedTarget
714 # @param WorkspaceDir The directory of workspace
715 # @param BuildOptions Build options passed from command line
716 #
717 def __init__(self, Target, WorkspaceDir, BuildOptions,log_q):
718 self.WorkspaceDir = WorkspaceDir
719 self.Target = Target
720 self.PlatformFile = BuildOptions.PlatformFile
721 self.ModuleFile = BuildOptions.ModuleFile
722 self.ArchList = BuildOptions.TargetArch
723 self.ToolChainList = BuildOptions.ToolChain
724 self.BuildTargetList= BuildOptions.BuildTarget
725 self.Fdf = BuildOptions.FdfFile
726 self.FdList = BuildOptions.RomImage
727 self.FvList = BuildOptions.FvImage
728 self.CapList = BuildOptions.CapName
729 self.SilentMode = BuildOptions.SilentMode
730 self.ThreadNumber = 1
731 self.SkipAutoGen = BuildOptions.SkipAutoGen
732 self.Reparse = BuildOptions.Reparse
733 self.SkuId = BuildOptions.SkuId
734 if self.SkuId:
735 GlobalData.gSKUID_CMD = self.SkuId
736 self.ConfDirectory = BuildOptions.ConfDirectory
737 self.SpawnMode = True
738 self.BuildReport = BuildReport(BuildOptions.ReportFile, BuildOptions.ReportType)
739 self.AutoGenTime = 0
740 self.MakeTime = 0
741 self.GenFdsTime = 0
742 self.MakeFileName = ""
743 TargetObj = TargetTxtDict()
744 ToolDefObj = ToolDefDict((os.path.join(os.getenv("WORKSPACE"),"Conf")))
745 self.TargetTxt = TargetObj.Target
746 self.ToolDef = ToolDefObj.ToolDef
747 GlobalData.BuildOptionPcd = BuildOptions.OptionPcd if BuildOptions.OptionPcd else []
748 #Set global flag for build mode
749 GlobalData.gIgnoreSource = BuildOptions.IgnoreSources
750 GlobalData.gUseHashCache = BuildOptions.UseHashCache
751 GlobalData.gBinCacheDest = BuildOptions.BinCacheDest
752 GlobalData.gBinCacheSource = BuildOptions.BinCacheSource
753 GlobalData.gEnableGenfdsMultiThread = not BuildOptions.NoGenfdsMultiThread
754 GlobalData.gDisableIncludePathCheck = BuildOptions.DisableIncludePathCheck
755
756 if GlobalData.gBinCacheDest and not GlobalData.gUseHashCache:
757 EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData="--binary-destination must be used together with --hash.")
758
759 if GlobalData.gBinCacheSource and not GlobalData.gUseHashCache:
760 EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData="--binary-source must be used together with --hash.")
761
762 if GlobalData.gBinCacheDest and GlobalData.gBinCacheSource:
763 EdkLogger.error("build", OPTION_NOT_SUPPORTED, ExtraData="--binary-destination can not be used together with --binary-source.")
764
765 if GlobalData.gBinCacheSource:
766 BinCacheSource = os.path.normpath(GlobalData.gBinCacheSource)
767 if not os.path.isabs(BinCacheSource):
768 BinCacheSource = mws.join(self.WorkspaceDir, BinCacheSource)
769 GlobalData.gBinCacheSource = BinCacheSource
770 else:
771 if GlobalData.gBinCacheSource is not None:
772 EdkLogger.error("build", OPTION_VALUE_INVALID, ExtraData="Invalid value of option --binary-source.")
773
774 if GlobalData.gBinCacheDest:
775 BinCacheDest = os.path.normpath(GlobalData.gBinCacheDest)
776 if not os.path.isabs(BinCacheDest):
777 BinCacheDest = mws.join(self.WorkspaceDir, BinCacheDest)
778 GlobalData.gBinCacheDest = BinCacheDest
779 else:
780 if GlobalData.gBinCacheDest is not None:
781 EdkLogger.error("build", OPTION_VALUE_INVALID, ExtraData="Invalid value of option --binary-destination.")
782
783 GlobalData.gDatabasePath = os.path.normpath(os.path.join(GlobalData.gConfDirectory, GlobalData.gDatabasePath))
784 if not os.path.exists(os.path.join(GlobalData.gConfDirectory, '.cache')):
785 os.makedirs(os.path.join(GlobalData.gConfDirectory, '.cache'))
786 self.Db = BuildDB
787 self.BuildDatabase = self.Db.BuildObject
788 self.Platform = None
789 self.ToolChainFamily = None
790 self.LoadFixAddress = 0
791 self.UniFlag = BuildOptions.Flag
792 self.BuildModules = []
793 self.HashSkipModules = []
794 self.Db_Flag = False
795 self.LaunchPrebuildFlag = False
796 self.PlatformBuildPath = os.path.join(GlobalData.gConfDirectory, '.cache', '.PlatformBuild')
797 if BuildOptions.CommandLength:
798 GlobalData.gCommandMaxLength = BuildOptions.CommandLength
799
800 # print dot character during doing some time-consuming work
801 self.Progress = Utils.Progressor()
802 # print current build environment and configuration
803 EdkLogger.quiet("%-16s = %s" % ("WORKSPACE", os.environ["WORKSPACE"]))
804 if "PACKAGES_PATH" in os.environ:
805 # WORKSPACE env has been converted before. Print the same path style with WORKSPACE env.
806 EdkLogger.quiet("%-16s = %s" % ("PACKAGES_PATH", os.path.normcase(os.path.normpath(os.environ["PACKAGES_PATH"]))))
807 EdkLogger.quiet("%-16s = %s" % ("EDK_TOOLS_PATH", os.environ["EDK_TOOLS_PATH"]))
808 if "EDK_TOOLS_BIN" in os.environ:
809 # Print the same path style with WORKSPACE env.
810 EdkLogger.quiet("%-16s = %s" % ("EDK_TOOLS_BIN", os.path.normcase(os.path.normpath(os.environ["EDK_TOOLS_BIN"]))))
811 EdkLogger.quiet("%-16s = %s" % ("CONF_PATH", GlobalData.gConfDirectory))
812 if "PYTHON3_ENABLE" in os.environ:
813 PYTHON3_ENABLE = os.environ["PYTHON3_ENABLE"]
814 if PYTHON3_ENABLE != "TRUE":
815 PYTHON3_ENABLE = "FALSE"
816 EdkLogger.quiet("%-16s = %s" % ("PYTHON3_ENABLE", PYTHON3_ENABLE))
817 if "PYTHON_COMMAND" in os.environ:
818 EdkLogger.quiet("%-16s = %s" % ("PYTHON_COMMAND", os.environ["PYTHON_COMMAND"]))
819 self.InitPreBuild()
820 self.InitPostBuild()
821 if self.Prebuild:
822 EdkLogger.quiet("%-16s = %s" % ("PREBUILD", self.Prebuild))
823 if self.Postbuild:
824 EdkLogger.quiet("%-16s = %s" % ("POSTBUILD", self.Postbuild))
825 if self.Prebuild:
826 self.LaunchPrebuild()
827 TargetObj = TargetTxtDict()
828 ToolDefObj = ToolDefDict((os.path.join(os.getenv("WORKSPACE"), "Conf")))
829 self.TargetTxt = TargetObj.Target
830 self.ToolDef = ToolDefObj.ToolDef
831 if not (self.LaunchPrebuildFlag and os.path.exists(self.PlatformBuildPath)):
832 self.InitBuild()
833
834 self.AutoGenMgr = None
835 EdkLogger.info("")
836 os.chdir(self.WorkspaceDir)
837 self.log_q = log_q
838 GlobalData.file_lock = mp.Lock()
839 # Init cache data for local only
840 GlobalData.gPackageHashFile = dict()
841 GlobalData.gModulePreMakeCacheStatus = dict()
842 GlobalData.gModuleMakeCacheStatus = dict()
843 GlobalData.gHashChainStatus = dict()
844 GlobalData.gCMakeHashFile = dict()
845 GlobalData.gModuleHashFile = dict()
846 GlobalData.gFileHashDict = dict()
847 GlobalData.gModuleAllCacheStatus = set()
848 GlobalData.gModuleCacheHit = set()
849
850 def StartAutoGen(self,mqueue, DataPipe,SkipAutoGen,PcdMaList,cqueue):
851 try:
852 if SkipAutoGen:
853 return True,0
854 feedback_q = mp.Queue()
855 error_event = mp.Event()
856 FfsCmd = DataPipe.Get("FfsCommand")
857 if FfsCmd is None:
858 FfsCmd = {}
859 GlobalData.FfsCmd = FfsCmd
860 auto_workers = [AutoGenWorkerInProcess(mqueue,DataPipe.dump_file,feedback_q,GlobalData.file_lock,cqueue,self.log_q,error_event) for _ in range(self.ThreadNumber)]
861 self.AutoGenMgr = AutoGenManager(auto_workers,feedback_q,error_event)
862 self.AutoGenMgr.start()
863 for w in auto_workers:
864 w.start()
865 if PcdMaList is not None:
866 for PcdMa in PcdMaList:
867 # SourceFileList calling sequence impact the makefile string sequence.
868 # Create cached SourceFileList here to unify its calling sequence for both
869 # CanSkipbyPreMakeCache and CreateCodeFile/CreateMakeFile.
870 RetVal = PcdMa.SourceFileList
871 # Force cache miss for PCD driver
872 if GlobalData.gUseHashCache and not GlobalData.gBinCacheDest and self.Target in [None, "", "all"]:
873 cqueue.put((PcdMa.MetaFile.Path, PcdMa.Arch, "PreMakeCache", False))
874
875 PcdMa.CreateCodeFile(False)
876 PcdMa.CreateMakeFile(False,GenFfsList = DataPipe.Get("FfsCommand").get((PcdMa.MetaFile.Path, PcdMa.Arch),[]))
877 PcdMa.CreateAsBuiltInf()
878 # Force cache miss for PCD driver
879 if GlobalData.gBinCacheSource and self.Target in [None, "", "all"]:
880 cqueue.put((PcdMa.MetaFile.Path, PcdMa.Arch, "MakeCache", False))
881
882 self.AutoGenMgr.join()
883 rt = self.AutoGenMgr.Status
884 err = 0
885 if not rt:
886 err = UNKNOWN_ERROR
887 return rt, err
888 except FatalError as e:
889 return False, e.args[0]
890 except:
891 return False, UNKNOWN_ERROR
892
893 ## Add TOOLCHAIN and FAMILY declared in DSC [BuildOptions] to ToolsDefTxtDatabase.
894 #
895 # Loop through the set of build targets, tool chains, and archs provided on either
896 # the command line or in target.txt to discover FAMILY and TOOLCHAIN delclarations
897 # in [BuildOptions] sections that may be within !if expressions that may use
898 # $(TARGET), $(TOOLCHAIN), $(TOOLCHAIN_TAG), or $(ARCH) operands.
899 #
900 def GetToolChainAndFamilyFromDsc (self, File):
901 SavedGlobalDefines = GlobalData.gGlobalDefines.copy()
902 for BuildTarget in self.BuildTargetList:
903 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
904 for BuildToolChain in self.ToolChainList:
905 GlobalData.gGlobalDefines['TOOLCHAIN'] = BuildToolChain
906 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = BuildToolChain
907 for BuildArch in self.ArchList:
908 GlobalData.gGlobalDefines['ARCH'] = BuildArch
909 dscobj = self.BuildDatabase[File, BuildArch]
910 for KeyFamily, Key, KeyCodeBase in dscobj.BuildOptions:
911 try:
912 Target, ToolChain, Arch, Tool, Attr = Key.split('_')
913 except:
914 continue
915 if ToolChain == TAB_STAR or Attr != TAB_TOD_DEFINES_FAMILY:
916 continue
917 try:
918 Family = dscobj.BuildOptions[(KeyFamily, Key, KeyCodeBase)]
919 Family = Family.strip().strip('=').strip()
920 except:
921 continue
922 if TAB_TOD_DEFINES_FAMILY not in self.ToolDef.ToolsDefTxtDatabase:
923 self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_FAMILY] = {}
924 if ToolChain not in self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_FAMILY]:
925 self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_FAMILY][ToolChain] = Family
926 if TAB_TOD_DEFINES_BUILDRULEFAMILY not in self.ToolDef.ToolsDefTxtDatabase:
927 self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_BUILDRULEFAMILY] = {}
928 if ToolChain not in self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_BUILDRULEFAMILY]:
929 self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_BUILDRULEFAMILY][ToolChain] = Family
930 if TAB_TOD_DEFINES_TOOL_CHAIN_TAG not in self.ToolDef.ToolsDefTxtDatabase:
931 self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_TOOL_CHAIN_TAG] = []
932 if ToolChain not in self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_TOOL_CHAIN_TAG]:
933 self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_TOOL_CHAIN_TAG].append(ToolChain)
934 GlobalData.gGlobalDefines = SavedGlobalDefines
935
936 ## Load configuration
937 #
938 # This method will parse target.txt and get the build configurations.
939 #
940 def LoadConfiguration(self):
941
942 # if no ARCH given in command line, get it from target.txt
943 if not self.ArchList:
944 self.ArchList = self.TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TARGET_ARCH]
945 self.ArchList = tuple(self.ArchList)
946
947 # if no build target given in command line, get it from target.txt
948 if not self.BuildTargetList:
949 self.BuildTargetList = self.TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TARGET]
950
951 # if no tool chain given in command line, get it from target.txt
952 if not self.ToolChainList:
953 self.ToolChainList = self.TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_TOOL_CHAIN_TAG]
954 if self.ToolChainList is None or len(self.ToolChainList) == 0:
955 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.\n")
956
957 if not self.PlatformFile:
958 PlatformFile = self.TargetTxt.TargetTxtDictionary[TAB_TAT_DEFINES_ACTIVE_PLATFORM]
959 if not PlatformFile:
960 # Try to find one in current directory
961 WorkingDirectory = os.getcwd()
962 FileList = glob.glob(os.path.normpath(os.path.join(WorkingDirectory, '*.dsc')))
963 FileNum = len(FileList)
964 if FileNum >= 2:
965 EdkLogger.error("build", OPTION_MISSING,
966 ExtraData="There are %d DSC files in %s. Use '-p' to specify one.\n" % (FileNum, WorkingDirectory))
967 elif FileNum == 1:
968 PlatformFile = FileList[0]
969 else:
970 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
971 ExtraData="No active platform specified in target.txt or command line! Nothing can be built.\n")
972
973 self.PlatformFile = PathClass(NormFile(PlatformFile, self.WorkspaceDir), self.WorkspaceDir)
974
975 self.GetToolChainAndFamilyFromDsc (self.PlatformFile)
976
977 # check if the tool chains are defined or not
978 NewToolChainList = []
979 for ToolChain in self.ToolChainList:
980 if ToolChain not in self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_TOOL_CHAIN_TAG]:
981 EdkLogger.warn("build", "Tool chain [%s] is not defined" % ToolChain)
982 else:
983 NewToolChainList.append(ToolChain)
984 # if no tool chain available, break the build
985 if len(NewToolChainList) == 0:
986 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
987 ExtraData="[%s] not defined. No toolchain available for build!\n" % ", ".join(self.ToolChainList))
988 else:
989 self.ToolChainList = NewToolChainList
990
991 ToolChainFamily = []
992 ToolDefinition = self.ToolDef.ToolsDefTxtDatabase
993 for Tool in self.ToolChainList:
994 if TAB_TOD_DEFINES_FAMILY not in ToolDefinition or Tool not in ToolDefinition[TAB_TOD_DEFINES_FAMILY] \
995 or not ToolDefinition[TAB_TOD_DEFINES_FAMILY][Tool]:
996 EdkLogger.warn("build", "No tool chain family found in configuration for %s. Default to MSFT." % Tool)
997 ToolChainFamily.append(TAB_COMPILER_MSFT)
998 else:
999 ToolChainFamily.append(ToolDefinition[TAB_TOD_DEFINES_FAMILY][Tool])
1000 self.ToolChainFamily = ToolChainFamily
1001
1002 self.ThreadNumber = ThreadNum()
1003 ## Initialize build configuration
1004 #
1005 # This method will parse DSC file and merge the configurations from
1006 # command line and target.txt, then get the final build configurations.
1007 #
1008 def InitBuild(self):
1009 # parse target.txt, tools_def.txt, and platform file
1010 self.LoadConfiguration()
1011
1012 # Allow case-insensitive for those from command line or configuration file
1013 ErrorCode, ErrorInfo = self.PlatformFile.Validate(".dsc", False)
1014 if ErrorCode != 0:
1015 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
1016
1017
1018 def InitPreBuild(self):
1019 self.LoadConfiguration()
1020 ErrorCode, ErrorInfo = self.PlatformFile.Validate(".dsc", False)
1021 if ErrorCode != 0:
1022 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
1023 if self.BuildTargetList:
1024 GlobalData.gGlobalDefines['TARGET'] = self.BuildTargetList[0]
1025 if self.ArchList:
1026 GlobalData.gGlobalDefines['ARCH'] = self.ArchList[0]
1027 if self.ToolChainList:
1028 GlobalData.gGlobalDefines['TOOLCHAIN'] = self.ToolChainList[0]
1029 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = self.ToolChainList[0]
1030 if self.ToolChainFamily:
1031 GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[0]
1032 if 'PREBUILD' in GlobalData.gCommandLineDefines:
1033 self.Prebuild = GlobalData.gCommandLineDefines.get('PREBUILD')
1034 else:
1035 self.Db_Flag = True
1036 Platform = self.Db.MapPlatform(str(self.PlatformFile))
1037 self.Prebuild = str(Platform.Prebuild)
1038 if self.Prebuild:
1039 PrebuildList = []
1040 #
1041 # Evaluate all arguments and convert arguments that are WORKSPACE
1042 # relative paths to absolute paths. Filter arguments that look like
1043 # flags or do not follow the file/dir naming rules to avoid false
1044 # positives on this conversion.
1045 #
1046 for Arg in self.Prebuild.split():
1047 #
1048 # Do not modify Arg if it looks like a flag or an absolute file path
1049 #
1050 if Arg.startswith('-') or os.path.isabs(Arg):
1051 PrebuildList.append(Arg)
1052 continue
1053 #
1054 # Do not modify Arg if it does not look like a Workspace relative
1055 # path that starts with a valid package directory name
1056 #
1057 if not Arg[0].isalpha() or os.path.dirname(Arg) == '':
1058 PrebuildList.append(Arg)
1059 continue
1060 #
1061 # If Arg looks like a WORKSPACE relative path, then convert to an
1062 # absolute path and check to see if the file exists.
1063 #
1064 Temp = mws.join(self.WorkspaceDir, Arg)
1065 if os.path.isfile(Temp):
1066 Arg = Temp
1067 PrebuildList.append(Arg)
1068 self.Prebuild = ' '.join(PrebuildList)
1069 self.Prebuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList, self.PlatformFile, self.Target)
1070
1071 def InitPostBuild(self):
1072 if 'POSTBUILD' in GlobalData.gCommandLineDefines:
1073 self.Postbuild = GlobalData.gCommandLineDefines.get('POSTBUILD')
1074 else:
1075 Platform = self.Db.MapPlatform(str(self.PlatformFile))
1076 self.Postbuild = str(Platform.Postbuild)
1077 if self.Postbuild:
1078 PostbuildList = []
1079 #
1080 # Evaluate all arguments and convert arguments that are WORKSPACE
1081 # relative paths to absolute paths. Filter arguments that look like
1082 # flags or do not follow the file/dir naming rules to avoid false
1083 # positives on this conversion.
1084 #
1085 for Arg in self.Postbuild.split():
1086 #
1087 # Do not modify Arg if it looks like a flag or an absolute file path
1088 #
1089 if Arg.startswith('-') or os.path.isabs(Arg):
1090 PostbuildList.append(Arg)
1091 continue
1092 #
1093 # Do not modify Arg if it does not look like a Workspace relative
1094 # path that starts with a valid package directory name
1095 #
1096 if not Arg[0].isalpha() or os.path.dirname(Arg) == '':
1097 PostbuildList.append(Arg)
1098 continue
1099 #
1100 # If Arg looks like a WORKSPACE relative path, then convert to an
1101 # absolute path and check to see if the file exists.
1102 #
1103 Temp = mws.join(self.WorkspaceDir, Arg)
1104 if os.path.isfile(Temp):
1105 Arg = Temp
1106 PostbuildList.append(Arg)
1107 self.Postbuild = ' '.join(PostbuildList)
1108 self.Postbuild += self.PassCommandOption(self.BuildTargetList, self.ArchList, self.ToolChainList, self.PlatformFile, self.Target)
1109
1110 def PassCommandOption(self, BuildTarget, TargetArch, ToolChain, PlatformFile, Target):
1111 BuildStr = ''
1112 if GlobalData.gCommand and isinstance(GlobalData.gCommand, list):
1113 BuildStr += ' ' + ' '.join(GlobalData.gCommand)
1114 TargetFlag = False
1115 ArchFlag = False
1116 ToolChainFlag = False
1117 PlatformFileFlag = False
1118
1119 if GlobalData.gOptions and not GlobalData.gOptions.BuildTarget:
1120 TargetFlag = True
1121 if GlobalData.gOptions and not GlobalData.gOptions.TargetArch:
1122 ArchFlag = True
1123 if GlobalData.gOptions and not GlobalData.gOptions.ToolChain:
1124 ToolChainFlag = True
1125 if GlobalData.gOptions and not GlobalData.gOptions.PlatformFile:
1126 PlatformFileFlag = True
1127
1128 if TargetFlag and BuildTarget:
1129 if isinstance(BuildTarget, list) or isinstance(BuildTarget, tuple):
1130 BuildStr += ' -b ' + ' -b '.join(BuildTarget)
1131 elif isinstance(BuildTarget, str):
1132 BuildStr += ' -b ' + BuildTarget
1133 if ArchFlag and TargetArch:
1134 if isinstance(TargetArch, list) or isinstance(TargetArch, tuple):
1135 BuildStr += ' -a ' + ' -a '.join(TargetArch)
1136 elif isinstance(TargetArch, str):
1137 BuildStr += ' -a ' + TargetArch
1138 if ToolChainFlag and ToolChain:
1139 if isinstance(ToolChain, list) or isinstance(ToolChain, tuple):
1140 BuildStr += ' -t ' + ' -t '.join(ToolChain)
1141 elif isinstance(ToolChain, str):
1142 BuildStr += ' -t ' + ToolChain
1143 if PlatformFileFlag and PlatformFile:
1144 if isinstance(PlatformFile, list) or isinstance(PlatformFile, tuple):
1145 BuildStr += ' -p ' + ' -p '.join(PlatformFile)
1146 elif isinstance(PlatformFile, str):
1147 BuildStr += ' -p' + PlatformFile
1148 BuildStr += ' --conf=' + GlobalData.gConfDirectory
1149 if Target:
1150 BuildStr += ' ' + Target
1151
1152 return BuildStr
1153
1154 def LaunchPrebuild(self):
1155 if self.Prebuild:
1156 EdkLogger.info("\n- Prebuild Start -\n")
1157 self.LaunchPrebuildFlag = True
1158 #
1159 # The purpose of .PrebuildEnv file is capture environment variable settings set by the prebuild script
1160 # and preserve them for the rest of the main build step, because the child process environment will
1161 # evaporate as soon as it exits, we cannot get it in build step.
1162 #
1163 PrebuildEnvFile = os.path.join(GlobalData.gConfDirectory, '.cache', '.PrebuildEnv')
1164 if os.path.isfile(PrebuildEnvFile):
1165 os.remove(PrebuildEnvFile)
1166 if os.path.isfile(self.PlatformBuildPath):
1167 os.remove(self.PlatformBuildPath)
1168 if sys.platform == "win32":
1169 args = ' && '.join((self.Prebuild, 'set > ' + PrebuildEnvFile))
1170 Process = Popen(args, stdout=PIPE, stderr=PIPE, shell=True)
1171 else:
1172 args = ' && '.join((self.Prebuild, 'env > ' + PrebuildEnvFile))
1173 Process = Popen(args, stdout=PIPE, stderr=PIPE, shell=True)
1174
1175 # launch two threads to read the STDOUT and STDERR
1176 EndOfProcedure = Event()
1177 EndOfProcedure.clear()
1178 if Process.stdout:
1179 StdOutThread = Thread(target=ReadMessage, args=(Process.stdout, EdkLogger.info, EndOfProcedure))
1180 StdOutThread.name = "STDOUT-Redirector"
1181 StdOutThread.daemon = False
1182 StdOutThread.start()
1183
1184 if Process.stderr:
1185 StdErrThread = Thread(target=ReadMessage, args=(Process.stderr, EdkLogger.quiet, EndOfProcedure))
1186 StdErrThread.name = "STDERR-Redirector"
1187 StdErrThread.daemon = False
1188 StdErrThread.start()
1189 # waiting for program exit
1190 Process.wait()
1191
1192 if Process.stdout:
1193 StdOutThread.join()
1194 if Process.stderr:
1195 StdErrThread.join()
1196 if Process.returncode != 0 :
1197 EdkLogger.error("Prebuild", PREBUILD_ERROR, 'Prebuild process is not success!')
1198
1199 if os.path.exists(PrebuildEnvFile):
1200 f = open(PrebuildEnvFile)
1201 envs = f.readlines()
1202 f.close()
1203 envs = [l.split("=", 1) for l in envs ]
1204 envs = [[I.strip() for I in item] for item in envs if len(item) == 2]
1205 os.environ.update(dict(envs))
1206 EdkLogger.info("\n- Prebuild Done -\n")
1207
1208 def LaunchPostbuild(self):
1209 if self.Postbuild:
1210 EdkLogger.info("\n- Postbuild Start -\n")
1211 if sys.platform == "win32":
1212 Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE, shell=True)
1213 else:
1214 Process = Popen(self.Postbuild, stdout=PIPE, stderr=PIPE, shell=True)
1215 # launch two threads to read the STDOUT and STDERR
1216 EndOfProcedure = Event()
1217 EndOfProcedure.clear()
1218 if Process.stdout:
1219 StdOutThread = Thread(target=ReadMessage, args=(Process.stdout, EdkLogger.info, EndOfProcedure))
1220 StdOutThread.name = "STDOUT-Redirector"
1221 StdOutThread.daemon = False
1222 StdOutThread.start()
1223
1224 if Process.stderr:
1225 StdErrThread = Thread(target=ReadMessage, args=(Process.stderr, EdkLogger.quiet, EndOfProcedure))
1226 StdErrThread.name = "STDERR-Redirector"
1227 StdErrThread.daemon = False
1228 StdErrThread.start()
1229 # waiting for program exit
1230 Process.wait()
1231
1232 if Process.stdout:
1233 StdOutThread.join()
1234 if Process.stderr:
1235 StdErrThread.join()
1236 if Process.returncode != 0 :
1237 EdkLogger.error("Postbuild", POSTBUILD_ERROR, 'Postbuild process is not success!')
1238 EdkLogger.info("\n- Postbuild Done -\n")
1239
1240 ## Build a module or platform
1241 #
1242 # Create autogen code and makefile for a module or platform, and the launch
1243 # "make" command to build it
1244 #
1245 # @param Target The target of build command
1246 # @param Platform The platform file
1247 # @param Module The module file
1248 # @param BuildTarget The name of build target, one of "DEBUG", "RELEASE"
1249 # @param ToolChain The name of toolchain to build
1250 # @param Arch The arch of the module/platform
1251 # @param CreateDepModuleCodeFile Flag used to indicate creating code
1252 # for dependent modules/Libraries
1253 # @param CreateDepModuleMakeFile Flag used to indicate creating makefile
1254 # for dependent modules/Libraries
1255 #
1256 def _BuildPa(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False, FfsCommand=None, PcdMaList=None):
1257 if AutoGenObject is None:
1258 return False
1259 if FfsCommand is None:
1260 FfsCommand = {}
1261 # skip file generation for cleanxxx targets, run and fds target
1262 if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:
1263 # for target which must generate AutoGen code and makefile
1264 mqueue = mp.Queue()
1265 for m in AutoGenObject.GetAllModuleInfo:
1266 mqueue.put(m)
1267 mqueue.put((None,None,None,None,None,None,None))
1268 AutoGenObject.DataPipe.DataContainer = {"CommandTarget": self.Target}
1269 AutoGenObject.DataPipe.DataContainer = {"Workspace_timestamp": AutoGenObject.Workspace._SrcTimeStamp}
1270 AutoGenObject.CreateLibModuelDirs()
1271 AutoGenObject.DataPipe.DataContainer = {"LibraryBuildDirectoryList":AutoGenObject.LibraryBuildDirectoryList}
1272 AutoGenObject.DataPipe.DataContainer = {"ModuleBuildDirectoryList":AutoGenObject.ModuleBuildDirectoryList}
1273 AutoGenObject.DataPipe.DataContainer = {"FdsCommandDict": AutoGenObject.Workspace.GenFdsCommandDict}
1274 self.Progress.Start("Generating makefile and code")
1275 data_pipe_file = os.path.join(AutoGenObject.BuildDir, "GlobalVar_%s_%s.bin" % (str(AutoGenObject.Guid),AutoGenObject.Arch))
1276 AutoGenObject.DataPipe.dump(data_pipe_file)
1277 cqueue = mp.Queue()
1278 autogen_rt,errorcode = self.StartAutoGen(mqueue, AutoGenObject.DataPipe, self.SkipAutoGen, PcdMaList, cqueue)
1279 AutoGenIdFile = os.path.join(GlobalData.gConfDirectory,".AutoGenIdFile.txt")
1280 with open(AutoGenIdFile,"w") as fw:
1281 fw.write("Arch=%s\n" % "|".join((AutoGenObject.Workspace.ArchList)))
1282 fw.write("BuildDir=%s\n" % AutoGenObject.Workspace.BuildDir)
1283 fw.write("PlatformGuid=%s\n" % str(AutoGenObject.Guid))
1284 self.Progress.Stop("done!")
1285 if not autogen_rt:
1286 self.AutoGenMgr.TerminateWorkers()
1287 self.AutoGenMgr.join(1)
1288 raise FatalError(errorcode)
1289 AutoGenObject.CreateCodeFile(False)
1290 AutoGenObject.CreateMakeFile(False)
1291 else:
1292 # always recreate top/platform makefile when clean, just in case of inconsistency
1293 AutoGenObject.CreateCodeFile(True)
1294 AutoGenObject.CreateMakeFile(True)
1295
1296 if EdkLogger.GetLevel() == EdkLogger.QUIET:
1297 EdkLogger.quiet("Building ... %s" % repr(AutoGenObject))
1298
1299 BuildCommand = AutoGenObject.BuildCommand
1300 if BuildCommand is None or len(BuildCommand) == 0:
1301 EdkLogger.error("build", OPTION_MISSING,
1302 "No build command found for this module. "
1303 "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %
1304 (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch),
1305 ExtraData=str(AutoGenObject))
1306
1307 # run
1308 if Target == 'run':
1309 return True
1310
1311 # Fetch the MakeFileName.
1312 self.MakeFileName = AutoGenObject.MakeFileName
1313
1314 # build modules
1315 if BuildModule:
1316 BuildCommand = BuildCommand + [Target]
1317 LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)
1318 if GlobalData.gBinCacheDest:
1319 self.GenDestCache()
1320 elif GlobalData.gUseHashCache and not GlobalData.gBinCacheSource:
1321 # Only for --hash
1322 # Update PreMakeCacheChain files
1323 self.GenLocalPreMakeCache()
1324 self.BuildModules = []
1325 return True
1326
1327 # build library
1328 if Target == 'libraries':
1329 DirList = []
1330 for Lib in AutoGenObject.LibraryAutoGenList:
1331 if not Lib.IsBinaryModule:
1332 DirList.append((os.path.join(AutoGenObject.BuildDir, Lib.BuildDir),Lib))
1333 for Lib, LibAutoGen in DirList:
1334 NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, self.MakeFileName)), 'pbuild']
1335 LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir,LibAutoGen)
1336 return True
1337
1338 # build module
1339 if Target == 'modules':
1340 DirList = []
1341 for Lib in AutoGenObject.LibraryAutoGenList:
1342 if not Lib.IsBinaryModule:
1343 DirList.append((os.path.join(AutoGenObject.BuildDir, Lib.BuildDir),Lib))
1344 for Lib, LibAutoGen in DirList:
1345 NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, self.MakeFileName)), 'pbuild']
1346 LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir,LibAutoGen)
1347
1348 DirList = []
1349 for ModuleAutoGen in AutoGenObject.ModuleAutoGenList:
1350 if not ModuleAutoGen.IsBinaryModule:
1351 DirList.append((os.path.join(AutoGenObject.BuildDir, ModuleAutoGen.BuildDir),ModuleAutoGen))
1352 for Mod,ModAutoGen in DirList:
1353 NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Mod, self.MakeFileName)), 'pbuild']
1354 LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir,ModAutoGen)
1355 self.CreateAsBuiltInf()
1356 if GlobalData.gBinCacheDest:
1357 self.GenDestCache()
1358 elif GlobalData.gUseHashCache and not GlobalData.gBinCacheSource:
1359 # Only for --hash
1360 # Update PreMakeCacheChain files
1361 self.GenLocalPreMakeCache()
1362 self.BuildModules = []
1363 return True
1364
1365 # cleanlib
1366 if Target == 'cleanlib':
1367 for Lib in AutoGenObject.LibraryBuildDirectoryList:
1368 LibMakefile = os.path.normpath(os.path.join(Lib, self.MakeFileName))
1369 if os.path.exists(LibMakefile):
1370 NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall']
1371 LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
1372 return True
1373
1374 # clean
1375 if Target == 'clean':
1376 for Mod in AutoGenObject.ModuleBuildDirectoryList:
1377 ModMakefile = os.path.normpath(os.path.join(Mod, self.MakeFileName))
1378 if os.path.exists(ModMakefile):
1379 NewBuildCommand = BuildCommand + ['-f', ModMakefile, 'cleanall']
1380 LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
1381 for Lib in AutoGenObject.LibraryBuildDirectoryList:
1382 LibMakefile = os.path.normpath(os.path.join(Lib, self.MakeFileName))
1383 if os.path.exists(LibMakefile):
1384 NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall']
1385 LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
1386 return True
1387
1388 # cleanall
1389 if Target == 'cleanall':
1390 try:
1391 #os.rmdir(AutoGenObject.BuildDir)
1392 RemoveDirectory(AutoGenObject.BuildDir, True)
1393 except WindowsError as X:
1394 EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X))
1395 return True
1396
1397 ## Build a module or platform
1398 #
1399 # Create autogen code and makefile for a module or platform, and the launch
1400 # "make" command to build it
1401 #
1402 # @param Target The target of build command
1403 # @param Platform The platform file
1404 # @param Module The module file
1405 # @param BuildTarget The name of build target, one of "DEBUG", "RELEASE"
1406 # @param ToolChain The name of toolchain to build
1407 # @param Arch The arch of the module/platform
1408 # @param CreateDepModuleCodeFile Flag used to indicate creating code
1409 # for dependent modules/Libraries
1410 # @param CreateDepModuleMakeFile Flag used to indicate creating makefile
1411 # for dependent modules/Libraries
1412 #
1413 def _Build(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False):
1414 if AutoGenObject is None:
1415 return False
1416
1417 # skip file generation for cleanxxx targets, run and fds target
1418 if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:
1419 # for target which must generate AutoGen code and makefile
1420 if not self.SkipAutoGen or Target == 'genc':
1421 self.Progress.Start("Generating code")
1422 AutoGenObject.CreateCodeFile(CreateDepsCodeFile)
1423 self.Progress.Stop("done!")
1424 if Target == "genc":
1425 return True
1426
1427 if not self.SkipAutoGen or Target == 'genmake':
1428 self.Progress.Start("Generating makefile")
1429 AutoGenObject.CreateMakeFile(CreateDepsMakeFile)
1430 #AutoGenObject.CreateAsBuiltInf()
1431 self.Progress.Stop("done!")
1432 if Target == "genmake":
1433 return True
1434 else:
1435 # always recreate top/platform makefile when clean, just in case of inconsistency
1436 AutoGenObject.CreateCodeFile(True)
1437 AutoGenObject.CreateMakeFile(True)
1438
1439 if EdkLogger.GetLevel() == EdkLogger.QUIET:
1440 EdkLogger.quiet("Building ... %s" % repr(AutoGenObject))
1441
1442 BuildCommand = AutoGenObject.BuildCommand
1443 if BuildCommand is None or len(BuildCommand) == 0:
1444 EdkLogger.error("build", OPTION_MISSING,
1445 "No build command found for this module. "
1446 "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %
1447 (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch),
1448 ExtraData=str(AutoGenObject))
1449
1450 # build modules
1451 if BuildModule:
1452 if Target != 'fds':
1453 BuildCommand = BuildCommand + [Target]
1454 AutoGenObject.BuildTime = LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)
1455 self.CreateAsBuiltInf()
1456 if GlobalData.gBinCacheDest:
1457 self.GenDestCache()
1458 elif GlobalData.gUseHashCache and not GlobalData.gBinCacheSource:
1459 # Only for --hash
1460 # Update PreMakeCacheChain files
1461 self.GenLocalPreMakeCache()
1462 self.BuildModules = []
1463 return True
1464
1465 # genfds
1466 if Target == 'fds':
1467 if GenFdsApi(AutoGenObject.GenFdsCommandDict, self.Db):
1468 EdkLogger.error("build", COMMAND_FAILURE)
1469 Threshold = self.GetFreeSizeThreshold()
1470 if Threshold:
1471 self.CheckFreeSizeThreshold(Threshold, AutoGenObject.FvDir)
1472 return True
1473
1474 # run
1475 if Target == 'run':
1476 return True
1477
1478 # build library
1479 if Target == 'libraries':
1480 pass
1481
1482 # not build modules
1483
1484
1485 # cleanall
1486 if Target == 'cleanall':
1487 try:
1488 #os.rmdir(AutoGenObject.BuildDir)
1489 RemoveDirectory(AutoGenObject.BuildDir, True)
1490 except WindowsError as X:
1491 EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X))
1492 return True
1493
1494 ## Rebase module image and Get function address for the input module list.
1495 #
1496 def _RebaseModule (self, MapBuffer, BaseAddress, ModuleList, AddrIsOffset = True, ModeIsSmm = False):
1497 if ModeIsSmm:
1498 AddrIsOffset = False
1499 for InfFile in ModuleList:
1500 sys.stdout.write (".")
1501 sys.stdout.flush()
1502 ModuleInfo = ModuleList[InfFile]
1503 ModuleName = ModuleInfo.BaseName
1504 ModuleOutputImage = ModuleInfo.Image.FileName
1505 ModuleDebugImage = os.path.join(ModuleInfo.DebugDir, ModuleInfo.BaseName + '.efi')
1506 ## for SMM module in SMRAM, the SMRAM will be allocated from base to top.
1507 if not ModeIsSmm:
1508 BaseAddress = BaseAddress - ModuleInfo.Image.Size
1509 #
1510 # Update Image to new BaseAddress by GenFw tool
1511 #
1512 LaunchCommand(["GenFw", "--rebase", str(BaseAddress), "-r", ModuleOutputImage], ModuleInfo.OutputDir)
1513 LaunchCommand(["GenFw", "--rebase", str(BaseAddress), "-r", ModuleDebugImage], ModuleInfo.DebugDir)
1514 else:
1515 #
1516 # Set new address to the section header only for SMM driver.
1517 #
1518 LaunchCommand(["GenFw", "--address", str(BaseAddress), "-r", ModuleOutputImage], ModuleInfo.OutputDir)
1519 LaunchCommand(["GenFw", "--address", str(BaseAddress), "-r", ModuleDebugImage], ModuleInfo.DebugDir)
1520 #
1521 # Collect function address from Map file
1522 #
1523 ImageMapTable = ModuleOutputImage.replace('.efi', '.map')
1524 FunctionList = []
1525 if os.path.exists(ImageMapTable):
1526 OrigImageBaseAddress = 0
1527 ImageMap = open(ImageMapTable, 'r')
1528 for LinStr in ImageMap:
1529 if len (LinStr.strip()) == 0:
1530 continue
1531 #
1532 # Get the preferred address set on link time.
1533 #
1534 if LinStr.find ('Preferred load address is') != -1:
1535 StrList = LinStr.split()
1536 OrigImageBaseAddress = int (StrList[len(StrList) - 1], 16)
1537
1538 StrList = LinStr.split()
1539 if len (StrList) > 4:
1540 if StrList[3] == 'f' or StrList[3] == 'F':
1541 Name = StrList[1]
1542 RelativeAddress = int (StrList[2], 16) - OrigImageBaseAddress
1543 FunctionList.append ((Name, RelativeAddress))
1544
1545 ImageMap.close()
1546 #
1547 # Add general information.
1548 #
1549 if ModeIsSmm:
1550 MapBuffer.append('\n\n%s (Fixed SMRAM Offset, BaseAddress=0x%010X, EntryPoint=0x%010X)\n' % (ModuleName, BaseAddress, BaseAddress + ModuleInfo.Image.EntryPoint))
1551 elif AddrIsOffset:
1552 MapBuffer.append('\n\n%s (Fixed Memory Offset, BaseAddress=-0x%010X, EntryPoint=-0x%010X)\n' % (ModuleName, 0 - BaseAddress, 0 - (BaseAddress + ModuleInfo.Image.EntryPoint)))
1553 else:
1554 MapBuffer.append('\n\n%s (Fixed Memory Address, BaseAddress=0x%010X, EntryPoint=0x%010X)\n' % (ModuleName, BaseAddress, BaseAddress + ModuleInfo.Image.EntryPoint))
1555 #
1556 # Add guid and general seciton section.
1557 #
1558 TextSectionAddress = 0
1559 DataSectionAddress = 0
1560 for SectionHeader in ModuleInfo.Image.SectionHeaderList:
1561 if SectionHeader[0] == '.text':
1562 TextSectionAddress = SectionHeader[1]
1563 elif SectionHeader[0] in ['.data', '.sdata']:
1564 DataSectionAddress = SectionHeader[1]
1565 if AddrIsOffset:
1566 MapBuffer.append('(GUID=%s, .textbaseaddress=-0x%010X, .databaseaddress=-0x%010X)\n' % (ModuleInfo.Guid, 0 - (BaseAddress + TextSectionAddress), 0 - (BaseAddress + DataSectionAddress)))
1567 else:
1568 MapBuffer.append('(GUID=%s, .textbaseaddress=0x%010X, .databaseaddress=0x%010X)\n' % (ModuleInfo.Guid, BaseAddress + TextSectionAddress, BaseAddress + DataSectionAddress))
1569 #
1570 # Add debug image full path.
1571 #
1572 MapBuffer.append('(IMAGE=%s)\n\n' % (ModuleDebugImage))
1573 #
1574 # Add function address
1575 #
1576 for Function in FunctionList:
1577 if AddrIsOffset:
1578 MapBuffer.append(' -0x%010X %s\n' % (0 - (BaseAddress + Function[1]), Function[0]))
1579 else:
1580 MapBuffer.append(' 0x%010X %s\n' % (BaseAddress + Function[1], Function[0]))
1581 ImageMap.close()
1582
1583 #
1584 # for SMM module in SMRAM, the SMRAM will be allocated from base to top.
1585 #
1586 if ModeIsSmm:
1587 BaseAddress = BaseAddress + ModuleInfo.Image.Size
1588
1589 ## Collect MAP information of all FVs
1590 #
1591 def _CollectFvMapBuffer (self, MapBuffer, Wa, ModuleList):
1592 if self.Fdf:
1593 # First get the XIP base address for FV map file.
1594 GuidPattern = re.compile("[-a-fA-F0-9]+")
1595 GuidName = re.compile(r"\(GUID=[-a-fA-F0-9]+")
1596 for FvName in Wa.FdfProfile.FvDict:
1597 FvMapBuffer = os.path.join(Wa.FvDir, FvName + '.Fv.map')
1598 if not os.path.exists(FvMapBuffer):
1599 continue
1600 FvMap = open(FvMapBuffer, 'r')
1601 #skip FV size information
1602 FvMap.readline()
1603 FvMap.readline()
1604 FvMap.readline()
1605 FvMap.readline()
1606 for Line in FvMap:
1607 MatchGuid = GuidPattern.match(Line)
1608 if MatchGuid is not None:
1609 #
1610 # Replace GUID with module name
1611 #
1612 GuidString = MatchGuid.group()
1613 if GuidString.upper() in ModuleList:
1614 Line = Line.replace(GuidString, ModuleList[GuidString.upper()].Name)
1615 MapBuffer.append(Line)
1616 #
1617 # Add the debug image full path.
1618 #
1619 MatchGuid = GuidName.match(Line)
1620 if MatchGuid is not None:
1621 GuidString = MatchGuid.group().split("=")[1]
1622 if GuidString.upper() in ModuleList:
1623 MapBuffer.append('(IMAGE=%s)\n' % (os.path.join(ModuleList[GuidString.upper()].DebugDir, ModuleList[GuidString.upper()].Name + '.efi')))
1624
1625 FvMap.close()
1626
1627 ## Collect MAP information of all modules
1628 #
1629 def _CollectModuleMapBuffer (self, MapBuffer, ModuleList):
1630 sys.stdout.write ("Generate Load Module At Fix Address Map")
1631 sys.stdout.flush()
1632 PatchEfiImageList = []
1633 PeiModuleList = {}
1634 BtModuleList = {}
1635 RtModuleList = {}
1636 SmmModuleList = {}
1637 PeiSize = 0
1638 BtSize = 0
1639 RtSize = 0
1640 # reserve 4K size in SMRAM to make SMM module address not from 0.
1641 SmmSize = 0x1000
1642 for ModuleGuid in ModuleList:
1643 Module = ModuleList[ModuleGuid]
1644 GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (Module.MetaFile, Module.Arch, Module.ToolChain, Module.BuildTarget)
1645
1646 OutputImageFile = ''
1647 for ResultFile in Module.CodaTargetList:
1648 if str(ResultFile.Target).endswith('.efi'):
1649 #
1650 # module list for PEI, DXE, RUNTIME and SMM
1651 #
1652 OutputImageFile = os.path.join(Module.OutputDir, Module.Name + '.efi')
1653 ImageClass = PeImageClass (OutputImageFile)
1654 if not ImageClass.IsValid:
1655 EdkLogger.error("build", FILE_PARSE_FAILURE, ExtraData=ImageClass.ErrorInfo)
1656 ImageInfo = PeImageInfo(Module.Name, Module.Guid, Module.Arch, Module.OutputDir, Module.DebugDir, ImageClass)
1657 if Module.ModuleType in [SUP_MODULE_PEI_CORE, SUP_MODULE_PEIM, EDK_COMPONENT_TYPE_COMBINED_PEIM_DRIVER, EDK_COMPONENT_TYPE_PIC_PEIM, EDK_COMPONENT_TYPE_RELOCATABLE_PEIM, SUP_MODULE_DXE_CORE]:
1658 PeiModuleList[Module.MetaFile] = ImageInfo
1659 PeiSize += ImageInfo.Image.Size
1660 elif Module.ModuleType in [EDK_COMPONENT_TYPE_BS_DRIVER, SUP_MODULE_DXE_DRIVER, SUP_MODULE_UEFI_DRIVER]:
1661 BtModuleList[Module.MetaFile] = ImageInfo
1662 BtSize += ImageInfo.Image.Size
1663 elif Module.ModuleType in [SUP_MODULE_DXE_RUNTIME_DRIVER, EDK_COMPONENT_TYPE_RT_DRIVER, SUP_MODULE_DXE_SAL_DRIVER, EDK_COMPONENT_TYPE_SAL_RT_DRIVER]:
1664 RtModuleList[Module.MetaFile] = ImageInfo
1665 RtSize += ImageInfo.Image.Size
1666 elif Module.ModuleType in [SUP_MODULE_SMM_CORE, SUP_MODULE_DXE_SMM_DRIVER, SUP_MODULE_MM_STANDALONE, SUP_MODULE_MM_CORE_STANDALONE]:
1667 SmmModuleList[Module.MetaFile] = ImageInfo
1668 SmmSize += ImageInfo.Image.Size
1669 if Module.ModuleType == SUP_MODULE_DXE_SMM_DRIVER:
1670 PiSpecVersion = Module.Module.Specification.get('PI_SPECIFICATION_VERSION', '0x00000000')
1671 # for PI specification < PI1.1, DXE_SMM_DRIVER also runs as BOOT time driver.
1672 if int(PiSpecVersion, 16) < 0x0001000A:
1673 BtModuleList[Module.MetaFile] = ImageInfo
1674 BtSize += ImageInfo.Image.Size
1675 break
1676 #
1677 # EFI image is final target.
1678 # Check EFI image contains patchable FixAddress related PCDs.
1679 #
1680 if OutputImageFile != '':
1681 ModuleIsPatch = False
1682 for Pcd in Module.ModulePcdList:
1683 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE and Pcd.TokenCName in TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SET:
1684 ModuleIsPatch = True
1685 break
1686 if not ModuleIsPatch:
1687 for Pcd in Module.LibraryPcdList:
1688 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE and Pcd.TokenCName in TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SET:
1689 ModuleIsPatch = True
1690 break
1691
1692 if not ModuleIsPatch:
1693 continue
1694 #
1695 # Module includes the patchable load fix address PCDs.
1696 # It will be fixed up later.
1697 #
1698 PatchEfiImageList.append (OutputImageFile)
1699
1700 #
1701 # Get Top Memory address
1702 #
1703 ReservedRuntimeMemorySize = 0
1704 TopMemoryAddress = 0
1705 if self.LoadFixAddress == 0xFFFFFFFFFFFFFFFF:
1706 TopMemoryAddress = 0
1707 else:
1708 TopMemoryAddress = self.LoadFixAddress
1709 if TopMemoryAddress < RtSize + BtSize + PeiSize:
1710 EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS is too low to load driver")
1711
1712 #
1713 # Patch FixAddress related PCDs into EFI image
1714 #
1715 for EfiImage in PatchEfiImageList:
1716 EfiImageMap = EfiImage.replace('.efi', '.map')
1717 if not os.path.exists(EfiImageMap):
1718 continue
1719 #
1720 # Get PCD offset in EFI image by GenPatchPcdTable function
1721 #
1722 PcdTable = parsePcdInfoFromMapFile(EfiImageMap, EfiImage)
1723 #
1724 # Patch real PCD value by PatchPcdValue tool
1725 #
1726 for PcdInfo in PcdTable:
1727 ReturnValue = 0
1728 if PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE:
1729 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE_DATA_TYPE, str (PeiSize // 0x1000))
1730 elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE:
1731 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE_DATA_TYPE, str (BtSize // 0x1000))
1732 elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE:
1733 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE_DATA_TYPE, str (RtSize // 0x1000))
1734 elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE and len (SmmModuleList) > 0:
1735 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE_DATA_TYPE, str (SmmSize // 0x1000))
1736 if ReturnValue != 0:
1737 EdkLogger.error("build", PARAMETER_INVALID, "Patch PCD value failed", ExtraData=ErrorInfo)
1738
1739 MapBuffer.append('PEI_CODE_PAGE_NUMBER = 0x%x\n' % (PeiSize // 0x1000))
1740 MapBuffer.append('BOOT_CODE_PAGE_NUMBER = 0x%x\n' % (BtSize // 0x1000))
1741 MapBuffer.append('RUNTIME_CODE_PAGE_NUMBER = 0x%x\n' % (RtSize // 0x1000))
1742 if len (SmmModuleList) > 0:
1743 MapBuffer.append('SMM_CODE_PAGE_NUMBER = 0x%x\n' % (SmmSize // 0x1000))
1744
1745 PeiBaseAddr = TopMemoryAddress - RtSize - BtSize
1746 BtBaseAddr = TopMemoryAddress - RtSize
1747 RtBaseAddr = TopMemoryAddress - ReservedRuntimeMemorySize
1748
1749 self._RebaseModule (MapBuffer, PeiBaseAddr, PeiModuleList, TopMemoryAddress == 0)
1750 self._RebaseModule (MapBuffer, BtBaseAddr, BtModuleList, TopMemoryAddress == 0)
1751 self._RebaseModule (MapBuffer, RtBaseAddr, RtModuleList, TopMemoryAddress == 0)
1752 self._RebaseModule (MapBuffer, 0x1000, SmmModuleList, AddrIsOffset=False, ModeIsSmm=True)
1753 MapBuffer.append('\n\n')
1754 sys.stdout.write ("\n")
1755 sys.stdout.flush()
1756
1757 ## Save platform Map file
1758 #
1759 def _SaveMapFile (self, MapBuffer, Wa):
1760 #
1761 # Map file path is got.
1762 #
1763 MapFilePath = os.path.join(Wa.BuildDir, Wa.Name + '.map')
1764 #
1765 # Save address map into MAP file.
1766 #
1767 SaveFileOnChange(MapFilePath, ''.join(MapBuffer), False)
1768 if self.LoadFixAddress != 0:
1769 sys.stdout.write ("\nLoad Module At Fix Address Map file can be found at %s\n" % (MapFilePath))
1770 sys.stdout.flush()
1771
1772 ## Build active platform for different build targets and different tool chains
1773 #
1774 def _BuildPlatform(self):
1775 SaveFileOnChange(self.PlatformBuildPath, '# DO NOT EDIT \n# FILE auto-generated\n', False)
1776 for BuildTarget in self.BuildTargetList:
1777 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
1778 index = 0
1779 for ToolChain in self.ToolChainList:
1780 GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain
1781 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain
1782 GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[index]
1783 index += 1
1784 Wa = WorkspaceAutoGen(
1785 self.WorkspaceDir,
1786 self.PlatformFile,
1787 BuildTarget,
1788 ToolChain,
1789 self.ArchList,
1790 self.BuildDatabase,
1791 self.TargetTxt,
1792 self.ToolDef,
1793 self.Fdf,
1794 self.FdList,
1795 self.FvList,
1796 self.CapList,
1797 self.SkuId,
1798 self.UniFlag,
1799 self.Progress
1800 )
1801 self.Fdf = Wa.FdfFile
1802 self.LoadFixAddress = Wa.Platform.LoadFixAddress
1803 self.BuildReport.AddPlatformReport(Wa)
1804 self.Progress.Stop("done!")
1805
1806 # Add ffs build to makefile
1807 CmdListDict = {}
1808 if GlobalData.gEnableGenfdsMultiThread and self.Fdf:
1809 CmdListDict = self._GenFfsCmd(Wa.ArchList)
1810
1811 for Arch in Wa.ArchList:
1812 PcdMaList = []
1813 GlobalData.gGlobalDefines['ARCH'] = Arch
1814 Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch)
1815 for Module in Pa.Platform.Modules:
1816 # Get ModuleAutoGen object to generate C code file and makefile
1817 Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile,Pa.DataPipe)
1818 if Ma is None:
1819 continue
1820 if Ma.PcdIsDriver:
1821 Ma.PlatformInfo = Pa
1822 Ma.Workspace = Wa
1823 PcdMaList.append(Ma)
1824 self.BuildModules.append(Ma)
1825 Pa.DataPipe.DataContainer = {"FfsCommand":CmdListDict}
1826 Pa.DataPipe.DataContainer = {"Workspace_timestamp": Wa._SrcTimeStamp}
1827 self._BuildPa(self.Target, Pa, FfsCommand=CmdListDict,PcdMaList=PcdMaList)
1828
1829 # Create MAP file when Load Fix Address is enabled.
1830 if self.Target in ["", "all", "fds"]:
1831 for Arch in Wa.ArchList:
1832 GlobalData.gGlobalDefines['ARCH'] = Arch
1833 #
1834 # Check whether the set fix address is above 4G for 32bit image.
1835 #
1836 if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000:
1837 EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS can't be set to larger than or equal to 4G for the platform with IA32 or ARM arch modules")
1838 #
1839 # Get Module List
1840 #
1841 ModuleList = {}
1842 for Pa in Wa.AutoGenObjectList:
1843 for Ma in Pa.ModuleAutoGenList:
1844 if Ma is None:
1845 continue
1846 if not Ma.IsLibrary:
1847 ModuleList[Ma.Guid.upper()] = Ma
1848
1849 MapBuffer = []
1850 if self.LoadFixAddress != 0:
1851 #
1852 # Rebase module to the preferred memory address before GenFds
1853 #
1854 self._CollectModuleMapBuffer(MapBuffer, ModuleList)
1855 if self.Fdf:
1856 #
1857 # create FDS again for the updated EFI image
1858 #
1859 self._Build("fds", Wa)
1860 #
1861 # Create MAP file for all platform FVs after GenFds.
1862 #
1863 self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList)
1864 #
1865 # Save MAP buffer into MAP file.
1866 #
1867 self._SaveMapFile (MapBuffer, Wa)
1868 self.CreateGuidedSectionToolsFile(Wa)
1869
1870 ## Build active module for different build targets, different tool chains and different archs
1871 #
1872 def _BuildModule(self):
1873 for BuildTarget in self.BuildTargetList:
1874 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
1875 index = 0
1876 for ToolChain in self.ToolChainList:
1877 WorkspaceAutoGenTime = time.time()
1878 GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain
1879 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain
1880 GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[index]
1881 index += 1
1882 #
1883 # module build needs platform build information, so get platform
1884 # AutoGen first
1885 #
1886 Wa = WorkspaceAutoGen(
1887 self.WorkspaceDir,
1888 self.PlatformFile,
1889 BuildTarget,
1890 ToolChain,
1891 self.ArchList,
1892 self.BuildDatabase,
1893 self.TargetTxt,
1894 self.ToolDef,
1895 self.Fdf,
1896 self.FdList,
1897 self.FvList,
1898 self.CapList,
1899 self.SkuId,
1900 self.UniFlag,
1901 self.Progress,
1902 self.ModuleFile
1903 )
1904 self.Fdf = Wa.FdfFile
1905 self.LoadFixAddress = Wa.Platform.LoadFixAddress
1906 Wa.CreateMakeFile(False)
1907 # Add ffs build to makefile
1908 CmdListDict = None
1909 if GlobalData.gEnableGenfdsMultiThread and self.Fdf:
1910 CmdListDict = self._GenFfsCmd(Wa.ArchList)
1911
1912 GlobalData.file_lock = mp.Lock()
1913 GlobalData.FfsCmd = CmdListDict
1914
1915 self.Progress.Stop("done!")
1916 MaList = []
1917 ExitFlag = threading.Event()
1918 ExitFlag.clear()
1919 self.AutoGenTime += int(round((time.time() - WorkspaceAutoGenTime)))
1920 for Arch in Wa.ArchList:
1921 AutoGenStart = time.time()
1922 GlobalData.gGlobalDefines['ARCH'] = Arch
1923 Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch)
1924 for Module in Pa.Platform.Modules:
1925 if self.ModuleFile.Dir == Module.Dir and self.ModuleFile.Name == Module.Name:
1926 Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile,Pa.DataPipe)
1927 if Ma is None:
1928 continue
1929 if Ma.PcdIsDriver:
1930 Ma.PlatformInfo = Pa
1931 Ma.Workspace = Wa
1932 MaList.append(Ma)
1933
1934 if GlobalData.gUseHashCache and not GlobalData.gBinCacheDest and self.Target in [None, "", "all"]:
1935 if Ma.CanSkipbyPreMakeCache():
1936 continue
1937 else:
1938 self.PreMakeCacheMiss.add(Ma)
1939
1940 # Not to auto-gen for targets 'clean', 'cleanlib', 'cleanall', 'run', 'fds'
1941 if self.Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:
1942 # for target which must generate AutoGen code and makefile
1943 if not self.SkipAutoGen or self.Target == 'genc':
1944 self.Progress.Start("Generating code")
1945 Ma.CreateCodeFile(True)
1946 self.Progress.Stop("done!")
1947 if self.Target == "genc":
1948 return True
1949 if not self.SkipAutoGen or self.Target == 'genmake':
1950 self.Progress.Start("Generating makefile")
1951 if CmdListDict and self.Fdf and (Module.Path, Arch) in CmdListDict:
1952 Ma.CreateMakeFile(True, CmdListDict[Module.Path, Arch])
1953 del CmdListDict[Module.Path, Arch]
1954 else:
1955 Ma.CreateMakeFile(True)
1956 self.Progress.Stop("done!")
1957 if self.Target == "genmake":
1958 return True
1959
1960 if GlobalData.gBinCacheSource and self.Target in [None, "", "all"]:
1961 if Ma.CanSkipbyMakeCache():
1962 continue
1963 else:
1964 self.MakeCacheMiss.add(Ma)
1965
1966 self.BuildModules.append(Ma)
1967 self.AutoGenTime += int(round((time.time() - AutoGenStart)))
1968 MakeStart = time.time()
1969 for Ma in self.BuildModules:
1970 if not Ma.IsBinaryModule:
1971 Bt = BuildTask.New(ModuleMakeUnit(Ma, Pa.BuildCommand,self.Target))
1972 # Break build if any build thread has error
1973 if BuildTask.HasError():
1974 # we need a full version of makefile for platform
1975 ExitFlag.set()
1976 BuildTask.WaitForComplete()
1977 Pa.CreateMakeFile(False)
1978 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
1979 # Start task scheduler
1980 if not BuildTask.IsOnGoing():
1981 BuildTask.StartScheduler(self.ThreadNumber, ExitFlag)
1982
1983 # in case there's an interruption. we need a full version of makefile for platform
1984 Pa.CreateMakeFile(False)
1985 if BuildTask.HasError():
1986 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
1987 self.MakeTime += int(round((time.time() - MakeStart)))
1988
1989 MakeContiue = time.time()
1990 ExitFlag.set()
1991 BuildTask.WaitForComplete()
1992 self.CreateAsBuiltInf()
1993 if GlobalData.gBinCacheDest:
1994 self.GenDestCache()
1995 elif GlobalData.gUseHashCache and not GlobalData.gBinCacheSource:
1996 # Only for --hash
1997 # Update PreMakeCacheChain files
1998 self.GenLocalPreMakeCache()
1999 self.BuildModules = []
2000 self.MakeTime += int(round((time.time() - MakeContiue)))
2001 if BuildTask.HasError():
2002 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
2003
2004 self.BuildReport.AddPlatformReport(Wa, MaList)
2005 if MaList == []:
2006 EdkLogger.error(
2007 'build',
2008 BUILD_ERROR,
2009 "Module for [%s] is not a component of active platform."\
2010 " Please make sure that the ARCH and inf file path are"\
2011 " given in the same as in [%s]" % \
2012 (', '.join(Wa.ArchList), self.PlatformFile),
2013 ExtraData=self.ModuleFile
2014 )
2015 # Create MAP file when Load Fix Address is enabled.
2016 if self.Target == "fds" and self.Fdf:
2017 for Arch in Wa.ArchList:
2018 #
2019 # Check whether the set fix address is above 4G for 32bit image.
2020 #
2021 if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000:
2022 EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS can't be set to larger than or equal to 4G for the platorm with IA32 or ARM arch modules")
2023 #
2024 # Get Module List
2025 #
2026 ModuleList = {}
2027 for Pa in Wa.AutoGenObjectList:
2028 for Ma in Pa.ModuleAutoGenList:
2029 if Ma is None:
2030 continue
2031 if not Ma.IsLibrary:
2032 ModuleList[Ma.Guid.upper()] = Ma
2033
2034 MapBuffer = []
2035 if self.LoadFixAddress != 0:
2036 #
2037 # Rebase module to the preferred memory address before GenFds
2038 #
2039 self._CollectModuleMapBuffer(MapBuffer, ModuleList)
2040 #
2041 # create FDS again for the updated EFI image
2042 #
2043 GenFdsStart = time.time()
2044 self._Build("fds", Wa)
2045 self.GenFdsTime += int(round((time.time() - GenFdsStart)))
2046 #
2047 # Create MAP file for all platform FVs after GenFds.
2048 #
2049 self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList)
2050 #
2051 # Save MAP buffer into MAP file.
2052 #
2053 self._SaveMapFile (MapBuffer, Wa)
2054
2055 def _GenFfsCmd(self,ArchList):
2056 # convert dictionary of Cmd:(Inf,Arch)
2057 # to a new dictionary of (Inf,Arch):Cmd,Cmd,Cmd...
2058 CmdSetDict = defaultdict(set)
2059 GenFfsDict = GenFds.GenFfsMakefile('', GlobalData.gFdfParser, self, ArchList, GlobalData)
2060 for Cmd in GenFfsDict:
2061 tmpInf, tmpArch = GenFfsDict[Cmd]
2062 CmdSetDict[tmpInf, tmpArch].add(Cmd)
2063 return CmdSetDict
2064 def VerifyAutoGenFiles(self):
2065 AutoGenIdFile = os.path.join(GlobalData.gConfDirectory,".AutoGenIdFile.txt")
2066 try:
2067 with open(AutoGenIdFile) as fd:
2068 lines = fd.readlines()
2069 except:
2070 return None
2071 for line in lines:
2072 if "Arch" in line:
2073 ArchList = line.strip().split("=")[1].split("|")
2074 if "BuildDir" in line:
2075 BuildDir = line.split("=")[1].strip()
2076 if "PlatformGuid" in line:
2077 PlatformGuid = line.split("=")[1].strip()
2078 GlobalVarList = []
2079 for arch in ArchList:
2080 global_var = os.path.join(BuildDir, "GlobalVar_%s_%s.bin" % (str(PlatformGuid),arch))
2081 if not os.path.exists(global_var):
2082 return None
2083 GlobalVarList.append(global_var)
2084 for global_var in GlobalVarList:
2085 data_pipe = MemoryDataPipe()
2086 data_pipe.load(global_var)
2087 target = data_pipe.Get("P_Info").get("Target")
2088 toolchain = data_pipe.Get("P_Info").get("ToolChain")
2089 archlist = data_pipe.Get("P_Info").get("ArchList")
2090 Arch = data_pipe.Get("P_Info").get("Arch")
2091 active_p = data_pipe.Get("P_Info").get("ActivePlatform")
2092 workspacedir = data_pipe.Get("P_Info").get("WorkspaceDir")
2093 PackagesPath = os.getenv("PACKAGES_PATH")
2094 mws.setWs(workspacedir, PackagesPath)
2095 LibraryBuildDirectoryList = data_pipe.Get("LibraryBuildDirectoryList")
2096 ModuleBuildDirectoryList = data_pipe.Get("ModuleBuildDirectoryList")
2097
2098 for m_build_dir in LibraryBuildDirectoryList:
2099 if not os.path.exists(os.path.join(m_build_dir,self.MakeFileName)):
2100 return None
2101 for m_build_dir in ModuleBuildDirectoryList:
2102 if not os.path.exists(os.path.join(m_build_dir,self.MakeFileName)):
2103 return None
2104 Wa = WorkSpaceInfo(
2105 workspacedir,active_p,target,toolchain,archlist
2106 )
2107 Pa = PlatformInfo(Wa, active_p, target, toolchain, Arch,data_pipe)
2108 Wa.AutoGenObjectList.append(Pa)
2109 return Wa
2110 def SetupMakeSetting(self,Wa):
2111 BuildModules = []
2112 for Pa in Wa.AutoGenObjectList:
2113 for m in Pa._MbList:
2114 ma = ModuleAutoGen(Wa,m.MetaFile, Pa.BuildTarget, Wa.ToolChain, Pa.Arch, Pa.MetaFile,Pa.DataPipe)
2115 BuildModules.append(ma)
2116 fdf_file = Wa.FlashDefinition
2117 if fdf_file:
2118 Fdf = FdfParser(fdf_file.Path)
2119 Fdf.ParseFile()
2120 GlobalData.gFdfParser = Fdf
2121 if Fdf.CurrentFdName and Fdf.CurrentFdName in Fdf.Profile.FdDict:
2122 FdDict = Fdf.Profile.FdDict[Fdf.CurrentFdName]
2123 for FdRegion in FdDict.RegionList:
2124 if str(FdRegion.RegionType) == 'FILE' and self.Platform.VpdToolGuid in str(FdRegion.RegionDataList):
2125 if int(FdRegion.Offset) % 8 != 0:
2126 EdkLogger.error("build", FORMAT_INVALID, 'The VPD Base Address %s must be 8-byte aligned.' % (FdRegion.Offset))
2127 Wa.FdfProfile = Fdf.Profile
2128 self.Fdf = Fdf
2129 else:
2130 self.Fdf = None
2131 return BuildModules
2132
2133 ## Build a platform in multi-thread mode
2134 #
2135 def PerformAutoGen(self,BuildTarget,ToolChain):
2136 WorkspaceAutoGenTime = time.time()
2137 Wa = WorkspaceAutoGen(
2138 self.WorkspaceDir,
2139 self.PlatformFile,
2140 BuildTarget,
2141 ToolChain,
2142 self.ArchList,
2143 self.BuildDatabase,
2144 self.TargetTxt,
2145 self.ToolDef,
2146 self.Fdf,
2147 self.FdList,
2148 self.FvList,
2149 self.CapList,
2150 self.SkuId,
2151 self.UniFlag,
2152 self.Progress
2153 )
2154 self.Fdf = Wa.FdfFile
2155 self.LoadFixAddress = Wa.Platform.LoadFixAddress
2156 self.BuildReport.AddPlatformReport(Wa)
2157 Wa.CreateMakeFile(False)
2158
2159 # Add ffs build to makefile
2160 CmdListDict = {}
2161 if GlobalData.gEnableGenfdsMultiThread and self.Fdf:
2162 CmdListDict = self._GenFfsCmd(Wa.ArchList)
2163
2164 self.AutoGenTime += int(round((time.time() - WorkspaceAutoGenTime)))
2165 BuildModules = []
2166 for Arch in Wa.ArchList:
2167 PcdMaList = []
2168 AutoGenStart = time.time()
2169 GlobalData.gGlobalDefines['ARCH'] = Arch
2170 Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch)
2171 if Pa is None:
2172 continue
2173 ModuleList = []
2174 for Inf in Pa.Platform.Modules:
2175 ModuleList.append(Inf)
2176 # Add the INF only list in FDF
2177 if GlobalData.gFdfParser is not None:
2178 for InfName in GlobalData.gFdfParser.Profile.InfList:
2179 Inf = PathClass(NormPath(InfName), self.WorkspaceDir, Arch)
2180 if Inf in Pa.Platform.Modules:
2181 continue
2182 ModuleList.append(Inf)
2183 Pa.DataPipe.DataContainer = {"FfsCommand":CmdListDict}
2184 Pa.DataPipe.DataContainer = {"Workspace_timestamp": Wa._SrcTimeStamp}
2185 Pa.DataPipe.DataContainer = {"CommandTarget": self.Target}
2186 Pa.CreateLibModuelDirs()
2187 # Fetch the MakeFileName.
2188 self.MakeFileName = Pa.MakeFileName
2189
2190 Pa.DataPipe.DataContainer = {"LibraryBuildDirectoryList":Pa.LibraryBuildDirectoryList}
2191 Pa.DataPipe.DataContainer = {"ModuleBuildDirectoryList":Pa.ModuleBuildDirectoryList}
2192 Pa.DataPipe.DataContainer = {"FdsCommandDict": Wa.GenFdsCommandDict}
2193 # Prepare the cache share data for multiprocessing
2194 Pa.DataPipe.DataContainer = {"gPlatformHashFile":GlobalData.gPlatformHashFile}
2195 ModuleCodaFile = {}
2196 for ma in Pa.ModuleAutoGenList:
2197 ModuleCodaFile[(ma.MetaFile.File,ma.MetaFile.Root,ma.Arch,ma.MetaFile.Path)] = [item.Target for item in ma.CodaTargetList]
2198 Pa.DataPipe.DataContainer = {"ModuleCodaFile":ModuleCodaFile}
2199 # ModuleList contains all driver modules only
2200 for Module in ModuleList:
2201 # Get ModuleAutoGen object to generate C code file and makefile
2202 Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile,Pa.DataPipe)
2203 if Ma is None:
2204 continue
2205 if Ma.PcdIsDriver:
2206 Ma.PlatformInfo = Pa
2207 Ma.Workspace = Wa
2208 PcdMaList.append(Ma)
2209 self.AllDrivers.add(Ma)
2210 self.AllModules.add(Ma)
2211
2212 mqueue = mp.Queue()
2213 cqueue = mp.Queue()
2214 for m in Pa.GetAllModuleInfo:
2215 mqueue.put(m)
2216 module_file,module_root,module_path,module_basename,\
2217 module_originalpath,module_arch,IsLib = m
2218 Ma = ModuleAutoGen(Wa, PathClass(module_path, Wa), BuildTarget,\
2219 ToolChain, Arch, self.PlatformFile,Pa.DataPipe)
2220 self.AllModules.add(Ma)
2221 data_pipe_file = os.path.join(Pa.BuildDir, "GlobalVar_%s_%s.bin" % (str(Pa.Guid),Pa.Arch))
2222 Pa.DataPipe.dump(data_pipe_file)
2223
2224 mqueue.put((None,None,None,None,None,None,None))
2225 autogen_rt, errorcode = self.StartAutoGen(mqueue, Pa.DataPipe, self.SkipAutoGen, PcdMaList, cqueue)
2226
2227 if not autogen_rt:
2228 self.AutoGenMgr.TerminateWorkers()
2229 self.AutoGenMgr.join(1)
2230 raise FatalError(errorcode)
2231
2232 if GlobalData.gUseHashCache:
2233 for item in GlobalData.gModuleAllCacheStatus:
2234 (MetaFilePath, Arch, CacheStr, Status) = item
2235 Ma = ModuleAutoGen(Wa, PathClass(MetaFilePath, Wa), BuildTarget,\
2236 ToolChain, Arch, self.PlatformFile,Pa.DataPipe)
2237 if CacheStr == "PreMakeCache" and Status == False:
2238 self.PreMakeCacheMiss.add(Ma)
2239 if CacheStr == "PreMakeCache" and Status == True:
2240 self.PreMakeCacheHit.add(Ma)
2241 GlobalData.gModuleCacheHit.add(Ma)
2242 if CacheStr == "MakeCache" and Status == False:
2243 self.MakeCacheMiss.add(Ma)
2244 if CacheStr == "MakeCache" and Status == True:
2245 self.MakeCacheHit.add(Ma)
2246 GlobalData.gModuleCacheHit.add(Ma)
2247 self.AutoGenTime += int(round((time.time() - AutoGenStart)))
2248 AutoGenIdFile = os.path.join(GlobalData.gConfDirectory,".AutoGenIdFile.txt")
2249 with open(AutoGenIdFile,"w") as fw:
2250 fw.write("Arch=%s\n" % "|".join((Wa.ArchList)))
2251 fw.write("BuildDir=%s\n" % Wa.BuildDir)
2252 fw.write("PlatformGuid=%s\n" % str(Wa.AutoGenObjectList[0].Guid))
2253
2254 if GlobalData.gBinCacheSource:
2255 BuildModules.extend(self.MakeCacheMiss)
2256 elif GlobalData.gUseHashCache and not GlobalData.gBinCacheDest:
2257 BuildModules.extend(self.PreMakeCacheMiss)
2258 else:
2259 BuildModules.extend(self.AllDrivers)
2260
2261 self.Progress.Stop("done!")
2262 return Wa, BuildModules
2263
2264 def _MultiThreadBuildPlatform(self):
2265 SaveFileOnChange(self.PlatformBuildPath, '# DO NOT EDIT \n# FILE auto-generated\n', False)
2266 for BuildTarget in self.BuildTargetList:
2267 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
2268 index = 0
2269 for ToolChain in self.ToolChainList:
2270 resetFdsGlobalVariable()
2271 GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain
2272 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain
2273 GlobalData.gGlobalDefines['FAMILY'] = self.ToolChainFamily[index]
2274 index += 1
2275 ExitFlag = threading.Event()
2276 ExitFlag.clear()
2277 if self.SkipAutoGen:
2278 Wa = self.VerifyAutoGenFiles()
2279 if Wa is None:
2280 self.SkipAutoGen = False
2281 Wa, self.BuildModules = self.PerformAutoGen(BuildTarget,ToolChain)
2282 else:
2283 GlobalData.gAutoGenPhase = True
2284 self.BuildModules = self.SetupMakeSetting(Wa)
2285 else:
2286 Wa, self.BuildModules = self.PerformAutoGen(BuildTarget,ToolChain)
2287 Pa = Wa.AutoGenObjectList[0]
2288 GlobalData.gAutoGenPhase = False
2289
2290 if GlobalData.gBinCacheSource:
2291 EdkLogger.quiet("[cache Summary]: Total module num: %s" % len(self.AllModules))
2292 EdkLogger.quiet("[cache Summary]: PreMakecache miss num: %s " % len(self.PreMakeCacheMiss))
2293 EdkLogger.quiet("[cache Summary]: Makecache miss num: %s " % len(self.MakeCacheMiss))
2294
2295 for Arch in Wa.ArchList:
2296 MakeStart = time.time()
2297 for Ma in set(self.BuildModules):
2298 # Generate build task for the module
2299 if not Ma.IsBinaryModule:
2300 Bt = BuildTask.New(ModuleMakeUnit(Ma, Pa.BuildCommand,self.Target))
2301 # Break build if any build thread has error
2302 if BuildTask.HasError():
2303 # we need a full version of makefile for platform
2304 ExitFlag.set()
2305 BuildTask.WaitForComplete()
2306 Pa.CreateMakeFile(False)
2307 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
2308 # Start task scheduler
2309 if not BuildTask.IsOnGoing():
2310 BuildTask.StartScheduler(self.ThreadNumber, ExitFlag)
2311
2312 # in case there's an interruption. we need a full version of makefile for platform
2313
2314 if BuildTask.HasError():
2315 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
2316 self.MakeTime += int(round((time.time() - MakeStart)))
2317
2318 MakeContiue = time.time()
2319 #
2320 #
2321 # All modules have been put in build tasks queue. Tell task scheduler
2322 # to exit if all tasks are completed
2323 #
2324 ExitFlag.set()
2325 BuildTask.WaitForComplete()
2326 if GlobalData.gBinCacheDest:
2327 self.GenDestCache()
2328 elif GlobalData.gUseHashCache and not GlobalData.gBinCacheSource:
2329 # Only for --hash
2330 # Update PreMakeCacheChain files
2331 self.GenLocalPreMakeCache()
2332 #
2333 # Get Module List
2334 #
2335 ModuleList = {ma.Guid.upper(): ma for ma in self.BuildModules}
2336 self.BuildModules = []
2337 self.MakeTime += int(round((time.time() - MakeContiue)))
2338 #
2339 # Check for build error, and raise exception if one
2340 # has been signaled.
2341 #
2342 if BuildTask.HasError():
2343 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
2344
2345 # Create MAP file when Load Fix Address is enabled.
2346 if self.Target in ["", "all", "fds"]:
2347 for Arch in Wa.ArchList:
2348 #
2349 # Check whether the set fix address is above 4G for 32bit image.
2350 #
2351 if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000:
2352 EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS can't be set to larger than or equal to 4G for the platorm with IA32 or ARM arch modules")
2353
2354 #
2355 # Rebase module to the preferred memory address before GenFds
2356 #
2357 MapBuffer = []
2358 if self.LoadFixAddress != 0:
2359 self._CollectModuleMapBuffer(MapBuffer, ModuleList)
2360
2361 if self.Fdf:
2362 #
2363 # Generate FD image if there's a FDF file found
2364 #
2365 GenFdsStart = time.time()
2366 if GenFdsApi(Wa.GenFdsCommandDict, self.Db):
2367 EdkLogger.error("build", COMMAND_FAILURE)
2368 Threshold = self.GetFreeSizeThreshold()
2369 if Threshold:
2370 self.CheckFreeSizeThreshold(Threshold, Wa.FvDir)
2371
2372 #
2373 # Create MAP file for all platform FVs after GenFds.
2374 #
2375 self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList)
2376 self.GenFdsTime += int(round((time.time() - GenFdsStart)))
2377 #
2378 # Save MAP buffer into MAP file.
2379 #
2380 self._SaveMapFile(MapBuffer, Wa)
2381 self.CreateGuidedSectionToolsFile(Wa)
2382
2383 ## GetFreeSizeThreshold()
2384 #
2385 # @retval int Threshold value
2386 #
2387 def GetFreeSizeThreshold(self):
2388 Threshold = None
2389 Threshold_Str = GlobalData.gCommandLineDefines.get('FV_SPARE_SPACE_THRESHOLD')
2390 if Threshold_Str:
2391 try:
2392 if Threshold_Str.lower().startswith('0x'):
2393 Threshold = int(Threshold_Str, 16)
2394 else:
2395 Threshold = int(Threshold_Str)
2396 except:
2397 EdkLogger.warn("build", 'incorrect value for FV_SPARE_SPACE_THRESHOLD %s.Only decimal or hex format is allowed.' % Threshold_Str)
2398 return Threshold
2399
2400 def CheckFreeSizeThreshold(self, Threshold=None, FvDir=None):
2401 if not isinstance(Threshold, int):
2402 return
2403 if not isinstance(FvDir, str) or not FvDir:
2404 return
2405 FdfParserObject = GlobalData.gFdfParser
2406 FvRegionNameList = [FvName for FvName in FdfParserObject.Profile.FvDict if FdfParserObject.Profile.FvDict[FvName].FvRegionInFD]
2407 for FvName in FdfParserObject.Profile.FvDict:
2408 if FvName in FvRegionNameList:
2409 FvSpaceInfoFileName = os.path.join(FvDir, FvName.upper() + '.Fv.map')
2410 if os.path.exists(FvSpaceInfoFileName):
2411 FileLinesList = getlines(FvSpaceInfoFileName)
2412 for Line in FileLinesList:
2413 NameValue = Line.split('=')
2414 if len(NameValue) == 2 and NameValue[0].strip() == 'EFI_FV_SPACE_SIZE':
2415 FreeSizeValue = int(NameValue[1].strip(), 0)
2416 if FreeSizeValue < Threshold:
2417 EdkLogger.error("build", FV_FREESIZE_ERROR,
2418 '%s FV free space %d is not enough to meet with the required spare space %d set by -D FV_SPARE_SPACE_THRESHOLD option.' % (
2419 FvName, FreeSizeValue, Threshold))
2420 break
2421
2422 ## Generate GuidedSectionTools.txt in the FV directories.
2423 #
2424 def CreateGuidedSectionToolsFile(self,Wa):
2425 for BuildTarget in self.BuildTargetList:
2426 for ToolChain in self.ToolChainList:
2427 FvDir = Wa.FvDir
2428 if not os.path.exists(FvDir):
2429 continue
2430 for Arch in self.ArchList:
2431 guidList = []
2432 tooldefguidList = []
2433 guidAttribs = []
2434 for Platform in Wa.AutoGenObjectList:
2435 if Platform.BuildTarget != BuildTarget:
2436 continue
2437 if Platform.ToolChain != ToolChain:
2438 continue
2439 if Platform.Arch != Arch:
2440 continue
2441 if hasattr (Platform, 'BuildOption'):
2442 for Tool in Platform.BuildOption:
2443 if 'GUID' in Platform.BuildOption[Tool]:
2444 if 'PATH' in Platform.BuildOption[Tool]:
2445 value = Platform.BuildOption[Tool]['GUID']
2446 if value in guidList:
2447 EdkLogger.error("build", FORMAT_INVALID, "Duplicate GUID value %s used with Tool %s in DSC [BuildOptions]." % (value, Tool))
2448 path = Platform.BuildOption[Tool]['PATH']
2449 guidList.append(value)
2450 guidAttribs.append((value, Tool, path))
2451 for Tool in Platform.ToolDefinition:
2452 if 'GUID' in Platform.ToolDefinition[Tool]:
2453 if 'PATH' in Platform.ToolDefinition[Tool]:
2454 value = Platform.ToolDefinition[Tool]['GUID']
2455 if value in tooldefguidList:
2456 EdkLogger.error("build", FORMAT_INVALID, "Duplicate GUID value %s used with Tool %s in tools_def.txt." % (value, Tool))
2457 tooldefguidList.append(value)
2458 if value in guidList:
2459 # Already added by platform
2460 continue
2461 path = Platform.ToolDefinition[Tool]['PATH']
2462 guidList.append(value)
2463 guidAttribs.append((value, Tool, path))
2464 # Sort by GuidTool name
2465 guidAttribs = sorted (guidAttribs, key=lambda x: x[1])
2466 # Write out GuidedSecTools.txt
2467 toolsFile = os.path.join(FvDir, 'GuidedSectionTools.txt')
2468 toolsFile = open(toolsFile, 'wt')
2469 for guidedSectionTool in guidAttribs:
2470 print(' '.join(guidedSectionTool), file=toolsFile)
2471 toolsFile.close()
2472
2473 ## Returns the real path of the tool.
2474 #
2475 def GetRealPathOfTool (self, tool):
2476 if os.path.exists(tool):
2477 return os.path.realpath(tool)
2478 return tool
2479
2480 ## Launch the module or platform build
2481 #
2482 def Launch(self):
2483 self.AllDrivers = set()
2484 self.AllModules = set()
2485 self.PreMakeCacheMiss = set()
2486 self.PreMakeCacheHit = set()
2487 self.MakeCacheMiss = set()
2488 self.MakeCacheHit = set()
2489 if not self.ModuleFile:
2490 if not self.SpawnMode or self.Target not in ["", "all"]:
2491 self.SpawnMode = False
2492 self._BuildPlatform()
2493 else:
2494 self._MultiThreadBuildPlatform()
2495 else:
2496 self.SpawnMode = False
2497 self._BuildModule()
2498
2499 if self.Target == 'cleanall':
2500 RemoveDirectory(os.path.dirname(GlobalData.gDatabasePath), True)
2501
2502 def CreateAsBuiltInf(self):
2503 for Module in self.BuildModules:
2504 Module.CreateAsBuiltInf()
2505
2506 def GenDestCache(self):
2507 for Module in self.AllModules:
2508 Module.GenPreMakefileHashList()
2509 Module.GenMakefileHashList()
2510 Module.CopyModuleToCache()
2511
2512 def GenLocalPreMakeCache(self):
2513 for Module in self.PreMakeCacheMiss:
2514 Module.GenPreMakefileHashList()
2515
2516 ## Do some clean-up works when error occurred
2517 def Relinquish(self):
2518 OldLogLevel = EdkLogger.GetLevel()
2519 EdkLogger.SetLevel(EdkLogger.ERROR)
2520 Utils.Progressor.Abort()
2521 if self.SpawnMode == True:
2522 BuildTask.Abort()
2523 EdkLogger.SetLevel(OldLogLevel)
2524
2525 def ParseDefines(DefineList=[]):
2526 DefineDict = {}
2527 if DefineList is not None:
2528 for Define in DefineList:
2529 DefineTokenList = Define.split("=", 1)
2530 if not GlobalData.gMacroNamePattern.match(DefineTokenList[0]):
2531 EdkLogger.error('build', FORMAT_INVALID,
2532 "The macro name must be in the pattern [A-Z][A-Z0-9_]*",
2533 ExtraData=DefineTokenList[0])
2534
2535 if len(DefineTokenList) == 1:
2536 DefineDict[DefineTokenList[0]] = "TRUE"
2537 else:
2538 DefineDict[DefineTokenList[0]] = DefineTokenList[1].strip()
2539 return DefineDict
2540
2541
2542
2543 def LogBuildTime(Time):
2544 if Time:
2545 TimeDurStr = ''
2546 TimeDur = time.gmtime(Time)
2547 if TimeDur.tm_yday > 1:
2548 TimeDurStr = time.strftime("%H:%M:%S", TimeDur) + ", %d day(s)" % (TimeDur.tm_yday - 1)
2549 else:
2550 TimeDurStr = time.strftime("%H:%M:%S", TimeDur)
2551 return TimeDurStr
2552 else:
2553 return None
2554 def ThreadNum():
2555 OptionParser = MyOptionParser()
2556 if not OptionParser.BuildOption and not OptionParser.BuildTarget:
2557 OptionParser.GetOption()
2558 BuildOption, BuildTarget = OptionParser.BuildOption, OptionParser.BuildTarget
2559 ThreadNumber = BuildOption.ThreadNumber
2560 GlobalData.gCmdConfDir = BuildOption.ConfDirectory
2561 if ThreadNumber is None:
2562 TargetObj = TargetTxtDict()
2563 ThreadNumber = TargetObj.Target.TargetTxtDictionary[TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]
2564 if ThreadNumber == '':
2565 ThreadNumber = 0
2566 else:
2567 ThreadNumber = int(ThreadNumber, 0)
2568
2569 if ThreadNumber == 0:
2570 try:
2571 ThreadNumber = multiprocessing.cpu_count()
2572 except (ImportError, NotImplementedError):
2573 ThreadNumber = 1
2574 return ThreadNumber
2575 ## Tool entrance method
2576 #
2577 # This method mainly dispatch specific methods per the command line options.
2578 # If no error found, return zero value so the caller of this tool can know
2579 # if it's executed successfully or not.
2580 #
2581 # @retval 0 Tool was successful
2582 # @retval 1 Tool failed
2583 #
2584 LogQMaxSize = ThreadNum() * 10
2585 def Main():
2586 StartTime = time.time()
2587
2588 #
2589 # Create a log Queue
2590 #
2591 LogQ = mp.Queue(LogQMaxSize)
2592 # Initialize log system
2593 EdkLogger.LogClientInitialize(LogQ)
2594 GlobalData.gCommand = sys.argv[1:]
2595 #
2596 # Parse the options and args
2597 #
2598 OptionParser = MyOptionParser()
2599 if not OptionParser.BuildOption and not OptionParser.BuildTarget:
2600 OptionParser.GetOption()
2601 Option, Target = OptionParser.BuildOption, OptionParser.BuildTarget
2602 GlobalData.gOptions = Option
2603 GlobalData.gCaseInsensitive = Option.CaseInsensitive
2604
2605 # Set log level
2606 LogLevel = EdkLogger.INFO
2607 if Option.verbose is not None:
2608 EdkLogger.SetLevel(EdkLogger.VERBOSE)
2609 LogLevel = EdkLogger.VERBOSE
2610 elif Option.quiet is not None:
2611 EdkLogger.SetLevel(EdkLogger.QUIET)
2612 LogLevel = EdkLogger.QUIET
2613 elif Option.debug is not None:
2614 EdkLogger.SetLevel(Option.debug + 1)
2615 LogLevel = Option.debug + 1
2616 else:
2617 EdkLogger.SetLevel(EdkLogger.INFO)
2618
2619 if Option.WarningAsError == True:
2620 EdkLogger.SetWarningAsError()
2621 Log_Agent = LogAgent(LogQ,LogLevel,Option.LogFile)
2622 Log_Agent.start()
2623
2624 if platform.platform().find("Windows") >= 0:
2625 GlobalData.gIsWindows = True
2626 else:
2627 GlobalData.gIsWindows = False
2628
2629 EdkLogger.quiet("Build environment: %s" % platform.platform())
2630 EdkLogger.quiet(time.strftime("Build start time: %H:%M:%S, %b.%d %Y\n", time.localtime()));
2631 ReturnCode = 0
2632 MyBuild = None
2633 BuildError = True
2634 try:
2635 if len(Target) == 0:
2636 Target = "all"
2637 elif len(Target) >= 2:
2638 EdkLogger.error("build", OPTION_NOT_SUPPORTED, "More than one targets are not supported.",
2639 ExtraData="Please select one of: %s" % (' '.join(gSupportedTarget)))
2640 else:
2641 Target = Target[0].lower()
2642
2643 if Target not in gSupportedTarget:
2644 EdkLogger.error("build", OPTION_NOT_SUPPORTED, "Not supported target [%s]." % Target,
2645 ExtraData="Please select one of: %s" % (' '.join(gSupportedTarget)))
2646
2647 #
2648 # Check environment variable: EDK_TOOLS_PATH, WORKSPACE, PATH
2649 #
2650 CheckEnvVariable()
2651 GlobalData.gCommandLineDefines.update(ParseDefines(Option.Macros))
2652
2653 Workspace = os.getenv("WORKSPACE")
2654 #
2655 # Get files real name in workspace dir
2656 #
2657 GlobalData.gAllFiles = Utils.DirCache(Workspace)
2658
2659 WorkingDirectory = os.getcwd()
2660 if not Option.ModuleFile:
2661 FileList = glob.glob(os.path.normpath(os.path.join(WorkingDirectory, '*.inf')))
2662 FileNum = len(FileList)
2663 if FileNum >= 2:
2664 EdkLogger.error("build", OPTION_NOT_SUPPORTED, "There are %d INF files in %s." % (FileNum, WorkingDirectory),
2665 ExtraData="Please use '-m <INF_FILE_PATH>' switch to choose one.")
2666 elif FileNum == 1:
2667 Option.ModuleFile = NormFile(FileList[0], Workspace)
2668
2669 if Option.ModuleFile:
2670 if os.path.isabs (Option.ModuleFile):
2671 if os.path.normcase (os.path.normpath(Option.ModuleFile)).find (Workspace) == 0:
2672 Option.ModuleFile = NormFile(os.path.normpath(Option.ModuleFile), Workspace)
2673 Option.ModuleFile = PathClass(Option.ModuleFile, Workspace)
2674 ErrorCode, ErrorInfo = Option.ModuleFile.Validate(".inf", False)
2675 if ErrorCode != 0:
2676 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
2677
2678 if Option.PlatformFile is not None:
2679 if os.path.isabs (Option.PlatformFile):
2680 if os.path.normcase (os.path.normpath(Option.PlatformFile)).find (Workspace) == 0:
2681 Option.PlatformFile = NormFile(os.path.normpath(Option.PlatformFile), Workspace)
2682 Option.PlatformFile = PathClass(Option.PlatformFile, Workspace)
2683
2684 if Option.FdfFile is not None:
2685 if os.path.isabs (Option.FdfFile):
2686 if os.path.normcase (os.path.normpath(Option.FdfFile)).find (Workspace) == 0:
2687 Option.FdfFile = NormFile(os.path.normpath(Option.FdfFile), Workspace)
2688 Option.FdfFile = PathClass(Option.FdfFile, Workspace)
2689 ErrorCode, ErrorInfo = Option.FdfFile.Validate(".fdf", False)
2690 if ErrorCode != 0:
2691 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
2692
2693 if Option.Flag is not None and Option.Flag not in ['-c', '-s']:
2694 EdkLogger.error("build", OPTION_VALUE_INVALID, "UNI flag must be one of -c or -s")
2695
2696 MyBuild = Build(Target, Workspace, Option,LogQ)
2697 GlobalData.gCommandLineDefines['ARCH'] = ' '.join(MyBuild.ArchList)
2698 if not (MyBuild.LaunchPrebuildFlag and os.path.exists(MyBuild.PlatformBuildPath)):
2699 MyBuild.Launch()
2700
2701 #
2702 # All job done, no error found and no exception raised
2703 #
2704 BuildError = False
2705 except FatalError as X:
2706 if MyBuild is not None:
2707 # for multi-thread build exits safely
2708 MyBuild.Relinquish()
2709 if Option is not None and Option.debug is not None:
2710 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
2711 ReturnCode = X.args[0]
2712 except Warning as X:
2713 # error from Fdf parser
2714 if MyBuild is not None:
2715 # for multi-thread build exits safely
2716 MyBuild.Relinquish()
2717 if Option is not None and Option.debug is not None:
2718 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
2719 else:
2720 EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError=False)
2721 ReturnCode = FORMAT_INVALID
2722 except KeyboardInterrupt:
2723 if MyBuild is not None:
2724
2725 # for multi-thread build exits safely
2726 MyBuild.Relinquish()
2727 ReturnCode = ABORT_ERROR
2728 if Option is not None and Option.debug is not None:
2729 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
2730 except:
2731 if MyBuild is not None:
2732 # for multi-thread build exits safely
2733 MyBuild.Relinquish()
2734
2735 # try to get the meta-file from the object causing exception
2736 Tb = sys.exc_info()[-1]
2737 MetaFile = GlobalData.gProcessingFile
2738 while Tb is not None:
2739 if 'self' in Tb.tb_frame.f_locals and hasattr(Tb.tb_frame.f_locals['self'], 'MetaFile'):
2740 MetaFile = Tb.tb_frame.f_locals['self'].MetaFile
2741 Tb = Tb.tb_next
2742 EdkLogger.error(
2743 "\nbuild",
2744 CODE_ERROR,
2745 "Unknown fatal error when processing [%s]" % MetaFile,
2746 ExtraData="\n(Please send email to %s for help, attaching following call stack trace!)\n" % MSG_EDKII_MAIL_ADDR,
2747 RaiseError=False
2748 )
2749 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
2750 ReturnCode = CODE_ERROR
2751 finally:
2752 Utils.Progressor.Abort()
2753 Utils.ClearDuplicatedInf()
2754
2755 if ReturnCode == 0:
2756 try:
2757 MyBuild.LaunchPostbuild()
2758 Conclusion = "Done"
2759 except:
2760 Conclusion = "Failed"
2761 ReturnCode = POSTBUILD_ERROR
2762 elif ReturnCode == ABORT_ERROR:
2763 Conclusion = "Aborted"
2764 else:
2765 Conclusion = "Failed"
2766 FinishTime = time.time()
2767 BuildDuration = time.gmtime(int(round(FinishTime - StartTime)))
2768 BuildDurationStr = ""
2769 if BuildDuration.tm_yday > 1:
2770 BuildDurationStr = time.strftime("%H:%M:%S", BuildDuration) + ", %d day(s)" % (BuildDuration.tm_yday - 1)
2771 else:
2772 BuildDurationStr = time.strftime("%H:%M:%S", BuildDuration)
2773 if MyBuild is not None:
2774 if not BuildError:
2775 MyBuild.BuildReport.GenerateReport(BuildDurationStr, LogBuildTime(MyBuild.AutoGenTime), LogBuildTime(MyBuild.MakeTime), LogBuildTime(MyBuild.GenFdsTime))
2776
2777 EdkLogger.SetLevel(EdkLogger.QUIET)
2778 EdkLogger.quiet("\n- %s -" % Conclusion)
2779 EdkLogger.quiet(time.strftime("Build end time: %H:%M:%S, %b.%d %Y", time.localtime()))
2780 EdkLogger.quiet("Build total time: %s\n" % BuildDurationStr)
2781 Log_Agent.kill()
2782 Log_Agent.join()
2783 return ReturnCode
2784
2785 if __name__ == '__main__':
2786 try:
2787 mp.set_start_method('spawn')
2788 except:
2789 pass
2790 r = Main()
2791 ## 0-127 is a safe return range, and 1 is a standard default error
2792 if r < 0 or r > 127: r = 1
2793 sys.exit(r)