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 from __future__
import absolute_import
17 import Common
.LongFilePathOs
as os
21 import os
.path
as path
22 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
23 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
24 from Common
.BuildToolError
import *
25 from Common
.Misc
import *
26 from Common
.StringUtils
import *
27 from .BuildEngine
import *
28 import Common
.GlobalData
as GlobalData
29 from collections
import OrderedDict
30 from Common
.DataType
import TAB_COMPILER_MSFT
32 ## Regular expression for finding header file inclusions
33 gIncludePattern
= re
.compile(r
"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?
[ \t]*)([-\w
.\\/() \t]+)(?
:[ \t]*[\">]?\
)?
)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
35 ## Regular expression for matching macro used in header file inclusion
36 gMacroPattern = re.compile("([_A
-Z
][_A
-Z0
-9]*)[ \t]*\
((.+)\
)", re.UNICODE)
40 ## pattern for include style in Edk.x code
41 gProtocolDefinition = "Protocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
42 gGuidDefinition = "Guid
/%(HeaderKey)s/%(HeaderKey)s.h
"
43 gArchProtocolDefinition = "ArchProtocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
44 gPpiDefinition = "Ppi
/%(HeaderKey)s/%(HeaderKey)s.h
"
45 gIncludeMacroConversion = {
46 "EFI_PROTOCOL_DEFINITION
" : gProtocolDefinition,
47 "EFI_GUID_DEFINITION
" : gGuidDefinition,
48 "EFI_ARCH_PROTOCOL_DEFINITION
" : gArchProtocolDefinition,
49 "EFI_PROTOCOL_PRODUCER
" : gProtocolDefinition,
50 "EFI_PROTOCOL_CONSUMER
" : gProtocolDefinition,
51 "EFI_PROTOCOL_DEPENDENCY
" : gProtocolDefinition,
52 "EFI_ARCH_PROTOCOL_PRODUCER
" : gArchProtocolDefinition,
53 "EFI_ARCH_PROTOCOL_CONSUMER
" : gArchProtocolDefinition,
54 "EFI_ARCH_PROTOCOL_DEPENDENCY
" : gArchProtocolDefinition,
55 "EFI_PPI_DEFINITION
" : gPpiDefinition,
56 "EFI_PPI_PRODUCER
" : gPpiDefinition,
57 "EFI_PPI_CONSUMER
" : gPpiDefinition,
58 "EFI_PPI_DEPENDENCY
" : gPpiDefinition,
61 ## default makefile type
63 if sys.platform == "win32
":
71 # This base class encapsules build file and its generation. It uses template to generate
72 # the content of build file. The content of build file will be got from AutoGen objects.
74 class BuildFile(object):
75 ## template used to generate the build file (i.e. makefile if using make)
76 _TEMPLATE_ = TemplateString('')
78 _DEFAULT_FILE_NAME_ = "Makefile
"
80 ## default file name for each type of build file
83 "gmake
" : "GNUmakefile
"
86 ## Fixed header string for makefile
87 _MAKEFILE_HEADER = '''#
89 # This file is auto-generated by build utility
97 # Auto-generated makefile for building modules, libraries or platform
101 ## Header string for each type of build file
103 "nmake
" : _MAKEFILE_HEADER % _FILE_NAME_["nmake
"],
104 "gmake
" : _MAKEFILE_HEADER % _FILE_NAME_["gmake
"]
107 ## shell commands which can be used in build file in the form of macro
108 # $(CP) copy file command
109 # $(MV) move file command
110 # $(RM) remove file command
111 # $(MD) create dir command
112 # $(RD) remove dir command
120 "RD
" : "rmdir
/s
/q
",
132 ## directory separator
138 ## directory creation template
140 "nmake
" : 'if not exist %(dir)s $(MD) %(dir)s',
141 "gmake
" : "$
(MD
) %(dir)s"
144 ## directory removal template
146 "nmake
" : 'if exist %(dir)s $(RD) %(dir)s',
147 "gmake
" : "$
(RD
) %(dir)s"
151 "nmake
" : 'if exist %(Src)s $(CP) %(Src)s %(Dst)s',
152 "gmake
" : "test
-f
%(Src)s && $
(CP
) %(Src)s %(Dst)s"
156 "nmake
" : 'if exist %(dir)s cd %(dir)s',
157 "gmake
" : "test
-e
%(dir)s && cd
%(dir)s"
161 "nmake
" : 'if exist %(file)s "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s',
162 "gmake
" : 'test -e %(file)s && "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s'
166 "nmake
" : '!INCLUDE',
170 _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I
", "GCC
" : "-I
", "INTEL
" : "-I
", "RVCT
" : "-I
"}
172 ## Constructor of BuildFile
174 # @param AutoGenObject Object of AutoGen class
176 def __init__(self, AutoGenObject):
177 self._AutoGenObject = AutoGenObject
178 self._FileType = gMakeType
182 # @param FileType Type of build file. Only nmake and gmake are supported now.
184 # @retval TRUE The build file is created or re-created successfully
185 # @retval FALSE The build file exists and is the same as the one to be generated
187 def Generate(self, FileType=gMakeType):
188 if FileType not in self._FILE_NAME_:
189 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid build
type [%s]" % FileType,
190 ExtraData="[%s]" % str(self._AutoGenObject))
191 self._FileType = FileType
192 FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
193 FileName = self._FILE_NAME_[FileType]
194 return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
196 ## Return a list of directory creation command string
198 # @param DirList The list of directory to be created
200 # @retval list The directory creation command list
202 def GetCreateDirectoryCommand(self, DirList):
203 return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
205 ## Return a list of directory removal command string
207 # @param DirList The list of directory to be removed
209 # @retval list The directory removal command list
211 def GetRemoveDirectoryCommand(self, DirList):
212 return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
214 def PlaceMacro(self, Path, MacroDefinitions={}):
215 if Path.startswith("$
("):
218 PathLength = len(Path)
219 for MacroName in MacroDefinitions:
220 MacroValue = MacroDefinitions[MacroName]
221 MacroValueLength = len(MacroValue)
222 if MacroValueLength == 0:
224 if MacroValueLength <= PathLength and Path.startswith(MacroValue):
225 Path = "$
(%s)%s" % (MacroName, Path[MacroValueLength:])
229 ## ModuleMakefile class
231 # This class encapsules makefie and its generation for module. It uses template to generate
232 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
234 class ModuleMakefile(BuildFile):
235 ## template used to generate the makefile for module
236 _TEMPLATE_ = TemplateString('''\
240 # Platform Macro Definition
242 PLATFORM_NAME = ${platform_name}
243 PLATFORM_GUID = ${platform_guid}
244 PLATFORM_VERSION = ${platform_version}
245 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
246 PLATFORM_DIR = ${platform_dir}
247 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
250 # Module Macro Definition
252 MODULE_NAME = ${module_name}
253 MODULE_GUID = ${module_guid}
254 MODULE_NAME_GUID = ${module_name_guid}
255 MODULE_VERSION = ${module_version}
256 MODULE_TYPE = ${module_type}
257 MODULE_FILE = ${module_file}
258 MODULE_FILE_BASE_NAME = ${module_file_base_name}
259 BASE_NAME = $(MODULE_NAME)
260 MODULE_RELATIVE_DIR = ${module_relative_directory}
261 PACKAGE_RELATIVE_DIR = ${package_relative_directory}
262 MODULE_DIR = ${module_dir}
263 FFS_OUTPUT_DIR = ${ffs_output_directory}
265 MODULE_ENTRY_POINT = ${module_entry_point}
266 ARCH_ENTRY_POINT = ${arch_entry_point}
267 IMAGE_ENTRY_POINT = ${image_entry_point}
269 ${BEGIN}${module_extra_defines}
272 # Build Configuration Macro Definition
274 ARCH = ${architecture}
275 TOOLCHAIN = ${toolchain_tag}
276 TOOLCHAIN_TAG = ${toolchain_tag}
277 TARGET = ${build_target}
280 # Build Directory Macro Definition
282 # PLATFORM_BUILD_DIR = ${platform_build_directory}
283 BUILD_DIR = ${platform_build_directory}
284 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
286 MODULE_BUILD_DIR = ${module_build_directory}
287 OUTPUT_DIR = ${module_output_directory}
288 DEBUG_DIR = ${module_debug_directory}
289 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
290 DEST_DIR_DEBUG = $(DEBUG_DIR)
293 # Shell Command Macro
295 ${BEGIN}${shell_command_code} = ${shell_command}
299 # Tools definitions specific to this module
301 ${BEGIN}${module_tool_definitions}
303 MAKE_FILE = ${makefile_path}
308 ${BEGIN}${file_macro}
311 COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
315 # Overridable Target Macro Definitions
317 FORCE_REBUILD = force_build
320 BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
321 CODA_TARGET = ${BEGIN}${remaining_build_target} \\
325 # Default target, which will build dependent libraries in addition to source files
332 # Target used when called from platform makefile, which will bypass the build of dependent libraries
335 pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
341 mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
344 # Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
347 tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
350 # Phony target which is used to force executing commands for a target
356 # Target to update the FD
362 # Initialization target: print build information and create necessary directories
367 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
370 ${BEGIN}\t-@${create_directory_command}\n${END}
373 \t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
379 \t${BEGIN}@"$
(MAKE
)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
380 \t${END}@cd $(MODULE_BUILD_DIR)
383 # Build Flash Device Image
386 \t@"$
(MAKE
)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
387 \t@cd $(MODULE_BUILD_DIR)
390 # Individual Object Build Targets
392 ${BEGIN}${file_build_target}
396 # clean all intermediate files
399 \t${BEGIN}${clean_command}
400 \t${END}\t$(RM) AutoGenTimeStamp
403 # clean all generated files
406 ${BEGIN}\t${cleanall_command}
407 ${END}\t$(RM) *.pdb *.idb > NUL 2>&1
408 \t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
409 \t$(RM) AutoGenTimeStamp
412 # clean all dependent libraries built
415 \t${BEGIN}-@${library_build_command} cleanall
416 \t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
418 _FILE_MACRO_TEMPLATE = TemplateString("${macro_name}
= ${BEGIN}
\\\n ${source_file}${END}
\n")
419 _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target}
: ${deps}
\n${END}
\t${cmd}
\n")
421 ## Constructor of ModuleMakefile
423 # @param ModuleAutoGen Object of ModuleAutoGen class
425 def __init__(self, ModuleAutoGen):
426 BuildFile.__init__(self, ModuleAutoGen)
427 self.PlatformInfo = self._AutoGenObject.PlatformInfo
429 self.ResultFileList = []
430 self.IntermediateDirectoryList = ["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]
432 self.FileBuildTargetList = [] # [(src, target string)]
433 self.BuildTargetList = [] # [target string]
434 self.PendingBuildTargetList = [] # [FileBuildRule objects]
435 self.CommonFileDependency = []
436 self.FileListMacros = {}
437 self.ListFileMacros = {}
440 self.LibraryBuildCommandList = []
441 self.LibraryFileList = []
442 self.LibraryMakefileList = []
443 self.LibraryBuildDirectoryList = []
444 self.SystemLibraryList = []
445 self.Macros = OrderedDict()
446 self.Macros["OUTPUT_DIR
" ] = self._AutoGenObject.Macros["OUTPUT_DIR
"]
447 self.Macros["DEBUG_DIR
" ] = self._AutoGenObject.Macros["DEBUG_DIR
"]
448 self.Macros["MODULE_BUILD_DIR
"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR
"]
449 self.Macros["BIN_DIR
" ] = self._AutoGenObject.Macros["BIN_DIR
"]
450 self.Macros["BUILD_DIR
" ] = self._AutoGenObject.Macros["BUILD_DIR
"]
451 self.Macros["WORKSPACE
" ] = self._AutoGenObject.Macros["WORKSPACE
"]
452 self.Macros["FFS_OUTPUT_DIR
" ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR
"]
453 self.GenFfsList = ModuleAutoGen.GenFfsList
454 self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']
455 self.FfsOutputFileList = []
457 # Compose a dict object containing information used to do replacement in template
459 def _TemplateDict(self):
460 if self._FileType not in self._SEP_:
461 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid Makefile
type [%s]" % self._FileType,
462 ExtraData="[%s]" % str(self._AutoGenObject))
463 MyAgo = self._AutoGenObject
464 Separator = self._SEP_[self._FileType]
466 # break build if no source files and binary files are found
467 if len(MyAgo.SourceFileList) == 0 and len(MyAgo.BinaryFileList) == 0:
468 EdkLogger.error("build
", AUTOGEN_ERROR, "No files to be built
in module
[%s, %s, %s]"
469 % (MyAgo.BuildTarget, MyAgo.ToolChain, MyAgo.Arch),
470 ExtraData="[%s]" % str(MyAgo))
472 # convert dependent libraries to build command
473 self.ProcessDependentLibrary()
474 if len(MyAgo.Module.ModuleEntryPointList) > 0:
475 ModuleEntryPoint = MyAgo.Module.ModuleEntryPointList[0]
477 ModuleEntryPoint = "_ModuleEntryPoint
"
479 # Intel EBC compiler enforces EfiMain
480 if MyAgo.AutoGenVersion < 0x00010005 and MyAgo.Arch == "EBC
":
481 ArchEntryPoint = "EfiMain
"
483 ArchEntryPoint = ModuleEntryPoint
485 if MyAgo.Arch == "EBC
":
486 # EBC compiler always use "EfiStart
" as entry point. Only applies to EdkII modules
487 ImageEntryPoint = "EfiStart
"
488 elif MyAgo.AutoGenVersion < 0x00010005:
489 # Edk modules use entry point specified in INF file
490 ImageEntryPoint = ModuleEntryPoint
492 # EdkII modules always use "_ModuleEntryPoint
" as entry point
493 ImageEntryPoint = "_ModuleEntryPoint
"
495 for k, v in MyAgo.Module.Defines.iteritems():
496 if k not in MyAgo.Macros:
499 if 'MODULE_ENTRY_POINT' not in MyAgo.Macros:
500 MyAgo.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint
501 if 'ARCH_ENTRY_POINT' not in MyAgo.Macros:
502 MyAgo.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint
503 if 'IMAGE_ENTRY_POINT' not in MyAgo.Macros:
504 MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint
506 PCI_COMPRESS_Flag = False
507 for k, v in MyAgo.Module.Defines.iteritems():
508 if 'PCI_COMPRESS' == k and 'TRUE' == v:
509 PCI_COMPRESS_Flag = True
513 IncPrefix = self._INC_FLAG_[MyAgo.ToolChainFamily]
514 for Tool in MyAgo.BuildOption:
515 for Attr in MyAgo.BuildOption[Tool]:
516 Value = MyAgo.BuildOption[Tool][Attr]
520 ToolsDef.append("%s = %s" % (Tool, Value))
522 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
525 # Remove duplicated include path, if any
527 Value = RemoveDupOption(Value, IncPrefix, MyAgo.IncludePathList)
528 if Tool == "OPTROM
" and PCI_COMPRESS_Flag:
529 ValueList = Value.split()
531 for i, v in enumerate(ValueList):
534 Value = ' '.join(ValueList)
536 ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
539 # generate the Response file and Response flag
540 RespDict = self.CommandExceedLimit()
541 RespFileList = os.path.join(MyAgo.OutputDir, 'respfilelist.txt')
543 RespFileListContent = ''
544 for Resp in RespDict:
545 RespFile = os.path.join(MyAgo.OutputDir, str(Resp).lower() + '.txt')
546 StrList = RespDict[Resp].split(' ')
551 UnexpandMacro.append(Str)
554 UnexpandMacroStr = ' '.join(UnexpandMacro)
555 NewRespStr = ' '.join(NewStr)
556 SaveFileOnChange(RespFile, NewRespStr, False)
557 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))
558 RespFileListContent += '@' + RespFile + os.linesep
559 RespFileListContent += NewRespStr + os.linesep
560 SaveFileOnChange(RespFileList, RespFileListContent, False)
562 if os.path.exists(RespFileList):
563 os.remove(RespFileList)
565 # convert source files and binary files to build targets
566 self.ResultFileList = [str(T.Target) for T in MyAgo.CodaTargetList]
567 if len(self.ResultFileList) == 0 and len(MyAgo.SourceFileList) != 0:
568 EdkLogger.error("build
", AUTOGEN_ERROR, "Nothing to build
",
569 ExtraData="[%s]" % str(MyAgo))
571 self.ProcessBuildTargetList()
572 self.ParserGenerateFfsCmd()
574 # Generate macros used to represent input files
575 FileMacroList = [] # macro name = file list
576 for FileListMacro in self.FileListMacros:
577 FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
579 "macro_name
" : FileListMacro,
580 "source_file
" : self.FileListMacros[FileListMacro]
583 FileMacroList.append(FileMacro)
585 # INC_LIST is special
588 for P in MyAgo.IncludePathList:
589 IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))
590 if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
591 self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)
592 FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
594 "macro_name
" : "INC
",
595 "source_file
" : IncludePathList
598 FileMacroList.append(FileMacro)
600 # Generate macros used to represent files containing list of input files
601 for ListFileMacro in self.ListFileMacros:
602 ListFileName = os.path.join(MyAgo.OutputDir, "%s.lst
" % ListFileMacro.lower()[:len(ListFileMacro) - 5])
603 FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
606 "\n".join(self.ListFileMacros[ListFileMacro]),
610 # Edk modules need <BaseName>StrDefs.h for string ID
611 #if MyAgo.AutoGenVersion < 0x00010005 and len(MyAgo.UnicodeFileList) > 0:
612 # BcTargetList = ['strdefs']
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.iteritems()],
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
" : self._SHELL_CMD_[self._FileType].keys(),
673 "shell_command
" : 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]
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 separater, 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 dependecies 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
, 'r')
1036 except BaseException
as X
:
1037 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=F
.Path
+ "\n\t" + str(X
))
1039 FileContent
= Fd
.read()
1041 if len(FileContent
) == 0:
1044 if FileContent
[0] == 0xff or FileContent
[0] == 0xfe:
1045 FileContent
= unicode(FileContent
, "utf-16")
1046 IncludedFileList
= gIncludePattern
.findall(FileContent
)
1048 for Inc
in IncludedFileList
:
1050 # if there's macro used to reference header file, expand it
1051 HeaderList
= gMacroPattern
.findall(Inc
)
1052 if len(HeaderList
) == 1 and len(HeaderList
[0]) == 2:
1053 HeaderType
= HeaderList
[0][0]
1054 HeaderKey
= HeaderList
[0][1]
1055 if HeaderType
in gIncludeMacroConversion
:
1056 Inc
= gIncludeMacroConversion
[HeaderType
] % {"HeaderKey" : HeaderKey
}
1058 # not known macro used in #include, always build the file by
1059 # returning a empty dependency
1060 self
.FileCache
[File
] = []
1062 Inc
= os
.path
.normpath(Inc
)
1063 CurrentFileDependencyList
.append(Inc
)
1064 DepDb
[F
] = CurrentFileDependencyList
1066 CurrentFilePath
= F
.Dir
1067 PathList
= [CurrentFilePath
] + SearchPathList
1068 for Inc
in CurrentFileDependencyList
:
1069 for SearchPath
in PathList
:
1070 FilePath
= os
.path
.join(SearchPath
, Inc
)
1071 if FilePath
in gIsFileMap
:
1072 if not gIsFileMap
[FilePath
]:
1074 # If isfile is called too many times, the performance is slow down.
1075 elif not os
.path
.isfile(FilePath
):
1076 gIsFileMap
[FilePath
] = False
1079 gIsFileMap
[FilePath
] = True
1080 FilePath
= PathClass(FilePath
)
1081 FullPathDependList
.append(FilePath
)
1082 if FilePath
not in DependencySet
:
1083 FileStack
.append(FilePath
)
1086 EdkLogger
.debug(EdkLogger
.DEBUG_9
, "%s included by %s was not found "\
1087 "in any given path:\n\t%s" % (Inc
, F
, "\n\t".join(SearchPathList
)))
1089 self
.FileCache
[F
] = FullPathDependList
1090 DependencySet
.update(FullPathDependList
)
1092 DependencySet
.update(ForceList
)
1093 if File
in DependencySet
:
1094 DependencySet
.remove(File
)
1095 DependencyList
= list(DependencySet
) # remove duplicate ones
1097 return DependencyList
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
1208 def _TemplateDict(self
):
1209 Separator
= self
._SEP
_[self
._FileType
]
1210 MyAgo
= self
._AutoGenObject
1211 if self
._FileType
not in MyAgo
.CustomMakefile
:
1212 EdkLogger
.error('build', OPTION_NOT_SUPPORTED
, "No custom makefile for %s" % self
._FileType
,
1213 ExtraData
="[%s]" % str(MyAgo
))
1214 MakefilePath
= mws
.join(
1216 MyAgo
.CustomMakefile
[self
._FileType
]
1219 CustomMakefile
= open(MakefilePath
, 'r').read()
1221 EdkLogger
.error('build', FILE_OPEN_FAILURE
, File
=str(MyAgo
),
1222 ExtraData
=MyAgo
.CustomMakefile
[self
._FileType
])
1226 for Tool
in MyAgo
.BuildOption
:
1227 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
1230 for Attr
in MyAgo
.BuildOption
[Tool
]:
1231 if Attr
== "FAMILY":
1233 elif Attr
== "PATH":
1234 ToolsDef
.append("%s = %s" % (Tool
, MyAgo
.BuildOption
[Tool
][Attr
]))
1236 ToolsDef
.append("%s_%s = %s" % (Tool
, Attr
, MyAgo
.BuildOption
[Tool
][Attr
]))
1239 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1240 MakefileTemplateDict
= {
1241 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1242 "makefile_path" : os
.path
.join("$(MODULE_BUILD_DIR)", MakefileName
),
1243 "platform_name" : self
.PlatformInfo
.Name
,
1244 "platform_guid" : self
.PlatformInfo
.Guid
,
1245 "platform_version" : self
.PlatformInfo
.Version
,
1246 "platform_relative_directory": self
.PlatformInfo
.SourceDir
,
1247 "platform_output_directory" : self
.PlatformInfo
.OutputDir
,
1248 "platform_dir" : MyAgo
.Macros
["PLATFORM_DIR"],
1250 "module_name" : MyAgo
.Name
,
1251 "module_guid" : MyAgo
.Guid
,
1252 "module_name_guid" : MyAgo
.UniqueBaseName
,
1253 "module_version" : MyAgo
.Version
,
1254 "module_type" : MyAgo
.ModuleType
,
1255 "module_file" : MyAgo
.MetaFile
,
1256 "module_file_base_name" : MyAgo
.MetaFile
.BaseName
,
1257 "module_relative_directory" : MyAgo
.SourceDir
,
1258 "module_dir" : mws
.join (MyAgo
.WorkspaceDir
, MyAgo
.SourceDir
),
1260 "architecture" : MyAgo
.Arch
,
1261 "toolchain_tag" : MyAgo
.ToolChain
,
1262 "build_target" : MyAgo
.BuildTarget
,
1264 "platform_build_directory" : self
.PlatformInfo
.BuildDir
,
1265 "module_build_directory" : MyAgo
.BuildDir
,
1266 "module_output_directory" : MyAgo
.OutputDir
,
1267 "module_debug_directory" : MyAgo
.DebugDir
,
1269 "separator" : Separator
,
1270 "module_tool_definitions" : ToolsDef
,
1272 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1273 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1275 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1276 "custom_makefile_content" : CustomMakefile
1279 return MakefileTemplateDict
1281 ## PlatformMakefile class
1283 # This class encapsules makefie and its generation for platform. It uses
1284 # template to generate the content of makefile. The content of makefile will be
1285 # got from PlatformAutoGen object.
1287 class PlatformMakefile(BuildFile
):
1288 ## template used to generate the makefile for platform
1289 _TEMPLATE_
= TemplateString('''\
1293 # Platform Macro Definition
1295 PLATFORM_NAME = ${platform_name}
1296 PLATFORM_GUID = ${platform_guid}
1297 PLATFORM_VERSION = ${platform_version}
1298 PLATFORM_FILE = ${platform_file}
1299 PLATFORM_DIR = ${platform_dir}
1300 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1303 # Build Configuration Macro Definition
1305 TOOLCHAIN = ${toolchain_tag}
1306 TOOLCHAIN_TAG = ${toolchain_tag}
1307 TARGET = ${build_target}
1310 # Build Directory Macro Definition
1312 BUILD_DIR = ${platform_build_directory}
1313 FV_DIR = ${platform_build_directory}${separator}FV
1316 # Shell Command Macro
1318 ${BEGIN}${shell_command_code} = ${shell_command}
1322 MAKE_FILE = ${makefile_path}
1327 all: init build_libraries build_modules
1330 # Initialization target: print build information and create necessary directories
1333 \t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
1334 \t${BEGIN}-@${create_directory_command}
1337 # library build target
1339 libraries: init build_libraries
1342 # module build target
1344 modules: init build_libraries build_modules
1347 # Build all libraries:
1350 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
1351 ${END}\t@cd $(BUILD_DIR)
1354 # Build all modules:
1357 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
1358 ${END}\t@cd $(BUILD_DIR)
1361 # Clean intermediate files
1364 \t${BEGIN}-@${library_build_command} clean
1365 \t${END}${BEGIN}-@${module_build_command} clean
1366 \t${END}@cd $(BUILD_DIR)
1369 # Clean all generated files except to makefile
1372 ${BEGIN}\t${cleanall_command}
1376 # Clean all library files
1379 \t${BEGIN}-@${library_build_command} cleanall
1380 \t${END}@cd $(BUILD_DIR)\n
1383 ## Constructor of PlatformMakefile
1385 # @param ModuleAutoGen Object of PlatformAutoGen class
1387 def __init__(self
, PlatformAutoGen
):
1388 BuildFile
.__init
__(self
, PlatformAutoGen
)
1389 self
.ModuleBuildCommandList
= []
1390 self
.ModuleMakefileList
= []
1391 self
.IntermediateDirectoryList
= []
1392 self
.ModuleBuildDirectoryList
= []
1393 self
.LibraryBuildDirectoryList
= []
1394 self
.LibraryMakeCommandList
= []
1396 # Compose a dict object containing information used to do replacement in template
1398 def _TemplateDict(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 ## TopLevelMakefile class
1485 # This class encapsules makefie and its generation for entrance makefile. It
1486 # uses template to generate the content of makefile. The content of makefile
1487 # will be got from WorkspaceAutoGen object.
1489 class TopLevelMakefile(BuildFile
):
1490 ## template used to generate toplevel makefile
1491 _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}''')
1493 ## Constructor of TopLevelMakefile
1495 # @param Workspace Object of WorkspaceAutoGen class
1497 def __init__(self
, Workspace
):
1498 BuildFile
.__init
__(self
, Workspace
)
1499 self
.IntermediateDirectoryList
= []
1501 # Compose a dict object containing information used to do replacement in template
1503 def _TemplateDict(self
):
1504 Separator
= self
._SEP
_[self
._FileType
]
1506 # any platform autogen object is ok because we just need common information
1507 MyAgo
= self
._AutoGenObject
1509 if "MAKE" not in MyAgo
.ToolDefinition
or "PATH" not in MyAgo
.ToolDefinition
["MAKE"]:
1510 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1511 ExtraData
="[%s]" % str(MyAgo
))
1513 for Arch
in MyAgo
.ArchList
:
1514 self
.IntermediateDirectoryList
.append(Separator
.join(["$(BUILD_DIR)", Arch
]))
1515 self
.IntermediateDirectoryList
.append("$(FV_DIR)")
1517 # TRICK: for not generating GenFds call in makefile if no FDF file
1519 if MyAgo
.FdfFile
is not None and MyAgo
.FdfFile
!= "":
1520 FdfFileList
= [MyAgo
.FdfFile
]
1521 # macros passed to GenFds
1522 MacroList
.append('"%s=%s"' % ("EFI_SOURCE", GlobalData
.gEfiSource
.replace('\\', '\\\\')))
1523 MacroList
.append('"%s=%s"' % ("EDK_SOURCE", GlobalData
.gEdkSource
.replace('\\', '\\\\')))
1525 MacroDict
.update(GlobalData
.gGlobalDefines
)
1526 MacroDict
.update(GlobalData
.gCommandLineDefines
)
1527 MacroDict
.pop("EFI_SOURCE", "dummy")
1528 MacroDict
.pop("EDK_SOURCE", "dummy")
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" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1583 "shell_command" : 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__':