2 # Create makefile for MS nmake and GNU make
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 import Common
.LongFilePathOs
as os
20 import os
.path
as path
21 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
22 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
23 from Common
.BuildToolError
import *
24 from Common
.Misc
import *
25 from Common
.StringUtils
import *
26 from BuildEngine
import *
27 import Common
.GlobalData
as GlobalData
28 from collections
import OrderedDict
30 ## Regular expression for finding header file inclusions
31 gIncludePattern
= re
.compile(r
"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?
[ \t]*)([-\w
.\\/() \t]+)(?
:[ \t]*[\">]?\
)?
)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
33 ## Regular expression for matching macro used in header file inclusion
34 gMacroPattern = re.compile("([_A
-Z
][_A
-Z0
-9]*)[ \t]*\
((.+)\
)", re.UNICODE)
38 ## pattern for include style in Edk.x code
39 gProtocolDefinition = "Protocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
40 gGuidDefinition = "Guid
/%(HeaderKey)s/%(HeaderKey)s.h
"
41 gArchProtocolDefinition = "ArchProtocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
42 gPpiDefinition = "Ppi
/%(HeaderKey)s/%(HeaderKey)s.h
"
43 gIncludeMacroConversion = {
44 "EFI_PROTOCOL_DEFINITION
" : gProtocolDefinition,
45 "EFI_GUID_DEFINITION
" : gGuidDefinition,
46 "EFI_ARCH_PROTOCOL_DEFINITION
" : gArchProtocolDefinition,
47 "EFI_PROTOCOL_PRODUCER
" : gProtocolDefinition,
48 "EFI_PROTOCOL_CONSUMER
" : gProtocolDefinition,
49 "EFI_PROTOCOL_DEPENDENCY
" : gProtocolDefinition,
50 "EFI_ARCH_PROTOCOL_PRODUCER
" : gArchProtocolDefinition,
51 "EFI_ARCH_PROTOCOL_CONSUMER
" : gArchProtocolDefinition,
52 "EFI_ARCH_PROTOCOL_DEPENDENCY
" : gArchProtocolDefinition,
53 "EFI_PPI_DEFINITION
" : gPpiDefinition,
54 "EFI_PPI_PRODUCER
" : gPpiDefinition,
55 "EFI_PPI_CONSUMER
" : gPpiDefinition,
56 "EFI_PPI_DEPENDENCY
" : gPpiDefinition,
59 ## default makefile type
61 if sys.platform == "win32
":
69 # This base class encapsules build file and its generation. It uses template to generate
70 # the content of build file. The content of build file will be got from AutoGen objects.
72 class BuildFile(object):
73 ## template used to generate the build file (i.e. makefile if using make)
74 _TEMPLATE_ = TemplateString('')
76 _DEFAULT_FILE_NAME_ = "Makefile
"
78 ## default file name for each type of build file
81 "gmake
" : "GNUmakefile
"
84 ## Fixed header string for makefile
85 _MAKEFILE_HEADER = '''#
87 # This file is auto-generated by build utility
95 # Auto-generated makefile for building modules, libraries or platform
99 ## Header string for each type of build file
101 "nmake
" : _MAKEFILE_HEADER % _FILE_NAME_["nmake
"],
102 "gmake
" : _MAKEFILE_HEADER % _FILE_NAME_["gmake
"]
105 ## shell commands which can be used in build file in the form of macro
106 # $(CP) copy file command
107 # $(MV) move file command
108 # $(RM) remove file command
109 # $(MD) create dir command
110 # $(RD) remove dir command
118 "RD
" : "rmdir
/s
/q
",
130 ## directory separator
136 ## directory creation template
138 "nmake
" : 'if not exist %(dir)s $(MD) %(dir)s',
139 "gmake
" : "$
(MD
) %(dir)s"
142 ## directory removal template
144 "nmake
" : 'if exist %(dir)s $(RD) %(dir)s',
145 "gmake
" : "$
(RD
) %(dir)s"
149 "nmake
" : 'if exist %(Src)s $(CP) %(Src)s %(Dst)s',
150 "gmake
" : "test
-f
%(Src)s && $
(CP
) %(Src)s %(Dst)s"
154 "nmake
" : 'if exist %(dir)s cd %(dir)s',
155 "gmake
" : "test
-e
%(dir)s && cd
%(dir)s"
159 "nmake
" : 'if exist %(file)s "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s',
160 "gmake
" : 'test -e %(file)s && "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s'
164 "nmake
" : '!INCLUDE',
168 _INC_FLAG_ = {"MSFT
" : "/I
", "GCC
" : "-I
", "INTEL
" : "-I
", "RVCT
" : "-I
"}
170 ## Constructor of BuildFile
172 # @param AutoGenObject Object of AutoGen class
174 def __init__(self, AutoGenObject):
175 self._AutoGenObject = AutoGenObject
176 self._FileType = gMakeType
180 # @param FileType Type of build file. Only nmake and gmake are supported now.
182 # @retval TRUE The build file is created or re-created successfully
183 # @retval FALSE The build file exists and is the same as the one to be generated
185 def Generate(self, FileType=gMakeType):
186 if FileType not in self._FILE_NAME_:
187 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid build
type [%s]" % FileType,
188 ExtraData="[%s]" % str(self._AutoGenObject))
189 self._FileType = FileType
190 FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
191 FileName = self._FILE_NAME_[FileType]
192 return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
194 ## Return a list of directory creation command string
196 # @param DirList The list of directory to be created
198 # @retval list The directory creation command list
200 def GetCreateDirectoryCommand(self, DirList):
201 return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
203 ## Return a list of directory removal command string
205 # @param DirList The list of directory to be removed
207 # @retval list The directory removal command list
209 def GetRemoveDirectoryCommand(self, DirList):
210 return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
212 def PlaceMacro(self, Path, MacroDefinitions={}):
213 if Path.startswith("$
("):
216 PathLength = len(Path)
217 for MacroName in MacroDefinitions:
218 MacroValue = MacroDefinitions[MacroName]
219 MacroValueLength = len(MacroValue)
220 if MacroValueLength == 0:
222 if MacroValueLength <= PathLength and Path.startswith(MacroValue):
223 Path = "$
(%s)%s" % (MacroName, Path[MacroValueLength:])
227 ## ModuleMakefile class
229 # This class encapsules makefie and its generation for module. It uses template to generate
230 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
232 class ModuleMakefile(BuildFile):
233 ## template used to generate the makefile for module
234 _TEMPLATE_ = TemplateString('''\
238 # Platform Macro Definition
240 PLATFORM_NAME = ${platform_name}
241 PLATFORM_GUID = ${platform_guid}
242 PLATFORM_VERSION = ${platform_version}
243 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
244 PLATFORM_DIR = ${platform_dir}
245 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
248 # Module Macro Definition
250 MODULE_NAME = ${module_name}
251 MODULE_GUID = ${module_guid}
252 MODULE_NAME_GUID = ${module_name_guid}
253 MODULE_VERSION = ${module_version}
254 MODULE_TYPE = ${module_type}
255 MODULE_FILE = ${module_file}
256 MODULE_FILE_BASE_NAME = ${module_file_base_name}
257 BASE_NAME = $(MODULE_NAME)
258 MODULE_RELATIVE_DIR = ${module_relative_directory}
259 PACKAGE_RELATIVE_DIR = ${package_relative_directory}
260 MODULE_DIR = ${module_dir}
261 FFS_OUTPUT_DIR = ${ffs_output_directory}
263 MODULE_ENTRY_POINT = ${module_entry_point}
264 ARCH_ENTRY_POINT = ${arch_entry_point}
265 IMAGE_ENTRY_POINT = ${image_entry_point}
267 ${BEGIN}${module_extra_defines}
270 # Build Configuration Macro Definition
272 ARCH = ${architecture}
273 TOOLCHAIN = ${toolchain_tag}
274 TOOLCHAIN_TAG = ${toolchain_tag}
275 TARGET = ${build_target}
278 # Build Directory Macro Definition
280 # PLATFORM_BUILD_DIR = ${platform_build_directory}
281 BUILD_DIR = ${platform_build_directory}
282 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
284 MODULE_BUILD_DIR = ${module_build_directory}
285 OUTPUT_DIR = ${module_output_directory}
286 DEBUG_DIR = ${module_debug_directory}
287 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
288 DEST_DIR_DEBUG = $(DEBUG_DIR)
291 # Shell Command Macro
293 ${BEGIN}${shell_command_code} = ${shell_command}
297 # Tools definitions specific to this module
299 ${BEGIN}${module_tool_definitions}
301 MAKE_FILE = ${makefile_path}
306 ${BEGIN}${file_macro}
309 COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
313 # Overridable Target Macro Definitions
315 FORCE_REBUILD = force_build
318 BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
319 CODA_TARGET = ${BEGIN}${remaining_build_target} \\
323 # Default target, which will build dependent libraries in addition to source files
330 # Target used when called from platform makefile, which will bypass the build of dependent libraries
333 pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
339 mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
342 # Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
345 tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
348 # Phony target which is used to force executing commands for a target
354 # Target to update the FD
360 # Initialization target: print build information and create necessary directories
365 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
368 ${BEGIN}\t-@${create_directory_command}\n${END}
371 \t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
377 \t${BEGIN}@"$
(MAKE
)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
378 \t${END}@cd $(MODULE_BUILD_DIR)
381 # Build Flash Device Image
384 \t@"$
(MAKE
)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
385 \t@cd $(MODULE_BUILD_DIR)
388 # Individual Object Build Targets
390 ${BEGIN}${file_build_target}
394 # clean all intermediate files
397 \t${BEGIN}${clean_command}
398 \t${END}\t$(RM) AutoGenTimeStamp
401 # clean all generated files
404 ${BEGIN}\t${cleanall_command}
405 ${END}\t$(RM) *.pdb *.idb > NUL 2>&1
406 \t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
407 \t$(RM) AutoGenTimeStamp
410 # clean all dependent libraries built
413 \t${BEGIN}-@${library_build_command} cleanall
414 \t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
416 _FILE_MACRO_TEMPLATE = TemplateString("${macro_name}
= ${BEGIN}
\\\n ${source_file}${END}
\n")
417 _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target}
: ${deps}
\n${END}
\t${cmd}
\n")
419 ## Constructor of ModuleMakefile
421 # @param ModuleAutoGen Object of ModuleAutoGen class
423 def __init__(self, ModuleAutoGen):
424 BuildFile.__init__(self, ModuleAutoGen)
425 self.PlatformInfo = self._AutoGenObject.PlatformInfo
427 self.ResultFileList = []
428 self.IntermediateDirectoryList = ["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]
430 self.FileBuildTargetList = [] # [(src, target string)]
431 self.BuildTargetList = [] # [target string]
432 self.PendingBuildTargetList = [] # [FileBuildRule objects]
433 self.CommonFileDependency = []
434 self.FileListMacros = {}
435 self.ListFileMacros = {}
438 self.FileDependency = []
439 self.LibraryBuildCommandList = []
440 self.LibraryFileList = []
441 self.LibraryMakefileList = []
442 self.LibraryBuildDirectoryList = []
443 self.SystemLibraryList = []
444 self.Macros = OrderedDict()
445 self.Macros["OUTPUT_DIR
" ] = self._AutoGenObject.Macros["OUTPUT_DIR
"]
446 self.Macros["DEBUG_DIR
" ] = self._AutoGenObject.Macros["DEBUG_DIR
"]
447 self.Macros["MODULE_BUILD_DIR
"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR
"]
448 self.Macros["BIN_DIR
" ] = self._AutoGenObject.Macros["BIN_DIR
"]
449 self.Macros["BUILD_DIR
" ] = self._AutoGenObject.Macros["BUILD_DIR
"]
450 self.Macros["WORKSPACE
" ] = self._AutoGenObject.Macros["WORKSPACE
"]
451 self.Macros["FFS_OUTPUT_DIR
" ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR
"]
452 self.GenFfsList = ModuleAutoGen.GenFfsList
453 self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']
454 self.FfsOutputFileList = []
456 # Compose a dict object containing information used to do replacement in template
457 def _CreateTemplateDict(self):
458 if self._FileType not in self._SEP_:
459 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid Makefile
type [%s]" % self._FileType,
460 ExtraData="[%s]" % str(self._AutoGenObject))
461 MyAgo = self._AutoGenObject
462 Separator = self._SEP_[self._FileType]
464 # break build if no source files and binary files are found
465 if len(MyAgo.SourceFileList) == 0 and len(MyAgo.BinaryFileList) == 0:
466 EdkLogger.error("build
", AUTOGEN_ERROR, "No files to be built
in module
[%s, %s, %s]"
467 % (MyAgo.BuildTarget, MyAgo.ToolChain, MyAgo.Arch),
468 ExtraData="[%s]" % str(MyAgo))
470 # convert dependent libraries to build command
471 self.ProcessDependentLibrary()
472 if len(MyAgo.Module.ModuleEntryPointList) > 0:
473 ModuleEntryPoint = MyAgo.Module.ModuleEntryPointList[0]
475 ModuleEntryPoint = "_ModuleEntryPoint
"
477 # Intel EBC compiler enforces EfiMain
478 if MyAgo.AutoGenVersion < 0x00010005 and MyAgo.Arch == "EBC
":
479 ArchEntryPoint = "EfiMain
"
481 ArchEntryPoint = ModuleEntryPoint
483 if MyAgo.Arch == "EBC
":
484 # EBC compiler always use "EfiStart
" as entry point. Only applies to EdkII modules
485 ImageEntryPoint = "EfiStart
"
486 elif MyAgo.AutoGenVersion < 0x00010005:
487 # Edk modules use entry point specified in INF file
488 ImageEntryPoint = ModuleEntryPoint
490 # EdkII modules always use "_ModuleEntryPoint
" as entry point
491 ImageEntryPoint = "_ModuleEntryPoint
"
493 for k, v in MyAgo.Module.Defines.iteritems():
494 if k not in MyAgo.Macros:
497 if 'MODULE_ENTRY_POINT' not in MyAgo.Macros:
498 MyAgo.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint
499 if 'ARCH_ENTRY_POINT' not in MyAgo.Macros:
500 MyAgo.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint
501 if 'IMAGE_ENTRY_POINT' not in MyAgo.Macros:
502 MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint
504 PCI_COMPRESS_Flag = False
505 for k, v in MyAgo.Module.Defines.iteritems():
506 if 'PCI_COMPRESS' == k and 'TRUE' == v:
507 PCI_COMPRESS_Flag = True
511 IncPrefix = self._INC_FLAG_[MyAgo.ToolChainFamily]
512 for Tool in MyAgo.BuildOption:
513 for Attr in MyAgo.BuildOption[Tool]:
514 Value = MyAgo.BuildOption[Tool][Attr]
518 ToolsDef.append("%s = %s" % (Tool, Value))
520 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
523 # Remove duplicated include path, if any
525 Value = RemoveDupOption(Value, IncPrefix, MyAgo.IncludePathList)
526 if Tool == "OPTROM
" and PCI_COMPRESS_Flag:
527 ValueList = Value.split()
529 for i, v in enumerate(ValueList):
532 Value = ' '.join(ValueList)
534 ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
537 # generate the Response file and Response flag
538 RespDict = self.CommandExceedLimit()
539 RespFileList = os.path.join(MyAgo.OutputDir, 'respfilelist.txt')
541 RespFileListContent = ''
542 for Resp in RespDict:
543 RespFile = os.path.join(MyAgo.OutputDir, str(Resp).lower() + '.txt')
544 StrList = RespDict[Resp].split(' ')
549 UnexpandMacro.append(Str)
552 UnexpandMacroStr = ' '.join(UnexpandMacro)
553 NewRespStr = ' '.join(NewStr)
554 SaveFileOnChange(RespFile, NewRespStr, False)
555 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))
556 RespFileListContent += '@' + RespFile + os.linesep
557 RespFileListContent += NewRespStr + os.linesep
558 SaveFileOnChange(RespFileList, RespFileListContent, False)
560 if os.path.exists(RespFileList):
561 os.remove(RespFileList)
563 # convert source files and binary files to build targets
564 self.ResultFileList = [str(T.Target) for T in MyAgo.CodaTargetList]
565 if len(self.ResultFileList) == 0 and len(MyAgo.SourceFileList) != 0:
566 EdkLogger.error("build
", AUTOGEN_ERROR, "Nothing to build
",
567 ExtraData="[%s]" % str(MyAgo))
569 self.ProcessBuildTargetList()
570 self.ParserGenerateFfsCmd()
572 # Generate macros used to represent input files
573 FileMacroList = [] # macro name = file list
574 for FileListMacro in self.FileListMacros:
575 FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
577 "macro_name
" : FileListMacro,
578 "source_file
" : self.FileListMacros[FileListMacro]
581 FileMacroList.append(FileMacro)
583 # INC_LIST is special
586 for P in MyAgo.IncludePathList:
587 IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))
588 if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
589 self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)
590 FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
592 "macro_name
" : "INC
",
593 "source_file
" : IncludePathList
596 FileMacroList.append(FileMacro)
598 # Generate macros used to represent files containing list of input files
599 for ListFileMacro in self.ListFileMacros:
600 ListFileName = os.path.join(MyAgo.OutputDir, "%s.lst
" % ListFileMacro.lower()[:len(ListFileMacro) - 5])
601 FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
604 "\n".join(self.ListFileMacros[ListFileMacro]),
608 # Edk modules need <BaseName>StrDefs.h for string ID
609 #if MyAgo.AutoGenVersion < 0x00010005 and len(MyAgo.UnicodeFileList) > 0:
610 # BcTargetList = ['strdefs']
615 MakefileName = self._FILE_NAME_[self._FileType]
616 LibraryMakeCommandList = []
617 for D in self.LibraryBuildDirectoryList:
618 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}
619 LibraryMakeCommandList.append(Command)
621 package_rel_dir = MyAgo.SourceDir
622 current_dir = self.Macros["WORKSPACE
"]
624 while not found and os.sep in package_rel_dir:
625 index = package_rel_dir.index(os.sep)
626 current_dir = mws.join(current_dir, package_rel_dir[:index])
627 if os.path.exists(current_dir):
628 for fl in os.listdir(current_dir):
629 if fl.endswith('.dec'):
632 package_rel_dir = package_rel_dir[index + 1:]
634 MakefileTemplateDict = {
635 "makefile_header
" : self._FILE_HEADER_[self._FileType],
636 "makefile_path
" : os.path.join("$
(MODULE_BUILD_DIR
)", MakefileName),
637 "makefile_name
" : MakefileName,
638 "platform_name
" : self.PlatformInfo.Name,
639 "platform_guid
" : self.PlatformInfo.Guid,
640 "platform_version
" : self.PlatformInfo.Version,
641 "platform_relative_directory
": self.PlatformInfo.SourceDir,
642 "platform_output_directory
" : self.PlatformInfo.OutputDir,
643 "ffs_output_directory
" : MyAgo.Macros["FFS_OUTPUT_DIR
"],
644 "platform_dir
" : MyAgo.Macros["PLATFORM_DIR
"],
646 "module_name
" : MyAgo.Name,
647 "module_guid
" : MyAgo.Guid,
648 "module_name_guid
" : MyAgo.UniqueBaseName,
649 "module_version
" : MyAgo.Version,
650 "module_type
" : MyAgo.ModuleType,
651 "module_file
" : MyAgo.MetaFile.Name,
652 "module_file_base_name
" : MyAgo.MetaFile.BaseName,
653 "module_relative_directory
" : MyAgo.SourceDir,
654 "module_dir
" : mws.join (self.Macros["WORKSPACE
"], MyAgo.SourceDir),
655 "package_relative_directory
": package_rel_dir,
656 "module_extra_defines
" : ["%s = %s" % (k, v) for k, v in MyAgo.Module.Defines.iteritems()],
658 "architecture
" : MyAgo.Arch,
659 "toolchain_tag
" : MyAgo.ToolChain,
660 "build_target
" : MyAgo.BuildTarget,
662 "platform_build_directory
" : self.PlatformInfo.BuildDir,
663 "module_build_directory
" : MyAgo.BuildDir,
664 "module_output_directory
" : MyAgo.OutputDir,
665 "module_debug_directory
" : MyAgo.DebugDir,
667 "separator
" : Separator,
668 "module_tool_definitions
" : ToolsDef,
670 "shell_command_code
" : self._SHELL_CMD_[self._FileType].keys(),
671 "shell_command
" : self._SHELL_CMD_[self._FileType].values(),
673 "module_entry_point
" : ModuleEntryPoint,
674 "image_entry_point
" : ImageEntryPoint,
675 "arch_entry_point
" : ArchEntryPoint,
676 "remaining_build_target
" : self.ResultFileList,
677 "common_dependency_file
" : self.CommonFileDependency,
678 "create_directory_command
" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
679 "clean_command
" : self.GetRemoveDirectoryCommand(["$
(OUTPUT_DIR
)"]),
680 "cleanall_command
" : self.GetRemoveDirectoryCommand(["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]),
681 "dependent_library_build_directory
" : self.LibraryBuildDirectoryList,
682 "library_build_command
" : LibraryMakeCommandList,
683 "file_macro
" : FileMacroList,
684 "file_build_target
" : self.BuildTargetList,
685 "backward_compatible_target
": BcTargetList,
688 return MakefileTemplateDict
690 def ParserGenerateFfsCmd(self):
691 #Add Ffs cmd to self.BuildTargetList
695 for Cmd in self.GenFfsList:
697 for CopyCmd in Cmd[2]:
699 Src = self.ReplaceMacro(Src)
700 Dst = self.ReplaceMacro(Dst)
701 if Dst not in self.ResultFileList:
702 self.ResultFileList.append(Dst)
703 if '%s :' %(Dst) not in self.BuildTargetList:
704 self.BuildTargetList.append("%s :" %(Dst))
705 self.BuildTargetList.append('\t' + self._CP_TEMPLATE_[self._FileType] %{'Src': Src, 'Dst': Dst})
708 for index, Str in enumerate(FfsCmdList):
710 OutputFile = FfsCmdList[index + 1]
712 if DepsFileList == []:
713 DepsFileList = [FfsCmdList[index + 1]]
715 DepsFileList.append(FfsCmdList[index + 1])
716 DepsFileString = ' '.join(DepsFileList).strip()
717 if DepsFileString == '':
719 OutputFile = self.ReplaceMacro(OutputFile)
720 self.ResultFileList.append(OutputFile)
721 DepsFileString = self.ReplaceMacro(DepsFileString)
722 self.BuildTargetList.append('%s : %s' % (OutputFile, DepsFileString))
723 CmdString = ' '.join(FfsCmdList).strip()
724 CmdString = self.ReplaceMacro(CmdString)
725 self.BuildTargetList.append('\t%s' % CmdString)
727 self.ParseSecCmd(DepsFileList, Cmd[1])
728 for SecOutputFile, SecDepsFile, SecCmd in self.FfsOutputFileList :
729 self.BuildTargetList.append('%s : %s' % (self.ReplaceMacro(SecOutputFile), self.ReplaceMacro(SecDepsFile)))
730 self.BuildTargetList.append('\t%s' % self.ReplaceMacro(SecCmd))
731 self.FfsOutputFileList = []
733 def ParseSecCmd(self, OutputFileList, CmdTuple):
734 for OutputFile in OutputFileList:
735 for SecCmdStr in CmdTuple:
737 SecCmdList = SecCmdStr.split()
738 CmdName = SecCmdList[0]
739 for index, CmdItem in enumerate(SecCmdList):
740 if '-o' == CmdItem and OutputFile == SecCmdList[index + 1]:
742 while index + 1 < len(SecCmdList):
743 if not SecCmdList[index+1].startswith('-'):
744 SecDepsFileList.append(SecCmdList[index + 1])
746 if CmdName == 'Trim':
747 SecDepsFileList.append(os.path.join('$(DEBUG_DIR)', os.path.basename(OutputFile).replace('offset', 'efi')))
748 if OutputFile.endswith('.ui') or OutputFile.endswith('.ver'):
749 SecDepsFileList.append(os.path.join('$(MODULE_DIR)', '$(MODULE_FILE)'))
750 self.FfsOutputFileList.append((OutputFile, ' '.join(SecDepsFileList), SecCmdStr))
751 if len(SecDepsFileList) > 0:
752 self.ParseSecCmd(SecDepsFileList, CmdTuple)
757 def ReplaceMacro(self, str):
758 for Macro in self.MacroList:
759 if self._AutoGenObject.Macros[Macro] and self._AutoGenObject.Macros[Macro] in str:
760 str = str.replace(self._AutoGenObject.Macros[Macro], '$(' + Macro + ')')
763 def CommandExceedLimit(self):
765 'CC' : { 'Macro' : '$(CC_FLAGS)', 'Value' : False},
766 'PP' : { 'Macro' : '$(PP_FLAGS)', 'Value' : False},
767 'APP' : { 'Macro' : '$(APP_FLAGS)', 'Value' : False},
768 'ASLPP' : { 'Macro' : '$(ASLPP_FLAGS)', 'Value' : False},
769 'VFRPP' : { 'Macro' : '$(VFRPP_FLAGS)', 'Value' : False},
770 'ASM' : { 'Macro' : '$(ASM_FLAGS)', 'Value' : False},
771 'ASLCC' : { 'Macro' : '$(ASLCC_FLAGS)', 'Value' : False},
776 IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]
778 # base on the source files to decide the file type
779 for File in self._AutoGenObject.SourceFileList:
780 for type in self._AutoGenObject.FileTypes:
781 if File in self._AutoGenObject.FileTypes[type]:
782 if type not in FileTypeList:
783 FileTypeList.append(type)
785 # calculate the command-line length
787 for type in FileTypeList:
788 BuildTargets = self._AutoGenObject.BuildRules[type].BuildTargets
789 for Target in BuildTargets:
790 CommandList = BuildTargets[Target].Commands
791 for SingleCommand in CommandList:
793 SingleCommandLength = len(SingleCommand)
794 SingleCommandList = SingleCommand.split()
795 if len(SingleCommandList) > 0:
796 for Flag in FlagDict:
797 if '$('+ Flag +')' in SingleCommandList[0]:
801 if 'PATH' not in self._AutoGenObject._BuildOption[Tool]:
802 EdkLogger.error("build
", AUTOGEN_ERROR, "%s_PATH doesn
't exist in %s ToolChain and %s Arch." %(Tool, self._AutoGenObject.ToolChain, self._AutoGenObject.Arch), ExtraData="[%s]" % str(self._AutoGenObject))
803 SingleCommandLength += len(self._AutoGenObject._BuildOption[Tool]['PATH
'])
804 for item in SingleCommandList[1:]:
805 if FlagDict[Tool]['Macro
'] in item:
806 if 'FLAGS
' not in self._AutoGenObject._BuildOption[Tool]:
807 EdkLogger.error("build", AUTOGEN_ERROR, "%s_FLAGS doesn't exist
in %s ToolChain
and %s Arch
." %(Tool, self._AutoGenObject.ToolChain, self._AutoGenObject.Arch), ExtraData="[%s]" % str(self._AutoGenObject))
808 Str = self._AutoGenObject._BuildOption[Tool]['FLAGS']
809 for Option in self._AutoGenObject.BuildOption:
810 for Attr in self._AutoGenObject.BuildOption[Option]:
811 if Str.find(Option + '_' + Attr) != -1:
812 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
813 while(Str.find('$(') != -1):
814 for macro in self._AutoGenObject.Macros:
815 MacroName = '$('+ macro + ')'
816 if (Str.find(MacroName) != -1):
817 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
821 SingleCommandLength += len(Str)
822 elif '$(INC)' in item:
823 SingleCommandLength += self._AutoGenObject.IncludePathLength + len(IncPrefix) * len(self._AutoGenObject._IncludePathList)
824 elif item.find('$(') != -1:
826 for Option in self._AutoGenObject.BuildOption:
827 for Attr in self._AutoGenObject.BuildOption[Option]:
828 if Str.find(Option + '_' + Attr) != -1:
829 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
830 while(Str.find('$(') != -1):
831 for macro in self._AutoGenObject.Macros:
832 MacroName = '$('+ macro + ')'
833 if (Str.find(MacroName) != -1):
834 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
838 SingleCommandLength += len(Str)
840 if SingleCommandLength > GlobalData.gCommandMaxLength:
841 FlagDict[Tool]['Value'] = True
843 # generate the response file content by combine the FLAGS and INC
844 for Flag in FlagDict:
845 if FlagDict[Flag]['Value']:
847 RespMacro = FlagDict[Flag]['Macro'].replace('FLAGS', 'RESP')
848 Value = self._AutoGenObject.BuildOption[Flag]['FLAGS']
849 for inc in self._AutoGenObject._IncludePathList:
850 Value += ' ' + IncPrefix + inc
851 for Option in self._AutoGenObject.BuildOption:
852 for Attr in self._AutoGenObject.BuildOption[Option]:
853 if Value.find(Option + '_' + Attr) != -1:
854 Value = Value.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
855 while (Value.find('$(') != -1):
856 for macro in self._AutoGenObject.Macros:
857 MacroName = '$('+ macro + ')'
858 if (Value.find(MacroName) != -1):
859 Value = Value.replace(MacroName, self._AutoGenObject.Macros[macro])
864 if self._AutoGenObject.ToolChainFamily == 'GCC':
865 RespDict[Key] = Value.replace('\\', '/')
867 RespDict[Key] = Value
868 for Target in BuildTargets:
869 for i, SingleCommand in enumerate(BuildTargets[Target].Commands):
870 if FlagDict[Flag]['Macro'] in SingleCommand:
871 BuildTargets[Target].Commands[i] = SingleCommand.replace('$(INC)', '').replace(FlagDict[Flag]['Macro'], RespMacro)
874 def ProcessBuildTargetList(self):
876 # Search dependency file list for each source file
878 ForceIncludedFile = []
879 for File in self._AutoGenObject.AutoGenFileList:
881 ForceIncludedFile.append(File)
884 for Target in self._AutoGenObject.IntroTargetList:
885 SourceFileList.extend(Target.Inputs)
886 OutPutFileList.extend(Target.Outputs)
889 for Item in OutPutFileList:
890 if Item in SourceFileList:
891 SourceFileList.remove(Item)
893 self.FileDependency = self.GetFileDependency(
896 self._AutoGenObject.IncludePathList + self._AutoGenObject.BuildOptionIncPathList
899 for File in self.FileDependency:
900 if not self.FileDependency[File]:
901 self.FileDependency[File] = ['$(FORCE_REBUILD)']
904 self._AutoGenObject.AutoGenDepSet |= set(self.FileDependency[File])
907 if File.Ext not in [".c
", ".C
"] or File.Name == "AutoGen
.c
":
910 DepSet = set(self.FileDependency[File])
912 DepSet &= set(self.FileDependency[File])
913 # in case nothing in SourceFileList
917 # Extract common files list in the dependency files
920 self.CommonFileDependency.append(self.PlaceMacro(File.Path, self.Macros))
922 for File in self.FileDependency:
924 if File.Ext not in [".c
", ".C
"] or File.Name == "AutoGen
.c
":
926 NewDepSet = set(self.FileDependency[File])
928 self.FileDependency[File] = ["$
(COMMON_DEPS
)"] + list(NewDepSet)
930 # Convert target description object to target string in makefile
931 for Type in self._AutoGenObject.Targets:
932 for T in self._AutoGenObject.Targets[Type]:
933 # Generate related macros if needed
934 if T.GenFileListMacro and T.FileListMacro not in self.FileListMacros:
935 self.FileListMacros[T.FileListMacro] = []
936 if T.GenListFile and T.ListFileMacro not in self.ListFileMacros:
937 self.ListFileMacros[T.ListFileMacro] = []
938 if T.GenIncListFile and T.IncListFileMacro not in self.ListFileMacros:
939 self.ListFileMacros[T.IncListFileMacro] = []
942 # Add force-dependencies
943 for Dep in T.Dependencies:
944 Deps.append(self.PlaceMacro(str(Dep), self.Macros))
945 # Add inclusion-dependencies
946 if len(T.Inputs) == 1 and T.Inputs[0] in self.FileDependency:
947 for F in self.FileDependency[T.Inputs[0]]:
948 Deps.append(self.PlaceMacro(str(F), self.Macros))
949 # Add source-dependencies
951 NewFile = self.PlaceMacro(str(F), self.Macros)
952 # In order to use file list macro as dependency
954 # gnu tools need forward slash path separater, even on Windows
955 self.ListFileMacros[T.ListFileMacro].append(str(F).replace ('\\', '/'))
956 self.FileListMacros[T.FileListMacro].append(NewFile)
957 elif T.GenFileListMacro:
958 self.FileListMacros[T.FileListMacro].append(NewFile)
962 # Use file list macro as dependency
963 if T.GenFileListMacro:
964 Deps.append("$
(%s)" % T.FileListMacro)
965 if Type in [TAB_OBJECT_FILE, TAB_STATIC_LIBRARY]:
966 Deps.append("$
(%s)" % T.ListFileMacro)
969 "target
" : self.PlaceMacro(T.Target.Path, self.Macros),
970 "cmd
" : "\n\t".join(T.Commands),
973 self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))
975 ## For creating makefile targets for dependent libraries
976 def ProcessDependentLibrary(self):
977 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
978 if not LibraryAutoGen.IsBinaryModule:
979 self.LibraryBuildDirectoryList.append(self.PlaceMacro(LibraryAutoGen.BuildDir, self.Macros))
981 ## Return a list containing source file's dependencies
983 # @param FileList The list of source files
984 # @param ForceInculeList The list of files which will be included forcely
985 # @param SearchPathList The list of search path
987 # @retval dict The mapping between source file path and its dependencies
989 def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):
992 Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)
995 ## Find dependencies for one source file
997 # By searching recursively "#include" directive in file, find out all the
998 # files needed by given source file. The dependecies will be only searched
999 # in given search path list.
1001 # @param File The source file
1002 # @param ForceInculeList The list of files which will be included forcely
1003 # @param SearchPathList The list of search path
1005 # @retval list The list of files the given source file depends on
1007 def GetDependencyList(self
, File
, ForceList
, SearchPathList
):
1008 EdkLogger
.debug(EdkLogger
.DEBUG_1
, "Try to get dependency files for %s" % File
)
1009 FileStack
= [File
] + ForceList
1010 DependencySet
= set()
1012 if self
._AutoGenObject
.Arch
not in gDependencyDatabase
:
1013 gDependencyDatabase
[self
._AutoGenObject
.Arch
] = {}
1014 DepDb
= gDependencyDatabase
[self
._AutoGenObject
.Arch
]
1016 while len(FileStack
) > 0:
1019 FullPathDependList
= []
1020 if F
in self
.FileCache
:
1021 for CacheFile
in self
.FileCache
[F
]:
1022 FullPathDependList
.append(CacheFile
)
1023 if CacheFile
not in DependencySet
:
1024 FileStack
.append(CacheFile
)
1025 DependencySet
.update(FullPathDependList
)
1028 CurrentFileDependencyList
= []
1030 CurrentFileDependencyList
= DepDb
[F
]
1033 Fd
= open(F
.Path
, 'r')
1034 except BaseException
as X
:
1035 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=F
.Path
+ "\n\t" + str(X
))
1037 FileContent
= Fd
.read()
1039 if len(FileContent
) == 0:
1042 if FileContent
[0] == 0xff or FileContent
[0] == 0xfe:
1043 FileContent
= unicode(FileContent
, "utf-16")
1044 IncludedFileList
= gIncludePattern
.findall(FileContent
)
1046 for Inc
in IncludedFileList
:
1048 # if there's macro used to reference header file, expand it
1049 HeaderList
= gMacroPattern
.findall(Inc
)
1050 if len(HeaderList
) == 1 and len(HeaderList
[0]) == 2:
1051 HeaderType
= HeaderList
[0][0]
1052 HeaderKey
= HeaderList
[0][1]
1053 if HeaderType
in gIncludeMacroConversion
:
1054 Inc
= gIncludeMacroConversion
[HeaderType
] % {"HeaderKey" : HeaderKey
}
1056 # not known macro used in #include, always build the file by
1057 # returning a empty dependency
1058 self
.FileCache
[File
] = []
1060 Inc
= os
.path
.normpath(Inc
)
1061 CurrentFileDependencyList
.append(Inc
)
1062 DepDb
[F
] = CurrentFileDependencyList
1064 CurrentFilePath
= F
.Dir
1065 PathList
= [CurrentFilePath
] + SearchPathList
1066 for Inc
in CurrentFileDependencyList
:
1067 for SearchPath
in PathList
:
1068 FilePath
= os
.path
.join(SearchPath
, Inc
)
1069 if FilePath
in gIsFileMap
:
1070 if not gIsFileMap
[FilePath
]:
1072 # If isfile is called too many times, the performance is slow down.
1073 elif not os
.path
.isfile(FilePath
):
1074 gIsFileMap
[FilePath
] = False
1077 gIsFileMap
[FilePath
] = True
1078 FilePath
= PathClass(FilePath
)
1079 FullPathDependList
.append(FilePath
)
1080 if FilePath
not in DependencySet
:
1081 FileStack
.append(FilePath
)
1084 EdkLogger
.debug(EdkLogger
.DEBUG_9
, "%s included by %s was not found "\
1085 "in any given path:\n\t%s" % (Inc
, F
, "\n\t".join(SearchPathList
)))
1087 self
.FileCache
[F
] = FullPathDependList
1088 DependencySet
.update(FullPathDependList
)
1090 DependencySet
.update(ForceList
)
1091 if File
in DependencySet
:
1092 DependencySet
.remove(File
)
1093 DependencyList
= list(DependencySet
) # remove duplicate ones
1095 return DependencyList
1097 _TemplateDict
= property(_CreateTemplateDict
)
1099 ## CustomMakefile class
1101 # This class encapsules makefie and its generation for module. It uses template to generate
1102 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
1104 class CustomMakefile(BuildFile
):
1105 ## template used to generate the makefile for module with custom makefile
1106 _TEMPLATE_
= TemplateString('''\
1110 # Platform Macro Definition
1112 PLATFORM_NAME = ${platform_name}
1113 PLATFORM_GUID = ${platform_guid}
1114 PLATFORM_VERSION = ${platform_version}
1115 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
1116 PLATFORM_DIR = ${platform_dir}
1117 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1120 # Module Macro Definition
1122 MODULE_NAME = ${module_name}
1123 MODULE_GUID = ${module_guid}
1124 MODULE_NAME_GUID = ${module_name_guid}
1125 MODULE_VERSION = ${module_version}
1126 MODULE_TYPE = ${module_type}
1127 MODULE_FILE = ${module_file}
1128 MODULE_FILE_BASE_NAME = ${module_file_base_name}
1129 BASE_NAME = $(MODULE_NAME)
1130 MODULE_RELATIVE_DIR = ${module_relative_directory}
1131 MODULE_DIR = ${module_dir}
1134 # Build Configuration Macro Definition
1136 ARCH = ${architecture}
1137 TOOLCHAIN = ${toolchain_tag}
1138 TOOLCHAIN_TAG = ${toolchain_tag}
1139 TARGET = ${build_target}
1142 # Build Directory Macro Definition
1144 # PLATFORM_BUILD_DIR = ${platform_build_directory}
1145 BUILD_DIR = ${platform_build_directory}
1146 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
1147 LIB_DIR = $(BIN_DIR)
1148 MODULE_BUILD_DIR = ${module_build_directory}
1149 OUTPUT_DIR = ${module_output_directory}
1150 DEBUG_DIR = ${module_debug_directory}
1151 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
1152 DEST_DIR_DEBUG = $(DEBUG_DIR)
1155 # Tools definitions specific to this module
1157 ${BEGIN}${module_tool_definitions}
1159 MAKE_FILE = ${makefile_path}
1162 # Shell Command Macro
1164 ${BEGIN}${shell_command_code} = ${shell_command}
1167 ${custom_makefile_content}
1170 # Target used when called from platform makefile, which will bypass the build of dependent libraries
1183 # Build Target used in multi-thread build mode, which no init target is needed
1189 # Initialization target: print build information and create necessary directories
1192 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
1193 ${BEGIN}\t-@${create_directory_command}\n${END}\
1197 ## Constructor of CustomMakefile
1199 # @param ModuleAutoGen Object of ModuleAutoGen class
1201 def __init__(self
, ModuleAutoGen
):
1202 BuildFile
.__init
__(self
, ModuleAutoGen
)
1203 self
.PlatformInfo
= self
._AutoGenObject
.PlatformInfo
1204 self
.IntermediateDirectoryList
= ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
1206 # Compose a dict object containing information used to do replacement in template
1207 def _CreateTemplateDict(self
):
1208 Separator
= self
._SEP
_[self
._FileType
]
1209 MyAgo
= self
._AutoGenObject
1210 if self
._FileType
not in MyAgo
.CustomMakefile
:
1211 EdkLogger
.error('build', OPTION_NOT_SUPPORTED
, "No custom makefile for %s" % self
._FileType
,
1212 ExtraData
="[%s]" % str(MyAgo
))
1213 MakefilePath
= mws
.join(
1215 MyAgo
.CustomMakefile
[self
._FileType
]
1218 CustomMakefile
= open(MakefilePath
, 'r').read()
1220 EdkLogger
.error('build', FILE_OPEN_FAILURE
, File
=str(MyAgo
),
1221 ExtraData
=MyAgo
.CustomMakefile
[self
._FileType
])
1225 for Tool
in MyAgo
.BuildOption
:
1226 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
1229 for Attr
in MyAgo
.BuildOption
[Tool
]:
1230 if Attr
== "FAMILY":
1232 elif Attr
== "PATH":
1233 ToolsDef
.append("%s = %s" % (Tool
, MyAgo
.BuildOption
[Tool
][Attr
]))
1235 ToolsDef
.append("%s_%s = %s" % (Tool
, Attr
, MyAgo
.BuildOption
[Tool
][Attr
]))
1238 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1239 MakefileTemplateDict
= {
1240 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1241 "makefile_path" : os
.path
.join("$(MODULE_BUILD_DIR)", MakefileName
),
1242 "platform_name" : self
.PlatformInfo
.Name
,
1243 "platform_guid" : self
.PlatformInfo
.Guid
,
1244 "platform_version" : self
.PlatformInfo
.Version
,
1245 "platform_relative_directory": self
.PlatformInfo
.SourceDir
,
1246 "platform_output_directory" : self
.PlatformInfo
.OutputDir
,
1247 "platform_dir" : MyAgo
.Macros
["PLATFORM_DIR"],
1249 "module_name" : MyAgo
.Name
,
1250 "module_guid" : MyAgo
.Guid
,
1251 "module_name_guid" : MyAgo
.UniqueBaseName
,
1252 "module_version" : MyAgo
.Version
,
1253 "module_type" : MyAgo
.ModuleType
,
1254 "module_file" : MyAgo
.MetaFile
,
1255 "module_file_base_name" : MyAgo
.MetaFile
.BaseName
,
1256 "module_relative_directory" : MyAgo
.SourceDir
,
1257 "module_dir" : mws
.join (MyAgo
.WorkspaceDir
, MyAgo
.SourceDir
),
1259 "architecture" : MyAgo
.Arch
,
1260 "toolchain_tag" : MyAgo
.ToolChain
,
1261 "build_target" : MyAgo
.BuildTarget
,
1263 "platform_build_directory" : self
.PlatformInfo
.BuildDir
,
1264 "module_build_directory" : MyAgo
.BuildDir
,
1265 "module_output_directory" : MyAgo
.OutputDir
,
1266 "module_debug_directory" : MyAgo
.DebugDir
,
1268 "separator" : Separator
,
1269 "module_tool_definitions" : ToolsDef
,
1271 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1272 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1274 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1275 "custom_makefile_content" : CustomMakefile
1278 return MakefileTemplateDict
1280 _TemplateDict
= property(_CreateTemplateDict
)
1282 ## PlatformMakefile class
1284 # This class encapsules makefie and its generation for platform. It uses
1285 # template to generate the content of makefile. The content of makefile will be
1286 # got from PlatformAutoGen object.
1288 class PlatformMakefile(BuildFile
):
1289 ## template used to generate the makefile for platform
1290 _TEMPLATE_
= TemplateString('''\
1294 # Platform Macro Definition
1296 PLATFORM_NAME = ${platform_name}
1297 PLATFORM_GUID = ${platform_guid}
1298 PLATFORM_VERSION = ${platform_version}
1299 PLATFORM_FILE = ${platform_file}
1300 PLATFORM_DIR = ${platform_dir}
1301 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1304 # Build Configuration Macro Definition
1306 TOOLCHAIN = ${toolchain_tag}
1307 TOOLCHAIN_TAG = ${toolchain_tag}
1308 TARGET = ${build_target}
1311 # Build Directory Macro Definition
1313 BUILD_DIR = ${platform_build_directory}
1314 FV_DIR = ${platform_build_directory}${separator}FV
1317 # Shell Command Macro
1319 ${BEGIN}${shell_command_code} = ${shell_command}
1323 MAKE_FILE = ${makefile_path}
1328 all: init build_libraries build_modules
1331 # Initialization target: print build information and create necessary directories
1334 \t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
1335 \t${BEGIN}-@${create_directory_command}
1338 # library build target
1340 libraries: init build_libraries
1343 # module build target
1345 modules: init build_libraries build_modules
1348 # Build all libraries:
1351 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
1352 ${END}\t@cd $(BUILD_DIR)
1355 # Build all modules:
1358 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
1359 ${END}\t@cd $(BUILD_DIR)
1362 # Clean intermediate files
1365 \t${BEGIN}-@${library_build_command} clean
1366 \t${END}${BEGIN}-@${module_build_command} clean
1367 \t${END}@cd $(BUILD_DIR)
1370 # Clean all generated files except to makefile
1373 ${BEGIN}\t${cleanall_command}
1377 # Clean all library files
1380 \t${BEGIN}-@${library_build_command} cleanall
1381 \t${END}@cd $(BUILD_DIR)\n
1384 ## Constructor of PlatformMakefile
1386 # @param ModuleAutoGen Object of PlatformAutoGen class
1388 def __init__(self
, PlatformAutoGen
):
1389 BuildFile
.__init
__(self
, PlatformAutoGen
)
1390 self
.ModuleBuildCommandList
= []
1391 self
.ModuleMakefileList
= []
1392 self
.IntermediateDirectoryList
= []
1393 self
.ModuleBuildDirectoryList
= []
1394 self
.LibraryBuildDirectoryList
= []
1395 self
.LibraryMakeCommandList
= []
1397 # Compose a dict object containing information used to do replacement in template
1398 def _CreateTemplateDict(self
):
1399 Separator
= self
._SEP
_[self
._FileType
]
1401 MyAgo
= self
._AutoGenObject
1402 if "MAKE" not in MyAgo
.ToolDefinition
or "PATH" not in MyAgo
.ToolDefinition
["MAKE"]:
1403 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1404 ExtraData
="[%s]" % str(MyAgo
))
1406 self
.IntermediateDirectoryList
= ["$(BUILD_DIR)"]
1407 self
.ModuleBuildDirectoryList
= self
.GetModuleBuildDirectoryList()
1408 self
.LibraryBuildDirectoryList
= self
.GetLibraryBuildDirectoryList()
1410 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1411 LibraryMakefileList
= []
1412 LibraryMakeCommandList
= []
1413 for D
in self
.LibraryBuildDirectoryList
:
1414 D
= self
.PlaceMacro(D
, {"BUILD_DIR":MyAgo
.BuildDir
})
1415 Makefile
= os
.path
.join(D
, MakefileName
)
1416 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":Makefile
}
1417 LibraryMakefileList
.append(Makefile
)
1418 LibraryMakeCommandList
.append(Command
)
1419 self
.LibraryMakeCommandList
= LibraryMakeCommandList
1421 ModuleMakefileList
= []
1422 ModuleMakeCommandList
= []
1423 for D
in self
.ModuleBuildDirectoryList
:
1424 D
= self
.PlaceMacro(D
, {"BUILD_DIR":MyAgo
.BuildDir
})
1425 Makefile
= os
.path
.join(D
, MakefileName
)
1426 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":Makefile
}
1427 ModuleMakefileList
.append(Makefile
)
1428 ModuleMakeCommandList
.append(Command
)
1430 MakefileTemplateDict
= {
1431 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1432 "makefile_path" : os
.path
.join("$(BUILD_DIR)", MakefileName
),
1433 "make_path" : MyAgo
.ToolDefinition
["MAKE"]["PATH"],
1434 "makefile_name" : MakefileName
,
1435 "platform_name" : MyAgo
.Name
,
1436 "platform_guid" : MyAgo
.Guid
,
1437 "platform_version" : MyAgo
.Version
,
1438 "platform_file" : MyAgo
.MetaFile
,
1439 "platform_relative_directory": MyAgo
.SourceDir
,
1440 "platform_output_directory" : MyAgo
.OutputDir
,
1441 "platform_build_directory" : MyAgo
.BuildDir
,
1442 "platform_dir" : MyAgo
.Macros
["PLATFORM_DIR"],
1444 "toolchain_tag" : MyAgo
.ToolChain
,
1445 "build_target" : MyAgo
.BuildTarget
,
1446 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1447 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1448 "build_architecture_list" : MyAgo
.Arch
,
1449 "architecture" : MyAgo
.Arch
,
1450 "separator" : Separator
,
1451 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1452 "cleanall_command" : self
.GetRemoveDirectoryCommand(self
.IntermediateDirectoryList
),
1453 "library_makefile_list" : LibraryMakefileList
,
1454 "module_makefile_list" : ModuleMakefileList
,
1455 "library_build_command" : LibraryMakeCommandList
,
1456 "module_build_command" : ModuleMakeCommandList
,
1459 return MakefileTemplateDict
1461 ## Get the root directory list for intermediate files of all modules build
1463 # @retval list The list of directory
1465 def GetModuleBuildDirectoryList(self
):
1467 for ModuleAutoGen
in self
._AutoGenObject
.ModuleAutoGenList
:
1468 if not ModuleAutoGen
.IsBinaryModule
:
1469 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, ModuleAutoGen
.BuildDir
))
1472 ## Get the root directory list for intermediate files of all libraries build
1474 # @retval list The list of directory
1476 def GetLibraryBuildDirectoryList(self
):
1478 for LibraryAutoGen
in self
._AutoGenObject
.LibraryAutoGenList
:
1479 if not LibraryAutoGen
.IsBinaryModule
:
1480 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, LibraryAutoGen
.BuildDir
))
1483 _TemplateDict
= property(_CreateTemplateDict
)
1485 ## TopLevelMakefile class
1487 # This class encapsules makefie and its generation for entrance makefile. It
1488 # uses template to generate the content of makefile. The content of makefile
1489 # will be got from WorkspaceAutoGen object.
1491 class TopLevelMakefile(BuildFile
):
1492 ## template used to generate toplevel makefile
1493 _TEMPLATE_
= TemplateString('''${BEGIN}\tGenFds -f ${fdf_file} --conf=${conf_directory} -o ${platform_build_directory} -t ${toolchain_tag} -b ${build_target} -p ${active_platform} -a ${build_architecture_list} ${extra_options}${END}${BEGIN} -r ${fd} ${END}${BEGIN} -i ${fv} ${END}${BEGIN} -C ${cap} ${END}${BEGIN} -D ${macro} ${END}''')
1495 ## Constructor of TopLevelMakefile
1497 # @param Workspace Object of WorkspaceAutoGen class
1499 def __init__(self
, Workspace
):
1500 BuildFile
.__init
__(self
, Workspace
)
1501 self
.IntermediateDirectoryList
= []
1503 # Compose a dict object containing information used to do replacement in template
1504 def _CreateTemplateDict(self
):
1505 Separator
= self
._SEP
_[self
._FileType
]
1507 # any platform autogen object is ok because we just need common information
1508 MyAgo
= self
._AutoGenObject
1510 if "MAKE" not in MyAgo
.ToolDefinition
or "PATH" not in MyAgo
.ToolDefinition
["MAKE"]:
1511 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1512 ExtraData
="[%s]" % str(MyAgo
))
1514 for Arch
in MyAgo
.ArchList
:
1515 self
.IntermediateDirectoryList
.append(Separator
.join(["$(BUILD_DIR)", Arch
]))
1516 self
.IntermediateDirectoryList
.append("$(FV_DIR)")
1518 # TRICK: for not generating GenFds call in makefile if no FDF file
1520 if MyAgo
.FdfFile
is not None and MyAgo
.FdfFile
!= "":
1521 FdfFileList
= [MyAgo
.FdfFile
]
1522 # macros passed to GenFds
1523 MacroList
.append('"%s=%s"' % ("EFI_SOURCE", GlobalData
.gEfiSource
.replace('\\', '\\\\')))
1524 MacroList
.append('"%s=%s"' % ("EDK_SOURCE", GlobalData
.gEdkSource
.replace('\\', '\\\\')))
1526 MacroDict
.update(GlobalData
.gGlobalDefines
)
1527 MacroDict
.update(GlobalData
.gCommandLineDefines
)
1528 MacroDict
.pop("EFI_SOURCE", "dummy")
1529 MacroDict
.pop("EDK_SOURCE", "dummy")
1530 for MacroName
in MacroDict
:
1531 if MacroDict
[MacroName
] != "":
1532 MacroList
.append('"%s=%s"' % (MacroName
, MacroDict
[MacroName
].replace('\\', '\\\\')))
1534 MacroList
.append('"%s"' % MacroName
)
1538 # pass extra common options to external program called in makefile, currently GenFds.exe
1540 LogLevel
= EdkLogger
.GetLevel()
1541 if LogLevel
== EdkLogger
.VERBOSE
:
1542 ExtraOption
+= " -v"
1543 elif LogLevel
<= EdkLogger
.DEBUG_9
:
1544 ExtraOption
+= " -d %d" % (LogLevel
- 1)
1545 elif LogLevel
== EdkLogger
.QUIET
:
1546 ExtraOption
+= " -q"
1548 if GlobalData
.gCaseInsensitive
:
1549 ExtraOption
+= " -c"
1550 if GlobalData
.gEnableGenfdsMultiThread
:
1551 ExtraOption
+= " --genfds-multi-thread"
1552 if GlobalData
.gIgnoreSource
:
1553 ExtraOption
+= " --ignore-sources"
1555 for pcd
in GlobalData
.BuildOptionPcd
:
1557 pcdname
= '.'.join(pcd
[0:3])
1559 pcdname
= '.'.join(pcd
[0:2])
1560 if pcd
[3].startswith('{'):
1561 ExtraOption
+= " --pcd " + pcdname
+ '=' + 'H' + '"' + pcd
[3] + '"'
1563 ExtraOption
+= " --pcd " + pcdname
+ '=' + pcd
[3]
1565 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1566 SubBuildCommandList
= []
1567 for A
in MyAgo
.ArchList
:
1568 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":os
.path
.join("$(BUILD_DIR)", A
, MakefileName
)}
1569 SubBuildCommandList
.append(Command
)
1571 MakefileTemplateDict
= {
1572 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1573 "makefile_path" : os
.path
.join("$(BUILD_DIR)", MakefileName
),
1574 "make_path" : MyAgo
.ToolDefinition
["MAKE"]["PATH"],
1575 "platform_name" : MyAgo
.Name
,
1576 "platform_guid" : MyAgo
.Guid
,
1577 "platform_version" : MyAgo
.Version
,
1578 "platform_build_directory" : MyAgo
.BuildDir
,
1579 "conf_directory" : GlobalData
.gConfDirectory
,
1581 "toolchain_tag" : MyAgo
.ToolChain
,
1582 "build_target" : MyAgo
.BuildTarget
,
1583 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1584 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1585 'arch' : list(MyAgo
.ArchList
),
1586 "build_architecture_list" : ','.join(MyAgo
.ArchList
),
1587 "separator" : Separator
,
1588 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1589 "cleanall_command" : self
.GetRemoveDirectoryCommand(self
.IntermediateDirectoryList
),
1590 "sub_build_command" : SubBuildCommandList
,
1591 "fdf_file" : FdfFileList
,
1592 "active_platform" : str(MyAgo
),
1593 "fd" : MyAgo
.FdTargetList
,
1594 "fv" : MyAgo
.FvTargetList
,
1595 "cap" : MyAgo
.CapTargetList
,
1596 "extra_options" : ExtraOption
,
1597 "macro" : MacroList
,
1600 return MakefileTemplateDict
1602 ## Get the root directory list for intermediate files of all modules build
1604 # @retval list The list of directory
1606 def GetModuleBuildDirectoryList(self
):
1608 for ModuleAutoGen
in self
._AutoGenObject
.ModuleAutoGenList
:
1609 if not ModuleAutoGen
.IsBinaryModule
:
1610 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, ModuleAutoGen
.BuildDir
))
1613 ## Get the root directory list for intermediate files of all libraries build
1615 # @retval list The list of directory
1617 def GetLibraryBuildDirectoryList(self
):
1619 for LibraryAutoGen
in self
._AutoGenObject
.LibraryAutoGenList
:
1620 if not LibraryAutoGen
.IsBinaryModule
:
1621 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, LibraryAutoGen
.BuildDir
))
1624 _TemplateDict
= property(_CreateTemplateDict
)
1626 # This acts like the main() function for the script, unless it is 'import'ed into another script.
1627 if __name__
== '__main__':