]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/build/build.py
Sync BaseTool trunk (version r2474) into EDKII BaseTools.
[mirror_edk2.git] / BaseTools / Source / Python / build / build.py
1 ## @file
2 # build a platform or a module
3 #
4 # Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.<BR>
5 #
6 # This program and the accompanying materials
7 # are licensed and made available under the terms and conditions of the BSD License
8 # which accompanies this distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
10 #
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13 #
14
15 ##
16 # Import Modules
17 #
18 import os
19 import re
20 import StringIO
21 import sys
22 import glob
23 import time
24 import platform
25 import traceback
26 import encodings.ascii
27
28 from struct import *
29 from threading import *
30 from optparse import OptionParser
31 from subprocess import *
32 from Common import Misc as Utils
33
34 from Common.TargetTxtClassObject import *
35 from Common.ToolDefClassObject import *
36 from Common.DataType import *
37 from Common.BuildVersion import gBUILD_VERSION
38 from AutoGen.AutoGen import *
39 from Common.BuildToolError import *
40 from Workspace.WorkspaceDatabase import *
41
42 from BuildReport import BuildReport
43 from GenPatchPcdTable.GenPatchPcdTable import *
44 from PatchPcdValue.PatchPcdValue import *
45
46 import Common.EdkLogger
47 import Common.GlobalData as GlobalData
48
49 # Version and Copyright
50 VersionNumber = "0.5" + ' ' + gBUILD_VERSION
51 __version__ = "%prog Version " + VersionNumber
52 __copyright__ = "Copyright (c) 2007 - 2010, Intel Corporation All rights reserved."
53
54 ## standard targets of build command
55 gSupportedTarget = ['all', 'genc', 'genmake', 'modules', 'libraries', 'fds', 'clean', 'cleanall', 'cleanlib', 'run']
56
57 ## build configuration file
58 gBuildConfiguration = "Conf/target.txt"
59 gBuildCacheDir = "Conf/.cache"
60 gToolsDefinition = "Conf/tools_def.txt"
61
62 ## Check environment PATH variable to make sure the specified tool is found
63 #
64 # If the tool is found in the PATH, then True is returned
65 # Otherwise, False is returned
66 #
67 def IsToolInPath(tool):
68 if os.environ.has_key('PATHEXT'):
69 extns = os.environ['PATHEXT'].split(os.path.pathsep)
70 else:
71 extns = ('',)
72 for pathDir in os.environ['PATH'].split(os.path.pathsep):
73 for ext in extns:
74 if os.path.exists(os.path.join(pathDir, tool + ext)):
75 return True
76 return False
77
78 ## Check environment variables
79 #
80 # Check environment variables that must be set for build. Currently they are
81 #
82 # WORKSPACE The directory all packages/platforms start from
83 # EDK_TOOLS_PATH The directory contains all tools needed by the build
84 # PATH $(EDK_TOOLS_PATH)/Bin/<sys> must be set in PATH
85 #
86 # If any of above environment variable is not set or has error, the build
87 # will be broken.
88 #
89 def CheckEnvVariable():
90 # check WORKSPACE
91 if "WORKSPACE" not in os.environ:
92 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
93 ExtraData="WORKSPACE")
94
95 WorkspaceDir = os.path.normcase(os.path.normpath(os.environ["WORKSPACE"]))
96 if not os.path.exists(WorkspaceDir):
97 EdkLogger.error("build", FILE_NOT_FOUND, "WORKSPACE doesn't exist", ExtraData="%s" % WorkspaceDir)
98 elif ' ' in WorkspaceDir:
99 EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in WORKSPACE path",
100 ExtraData=WorkspaceDir)
101 os.environ["WORKSPACE"] = WorkspaceDir
102
103 #
104 # Check EFI_SOURCE (Edk build convention). EDK_SOURCE will always point to ECP
105 #
106 if "ECP_SOURCE" not in os.environ:
107 os.environ["ECP_SOURCE"] = os.path.join(WorkspaceDir, GlobalData.gEdkCompatibilityPkg)
108 if "EFI_SOURCE" not in os.environ:
109 os.environ["EFI_SOURCE"] = os.environ["ECP_SOURCE"]
110 if "EDK_SOURCE" not in os.environ:
111 os.environ["EDK_SOURCE"] = os.environ["ECP_SOURCE"]
112
113 #
114 # Unify case of characters on case-insensitive systems
115 #
116 EfiSourceDir = os.path.normcase(os.path.normpath(os.environ["EFI_SOURCE"]))
117 EdkSourceDir = os.path.normcase(os.path.normpath(os.environ["EDK_SOURCE"]))
118 EcpSourceDir = os.path.normcase(os.path.normpath(os.environ["ECP_SOURCE"]))
119
120 os.environ["EFI_SOURCE"] = EfiSourceDir
121 os.environ["EDK_SOURCE"] = EdkSourceDir
122 os.environ["ECP_SOURCE"] = EcpSourceDir
123 os.environ["EDK_TOOLS_PATH"] = os.path.normcase(os.environ["EDK_TOOLS_PATH"])
124
125 if not os.path.exists(EcpSourceDir):
126 EdkLogger.verbose("ECP_SOURCE = %s doesn't exist. Edk modules could not be built." % EcpSourceDir)
127 elif ' ' in EcpSourceDir:
128 EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in ECP_SOURCE path",
129 ExtraData=EcpSourceDir)
130 if not os.path.exists(EdkSourceDir):
131 if EdkSourceDir == EcpSourceDir:
132 EdkLogger.verbose("EDK_SOURCE = %s doesn't exist. Edk modules could not be built." % EdkSourceDir)
133 else:
134 EdkLogger.error("build", PARAMETER_INVALID, "EDK_SOURCE does not exist",
135 ExtraData=EdkSourceDir)
136 elif ' ' in EdkSourceDir:
137 EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in EDK_SOURCE path",
138 ExtraData=EdkSourceDir)
139 if not os.path.exists(EfiSourceDir):
140 if EfiSourceDir == EcpSourceDir:
141 EdkLogger.verbose("EFI_SOURCE = %s doesn't exist. Edk modules could not be built." % EfiSourceDir)
142 else:
143 EdkLogger.error("build", PARAMETER_INVALID, "EFI_SOURCE does not exist",
144 ExtraData=EfiSourceDir)
145 elif ' ' in EfiSourceDir:
146 EdkLogger.error("build", FORMAT_NOT_SUPPORTED, "No space is allowed in EFI_SOURCE path",
147 ExtraData=EfiSourceDir)
148
149 # change absolute path to relative path to WORKSPACE
150 if EfiSourceDir.upper().find(WorkspaceDir.upper()) != 0:
151 EdkLogger.error("build", PARAMETER_INVALID, "EFI_SOURCE is not under WORKSPACE",
152 ExtraData="WORKSPACE = %s\n EFI_SOURCE = %s" % (WorkspaceDir, EfiSourceDir))
153 if EdkSourceDir.upper().find(WorkspaceDir.upper()) != 0:
154 EdkLogger.error("build", PARAMETER_INVALID, "EDK_SOURCE is not under WORKSPACE",
155 ExtraData="WORKSPACE = %s\n EDK_SOURCE = %s" % (WorkspaceDir, EdkSourceDir))
156 if EcpSourceDir.upper().find(WorkspaceDir.upper()) != 0:
157 EdkLogger.error("build", PARAMETER_INVALID, "ECP_SOURCE is not under WORKSPACE",
158 ExtraData="WORKSPACE = %s\n ECP_SOURCE = %s" % (WorkspaceDir, EcpSourceDir))
159
160 # check EDK_TOOLS_PATH
161 if "EDK_TOOLS_PATH" not in os.environ:
162 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
163 ExtraData="EDK_TOOLS_PATH")
164
165 # check PATH
166 if "PATH" not in os.environ:
167 EdkLogger.error("build", ATTRIBUTE_NOT_AVAILABLE, "Environment variable not found",
168 ExtraData="PATH")
169
170 GlobalData.gWorkspace = WorkspaceDir
171 GlobalData.gEfiSource = EfiSourceDir
172 GlobalData.gEdkSource = EdkSourceDir
173 GlobalData.gEcpSource = EcpSourceDir
174
175 GlobalData.gGlobalDefines["WORKSPACE"] = WorkspaceDir
176 GlobalData.gGlobalDefines["EFI_SOURCE"] = EfiSourceDir
177 GlobalData.gGlobalDefines["EDK_SOURCE"] = EdkSourceDir
178 GlobalData.gGlobalDefines["ECP_SOURCE"] = EcpSourceDir
179 GlobalData.gGlobalDefines["EDK_TOOLS_PATH"] = os.environ["EDK_TOOLS_PATH"]
180
181 ## Get normalized file path
182 #
183 # Convert the path to be local format, and remove the WORKSPACE path at the
184 # beginning if the file path is given in full path.
185 #
186 # @param FilePath File path to be normalized
187 # @param Workspace Workspace path which the FilePath will be checked against
188 #
189 # @retval string The normalized file path
190 #
191 def NormFile(FilePath, Workspace):
192 # check if the path is absolute or relative
193 if os.path.isabs(FilePath):
194 FileFullPath = os.path.normpath(FilePath)
195 else:
196 FileFullPath = os.path.normpath(os.path.join(Workspace, FilePath))
197
198 # check if the file path exists or not
199 if not os.path.isfile(FileFullPath):
200 EdkLogger.error("build", FILE_NOT_FOUND, ExtraData="\t%s (Please give file in absolute path or relative to WORKSPACE)" % FileFullPath)
201
202 # remove workspace directory from the beginning part of the file path
203 if Workspace[-1] in ["\\", "/"]:
204 return FileFullPath[len(Workspace):]
205 else:
206 return FileFullPath[(len(Workspace) + 1):]
207
208 ## Get the output of an external program
209 #
210 # This is the entrance method of thread reading output of an external program and
211 # putting them in STDOUT/STDERR of current program.
212 #
213 # @param From The stream message read from
214 # @param To The stream message put on
215 # @param ExitFlag The flag used to indicate stopping reading
216 #
217 def ReadMessage(From, To, ExitFlag):
218 while True:
219 # read one line a time
220 Line = From.readline()
221 # empty string means "end"
222 if Line != None and Line != "":
223 To(Line.rstrip())
224 else:
225 break
226 if ExitFlag.isSet():
227 break
228
229 ## Launch an external program
230 #
231 # This method will call subprocess.Popen to execute an external program with
232 # given options in specified directory. Because of the dead-lock issue during
233 # redirecting output of the external program, threads are used to to do the
234 # redirection work.
235 #
236 # @param Command A list or string containing the call of the program
237 # @param WorkingDir The directory in which the program will be running
238 #
239 def LaunchCommand(Command, WorkingDir):
240 # if working directory doesn't exist, Popen() will raise an exception
241 if not os.path.isdir(WorkingDir):
242 EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=WorkingDir)
243
244 Proc = None
245 EndOfProcedure = None
246 try:
247 # launch the command
248 Proc = Popen(Command, stdout=PIPE, stderr=PIPE, env=os.environ, cwd=WorkingDir, bufsize=-1)
249
250 # launch two threads to read the STDOUT and STDERR
251 EndOfProcedure = Event()
252 EndOfProcedure.clear()
253 if Proc.stdout:
254 StdOutThread = Thread(target=ReadMessage, args=(Proc.stdout, EdkLogger.info, EndOfProcedure))
255 StdOutThread.setName("STDOUT-Redirector")
256 StdOutThread.setDaemon(False)
257 StdOutThread.start()
258
259 if Proc.stderr:
260 StdErrThread = Thread(target=ReadMessage, args=(Proc.stderr, EdkLogger.quiet, EndOfProcedure))
261 StdErrThread.setName("STDERR-Redirector")
262 StdErrThread.setDaemon(False)
263 StdErrThread.start()
264
265 # waiting for program exit
266 Proc.wait()
267 except: # in case of aborting
268 # terminate the threads redirecting the program output
269 if EndOfProcedure != None:
270 EndOfProcedure.set()
271 if Proc == None:
272 if type(Command) != type(""):
273 Command = " ".join(Command)
274 EdkLogger.error("build", COMMAND_FAILURE, "Failed to start command", ExtraData="%s [%s]" % (Command, WorkingDir))
275
276 if Proc.stdout:
277 StdOutThread.join()
278 if Proc.stderr:
279 StdErrThread.join()
280
281 # check the return code of the program
282 if Proc.returncode != 0:
283 if type(Command) != type(""):
284 Command = " ".join(Command)
285 EdkLogger.error("build", COMMAND_FAILURE, ExtraData="%s [%s]" % (Command, WorkingDir))
286
287 ## The smallest unit that can be built in multi-thread build mode
288 #
289 # This is the base class of build unit. The "Obj" parameter must provide
290 # __str__(), __eq__() and __hash__() methods. Otherwise there could be build units
291 # missing build.
292 #
293 # Currently the "Obj" should be only ModuleAutoGen or PlatformAutoGen objects.
294 #
295 class BuildUnit:
296 ## The constructor
297 #
298 # @param self The object pointer
299 # @param Obj The object the build is working on
300 # @param Target The build target name, one of gSupportedTarget
301 # @param Dependency The BuildUnit(s) which must be completed in advance
302 # @param WorkingDir The directory build command starts in
303 #
304 def __init__(self, Obj, BuildCommand, Target, Dependency, WorkingDir="."):
305 self.BuildObject = Obj
306 self.Dependency = Dependency
307 self.WorkingDir = WorkingDir
308 self.Target = Target
309 self.BuildCommand = BuildCommand
310 if not BuildCommand:
311 EdkLogger.error("build", OPTION_MISSING,
312 "No build command found for this module. "
313 "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %
314 (Obj.BuildTarget, Obj.ToolChain, Obj.Arch),
315 ExtraData=str(Obj))
316
317
318 ## str() method
319 #
320 # It just returns the string representation of self.BuildObject
321 #
322 # @param self The object pointer
323 #
324 def __str__(self):
325 return str(self.BuildObject)
326
327 ## "==" operator method
328 #
329 # It just compares self.BuildObject with "Other". So self.BuildObject must
330 # provide its own __eq__() method.
331 #
332 # @param self The object pointer
333 # @param Other The other BuildUnit object compared to
334 #
335 def __eq__(self, Other):
336 return Other != None and self.BuildObject == Other.BuildObject \
337 and self.BuildObject.Arch == Other.BuildObject.Arch
338
339 ## hash() method
340 #
341 # It just returns the hash value of self.BuildObject which must be hashable.
342 #
343 # @param self The object pointer
344 #
345 def __hash__(self):
346 return hash(self.BuildObject) + hash(self.BuildObject.Arch)
347
348 def __repr__(self):
349 return repr(self.BuildObject)
350
351 ## The smallest module unit that can be built by nmake/make command in multi-thread build mode
352 #
353 # This class is for module build by nmake/make build system. The "Obj" parameter
354 # must provide __str__(), __eq__() and __hash__() methods. Otherwise there could
355 # be make units missing build.
356 #
357 # Currently the "Obj" should be only ModuleAutoGen object.
358 #
359 class ModuleMakeUnit(BuildUnit):
360 ## The constructor
361 #
362 # @param self The object pointer
363 # @param Obj The ModuleAutoGen object the build is working on
364 # @param Target The build target name, one of gSupportedTarget
365 #
366 def __init__(self, Obj, Target):
367 Dependency = [ModuleMakeUnit(La, Target) for La in Obj.LibraryAutoGenList]
368 BuildUnit.__init__(self, Obj, Obj.BuildCommand, Target, Dependency, Obj.MakeFileDir)
369 if Target in [None, "", "all"]:
370 self.Target = "tbuild"
371
372 ## The smallest platform unit that can be built by nmake/make command in multi-thread build mode
373 #
374 # This class is for platform build by nmake/make build system. The "Obj" parameter
375 # must provide __str__(), __eq__() and __hash__() methods. Otherwise there could
376 # be make units missing build.
377 #
378 # Currently the "Obj" should be only PlatformAutoGen object.
379 #
380 class PlatformMakeUnit(BuildUnit):
381 ## The constructor
382 #
383 # @param self The object pointer
384 # @param Obj The PlatformAutoGen object the build is working on
385 # @param Target The build target name, one of gSupportedTarget
386 #
387 def __init__(self, Obj, Target):
388 Dependency = [ModuleMakeUnit(Lib, Target) for Lib in self.BuildObject.LibraryAutoGenList]
389 Dependency.extend([ModuleMakeUnit(Mod, Target) for Mod in self.BuildObject.ModuleAutoGenList])
390 BuildUnit.__init__(self, Obj, Obj.BuildCommand, Target, Dependency, Obj.MakeFileDir)
391
392 ## The class representing the task of a module build or platform build
393 #
394 # This class manages the build tasks in multi-thread build mode. Its jobs include
395 # scheduling thread running, catching thread error, monitor the thread status, etc.
396 #
397 class BuildTask:
398 # queue for tasks waiting for schedule
399 _PendingQueue = sdict()
400 _PendingQueueLock = threading.Lock()
401
402 # queue for tasks ready for running
403 _ReadyQueue = sdict()
404 _ReadyQueueLock = threading.Lock()
405
406 # queue for run tasks
407 _RunningQueue = sdict()
408 _RunningQueueLock = threading.Lock()
409
410 # queue containing all build tasks, in case duplicate build
411 _TaskQueue = sdict()
412
413 # flag indicating error occurs in a running thread
414 _ErrorFlag = threading.Event()
415 _ErrorFlag.clear()
416 _ErrorMessage = ""
417
418 # BoundedSemaphore object used to control the number of running threads
419 _Thread = None
420
421 # flag indicating if the scheduler is started or not
422 _SchedulerStopped = threading.Event()
423 _SchedulerStopped.set()
424
425 ## Start the task scheduler thread
426 #
427 # @param MaxThreadNumber The maximum thread number
428 # @param ExitFlag Flag used to end the scheduler
429 #
430 @staticmethod
431 def StartScheduler(MaxThreadNumber, ExitFlag):
432 SchedulerThread = Thread(target=BuildTask.Scheduler, args=(MaxThreadNumber, ExitFlag))
433 SchedulerThread.setName("Build-Task-Scheduler")
434 SchedulerThread.setDaemon(False)
435 SchedulerThread.start()
436 # wait for the scheduler to be started, especially useful in Linux
437 while not BuildTask.IsOnGoing():
438 time.sleep(0.01)
439
440 ## Scheduler method
441 #
442 # @param MaxThreadNumber The maximum thread number
443 # @param ExitFlag Flag used to end the scheduler
444 #
445 @staticmethod
446 def Scheduler(MaxThreadNumber, ExitFlag):
447 BuildTask._SchedulerStopped.clear()
448 try:
449 # use BoundedSemaphore to control the maximum running threads
450 BuildTask._Thread = BoundedSemaphore(MaxThreadNumber)
451 #
452 # scheduling loop, which will exits when no pending/ready task and
453 # indicated to do so, or there's error in running thread
454 #
455 while (len(BuildTask._PendingQueue) > 0 or len(BuildTask._ReadyQueue) > 0 \
456 or not ExitFlag.isSet()) and not BuildTask._ErrorFlag.isSet():
457 EdkLogger.debug(EdkLogger.DEBUG_8, "Pending Queue (%d), Ready Queue (%d)"
458 % (len(BuildTask._PendingQueue), len(BuildTask._ReadyQueue)))
459
460 # get all pending tasks
461 BuildTask._PendingQueueLock.acquire()
462 BuildObjectList = BuildTask._PendingQueue.keys()
463 #
464 # check if their dependency is resolved, and if true, move them
465 # into ready queue
466 #
467 for BuildObject in BuildObjectList:
468 Bt = BuildTask._PendingQueue[BuildObject]
469 if Bt.IsReady():
470 BuildTask._ReadyQueue[BuildObject] = BuildTask._PendingQueue.pop(BuildObject)
471 BuildTask._PendingQueueLock.release()
472
473 # launch build thread until the maximum number of threads is reached
474 while not BuildTask._ErrorFlag.isSet():
475 # empty ready queue, do nothing further
476 if len(BuildTask._ReadyQueue) == 0:
477 break
478
479 # wait for active thread(s) exit
480 BuildTask._Thread.acquire(True)
481
482 # start a new build thread
483 Bo = BuildTask._ReadyQueue.keys()[0]
484 Bt = BuildTask._ReadyQueue.pop(Bo)
485
486 # move into running queue
487 BuildTask._RunningQueueLock.acquire()
488 BuildTask._RunningQueue[Bo] = Bt
489 BuildTask._RunningQueueLock.release()
490
491 Bt.Start()
492 # avoid tense loop
493 time.sleep(0.01)
494
495 # avoid tense loop
496 time.sleep(0.01)
497
498 # wait for all running threads exit
499 if BuildTask._ErrorFlag.isSet():
500 EdkLogger.quiet("\nWaiting for all build threads exit...")
501 # while not BuildTask._ErrorFlag.isSet() and \
502 while len(BuildTask._RunningQueue) > 0:
503 EdkLogger.verbose("Waiting for thread ending...(%d)" % len(BuildTask._RunningQueue))
504 EdkLogger.debug(EdkLogger.DEBUG_8, "Threads [%s]" % ", ".join([Th.getName() for Th in threading.enumerate()]))
505 # avoid tense loop
506 time.sleep(0.1)
507 except BaseException, X:
508 #
509 # TRICK: hide the output of threads left runing, so that the user can
510 # catch the error message easily
511 #
512 EdkLogger.SetLevel(EdkLogger.ERROR)
513 BuildTask._ErrorFlag.set()
514 BuildTask._ErrorMessage = "build thread scheduler error\n\t%s" % str(X)
515
516 BuildTask._PendingQueue.clear()
517 BuildTask._ReadyQueue.clear()
518 BuildTask._RunningQueue.clear()
519 BuildTask._TaskQueue.clear()
520 BuildTask._SchedulerStopped.set()
521
522 ## Wait for all running method exit
523 #
524 @staticmethod
525 def WaitForComplete():
526 BuildTask._SchedulerStopped.wait()
527
528 ## Check if the scheduler is running or not
529 #
530 @staticmethod
531 def IsOnGoing():
532 return not BuildTask._SchedulerStopped.isSet()
533
534 ## Abort the build
535 @staticmethod
536 def Abort():
537 if BuildTask.IsOnGoing():
538 BuildTask._ErrorFlag.set()
539 BuildTask.WaitForComplete()
540
541 ## Check if there's error in running thread
542 #
543 # Since the main thread cannot catch exceptions in other thread, we have to
544 # use threading.Event to communicate this formation to main thread.
545 #
546 @staticmethod
547 def HasError():
548 return BuildTask._ErrorFlag.isSet()
549
550 ## Get error message in running thread
551 #
552 # Since the main thread cannot catch exceptions in other thread, we have to
553 # use a static variable to communicate this message to main thread.
554 #
555 @staticmethod
556 def GetErrorMessage():
557 return BuildTask._ErrorMessage
558
559 ## Factory method to create a BuildTask object
560 #
561 # This method will check if a module is building or has been built. And if
562 # true, just return the associated BuildTask object in the _TaskQueue. If
563 # not, create and return a new BuildTask object. The new BuildTask object
564 # will be appended to the _PendingQueue for scheduling later.
565 #
566 # @param BuildItem A BuildUnit object representing a build object
567 # @param Dependency The dependent build object of BuildItem
568 #
569 @staticmethod
570 def New(BuildItem, Dependency=None):
571 if BuildItem in BuildTask._TaskQueue:
572 Bt = BuildTask._TaskQueue[BuildItem]
573 return Bt
574
575 Bt = BuildTask()
576 Bt._Init(BuildItem, Dependency)
577 BuildTask._TaskQueue[BuildItem] = Bt
578
579 BuildTask._PendingQueueLock.acquire()
580 BuildTask._PendingQueue[BuildItem] = Bt
581 BuildTask._PendingQueueLock.release()
582
583 return Bt
584
585 ## The real constructor of BuildTask
586 #
587 # @param BuildItem A BuildUnit object representing a build object
588 # @param Dependency The dependent build object of BuildItem
589 #
590 def _Init(self, BuildItem, Dependency=None):
591 self.BuildItem = BuildItem
592
593 self.DependencyList = []
594 if Dependency == None:
595 Dependency = BuildItem.Dependency
596 else:
597 Dependency.extend(BuildItem.Dependency)
598 self.AddDependency(Dependency)
599 # flag indicating build completes, used to avoid unnecessary re-build
600 self.CompleteFlag = False
601
602 ## Check if all dependent build tasks are completed or not
603 #
604 def IsReady(self):
605 ReadyFlag = True
606 for Dep in self.DependencyList:
607 if Dep.CompleteFlag == True:
608 continue
609 ReadyFlag = False
610 break
611
612 return ReadyFlag
613
614 ## Add dependent build task
615 #
616 # @param Dependency The list of dependent build objects
617 #
618 def AddDependency(self, Dependency):
619 for Dep in Dependency:
620 self.DependencyList.append(BuildTask.New(Dep)) # BuildTask list
621
622 ## The thread wrapper of LaunchCommand function
623 #
624 # @param Command A list or string contains the call of the command
625 # @param WorkingDir The directory in which the program will be running
626 #
627 def _CommandThread(self, Command, WorkingDir):
628 try:
629 LaunchCommand(Command, WorkingDir)
630 self.CompleteFlag = True
631 except:
632 #
633 # TRICK: hide the output of threads left runing, so that the user can
634 # catch the error message easily
635 #
636 if not BuildTask._ErrorFlag.isSet():
637 GlobalData.gBuildingModule = "%s [%s, %s, %s]" % (str(self.BuildItem.BuildObject),
638 self.BuildItem.BuildObject.Arch,
639 self.BuildItem.BuildObject.ToolChain,
640 self.BuildItem.BuildObject.BuildTarget
641 )
642 EdkLogger.SetLevel(EdkLogger.ERROR)
643 BuildTask._ErrorFlag.set()
644 BuildTask._ErrorMessage = "%s broken\n %s [%s]" % \
645 (threading.currentThread().getName(), Command, WorkingDir)
646 # indicate there's a thread is available for another build task
647 BuildTask._RunningQueueLock.acquire()
648 BuildTask._RunningQueue.pop(self.BuildItem)
649 BuildTask._RunningQueueLock.release()
650 BuildTask._Thread.release()
651
652 ## Start build task thread
653 #
654 def Start(self):
655 EdkLogger.quiet("Building ... %s" % repr(self.BuildItem))
656 Command = self.BuildItem.BuildCommand + [self.BuildItem.Target]
657 self.BuildTread = Thread(target=self._CommandThread, args=(Command, self.BuildItem.WorkingDir))
658 self.BuildTread.setName("build thread")
659 self.BuildTread.setDaemon(False)
660 self.BuildTread.start()
661
662 ## The class contains the information related to EFI image
663 #
664 class PeImageInfo():
665 ## Constructor
666 #
667 # Constructor will load all required image information.
668 #
669 # @param BaseName The full file path of image.
670 # @param Guid The GUID for image.
671 # @param Arch Arch of this image.
672 # @param OutputDir The output directory for image.
673 # @param DebugDir The debug directory for image.
674 # @param ImageClass PeImage Information
675 #
676 def __init__(self, BaseName, Guid, Arch, OutputDir, DebugDir, ImageClass):
677 self.BaseName = BaseName
678 self.Guid = Guid
679 self.Arch = Arch
680 self.OutputDir = OutputDir
681 self.DebugDir = DebugDir
682 self.Image = ImageClass
683 self.Image.Size = (self.Image.Size / 0x1000 + 1) * 0x1000
684
685 ## The class implementing the EDK2 build process
686 #
687 # The build process includes:
688 # 1. Load configuration from target.txt and tools_def.txt in $(WORKSPACE)/Conf
689 # 2. Parse DSC file of active platform
690 # 3. Parse FDF file if any
691 # 4. Establish build database, including parse all other files (module, package)
692 # 5. Create AutoGen files (C code file, depex file, makefile) if necessary
693 # 6. Call build command
694 #
695 class Build():
696 ## Constructor
697 #
698 # Constructor will load all necessary configurations, parse platform, modules
699 # and packages and the establish a database for AutoGen.
700 #
701 # @param Target The build command target, one of gSupportedTarget
702 # @param WorkspaceDir The directory of workspace
703 # @param BuildOptions Build options passed from command line
704 #
705 def __init__(self, Target, WorkspaceDir, BuildOptions):
706 self.WorkspaceDir = WorkspaceDir
707 self.Target = Target
708 self.PlatformFile = BuildOptions.PlatformFile
709 self.ModuleFile = BuildOptions.ModuleFile
710 self.ArchList = BuildOptions.TargetArch
711 self.ToolChainList = BuildOptions.ToolChain
712 self.BuildTargetList= BuildOptions.BuildTarget
713 self.Fdf = BuildOptions.FdfFile
714 self.FdList = BuildOptions.RomImage
715 self.FvList = BuildOptions.FvImage
716 self.CapList = BuildOptions.CapName
717 self.SilentMode = BuildOptions.SilentMode
718 self.ThreadNumber = BuildOptions.ThreadNumber
719 self.SkipAutoGen = BuildOptions.SkipAutoGen
720 self.Reparse = BuildOptions.Reparse
721 self.SkuId = BuildOptions.SkuId
722 self.SpawnMode = True
723 self.BuildReport = BuildReport(BuildOptions.ReportFile, BuildOptions.ReportType)
724 self.TargetTxt = TargetTxtClassObject()
725 self.ToolDef = ToolDefClassObject()
726 if BuildOptions.DisableCache:
727 self.Db = WorkspaceDatabase(":memory:")
728 else:
729 self.Db = WorkspaceDatabase(None, self.Reparse)
730 self.BuildDatabase = self.Db.BuildObject
731 self.Platform = None
732 self.LoadFixAddress = 0
733 self.UniFlag = BuildOptions.Flag
734
735 # print dot character during doing some time-consuming work
736 self.Progress = Utils.Progressor()
737
738 self.InitBuild()
739
740 # print current build environment and configuration
741 EdkLogger.quiet("%-16s = %s" % ("WORKSPACE", os.environ["WORKSPACE"]))
742 EdkLogger.quiet("%-16s = %s" % ("ECP_SOURCE", os.environ["ECP_SOURCE"]))
743 EdkLogger.quiet("%-16s = %s" % ("EDK_SOURCE", os.environ["EDK_SOURCE"]))
744 EdkLogger.quiet("%-16s = %s" % ("EFI_SOURCE", os.environ["EFI_SOURCE"]))
745 EdkLogger.quiet("%-16s = %s" % ("EDK_TOOLS_PATH", os.environ["EDK_TOOLS_PATH"]))
746
747 EdkLogger.info("")
748
749 os.chdir(self.WorkspaceDir)
750
751 ## Load configuration
752 #
753 # This method will parse target.txt and get the build configurations.
754 #
755 def LoadConfiguration(self):
756 #
757 # Check target.txt and tools_def.txt and Init them
758 #
759 BuildConfigurationFile = os.path.normpath(os.path.join(self.WorkspaceDir, gBuildConfiguration))
760 if os.path.isfile(BuildConfigurationFile) == True:
761 StatusCode = self.TargetTxt.LoadTargetTxtFile(BuildConfigurationFile)
762
763 ToolDefinitionFile = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF]
764 if ToolDefinitionFile == '':
765 ToolDefinitionFile = gToolsDefinition
766 ToolDefinitionFile = os.path.normpath(os.path.join(self.WorkspaceDir, ToolDefinitionFile))
767 if os.path.isfile(ToolDefinitionFile) == True:
768 StatusCode = self.ToolDef.LoadToolDefFile(ToolDefinitionFile)
769 else:
770 EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=ToolDefinitionFile)
771 else:
772 EdkLogger.error("build", FILE_NOT_FOUND, ExtraData=BuildConfigurationFile)
773
774 # if no ARCH given in command line, get it from target.txt
775 if not self.ArchList:
776 self.ArchList = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET_ARCH]
777 self.ArchList = tuple(self.ArchList)
778
779 # if no build target given in command line, get it from target.txt
780 if not self.BuildTargetList:
781 self.BuildTargetList = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]
782
783 # if no tool chain given in command line, get it from target.txt
784 if not self.ToolChainList:
785 self.ToolChainList = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]
786 if self.ToolChainList == None or len(self.ToolChainList) == 0:
787 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE, ExtraData="No toolchain given. Don't know how to build.\n")
788
789 # check if the tool chains are defined or not
790 NewToolChainList = []
791 for ToolChain in self.ToolChainList:
792 if ToolChain not in self.ToolDef.ToolsDefTxtDatabase[TAB_TOD_DEFINES_TOOL_CHAIN_TAG]:
793 EdkLogger.warn("build", "Tool chain [%s] is not defined" % ToolChain)
794 else:
795 NewToolChainList.append(ToolChain)
796 # if no tool chain available, break the build
797 if len(NewToolChainList) == 0:
798 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
799 ExtraData="[%s] not defined. No toolchain available for build!\n" % ", ".join(self.ToolChainList))
800 else:
801 self.ToolChainList = NewToolChainList
802
803 if self.ThreadNumber == None:
804 self.ThreadNumber = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]
805 if self.ThreadNumber == '':
806 self.ThreadNumber = 0
807 else:
808 self.ThreadNumber = int(self.ThreadNumber, 0)
809
810 if self.ThreadNumber == 0:
811 self.ThreadNumber = 1
812
813 if not self.PlatformFile:
814 PlatformFile = self.TargetTxt.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM]
815 if not PlatformFile:
816 # Try to find one in current directory
817 WorkingDirectory = os.getcwd()
818 FileList = glob.glob(os.path.normpath(os.path.join(WorkingDirectory, '*.dsc')))
819 FileNum = len(FileList)
820 if FileNum >= 2:
821 EdkLogger.error("build", OPTION_MISSING,
822 ExtraData="There are %d DSC files in %s. Use '-p' to specify one.\n" % (FileNum, WorkingDirectory))
823 elif FileNum == 1:
824 PlatformFile = FileList[0]
825 else:
826 EdkLogger.error("build", RESOURCE_NOT_AVAILABLE,
827 ExtraData="No active platform specified in target.txt or command line! Nothing can be built.\n")
828
829 self.PlatformFile = PathClass(NormFile(PlatformFile, self.WorkspaceDir), self.WorkspaceDir)
830
831 ## Initialize build configuration
832 #
833 # This method will parse DSC file and merge the configurations from
834 # command line and target.txt, then get the final build configurations.
835 #
836 def InitBuild(self):
837 # parse target.txt, tools_def.txt, and platform file
838 self.LoadConfiguration()
839
840 # Allow case-insensitive for those from command line or configuration file
841 ErrorCode, ErrorInfo = self.PlatformFile.Validate(".dsc", False)
842 if ErrorCode != 0:
843 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
844
845 # create metafile database
846 self.Db.InitDatabase()
847
848 ## Build a module or platform
849 #
850 # Create autogen code and makefile for a module or platform, and the launch
851 # "make" command to build it
852 #
853 # @param Target The target of build command
854 # @param Platform The platform file
855 # @param Module The module file
856 # @param BuildTarget The name of build target, one of "DEBUG", "RELEASE"
857 # @param ToolChain The name of toolchain to build
858 # @param Arch The arch of the module/platform
859 # @param CreateDepModuleCodeFile Flag used to indicate creating code
860 # for dependent modules/Libraries
861 # @param CreateDepModuleMakeFile Flag used to indicate creating makefile
862 # for dependent modules/Libraries
863 #
864 def _Build(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True):
865 if AutoGenObject == None:
866 return False
867
868 # skip file generation for cleanxxx targets, run and fds target
869 if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:
870 # for target which must generate AutoGen code and makefile
871 if not self.SkipAutoGen or Target == 'genc':
872 self.Progress.Start("Generating code")
873 AutoGenObject.CreateCodeFile(CreateDepsCodeFile)
874 self.Progress.Stop("done!")
875 if Target == "genc":
876 return True
877
878 if not self.SkipAutoGen or Target == 'genmake':
879 self.Progress.Start("Generating makefile")
880 AutoGenObject.CreateMakeFile(CreateDepsMakeFile)
881 AutoGenObject.CreateAsBuiltInf()
882 self.Progress.Stop("done!")
883 if Target == "genmake":
884 return True
885 else:
886 # always recreate top/platform makefile when clean, just in case of inconsistency
887 AutoGenObject.CreateCodeFile(False)
888 AutoGenObject.CreateMakeFile(False)
889
890 if EdkLogger.GetLevel() == EdkLogger.QUIET:
891 EdkLogger.quiet("Building ... %s" % repr(AutoGenObject))
892
893 BuildCommand = AutoGenObject.BuildCommand
894 if BuildCommand == None or len(BuildCommand) == 0:
895 EdkLogger.error("build", OPTION_MISSING,
896 "No build command found for this module. "
897 "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %
898 (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch),
899 ExtraData=str(AutoGenObject))
900
901 BuildCommand = BuildCommand + [Target]
902 LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)
903 if Target == 'cleanall':
904 try:
905 #os.rmdir(AutoGenObject.BuildDir)
906 RemoveDirectory(AutoGenObject.BuildDir, True)
907 #
908 # First should close DB.
909 #
910 self.Db.Close()
911 RemoveDirectory(gBuildCacheDir, True)
912 except WindowsError, X:
913 EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X))
914 return True
915
916 ## Rebase module image and Get function address for the input module list.
917 #
918 def _RebaseModule (self, MapBuffer, BaseAddress, ModuleList, AddrIsOffset = True, ModeIsSmm = False):
919 if ModeIsSmm:
920 AddrIsOffset = False
921 InfFileNameList = ModuleList.keys()
922 #InfFileNameList.sort()
923 for InfFile in InfFileNameList:
924 sys.stdout.write (".")
925 sys.stdout.flush()
926 ModuleInfo = ModuleList[InfFile]
927 ModuleName = ModuleInfo.BaseName
928 ModuleOutputImage = ModuleInfo.Image.FileName
929 ModuleDebugImage = os.path.join(ModuleInfo.DebugDir, ModuleInfo.BaseName + '.efi')
930 ## for SMM module in SMRAM, the SMRAM will be allocated from base to top.
931 if not ModeIsSmm:
932 BaseAddress = BaseAddress - ModuleInfo.Image.Size
933 #
934 # Update Image to new BaseAddress by GenFw tool
935 #
936 LaunchCommand(["GenFw", "--rebase", str(BaseAddress), "-r", ModuleOutputImage], ModuleInfo.OutputDir)
937 LaunchCommand(["GenFw", "--rebase", str(BaseAddress), "-r", ModuleDebugImage], ModuleInfo.DebugDir)
938 else:
939 #
940 # Set new address to the section header only for SMM driver.
941 #
942 LaunchCommand(["GenFw", "--address", str(BaseAddress), "-r", ModuleOutputImage], ModuleInfo.OutputDir)
943 LaunchCommand(["GenFw", "--address", str(BaseAddress), "-r", ModuleDebugImage], ModuleInfo.DebugDir)
944 #
945 # Collect funtion address from Map file
946 #
947 ImageMapTable = ModuleOutputImage.replace('.efi', '.map')
948 FunctionList = []
949 if os.path.exists(ImageMapTable):
950 OrigImageBaseAddress = 0
951 ImageMap = open (ImageMapTable, 'r')
952 for LinStr in ImageMap:
953 if len (LinStr.strip()) == 0:
954 continue
955 #
956 # Get the preferred address set on link time.
957 #
958 if LinStr.find ('Preferred load address is') != -1:
959 StrList = LinStr.split()
960 OrigImageBaseAddress = int (StrList[len(StrList) - 1], 16)
961
962 StrList = LinStr.split()
963 if len (StrList) > 4:
964 if StrList[3] == 'f' or StrList[3] =='F':
965 Name = StrList[1]
966 RelativeAddress = int (StrList[2], 16) - OrigImageBaseAddress
967 FunctionList.append ((Name, RelativeAddress))
968 if ModuleInfo.Arch == 'IPF' and Name.endswith('_ModuleEntryPoint'):
969 #
970 # Get the real entry point address for IPF image.
971 #
972 ModuleInfo.Image.EntryPoint = RelativeAddress
973 ImageMap.close()
974 #
975 # Add general information.
976 #
977 if ModeIsSmm:
978 MapBuffer.write('\n\n%s (Fixed SMRAM Offset, BaseAddress=0x%010X, EntryPoint=0x%010X)\n' % (ModuleName, BaseAddress, BaseAddress + ModuleInfo.Image.EntryPoint))
979 elif AddrIsOffset:
980 MapBuffer.write('\n\n%s (Fixed Memory Offset, BaseAddress=-0x%010X, EntryPoint=-0x%010X)\n' % (ModuleName, 0 - BaseAddress, 0 - (BaseAddress + ModuleInfo.Image.EntryPoint)))
981 else:
982 MapBuffer.write('\n\n%s (Fixed Memory Address, BaseAddress=0x%010X, EntryPoint=0x%010X)\n' % (ModuleName, BaseAddress, BaseAddress + ModuleInfo.Image.EntryPoint))
983 #
984 # Add guid and general seciton section.
985 #
986 TextSectionAddress = 0
987 DataSectionAddress = 0
988 for SectionHeader in ModuleInfo.Image.SectionHeaderList:
989 if SectionHeader[0] == '.text':
990 TextSectionAddress = SectionHeader[1]
991 elif SectionHeader[0] in ['.data', '.sdata']:
992 DataSectionAddress = SectionHeader[1]
993 if AddrIsOffset:
994 MapBuffer.write('(GUID=%s, .textbaseaddress=-0x%010X, .databaseaddress=-0x%010X)\n' % (ModuleInfo.Guid, 0 - (BaseAddress + TextSectionAddress), 0 - (BaseAddress + DataSectionAddress)))
995 else:
996 MapBuffer.write('(GUID=%s, .textbaseaddress=0x%010X, .databaseaddress=0x%010X)\n' % (ModuleInfo.Guid, BaseAddress + TextSectionAddress, BaseAddress + DataSectionAddress))
997 #
998 # Add debug image full path.
999 #
1000 MapBuffer.write('(IMAGE=%s)\n\n' % (ModuleDebugImage))
1001 #
1002 # Add funtion address
1003 #
1004 for Function in FunctionList:
1005 if AddrIsOffset:
1006 MapBuffer.write(' -0x%010X %s\n' % (0 - (BaseAddress + Function[1]), Function[0]))
1007 else:
1008 MapBuffer.write(' 0x%010X %s\n' % (BaseAddress + Function[1], Function[0]))
1009 ImageMap.close()
1010
1011 #
1012 # for SMM module in SMRAM, the SMRAM will be allocated from base to top.
1013 #
1014 if ModeIsSmm:
1015 BaseAddress = BaseAddress + ModuleInfo.Image.Size
1016
1017 ## Collect MAP information of all FVs
1018 #
1019 def _CollectFvMapBuffer (self, MapBuffer, Wa, ModuleList):
1020 if self.Fdf:
1021 # First get the XIP base address for FV map file.
1022 GuidPattern = re.compile("[-a-fA-F0-9]+")
1023 GuidName = re.compile("\(GUID=[-a-fA-F0-9]+")
1024 for FvName in Wa.FdfProfile.FvDict.keys():
1025 FvMapBuffer = os.path.join(Wa.FvDir, FvName + '.Fv.map')
1026 if not os.path.exists(FvMapBuffer):
1027 continue
1028 FvMap = open (FvMapBuffer, 'r')
1029 #skip FV size information
1030 FvMap.readline()
1031 FvMap.readline()
1032 FvMap.readline()
1033 FvMap.readline()
1034 for Line in FvMap:
1035 MatchGuid = GuidPattern.match(Line)
1036 if MatchGuid != None:
1037 #
1038 # Replace GUID with module name
1039 #
1040 GuidString = MatchGuid.group()
1041 if GuidString.upper() in ModuleList:
1042 Line = Line.replace(GuidString, ModuleList[GuidString.upper()].Name)
1043 MapBuffer.write('%s' % (Line))
1044 #
1045 # Add the debug image full path.
1046 #
1047 MatchGuid = GuidName.match(Line)
1048 if MatchGuid != None:
1049 GuidString = MatchGuid.group().split("=")[1]
1050 if GuidString.upper() in ModuleList:
1051 MapBuffer.write('(IMAGE=%s)\n' % (os.path.join(ModuleList[GuidString.upper()].DebugDir, ModuleList[GuidString.upper()].Name + '.efi')))
1052
1053 FvMap.close()
1054
1055 ## Collect MAP information of all modules
1056 #
1057 def _CollectModuleMapBuffer (self, MapBuffer, ModuleList):
1058 sys.stdout.write ("Generate Load Module At Fix Address Map")
1059 sys.stdout.flush()
1060 PatchEfiImageList = []
1061 PeiModuleList = {}
1062 BtModuleList = {}
1063 RtModuleList = {}
1064 SmmModuleList = {}
1065 PeiSize = 0
1066 BtSize = 0
1067 RtSize = 0
1068 # reserve 4K size in SMRAM to make SMM module address not from 0.
1069 SmmSize = 0x1000
1070 IsIpfPlatform = False
1071 if 'IPF' in self.ArchList:
1072 IsIpfPlatform = True
1073 for ModuleGuid in ModuleList:
1074 Module = ModuleList[ModuleGuid]
1075 GlobalData.gProcessingFile = "%s [%s, %s, %s]" % (Module.MetaFile, Module.Arch, Module.ToolChain, Module.BuildTarget)
1076
1077 OutputImageFile = ''
1078 for ResultFile in Module.CodaTargetList:
1079 if str(ResultFile.Target).endswith('.efi'):
1080 #
1081 # module list for PEI, DXE, RUNTIME and SMM
1082 #
1083 OutputImageFile = os.path.join(Module.OutputDir, Module.Name + '.efi')
1084 ImageClass = PeImageClass (OutputImageFile)
1085 if not ImageClass.IsValid:
1086 EdkLogger.error("build", FILE_PARSE_FAILURE, ExtraData=ImageClass.ErrorInfo)
1087 ImageInfo = PeImageInfo(Module.Name, Module.Guid, Module.Arch, Module.OutputDir, Module.DebugDir, ImageClass)
1088 if Module.ModuleType in ['PEI_CORE', 'PEIM', 'COMBINED_PEIM_DRIVER','PIC_PEIM', 'RELOCATABLE_PEIM', 'DXE_CORE']:
1089 PeiModuleList[Module.MetaFile] = ImageInfo
1090 PeiSize += ImageInfo.Image.Size
1091 elif Module.ModuleType in ['BS_DRIVER', 'DXE_DRIVER', 'UEFI_DRIVER']:
1092 BtModuleList[Module.MetaFile] = ImageInfo
1093 BtSize += ImageInfo.Image.Size
1094 elif Module.ModuleType in ['DXE_RUNTIME_DRIVER', 'RT_DRIVER', 'DXE_SAL_DRIVER', 'SAL_RT_DRIVER']:
1095 RtModuleList[Module.MetaFile] = ImageInfo
1096 #IPF runtime driver needs to be at 2 page alignment.
1097 if IsIpfPlatform and ImageInfo.Image.Size % 0x2000 != 0:
1098 ImageInfo.Image.Size = (ImageInfo.Image.Size / 0x2000 + 1) * 0x2000
1099 RtSize += ImageInfo.Image.Size
1100 elif Module.ModuleType in ['SMM_CORE', 'DXE_SMM_DRIVER']:
1101 SmmModuleList[Module.MetaFile] = ImageInfo
1102 SmmSize += ImageInfo.Image.Size
1103 if Module.ModuleType == 'DXE_SMM_DRIVER':
1104 PiSpecVersion = '0x00000000'
1105 if 'PI_SPECIFICATION_VERSION' in Module.Module.Specification:
1106 PiSpecVersion = Module.Module.Specification['PI_SPECIFICATION_VERSION']
1107 # for PI specification < PI1.1, DXE_SMM_DRIVER also runs as BOOT time driver.
1108 if int(PiSpecVersion, 16) < 0x0001000A:
1109 BtModuleList[Module.MetaFile] = ImageInfo
1110 BtSize += ImageInfo.Image.Size
1111 break
1112 #
1113 # EFI image is final target.
1114 # Check EFI image contains patchable FixAddress related PCDs.
1115 #
1116 if OutputImageFile != '':
1117 ModuleIsPatch = False
1118 for Pcd in Module.ModulePcdList:
1119 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE and Pcd.TokenCName in TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_LIST:
1120 ModuleIsPatch = True
1121 break
1122 if not ModuleIsPatch:
1123 for Pcd in Module.LibraryPcdList:
1124 if Pcd.Type == TAB_PCDS_PATCHABLE_IN_MODULE and Pcd.TokenCName in TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_LIST:
1125 ModuleIsPatch = True
1126 break
1127
1128 if not ModuleIsPatch:
1129 continue
1130 #
1131 # Module includes the patchable load fix address PCDs.
1132 # It will be fixed up later.
1133 #
1134 PatchEfiImageList.append (OutputImageFile)
1135
1136 #
1137 # Get Top Memory address
1138 #
1139 ReservedRuntimeMemorySize = 0
1140 TopMemoryAddress = 0
1141 if self.LoadFixAddress == 0xFFFFFFFFFFFFFFFF:
1142 TopMemoryAddress = 0
1143 else:
1144 TopMemoryAddress = self.LoadFixAddress
1145 if TopMemoryAddress < RtSize + BtSize + PeiSize:
1146 EdkLogger.error("build", PARAMETER_INVALID, "FIX_LOAD_TOP_MEMORY_ADDRESS is too low to load driver")
1147 # Make IPF runtime driver at 2 page alignment.
1148 if IsIpfPlatform:
1149 ReservedRuntimeMemorySize = TopMemoryAddress % 0x2000
1150 RtSize = RtSize + ReservedRuntimeMemorySize
1151
1152 #
1153 # Patch FixAddress related PCDs into EFI image
1154 #
1155 for EfiImage in PatchEfiImageList:
1156 EfiImageMap = EfiImage.replace('.efi', '.map')
1157 if not os.path.exists(EfiImageMap):
1158 continue
1159 #
1160 # Get PCD offset in EFI image by GenPatchPcdTable function
1161 #
1162 PcdTable = parsePcdInfoFromMapFile(EfiImageMap, EfiImage)
1163 #
1164 # Patch real PCD value by PatchPcdValue tool
1165 #
1166 for PcdInfo in PcdTable:
1167 ReturnValue = 0
1168 if PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE:
1169 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_PEI_PAGE_SIZE_DATA_TYPE, str (PeiSize/0x1000))
1170 elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE:
1171 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_DXE_PAGE_SIZE_DATA_TYPE, str (BtSize/0x1000))
1172 elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE:
1173 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_RUNTIME_PAGE_SIZE_DATA_TYPE, str (RtSize/0x1000))
1174 elif PcdInfo[0] == TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE and len (SmmModuleList) > 0:
1175 ReturnValue, ErrorInfo = PatchBinaryFile (EfiImage, PcdInfo[1], TAB_PCDS_PATCHABLE_LOAD_FIX_ADDRESS_SMM_PAGE_SIZE_DATA_TYPE, str (SmmSize/0x1000))
1176 if ReturnValue != 0:
1177 EdkLogger.error("build", PARAMETER_INVALID, "Patch PCD value failed", ExtraData=ErrorInfo)
1178
1179 MapBuffer.write('PEI_CODE_PAGE_NUMBER = 0x%x\n' % (PeiSize/0x1000))
1180 MapBuffer.write('BOOT_CODE_PAGE_NUMBER = 0x%x\n' % (BtSize/0x1000))
1181 MapBuffer.write('RUNTIME_CODE_PAGE_NUMBER = 0x%x\n' % (RtSize/0x1000))
1182 if len (SmmModuleList) > 0:
1183 MapBuffer.write('SMM_CODE_PAGE_NUMBER = 0x%x\n' % (SmmSize/0x1000))
1184
1185 PeiBaseAddr = TopMemoryAddress - RtSize - BtSize
1186 BtBaseAddr = TopMemoryAddress - RtSize
1187 RtBaseAddr = TopMemoryAddress - ReservedRuntimeMemorySize
1188
1189 self._RebaseModule (MapBuffer, PeiBaseAddr, PeiModuleList, TopMemoryAddress == 0)
1190 self._RebaseModule (MapBuffer, BtBaseAddr, BtModuleList, TopMemoryAddress == 0)
1191 self._RebaseModule (MapBuffer, RtBaseAddr, RtModuleList, TopMemoryAddress == 0)
1192 self._RebaseModule (MapBuffer, 0x1000, SmmModuleList, AddrIsOffset = False, ModeIsSmm = True)
1193 MapBuffer.write('\n\n')
1194 sys.stdout.write ("\n")
1195 sys.stdout.flush()
1196
1197 ## Save platform Map file
1198 #
1199 def _SaveMapFile (self, MapBuffer, Wa):
1200 #
1201 # Map file path is got.
1202 #
1203 MapFilePath = os.path.join(Wa.BuildDir, Wa.Name + '.map')
1204 #
1205 # Save address map into MAP file.
1206 #
1207 SaveFileOnChange(MapFilePath, MapBuffer.getvalue(), False)
1208 MapBuffer.close()
1209 if self.LoadFixAddress != 0:
1210 sys.stdout.write ("\nLoad Module At Fix Address Map file can be found at %s\n" %(MapFilePath))
1211 sys.stdout.flush()
1212
1213 ## Build active platform for different build targets and different tool chains
1214 #
1215 def _BuildPlatform(self):
1216 for BuildTarget in self.BuildTargetList:
1217 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
1218 for ToolChain in self.ToolChainList:
1219 GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain
1220 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain
1221 Wa = WorkspaceAutoGen(
1222 self.WorkspaceDir,
1223 self.PlatformFile,
1224 BuildTarget,
1225 ToolChain,
1226 self.ArchList,
1227 self.BuildDatabase,
1228 self.TargetTxt,
1229 self.ToolDef,
1230 self.Fdf,
1231 self.FdList,
1232 self.FvList,
1233 self.CapList,
1234 self.SkuId,
1235 self.UniFlag,
1236 self.Progress
1237 )
1238 self.Fdf = Wa.FdfFile
1239 self.LoadFixAddress = Wa.Platform.LoadFixAddress
1240 self.BuildReport.AddPlatformReport(Wa)
1241 self.Progress.Stop("done!")
1242 self._Build(self.Target, Wa)
1243
1244 # Create MAP file when Load Fix Address is enabled.
1245 if self.Target in ["", "all", "fds"]:
1246 for Arch in Wa.ArchList:
1247 GlobalData.gGlobalDefines['ARCH'] = Arch
1248 #
1249 # Check whether the set fix address is above 4G for 32bit image.
1250 #
1251 if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000:
1252 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")
1253 #
1254 # Get Module List
1255 #
1256 ModuleList = {}
1257 for Pa in Wa.AutoGenObjectList:
1258 for Ma in Pa.ModuleAutoGenList:
1259 if Ma == None:
1260 continue
1261 if not Ma.IsLibrary:
1262 ModuleList[Ma.Guid.upper()] = Ma
1263
1264 MapBuffer = StringIO('')
1265 if self.LoadFixAddress != 0:
1266 #
1267 # Rebase module to the preferred memory address before GenFds
1268 #
1269 self._CollectModuleMapBuffer(MapBuffer, ModuleList)
1270 if self.Fdf:
1271 #
1272 # create FDS again for the updated EFI image
1273 #
1274 self._Build("fds", Wa)
1275 if self.Fdf:
1276 #
1277 # Create MAP file for all platform FVs after GenFds.
1278 #
1279 self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList)
1280 #
1281 # Save MAP buffer into MAP file.
1282 #
1283 self._SaveMapFile (MapBuffer, Wa)
1284
1285 ## Build active module for different build targets, different tool chains and different archs
1286 #
1287 def _BuildModule(self):
1288 for BuildTarget in self.BuildTargetList:
1289 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
1290 for ToolChain in self.ToolChainList:
1291 GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain
1292 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain
1293 #
1294 # module build needs platform build information, so get platform
1295 # AutoGen first
1296 #
1297 Wa = WorkspaceAutoGen(
1298 self.WorkspaceDir,
1299 self.PlatformFile,
1300 BuildTarget,
1301 ToolChain,
1302 self.ArchList,
1303 self.BuildDatabase,
1304 self.TargetTxt,
1305 self.ToolDef,
1306 self.Fdf,
1307 self.FdList,
1308 self.FvList,
1309 self.CapList,
1310 self.SkuId,
1311 self.UniFlag,
1312 self.Progress,
1313 self.ModuleFile
1314 )
1315 self.Fdf = Wa.FdfFile
1316 self.LoadFixAddress = Wa.Platform.LoadFixAddress
1317 Wa.CreateMakeFile(False)
1318 self.Progress.Stop("done!")
1319 MaList = []
1320 for Arch in Wa.ArchList:
1321 GlobalData.gGlobalDefines['ARCH'] = Arch
1322 Ma = ModuleAutoGen(Wa, self.ModuleFile, BuildTarget, ToolChain, Arch, self.PlatformFile)
1323 if Ma == None: continue
1324 MaList.append(Ma)
1325 self._Build(self.Target, Ma)
1326
1327 self.BuildReport.AddPlatformReport(Wa, MaList)
1328 if MaList == []:
1329 EdkLogger.error(
1330 'build',
1331 BUILD_ERROR,
1332 "Module for [%s] is not a component of active platform."\
1333 " Please make sure that the ARCH and inf file path are"\
1334 " given in the same as in [%s]" %\
1335 (', '.join(Wa.ArchList), self.PlatformFile),
1336 ExtraData=self.ModuleFile
1337 )
1338 # Create MAP file when Load Fix Address is enabled.
1339 if self.Target == "fds" and self.Fdf:
1340 for Arch in Wa.ArchList:
1341 #
1342 # Check whether the set fix address is above 4G for 32bit image.
1343 #
1344 if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000:
1345 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")
1346 #
1347 # Get Module List
1348 #
1349 ModuleList = {}
1350 for Pa in Wa.AutoGenObjectList:
1351 for Ma in Pa.ModuleAutoGenList:
1352 if Ma == None:
1353 continue
1354 if not Ma.IsLibrary:
1355 ModuleList[Ma.Guid.upper()] = Ma
1356
1357 MapBuffer = StringIO('')
1358 if self.LoadFixAddress != 0:
1359 #
1360 # Rebase module to the preferred memory address before GenFds
1361 #
1362 self._CollectModuleMapBuffer(MapBuffer, ModuleList)
1363 #
1364 # create FDS again for the updated EFI image
1365 #
1366 self._Build("fds", Wa)
1367 #
1368 # Create MAP file for all platform FVs after GenFds.
1369 #
1370 self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList)
1371 #
1372 # Save MAP buffer into MAP file.
1373 #
1374 self._SaveMapFile (MapBuffer, Wa)
1375
1376 ## Build a platform in multi-thread mode
1377 #
1378 def _MultiThreadBuildPlatform(self):
1379 for BuildTarget in self.BuildTargetList:
1380 GlobalData.gGlobalDefines['TARGET'] = BuildTarget
1381 for ToolChain in self.ToolChainList:
1382 GlobalData.gGlobalDefines['TOOLCHAIN'] = ToolChain
1383 GlobalData.gGlobalDefines['TOOL_CHAIN_TAG'] = ToolChain
1384 Wa = WorkspaceAutoGen(
1385 self.WorkspaceDir,
1386 self.PlatformFile,
1387 BuildTarget,
1388 ToolChain,
1389 self.ArchList,
1390 self.BuildDatabase,
1391 self.TargetTxt,
1392 self.ToolDef,
1393 self.Fdf,
1394 self.FdList,
1395 self.FvList,
1396 self.CapList,
1397 self.SkuId,
1398 self.UniFlag,
1399 self.Progress
1400 )
1401 self.Fdf = Wa.FdfFile
1402 self.LoadFixAddress = Wa.Platform.LoadFixAddress
1403 self.BuildReport.AddPlatformReport(Wa)
1404 Wa.CreateMakeFile(False)
1405
1406 # multi-thread exit flag
1407 ExitFlag = threading.Event()
1408 ExitFlag.clear()
1409 for Arch in Wa.ArchList:
1410 GlobalData.gGlobalDefines['ARCH'] = Arch
1411 Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch)
1412 if Pa == None:
1413 continue
1414 for Module in Pa.Platform.Modules:
1415 # Get ModuleAutoGen object to generate C code file and makefile
1416 Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile)
1417 if Ma == None:
1418 continue
1419 # Not to auto-gen for targets 'clean', 'cleanlib', 'cleanall', 'run', 'fds'
1420 if self.Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:
1421 # for target which must generate AutoGen code and makefile
1422 if not self.SkipAutoGen or self.Target == 'genc':
1423 Ma.CreateCodeFile(True)
1424 if self.Target == "genc":
1425 continue
1426
1427 if not self.SkipAutoGen or self.Target == 'genmake':
1428 Ma.CreateMakeFile(True)
1429 Ma.CreateAsBuiltInf()
1430 if self.Target == "genmake":
1431 continue
1432 self.Progress.Stop("done!")
1433 # Generate build task for the module
1434 Bt = BuildTask.New(ModuleMakeUnit(Ma, self.Target))
1435 # Break build if any build thread has error
1436 if BuildTask.HasError():
1437 # we need a full version of makefile for platform
1438 ExitFlag.set()
1439 BuildTask.WaitForComplete()
1440 Pa.CreateMakeFile(False)
1441 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
1442 # Start task scheduler
1443 if not BuildTask.IsOnGoing():
1444 BuildTask.StartScheduler(self.ThreadNumber, ExitFlag)
1445
1446 # in case there's an interruption. we need a full version of makefile for platform
1447 Pa.CreateMakeFile(False)
1448 if BuildTask.HasError():
1449 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
1450
1451 #
1452 # All modules have been put in build tasks queue. Tell task scheduler
1453 # to exit if all tasks are completed
1454 #
1455 ExitFlag.set()
1456 BuildTask.WaitForComplete()
1457
1458 #
1459 # Check for build error, and raise exception if one
1460 # has been signaled.
1461 #
1462 if BuildTask.HasError():
1463 EdkLogger.error("build", BUILD_ERROR, "Failed to build module", ExtraData=GlobalData.gBuildingModule)
1464
1465 # Create MAP file when Load Fix Address is enabled.
1466 if self.Target in ["", "all", "fds"]:
1467 for Arch in Wa.ArchList:
1468 #
1469 # Check whether the set fix address is above 4G for 32bit image.
1470 #
1471 if (Arch == 'IA32' or Arch == 'ARM') and self.LoadFixAddress != 0xFFFFFFFFFFFFFFFF and self.LoadFixAddress >= 0x100000000:
1472 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")
1473 #
1474 # Get Module List
1475 #
1476 ModuleList = {}
1477 for Pa in Wa.AutoGenObjectList:
1478 for Ma in Pa.ModuleAutoGenList:
1479 if Ma == None:
1480 continue
1481 if not Ma.IsLibrary:
1482 ModuleList[Ma.Guid.upper()] = Ma
1483 #
1484 # Rebase module to the preferred memory address before GenFds
1485 #
1486 MapBuffer = StringIO('')
1487 if self.LoadFixAddress != 0:
1488 self._CollectModuleMapBuffer(MapBuffer, ModuleList)
1489
1490 if self.Fdf:
1491 #
1492 # Generate FD image if there's a FDF file found
1493 #
1494 LaunchCommand(Wa.BuildCommand + ["fds"], Wa.MakeFileDir)
1495 #
1496 # Create MAP file for all platform FVs after GenFds.
1497 #
1498 self._CollectFvMapBuffer(MapBuffer, Wa, ModuleList)
1499 #
1500 # Save MAP buffer into MAP file.
1501 #
1502 self._SaveMapFile(MapBuffer, Wa)
1503
1504 ## Generate GuidedSectionTools.txt in the FV directories.
1505 #
1506 def CreateGuidedSectionToolsFile(self):
1507 for BuildTarget in self.BuildTargetList:
1508 for ToolChain in self.ToolChainList:
1509 Wa = WorkspaceAutoGen(
1510 self.WorkspaceDir,
1511 self.PlatformFile,
1512 BuildTarget,
1513 ToolChain,
1514 self.ArchList,
1515 self.BuildDatabase,
1516 self.TargetTxt,
1517 self.ToolDef,
1518 self.Fdf,
1519 self.FdList,
1520 self.FvList,
1521 self.CapList,
1522 self.SkuId,
1523 self.UniFlag
1524 )
1525 FvDir = Wa.FvDir
1526 if not os.path.exists(FvDir):
1527 continue
1528
1529 for Arch in self.ArchList:
1530 # Build up the list of supported architectures for this build
1531 prefix = '%s_%s_%s_' % (BuildTarget, ToolChain, Arch)
1532
1533 # Look through the tool definitions for GUIDed tools
1534 guidAttribs = []
1535 for (attrib, value) in self.ToolDef.ToolsDefTxtDictionary.iteritems():
1536 if attrib.upper().endswith('_GUID'):
1537 split = attrib.split('_')
1538 thisPrefix = '_'.join(split[0:3]) + '_'
1539 if thisPrefix == prefix:
1540 guid = self.ToolDef.ToolsDefTxtDictionary[attrib]
1541 guid = guid.lower()
1542 toolName = split[3]
1543 path = '_'.join(split[0:4]) + '_PATH'
1544 path = self.ToolDef.ToolsDefTxtDictionary[path]
1545 path = self.GetFullPathOfTool(path)
1546 guidAttribs.append((guid, toolName, path))
1547
1548 # Write out GuidedSecTools.txt
1549 toolsFile = os.path.join(FvDir, 'GuidedSectionTools.txt')
1550 toolsFile = open(toolsFile, 'wt')
1551 for guidedSectionTool in guidAttribs:
1552 print >> toolsFile, ' '.join(guidedSectionTool)
1553 toolsFile.close()
1554
1555 ## Returns the full path of the tool.
1556 #
1557 def GetFullPathOfTool (self, tool):
1558 if os.path.exists(tool):
1559 return os.path.realpath(tool)
1560 else:
1561 # We need to search for the tool using the
1562 # PATH environment variable.
1563 for dirInPath in os.environ['PATH'].split(os.pathsep):
1564 foundPath = os.path.join(dirInPath, tool)
1565 if os.path.exists(foundPath):
1566 return os.path.realpath(foundPath)
1567
1568 # If the tool was not found in the path then we just return
1569 # the input tool.
1570 return tool
1571
1572 ## Launch the module or platform build
1573 #
1574 def Launch(self):
1575 if not self.ModuleFile:
1576 if not self.SpawnMode or self.Target not in ["", "all"]:
1577 self.SpawnMode = False
1578 self._BuildPlatform()
1579 else:
1580 self._MultiThreadBuildPlatform()
1581 self.CreateGuidedSectionToolsFile()
1582 else:
1583 self.SpawnMode = False
1584 self._BuildModule()
1585
1586 ## Do some clean-up works when error occurred
1587 def Relinquish(self):
1588 OldLogLevel = EdkLogger.GetLevel()
1589 EdkLogger.SetLevel(EdkLogger.ERROR)
1590 #self.DumpBuildData()
1591 Utils.Progressor.Abort()
1592 if self.SpawnMode == True:
1593 BuildTask.Abort()
1594 EdkLogger.SetLevel(OldLogLevel)
1595
1596 def DumpBuildData(self):
1597 CacheDirectory = os.path.join(self.WorkspaceDir, gBuildCacheDir)
1598 Utils.CreateDirectory(CacheDirectory)
1599 Utils.DataDump(Utils.gFileTimeStampCache, os.path.join(CacheDirectory, "gFileTimeStampCache"))
1600 Utils.DataDump(Utils.gDependencyDatabase, os.path.join(CacheDirectory, "gDependencyDatabase"))
1601
1602 def RestoreBuildData(self):
1603 FilePath = os.path.join(self.WorkspaceDir, gBuildCacheDir, "gFileTimeStampCache")
1604 if Utils.gFileTimeStampCache == {} and os.path.isfile(FilePath):
1605 Utils.gFileTimeStampCache = Utils.DataRestore(FilePath)
1606 if Utils.gFileTimeStampCache == None:
1607 Utils.gFileTimeStampCache = {}
1608
1609 FilePath = os.path.join(self.WorkspaceDir, gBuildCacheDir, "gDependencyDatabase")
1610 if Utils.gDependencyDatabase == {} and os.path.isfile(FilePath):
1611 Utils.gDependencyDatabase = Utils.DataRestore(FilePath)
1612 if Utils.gDependencyDatabase == None:
1613 Utils.gDependencyDatabase = {}
1614
1615 def ParseDefines(DefineList=[]):
1616 DefineDict = {}
1617 if DefineList != None:
1618 for Define in DefineList:
1619 DefineTokenList = Define.split("=", 1)
1620 if not GlobalData.gMacroNamePattern.match(DefineTokenList[0]):
1621 EdkLogger.error('build', FORMAT_INVALID,
1622 "The macro name must be in the pattern [A-Z][A-Z0-9_]*",
1623 ExtraData=DefineTokenList[0])
1624
1625 if len(DefineTokenList) == 1:
1626 DefineDict[DefineTokenList[0]] = "TRUE"
1627 else:
1628 DefineDict[DefineTokenList[0]] = DefineTokenList[1].strip()
1629 return DefineDict
1630
1631 gParamCheck = []
1632 def SingleCheckCallback(option, opt_str, value, parser):
1633 if option not in gParamCheck:
1634 setattr(parser.values, option.dest, value)
1635 gParamCheck.append(option)
1636 else:
1637 parser.error("Option %s only allows one instance in command line!" % option)
1638
1639 ## Parse command line options
1640 #
1641 # Using standard Python module optparse to parse command line option of this tool.
1642 #
1643 # @retval Opt A optparse.Values object containing the parsed options
1644 # @retval Args Target of build command
1645 #
1646 def MyOptionParser():
1647 Parser = OptionParser(description=__copyright__,version=__version__,prog="build.exe",usage="%prog [options] [all|fds|genc|genmake|clean|cleanall|cleanlib|modules|libraries|run]")
1648 Parser.add_option("-a", "--arch", action="append", type="choice", choices=['IA32','X64','IPF','EBC','ARM'], dest="TargetArch",
1649 help="ARCHS is one of list: IA32, X64, IPF, ARM or EBC, which overrides target.txt's TARGET_ARCH definition. To specify more archs, please repeat this option.")
1650 Parser.add_option("-p", "--platform", action="callback", type="string", dest="PlatformFile", callback=SingleCheckCallback,
1651 help="Build the platform specified by the DSC file name argument, overriding target.txt's ACTIVE_PLATFORM definition.")
1652 Parser.add_option("-m", "--module", action="callback", type="string", dest="ModuleFile", callback=SingleCheckCallback,
1653 help="Build the module specified by the INF file name argument.")
1654 Parser.add_option("-b", "--buildtarget", action="append", type="choice", choices=['DEBUG','RELEASE','NOOPT'], dest="BuildTarget",
1655 help="BuildTarget is one of list: DEBUG, RELEASE, NOOPT, which overrides target.txt's TARGET definition. To specify more TARGET, please repeat this option.")
1656 Parser.add_option("-t", "--tagname", action="append", type="string", dest="ToolChain",
1657 help="Using the Tool Chain Tagname to build the platform, overriding target.txt's TOOL_CHAIN_TAG definition.")
1658 Parser.add_option("-x", "--sku-id", action="callback", type="string", dest="SkuId", callback=SingleCheckCallback,
1659 help="Using this name of SKU ID to build the platform, overriding SKUID_IDENTIFIER in DSC file.")
1660
1661 Parser.add_option("-n", action="callback", type="int", dest="ThreadNumber", callback=SingleCheckCallback,
1662 help="Build the platform using multi-threaded compiler. The value overrides target.txt's MAX_CONCURRENT_THREAD_NUMBER. Less than 2 will disable multi-thread builds.")
1663
1664 Parser.add_option("-f", "--fdf", action="callback", type="string", dest="FdfFile", callback=SingleCheckCallback,
1665 help="The name of the FDF file to use, which overrides the setting in the DSC file.")
1666 Parser.add_option("-r", "--rom-image", action="append", type="string", dest="RomImage", default=[],
1667 help="The name of FD to be generated. The name must be from [FD] section in FDF file.")
1668 Parser.add_option("-i", "--fv-image", action="append", type="string", dest="FvImage", default=[],
1669 help="The name of FV to be generated. The name must be from [FV] section in FDF file.")
1670 Parser.add_option("-C", "--capsule-image", action="append", type="string", dest="CapName", default=[],
1671 help="The name of Capsule to be generated. The name must be from [Capsule] section in FDF file.")
1672 Parser.add_option("-u", "--skip-autogen", action="store_true", dest="SkipAutoGen", help="Skip AutoGen step.")
1673 Parser.add_option("-e", "--re-parse", action="store_true", dest="Reparse", help="Re-parse all meta-data files.")
1674
1675 Parser.add_option("-c", "--case-insensitive", action="store_true", dest="CaseInsensitive", default=False, help="Don't check case of file name.")
1676
1677 Parser.add_option("-w", "--warning-as-error", action="store_true", dest="WarningAsError", help="Treat warning in tools as error.")
1678 Parser.add_option("-j", "--log", action="store", dest="LogFile", help="Put log in specified file as well as on console.")
1679
1680 Parser.add_option("-s", "--silent", action="store_true", type=None, dest="SilentMode",
1681 help="Make use of silent mode of (n)make.")
1682 Parser.add_option("-q", "--quiet", action="store_true", type=None, help="Disable all messages except FATAL ERRORS.")
1683 Parser.add_option("-v", "--verbose", action="store_true", type=None, help="Turn on verbose output with informational messages printed, "\
1684 "including library instances selected, final dependency expression, "\
1685 "and warning messages, etc.")
1686 Parser.add_option("-d", "--debug", action="store", type="int", help="Enable debug messages at specified level.")
1687 Parser.add_option("-D", "--define", action="append", type="string", dest="Macros", help="Macro: \"Name [= Value]\".")
1688
1689 Parser.add_option("-y", "--report-file", action="store", dest="ReportFile", help="Create/overwrite the report to the specified filename.")
1690 Parser.add_option("-Y", "--report-type", action="append", type="choice", choices=['PCD','LIBRARY','FLASH','DEPEX','BUILD_FLAGS','FIXED_ADDRESS', 'EXECUTION_ORDER'], dest="ReportType", default=[],
1691 help="Flags that control the type of build report to generate. Must be one of: [PCD, LIBRARY, FLASH, DEPEX, BUILD_FLAGS, FIXED_ADDRESS, EXECUTION_ORDER]. "\
1692 "To specify more than one flag, repeat this option on the command line and the default flag set is [PCD, LIBRARY, FLASH, DEPEX, BUILD_FLAGS, FIXED_ADDRESS]")
1693 Parser.add_option("-F", "--flag", action="store", type="string", dest="Flag",
1694 help="Specify the specific option to parse EDK UNI file. Must be one of: [-c, -s]. -c is for EDK framework UNI file, and -s is for EDK UEFI UNI file. "\
1695 "This option can also be specified by setting *_*_*_BUILD_FLAGS in [BuildOptions] section of platform DSC. If they are both specified, this value "\
1696 "will override the setting in [BuildOptions] section of platform DSC.")
1697 Parser.add_option("-N", "--no-cache", action="store_true", dest="DisableCache", default=False, help="Disable build cache mechanism")
1698
1699 (Opt, Args)=Parser.parse_args()
1700 return (Opt, Args)
1701
1702 ## Tool entrance method
1703 #
1704 # This method mainly dispatch specific methods per the command line options.
1705 # If no error found, return zero value so the caller of this tool can know
1706 # if it's executed successfully or not.
1707 #
1708 # @retval 0 Tool was successful
1709 # @retval 1 Tool failed
1710 #
1711 def Main():
1712 StartTime = time.time()
1713
1714 # Initialize log system
1715 EdkLogger.Initialize()
1716
1717 #
1718 # Parse the options and args
1719 #
1720 (Option, Target) = MyOptionParser()
1721 GlobalData.gOptions = Option
1722 GlobalData.gCaseInsensitive = Option.CaseInsensitive
1723
1724 # Set log level
1725 if Option.verbose != None:
1726 EdkLogger.SetLevel(EdkLogger.VERBOSE)
1727 elif Option.quiet != None:
1728 EdkLogger.SetLevel(EdkLogger.QUIET)
1729 elif Option.debug != None:
1730 EdkLogger.SetLevel(Option.debug + 1)
1731 else:
1732 EdkLogger.SetLevel(EdkLogger.INFO)
1733
1734 if Option.LogFile != None:
1735 EdkLogger.SetLogFile(Option.LogFile)
1736
1737 if Option.WarningAsError == True:
1738 EdkLogger.SetWarningAsError()
1739
1740 if platform.platform().find("Windows") >= 0:
1741 GlobalData.gIsWindows = True
1742 else:
1743 GlobalData.gIsWindows = False
1744
1745 EdkLogger.quiet("Build environment: %s" % platform.platform())
1746 EdkLogger.quiet(time.strftime("Build start time: %H:%M:%S, %b.%d %Y\n", time.localtime()));
1747 ReturnCode = 0
1748 MyBuild = None
1749 try:
1750 if len(Target) == 0:
1751 Target = "all"
1752 elif len(Target) >= 2:
1753 EdkLogger.error("build", OPTION_NOT_SUPPORTED, "More than one targets are not supported.",
1754 ExtraData="Please select one of: %s" %(' '.join(gSupportedTarget)))
1755 else:
1756 Target = Target[0].lower()
1757
1758 if Target not in gSupportedTarget:
1759 EdkLogger.error("build", OPTION_NOT_SUPPORTED, "Not supported target [%s]." % Target,
1760 ExtraData="Please select one of: %s" %(' '.join(gSupportedTarget)))
1761
1762 #
1763 # Check environment variable: EDK_TOOLS_PATH, WORKSPACE, PATH
1764 #
1765 CheckEnvVariable()
1766 GlobalData.gCommandLineDefines.update(ParseDefines(Option.Macros))
1767
1768 Workspace = os.getenv("WORKSPACE")
1769 #
1770 # Get files real name in workspace dir
1771 #
1772 GlobalData.gAllFiles = Utils.DirCache(Workspace)
1773
1774 WorkingDirectory = os.getcwd()
1775 if not Option.ModuleFile:
1776 FileList = glob.glob(os.path.normpath(os.path.join(WorkingDirectory, '*.inf')))
1777 FileNum = len(FileList)
1778 if FileNum >= 2:
1779 EdkLogger.error("build", OPTION_NOT_SUPPORTED, "There are %d INF files in %s." % (FileNum, WorkingDirectory),
1780 ExtraData="Please use '-m <INF_FILE_PATH>' switch to choose one.")
1781 elif FileNum == 1:
1782 Option.ModuleFile = NormFile(FileList[0], Workspace)
1783
1784 if Option.ModuleFile:
1785 if os.path.isabs (Option.ModuleFile):
1786 if os.path.normcase (os.path.normpath(Option.ModuleFile)).find (Workspace) == 0:
1787 Option.ModuleFile = NormFile(os.path.normpath(Option.ModuleFile), Workspace)
1788 Option.ModuleFile = PathClass(Option.ModuleFile, Workspace)
1789 ErrorCode, ErrorInfo = Option.ModuleFile.Validate(".inf", False)
1790 if ErrorCode != 0:
1791 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
1792
1793 if Option.PlatformFile != None:
1794 if os.path.isabs (Option.PlatformFile):
1795 if os.path.normcase (os.path.normpath(Option.PlatformFile)).find (Workspace) == 0:
1796 Option.PlatformFile = NormFile(os.path.normpath(Option.PlatformFile), Workspace)
1797 Option.PlatformFile = PathClass(Option.PlatformFile, Workspace)
1798
1799 if Option.FdfFile != None:
1800 if os.path.isabs (Option.FdfFile):
1801 if os.path.normcase (os.path.normpath(Option.FdfFile)).find (Workspace) == 0:
1802 Option.FdfFile = NormFile(os.path.normpath(Option.FdfFile), Workspace)
1803 Option.FdfFile = PathClass(Option.FdfFile, Workspace)
1804 ErrorCode, ErrorInfo = Option.FdfFile.Validate(".fdf", False)
1805 if ErrorCode != 0:
1806 EdkLogger.error("build", ErrorCode, ExtraData=ErrorInfo)
1807
1808 if Option.Flag != None and Option.Flag not in ['-c', '-s']:
1809 EdkLogger.error("build", OPTION_VALUE_INVALID, "UNI flag must be one of -c or -s")
1810
1811 MyBuild = Build(Target, Workspace, Option)
1812 MyBuild.Launch()
1813 #MyBuild.DumpBuildData()
1814 except FatalError, X:
1815 if MyBuild != None:
1816 # for multi-thread build exits safely
1817 MyBuild.Relinquish()
1818 if Option != None and Option.debug != None:
1819 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
1820 ReturnCode = X.args[0]
1821 except Warning, X:
1822 # error from Fdf parser
1823 if MyBuild != None:
1824 # for multi-thread build exits safely
1825 MyBuild.Relinquish()
1826 if Option != None and Option.debug != None:
1827 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
1828 else:
1829 EdkLogger.error(X.ToolName, FORMAT_INVALID, File=X.FileName, Line=X.LineNumber, ExtraData=X.Message, RaiseError = False)
1830 ReturnCode = FORMAT_INVALID
1831 except KeyboardInterrupt:
1832 ReturnCode = ABORT_ERROR
1833 if Option != None and Option.debug != None:
1834 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
1835 except:
1836 if MyBuild != None:
1837 # for multi-thread build exits safely
1838 MyBuild.Relinquish()
1839
1840 # try to get the meta-file from the object causing exception
1841 Tb = sys.exc_info()[-1]
1842 MetaFile = GlobalData.gProcessingFile
1843 while Tb != None:
1844 if 'self' in Tb.tb_frame.f_locals and hasattr(Tb.tb_frame.f_locals['self'], 'MetaFile'):
1845 MetaFile = Tb.tb_frame.f_locals['self'].MetaFile
1846 Tb = Tb.tb_next
1847 EdkLogger.error(
1848 "\nbuild",
1849 CODE_ERROR,
1850 "Unknown fatal error when processing [%s]" % MetaFile,
1851 ExtraData="\n(Please send email to edk2-buildtools-devel@lists.sourceforge.net for help, attaching following call stack trace!)\n",
1852 RaiseError=False
1853 )
1854 EdkLogger.quiet("(Python %s on %s) " % (platform.python_version(), sys.platform) + traceback.format_exc())
1855 ReturnCode = CODE_ERROR
1856 finally:
1857 Utils.Progressor.Abort()
1858
1859 if ReturnCode == 0:
1860 Conclusion = "Done"
1861 elif ReturnCode == ABORT_ERROR:
1862 Conclusion = "Aborted"
1863 else:
1864 Conclusion = "Failed"
1865 FinishTime = time.time()
1866 BuildDuration = time.gmtime(int(round(FinishTime - StartTime)))
1867 BuildDurationStr = ""
1868 if BuildDuration.tm_yday > 1:
1869 BuildDurationStr = time.strftime("%H:%M:%S", BuildDuration) + ", %d day(s)"%(BuildDuration.tm_yday - 1)
1870 else:
1871 BuildDurationStr = time.strftime("%H:%M:%S", BuildDuration)
1872 if MyBuild != None:
1873 MyBuild.BuildReport.GenerateReport(BuildDurationStr)
1874 MyBuild.Db.Close()
1875 EdkLogger.SetLevel(EdkLogger.QUIET)
1876 EdkLogger.quiet("\n- %s -" % Conclusion)
1877 EdkLogger.quiet(time.strftime("Build end time: %H:%M:%S, %b.%d %Y", time.localtime()))
1878 EdkLogger.quiet("Build total time: %s\n" % BuildDurationStr)
1879 return ReturnCode
1880
1881 if __name__ == '__main__':
1882 r = Main()
1883 ## 0-127 is a safe return range, and 1 is a standard default error
1884 if r < 0 or r > 127: r = 1
1885 sys.exit(r)
1886