2 # Create makefile for MS nmake and GNU make
4 # Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>
5 # This program and the accompanying materials
6 # are licensed and made available under the terms and conditions of the BSD License
7 # which accompanies this distribution. The full text of the license may be found at
8 # http://opensource.org/licenses/bsd-license.php
10 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
16 import Common
.LongFilePathOs
as os
20 import os
.path
as path
21 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
22 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
23 from Common
.BuildToolError
import *
24 from Common
.Misc
import *
25 from Common
.StringUtils
import *
26 from BuildEngine
import *
27 import Common
.GlobalData
as GlobalData
28 from collections
import OrderedDict
30 ## Regular expression for finding header file inclusions
31 gIncludePattern
= re
.compile(r
"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?
[ \t]*)([-\w
.\\/() \t]+)(?
:[ \t]*[\">]?\
)?
)", re.MULTILINE | re.UNICODE | re.IGNORECASE)
33 ## Regular expression for matching macro used in header file inclusion
34 gMacroPattern = re.compile("([_A
-Z
][_A
-Z0
-9]*)[ \t]*\
((.+)\
)", re.UNICODE)
38 ## pattern for include style in Edk.x code
39 gProtocolDefinition = "Protocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
40 gGuidDefinition = "Guid
/%(HeaderKey)s/%(HeaderKey)s.h
"
41 gArchProtocolDefinition = "ArchProtocol
/%(HeaderKey)s/%(HeaderKey)s.h
"
42 gPpiDefinition = "Ppi
/%(HeaderKey)s/%(HeaderKey)s.h
"
43 gIncludeMacroConversion = {
44 "EFI_PROTOCOL_DEFINITION
" : gProtocolDefinition,
45 "EFI_GUID_DEFINITION
" : gGuidDefinition,
46 "EFI_ARCH_PROTOCOL_DEFINITION
" : gArchProtocolDefinition,
47 "EFI_PROTOCOL_PRODUCER
" : gProtocolDefinition,
48 "EFI_PROTOCOL_CONSUMER
" : gProtocolDefinition,
49 "EFI_PROTOCOL_DEPENDENCY
" : gProtocolDefinition,
50 "EFI_ARCH_PROTOCOL_PRODUCER
" : gArchProtocolDefinition,
51 "EFI_ARCH_PROTOCOL_CONSUMER
" : gArchProtocolDefinition,
52 "EFI_ARCH_PROTOCOL_DEPENDENCY
" : gArchProtocolDefinition,
53 "EFI_PPI_DEFINITION
" : gPpiDefinition,
54 "EFI_PPI_PRODUCER
" : gPpiDefinition,
55 "EFI_PPI_CONSUMER
" : gPpiDefinition,
56 "EFI_PPI_DEPENDENCY
" : gPpiDefinition,
59 ## default makefile type
61 if sys.platform == "win32
":
69 # This base class encapsules build file and its generation. It uses template to generate
70 # the content of build file. The content of build file will be got from AutoGen objects.
72 class BuildFile(object):
73 ## template used to generate the build file (i.e. makefile if using make)
74 _TEMPLATE_ = TemplateString('')
76 _DEFAULT_FILE_NAME_ = "Makefile
"
78 ## default file name for each type of build file
81 "gmake
" : "GNUmakefile
"
84 ## Fixed header string for makefile
85 _MAKEFILE_HEADER = '''#
87 # This file is auto-generated by build utility
95 # Auto-generated makefile for building modules, libraries or platform
99 ## Header string for each type of build file
101 "nmake
" : _MAKEFILE_HEADER % _FILE_NAME_["nmake
"],
102 "gmake
" : _MAKEFILE_HEADER % _FILE_NAME_["gmake
"]
105 ## shell commands which can be used in build file in the form of macro
106 # $(CP) copy file command
107 # $(MV) move file command
108 # $(RM) remove file command
109 # $(MD) create dir command
110 # $(RD) remove dir command
118 "RD
" : "rmdir
/s
/q
",
130 ## directory separator
136 ## directory creation template
138 "nmake
" : 'if not exist %(dir)s $(MD) %(dir)s',
139 "gmake
" : "$
(MD
) %(dir)s"
142 ## directory removal template
144 "nmake
" : 'if exist %(dir)s $(RD) %(dir)s',
145 "gmake
" : "$
(RD
) %(dir)s"
149 "nmake
" : 'if exist %(Src)s $(CP) %(Src)s %(Dst)s',
150 "gmake
" : "test
-f
%(Src)s && $
(CP
) %(Src)s %(Dst)s"
154 "nmake
" : 'if exist %(dir)s cd %(dir)s',
155 "gmake
" : "test
-e
%(dir)s && cd
%(dir)s"
159 "nmake
" : 'if exist %(file)s "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s',
160 "gmake
" : 'test -e %(file)s && "$
(MAKE
)" $(MAKE_FLAGS) -f %(file)s'
164 "nmake
" : '!INCLUDE',
168 _INC_FLAG_ = {"MSFT
" : "/I
", "GCC
" : "-I
", "INTEL
" : "-I
", "RVCT
" : "-I
"}
170 ## Constructor of BuildFile
172 # @param AutoGenObject Object of AutoGen class
174 def __init__(self, AutoGenObject):
175 self._AutoGenObject = AutoGenObject
176 self._FileType = gMakeType
180 # @param FileType Type of build file. Only nmake and gmake are supported now.
182 # @retval TRUE The build file is created or re-created successfully
183 # @retval FALSE The build file exists and is the same as the one to be generated
185 def Generate(self, FileType=gMakeType):
186 if FileType not in self._FILE_NAME_:
187 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid build
type [%s]" % FileType,
188 ExtraData="[%s]" % str(self._AutoGenObject))
189 self._FileType = FileType
190 FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
191 FileName = self._FILE_NAME_[FileType]
192 return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
194 ## Return a list of directory creation command string
196 # @param DirList The list of directory to be created
198 # @retval list The directory creation command list
200 def GetCreateDirectoryCommand(self, DirList):
201 return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
203 ## Return a list of directory removal command string
205 # @param DirList The list of directory to be removed
207 # @retval list The directory removal command list
209 def GetRemoveDirectoryCommand(self, DirList):
210 return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
212 def PlaceMacro(self, Path, MacroDefinitions={}):
213 if Path.startswith("$
("):
216 PathLength = len(Path)
217 for MacroName in MacroDefinitions:
218 MacroValue = MacroDefinitions[MacroName]
219 MacroValueLength = len(MacroValue)
220 if MacroValueLength == 0:
222 if MacroValueLength <= PathLength and Path.startswith(MacroValue):
223 Path = "$
(%s)%s" % (MacroName, Path[MacroValueLength:])
227 ## ModuleMakefile class
229 # This class encapsules makefie and its generation for module. It uses template to generate
230 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
232 class ModuleMakefile(BuildFile):
233 ## template used to generate the makefile for module
234 _TEMPLATE_ = TemplateString('''\
238 # Platform Macro Definition
240 PLATFORM_NAME = ${platform_name}
241 PLATFORM_GUID = ${platform_guid}
242 PLATFORM_VERSION = ${platform_version}
243 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
244 PLATFORM_DIR = ${platform_dir}
245 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
248 # Module Macro Definition
250 MODULE_NAME = ${module_name}
251 MODULE_GUID = ${module_guid}
252 MODULE_NAME_GUID = ${module_name_guid}
253 MODULE_VERSION = ${module_version}
254 MODULE_TYPE = ${module_type}
255 MODULE_FILE = ${module_file}
256 MODULE_FILE_BASE_NAME = ${module_file_base_name}
257 BASE_NAME = $(MODULE_NAME)
258 MODULE_RELATIVE_DIR = ${module_relative_directory}
259 PACKAGE_RELATIVE_DIR = ${package_relative_directory}
260 MODULE_DIR = ${module_dir}
261 FFS_OUTPUT_DIR = ${ffs_output_directory}
263 MODULE_ENTRY_POINT = ${module_entry_point}
264 ARCH_ENTRY_POINT = ${arch_entry_point}
265 IMAGE_ENTRY_POINT = ${image_entry_point}
267 ${BEGIN}${module_extra_defines}
270 # Build Configuration Macro Definition
272 ARCH = ${architecture}
273 TOOLCHAIN = ${toolchain_tag}
274 TOOLCHAIN_TAG = ${toolchain_tag}
275 TARGET = ${build_target}
278 # Build Directory Macro Definition
280 # PLATFORM_BUILD_DIR = ${platform_build_directory}
281 BUILD_DIR = ${platform_build_directory}
282 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
284 MODULE_BUILD_DIR = ${module_build_directory}
285 OUTPUT_DIR = ${module_output_directory}
286 DEBUG_DIR = ${module_debug_directory}
287 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
288 DEST_DIR_DEBUG = $(DEBUG_DIR)
291 # Shell Command Macro
293 ${BEGIN}${shell_command_code} = ${shell_command}
297 # Tools definitions specific to this module
299 ${BEGIN}${module_tool_definitions}
301 MAKE_FILE = ${makefile_path}
306 ${BEGIN}${file_macro}
309 COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
313 # Overridable Target Macro Definitions
315 FORCE_REBUILD = force_build
318 BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
319 CODA_TARGET = ${BEGIN}${remaining_build_target} \\
323 # Default target, which will build dependent libraries in addition to source files
330 # Target used when called from platform makefile, which will bypass the build of dependent libraries
333 pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
339 mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
342 # Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
345 tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
348 # Phony target which is used to force executing commands for a target
354 # Target to update the FD
360 # Initialization target: print build information and create necessary directories
365 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
368 ${BEGIN}\t-@${create_directory_command}\n${END}
371 \t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
377 \t${BEGIN}@"$
(MAKE
)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
378 \t${END}@cd $(MODULE_BUILD_DIR)
381 # Build Flash Device Image
384 \t@"$
(MAKE
)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
385 \t@cd $(MODULE_BUILD_DIR)
388 # Individual Object Build Targets
390 ${BEGIN}${file_build_target}
394 # clean all intermediate files
397 \t${BEGIN}${clean_command}
398 \t${END}\t$(RM) AutoGenTimeStamp
401 # clean all generated files
404 ${BEGIN}\t${cleanall_command}
405 ${END}\t$(RM) *.pdb *.idb > NUL 2>&1
406 \t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
407 \t$(RM) AutoGenTimeStamp
410 # clean all dependent libraries built
413 \t${BEGIN}-@${library_build_command} cleanall
414 \t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
416 _FILE_MACRO_TEMPLATE = TemplateString("${macro_name}
= ${BEGIN}
\\\n ${source_file}${END}
\n")
417 _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target}
: ${deps}
\n${END}
\t${cmd}
\n")
419 ## Constructor of ModuleMakefile
421 # @param ModuleAutoGen Object of ModuleAutoGen class
423 def __init__(self, ModuleAutoGen):
424 BuildFile.__init__(self, ModuleAutoGen)
425 self.PlatformInfo = self._AutoGenObject.PlatformInfo
427 self.ResultFileList = []
428 self.IntermediateDirectoryList = ["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]
430 self.FileBuildTargetList = [] # [(src, target string)]
431 self.BuildTargetList = [] # [target string]
432 self.PendingBuildTargetList = [] # [FileBuildRule objects]
433 self.CommonFileDependency = []
434 self.FileListMacros = {}
435 self.ListFileMacros = {}
438 self.FileDependency = []
439 self.LibraryBuildCommandList = []
440 self.LibraryFileList = []
441 self.LibraryMakefileList = []
442 self.LibraryBuildDirectoryList = []
443 self.SystemLibraryList = []
444 self.Macros = OrderedDict()
445 self.Macros["OUTPUT_DIR
" ] = self._AutoGenObject.Macros["OUTPUT_DIR
"]
446 self.Macros["DEBUG_DIR
" ] = self._AutoGenObject.Macros["DEBUG_DIR
"]
447 self.Macros["MODULE_BUILD_DIR
"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR
"]
448 self.Macros["BIN_DIR
" ] = self._AutoGenObject.Macros["BIN_DIR
"]
449 self.Macros["BUILD_DIR
" ] = self._AutoGenObject.Macros["BUILD_DIR
"]
450 self.Macros["WORKSPACE
" ] = self._AutoGenObject.Macros["WORKSPACE
"]
451 self.Macros["FFS_OUTPUT_DIR
" ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR
"]
452 self.GenFfsList = ModuleAutoGen.GenFfsList
453 self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']
454 self.FfsOutputFileList = []
456 # Compose a dict object containing information used to do replacement in template
457 def _CreateTemplateDict(self):
458 if self._FileType not in self._SEP_:
459 EdkLogger.error("build
", PARAMETER_INVALID, "Invalid Makefile
type [%s]" % self._FileType,
460 ExtraData="[%s]" % str(self._AutoGenObject))
461 Separator = self._SEP_[self._FileType]
463 # break build if no source files and binary files are found
464 if len(self._AutoGenObject.SourceFileList) == 0 and len(self._AutoGenObject.BinaryFileList) == 0:
465 EdkLogger.error("build
", AUTOGEN_ERROR, "No files to be built
in module
[%s, %s, %s]"
466 % (self._AutoGenObject.BuildTarget, self._AutoGenObject.ToolChain, self._AutoGenObject.Arch),
467 ExtraData="[%s]" % str(self._AutoGenObject))
469 # convert dependent libraries to build command
470 self.ProcessDependentLibrary()
471 if len(self._AutoGenObject.Module.ModuleEntryPointList) > 0:
472 ModuleEntryPoint = self._AutoGenObject.Module.ModuleEntryPointList[0]
474 ModuleEntryPoint = "_ModuleEntryPoint
"
476 # Intel EBC compiler enforces EfiMain
477 if self._AutoGenObject.AutoGenVersion < 0x00010005 and self._AutoGenObject.Arch == "EBC
":
478 ArchEntryPoint = "EfiMain
"
480 ArchEntryPoint = ModuleEntryPoint
482 if self._AutoGenObject.Arch == "EBC
":
483 # EBC compiler always use "EfiStart
" as entry point. Only applies to EdkII modules
484 ImageEntryPoint = "EfiStart
"
485 elif self._AutoGenObject.AutoGenVersion < 0x00010005:
486 # Edk modules use entry point specified in INF file
487 ImageEntryPoint = ModuleEntryPoint
489 # EdkII modules always use "_ModuleEntryPoint
" as entry point
490 ImageEntryPoint = "_ModuleEntryPoint
"
492 for k, v in self._AutoGenObject.Module.Defines.iteritems():
493 if k not in self._AutoGenObject.Macros:
494 self._AutoGenObject.Macros[k] = v
496 if 'MODULE_ENTRY_POINT' not in self._AutoGenObject.Macros:
497 self._AutoGenObject.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint
498 if 'ARCH_ENTRY_POINT' not in self._AutoGenObject.Macros:
499 self._AutoGenObject.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint
500 if 'IMAGE_ENTRY_POINT' not in self._AutoGenObject.Macros:
501 self._AutoGenObject.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint
503 PCI_COMPRESS_Flag = False
504 for k, v in self._AutoGenObject.Module.Defines.iteritems():
505 if 'PCI_COMPRESS' == k and 'TRUE' == v:
506 PCI_COMPRESS_Flag = True
510 IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]
511 for Tool in self._AutoGenObject.BuildOption:
512 for Attr in self._AutoGenObject.BuildOption[Tool]:
513 Value = self._AutoGenObject.BuildOption[Tool][Attr]
517 ToolsDef.append("%s = %s" % (Tool, Value))
519 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
522 # Remove duplicated include path, if any
524 Value = RemoveDupOption(Value, IncPrefix, self._AutoGenObject.IncludePathList)
525 if Tool == "OPTROM
" and PCI_COMPRESS_Flag:
526 ValueList = Value.split()
528 for i, v in enumerate(ValueList):
531 Value = ' '.join(ValueList)
533 ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
536 # generate the Response file and Response flag
537 RespDict = self.CommandExceedLimit()
538 RespFileList = os.path.join(self._AutoGenObject.OutputDir, 'respfilelist.txt')
540 RespFileListContent = ''
541 for Resp in RespDict:
542 RespFile = os.path.join(self._AutoGenObject.OutputDir, str(Resp).lower() + '.txt')
543 StrList = RespDict[Resp].split(' ')
548 UnexpandMacro.append(Str)
551 UnexpandMacroStr = ' '.join(UnexpandMacro)
552 NewRespStr = ' '.join(NewStr)
553 SaveFileOnChange(RespFile, NewRespStr, False)
554 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))
555 RespFileListContent += '@' + RespFile + os.linesep
556 RespFileListContent += NewRespStr + os.linesep
557 SaveFileOnChange(RespFileList, RespFileListContent, False)
559 if os.path.exists(RespFileList):
560 os.remove(RespFileList)
562 # convert source files and binary files to build targets
563 self.ResultFileList = [str(T.Target) for T in self._AutoGenObject.CodaTargetList]
564 if len(self.ResultFileList) == 0 and len(self._AutoGenObject.SourceFileList) <> 0:
565 EdkLogger.error("build
", AUTOGEN_ERROR, "Nothing to build
",
566 ExtraData="[%s]" % str(self._AutoGenObject))
568 self.ProcessBuildTargetList()
569 self.ParserGenerateFfsCmd()
571 # Generate macros used to represent input files
572 FileMacroList = [] # macro name = file list
573 for FileListMacro in self.FileListMacros:
574 FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
576 "macro_name
" : FileListMacro,
577 "source_file
" : self.FileListMacros[FileListMacro]
580 FileMacroList.append(FileMacro)
582 # INC_LIST is special
585 for P in self._AutoGenObject.IncludePathList:
586 IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))
587 if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
588 self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)
589 FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
591 "macro_name
" : "INC
",
592 "source_file
" : IncludePathList
595 FileMacroList.append(FileMacro)
597 # Generate macros used to represent files containing list of input files
598 for ListFileMacro in self.ListFileMacros:
599 ListFileName = os.path.join(self._AutoGenObject.OutputDir, "%s.lst
" % ListFileMacro.lower()[:len(ListFileMacro) - 5])
600 FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
603 "\n".join(self.ListFileMacros[ListFileMacro]),
607 # Edk modules need <BaseName>StrDefs.h for string ID
608 #if self._AutoGenObject.AutoGenVersion < 0x00010005 and len(self._AutoGenObject.UnicodeFileList) > 0:
609 # BcTargetList = ['strdefs']
614 MakefileName = self._FILE_NAME_[self._FileType]
615 LibraryMakeCommandList = []
616 for D in self.LibraryBuildDirectoryList:
617 Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}
618 LibraryMakeCommandList.append(Command)
620 package_rel_dir = self._AutoGenObject.SourceDir
621 current_dir = self.Macros["WORKSPACE
"]
623 while not found and os.sep in package_rel_dir:
624 index = package_rel_dir.index(os.sep)
625 current_dir = mws.join(current_dir, package_rel_dir[:index])
626 if os.path.exists(current_dir):
627 for fl in os.listdir(current_dir):
628 if fl.endswith('.dec'):
631 package_rel_dir = package_rel_dir[index + 1:]
633 MakefileTemplateDict = {
634 "makefile_header
" : self._FILE_HEADER_[self._FileType],
635 "makefile_path
" : os.path.join("$
(MODULE_BUILD_DIR
)", MakefileName),
636 "makefile_name
" : MakefileName,
637 "platform_name
" : self.PlatformInfo.Name,
638 "platform_guid
" : self.PlatformInfo.Guid,
639 "platform_version
" : self.PlatformInfo.Version,
640 "platform_relative_directory
": self.PlatformInfo.SourceDir,
641 "platform_output_directory
" : self.PlatformInfo.OutputDir,
642 "ffs_output_directory
" : self._AutoGenObject.Macros["FFS_OUTPUT_DIR
"],
643 "platform_dir
" : self._AutoGenObject.Macros["PLATFORM_DIR
"],
645 "module_name
" : self._AutoGenObject.Name,
646 "module_guid
" : self._AutoGenObject.Guid,
647 "module_name_guid
" : self._AutoGenObject._GetUniqueBaseName(),
648 "module_version
" : self._AutoGenObject.Version,
649 "module_type
" : self._AutoGenObject.ModuleType,
650 "module_file
" : self._AutoGenObject.MetaFile.Name,
651 "module_file_base_name
" : self._AutoGenObject.MetaFile.BaseName,
652 "module_relative_directory
" : self._AutoGenObject.SourceDir,
653 "module_dir
" : mws.join (self.Macros["WORKSPACE
"], self._AutoGenObject.SourceDir),
654 "package_relative_directory
": package_rel_dir,
655 "module_extra_defines
" : ["%s = %s" % (k, v) for k, v in self._AutoGenObject.Module.Defines.iteritems()],
657 "architecture
" : self._AutoGenObject.Arch,
658 "toolchain_tag
" : self._AutoGenObject.ToolChain,
659 "build_target
" : self._AutoGenObject.BuildTarget,
661 "platform_build_directory
" : self.PlatformInfo.BuildDir,
662 "module_build_directory
" : self._AutoGenObject.BuildDir,
663 "module_output_directory
" : self._AutoGenObject.OutputDir,
664 "module_debug_directory
" : self._AutoGenObject.DebugDir,
666 "separator
" : Separator,
667 "module_tool_definitions
" : ToolsDef,
669 "shell_command_code
" : self._SHELL_CMD_[self._FileType].keys(),
670 "shell_command
" : self._SHELL_CMD_[self._FileType].values(),
672 "module_entry_point
" : ModuleEntryPoint,
673 "image_entry_point
" : ImageEntryPoint,
674 "arch_entry_point
" : ArchEntryPoint,
675 "remaining_build_target
" : self.ResultFileList,
676 "common_dependency_file
" : self.CommonFileDependency,
677 "create_directory_command
" : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
678 "clean_command
" : self.GetRemoveDirectoryCommand(["$
(OUTPUT_DIR
)"]),
679 "cleanall_command
" : self.GetRemoveDirectoryCommand(["$
(DEBUG_DIR
)", "$
(OUTPUT_DIR
)"]),
680 "dependent_library_build_directory
" : self.LibraryBuildDirectoryList,
681 "library_build_command
" : LibraryMakeCommandList,
682 "file_macro
" : FileMacroList,
683 "file_build_target
" : self.BuildTargetList,
684 "backward_compatible_target
": BcTargetList,
687 return MakefileTemplateDict
689 def ParserGenerateFfsCmd(self):
690 #Add Ffs cmd to self.BuildTargetList
694 for Cmd in self.GenFfsList:
696 for CopyCmd in Cmd[2]:
698 Src = self.ReplaceMacro(Src)
699 Dst = self.ReplaceMacro(Dst)
700 if Dst not in self.ResultFileList:
701 self.ResultFileList.append(Dst)
702 if '%s :' %(Dst) not in self.BuildTargetList:
703 self.BuildTargetList.append("%s :" %(Dst))
704 self.BuildTargetList.append('\t' + self._CP_TEMPLATE_[self._FileType] %{'Src': Src, 'Dst': Dst})
707 for index, Str in enumerate(FfsCmdList):
709 OutputFile = FfsCmdList[index + 1]
711 if DepsFileList == []:
712 DepsFileList = [FfsCmdList[index + 1]]
714 DepsFileList.append(FfsCmdList[index + 1])
715 DepsFileString = ' '.join(DepsFileList).strip()
716 if DepsFileString == '':
718 OutputFile = self.ReplaceMacro(OutputFile)
719 self.ResultFileList.append(OutputFile)
720 DepsFileString = self.ReplaceMacro(DepsFileString)
721 self.BuildTargetList.append('%s : %s' % (OutputFile, DepsFileString))
722 CmdString = ' '.join(FfsCmdList).strip()
723 CmdString = self.ReplaceMacro(CmdString)
724 self.BuildTargetList.append('\t%s' % CmdString)
726 self.ParseSecCmd(DepsFileList, Cmd[1])
727 for SecOutputFile, SecDepsFile, SecCmd in self.FfsOutputFileList :
728 self.BuildTargetList.append('%s : %s' % (self.ReplaceMacro(SecOutputFile), self.ReplaceMacro(SecDepsFile)))
729 self.BuildTargetList.append('\t%s' % self.ReplaceMacro(SecCmd))
730 self.FfsOutputFileList = []
732 def ParseSecCmd(self, OutputFileList, CmdTuple):
733 for OutputFile in OutputFileList:
734 for SecCmdStr in CmdTuple:
736 SecCmdList = SecCmdStr.split()
737 CmdName = SecCmdList[0]
738 for index, CmdItem in enumerate(SecCmdList):
739 if '-o' == CmdItem and OutputFile == SecCmdList[index + 1]:
741 while index + 1 < len(SecCmdList):
742 if not SecCmdList[index+1].startswith('-'):
743 SecDepsFileList.append(SecCmdList[index + 1])
745 if CmdName == 'Trim':
746 SecDepsFileList.append(os.path.join('$(DEBUG_DIR)', os.path.basename(OutputFile).replace('offset', 'efi')))
747 if OutputFile.endswith('.ui') or OutputFile.endswith('.ver'):
748 SecDepsFileList.append(os.path.join('$(MODULE_DIR)','$(MODULE_FILE)'))
749 self.FfsOutputFileList.append((OutputFile, ' '.join(SecDepsFileList), SecCmdStr))
750 if len(SecDepsFileList) > 0:
751 self.ParseSecCmd(SecDepsFileList, CmdTuple)
756 def ReplaceMacro(self, str):
757 for Macro in self.MacroList:
758 if self._AutoGenObject.Macros[Macro] and self._AutoGenObject.Macros[Macro] in str:
759 str = str.replace(self._AutoGenObject.Macros[Macro], '$(' + Macro + ')')
762 def CommandExceedLimit(self):
764 'CC' : { 'Macro' : '$(CC_FLAGS)', 'Value' : False},
765 'PP' : { 'Macro' : '$(PP_FLAGS)', 'Value' : False},
766 'APP' : { 'Macro' : '$(APP_FLAGS)', 'Value' : False},
767 'ASLPP' : { 'Macro' : '$(ASLPP_FLAGS)', 'Value' : False},
768 'VFRPP' : { 'Macro' : '$(VFRPP_FLAGS)', 'Value' : False},
769 'ASM' : { 'Macro' : '$(ASM_FLAGS)', 'Value' : False},
770 'ASLCC' : { 'Macro' : '$(ASLCC_FLAGS)', 'Value' : False},
775 IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]
777 # base on the source files to decide the file type
778 for File in self._AutoGenObject.SourceFileList:
779 for type in self._AutoGenObject.FileTypes:
780 if File in self._AutoGenObject.FileTypes[type]:
781 if type not in FileTypeList:
782 FileTypeList.append(type)
784 # calculate the command-line length
786 for type in FileTypeList:
787 BuildTargets = self._AutoGenObject.BuildRules[type].BuildTargets
788 for Target in BuildTargets:
789 CommandList = BuildTargets[Target].Commands
790 for SingleCommand in CommandList:
792 SingleCommandLength = len(SingleCommand)
793 SingleCommandList = SingleCommand.split()
794 if len(SingleCommandList) > 0:
795 for Flag in FlagDict:
796 if '$('+ Flag +')' in SingleCommandList[0]:
800 if 'PATH' not in self._AutoGenObject._BuildOption[Tool]:
801 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))
802 SingleCommandLength += len(self._AutoGenObject._BuildOption[Tool]['PATH
'])
803 for item in SingleCommandList[1:]:
804 if FlagDict[Tool]['Macro
'] in item:
805 if 'FLAGS
' not in self._AutoGenObject._BuildOption[Tool]:
806 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))
807 Str = self._AutoGenObject._BuildOption[Tool]['FLAGS']
808 for Option in self._AutoGenObject.BuildOption:
809 for Attr in self._AutoGenObject.BuildOption[Option]:
810 if Str.find(Option + '_' + Attr) != -1:
811 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
812 while(Str.find('$(') != -1):
813 for macro in self._AutoGenObject.Macros:
814 MacroName = '$('+ macro + ')'
815 if (Str.find(MacroName) != -1):
816 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
820 SingleCommandLength += len(Str)
821 elif '$(INC)' in item:
822 SingleCommandLength += self._AutoGenObject.IncludePathLength + len(IncPrefix) * len(self._AutoGenObject._IncludePathList)
823 elif item.find('$(') != -1:
825 for Option in self._AutoGenObject.BuildOption:
826 for Attr in self._AutoGenObject.BuildOption[Option]:
827 if Str.find(Option + '_' + Attr) != -1:
828 Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
829 while(Str.find('$(') != -1):
830 for macro in self._AutoGenObject.Macros:
831 MacroName = '$('+ macro + ')'
832 if (Str.find(MacroName) != -1):
833 Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])
837 SingleCommandLength += len(Str)
839 if SingleCommandLength > GlobalData.gCommandMaxLength:
840 FlagDict[Tool]['Value'] = True
842 # generate the response file content by combine the FLAGS and INC
843 for Flag in FlagDict:
844 if FlagDict[Flag]['Value']:
846 RespMacro = FlagDict[Flag]['Macro'].replace('FLAGS', 'RESP')
847 Value = self._AutoGenObject.BuildOption[Flag]['FLAGS']
848 for inc in self._AutoGenObject._IncludePathList:
849 Value += ' ' + IncPrefix + inc
850 for Option in self._AutoGenObject.BuildOption:
851 for Attr in self._AutoGenObject.BuildOption[Option]:
852 if Value.find(Option + '_' + Attr) != -1:
853 Value = Value.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])
854 while (Value.find('$(') != -1):
855 for macro in self._AutoGenObject.Macros:
856 MacroName = '$('+ macro + ')'
857 if (Value.find(MacroName) != -1):
858 Value = Value.replace(MacroName, self._AutoGenObject.Macros[macro])
863 if self._AutoGenObject.ToolChainFamily == 'GCC':
864 RespDict[Key] = Value.replace('\\', '/')
866 RespDict[Key] = Value
867 for Target in BuildTargets:
868 for i, SingleCommand in enumerate(BuildTargets[Target].Commands):
869 if FlagDict[Flag]['Macro'] in SingleCommand:
870 BuildTargets[Target].Commands[i] = SingleCommand.replace('$(INC)','').replace(FlagDict[Flag]['Macro'], RespMacro)
873 def ProcessBuildTargetList(self):
875 # Search dependency file list for each source file
877 ForceIncludedFile = []
878 for File in self._AutoGenObject.AutoGenFileList:
880 ForceIncludedFile.append(File)
883 for Target in self._AutoGenObject.IntroTargetList:
884 SourceFileList.extend(Target.Inputs)
885 OutPutFileList.extend(Target.Outputs)
888 for Item in OutPutFileList:
889 if Item in SourceFileList:
890 SourceFileList.remove(Item)
892 self.FileDependency = self.GetFileDependency(
895 self._AutoGenObject.IncludePathList + self._AutoGenObject.BuildOptionIncPathList
898 for File in self.FileDependency:
899 if not self.FileDependency[File]:
900 self.FileDependency[File] = ['$(FORCE_REBUILD)']
903 self._AutoGenObject.AutoGenDepSet |= set(self.FileDependency[File])
906 if File.Ext not in [".c
", ".C
"] or File.Name == "AutoGen
.c
":
909 DepSet = set(self.FileDependency[File])
911 DepSet &= set(self.FileDependency[File])
912 # in case nothing in SourceFileList
916 # Extract common files list in the dependency files
919 self.CommonFileDependency.append(self.PlaceMacro(File.Path, self.Macros))
921 for File in self.FileDependency:
923 if File.Ext not in [".c
", ".C
"] or File.Name == "AutoGen
.c
":
925 NewDepSet = set(self.FileDependency[File])
927 self.FileDependency[File] = ["$
(COMMON_DEPS
)"] + list(NewDepSet)
929 # Convert target description object to target string in makefile
930 for Type in self._AutoGenObject.Targets:
931 for T in self._AutoGenObject.Targets[Type]:
932 # Generate related macros if needed
933 if T.GenFileListMacro and T.FileListMacro not in self.FileListMacros:
934 self.FileListMacros[T.FileListMacro] = []
935 if T.GenListFile and T.ListFileMacro not in self.ListFileMacros:
936 self.ListFileMacros[T.ListFileMacro] = []
937 if T.GenIncListFile and T.IncListFileMacro not in self.ListFileMacros:
938 self.ListFileMacros[T.IncListFileMacro] = []
941 # Add force-dependencies
942 for Dep in T.Dependencies:
943 Deps.append(self.PlaceMacro(str(Dep), self.Macros))
944 # Add inclusion-dependencies
945 if len(T.Inputs) == 1 and T.Inputs[0] in self.FileDependency:
946 for F in self.FileDependency[T.Inputs[0]]:
947 Deps.append(self.PlaceMacro(str(F), self.Macros))
948 # Add source-dependencies
950 NewFile = self.PlaceMacro(str(F), self.Macros)
951 # In order to use file list macro as dependency
953 # gnu tools need forward slash path separater, even on Windows
954 self.ListFileMacros[T.ListFileMacro].append(str(F).replace ('\\', '/'))
955 self.FileListMacros[T.FileListMacro].append(NewFile)
956 elif T.GenFileListMacro:
957 self.FileListMacros[T.FileListMacro].append(NewFile)
961 # Use file list macro as dependency
962 if T.GenFileListMacro:
963 Deps.append("$
(%s)" % T.FileListMacro)
964 if Type in [TAB_OBJECT_FILE, TAB_STATIC_LIBRARY]:
965 Deps.append("$
(%s)" % T.ListFileMacro)
968 "target
" : self.PlaceMacro(T.Target.Path, self.Macros),
969 "cmd
" : "\n\t".join(T.Commands),
972 self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))
974 ## For creating makefile targets for dependent libraries
975 def ProcessDependentLibrary(self):
976 for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
977 if not LibraryAutoGen.IsBinaryModule:
978 self.LibraryBuildDirectoryList.append(self.PlaceMacro(LibraryAutoGen.BuildDir, self.Macros))
980 ## Return a list containing source file's dependencies
982 # @param FileList The list of source files
983 # @param ForceInculeList The list of files which will be included forcely
984 # @param SearchPathList The list of search path
986 # @retval dict The mapping between source file path and its dependencies
988 def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):
991 Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)
994 ## Find dependencies for one source file
996 # By searching recursively "#include" directive in file, find out all the
997 # files needed by given source file. The dependecies will be only searched
998 # in given search path list.
1000 # @param File The source file
1001 # @param ForceInculeList The list of files which will be included forcely
1002 # @param SearchPathList The list of search path
1004 # @retval list The list of files the given source file depends on
1006 def GetDependencyList(self
, File
, ForceList
, SearchPathList
):
1007 EdkLogger
.debug(EdkLogger
.DEBUG_1
, "Try to get dependency files for %s" % File
)
1008 FileStack
= [File
] + ForceList
1009 DependencySet
= set()
1011 if self
._AutoGenObject
.Arch
not in gDependencyDatabase
:
1012 gDependencyDatabase
[self
._AutoGenObject
.Arch
] = {}
1013 DepDb
= gDependencyDatabase
[self
._AutoGenObject
.Arch
]
1015 while len(FileStack
) > 0:
1018 FullPathDependList
= []
1019 if F
in self
.FileCache
:
1020 for CacheFile
in self
.FileCache
[F
]:
1021 FullPathDependList
.append(CacheFile
)
1022 if CacheFile
not in DependencySet
:
1023 FileStack
.append(CacheFile
)
1024 DependencySet
.update(FullPathDependList
)
1027 CurrentFileDependencyList
= []
1029 CurrentFileDependencyList
= DepDb
[F
]
1032 Fd
= open(F
.Path
, 'r')
1033 except BaseException
, X
:
1034 EdkLogger
.error("build", FILE_OPEN_FAILURE
, ExtraData
=F
.Path
+ "\n\t" + str(X
))
1036 FileContent
= Fd
.read()
1038 if len(FileContent
) == 0:
1041 if FileContent
[0] == 0xff or FileContent
[0] == 0xfe:
1042 FileContent
= unicode(FileContent
, "utf-16")
1043 IncludedFileList
= gIncludePattern
.findall(FileContent
)
1045 for Inc
in IncludedFileList
:
1047 # if there's macro used to reference header file, expand it
1048 HeaderList
= gMacroPattern
.findall(Inc
)
1049 if len(HeaderList
) == 1 and len(HeaderList
[0]) == 2:
1050 HeaderType
= HeaderList
[0][0]
1051 HeaderKey
= HeaderList
[0][1]
1052 if HeaderType
in gIncludeMacroConversion
:
1053 Inc
= gIncludeMacroConversion
[HeaderType
] % {"HeaderKey" : HeaderKey
}
1055 # not known macro used in #include, always build the file by
1056 # returning a empty dependency
1057 self
.FileCache
[File
] = []
1059 Inc
= os
.path
.normpath(Inc
)
1060 CurrentFileDependencyList
.append(Inc
)
1061 DepDb
[F
] = CurrentFileDependencyList
1063 CurrentFilePath
= F
.Dir
1064 PathList
= [CurrentFilePath
] + SearchPathList
1065 for Inc
in CurrentFileDependencyList
:
1066 for SearchPath
in PathList
:
1067 FilePath
= os
.path
.join(SearchPath
, Inc
)
1068 if FilePath
in gIsFileMap
:
1069 if not gIsFileMap
[FilePath
]:
1071 # If isfile is called too many times, the performance is slow down.
1072 elif not os
.path
.isfile(FilePath
):
1073 gIsFileMap
[FilePath
] = False
1076 gIsFileMap
[FilePath
] = True
1077 FilePath
= PathClass(FilePath
)
1078 FullPathDependList
.append(FilePath
)
1079 if FilePath
not in DependencySet
:
1080 FileStack
.append(FilePath
)
1083 EdkLogger
.debug(EdkLogger
.DEBUG_9
, "%s included by %s was not found "\
1084 "in any given path:\n\t%s" % (Inc
, F
, "\n\t".join(SearchPathList
)))
1086 self
.FileCache
[F
] = FullPathDependList
1087 DependencySet
.update(FullPathDependList
)
1089 DependencySet
.update(ForceList
)
1090 if File
in DependencySet
:
1091 DependencySet
.remove(File
)
1092 DependencyList
= list(DependencySet
) # remove duplicate ones
1094 return DependencyList
1096 _TemplateDict
= property(_CreateTemplateDict
)
1098 ## CustomMakefile class
1100 # This class encapsules makefie and its generation for module. It uses template to generate
1101 # the content of makefile. The content of makefile will be got from ModuleAutoGen object.
1103 class CustomMakefile(BuildFile
):
1104 ## template used to generate the makefile for module with custom makefile
1105 _TEMPLATE_
= TemplateString('''\
1109 # Platform Macro Definition
1111 PLATFORM_NAME = ${platform_name}
1112 PLATFORM_GUID = ${platform_guid}
1113 PLATFORM_VERSION = ${platform_version}
1114 PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
1115 PLATFORM_DIR = ${platform_dir}
1116 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1119 # Module Macro Definition
1121 MODULE_NAME = ${module_name}
1122 MODULE_GUID = ${module_guid}
1123 MODULE_NAME_GUID = ${module_name_guid}
1124 MODULE_VERSION = ${module_version}
1125 MODULE_TYPE = ${module_type}
1126 MODULE_FILE = ${module_file}
1127 MODULE_FILE_BASE_NAME = ${module_file_base_name}
1128 BASE_NAME = $(MODULE_NAME)
1129 MODULE_RELATIVE_DIR = ${module_relative_directory}
1130 MODULE_DIR = ${module_dir}
1133 # Build Configuration Macro Definition
1135 ARCH = ${architecture}
1136 TOOLCHAIN = ${toolchain_tag}
1137 TOOLCHAIN_TAG = ${toolchain_tag}
1138 TARGET = ${build_target}
1141 # Build Directory Macro Definition
1143 # PLATFORM_BUILD_DIR = ${platform_build_directory}
1144 BUILD_DIR = ${platform_build_directory}
1145 BIN_DIR = $(BUILD_DIR)${separator}${architecture}
1146 LIB_DIR = $(BIN_DIR)
1147 MODULE_BUILD_DIR = ${module_build_directory}
1148 OUTPUT_DIR = ${module_output_directory}
1149 DEBUG_DIR = ${module_debug_directory}
1150 DEST_DIR_OUTPUT = $(OUTPUT_DIR)
1151 DEST_DIR_DEBUG = $(DEBUG_DIR)
1154 # Tools definitions specific to this module
1156 ${BEGIN}${module_tool_definitions}
1158 MAKE_FILE = ${makefile_path}
1161 # Shell Command Macro
1163 ${BEGIN}${shell_command_code} = ${shell_command}
1166 ${custom_makefile_content}
1169 # Target used when called from platform makefile, which will bypass the build of dependent libraries
1182 # Build Target used in multi-thread build mode, which no init target is needed
1188 # Initialization target: print build information and create necessary directories
1191 \t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
1192 ${BEGIN}\t-@${create_directory_command}\n${END}\
1196 ## Constructor of CustomMakefile
1198 # @param ModuleAutoGen Object of ModuleAutoGen class
1200 def __init__(self
, ModuleAutoGen
):
1201 BuildFile
.__init
__(self
, ModuleAutoGen
)
1202 self
.PlatformInfo
= self
._AutoGenObject
.PlatformInfo
1203 self
.IntermediateDirectoryList
= ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
1205 # Compose a dict object containing information used to do replacement in template
1206 def _CreateTemplateDict(self
):
1207 Separator
= self
._SEP
_[self
._FileType
]
1208 if self
._FileType
not in self
._AutoGenObject
.CustomMakefile
:
1209 EdkLogger
.error('build', OPTION_NOT_SUPPORTED
, "No custom makefile for %s" % self
._FileType
,
1210 ExtraData
="[%s]" % str(self
._AutoGenObject
))
1211 MakefilePath
= mws
.join(
1212 self
._AutoGenObject
.WorkspaceDir
,
1213 self
._AutoGenObject
.CustomMakefile
[self
._FileType
]
1216 CustomMakefile
= open(MakefilePath
, 'r').read()
1218 EdkLogger
.error('build', FILE_OPEN_FAILURE
, File
=str(self
._AutoGenObject
),
1219 ExtraData
=self
._AutoGenObject
.CustomMakefile
[self
._FileType
])
1223 for Tool
in self
._AutoGenObject
.BuildOption
:
1224 # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
1227 for Attr
in self
._AutoGenObject
.BuildOption
[Tool
]:
1228 if Attr
== "FAMILY":
1230 elif Attr
== "PATH":
1231 ToolsDef
.append("%s = %s" % (Tool
, self
._AutoGenObject
.BuildOption
[Tool
][Attr
]))
1233 ToolsDef
.append("%s_%s = %s" % (Tool
, Attr
, self
._AutoGenObject
.BuildOption
[Tool
][Attr
]))
1236 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1237 MakefileTemplateDict
= {
1238 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1239 "makefile_path" : os
.path
.join("$(MODULE_BUILD_DIR)", MakefileName
),
1240 "platform_name" : self
.PlatformInfo
.Name
,
1241 "platform_guid" : self
.PlatformInfo
.Guid
,
1242 "platform_version" : self
.PlatformInfo
.Version
,
1243 "platform_relative_directory": self
.PlatformInfo
.SourceDir
,
1244 "platform_output_directory" : self
.PlatformInfo
.OutputDir
,
1245 "platform_dir" : self
._AutoGenObject
.Macros
["PLATFORM_DIR"],
1247 "module_name" : self
._AutoGenObject
.Name
,
1248 "module_guid" : self
._AutoGenObject
.Guid
,
1249 "module_name_guid" : self
._AutoGenObject
._GetUniqueBaseName
(),
1250 "module_version" : self
._AutoGenObject
.Version
,
1251 "module_type" : self
._AutoGenObject
.ModuleType
,
1252 "module_file" : self
._AutoGenObject
.MetaFile
,
1253 "module_file_base_name" : self
._AutoGenObject
.MetaFile
.BaseName
,
1254 "module_relative_directory" : self
._AutoGenObject
.SourceDir
,
1255 "module_dir" : mws
.join (self
._AutoGenObject
.WorkspaceDir
, self
._AutoGenObject
.SourceDir
),
1257 "architecture" : self
._AutoGenObject
.Arch
,
1258 "toolchain_tag" : self
._AutoGenObject
.ToolChain
,
1259 "build_target" : self
._AutoGenObject
.BuildTarget
,
1261 "platform_build_directory" : self
.PlatformInfo
.BuildDir
,
1262 "module_build_directory" : self
._AutoGenObject
.BuildDir
,
1263 "module_output_directory" : self
._AutoGenObject
.OutputDir
,
1264 "module_debug_directory" : self
._AutoGenObject
.DebugDir
,
1266 "separator" : Separator
,
1267 "module_tool_definitions" : ToolsDef
,
1269 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1270 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1272 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1273 "custom_makefile_content" : CustomMakefile
1276 return MakefileTemplateDict
1278 _TemplateDict
= property(_CreateTemplateDict
)
1280 ## PlatformMakefile class
1282 # This class encapsules makefie and its generation for platform. It uses
1283 # template to generate the content of makefile. The content of makefile will be
1284 # got from PlatformAutoGen object.
1286 class PlatformMakefile(BuildFile
):
1287 ## template used to generate the makefile for platform
1288 _TEMPLATE_
= TemplateString('''\
1292 # Platform Macro Definition
1294 PLATFORM_NAME = ${platform_name}
1295 PLATFORM_GUID = ${platform_guid}
1296 PLATFORM_VERSION = ${platform_version}
1297 PLATFORM_FILE = ${platform_file}
1298 PLATFORM_DIR = ${platform_dir}
1299 PLATFORM_OUTPUT_DIR = ${platform_output_directory}
1302 # Build Configuration Macro Definition
1304 TOOLCHAIN = ${toolchain_tag}
1305 TOOLCHAIN_TAG = ${toolchain_tag}
1306 TARGET = ${build_target}
1309 # Build Directory Macro Definition
1311 BUILD_DIR = ${platform_build_directory}
1312 FV_DIR = ${platform_build_directory}${separator}FV
1315 # Shell Command Macro
1317 ${BEGIN}${shell_command_code} = ${shell_command}
1321 MAKE_FILE = ${makefile_path}
1326 all: init build_libraries build_modules
1329 # Initialization target: print build information and create necessary directories
1332 \t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
1333 \t${BEGIN}-@${create_directory_command}
1336 # library build target
1338 libraries: init build_libraries
1341 # module build target
1343 modules: init build_libraries build_modules
1346 # Build all libraries:
1349 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
1350 ${END}\t@cd $(BUILD_DIR)
1353 # Build all modules:
1356 ${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
1357 ${END}\t@cd $(BUILD_DIR)
1360 # Clean intermediate files
1363 \t${BEGIN}-@${library_build_command} clean
1364 \t${END}${BEGIN}-@${module_build_command} clean
1365 \t${END}@cd $(BUILD_DIR)
1368 # Clean all generated files except to makefile
1371 ${BEGIN}\t${cleanall_command}
1375 # Clean all library files
1378 \t${BEGIN}-@${library_build_command} cleanall
1379 \t${END}@cd $(BUILD_DIR)\n
1382 ## Constructor of PlatformMakefile
1384 # @param ModuleAutoGen Object of PlatformAutoGen class
1386 def __init__(self
, PlatformAutoGen
):
1387 BuildFile
.__init
__(self
, PlatformAutoGen
)
1388 self
.ModuleBuildCommandList
= []
1389 self
.ModuleMakefileList
= []
1390 self
.IntermediateDirectoryList
= []
1391 self
.ModuleBuildDirectoryList
= []
1392 self
.LibraryBuildDirectoryList
= []
1393 self
.LibraryMakeCommandList
= []
1395 # Compose a dict object containing information used to do replacement in template
1396 def _CreateTemplateDict(self
):
1397 Separator
= self
._SEP
_[self
._FileType
]
1399 PlatformInfo
= self
._AutoGenObject
1400 if "MAKE" not in PlatformInfo
.ToolDefinition
or "PATH" not in PlatformInfo
.ToolDefinition
["MAKE"]:
1401 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1402 ExtraData
="[%s]" % str(self
._AutoGenObject
))
1404 self
.IntermediateDirectoryList
= ["$(BUILD_DIR)"]
1405 self
.ModuleBuildDirectoryList
= self
.GetModuleBuildDirectoryList()
1406 self
.LibraryBuildDirectoryList
= self
.GetLibraryBuildDirectoryList()
1408 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1409 LibraryMakefileList
= []
1410 LibraryMakeCommandList
= []
1411 for D
in self
.LibraryBuildDirectoryList
:
1412 D
= self
.PlaceMacro(D
, {"BUILD_DIR":PlatformInfo
.BuildDir
})
1413 Makefile
= os
.path
.join(D
, MakefileName
)
1414 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":Makefile
}
1415 LibraryMakefileList
.append(Makefile
)
1416 LibraryMakeCommandList
.append(Command
)
1417 self
.LibraryMakeCommandList
= LibraryMakeCommandList
1419 ModuleMakefileList
= []
1420 ModuleMakeCommandList
= []
1421 for D
in self
.ModuleBuildDirectoryList
:
1422 D
= self
.PlaceMacro(D
, {"BUILD_DIR":PlatformInfo
.BuildDir
})
1423 Makefile
= os
.path
.join(D
, MakefileName
)
1424 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":Makefile
}
1425 ModuleMakefileList
.append(Makefile
)
1426 ModuleMakeCommandList
.append(Command
)
1428 MakefileTemplateDict
= {
1429 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1430 "makefile_path" : os
.path
.join("$(BUILD_DIR)", MakefileName
),
1431 "make_path" : PlatformInfo
.ToolDefinition
["MAKE"]["PATH"],
1432 "makefile_name" : MakefileName
,
1433 "platform_name" : PlatformInfo
.Name
,
1434 "platform_guid" : PlatformInfo
.Guid
,
1435 "platform_version" : PlatformInfo
.Version
,
1436 "platform_file" : self
._AutoGenObject
.MetaFile
,
1437 "platform_relative_directory": PlatformInfo
.SourceDir
,
1438 "platform_output_directory" : PlatformInfo
.OutputDir
,
1439 "platform_build_directory" : PlatformInfo
.BuildDir
,
1440 "platform_dir" : self
._AutoGenObject
.Macros
["PLATFORM_DIR"],
1442 "toolchain_tag" : PlatformInfo
.ToolChain
,
1443 "build_target" : PlatformInfo
.BuildTarget
,
1444 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1445 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1446 "build_architecture_list" : self
._AutoGenObject
.Arch
,
1447 "architecture" : self
._AutoGenObject
.Arch
,
1448 "separator" : Separator
,
1449 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1450 "cleanall_command" : self
.GetRemoveDirectoryCommand(self
.IntermediateDirectoryList
),
1451 "library_makefile_list" : LibraryMakefileList
,
1452 "module_makefile_list" : ModuleMakefileList
,
1453 "library_build_command" : LibraryMakeCommandList
,
1454 "module_build_command" : ModuleMakeCommandList
,
1457 return MakefileTemplateDict
1459 ## Get the root directory list for intermediate files of all modules build
1461 # @retval list The list of directory
1463 def GetModuleBuildDirectoryList(self
):
1465 for ModuleAutoGen
in self
._AutoGenObject
.ModuleAutoGenList
:
1466 if not ModuleAutoGen
.IsBinaryModule
:
1467 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, ModuleAutoGen
.BuildDir
))
1470 ## Get the root directory list for intermediate files of all libraries build
1472 # @retval list The list of directory
1474 def GetLibraryBuildDirectoryList(self
):
1476 for LibraryAutoGen
in self
._AutoGenObject
.LibraryAutoGenList
:
1477 if not LibraryAutoGen
.IsBinaryModule
:
1478 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, LibraryAutoGen
.BuildDir
))
1481 _TemplateDict
= property(_CreateTemplateDict
)
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
1502 def _CreateTemplateDict(self
):
1503 Separator
= self
._SEP
_[self
._FileType
]
1505 # any platform autogen object is ok because we just need common information
1506 PlatformInfo
= self
._AutoGenObject
1508 if "MAKE" not in PlatformInfo
.ToolDefinition
or "PATH" not in PlatformInfo
.ToolDefinition
["MAKE"]:
1509 EdkLogger
.error("build", OPTION_MISSING
, "No MAKE command defined. Please check your tools_def.txt!",
1510 ExtraData
="[%s]" % str(self
._AutoGenObject
))
1512 for Arch
in PlatformInfo
.ArchList
:
1513 self
.IntermediateDirectoryList
.append(Separator
.join(["$(BUILD_DIR)", Arch
]))
1514 self
.IntermediateDirectoryList
.append("$(FV_DIR)")
1516 # TRICK: for not generating GenFds call in makefile if no FDF file
1518 if PlatformInfo
.FdfFile
is not None and PlatformInfo
.FdfFile
!= "":
1519 FdfFileList
= [PlatformInfo
.FdfFile
]
1520 # macros passed to GenFds
1521 MacroList
.append('"%s=%s"' % ("EFI_SOURCE", GlobalData
.gEfiSource
.replace('\\', '\\\\')))
1522 MacroList
.append('"%s=%s"' % ("EDK_SOURCE", GlobalData
.gEdkSource
.replace('\\', '\\\\')))
1524 MacroDict
.update(GlobalData
.gGlobalDefines
)
1525 MacroDict
.update(GlobalData
.gCommandLineDefines
)
1526 MacroDict
.pop("EFI_SOURCE", "dummy")
1527 MacroDict
.pop("EDK_SOURCE", "dummy")
1528 for MacroName
in MacroDict
:
1529 if MacroDict
[MacroName
] != "":
1530 MacroList
.append('"%s=%s"' % (MacroName
, MacroDict
[MacroName
].replace('\\', '\\\\')))
1532 MacroList
.append('"%s"' % MacroName
)
1536 # pass extra common options to external program called in makefile, currently GenFds.exe
1538 LogLevel
= EdkLogger
.GetLevel()
1539 if LogLevel
== EdkLogger
.VERBOSE
:
1540 ExtraOption
+= " -v"
1541 elif LogLevel
<= EdkLogger
.DEBUG_9
:
1542 ExtraOption
+= " -d %d" % (LogLevel
- 1)
1543 elif LogLevel
== EdkLogger
.QUIET
:
1544 ExtraOption
+= " -q"
1546 if GlobalData
.gCaseInsensitive
:
1547 ExtraOption
+= " -c"
1548 if GlobalData
.gEnableGenfdsMultiThread
:
1549 ExtraOption
+= " --genfds-multi-thread"
1550 if GlobalData
.gIgnoreSource
:
1551 ExtraOption
+= " --ignore-sources"
1553 for pcd
in GlobalData
.BuildOptionPcd
:
1555 pcdname
= '.'.join(pcd
[0:3])
1557 pcdname
= '.'.join(pcd
[0:2])
1558 if pcd
[3].startswith('{'):
1559 ExtraOption
+= " --pcd " + pcdname
+ '=' + 'H' + '"' + pcd
[3] + '"'
1561 ExtraOption
+= " --pcd " + pcdname
+ '=' + pcd
[3]
1563 MakefileName
= self
._FILE
_NAME
_[self
._FileType
]
1564 SubBuildCommandList
= []
1565 for A
in PlatformInfo
.ArchList
:
1566 Command
= self
._MAKE
_TEMPLATE
_[self
._FileType
] % {"file":os
.path
.join("$(BUILD_DIR)", A
, MakefileName
)}
1567 SubBuildCommandList
.append(Command
)
1569 MakefileTemplateDict
= {
1570 "makefile_header" : self
._FILE
_HEADER
_[self
._FileType
],
1571 "makefile_path" : os
.path
.join("$(BUILD_DIR)", MakefileName
),
1572 "make_path" : PlatformInfo
.ToolDefinition
["MAKE"]["PATH"],
1573 "platform_name" : PlatformInfo
.Name
,
1574 "platform_guid" : PlatformInfo
.Guid
,
1575 "platform_version" : PlatformInfo
.Version
,
1576 "platform_build_directory" : PlatformInfo
.BuildDir
,
1577 "conf_directory" : GlobalData
.gConfDirectory
,
1579 "toolchain_tag" : PlatformInfo
.ToolChain
,
1580 "build_target" : PlatformInfo
.BuildTarget
,
1581 "shell_command_code" : self
._SHELL
_CMD
_[self
._FileType
].keys(),
1582 "shell_command" : self
._SHELL
_CMD
_[self
._FileType
].values(),
1583 'arch' : list(PlatformInfo
.ArchList
),
1584 "build_architecture_list" : ','.join(PlatformInfo
.ArchList
),
1585 "separator" : Separator
,
1586 "create_directory_command" : self
.GetCreateDirectoryCommand(self
.IntermediateDirectoryList
),
1587 "cleanall_command" : self
.GetRemoveDirectoryCommand(self
.IntermediateDirectoryList
),
1588 "sub_build_command" : SubBuildCommandList
,
1589 "fdf_file" : FdfFileList
,
1590 "active_platform" : str(PlatformInfo
),
1591 "fd" : PlatformInfo
.FdTargetList
,
1592 "fv" : PlatformInfo
.FvTargetList
,
1593 "cap" : PlatformInfo
.CapTargetList
,
1594 "extra_options" : ExtraOption
,
1595 "macro" : MacroList
,
1598 return MakefileTemplateDict
1600 ## Get the root directory list for intermediate files of all modules build
1602 # @retval list The list of directory
1604 def GetModuleBuildDirectoryList(self
):
1606 for ModuleAutoGen
in self
._AutoGenObject
.ModuleAutoGenList
:
1607 if not ModuleAutoGen
.IsBinaryModule
:
1608 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, ModuleAutoGen
.BuildDir
))
1611 ## Get the root directory list for intermediate files of all libraries build
1613 # @retval list The list of directory
1615 def GetLibraryBuildDirectoryList(self
):
1617 for LibraryAutoGen
in self
._AutoGenObject
.LibraryAutoGenList
:
1618 if not LibraryAutoGen
.IsBinaryModule
:
1619 DirList
.append(os
.path
.join(self
._AutoGenObject
.BuildDir
, LibraryAutoGen
.BuildDir
))
1622 _TemplateDict
= property(_CreateTemplateDict
)
1624 # This acts like the main() function for the script, unless it is 'import'ed into another script.
1625 if __name__
== '__main__':