2 # Create makefile for MS nmake and GNU make
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
10 from __future__
import absolute_import
11 import Common
.LongFilePathOs
as os
15 import os
.path
as path
16 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
17 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
18 from Common
.BuildToolError
import *
19 from Common
.Misc
import *
20 from Common
.StringUtils
import *
21 from .BuildEngine
import *
22 import Common
.GlobalData
as GlobalData
23 from collections
import OrderedDict
24 from Common
.DataType
import TAB_COMPILER_MSFT
26 ## Regular expression for finding header file inclusions
27 gIncludePattern
= re
.compile(r
"^[ \t]*[#%]?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?
[ \t]*)([-\w
.\\/() \t]+)(?
:[ \t]*[\">]?\
)?
)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
29 ## Regular expression for matching macro used in header file inclusion
30 gMacroPattern = re.compile("([_A
-Z
][_A
-Z0
-9]*)[ \t]*\
((.+)\
)", re.UNICODE)
34 ## pattern for include style in Edk.x code
35 gProtocolDefinition = "Protocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
36 gGuidDefinition = "Guid
/%(HeaderKey)s/%(HeaderKey)s.h
"
37 gArchProtocolDefinition = "ArchProtocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
38 gPpiDefinition = "Ppi
/%(HeaderKey)s/%(HeaderKey)s.h
"
39 gIncludeMacroConversion = {
40 "EFI_PROTOCOL_DEFINITION
" : gProtocolDefinition,
41 "EFI_GUID_DEFINITION
" : gGuidDefinition,
42 "EFI_ARCH_PROTOCOL_DEFINITION
" : gArchProtocolDefinition,
43 "EFI_PROTOCOL_PRODUCER
" : gProtocolDefinition,
44 "EFI_PROTOCOL_CONSUMER
" : gProtocolDefinition,
45 "EFI_PROTOCOL_DEPENDENCY
" : gProtocolDefinition,
46 "EFI_ARCH_PROTOCOL_PRODUCER
" : gArchProtocolDefinition,
47 "EFI_ARCH_PROTOCOL_CONSUMER
" : gArchProtocolDefinition,
48 "EFI_ARCH_PROTOCOL_DEPENDENCY
" : gArchProtocolDefinition,
49 "EFI_PPI_DEFINITION
" : gPpiDefinition,
50 "EFI_PPI_PRODUCER
" : gPpiDefinition,
51 "EFI_PPI_CONSUMER
" : gPpiDefinition,
52 "EFI_PPI_DEPENDENCY
" : gPpiDefinition,
55 ## default makefile type
57 if sys.platform == "win32
":
65 # This base class encapsules build file and its generation. It uses template to generate
66 # the content of build file. The content of build file will be got from AutoGen objects.
68 class BuildFile(object):
69 ## template used to generate the build file (i.e. makefile if using make)
70 _TEMPLATE_ = TemplateString('')
72 _DEFAULT_FILE_NAME_ = "Makefile
"
74 ## default file name for each type of build file
77 "gmake
" : "GNUmakefile
"
80 ## Fixed header string for makefile
81 _MAKEFILE_HEADER = '''#
83 # This file is auto-generated by build utility
91 # Auto-generated makefile for building modules, libraries or platform
95 ## Header string for each type of build file
97 "nmake
" : _MAKEFILE_HEADER % _FILE_NAME_["nmake
"],
98 "gmake
" : _MAKEFILE_HEADER % _FILE_NAME_["gmake
"]
101 ## shell commands which can be used in build file in the form of macro
102 # $(CP) copy file command
103 # $(MV) move file command
104 # $(RM) remove file command
105 # $(MD) create dir command
106 # $(RD) remove dir command
114 "RD
" : "rmdir
/s
/q
",
126 ## directory separator
132 ## directory creation template
134 "nmake
" : 'if not exist %(dir)s $(MD) %(dir)s',
135 "gmake
" : "$
(MD
) %(dir)s"
138 ## directory removal template
140 "nmake
" : 'if exist %(dir)s $(RD) %(dir)s',
141 "gmake
" : "$
(RD
) %(dir)s"
145 "nmake
" : 'if exist %(Src)s $(CP) %(Src)s %(Dst)s',
146 "gmake
" : "test
-f
%(Src)s && $
(CP
) %(Src)s %(Dst)s"
150 "nmake
" : 'if exist %(dir)s cd %(dir)s',
151 "gmake
" : "test
-e
%(dir)s && cd
%(dir)s"
155 "nmake
" : 'if exist %(file)s "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s',
156 "gmake
" : 'test -e %(file)s && "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s'
160 "nmake
" : '!INCLUDE',
164 _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I
", "GCC
" : "-I
", "INTEL
" : "-I
", "RVCT
" : "-I
", "NASM
" : "-I
"}
166 ## Constructor of BuildFile
168 # @param AutoGenObject Object of AutoGen class
170 def __init__(self, AutoGenObject):
171 self._AutoGenObject = AutoGenObject
172 self._FileType = gMakeType
176 # @param FileType Type of build file. Only nmake and gmake are supported now.
178 # @retval TRUE The build file is created or re-created successfully
179 # @retval FALSE The build file exists and is the same as the one to be generated
181 def Generate(self, FileType=gMakeType):
182 if FileType not in self._FILE_NAME_:
183 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid build
type [%s]" % FileType,
184 ExtraData="[%s]" % str(self._AutoGenObject))
185 self._FileType = FileType
186 FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
187 FileName = self._FILE_NAME_[FileType]
188 return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
190 ## Return a list of directory creation command string
192 # @param DirList The list of directory to be created
194 # @retval list The directory creation command list
196 def GetCreateDirectoryCommand(self, DirList):
197 return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
199 ## Return a list of directory removal command string
201 # @param DirList The list of directory to be removed
203 # @retval list The directory removal command list
205 def GetRemoveDirectoryCommand(self, DirList):
206 return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
208 def PlaceMacro(self, Path, MacroDefinitions={}):
209 if Path.startswith("$
("):
212 PathLength = len(Path)
213 for MacroName in MacroDefinitions:
214 MacroValue = MacroDefinitions[MacroName]
215 MacroValueLength = len(MacroValue)
216 if MacroValueLength == 0:
218 if MacroValueLength <= PathLength and Path.startswith(MacroValue):
219 Path = "$
(%s)%s" % (MacroName, Path[MacroValueLength:])
223 ## ModuleMakefile class
225 # This class encapsules makefie and its generation for module. It uses template to generate
226 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
228 class ModuleMakefile(BuildFile):
229 ## template used to generate the makefile for module
230 _TEMPLATE_ = TemplateString('''\
234 # Platform Macro Definition
236 PLATFORM_NAME = ${platform_name}
237 PLATFORM_GUID = ${platform_guid}
238 PLATFORM_VERSION = ${platform_version}
239 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
240 PLATFORM_DIR = ${platform_dir}
241 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
244 # Module Macro Definition
246 MODULE_NAME = ${module_name}
247 MODULE_GUID = ${module_guid}
248 MODULE_NAME_GUID = ${module_name_guid}
249 MODULE_VERSION = ${module_version}
250 MODULE_TYPE = ${module_type}
251 MODULE_FILE = ${module_file}
252 MODULE_FILE_BASE_NAME = ${module_file_base_name}
253 BASE_NAME = $(MODULE_NAME)
254 MODULE_RELATIVE_DIR = ${module_relative_directory}
255 PACKAGE_RELATIVE_DIR = ${package_relative_directory}
256 MODULE_DIR = ${module_dir}
257 FFS_OUTPUT_DIR = ${ffs_output_directory}
259 MODULE_ENTRY_POINT = ${module_entry_point}
260 ARCH_ENTRY_POINT = ${arch_entry_point}
261 IMAGE_ENTRY_POINT = ${image_entry_point}
263 ${BEGIN}${module_extra_defines}
266 # Build Configuration Macro Definition
268 ARCH = ${architecture}
269 TOOLCHAIN = ${toolchain_tag}
270 TOOLCHAIN_TAG = ${toolchain_tag}
271 TARGET = ${build_target}
274 # Build Directory Macro Definition
276 # PLATFORM_BUILD_DIR = ${platform_build_directory}
277 BUILD_DIR = ${platform_build_directory}
278 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
280 MODULE_BUILD_DIR = ${module_build_directory}
281 OUTPUT_DIR = ${module_output_directory}
282 DEBUG_DIR = ${module_debug_directory}
283 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
284 DEST_DIR_DEBUG = $(DEBUG_DIR)
287 # Shell Command Macro
289 ${BEGIN}${shell_command_code} = ${shell_command}
293 # Tools definitions specific to this module
295 ${BEGIN}${module_tool_definitions}
297 MAKE_FILE = ${makefile_path}
302 ${BEGIN}${file_macro}
305 COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
309 # Overridable Target Macro Definitions
311 FORCE_REBUILD = force_build
314 BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
315 CODA_TARGET = ${BEGIN}${remaining_build_target} \\
319 # Default target, which will build dependent libraries in addition to source files
326 # Target used when called from platform makefile, which will bypass the build of dependent libraries
329 pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
335 mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
338 # Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
341 tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
344 # Phony target which is used to force executing commands for a target
350 # Target to update the FD
356 # Initialization target: print build information and create necessary directories
361 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
364 ${BEGIN}\t-@${create_directory_command}\n${END}
367 \t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
373 \t${BEGIN}@"$
(MAKE
)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
374 \t${END}@cd $(MODULE_BUILD_DIR)
377 # Build Flash Device Image
380 \t@"$
(MAKE
)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
381 \t@cd $(MODULE_BUILD_DIR)
384 # Individual Object Build Targets
386 ${BEGIN}${file_build_target}
390 # clean all intermediate files
393 \t${BEGIN}${clean_command}
394 \t${END}\t$(RM) AutoGenTimeStamp
397 # clean all generated files
400 ${BEGIN}\t${cleanall_command}
401 ${END}\t$(RM) *.pdb *.idb > NUL 2>&1
402 \t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
403 \t$(RM) AutoGenTimeStamp
406 # clean all dependent libraries built
409 \t${BEGIN}-@${library_build_command} cleanall
410 \t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
412 _FILE_MACRO_TEMPLATE = TemplateString("${macro_name}
= ${BEGIN}
\\\n ${source_file}${END}
\n")
413 _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target}
: ${deps}
\n${END}
\t${cmd}
\n")
415 ## Constructor of ModuleMakefile
417 # @param ModuleAutoGen Object of ModuleAutoGen class
419 def __init__(self, ModuleAutoGen):
420 BuildFile.__init__(self, ModuleAutoGen)
421 self.PlatformInfo = self._AutoGenObject.PlatformInfo
423 self.ResultFileList = []
424 self.IntermediateDirectoryList = ["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]
426 self.FileBuildTargetList = [] # [(src, target string)]
427 self.BuildTargetList = [] # [target string]
428 self.PendingBuildTargetList = [] # [FileBuildRule objects]
429 self.CommonFileDependency = []
430 self.FileListMacros = {}
431 self.ListFileMacros = {}
434 self.LibraryBuildCommandList = []
435 self.LibraryFileList = []
436 self.LibraryMakefileList = []
437 self.LibraryBuildDirectoryList = []
438 self.SystemLibraryList = []
439 self.Macros = OrderedDict()
440 self.Macros["OUTPUT_DIR
" ] = self._AutoGenObject.Macros["OUTPUT_DIR
"]
441 self.Macros["DEBUG_DIR
" ] = self._AutoGenObject.Macros["DEBUG_DIR
"]
442 self.Macros["MODULE_BUILD_DIR
"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR
"]
443 self.Macros["BIN_DIR
" ] = self._AutoGenObject.Macros["BIN_DIR
"]
444 self.Macros["BUILD_DIR
" ] = self._AutoGenObject.Macros["BUILD_DIR
"]
445 self.Macros["WORKSPACE
" ] = self._AutoGenObject.Macros["WORKSPACE
"]
446 self.Macros["FFS_OUTPUT_DIR
" ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR
"]
447 self.GenFfsList = ModuleAutoGen.GenFfsList
448 self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']
449 self.FfsOutputFileList = []
451 # Compose a dict object containing information used to do replacement in template
453 def _TemplateDict(self):
454 if self._FileType not in self._SEP_:
455 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid Makefile
type [%s]" % self._FileType,
456 ExtraData="[%s]" % str(self._AutoGenObject))
457 MyAgo = self._AutoGenObject
458 Separator = self._SEP_[self._FileType]
460 # break build if no source files and binary files are found
461 if len(MyAgo.SourceFileList) == 0 and len(MyAgo.BinaryFileList) == 0:
462 EdkLogger.error("build
", AUTOGEN_ERROR, "No files to be built
in module
[%s, %s, %s]"
463 % (MyAgo.BuildTarget, MyAgo.ToolChain, MyAgo.Arch),
464 ExtraData="[%s]" % str(MyAgo))
466 # convert dependent libraries to build command
467 self.ProcessDependentLibrary()
468 if len(MyAgo.Module.ModuleEntryPointList) > 0:
469 ModuleEntryPoint = MyAgo.Module.ModuleEntryPointList[0]
471 ModuleEntryPoint = "_ModuleEntryPoint
"
473 ArchEntryPoint = ModuleEntryPoint
475 if MyAgo.Arch == "EBC
":
476 # EBC compiler always use "EfiStart
" as entry point. Only applies to EdkII modules
477 ImageEntryPoint = "EfiStart
"
479 # EdkII modules always use "_ModuleEntryPoint
" as entry point
480 ImageEntryPoint = "_ModuleEntryPoint
"
482 for k, v in MyAgo.Module.Defines.items():
483 if k not in MyAgo.Macros:
486 if 'MODULE_ENTRY_POINT' not in MyAgo.Macros:
487 MyAgo.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint
488 if 'ARCH_ENTRY_POINT' not in MyAgo.Macros:
489 MyAgo.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint
490 if 'IMAGE_ENTRY_POINT' not in MyAgo.Macros:
491 MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint
493 PCI_COMPRESS_Flag = False
494 for k, v in MyAgo.Module.Defines.items():
495 if 'PCI_COMPRESS' == k and 'TRUE' == v:
496 PCI_COMPRESS_Flag = True
500 IncPrefix = self._INC_FLAG_[MyAgo.ToolChainFamily]
501 for Tool in MyAgo.BuildOption:
502 for Attr in MyAgo.BuildOption[Tool]:
503 Value = MyAgo.BuildOption[Tool][Attr]
507 ToolsDef.append("%s = %s" % (Tool, Value))
509 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
512 # Remove duplicated include path, if any
514 Value = RemoveDupOption(Value, IncPrefix, MyAgo.IncludePathList)
515 if Tool == "OPTROM
" and PCI_COMPRESS_Flag:
516 ValueList = Value.split()
518 for i, v in enumerate(ValueList):
521 Value = ' '.join(ValueList)
523 ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
526 # generate the Response file and Response flag
527 RespDict = self.CommandExceedLimit()
528 RespFileList = os.path.join(MyAgo.OutputDir, 'respfilelist.txt')
530 RespFileListContent = ''
531 for Resp in RespDict:
532 RespFile = os.path.join(MyAgo.OutputDir, str(Resp).lower() + '.txt')
533 StrList = RespDict[Resp].split(' ')
538 UnexpandMacro.append(Str)
541 UnexpandMacroStr = ' '.join(UnexpandMacro)
542 NewRespStr = ' '.join(NewStr)
543 SaveFileOnChange(RespFile, NewRespStr, False)
544 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))
545 RespFileListContent += '@' + RespFile + TAB_LINE_BREAK
546 RespFileListContent += NewRespStr + TAB_LINE_BREAK
547 SaveFileOnChange(RespFileList, RespFileListContent, False)
549 if os.path.exists(RespFileList):
550 os.remove(RespFileList)
552 # convert source files and binary files to build targets
553 self.ResultFileList = [str(T.Target) for T in MyAgo.CodaTargetList]
554 if len(self.ResultFileList) == 0 and len(MyAgo.SourceFileList) != 0:
555 EdkLogger.error("build
", AUTOGEN_ERROR, "Nothing to build
",
556 ExtraData="[%s]" % str(MyAgo))
558 self.ProcessBuildTargetList()
559 self.ParserGenerateFfsCmd()
561 # Generate macros used to represent input files
562 FileMacroList = [] # macro name = file list
563 for FileListMacro in self.FileListMacros:
564 FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
566 "macro_name
" : FileListMacro,
567 "source_file
" : self.FileListMacros[FileListMacro]
570 FileMacroList.append(FileMacro)
572 # INC_LIST is special
575 for P in MyAgo.IncludePathList:
576 IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))
577 if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
578 self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)
579 FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
581 "macro_name
" : "INC
",
582 "source_file
" : IncludePathList
585 FileMacroList.append(FileMacro)
586 # Add support when compiling .nasm source files
587 for File in self.FileCache.keys():
588 if not str(File).endswith('.nasm'):
591 for P in MyAgo.IncludePathList:
592 IncludePath = self._INC_FLAG_['NASM'] + self.PlaceMacro(P, self.Macros)
593 if IncludePath.endswith(os.sep):
594 IncludePath = IncludePath.rstrip(os.sep)
595 # When compiling .nasm files, need to add a literal backslash at each path
596 # To specify a literal backslash at the end of the line, precede it with a caret (^)
597 if P == MyAgo.IncludePathList[-1] and os.sep == '\\':
598 IncludePath = ''.join([IncludePath, '^', os.sep])
600 IncludePath = os.path.join(IncludePath, '')
601 IncludePathList.append(IncludePath)
602 FileMacroList.append(self._FILE_MACRO_TEMPLATE.Replace({"macro_name
": "NASM_INC
", "source_file
": IncludePathList}))
605 # Generate macros used to represent files containing list of input files
606 for ListFileMacro in self.ListFileMacros:
607 ListFileName = os.path.join(MyAgo.OutputDir, "%s.lst
" % ListFileMacro.lower()[:len(ListFileMacro) - 5])
608 FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
611 "\n".join(self.ListFileMacros[ListFileMacro]),
617 MakefileName = self._FILE_NAME_[self._FileType]
618 LibraryMakeCommandList = []
619 for D in self.LibraryBuildDirectoryList:
620 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}
621 LibraryMakeCommandList.append(Command)
623 package_rel_dir = MyAgo.SourceDir
624 current_dir = self.Macros["WORKSPACE
"]
626 while not found and os.sep in package_rel_dir:
627 index = package_rel_dir.index(os.sep)
628 current_dir = mws.join(current_dir, package_rel_dir[:index])
629 if os.path.exists(current_dir):
630 for fl in os.listdir(current_dir):
631 if fl.endswith('.dec'):
634 package_rel_dir = package_rel_dir[index + 1:]
636 MakefileTemplateDict = {
637 "makefile_header
" : self._FILE_HEADER_[self._FileType],
638 "makefile_path
" : os.path.join("$
(MODULE_BUILD_DIR
)", MakefileName),
639 "makefile_name
" : MakefileName,
640 "platform_name
" : self.PlatformInfo.Name,
641 "platform_guid
" : self.PlatformInfo.Guid,
642 "platform_version
" : self.PlatformInfo.Version,
643 "platform_relative_directory
": self.PlatformInfo.SourceDir,
644 "platform_output_directory
" : self.PlatformInfo.OutputDir,
645 "ffs_output_directory
" : MyAgo.Macros["FFS_OUTPUT_DIR
"],
646 "platform_dir
" : MyAgo.Macros["PLATFORM_DIR
"],
648 "module_name
" : MyAgo.Name,
649 "module_guid
" : MyAgo.Guid,
650 "module_name_guid
" : MyAgo.UniqueBaseName,
651 "module_version
" : MyAgo.Version,
652 "module_type
" : MyAgo.ModuleType,
653 "module_file
" : MyAgo.MetaFile.Name,
654 "module_file_base_name
" : MyAgo.MetaFile.BaseName,
655 "module_relative_directory
" : MyAgo.SourceDir,
656 "module_dir
" : mws.join (self.Macros["WORKSPACE
"], MyAgo.SourceDir),
657 "package_relative_directory
": package_rel_dir,
658 "module_extra_defines
" : ["%s = %s" % (k, v) for k, v in MyAgo.Module.Defines.items()],
660 "architecture
" : MyAgo.Arch,
661 "toolchain_tag
" : MyAgo.ToolChain,
662 "build_target
" : MyAgo.BuildTarget,
664 "platform_build_directory
" : self.PlatformInfo.BuildDir,
665 "module_build_directory
" : MyAgo.BuildDir,
666 "module_output_directory
" : MyAgo.OutputDir,
667 "module_debug_directory
" : MyAgo.DebugDir,
669 "separator
" : Separator,
670 "module_tool_definitions
" : ToolsDef,
672 "shell_command_code
" : list(self._SHELL_CMD_[self._FileType].keys()),
673 "shell_command
" : list(self._SHELL_CMD_[self._FileType].values()),
675 "module_entry_point
" : ModuleEntryPoint,
676 "image_entry_point
" : ImageEntryPoint,
677 "arch_entry_point
" : ArchEntryPoint,
678 "remaining_build_target
" : self.ResultFileList,
679 "common_dependency_file
" : self.CommonFileDependency,
680 "create_directory_command
" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
681 "clean_command
" : self.GetRemoveDirectoryCommand(["$
(OUTPUT_DIR
)"]),
682 "cleanall_command
" : self.GetRemoveDirectoryCommand(["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]),
683 "dependent_library_build_directory
" : self.LibraryBuildDirectoryList,
684 "library_build_command
" : LibraryMakeCommandList,
685 "file_macro
" : FileMacroList,
686 "file_build_target
" : self.BuildTargetList,
687 "backward_compatible_target
": BcTargetList,
690 return MakefileTemplateDict
692 def ParserGenerateFfsCmd(self):
693 #Add Ffs cmd to self.BuildTargetList
697 for Cmd in self.GenFfsList:
699 for CopyCmd in Cmd[2]:
701 Src = self.ReplaceMacro(Src)
702 Dst = self.ReplaceMacro(Dst)
703 if Dst not in self.ResultFileList:
704 self.ResultFileList.append(Dst)
705 if '%s :' %(Dst) not in self.BuildTargetList:
706 self.BuildTargetList.append("%s :" %(Dst))
707 self.BuildTargetList.append('\t' + self._CP_TEMPLATE_[self._FileType] %{'Src': Src, 'Dst': Dst})
710 for index, Str in enumerate(FfsCmdList):
712 OutputFile = FfsCmdList[index + 1]
713 if '-i' == Str or "-oi
" == Str:
714 if DepsFileList == []:
715 DepsFileList = [FfsCmdList[index + 1]]
717 DepsFileList.append(FfsCmdList[index + 1])
718 DepsFileString = ' '.join(DepsFileList).strip()
719 if DepsFileString == '':
721 OutputFile = self.ReplaceMacro(OutputFile)
722 self.ResultFileList.append(OutputFile)
723 DepsFileString = self.ReplaceMacro(DepsFileString)
724 self.BuildTargetList.append('%s : %s' % (OutputFile, DepsFileString))
725 CmdString = ' '.join(FfsCmdList).strip()
726 CmdString = self.ReplaceMacro(CmdString)
727 self.BuildTargetList.append('\t%s' % CmdString)
729 self.ParseSecCmd(DepsFileList, Cmd[1])
730 for SecOutputFile, SecDepsFile, SecCmd in self.FfsOutputFileList :
731 self.BuildTargetList.append('%s : %s' % (self.ReplaceMacro(SecOutputFile), self.ReplaceMacro(SecDepsFile)))
732 self.BuildTargetList.append('\t%s' % self.ReplaceMacro(SecCmd))
733 self.FfsOutputFileList = []
735 def ParseSecCmd(self, OutputFileList, CmdTuple):
736 for OutputFile in OutputFileList:
737 for SecCmdStr in CmdTuple:
739 SecCmdList = SecCmdStr.split()
740 CmdName = SecCmdList[0]
741 for index, CmdItem in enumerate(SecCmdList):
742 if '-o' == CmdItem and OutputFile == SecCmdList[index + 1]:
744 while index + 1 < len(SecCmdList):
745 if not SecCmdList[index+1].startswith('-'):
746 SecDepsFileList.append(SecCmdList[index + 1])
748 if CmdName == 'Trim':
749 SecDepsFileList.append(os.path.join('$(DEBUG_DIR)', os.path.basename(OutputFile).replace('offset', 'efi')))
750 if OutputFile.endswith('.ui') or OutputFile.endswith('.ver'):
751 SecDepsFileList.append(os.path.join('$(MODULE_DIR)', '$(MODULE_FILE)'))
752 self.FfsOutputFileList.append((OutputFile, ' '.join(SecDepsFileList), SecCmdStr))
753 if len(SecDepsFileList) > 0:
754 self.ParseSecCmd(SecDepsFileList, CmdTuple)
759 def ReplaceMacro(self, str):
760 for Macro in self.MacroList:
761 if self._AutoGenObject.Macros[Macro] and self._AutoGenObject.Macros[Macro] in str:
762 str = str.replace(self._AutoGenObject.Macros[Macro], '$(' + Macro + ')')
765 def CommandExceedLimit(self):
767 'CC' : { 'Macro' : '$(CC_FLAGS)', 'Value' : False},
768 'PP' : { 'Macro' : '$(PP_FLAGS)', 'Value' : False},
769 'APP' : { 'Macro' : '$(APP_FLAGS)', 'Value' : False},
770 'ASLPP' : { 'Macro' : '$(ASLPP_FLAGS)', 'Value' : False},
771 'VFRPP' : { 'Macro' : '$(VFRPP_FLAGS)', 'Value' : False},
772 'ASM' : { 'Macro' : '$(ASM_FLAGS)', 'Value' : False},
773 'ASLCC' : { 'Macro' : '$(ASLCC_FLAGS)', 'Value' : False},
778 IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]
780 # base on the source files to decide the file type
781 for File in self._AutoGenObject.SourceFileList:
782 for type in self._AutoGenObject.FileTypes:
783 if File in self._AutoGenObject.FileTypes[type]:
784 if type not in FileTypeList:
785 FileTypeList.append(type)
787 # calculate the command-line length
789 for type in FileTypeList:
790 BuildTargets = self._AutoGenObject.BuildRules[type].BuildTargets
791 for Target in BuildTargets:
792 CommandList = BuildTargets[Target].Commands
793 for SingleCommand in CommandList:
795 SingleCommandLength = len(SingleCommand)
796 SingleCommandList = SingleCommand.split()
797 if len(SingleCommandList) > 0:
798 for Flag in FlagDict:
799 if '$('+ Flag +')' in SingleCommandList[0]:
803 if 'PATH' not in self._AutoGenObject.BuildOption[Tool]:
804 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))
805 SingleCommandLength += len(self._AutoGenObject.BuildOption[Tool]['PATH
'])
806 for item in SingleCommandList[1:]:
807 if FlagDict[Tool]['Macro
'] in item:
808 if 'FLAGS
' not in self._AutoGenObject.BuildOption[Tool]:
809 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))
810 Str = self._AutoGenObject.BuildOption[Tool]['FLAGS']
811 for Option in self._AutoGenObject.BuildOption:
812 for Attr in self._AutoGenObject.BuildOption[Option]:
813 if Str.find(Option + '_' + Attr) != -1:
814 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
815 while(Str.find('$(') != -1):
816 for macro in self._AutoGenObject.Macros:
817 MacroName = '$('+ macro + ')'
818 if (Str.find(MacroName) != -1):
819 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
823 SingleCommandLength += len(Str)
824 elif '$(INC)' in item:
825 SingleCommandLength += self._AutoGenObject.IncludePathLength + len(IncPrefix) * len(self._AutoGenObject.IncludePathList)
826 elif item.find('$(') != -1:
828 for Option in self._AutoGenObject.BuildOption:
829 for Attr in self._AutoGenObject.BuildOption[Option]:
830 if Str.find(Option + '_' + Attr) != -1:
831 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
832 while(Str.find('$(') != -1):
833 for macro in self._AutoGenObject.Macros:
834 MacroName = '$('+ macro + ')'
835 if (Str.find(MacroName) != -1):
836 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
840 SingleCommandLength += len(Str)
842 if SingleCommandLength > GlobalData.gCommandMaxLength:
843 FlagDict[Tool]['Value'] = True
845 # generate the response file content by combine the FLAGS and INC
846 for Flag in FlagDict:
847 if FlagDict[Flag]['Value']:
849 RespMacro = FlagDict[Flag]['Macro'].replace('FLAGS', 'RESP')
850 Value = self._AutoGenObject.BuildOption[Flag]['FLAGS']
851 for inc in self._AutoGenObject.IncludePathList:
852 Value += ' ' + IncPrefix + inc
853 for Option in self._AutoGenObject.BuildOption:
854 for Attr in self._AutoGenObject.BuildOption[Option]:
855 if Value.find(Option + '_' + Attr) != -1:
856 Value = Value.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
857 while (Value.find('$(') != -1):
858 for macro in self._AutoGenObject.Macros:
859 MacroName = '$('+ macro + ')'
860 if (Value.find(MacroName) != -1):
861 Value = Value.replace(MacroName, self._AutoGenObject.Macros[macro])
866 if self._AutoGenObject.ToolChainFamily == 'GCC':
867 RespDict[Key] = Value.replace('\\', '/')
869 RespDict[Key] = Value
870 for Target in BuildTargets:
871 for i, SingleCommand in enumerate(BuildTargets[Target].Commands):
872 if FlagDict[Flag]['Macro'] in SingleCommand:
873 BuildTargets[Target].Commands[i] = SingleCommand.replace('$(INC)', '').replace(FlagDict[Flag]['Macro'], RespMacro)
876 def ProcessBuildTargetList(self):
878 # Search dependency file list for each source file
880 ForceIncludedFile = []
881 for File in self._AutoGenObject.AutoGenFileList:
883 ForceIncludedFile.append(File)
886 for Target in self._AutoGenObject.IntroTargetList:
887 SourceFileList.extend(Target.Inputs)
888 OutPutFileList.extend(Target.Outputs)
891 for Item in OutPutFileList:
892 if Item in SourceFileList:
893 SourceFileList.remove(Item)
895 FileDependencyDict = self.GetFileDependency(
898 self._AutoGenObject.IncludePathList + self._AutoGenObject.BuildOptionIncPathList
901 for File,Dependency in FileDependencyDict.items():
903 FileDependencyDict[File] = ['$(FORCE_REBUILD)']
906 self._AutoGenObject.AutoGenDepSet |= set(Dependency)
909 if File.Ext not in [".c
", ".C
"] or File.Name == "AutoGen
.c
":
912 DepSet = set(Dependency)
914 DepSet &= set(Dependency)
915 # in case nothing in SourceFileList
919 # Extract common files list in the dependency files
922 self.CommonFileDependency.append(self.PlaceMacro(File.Path, self.Macros))
924 for File in FileDependencyDict:
926 if File.Ext not in [".c
", ".C
"] or File.Name == "AutoGen
.c
":
928 NewDepSet = set(FileDependencyDict[File])
930 FileDependencyDict[File] = ["$
(COMMON_DEPS
)"] + list(NewDepSet)
932 # Convert target description object to target string in makefile
933 for Type in self._AutoGenObject.Targets:
934 for T in self._AutoGenObject.Targets[Type]:
935 # Generate related macros if needed
936 if T.GenFileListMacro and T.FileListMacro not in self.FileListMacros:
937 self.FileListMacros[T.FileListMacro] = []
938 if T.GenListFile and T.ListFileMacro not in self.ListFileMacros:
939 self.ListFileMacros[T.ListFileMacro] = []
940 if T.GenIncListFile and T.IncListFileMacro not in self.ListFileMacros:
941 self.ListFileMacros[T.IncListFileMacro] = []
944 # Add force-dependencies
945 for Dep in T.Dependencies:
946 Deps.append(self.PlaceMacro(str(Dep), self.Macros))
947 # Add inclusion-dependencies
948 if len(T.Inputs) == 1 and T.Inputs[0] in FileDependencyDict:
949 for F in FileDependencyDict[T.Inputs[0]]:
950 Deps.append(self.PlaceMacro(str(F), self.Macros))
951 # Add source-dependencies
953 NewFile = self.PlaceMacro(str(F), self.Macros)
954 # In order to use file list macro as dependency
956 # gnu tools need forward slash path separator, even on Windows
957 self.ListFileMacros[T.ListFileMacro].append(str(F).replace ('\\', '/'))
958 self.FileListMacros[T.FileListMacro].append(NewFile)
959 elif T.GenFileListMacro:
960 self.FileListMacros[T.FileListMacro].append(NewFile)
964 # Use file list macro as dependency
965 if T.GenFileListMacro:
966 Deps.append("$
(%s)" % T.FileListMacro)
967 if Type in [TAB_OBJECT_FILE, TAB_STATIC_LIBRARY]:
968 Deps.append("$
(%s)" % T.ListFileMacro)
971 "target
" : self.PlaceMacro(T.Target.Path, self.Macros),
972 "cmd
" : "\n\t".join(T.Commands),
975 self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))
977 ## For creating makefile targets for dependent libraries
978 def ProcessDependentLibrary(self):
979 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
980 if not LibraryAutoGen.IsBinaryModule:
981 self.LibraryBuildDirectoryList.append(self.PlaceMacro(LibraryAutoGen.BuildDir, self.Macros))
983 ## Return a list containing source file's dependencies
985 # @param FileList The list of source files
986 # @param ForceInculeList The list of files which will be included forcely
987 # @param SearchPathList The list of search path
989 # @retval dict The mapping between source file path and its dependencies
991 def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):
994 Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)
997 ## Find dependencies for one source file
999 # By searching recursively "#include" directive in file, find out all the
1000 # files needed by given source file. The dependencies will be only searched
1001 # in given search path list.
1003 # @param File The source file
1004 # @param ForceInculeList The list of files which will be included forcely
1005 # @param SearchPathList The list of search path
1007 # @retval list The list of files the given source file depends on
1009 def GetDependencyList(self
, File
, ForceList
, SearchPathList
):
1010 EdkLogger
.debug(EdkLogger
.DEBUG_1
, "Try to get dependency files for %s" % File
)
1011 FileStack
= [File
] + ForceList
1012 DependencySet
= set()
1014 if self
._AutoGenObject
.Arch
not in gDependencyDatabase
:
1015 gDependencyDatabase
[self
._AutoGenObject
.Arch
] = {}
1016 DepDb
= gDependencyDatabase
[self
._AutoGenObject
.Arch
]
1018 while len(FileStack
) > 0:
1021 FullPathDependList
= []
1022 if F
in self
.FileCache
:
1023 for CacheFile
in self
.FileCache
[F
]:
1024 FullPathDependList
.append(CacheFile
)
1025 if CacheFile
not in DependencySet
:
1026 FileStack
.append(CacheFile
)
1027 DependencySet
.update(FullPathDependList
)
1030 CurrentFileDependencyList
= []
1032 CurrentFileDependencyList
= DepDb
[F
]
1035 Fd
= open(F
.Path
, 'rb')
1036 FileContent
= Fd
.read()
1038 except BaseException
as X
:
1039 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=F
.Path
+ "\n\t" + str(X
))
1040 if len(FileContent
) == 0:
1043 if FileContent
[0] == 0xff or FileContent
[0] == 0xfe:
1044 FileContent
= FileContent
.decode('utf-16')
1046 FileContent
= FileContent
.decode()
1048 # The file is not txt file. for example .mcb file
1050 IncludedFileList
= gIncludePattern
.findall(FileContent
)
1052 for Inc
in IncludedFileList
:
1054 # if there's macro used to reference header file, expand it
1055 HeaderList
= gMacroPattern
.findall(Inc
)
1056 if len(HeaderList
) == 1 and len(HeaderList
[0]) == 2:
1057 HeaderType
= HeaderList
[0][0]
1058 HeaderKey
= HeaderList
[0][1]
1059 if HeaderType
in gIncludeMacroConversion
:
1060 Inc
= gIncludeMacroConversion
[HeaderType
] % {"HeaderKey" : HeaderKey
}
1062 # not known macro used in #include, always build the file by
1063 # returning a empty dependency
1064 self
.FileCache
[File
] = []
1066 Inc
= os
.path
.normpath(Inc
)
1067 CurrentFileDependencyList
.append(Inc
)
1068 DepDb
[F
] = CurrentFileDependencyList
1070 CurrentFilePath
= F
.Dir
1071 PathList
= [CurrentFilePath
] + SearchPathList
1072 for Inc
in CurrentFileDependencyList
:
1073 for SearchPath
in PathList
:
1074 FilePath
= os
.path
.join(SearchPath
, Inc
)
1075 if FilePath
in gIsFileMap
:
1076 if not gIsFileMap
[FilePath
]:
1078 # If isfile is called too many times, the performance is slow down.
1079 elif not os
.path
.isfile(FilePath
):
1080 gIsFileMap
[FilePath
] = False
1083 gIsFileMap
[FilePath
] = True
1084 FilePath
= PathClass(FilePath
)
1085 FullPathDependList
.append(FilePath
)
1086 if FilePath
not in DependencySet
:
1087 FileStack
.append(FilePath
)
1090 EdkLogger
.debug(EdkLogger
.DEBUG_9
, "%s included by %s was not found "\
1091 "in any given path:\n\t%s" % (Inc
, F
, "\n\t".join(SearchPathList
)))
1093 self
.FileCache
[F
] = FullPathDependList
1094 DependencySet
.update(FullPathDependList
)
1096 DependencySet
.update(ForceList
)
1097 if File
in DependencySet
:
1098 DependencySet
.remove(File
)
1099 DependencyList
= list(DependencySet
) # remove duplicate ones
1101 return DependencyList
1103 ## CustomMakefile class
1105 # This class encapsules makefie and its generation for module. It uses template to generate
1106 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
1108 class CustomMakefile(BuildFile
):
1109 ## template used to generate the makefile for module with custom makefile
1110 _TEMPLATE_
= TemplateString('''\
1114 # Platform Macro Definition
1116 PLATFORM_NAME = ${platform_name}
1117 PLATFORM_GUID = ${platform_guid}
1118 PLATFORM_VERSION = ${platform_version}
1119 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
1120 PLATFORM_DIR = ${platform_dir}
1121 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1124 # Module Macro Definition
1126 MODULE_NAME = ${module_name}
1127 MODULE_GUID = ${module_guid}
1128 MODULE_NAME_GUID = ${module_name_guid}
1129 MODULE_VERSION = ${module_version}
1130 MODULE_TYPE = ${module_type}
1131 MODULE_FILE = ${module_file}
1132 MODULE_FILE_BASE_NAME = ${module_file_base_name}
1133 BASE_NAME = $(MODULE_NAME)
1134 MODULE_RELATIVE_DIR = ${module_relative_directory}
1135 MODULE_DIR = ${module_dir}
1138 # Build Configuration Macro Definition
1140 ARCH = ${architecture}
1141 TOOLCHAIN = ${toolchain_tag}
1142 TOOLCHAIN_TAG = ${toolchain_tag}
1143 TARGET = ${build_target}
1146 # Build Directory Macro Definition
1148 # PLATFORM_BUILD_DIR = ${platform_build_directory}
1149 BUILD_DIR = ${platform_build_directory}
1150 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
1151 LIB_DIR = $(BIN_DIR)
1152 MODULE_BUILD_DIR = ${module_build_directory}
1153 OUTPUT_DIR = ${module_output_directory}
1154 DEBUG_DIR = ${module_debug_directory}
1155 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
1156 DEST_DIR_DEBUG = $(DEBUG_DIR)
1159 # Tools definitions specific to this module
1161 ${BEGIN}${module_tool_definitions}
1163 MAKE_FILE = ${makefile_path}
1166 # Shell Command Macro
1168 ${BEGIN}${shell_command_code} = ${shell_command}
1171 ${custom_makefile_content}
1174 # Target used when called from platform makefile, which will bypass the build of dependent libraries
1187 # Build Target used in multi-thread build mode, which no init target is needed
1193 # Initialization target: print build information and create necessary directories
1196 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
1197 ${BEGIN}\t-@${create_directory_command}\n${END}\
1201 ## Constructor of CustomMakefile
1203 # @param ModuleAutoGen Object of ModuleAutoGen class
1205 def __init__(self
, ModuleAutoGen
):
1206 BuildFile
.__init
__(self
, ModuleAutoGen
)
1207 self
.PlatformInfo
= self
._AutoGenObject
.PlatformInfo
1208 self
.IntermediateDirectoryList
= ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
1210 # Compose a dict object containing information used to do replacement in template
1212 def _TemplateDict(self
):
1213 Separator
= self
._SEP
_[self
._FileType
]
1214 MyAgo
= self
._AutoGenObject
1215 if self
._FileType
not in MyAgo
.CustomMakefile
:
1216 EdkLogger
.error('build', OPTION_NOT_SUPPORTED
, "No custom makefile for %s" % self
._FileType
,
1217 ExtraData
="[%s]" % str(MyAgo
))
1218 MakefilePath
= mws
.join(
1220 MyAgo
.CustomMakefile
[self
._FileType
]
1223 CustomMakefile
= open(MakefilePath
, 'r').read()
1225 EdkLogger
.error('build', FILE_OPEN_FAILURE
, File
=str(MyAgo
),
1226 ExtraData
=MyAgo
.CustomMakefile
[self
._FileType
])
1230 for Tool
in MyAgo
.BuildOption
:
1231 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
1234 for Attr
in MyAgo
.BuildOption
[Tool
]:
1235 if Attr
== "FAMILY":
1237 elif Attr
== "PATH":
1238 ToolsDef
.append("%s = %s" % (Tool
, MyAgo
.BuildOption
[Tool
][Attr
]))
1240 ToolsDef
.append("%s_%s = %s" % (Tool
, Attr
, MyAgo
.BuildOption
[Tool
][Attr
]))
1243 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1244 MakefileTemplateDict
= {
1245 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1246 "makefile_path" : os
.path
.join("$(MODULE_BUILD_DIR)", MakefileName
),
1247 "platform_name" : self
.PlatformInfo
.Name
,
1248 "platform_guid" : self
.PlatformInfo
.Guid
,
1249 "platform_version" : self
.PlatformInfo
.Version
,
1250 "platform_relative_directory": self
.PlatformInfo
.SourceDir
,
1251 "platform_output_directory" : self
.PlatformInfo
.OutputDir
,
1252 "platform_dir" : MyAgo
.Macros
["PLATFORM_DIR"],
1254 "module_name" : MyAgo
.Name
,
1255 "module_guid" : MyAgo
.Guid
,
1256 "module_name_guid" : MyAgo
.UniqueBaseName
,
1257 "module_version" : MyAgo
.Version
,
1258 "module_type" : MyAgo
.ModuleType
,
1259 "module_file" : MyAgo
.MetaFile
,
1260 "module_file_base_name" : MyAgo
.MetaFile
.BaseName
,
1261 "module_relative_directory" : MyAgo
.SourceDir
,
1262 "module_dir" : mws
.join (MyAgo
.WorkspaceDir
, MyAgo
.SourceDir
),
1264 "architecture" : MyAgo
.Arch
,
1265 "toolchain_tag" : MyAgo
.ToolChain
,
1266 "build_target" : MyAgo
.BuildTarget
,
1268 "platform_build_directory" : self
.PlatformInfo
.BuildDir
,
1269 "module_build_directory" : MyAgo
.BuildDir
,
1270 "module_output_directory" : MyAgo
.OutputDir
,
1271 "module_debug_directory" : MyAgo
.DebugDir
,
1273 "separator" : Separator
,
1274 "module_tool_definitions" : ToolsDef
,
1276 "shell_command_code" : list(self
._SHELL
_CMD
_[self
._FileType
].keys()),
1277 "shell_command" : list(self
._SHELL
_CMD
_[self
._FileType
].values()),
1279 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1280 "custom_makefile_content" : CustomMakefile
1283 return MakefileTemplateDict
1285 ## PlatformMakefile class
1287 # This class encapsules makefie and its generation for platform. It uses
1288 # template to generate the content of makefile. The content of makefile will be
1289 # got from PlatformAutoGen object.
1291 class PlatformMakefile(BuildFile
):
1292 ## template used to generate the makefile for platform
1293 _TEMPLATE_
= TemplateString('''\
1297 # Platform Macro Definition
1299 PLATFORM_NAME = ${platform_name}
1300 PLATFORM_GUID = ${platform_guid}
1301 PLATFORM_VERSION = ${platform_version}
1302 PLATFORM_FILE = ${platform_file}
1303 PLATFORM_DIR = ${platform_dir}
1304 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1307 # Build Configuration Macro Definition
1309 TOOLCHAIN = ${toolchain_tag}
1310 TOOLCHAIN_TAG = ${toolchain_tag}
1311 TARGET = ${build_target}
1314 # Build Directory Macro Definition
1316 BUILD_DIR = ${platform_build_directory}
1317 FV_DIR = ${platform_build_directory}${separator}FV
1320 # Shell Command Macro
1322 ${BEGIN}${shell_command_code} = ${shell_command}
1326 MAKE_FILE = ${makefile_path}
1331 all: init build_libraries build_modules
1334 # Initialization target: print build information and create necessary directories
1337 \t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
1338 \t${BEGIN}-@${create_directory_command}
1341 # library build target
1343 libraries: init build_libraries
1346 # module build target
1348 modules: init build_libraries build_modules
1351 # Build all libraries:
1354 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
1355 ${END}\t@cd $(BUILD_DIR)
1358 # Build all modules:
1361 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
1362 ${END}\t@cd $(BUILD_DIR)
1365 # Clean intermediate files
1368 \t${BEGIN}-@${library_build_command} clean
1369 \t${END}${BEGIN}-@${module_build_command} clean
1370 \t${END}@cd $(BUILD_DIR)
1373 # Clean all generated files except to makefile
1376 ${BEGIN}\t${cleanall_command}
1380 # Clean all library files
1383 \t${BEGIN}-@${library_build_command} cleanall
1384 \t${END}@cd $(BUILD_DIR)\n
1387 ## Constructor of PlatformMakefile
1389 # @param ModuleAutoGen Object of PlatformAutoGen class
1391 def __init__(self
, PlatformAutoGen
):
1392 BuildFile
.__init
__(self
, PlatformAutoGen
)
1393 self
.ModuleBuildCommandList
= []
1394 self
.ModuleMakefileList
= []
1395 self
.IntermediateDirectoryList
= []
1396 self
.ModuleBuildDirectoryList
= []
1397 self
.LibraryBuildDirectoryList
= []
1398 self
.LibraryMakeCommandList
= []
1400 # Compose a dict object containing information used to do replacement in template
1402 def _TemplateDict(self
):
1403 Separator
= self
._SEP
_[self
._FileType
]
1405 MyAgo
= self
._AutoGenObject
1406 if "MAKE" not in MyAgo
.ToolDefinition
or "PATH" not in MyAgo
.ToolDefinition
["MAKE"]:
1407 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1408 ExtraData
="[%s]" % str(MyAgo
))
1410 self
.IntermediateDirectoryList
= ["$(BUILD_DIR)"]
1411 self
.ModuleBuildDirectoryList
= self
.GetModuleBuildDirectoryList()
1412 self
.LibraryBuildDirectoryList
= self
.GetLibraryBuildDirectoryList()
1414 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1415 LibraryMakefileList
= []
1416 LibraryMakeCommandList
= []
1417 for D
in self
.LibraryBuildDirectoryList
:
1418 D
= self
.PlaceMacro(D
, {"BUILD_DIR":MyAgo
.BuildDir
})
1419 Makefile
= os
.path
.join(D
, MakefileName
)
1420 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":Makefile
}
1421 LibraryMakefileList
.append(Makefile
)
1422 LibraryMakeCommandList
.append(Command
)
1423 self
.LibraryMakeCommandList
= LibraryMakeCommandList
1425 ModuleMakefileList
= []
1426 ModuleMakeCommandList
= []
1427 for D
in self
.ModuleBuildDirectoryList
:
1428 D
= self
.PlaceMacro(D
, {"BUILD_DIR":MyAgo
.BuildDir
})
1429 Makefile
= os
.path
.join(D
, MakefileName
)
1430 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":Makefile
}
1431 ModuleMakefileList
.append(Makefile
)
1432 ModuleMakeCommandList
.append(Command
)
1434 MakefileTemplateDict
= {
1435 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1436 "makefile_path" : os
.path
.join("$(BUILD_DIR)", MakefileName
),
1437 "make_path" : MyAgo
.ToolDefinition
["MAKE"]["PATH"],
1438 "makefile_name" : MakefileName
,
1439 "platform_name" : MyAgo
.Name
,
1440 "platform_guid" : MyAgo
.Guid
,
1441 "platform_version" : MyAgo
.Version
,
1442 "platform_file" : MyAgo
.MetaFile
,
1443 "platform_relative_directory": MyAgo
.SourceDir
,
1444 "platform_output_directory" : MyAgo
.OutputDir
,
1445 "platform_build_directory" : MyAgo
.BuildDir
,
1446 "platform_dir" : MyAgo
.Macros
["PLATFORM_DIR"],
1448 "toolchain_tag" : MyAgo
.ToolChain
,
1449 "build_target" : MyAgo
.BuildTarget
,
1450 "shell_command_code" : list(self
._SHELL
_CMD
_[self
._FileType
].keys()),
1451 "shell_command" : list(self
._SHELL
_CMD
_[self
._FileType
].values()),
1452 "build_architecture_list" : MyAgo
.Arch
,
1453 "architecture" : MyAgo
.Arch
,
1454 "separator" : Separator
,
1455 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1456 "cleanall_command" : self
.GetRemoveDirectoryCommand(self
.IntermediateDirectoryList
),
1457 "library_makefile_list" : LibraryMakefileList
,
1458 "module_makefile_list" : ModuleMakefileList
,
1459 "library_build_command" : LibraryMakeCommandList
,
1460 "module_build_command" : ModuleMakeCommandList
,
1463 return MakefileTemplateDict
1465 ## Get the root directory list for intermediate files of all modules build
1467 # @retval list The list of directory
1469 def GetModuleBuildDirectoryList(self
):
1471 for ModuleAutoGen
in self
._AutoGenObject
.ModuleAutoGenList
:
1472 if not ModuleAutoGen
.IsBinaryModule
:
1473 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, ModuleAutoGen
.BuildDir
))
1476 ## Get the root directory list for intermediate files of all libraries build
1478 # @retval list The list of directory
1480 def GetLibraryBuildDirectoryList(self
):
1482 for LibraryAutoGen
in self
._AutoGenObject
.LibraryAutoGenList
:
1483 if not LibraryAutoGen
.IsBinaryModule
:
1484 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, LibraryAutoGen
.BuildDir
))
1487 ## TopLevelMakefile class
1489 # This class encapsules makefie and its generation for entrance makefile. It
1490 # uses template to generate the content of makefile. The content of makefile
1491 # will be got from WorkspaceAutoGen object.
1493 class TopLevelMakefile(BuildFile
):
1494 ## template used to generate toplevel makefile
1495 _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}''')
1497 ## Constructor of TopLevelMakefile
1499 # @param Workspace Object of WorkspaceAutoGen class
1501 def __init__(self
, Workspace
):
1502 BuildFile
.__init
__(self
, Workspace
)
1503 self
.IntermediateDirectoryList
= []
1505 # Compose a dict object containing information used to do replacement in template
1507 def _TemplateDict(self
):
1508 Separator
= self
._SEP
_[self
._FileType
]
1510 # any platform autogen object is ok because we just need common information
1511 MyAgo
= self
._AutoGenObject
1513 if "MAKE" not in MyAgo
.ToolDefinition
or "PATH" not in MyAgo
.ToolDefinition
["MAKE"]:
1514 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1515 ExtraData
="[%s]" % str(MyAgo
))
1517 for Arch
in MyAgo
.ArchList
:
1518 self
.IntermediateDirectoryList
.append(Separator
.join(["$(BUILD_DIR)", Arch
]))
1519 self
.IntermediateDirectoryList
.append("$(FV_DIR)")
1521 # TRICK: for not generating GenFds call in makefile if no FDF file
1523 if MyAgo
.FdfFile
is not None and MyAgo
.FdfFile
!= "":
1524 FdfFileList
= [MyAgo
.FdfFile
]
1525 # macros passed to GenFds
1527 MacroDict
.update(GlobalData
.gGlobalDefines
)
1528 MacroDict
.update(GlobalData
.gCommandLineDefines
)
1529 for MacroName
in MacroDict
:
1530 if MacroDict
[MacroName
] != "":
1531 MacroList
.append('"%s=%s"' % (MacroName
, MacroDict
[MacroName
].replace('\\', '\\\\')))
1533 MacroList
.append('"%s"' % MacroName
)
1537 # pass extra common options to external program called in makefile, currently GenFds.exe
1539 LogLevel
= EdkLogger
.GetLevel()
1540 if LogLevel
== EdkLogger
.VERBOSE
:
1541 ExtraOption
+= " -v"
1542 elif LogLevel
<= EdkLogger
.DEBUG_9
:
1543 ExtraOption
+= " -d %d" % (LogLevel
- 1)
1544 elif LogLevel
== EdkLogger
.QUIET
:
1545 ExtraOption
+= " -q"
1547 if GlobalData
.gCaseInsensitive
:
1548 ExtraOption
+= " -c"
1549 if GlobalData
.gEnableGenfdsMultiThread
:
1550 ExtraOption
+= " --genfds-multi-thread"
1551 if GlobalData
.gIgnoreSource
:
1552 ExtraOption
+= " --ignore-sources"
1554 for pcd
in GlobalData
.BuildOptionPcd
:
1556 pcdname
= '.'.join(pcd
[0:3])
1558 pcdname
= '.'.join(pcd
[0:2])
1559 if pcd
[3].startswith('{'):
1560 ExtraOption
+= " --pcd " + pcdname
+ '=' + 'H' + '"' + pcd
[3] + '"'
1562 ExtraOption
+= " --pcd " + pcdname
+ '=' + pcd
[3]
1564 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1565 SubBuildCommandList
= []
1566 for A
in MyAgo
.ArchList
:
1567 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":os
.path
.join("$(BUILD_DIR)", A
, MakefileName
)}
1568 SubBuildCommandList
.append(Command
)
1570 MakefileTemplateDict
= {
1571 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1572 "makefile_path" : os
.path
.join("$(BUILD_DIR)", MakefileName
),
1573 "make_path" : MyAgo
.ToolDefinition
["MAKE"]["PATH"],
1574 "platform_name" : MyAgo
.Name
,
1575 "platform_guid" : MyAgo
.Guid
,
1576 "platform_version" : MyAgo
.Version
,
1577 "platform_build_directory" : MyAgo
.BuildDir
,
1578 "conf_directory" : GlobalData
.gConfDirectory
,
1580 "toolchain_tag" : MyAgo
.ToolChain
,
1581 "build_target" : MyAgo
.BuildTarget
,
1582 "shell_command_code" : list(self
._SHELL
_CMD
_[self
._FileType
].keys()),
1583 "shell_command" : list(self
._SHELL
_CMD
_[self
._FileType
].values()),
1584 'arch' : list(MyAgo
.ArchList
),
1585 "build_architecture_list" : ','.join(MyAgo
.ArchList
),
1586 "separator" : Separator
,
1587 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1588 "cleanall_command" : self
.GetRemoveDirectoryCommand(self
.IntermediateDirectoryList
),
1589 "sub_build_command" : SubBuildCommandList
,
1590 "fdf_file" : FdfFileList
,
1591 "active_platform" : str(MyAgo
),
1592 "fd" : MyAgo
.FdTargetList
,
1593 "fv" : MyAgo
.FvTargetList
,
1594 "cap" : MyAgo
.CapTargetList
,
1595 "extra_options" : ExtraOption
,
1596 "macro" : MacroList
,
1599 return MakefileTemplateDict
1601 ## Get the root directory list for intermediate files of all modules build
1603 # @retval list The list of directory
1605 def GetModuleBuildDirectoryList(self
):
1607 for ModuleAutoGen
in self
._AutoGenObject
.ModuleAutoGenList
:
1608 if not ModuleAutoGen
.IsBinaryModule
:
1609 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, ModuleAutoGen
.BuildDir
))
1612 ## Get the root directory list for intermediate files of all libraries build
1614 # @retval list The list of directory
1616 def GetLibraryBuildDirectoryList(self
):
1618 for LibraryAutoGen
in self
._AutoGenObject
.LibraryAutoGenList
:
1619 if not LibraryAutoGen
.IsBinaryModule
:
1620 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, LibraryAutoGen
.BuildDir
))
1623 # This acts like the main() function for the script, unless it is 'import'ed into another script.
1624 if __name__
== '__main__':