]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/GenMake.py
BaseTools: Add a checking for Sources section in INF file
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenMake.py
index 3720c8bfedbdfb8abdd2ba8ae669f7c2bf540c2f..5c992d7c267437bcfe79c798f40e442f0c5f1c3b 100644 (file)
-## @file
-# Create makefile for MS nmake and GNU make
-#
-# Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
-# This program and the accompanying materials
-# are licensed and made available under the terms and conditions of the BSD License
-# which accompanies this distribution.  The full text of the license may be found at
-# http://opensource.org/licenses/bsd-license.php
-#
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
-#
-
-## Import Modules
-#
-import os
-import sys
-import string
-import re
-import os.path as path
-
-from Common.BuildToolError import *
-from Common.Misc import *
-from Common.String import *
-from BuildEngine import *
-import Common.GlobalData as GlobalData
-
-## Regular expression for finding header file inclusions
-gIncludePattern = re.compile(r"^[ \t]*#?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE|re.UNICODE|re.IGNORECASE)
-
-## Regular expression for matching macro used in header file inclusion
-gMacroPattern = re.compile("([_A-Z][_A-Z0-9]*)[ \t]*\((.+)\)", re.UNICODE)
-
-## pattern for include style in Edk.x code
-gProtocolDefinition = "Protocol/%(HeaderKey)s/%(HeaderKey)s.h"
-gGuidDefinition = "Guid/%(HeaderKey)s/%(HeaderKey)s.h"
-gArchProtocolDefinition = "ArchProtocol/%(HeaderKey)s/%(HeaderKey)s.h"
-gPpiDefinition = "Ppi/%(HeaderKey)s/%(HeaderKey)s.h"
-gIncludeMacroConversion = {
-  "EFI_PROTOCOL_DEFINITION"         :   gProtocolDefinition,
-  "EFI_GUID_DEFINITION"             :   gGuidDefinition,
-  "EFI_ARCH_PROTOCOL_DEFINITION"    :   gArchProtocolDefinition,
-  "EFI_PROTOCOL_PRODUCER"           :   gProtocolDefinition,
-  "EFI_PROTOCOL_CONSUMER"           :   gProtocolDefinition,
-  "EFI_PROTOCOL_DEPENDENCY"         :   gProtocolDefinition,
-  "EFI_ARCH_PROTOCOL_PRODUCER"      :   gArchProtocolDefinition,
-  "EFI_ARCH_PROTOCOL_CONSUMER"      :   gArchProtocolDefinition,
-  "EFI_ARCH_PROTOCOL_DEPENDENCY"    :   gArchProtocolDefinition,
-  "EFI_PPI_DEFINITION"              :   gPpiDefinition,
-  "EFI_PPI_PRODUCER"                :   gPpiDefinition,
-  "EFI_PPI_CONSUMER"                :   gPpiDefinition,
-  "EFI_PPI_DEPENDENCY"              :   gPpiDefinition,
-}
-
-## default makefile type
-gMakeType = ""
-if sys.platform == "win32":
-    gMakeType = "nmake"
-else:
-    gMakeType = "gmake"
-
-
-## BuildFile class
-#
-#  This base class encapsules build file and its generation. It uses template to generate
-#  the content of build file. The content of build file will be got from AutoGen objects.
-#
-class BuildFile(object):
-    ## template used to generate the build file (i.e. makefile if using make)
-    _TEMPLATE_ = TemplateString('')
-
-    _DEFAULT_FILE_NAME_ = "Makefile"
-
-    ## default file name for each type of build file
-    _FILE_NAME_ = {
-        "nmake" :   "Makefile",
-        "gmake" :   "GNUmakefile"
-    }
-
-    ## Fixed header string for makefile
-    _MAKEFILE_HEADER = '''#
-# DO NOT EDIT
-# This file is auto-generated by build utility
-#
-# Module Name:
-#
-#   %s
-#
-# Abstract:
-#
-#   Auto-generated makefile for building modules, libraries or platform
-#
-    '''
-
-    ## Header string for each type of build file
-    _FILE_HEADER_ = {
-        "nmake" :   _MAKEFILE_HEADER % _FILE_NAME_["nmake"],
-        "gmake" :   _MAKEFILE_HEADER % _FILE_NAME_["gmake"]
-    }
-
-    ## shell commands which can be used in build file in the form of macro
-    #   $(CP)     copy file command
-    #   $(MV)     move file command
-    #   $(RM)     remove file command
-    #   $(MD)     create dir command
-    #   $(RD)     remove dir command
-    #
-    _SHELL_CMD_ = {
-        "nmake" : {
-            "CP"    :   "copy /y",
-            "MV"    :   "move /y",
-            "RM"    :   "del /f /q",
-            "MD"    :   "mkdir",
-            "RD"    :   "rmdir /s /q",
-        },
-
-        "gmake" : {
-            "CP"    :   "cp -f",
-            "MV"    :   "mv -f",
-            "RM"    :   "rm -f",
-            "MD"    :   "mkdir -p",
-            "RD"    :   "rm -r -f",
-        }
-    }
-
-    ## directory separator
-    _SEP_ = {
-        "nmake" :   "\\",
-        "gmake" :   "/"
-    }
-
-    ## directory creation template
-    _MD_TEMPLATE_ = {
-        "nmake" :   'if not exist %(dir)s $(MD) %(dir)s',
-        "gmake" :   "$(MD) %(dir)s"
-    }
-
-    ## directory removal template
-    _RD_TEMPLATE_ = {
-        "nmake" :   'if exist %(dir)s $(RD) %(dir)s',
-        "gmake" :   "$(RD) %(dir)s"
-    }
-
-    _CD_TEMPLATE_ = {
-        "nmake" :   'if exist %(dir)s cd %(dir)s',
-        "gmake" :   "test -e %(dir)s && cd %(dir)s"
-    }
-
-    _MAKE_TEMPLATE_ = {
-        "nmake" :   'if exist %(file)s "$(MAKE)" $(MAKE_FLAGS) -f %(file)s',
-        "gmake" :   'test -e %(file)s && "$(MAKE)" $(MAKE_FLAGS) -f %(file)s'
-    }
-
-    _INCLUDE_CMD_ = {
-        "nmake" :   '!INCLUDE',
-        "gmake" :   "include"
-    }
-
-    _INC_FLAG_ = {"MSFT" : "/I", "GCC" : "-I", "INTEL" : "-I", "RVCT" : "-I"}
-
-    ## Constructor of BuildFile
-    #
-    #   @param  AutoGenObject   Object of AutoGen class
-    #
-    def __init__(self, AutoGenObject):
-        self._AutoGenObject = AutoGenObject
-        self._FileType = gMakeType
-
-    ## Create build file
-    #
-    #   @param  FileType    Type of build file. Only nmake and gmake are supported now.
-    #
-    #   @retval TRUE        The build file is created or re-created successfully
-    #   @retval FALSE       The build file exists and is the same as the one to be generated
-    #
-    def Generate(self, FileType=gMakeType):
-        if FileType not in self._FILE_NAME_:
-            EdkLogger.error("build", PARAMETER_INVALID, "Invalid build type [%s]" % FileType,
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-        self._FileType = FileType
-        FileContent = self._TEMPLATE_.Replace(self._TemplateDict)
-        FileName = self._FILE_NAME_[FileType]
-        return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)
-
-    ## Return a list of directory creation command string
-    #
-    #   @param      DirList     The list of directory to be created
-    #
-    #   @retval     list        The directory creation command list
-    #
-    def GetCreateDirectoryCommand(self, DirList):
-        return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
-
-    ## Return a list of directory removal command string
-    #
-    #   @param      DirList     The list of directory to be removed
-    #
-    #   @retval     list        The directory removal command list
-    #
-    def GetRemoveDirectoryCommand(self, DirList):
-        return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]
-
-    def PlaceMacro(self, Path, MacroDefinitions={}):
-        if Path.startswith("$("):
-            return Path
-        else:
-            PathLength = len(Path)
-            for MacroName in MacroDefinitions:
-                MacroValue = MacroDefinitions[MacroName]
-                MacroValueLength = len(MacroValue)
-                if MacroValueLength <= PathLength and Path.startswith(MacroValue):
-                    Path = "$(%s)%s" % (MacroName, Path[MacroValueLength:])
-                    break
-            return Path
-
-## ModuleMakefile class
-#
-#  This class encapsules makefie and its generation for module. It uses template to generate
-#  the content of makefile. The content of makefile will be got from ModuleAutoGen object.
-#
-class ModuleMakefile(BuildFile):
-    ## template used to generate the makefile for module
-    _TEMPLATE_ = TemplateString('''\
-${makefile_header}
-
-#
-# Platform Macro Definition
-#
-PLATFORM_NAME = ${platform_name}
-PLATFORM_GUID = ${platform_guid}
-PLATFORM_VERSION = ${platform_version}
-PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
-PLATFORM_DIR = $(WORKSPACE)${separator}${platform_relative_directory}
-PLATFORM_OUTPUT_DIR = ${platform_output_directory}
-
-#
-# Module Macro Definition
-#
-MODULE_NAME = ${module_name}
-MODULE_GUID = ${module_guid}
-MODULE_VERSION = ${module_version}
-MODULE_TYPE = ${module_type}
-MODULE_FILE = ${module_file}
-MODULE_FILE_BASE_NAME = ${module_file_base_name}
-BASE_NAME = $(MODULE_NAME)
-MODULE_RELATIVE_DIR = ${module_relative_directory}
-MODULE_DIR = $(WORKSPACE)${separator}${module_relative_directory}
-
-MODULE_ENTRY_POINT = ${module_entry_point}
-ARCH_ENTRY_POINT = ${arch_entry_point}
-IMAGE_ENTRY_POINT = ${image_entry_point}
-
-${BEGIN}${module_extra_defines}
-${END}
-#
-# Build Configuration Macro Definition
-#
-ARCH = ${architecture}
-TOOLCHAIN = ${toolchain_tag}
-TOOLCHAIN_TAG = ${toolchain_tag}
-TARGET = ${build_target}
-
-#
-# Build Directory Macro Definition
-#
-# PLATFORM_BUILD_DIR = ${platform_build_directory}
-BUILD_DIR = ${platform_build_directory}
-BIN_DIR = $(BUILD_DIR)${separator}${architecture}
-LIB_DIR = $(BIN_DIR)
-MODULE_BUILD_DIR = ${module_build_directory}
-OUTPUT_DIR = ${module_output_directory}
-DEBUG_DIR = ${module_debug_directory}
-DEST_DIR_OUTPUT = $(OUTPUT_DIR)
-DEST_DIR_DEBUG = $(DEBUG_DIR)
-
-#
-# Shell Command Macro
-#
-${BEGIN}${shell_command_code} = ${shell_command}
-${END}
-
-#
-# Tools definitions specific to this module
-#
-${BEGIN}${module_tool_definitions}
-${END}
-MAKE_FILE = ${makefile_path}
-
-#
-# Build Macro
-#
-${BEGIN}${file_macro}
-${END}
-
-COMMON_DEPS = ${BEGIN}${common_dependency_file} \\
-              ${END}
-
-#
-# Overridable Target Macro Definitions
-#
-FORCE_REBUILD = force_build
-INIT_TARGET = init
-PCH_TARGET =
-BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}
-CODA_TARGET = ${BEGIN}${remaining_build_target} \\
-              ${END}
-
-#
-# Default target, which will build dependent libraries in addition to source files
-#
-
-all: mbuild
-
-
-#
-# Target used when called from platform makefile, which will bypass the build of dependent libraries
-#
-
-pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
-
-#
-# ModuleTarget
-#
-
-mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)
-
-#
-# Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets
-#
-
-tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)
-
-#
-# Phony target which is used to force executing commands for a target
-#
-force_build:
-\t-@
-
-#
-# Target to update the FD
-#
-
-fds: mbuild gen_fds
-
-#
-# Initialization target: print build information and create necessary directories
-#
-init: info dirs
-
-info:
-\t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
-
-dirs:
-${BEGIN}\t-@${create_directory_command}\n${END}
-
-strdefs:
-\t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h
-
-#
-# GenLibsTarget
-#
-gen_libs:
-\t${BEGIN}@"$(MAKE)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}
-\t${END}@cd $(MODULE_BUILD_DIR)
-
-#
-# Build Flash Device Image
-#
-gen_fds:
-\t@"$(MAKE)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds
-\t@cd $(MODULE_BUILD_DIR)
-
-#
-# Individual Object Build Targets
-#
-${BEGIN}${file_build_target}
-${END}
-
-#
-# clean all intermediate files
-#
-clean:
-\t${BEGIN}${clean_command}
-\t${END}
-
-#
-# clean all generated files
-#
-cleanall:
-${BEGIN}\t${cleanall_command}
-${END}\t$(RM) *.pdb *.idb > NUL 2>&1
-\t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi
-
-#
-# clean all dependent libraries built
-#
-cleanlib:
-\t${BEGIN}-@${library_build_command} cleanall
-\t${END}@cd $(MODULE_BUILD_DIR)\n\n''')
-
-    _FILE_MACRO_TEMPLATE = TemplateString("${macro_name} = ${BEGIN} \\\n    ${source_file}${END}\n")
-    _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target} : ${deps}\n${END}\t${cmd}\n")
-
-    ## Constructor of ModuleMakefile
-    #
-    #   @param  ModuleAutoGen   Object of ModuleAutoGen class
-    #
-    def __init__(self, ModuleAutoGen):
-        BuildFile.__init__(self, ModuleAutoGen)
-        self.PlatformInfo = self._AutoGenObject.PlatformInfo
-
-        self.ResultFileList = []
-        self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
-
-        self.SourceFileDatabase = {}        # {file type : file path}
-        self.DestFileDatabase = {}          # {file type : file path}
-        self.FileBuildTargetList = []       # [(src, target string)]
-        self.BuildTargetList = []           # [target string]
-        self.PendingBuildTargetList = []    # [FileBuildRule objects]
-        self.CommonFileDependency = []
-        self.FileListMacros = {}
-        self.ListFileMacros = {}
-
-        self.FileDependency = []
-        self.LibraryBuildCommandList = []
-        self.LibraryFileList = []
-        self.LibraryMakefileList = []
-        self.LibraryBuildDirectoryList = []
-        self.SystemLibraryList = []
-        self.Macros = sdict()
-        self.Macros["OUTPUT_DIR"      ] = self._AutoGenObject.Macros["OUTPUT_DIR"]
-        self.Macros["DEBUG_DIR"       ] = self._AutoGenObject.Macros["DEBUG_DIR"]
-        self.Macros["MODULE_BUILD_DIR"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR"]
-        self.Macros["BIN_DIR"         ] = self._AutoGenObject.Macros["BIN_DIR"]
-        self.Macros["BUILD_DIR"       ] = self._AutoGenObject.Macros["BUILD_DIR"]
-        self.Macros["WORKSPACE"       ] = self._AutoGenObject.Macros["WORKSPACE"]
-
-    # Compose a dict object containing information used to do replacement in template
-    def _CreateTemplateDict(self):
-        if self._FileType not in self._SEP_:
-            EdkLogger.error("build", PARAMETER_INVALID, "Invalid Makefile type [%s]" % self._FileType,
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-        Separator = self._SEP_[self._FileType]
-
-        # break build if no source files and binary files are found
-        if len(self._AutoGenObject.SourceFileList) == 0 and len(self._AutoGenObject.BinaryFileList) == 0:
-            EdkLogger.error("build", AUTOGEN_ERROR, "No files to be built in module [%s, %s, %s]"
-                            % (self._AutoGenObject.BuildTarget, self._AutoGenObject.ToolChain, self._AutoGenObject.Arch),
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-
-        # convert dependent libraries to build command
-        self.ProcessDependentLibrary()
-        if len(self._AutoGenObject.Module.ModuleEntryPointList) > 0:
-            ModuleEntryPoint = self._AutoGenObject.Module.ModuleEntryPointList[0]
-        else:
-            ModuleEntryPoint = "_ModuleEntryPoint"
-
-        # Intel EBC compiler enforces EfiMain
-        if self._AutoGenObject.AutoGenVersion < 0x00010005 and self._AutoGenObject.Arch == "EBC":
-            ArchEntryPoint = "EfiMain"
-        else:
-            ArchEntryPoint = ModuleEntryPoint
-
-        if self._AutoGenObject.Arch == "EBC":
-            # EBC compiler always use "EfiStart" as entry point. Only applies to EdkII modules
-            ImageEntryPoint = "EfiStart"
-        elif self._AutoGenObject.AutoGenVersion < 0x00010005:
-            # Edk modules use entry point specified in INF file
-            ImageEntryPoint = ModuleEntryPoint
-        else:
-            # EdkII modules always use "_ModuleEntryPoint" as entry point
-            ImageEntryPoint = "_ModuleEntryPoint"
-
-        # tools definitions
-        ToolsDef = []
-        IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]
-        for Tool in self._AutoGenObject.BuildOption:
-            for Attr in self._AutoGenObject.BuildOption[Tool]:
-                Value = self._AutoGenObject.BuildOption[Tool][Attr]
-                if Attr == "FAMILY":
-                    continue
-                elif Attr == "PATH":
-                    ToolsDef.append("%s = %s" % (Tool, Value))
-                else:
-                    # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
-                    if Tool == "MAKE":
-                        continue
-                    # Remove duplicated include path, if any
-                    if Attr == "FLAGS":
-                        Value = RemoveDupOption(Value, IncPrefix, self._AutoGenObject.IncludePathList)
-                    ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))
-            ToolsDef.append("")
-
-        # convert source files and binary files to build targets
-        self.ResultFileList = [str(T.Target) for T in self._AutoGenObject.CodaTargetList]
-        if len(self.ResultFileList) == 0 and len(self._AutoGenObject.SourceFileList) <> 0:
-            EdkLogger.error("build", AUTOGEN_ERROR, "Nothing to build",
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-
-        self.ProcessBuildTargetList()
-
-        # Generate macros used to represent input files
-        FileMacroList = [] # macro name = file list
-        for FileListMacro in self.FileListMacros:
-            FileMacro = self._FILE_MACRO_TEMPLATE.Replace(
-                                                    {
-                                                        "macro_name"  : FileListMacro,
-                                                        "source_file" : self.FileListMacros[FileListMacro]
-                                                    }
-                                                    )
-            FileMacroList.append(FileMacro)
-
-        # INC_LIST is special
-        FileMacro = ""
-        IncludePathList = []
-        for P in  self._AutoGenObject.IncludePathList:
-            IncludePathList.append(IncPrefix+self.PlaceMacro(P, self.Macros))
-            if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:
-                self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix+P)
-        FileMacro += self._FILE_MACRO_TEMPLATE.Replace(
-                                                {
-                                                    "macro_name"   : "INC",
-                                                    "source_file" : IncludePathList
-                                                }
-                                                )
-        FileMacroList.append(FileMacro)
-
-        # Generate macros used to represent files containing list of input files
-        for ListFileMacro in self.ListFileMacros:
-            ListFileName = os.path.join(self._AutoGenObject.OutputDir, "%s.lst" % ListFileMacro.lower()[:len(ListFileMacro)-5])
-            FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))
-            SaveFileOnChange(
-                ListFileName,
-                "\n".join(self.ListFileMacros[ListFileMacro]),
-                False
-                )
-
-        # Edk modules need <BaseName>StrDefs.h for string ID
-        #if self._AutoGenObject.AutoGenVersion < 0x00010005 and len(self._AutoGenObject.UnicodeFileList) > 0:
-        #    BcTargetList = ['strdefs']
-        #else:
-        #    BcTargetList = []
-        BcTargetList = []
-
-        MakefileName = self._FILE_NAME_[self._FileType]
-        LibraryMakeCommandList = []
-        for D in self.LibraryBuildDirectoryList:
-            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}
-            LibraryMakeCommandList.append(Command)
-
-        MakefileTemplateDict = {
-            "makefile_header"           : self._FILE_HEADER_[self._FileType],
-            "makefile_path"             : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),
-            "makefile_name"             : MakefileName,
-            "platform_name"             : self.PlatformInfo.Name,
-            "platform_guid"             : self.PlatformInfo.Guid,
-            "platform_version"          : self.PlatformInfo.Version,
-            "platform_relative_directory": self.PlatformInfo.SourceDir,
-            "platform_output_directory" : self.PlatformInfo.OutputDir,
-
-            "module_name"               : self._AutoGenObject.Name,
-            "module_guid"               : self._AutoGenObject.Guid,
-            "module_version"            : self._AutoGenObject.Version,
-            "module_type"               : self._AutoGenObject.ModuleType,
-            "module_file"               : self._AutoGenObject.MetaFile.Name,
-            "module_file_base_name"     : self._AutoGenObject.MetaFile.BaseName,
-            "module_relative_directory" : self._AutoGenObject.SourceDir,
-            "module_extra_defines"      : ["%s = %s" % (k, v) for k,v in self._AutoGenObject.Module.Defines.iteritems()],
-
-            "architecture"              : self._AutoGenObject.Arch,
-            "toolchain_tag"             : self._AutoGenObject.ToolChain,
-            "build_target"              : self._AutoGenObject.BuildTarget,
-
-            "platform_build_directory"  : self.PlatformInfo.BuildDir,
-            "module_build_directory"    : self._AutoGenObject.BuildDir,
-            "module_output_directory"   : self._AutoGenObject.OutputDir,
-            "module_debug_directory"    : self._AutoGenObject.DebugDir,
-
-            "separator"                 : Separator,
-            "module_tool_definitions"   : ToolsDef,
-
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),
-
-            "module_entry_point"        : ModuleEntryPoint,
-            "image_entry_point"         : ImageEntryPoint,
-            "arch_entry_point"          : ArchEntryPoint,
-            "remaining_build_target"    : self.ResultFileList,
-            "common_dependency_file"    : self.CommonFileDependency,
-            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
-            "clean_command"             : self.GetRemoveDirectoryCommand(["$(OUTPUT_DIR)"]),
-            "cleanall_command"          : self.GetRemoveDirectoryCommand(["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]),
-            "dependent_library_build_directory" : self.LibraryBuildDirectoryList,
-            "library_build_command"     : LibraryMakeCommandList,
-            "file_macro"                : FileMacroList,
-            "file_build_target"         : self.BuildTargetList,
-            "backward_compatible_target": BcTargetList,
-        }
-
-        return MakefileTemplateDict
-
-    def ProcessBuildTargetList(self):
-        #
-        # Search dependency file list for each source file
-        #
-        ForceIncludedFile = []
-        for File in self._AutoGenObject.AutoGenFileList:
-            if File.Ext == '.h':
-                ForceIncludedFile.append(File)
-        SourceFileList = []
-        for Target in self._AutoGenObject.IntroTargetList:
-            SourceFileList.extend(Target.Inputs)
-
-        self.FileDependency = self.GetFileDependency(
-                                    SourceFileList,
-                                    ForceIncludedFile,
-                                    self._AutoGenObject.IncludePathList + self._AutoGenObject.BuildOptionIncPathList
-                                    )
-        DepSet = None
-        for File in self.FileDependency:
-            if not self.FileDependency[File]:
-                self.FileDependency[File] = ['$(FORCE_REBUILD)']
-                continue
-            # skip non-C files
-            if File.Ext not in [".c", ".C"] or File.Name == "AutoGen.c":
-                continue
-            elif DepSet == None:
-                DepSet = set(self.FileDependency[File])
-            else:
-                DepSet &= set(self.FileDependency[File])
-        # in case nothing in SourceFileList
-        if DepSet == None:
-            DepSet = set()
-        #
-        # Extract common files list in the dependency files
-        #
-        for File in DepSet:
-            self.CommonFileDependency.append(self.PlaceMacro(File.Path, self.Macros))
-
-        for File in self.FileDependency:
-            # skip non-C files
-            if File.Ext not in [".c", ".C"] or File.Name == "AutoGen.c":
-                continue
-            NewDepSet = set(self.FileDependency[File])
-            NewDepSet -= DepSet
-            self.FileDependency[File] = ["$(COMMON_DEPS)"] + list(NewDepSet)
-
-        # Convert target description object to target string in makefile
-        for Type in self._AutoGenObject.Targets:
-            for T in self._AutoGenObject.Targets[Type]:
-                # Generate related macros if needed
-                if T.GenFileListMacro and T.FileListMacro not in self.FileListMacros:
-                    self.FileListMacros[T.FileListMacro] = []
-                if T.GenListFile and T.ListFileMacro not in self.ListFileMacros:
-                    self.ListFileMacros[T.ListFileMacro] = []
-                if T.GenIncListFile and T.IncListFileMacro not in self.ListFileMacros:
-                    self.ListFileMacros[T.IncListFileMacro] = []
-
-                Deps = []
-                # Add force-dependencies
-                for Dep in T.Dependencies:
-                    Deps.append(self.PlaceMacro(str(Dep), self.Macros))
-                # Add inclusion-dependencies
-                if len(T.Inputs) == 1 and T.Inputs[0] in self.FileDependency:
-                    for F in self.FileDependency[T.Inputs[0]]:
-                        Deps.append(self.PlaceMacro(str(F), self.Macros))
-                # Add source-dependencies
-                for F in T.Inputs:
-                    NewFile = self.PlaceMacro(str(F), self.Macros)
-                    # In order to use file list macro as dependency
-                    if T.GenListFile:
-                        self.ListFileMacros[T.ListFileMacro].append(str(F))
-                        self.FileListMacros[T.FileListMacro].append(NewFile)
-                    elif T.GenFileListMacro:
-                        self.FileListMacros[T.FileListMacro].append(NewFile)
-                    else:
-                        Deps.append(NewFile)
-
-                # Use file list macro as dependency
-                if T.GenFileListMacro:
-                    Deps.append("$(%s)" % T.FileListMacro)
-
-                TargetDict = {
-                    "target"    :   self.PlaceMacro(T.Target.Path, self.Macros),
-                    "cmd"       :   "\n\t".join(T.Commands),
-                    "deps"      :   Deps
-                }
-                self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))
-
-    ## For creating makefile targets for dependent libraries
-    def ProcessDependentLibrary(self):
-        for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
-            self.LibraryBuildDirectoryList.append(self.PlaceMacro(LibraryAutoGen.BuildDir, self.Macros))
-
-    ## Return a list containing source file's dependencies
-    #
-    #   @param      FileList        The list of source files
-    #   @param      ForceInculeList The list of files which will be included forcely
-    #   @param      SearchPathList  The list of search path
-    #
-    #   @retval     dict            The mapping between source file path and its dependencies
-    #
-    def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):
-        Dependency = {}
-        for F in FileList:
-            Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)
-        return Dependency
-
-    ## Find dependencies for one source file
-    #
-    #  By searching recursively "#include" directive in file, find out all the
-    #  files needed by given source file. The dependecies will be only searched
-    #  in given search path list.
-    #
-    #   @param      File            The source file
-    #   @param      ForceInculeList The list of files which will be included forcely
-    #   @param      SearchPathList  The list of search path
-    #
-    #   @retval     list            The list of files the given source file depends on
-    #
-    def GetDependencyList(self, File, ForceList, SearchPathList):
-        EdkLogger.debug(EdkLogger.DEBUG_1, "Try to get dependency files for %s" % File)
-        FileStack = [File] + ForceList
-        DependencySet = set()
-        MacroUsedByIncludedFile = False
-
-        if self._AutoGenObject.Arch not in gDependencyDatabase:
-            gDependencyDatabase[self._AutoGenObject.Arch] = {}
-        DepDb = gDependencyDatabase[self._AutoGenObject.Arch]
-
-        # add path of given source file into search path list.
-        if File.Dir not in SearchPathList:
-            SearchPathList.append(File.Dir)
-        while len(FileStack) > 0:
-            F = FileStack.pop()
-
-            CurrentFileDependencyList = []
-            if F in DepDb:
-                CurrentFileDependencyList = DepDb[F]
-                for Dep in CurrentFileDependencyList:
-                    if Dep not in FileStack and Dep not in DependencySet:
-                        FileStack.append(Dep)
-            else:
-                try:
-                    Fd = open(F.Path, 'r')
-                except BaseException, X:
-                    EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path+"\n\t"+str(X))
-
-                FileContent = Fd.read()
-                Fd.close()
-                if len(FileContent) == 0:
-                    continue
-
-                if FileContent[0] == 0xff or FileContent[0] == 0xfe:
-                    FileContent = unicode(FileContent, "utf-16")
-                IncludedFileList = gIncludePattern.findall(FileContent)
-
-                CurrentFilePath = F.Dir
-                for Inc in IncludedFileList:
-                    Inc = Inc.strip()
-                    # if there's macro used to reference header file, expand it
-                    HeaderList = gMacroPattern.findall(Inc)
-                    if len(HeaderList) == 1 and len(HeaderList[0]) == 2:
-                        HeaderType = HeaderList[0][0]
-                        HeaderKey = HeaderList[0][1]
-                        if HeaderType in gIncludeMacroConversion:
-                            Inc = gIncludeMacroConversion[HeaderType] % {"HeaderKey" : HeaderKey}
-                        else:
-                            # not known macro used in #include
-                            MacroUsedByIncludedFile = True
-                            continue
-                    Inc = os.path.normpath(Inc)
-                    for SearchPath in [CurrentFilePath] + SearchPathList:
-                        FilePath = os.path.join(SearchPath, Inc)
-                        if not os.path.isfile(FilePath) or FilePath in CurrentFileDependencyList:
-                            continue
-                        FilePath = PathClass(FilePath)
-                        CurrentFileDependencyList.append(FilePath)
-                        if FilePath not in FileStack and FilePath not in DependencySet:
-                            FileStack.append(FilePath)
-                        break
-                    else:
-                        EdkLogger.debug(EdkLogger.DEBUG_9, "%s included by %s was not found "\
-                                        "in any given path:\n\t%s" % (Inc, F, "\n\t".join(SearchPathList)))
-
-                if not MacroUsedByIncludedFile:
-                    if F == File:
-                        CurrentFileDependencyList += ForceList
-                    #
-                    # Don't keep the file in cache if it uses macro in included file.
-                    # So it will be scanned again if another file includes this file.
-                    #
-                    DepDb[F] = CurrentFileDependencyList
-            DependencySet.update(CurrentFileDependencyList)
-
-        #
-        # If there's macro used in included file, always build the file by
-        # returning a empty dependency
-        #
-        if MacroUsedByIncludedFile:
-            DependencyList = []
-        else:
-            DependencyList = list(DependencySet)  # remove duplicate ones
-
-        return DependencyList
-
-    _TemplateDict = property(_CreateTemplateDict)
-
-## CustomMakefile class
-#
-#  This class encapsules makefie and its generation for module. It uses template to generate
-#  the content of makefile. The content of makefile will be got from ModuleAutoGen object.
-#
-class CustomMakefile(BuildFile):
-    ## template used to generate the makefile for module with custom makefile
-    _TEMPLATE_ = TemplateString('''\
-${makefile_header}
-
-#
-# Platform Macro Definition
-#
-PLATFORM_NAME = ${platform_name}
-PLATFORM_GUID = ${platform_guid}
-PLATFORM_VERSION = ${platform_version}
-PLATFORM_RELATIVE_DIR = ${platform_relative_directory}
-PLATFORM_DIR = $(WORKSPACE)${separator}${platform_relative_directory}
-PLATFORM_OUTPUT_DIR = ${platform_output_directory}
-
-#
-# Module Macro Definition
-#
-MODULE_NAME = ${module_name}
-MODULE_GUID = ${module_guid}
-MODULE_VERSION = ${module_version}
-MODULE_TYPE = ${module_type}
-MODULE_FILE = ${module_file}
-MODULE_FILE_BASE_NAME = ${module_file_base_name}
-BASE_NAME = $(MODULE_NAME)
-MODULE_RELATIVE_DIR = ${module_relative_directory}
-MODULE_DIR = $(WORKSPACE)${separator}${module_relative_directory}
-
-#
-# Build Configuration Macro Definition
-#
-ARCH = ${architecture}
-TOOLCHAIN = ${toolchain_tag}
-TOOLCHAIN_TAG = ${toolchain_tag}
-TARGET = ${build_target}
-
-#
-# Build Directory Macro Definition
-#
-# PLATFORM_BUILD_DIR = ${platform_build_directory}
-BUILD_DIR = ${platform_build_directory}
-BIN_DIR = $(BUILD_DIR)${separator}${architecture}
-LIB_DIR = $(BIN_DIR)
-MODULE_BUILD_DIR = ${module_build_directory}
-OUTPUT_DIR = ${module_output_directory}
-DEBUG_DIR = ${module_debug_directory}
-DEST_DIR_OUTPUT = $(OUTPUT_DIR)
-DEST_DIR_DEBUG = $(DEBUG_DIR)
-
-#
-# Tools definitions specific to this module
-#
-${BEGIN}${module_tool_definitions}
-${END}
-MAKE_FILE = ${makefile_path}
-
-#
-# Shell Command Macro
-#
-${BEGIN}${shell_command_code} = ${shell_command}
-${END}
-
-${custom_makefile_content}
-
-#
-# Target used when called from platform makefile, which will bypass the build of dependent libraries
-#
-
-pbuild: init all
-
-
-#
-# ModuleTarget
-#
-
-mbuild: init all
-
-#
-# Build Target used in multi-thread build mode, which no init target is needed
-#
-
-tbuild: all
-
-#
-# Initialization target: print build information and create necessary directories
-#
-init:
-\t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]
-${BEGIN}\t-@${create_directory_command}\n${END}\
-
-''')
-
-    ## Constructor of CustomMakefile
-    #
-    #   @param  ModuleAutoGen   Object of ModuleAutoGen class
-    #
-    def __init__(self, ModuleAutoGen):
-        BuildFile.__init__(self, ModuleAutoGen)
-        self.PlatformInfo = self._AutoGenObject.PlatformInfo
-        self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]
-
-    # Compose a dict object containing information used to do replacement in template
-    def _CreateTemplateDict(self):
-        Separator = self._SEP_[self._FileType]
-        if self._FileType not in self._AutoGenObject.CustomMakefile:
-            EdkLogger.error('build', OPTION_NOT_SUPPORTED, "No custom makefile for %s" % self._FileType,
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-        MakefilePath = os.path.join(
-                                self._AutoGenObject.WorkspaceDir,
-                                self._AutoGenObject.CustomMakefile[self._FileType]
-                                )
-        try:
-            CustomMakefile = open(MakefilePath, 'r').read()
-        except:
-            EdkLogger.error('build', FILE_OPEN_FAILURE, File=str(self._AutoGenObject),
-                            ExtraData=self._AutoGenObject.CustomMakefile[self._FileType])
-
-        # tools definitions
-        ToolsDef = []
-        for Tool in self._AutoGenObject.BuildOption:
-            # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.
-            if Tool == "MAKE":
-                continue
-            for Attr in self._AutoGenObject.BuildOption[Tool]:
-                if Attr == "FAMILY":
-                    continue
-                elif Attr == "PATH":
-                    ToolsDef.append("%s = %s" % (Tool, self._AutoGenObject.BuildOption[Tool][Attr]))
-                else:
-                    ToolsDef.append("%s_%s = %s" % (Tool, Attr, self._AutoGenObject.BuildOption[Tool][Attr]))
-            ToolsDef.append("")
-
-        MakefileName = self._FILE_NAME_[self._FileType]
-        MakefileTemplateDict = {
-            "makefile_header"           : self._FILE_HEADER_[self._FileType],
-            "makefile_path"             : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),
-            "platform_name"             : self.PlatformInfo.Name,
-            "platform_guid"             : self.PlatformInfo.Guid,
-            "platform_version"          : self.PlatformInfo.Version,
-            "platform_relative_directory": self.PlatformInfo.SourceDir,
-            "platform_output_directory" : self.PlatformInfo.OutputDir,
-
-            "module_name"               : self._AutoGenObject.Name,
-            "module_guid"               : self._AutoGenObject.Guid,
-            "module_version"            : self._AutoGenObject.Version,
-            "module_type"               : self._AutoGenObject.ModuleType,
-            "module_file"               : self._AutoGenObject.MetaFile,
-            "module_file_base_name"     : self._AutoGenObject.MetaFile.BaseName,
-            "module_relative_directory" : self._AutoGenObject.SourceDir,
-
-            "architecture"              : self._AutoGenObject.Arch,
-            "toolchain_tag"             : self._AutoGenObject.ToolChain,
-            "build_target"              : self._AutoGenObject.BuildTarget,
-
-            "platform_build_directory"  : self.PlatformInfo.BuildDir,
-            "module_build_directory"    : self._AutoGenObject.BuildDir,
-            "module_output_directory"   : self._AutoGenObject.OutputDir,
-            "module_debug_directory"    : self._AutoGenObject.DebugDir,
-
-            "separator"                 : Separator,
-            "module_tool_definitions"   : ToolsDef,
-
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),
-
-            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
-            "custom_makefile_content"   : CustomMakefile
-        }
-
-        return MakefileTemplateDict
-
-    _TemplateDict = property(_CreateTemplateDict)
-
-## PlatformMakefile class
-#
-#  This class encapsules makefie and its generation for platform. It uses
-# template to generate the content of makefile. The content of makefile will be
-# got from PlatformAutoGen object.
-#
-class PlatformMakefile(BuildFile):
-    ## template used to generate the makefile for platform
-    _TEMPLATE_ = TemplateString('''\
-${makefile_header}
-
-#
-# Platform Macro Definition
-#
-PLATFORM_NAME = ${platform_name}
-PLATFORM_GUID = ${platform_guid}
-PLATFORM_VERSION = ${platform_version}
-PLATFORM_FILE = ${platform_file}
-PLATFORM_DIR = $(WORKSPACE)${separator}${platform_relative_directory}
-PLATFORM_OUTPUT_DIR = ${platform_output_directory}
-
-#
-# Build Configuration Macro Definition
-#
-TOOLCHAIN = ${toolchain_tag}
-TOOLCHAIN_TAG = ${toolchain_tag}
-TARGET = ${build_target}
-
-#
-# Build Directory Macro Definition
-#
-BUILD_DIR = ${platform_build_directory}
-FV_DIR = ${platform_build_directory}${separator}FV
-
-#
-# Shell Command Macro
-#
-${BEGIN}${shell_command_code} = ${shell_command}
-${END}
-
-MAKE = ${make_path}
-MAKE_FILE = ${makefile_path}
-
-#
-# Default target
-#
-all: init build_libraries build_modules
-
-#
-# Initialization target: print build information and create necessary directories
-#
-init:
-\t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]
-\t${BEGIN}-@${create_directory_command}
-\t${END}
-#
-# library build target
-#
-libraries: init build_libraries
-
-#
-# module build target
-#
-modules: init build_libraries build_modules
-
-#
-# Build all libraries:
-#
-build_libraries:
-${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild
-${END}\t@cd $(BUILD_DIR)
-
-#
-# Build all modules:
-#
-build_modules:
-${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild
-${END}\t@cd $(BUILD_DIR)
-
-#
-# Clean intermediate files
-#
-clean:
-\t${BEGIN}-@${library_build_command} clean
-\t${END}${BEGIN}-@${module_build_command} clean
-\t${END}@cd $(BUILD_DIR)
-
-#
-# Clean all generated files except to makefile
-#
-cleanall:
-${BEGIN}\t${cleanall_command}
-${END}
-
-#
-# Clean all library files
-#
-cleanlib:
-\t${BEGIN}-@${library_build_command} cleanall
-\t${END}@cd $(BUILD_DIR)\n
-''')
-
-    ## Constructor of PlatformMakefile
-    #
-    #   @param  ModuleAutoGen   Object of PlatformAutoGen class
-    #
-    def __init__(self, PlatformAutoGen):
-        BuildFile.__init__(self, PlatformAutoGen)
-        self.ModuleBuildCommandList = []
-        self.ModuleMakefileList = []
-        self.IntermediateDirectoryList = []
-        self.ModuleBuildDirectoryList = []
-        self.LibraryBuildDirectoryList = []
-
-    # Compose a dict object containing information used to do replacement in template
-    def _CreateTemplateDict(self):
-        Separator = self._SEP_[self._FileType]
-
-        PlatformInfo = self._AutoGenObject
-        if "MAKE" not in PlatformInfo.ToolDefinition or "PATH" not in PlatformInfo.ToolDefinition["MAKE"]:
-            EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-
-        self.IntermediateDirectoryList = ["$(BUILD_DIR)"]
-        self.ModuleBuildDirectoryList = self.GetModuleBuildDirectoryList()
-        self.LibraryBuildDirectoryList = self.GetLibraryBuildDirectoryList()
-
-        MakefileName = self._FILE_NAME_[self._FileType]
-        LibraryMakefileList = []
-        LibraryMakeCommandList = []
-        for D in self.LibraryBuildDirectoryList:
-            D = self.PlaceMacro(D, {"BUILD_DIR":PlatformInfo.BuildDir})
-            Makefile = os.path.join(D, MakefileName)
-            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
-            LibraryMakefileList.append(Makefile)
-            LibraryMakeCommandList.append(Command)
-
-        ModuleMakefileList = []
-        ModuleMakeCommandList = []
-        for D in self.ModuleBuildDirectoryList:
-            D = self.PlaceMacro(D, {"BUILD_DIR":PlatformInfo.BuildDir})
-            Makefile = os.path.join(D, MakefileName)
-            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
-            ModuleMakefileList.append(Makefile)
-            ModuleMakeCommandList.append(Command)
-
-        MakefileTemplateDict = {
-            "makefile_header"           : self._FILE_HEADER_[self._FileType],
-            "makefile_path"             : os.path.join("$(BUILD_DIR)", MakefileName),
-            "make_path"                 : PlatformInfo.ToolDefinition["MAKE"]["PATH"],
-            "makefile_name"             : MakefileName,
-            "platform_name"             : PlatformInfo.Name,
-            "platform_guid"             : PlatformInfo.Guid,
-            "platform_version"          : PlatformInfo.Version,
-            "platform_file"             : self._AutoGenObject.MetaFile,
-            "platform_relative_directory": PlatformInfo.SourceDir,
-            "platform_output_directory" : PlatformInfo.OutputDir,
-            "platform_build_directory"  : PlatformInfo.BuildDir,
-
-            "toolchain_tag"             : PlatformInfo.ToolChain,
-            "build_target"              : PlatformInfo.BuildTarget,
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),
-            "build_architecture_list"   : self._AutoGenObject.Arch,
-            "architecture"              : self._AutoGenObject.Arch,
-            "separator"                 : Separator,
-            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
-            "cleanall_command"          : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),
-            "library_makefile_list"     : LibraryMakefileList,
-            "module_makefile_list"      : ModuleMakefileList,
-            "library_build_command"     : LibraryMakeCommandList,
-            "module_build_command"      : ModuleMakeCommandList,
-        }
-
-        return MakefileTemplateDict
-
-    ## Get the root directory list for intermediate files of all modules build
-    #
-    #   @retval     list    The list of directory
-    #
-    def GetModuleBuildDirectoryList(self):
-        DirList = []
-        for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:
-            DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))
-        return DirList
-
-    ## Get the root directory list for intermediate files of all libraries build
-    #
-    #   @retval     list    The list of directory
-    #
-    def GetLibraryBuildDirectoryList(self):
-        DirList = []
-        for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
-            DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))
-        return DirList
-
-    _TemplateDict = property(_CreateTemplateDict)
-
-## TopLevelMakefile class
-#
-#  This class encapsules makefie and its generation for entrance makefile. It
-# uses template to generate the content of makefile. The content of makefile
-# will be got from WorkspaceAutoGen object.
-#
-class TopLevelMakefile(BuildFile):
-    ## template used to generate toplevel makefile
-    _TEMPLATE_ = TemplateString('''\
-${makefile_header}
-
-#
-# Platform Macro Definition
-#
-PLATFORM_NAME = ${platform_name}
-PLATFORM_GUID = ${platform_guid}
-PLATFORM_VERSION = ${platform_version}
-
-#
-# Build Configuration Macro Definition
-#
-TOOLCHAIN = ${toolchain_tag}
-TOOLCHAIN_TAG = ${toolchain_tag}
-TARGET = ${build_target}
-
-#
-# Build Directory Macro Definition
-#
-BUILD_DIR = ${platform_build_directory}
-FV_DIR = ${platform_build_directory}${separator}FV
-
-#
-# Shell Command Macro
-#
-${BEGIN}${shell_command_code} = ${shell_command}
-${END}
-
-MAKE = ${make_path}
-MAKE_FILE = ${makefile_path}
-
-#
-# Default target
-#
-all: modules fds
-
-#
-# Initialization target: print build information and create necessary directories
-#
-init:
-\t-@
-\t${BEGIN}-@${create_directory_command}
-\t${END}
-#
-# library build target
-#
-libraries: init
-${BEGIN}\t@cd $(BUILD_DIR)${separator}${arch} && "$(MAKE)" $(MAKE_FLAGS) libraries
-${END}\t@cd $(BUILD_DIR)
-
-#
-# module build target
-#
-modules: init
-${BEGIN}\t@cd $(BUILD_DIR)${separator}${arch} && "$(MAKE)" $(MAKE_FLAGS) modules
-${END}\t@cd $(BUILD_DIR)
-
-#
-# Flash Device Image Target
-#
-fds: init
-\t-@cd $(FV_DIR)
-${BEGIN}\tGenFds -f ${fdf_file} -o $(BUILD_DIR) -t $(TOOLCHAIN) -b $(TARGET) -p ${active_platform} -a ${build_architecture_list}${END}${BEGIN}${extra_options}${END}${BEGIN} -r ${fd}${END}${BEGIN} -i ${fv}${END}${BEGIN} -C ${cap}${END}${BEGIN} -D${macro}${END}
-
-#
-# run command for emulator platform only
-#
-run:
-\tcd $(BUILD_DIR)${separator}IA32 && ".${separator}SecMain"
-\tcd $(BUILD_DIR)
-
-#
-# Clean intermediate files
-#
-clean:
-${BEGIN}\t-@${sub_build_command} clean
-${END}\t@cd $(BUILD_DIR)
-
-#
-# Clean all generated files except to makefile
-#
-cleanall:
-${BEGIN}\t${cleanall_command}
-${END}
-
-#
-# Clean all library files
-#
-cleanlib:
-${BEGIN}\t-@${sub_build_command} cleanlib
-${END}\t@cd $(BUILD_DIR)\n
-''')
-
-    ## Constructor of TopLevelMakefile
-    #
-    #   @param  Workspace   Object of WorkspaceAutoGen class
-    #
-    def __init__(self, Workspace):
-        BuildFile.__init__(self, Workspace)
-        self.IntermediateDirectoryList = []
-
-    # Compose a dict object containing information used to do replacement in template
-    def _CreateTemplateDict(self):
-        Separator = self._SEP_[self._FileType]
-
-        # any platform autogen object is ok because we just need common information
-        PlatformInfo = self._AutoGenObject
-
-        if "MAKE" not in PlatformInfo.ToolDefinition or "PATH" not in PlatformInfo.ToolDefinition["MAKE"]:
-            EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",
-                            ExtraData="[%s]" % str(self._AutoGenObject))
-
-        for Arch in PlatformInfo.ArchList:
-            self.IntermediateDirectoryList.append(Separator.join(["$(BUILD_DIR)", Arch]))
-        self.IntermediateDirectoryList.append("$(FV_DIR)")
-
-        # TRICK: for not generating GenFds call in makefile if no FDF file
-        MacroList = []
-        if PlatformInfo.FdfFile != None and PlatformInfo.FdfFile != "":
-            FdfFileList = [PlatformInfo.FdfFile]
-            # macros passed to GenFds
-            # MacroList.append('"%s=%s"' % ("WORKSPACE", GlobalData.gWorkspace))
-            MacroList.append('"%s=%s"' % ("EFI_SOURCE", GlobalData.gEfiSource))
-            MacroList.append('"%s=%s"' % ("EDK_SOURCE", GlobalData.gEdkSource))
-            for MacroName in GlobalData.gGlobalDefines:
-                if GlobalData.gGlobalDefines[MacroName] != "":
-                    MacroList.append('"%s=%s"' % (MacroName, GlobalData.gGlobalDefines[MacroName]))
-                else:
-                    MacroList.append('"%s"' % MacroName)
-        else:
-            FdfFileList = []
-
-        # pass extra common options to external program called in makefile, currently GenFds.exe
-        ExtraOption = ''
-        LogLevel = EdkLogger.GetLevel()
-        if LogLevel == EdkLogger.VERBOSE:
-            ExtraOption += " -v"
-        elif LogLevel <= EdkLogger.DEBUG_9:
-            ExtraOption += " -d %d" % (LogLevel - 1)
-        elif LogLevel == EdkLogger.QUIET:
-            ExtraOption += " -q"
-
-        if GlobalData.gCaseInsensitive:
-            ExtraOption += " -c"
-        ExtraOptionList = []
-        if ExtraOption:
-            ExtraOptionList.append(ExtraOption)
-
-        MakefileName = self._FILE_NAME_[self._FileType]
-        SubBuildCommandList = []
-        for A in PlatformInfo.ArchList:
-            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join("$(BUILD_DIR)", A, MakefileName)}
-            SubBuildCommandList.append(Command)
-
-        MakefileTemplateDict = {
-            "makefile_header"           : self._FILE_HEADER_[self._FileType],
-            "makefile_path"             : os.path.join("$(BUILD_DIR)", MakefileName),
-            "make_path"                 : PlatformInfo.ToolDefinition["MAKE"]["PATH"],
-            "platform_name"             : PlatformInfo.Name,
-            "platform_guid"             : PlatformInfo.Guid,
-            "platform_version"          : PlatformInfo.Version,
-            "platform_build_directory"  : PlatformInfo.BuildDir,
-
-            "toolchain_tag"             : PlatformInfo.ToolChain,
-            "build_target"              : PlatformInfo.BuildTarget,
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),
-            'arch'                      : list(PlatformInfo.ArchList),
-            "build_architecture_list"   : ','.join(PlatformInfo.ArchList),
-            "separator"                 : Separator,
-            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),
-            "cleanall_command"          : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),
-            "sub_build_command"         : SubBuildCommandList,
-            "fdf_file"                  : FdfFileList,
-            "active_platform"           : str(PlatformInfo),
-            "fd"                        : PlatformInfo.FdTargetList,
-            "fv"                        : PlatformInfo.FvTargetList,
-            "cap"                       : PlatformInfo.CapTargetList,
-            "extra_options"             : ExtraOptionList,
-            "macro"                     : MacroList,
-        }
-
-        return MakefileTemplateDict
-
-    ## Get the root directory list for intermediate files of all modules build
-    #
-    #   @retval     list    The list of directory
-    #
-    def GetModuleBuildDirectoryList(self):
-        DirList = []
-        for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:
-            DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))
-        return DirList
-
-    ## Get the root directory list for intermediate files of all libraries build
-    #
-    #   @retval     list    The list of directory
-    #
-    def GetLibraryBuildDirectoryList(self):
-        DirList = []
-        for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:
-            DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))
-        return DirList
-
-    _TemplateDict = property(_CreateTemplateDict)
-
-# This acts like the main() function for the script, unless it is 'import'ed into another script.
-if __name__ == '__main__':
-    pass
-
+## @file\r
+# Create makefile for MS nmake and GNU make\r
+#\r
+# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+\r
+## Import Modules\r
+#\r
+from __future__ import absolute_import\r
+import Common.LongFilePathOs as os\r
+import sys\r
+import string\r
+import re\r
+import os.path as path\r
+from Common.LongFilePathSupport import OpenLongFilePath as open\r
+from Common.MultipleWorkspace import MultipleWorkspace as mws\r
+from Common.BuildToolError import *\r
+from Common.Misc import *\r
+from Common.StringUtils import *\r
+from .BuildEngine import *\r
+import Common.GlobalData as GlobalData\r
+from collections import OrderedDict\r
+from Common.DataType import TAB_COMPILER_MSFT\r
+\r
+## Regular expression for finding header file inclusions\r
+gIncludePattern = re.compile(r"^[ \t]*[#%]?[ \t]*include(?:[ \t]*(?:\\(?:\r\n|\r|\n))*[ \t]*)*(?:\(?[\"<]?[ \t]*)([-\w.\\/() \t]+)(?:[ \t]*[\">]?\)?)", re.MULTILINE | re.UNICODE | re.IGNORECASE)\r
+\r
+## Regular expression for matching macro used in header file inclusion\r
+gMacroPattern = re.compile("([_A-Z][_A-Z0-9]*)[ \t]*\((.+)\)", re.UNICODE)\r
+\r
+gIsFileMap = {}\r
+\r
+## pattern for include style in Edk.x code\r
+gProtocolDefinition = "Protocol/%(HeaderKey)s/%(HeaderKey)s.h"\r
+gGuidDefinition = "Guid/%(HeaderKey)s/%(HeaderKey)s.h"\r
+gArchProtocolDefinition = "ArchProtocol/%(HeaderKey)s/%(HeaderKey)s.h"\r
+gPpiDefinition = "Ppi/%(HeaderKey)s/%(HeaderKey)s.h"\r
+gIncludeMacroConversion = {\r
+  "EFI_PROTOCOL_DEFINITION"         :   gProtocolDefinition,\r
+  "EFI_GUID_DEFINITION"             :   gGuidDefinition,\r
+  "EFI_ARCH_PROTOCOL_DEFINITION"    :   gArchProtocolDefinition,\r
+  "EFI_PROTOCOL_PRODUCER"           :   gProtocolDefinition,\r
+  "EFI_PROTOCOL_CONSUMER"           :   gProtocolDefinition,\r
+  "EFI_PROTOCOL_DEPENDENCY"         :   gProtocolDefinition,\r
+  "EFI_ARCH_PROTOCOL_PRODUCER"      :   gArchProtocolDefinition,\r
+  "EFI_ARCH_PROTOCOL_CONSUMER"      :   gArchProtocolDefinition,\r
+  "EFI_ARCH_PROTOCOL_DEPENDENCY"    :   gArchProtocolDefinition,\r
+  "EFI_PPI_DEFINITION"              :   gPpiDefinition,\r
+  "EFI_PPI_PRODUCER"                :   gPpiDefinition,\r
+  "EFI_PPI_CONSUMER"                :   gPpiDefinition,\r
+  "EFI_PPI_DEPENDENCY"              :   gPpiDefinition,\r
+}\r
+\r
+## default makefile type\r
+gMakeType = ""\r
+if sys.platform == "win32":\r
+    gMakeType = "nmake"\r
+else:\r
+    gMakeType = "gmake"\r
+\r
+\r
+## BuildFile class\r
+#\r
+#  This base class encapsules build file and its generation. It uses template to generate\r
+#  the content of build file. The content of build file will be got from AutoGen objects.\r
+#\r
+class BuildFile(object):\r
+    ## template used to generate the build file (i.e. makefile if using make)\r
+    _TEMPLATE_ = TemplateString('')\r
+\r
+    _DEFAULT_FILE_NAME_ = "Makefile"\r
+\r
+    ## default file name for each type of build file\r
+    _FILE_NAME_ = {\r
+        "nmake" :   "Makefile",\r
+        "gmake" :   "GNUmakefile"\r
+    }\r
+\r
+    ## Fixed header string for makefile\r
+    _MAKEFILE_HEADER = '''#\r
+# DO NOT EDIT\r
+# This file is auto-generated by build utility\r
+#\r
+# Module Name:\r
+#\r
+#   %s\r
+#\r
+# Abstract:\r
+#\r
+#   Auto-generated makefile for building modules, libraries or platform\r
+#\r
+    '''\r
+\r
+    ## Header string for each type of build file\r
+    _FILE_HEADER_ = {\r
+        "nmake" :   _MAKEFILE_HEADER % _FILE_NAME_["nmake"],\r
+        "gmake" :   _MAKEFILE_HEADER % _FILE_NAME_["gmake"]\r
+    }\r
+\r
+    ## shell commands which can be used in build file in the form of macro\r
+    #   $(CP)     copy file command\r
+    #   $(MV)     move file command\r
+    #   $(RM)     remove file command\r
+    #   $(MD)     create dir command\r
+    #   $(RD)     remove dir command\r
+    #\r
+    _SHELL_CMD_ = {\r
+        "nmake" : {\r
+            "CP"    :   "copy /y",\r
+            "MV"    :   "move /y",\r
+            "RM"    :   "del /f /q",\r
+            "MD"    :   "mkdir",\r
+            "RD"    :   "rmdir /s /q",\r
+        },\r
+\r
+        "gmake" : {\r
+            "CP"    :   "cp -f",\r
+            "MV"    :   "mv -f",\r
+            "RM"    :   "rm -f",\r
+            "MD"    :   "mkdir -p",\r
+            "RD"    :   "rm -r -f",\r
+        }\r
+    }\r
+\r
+    ## directory separator\r
+    _SEP_ = {\r
+        "nmake" :   "\\",\r
+        "gmake" :   "/"\r
+    }\r
+\r
+    ## directory creation template\r
+    _MD_TEMPLATE_ = {\r
+        "nmake" :   'if not exist %(dir)s $(MD) %(dir)s',\r
+        "gmake" :   "$(MD) %(dir)s"\r
+    }\r
+\r
+    ## directory removal template\r
+    _RD_TEMPLATE_ = {\r
+        "nmake" :   'if exist %(dir)s $(RD) %(dir)s',\r
+        "gmake" :   "$(RD) %(dir)s"\r
+    }\r
+    ## cp if exist\r
+    _CP_TEMPLATE_ = {\r
+        "nmake" :   'if exist %(Src)s $(CP) %(Src)s %(Dst)s',\r
+        "gmake" :   "test -f %(Src)s && $(CP) %(Src)s %(Dst)s"\r
+    }\r
+\r
+    _CD_TEMPLATE_ = {\r
+        "nmake" :   'if exist %(dir)s cd %(dir)s',\r
+        "gmake" :   "test -e %(dir)s && cd %(dir)s"\r
+    }\r
+\r
+    _MAKE_TEMPLATE_ = {\r
+        "nmake" :   'if exist %(file)s "$(MAKE)" $(MAKE_FLAGS) -f %(file)s',\r
+        "gmake" :   'test -e %(file)s && "$(MAKE)" $(MAKE_FLAGS) -f %(file)s'\r
+    }\r
+\r
+    _INCLUDE_CMD_ = {\r
+        "nmake" :   '!INCLUDE',\r
+        "gmake" :   "include"\r
+    }\r
+\r
+    _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I", "GCC" : "-I", "INTEL" : "-I", "RVCT" : "-I", "NASM" : "-I"}\r
+\r
+    ## Constructor of BuildFile\r
+    #\r
+    #   @param  AutoGenObject   Object of AutoGen class\r
+    #\r
+    def __init__(self, AutoGenObject):\r
+        self._AutoGenObject = AutoGenObject\r
+        self._FileType = gMakeType\r
+\r
+    ## Create build file\r
+    #\r
+    #   @param  FileType    Type of build file. Only nmake and gmake are supported now.\r
+    #\r
+    #   @retval TRUE        The build file is created or re-created successfully\r
+    #   @retval FALSE       The build file exists and is the same as the one to be generated\r
+    #\r
+    def Generate(self, FileType=gMakeType):\r
+        if FileType not in self._FILE_NAME_:\r
+            EdkLogger.error("build", PARAMETER_INVALID, "Invalid build type [%s]" % FileType,\r
+                            ExtraData="[%s]" % str(self._AutoGenObject))\r
+        self._FileType = FileType\r
+        FileContent = self._TEMPLATE_.Replace(self._TemplateDict)\r
+        FileName = self._FILE_NAME_[FileType]\r
+        return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)\r
+\r
+    ## Return a list of directory creation command string\r
+    #\r
+    #   @param      DirList     The list of directory to be created\r
+    #\r
+    #   @retval     list        The directory creation command list\r
+    #\r
+    def GetCreateDirectoryCommand(self, DirList):\r
+        return [self._MD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]\r
+\r
+    ## Return a list of directory removal command string\r
+    #\r
+    #   @param      DirList     The list of directory to be removed\r
+    #\r
+    #   @retval     list        The directory removal command list\r
+    #\r
+    def GetRemoveDirectoryCommand(self, DirList):\r
+        return [self._RD_TEMPLATE_[self._FileType] % {'dir':Dir} for Dir in DirList]\r
+\r
+    def PlaceMacro(self, Path, MacroDefinitions={}):\r
+        if Path.startswith("$("):\r
+            return Path\r
+        else:\r
+            PathLength = len(Path)\r
+            for MacroName in MacroDefinitions:\r
+                MacroValue = MacroDefinitions[MacroName]\r
+                MacroValueLength = len(MacroValue)\r
+                if MacroValueLength == 0:\r
+                    continue\r
+                if MacroValueLength <= PathLength and Path.startswith(MacroValue):\r
+                    Path = "$(%s)%s" % (MacroName, Path[MacroValueLength:])\r
+                    break\r
+            return Path\r
+\r
+## ModuleMakefile class\r
+#\r
+#  This class encapsules makefie and its generation for module. It uses template to generate\r
+#  the content of makefile. The content of makefile will be got from ModuleAutoGen object.\r
+#\r
+class ModuleMakefile(BuildFile):\r
+    ## template used to generate the makefile for module\r
+    _TEMPLATE_ = TemplateString('''\\r
+${makefile_header}\r
+\r
+#\r
+# Platform Macro Definition\r
+#\r
+PLATFORM_NAME = ${platform_name}\r
+PLATFORM_GUID = ${platform_guid}\r
+PLATFORM_VERSION = ${platform_version}\r
+PLATFORM_RELATIVE_DIR = ${platform_relative_directory}\r
+PLATFORM_DIR = ${platform_dir}\r
+PLATFORM_OUTPUT_DIR = ${platform_output_directory}\r
+\r
+#\r
+# Module Macro Definition\r
+#\r
+MODULE_NAME = ${module_name}\r
+MODULE_GUID = ${module_guid}\r
+MODULE_NAME_GUID = ${module_name_guid}\r
+MODULE_VERSION = ${module_version}\r
+MODULE_TYPE = ${module_type}\r
+MODULE_FILE = ${module_file}\r
+MODULE_FILE_BASE_NAME = ${module_file_base_name}\r
+BASE_NAME = $(MODULE_NAME)\r
+MODULE_RELATIVE_DIR = ${module_relative_directory}\r
+PACKAGE_RELATIVE_DIR = ${package_relative_directory}\r
+MODULE_DIR = ${module_dir}\r
+FFS_OUTPUT_DIR = ${ffs_output_directory}\r
+\r
+MODULE_ENTRY_POINT = ${module_entry_point}\r
+ARCH_ENTRY_POINT = ${arch_entry_point}\r
+IMAGE_ENTRY_POINT = ${image_entry_point}\r
+\r
+${BEGIN}${module_extra_defines}\r
+${END}\r
+#\r
+# Build Configuration Macro Definition\r
+#\r
+ARCH = ${architecture}\r
+TOOLCHAIN = ${toolchain_tag}\r
+TOOLCHAIN_TAG = ${toolchain_tag}\r
+TARGET = ${build_target}\r
+\r
+#\r
+# Build Directory Macro Definition\r
+#\r
+# PLATFORM_BUILD_DIR = ${platform_build_directory}\r
+BUILD_DIR = ${platform_build_directory}\r
+BIN_DIR = $(BUILD_DIR)${separator}${architecture}\r
+LIB_DIR = $(BIN_DIR)\r
+MODULE_BUILD_DIR = ${module_build_directory}\r
+OUTPUT_DIR = ${module_output_directory}\r
+DEBUG_DIR = ${module_debug_directory}\r
+DEST_DIR_OUTPUT = $(OUTPUT_DIR)\r
+DEST_DIR_DEBUG = $(DEBUG_DIR)\r
+\r
+#\r
+# Shell Command Macro\r
+#\r
+${BEGIN}${shell_command_code} = ${shell_command}\r
+${END}\r
+\r
+#\r
+# Tools definitions specific to this module\r
+#\r
+${BEGIN}${module_tool_definitions}\r
+${END}\r
+MAKE_FILE = ${makefile_path}\r
+\r
+#\r
+# Build Macro\r
+#\r
+${BEGIN}${file_macro}\r
+${END}\r
+\r
+COMMON_DEPS = ${BEGIN}${common_dependency_file} \\\r
+              ${END}\r
+\r
+#\r
+# Overridable Target Macro Definitions\r
+#\r
+FORCE_REBUILD = force_build\r
+INIT_TARGET = init\r
+PCH_TARGET =\r
+BC_TARGET = ${BEGIN}${backward_compatible_target} ${END}\r
+CODA_TARGET = ${BEGIN}${remaining_build_target} \\\r
+              ${END}\r
+\r
+#\r
+# Default target, which will build dependent libraries in addition to source files\r
+#\r
+\r
+all: mbuild\r
+\r
+\r
+#\r
+# Target used when called from platform makefile, which will bypass the build of dependent libraries\r
+#\r
+\r
+pbuild: $(INIT_TARGET) $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)\r
+\r
+#\r
+# ModuleTarget\r
+#\r
+\r
+mbuild: $(INIT_TARGET) $(BC_TARGET) gen_libs $(PCH_TARGET) $(CODA_TARGET)\r
+\r
+#\r
+# Build Target used in multi-thread build mode, which will bypass the init and gen_libs targets\r
+#\r
+\r
+tbuild: $(BC_TARGET) $(PCH_TARGET) $(CODA_TARGET)\r
+\r
+#\r
+# Phony target which is used to force executing commands for a target\r
+#\r
+force_build:\r
+\t-@\r
+\r
+#\r
+# Target to update the FD\r
+#\r
+\r
+fds: mbuild gen_fds\r
+\r
+#\r
+# Initialization target: print build information and create necessary directories\r
+#\r
+init: info dirs\r
+\r
+info:\r
+\t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]\r
+\r
+dirs:\r
+${BEGIN}\t-@${create_directory_command}\n${END}\r
+\r
+strdefs:\r
+\t-@$(CP) $(DEBUG_DIR)${separator}AutoGen.h $(DEBUG_DIR)${separator}$(MODULE_NAME)StrDefs.h\r
+\r
+#\r
+# GenLibsTarget\r
+#\r
+gen_libs:\r
+\t${BEGIN}@"$(MAKE)" $(MAKE_FLAGS) -f ${dependent_library_build_directory}${separator}${makefile_name}\r
+\t${END}@cd $(MODULE_BUILD_DIR)\r
+\r
+#\r
+# Build Flash Device Image\r
+#\r
+gen_fds:\r
+\t@"$(MAKE)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds\r
+\t@cd $(MODULE_BUILD_DIR)\r
+\r
+#\r
+# Individual Object Build Targets\r
+#\r
+${BEGIN}${file_build_target}\r
+${END}\r
+\r
+#\r
+# clean all intermediate files\r
+#\r
+clean:\r
+\t${BEGIN}${clean_command}\r
+\t${END}\t$(RM) AutoGenTimeStamp\r
+\r
+#\r
+# clean all generated files\r
+#\r
+cleanall:\r
+${BEGIN}\t${cleanall_command}\r
+${END}\t$(RM) *.pdb *.idb > NUL 2>&1\r
+\t$(RM) $(BIN_DIR)${separator}$(MODULE_NAME).efi\r
+\t$(RM) AutoGenTimeStamp\r
+\r
+#\r
+# clean all dependent libraries built\r
+#\r
+cleanlib:\r
+\t${BEGIN}-@${library_build_command} cleanall\r
+\t${END}@cd $(MODULE_BUILD_DIR)\n\n''')\r
+\r
+    _FILE_MACRO_TEMPLATE = TemplateString("${macro_name} = ${BEGIN} \\\n    ${source_file}${END}\n")\r
+    _BUILD_TARGET_TEMPLATE = TemplateString("${BEGIN}${target} : ${deps}\n${END}\t${cmd}\n")\r
+\r
+    ## Constructor of ModuleMakefile\r
+    #\r
+    #   @param  ModuleAutoGen   Object of ModuleAutoGen class\r
+    #\r
+    def __init__(self, ModuleAutoGen):\r
+        BuildFile.__init__(self, ModuleAutoGen)\r
+        self.PlatformInfo = self._AutoGenObject.PlatformInfo\r
+\r
+        self.ResultFileList = []\r
+        self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]\r
+\r
+        self.FileBuildTargetList = []       # [(src, target string)]\r
+        self.BuildTargetList = []           # [target string]\r
+        self.PendingBuildTargetList = []    # [FileBuildRule objects]\r
+        self.CommonFileDependency = []\r
+        self.FileListMacros = {}\r
+        self.ListFileMacros = {}\r
+        self.ObjTargetDict = OrderedDict()\r
+        self.FileCache = {}\r
+        self.LibraryBuildCommandList = []\r
+        self.LibraryFileList = []\r
+        self.LibraryMakefileList = []\r
+        self.LibraryBuildDirectoryList = []\r
+        self.SystemLibraryList = []\r
+        self.Macros = OrderedDict()\r
+        self.Macros["OUTPUT_DIR"      ] = self._AutoGenObject.Macros["OUTPUT_DIR"]\r
+        self.Macros["DEBUG_DIR"       ] = self._AutoGenObject.Macros["DEBUG_DIR"]\r
+        self.Macros["MODULE_BUILD_DIR"] = self._AutoGenObject.Macros["MODULE_BUILD_DIR"]\r
+        self.Macros["BIN_DIR"         ] = self._AutoGenObject.Macros["BIN_DIR"]\r
+        self.Macros["BUILD_DIR"       ] = self._AutoGenObject.Macros["BUILD_DIR"]\r
+        self.Macros["WORKSPACE"       ] = self._AutoGenObject.Macros["WORKSPACE"]\r
+        self.Macros["FFS_OUTPUT_DIR"  ] = self._AutoGenObject.Macros["FFS_OUTPUT_DIR"]\r
+        self.GenFfsList                 = ModuleAutoGen.GenFfsList\r
+        self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']\r
+        self.FfsOutputFileList = []\r
+\r
+    # Compose a dict object containing information used to do replacement in template\r
+    @property\r
+    def _TemplateDict(self):\r
+        if self._FileType not in self._SEP_:\r
+            EdkLogger.error("build", PARAMETER_INVALID, "Invalid Makefile type [%s]" % self._FileType,\r
+                            ExtraData="[%s]" % str(self._AutoGenObject))\r
+        MyAgo = self._AutoGenObject\r
+        Separator = self._SEP_[self._FileType]\r
+\r
+        # break build if no source files and binary files are found\r
+        if len(MyAgo.SourceFileList) == 0 and len(MyAgo.BinaryFileList) == 0:\r
+            EdkLogger.error("build", AUTOGEN_ERROR, "No files to be built in module [%s, %s, %s]"\r
+                            % (MyAgo.BuildTarget, MyAgo.ToolChain, MyAgo.Arch),\r
+                            ExtraData="[%s]" % str(MyAgo))\r
+\r
+        # convert dependent libraries to build command\r
+        self.ProcessDependentLibrary()\r
+        if len(MyAgo.Module.ModuleEntryPointList) > 0:\r
+            ModuleEntryPoint = MyAgo.Module.ModuleEntryPointList[0]\r
+        else:\r
+            ModuleEntryPoint = "_ModuleEntryPoint"\r
+\r
+        ArchEntryPoint = ModuleEntryPoint\r
+\r
+        if MyAgo.Arch == "EBC":\r
+            # EBC compiler always use "EfiStart" as entry point. Only applies to EdkII modules\r
+            ImageEntryPoint = "EfiStart"\r
+        else:\r
+            # EdkII modules always use "_ModuleEntryPoint" as entry point\r
+            ImageEntryPoint = "_ModuleEntryPoint"\r
+\r
+        for k, v in MyAgo.Module.Defines.items():\r
+            if k not in MyAgo.Macros:\r
+                MyAgo.Macros[k] = v\r
+\r
+        if 'MODULE_ENTRY_POINT' not in MyAgo.Macros:\r
+            MyAgo.Macros['MODULE_ENTRY_POINT'] = ModuleEntryPoint\r
+        if 'ARCH_ENTRY_POINT' not in MyAgo.Macros:\r
+            MyAgo.Macros['ARCH_ENTRY_POINT'] = ArchEntryPoint\r
+        if 'IMAGE_ENTRY_POINT' not in MyAgo.Macros:\r
+            MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint\r
+\r
+        PCI_COMPRESS_Flag = False\r
+        for k, v in MyAgo.Module.Defines.items():\r
+            if 'PCI_COMPRESS' == k and 'TRUE' == v:\r
+                PCI_COMPRESS_Flag = True\r
+\r
+        # tools definitions\r
+        ToolsDef = []\r
+        IncPrefix = self._INC_FLAG_[MyAgo.ToolChainFamily]\r
+        for Tool in MyAgo.BuildOption:\r
+            for Attr in MyAgo.BuildOption[Tool]:\r
+                Value = MyAgo.BuildOption[Tool][Attr]\r
+                if Attr == "FAMILY":\r
+                    continue\r
+                elif Attr == "PATH":\r
+                    ToolsDef.append("%s = %s" % (Tool, Value))\r
+                else:\r
+                    # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.\r
+                    if Tool == "MAKE":\r
+                        continue\r
+                    # Remove duplicated include path, if any\r
+                    if Attr == "FLAGS":\r
+                        Value = RemoveDupOption(Value, IncPrefix, MyAgo.IncludePathList)\r
+                        if self._AutoGenObject.BuildRuleFamily == TAB_COMPILER_MSFT and Tool == 'CC' and '/GM' in Value:\r
+                            Value = Value.replace(' /MP', '')\r
+                            MyAgo.BuildOption[Tool][Attr] = Value\r
+                        if Tool == "OPTROM" and PCI_COMPRESS_Flag:\r
+                            ValueList = Value.split()\r
+                            if ValueList:\r
+                                for i, v in enumerate(ValueList):\r
+                                    if '-e' == v:\r
+                                        ValueList[i] = '-ec'\r
+                                Value = ' '.join(ValueList)\r
+\r
+                    ToolsDef.append("%s_%s = %s" % (Tool, Attr, Value))\r
+            ToolsDef.append("")\r
+\r
+        # generate the Response file and Response flag\r
+        RespDict = self.CommandExceedLimit()\r
+        RespFileList = os.path.join(MyAgo.OutputDir, 'respfilelist.txt')\r
+        if RespDict:\r
+            RespFileListContent = ''\r
+            for Resp in RespDict:\r
+                RespFile = os.path.join(MyAgo.OutputDir, str(Resp).lower() + '.txt')\r
+                StrList = RespDict[Resp].split(' ')\r
+                UnexpandMacro = []\r
+                NewStr = []\r
+                for Str in StrList:\r
+                    if '$' in Str:\r
+                        UnexpandMacro.append(Str)\r
+                    else:\r
+                        NewStr.append(Str)\r
+                UnexpandMacroStr = ' '.join(UnexpandMacro)\r
+                NewRespStr = ' '.join(NewStr)\r
+                SaveFileOnChange(RespFile, NewRespStr, False)\r
+                ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))\r
+                RespFileListContent += '@' + RespFile + TAB_LINE_BREAK\r
+                RespFileListContent += NewRespStr + TAB_LINE_BREAK\r
+            SaveFileOnChange(RespFileList, RespFileListContent, False)\r
+        else:\r
+            if os.path.exists(RespFileList):\r
+                os.remove(RespFileList)\r
+\r
+        # convert source files and binary files to build targets\r
+        self.ResultFileList = [str(T.Target) for T in MyAgo.CodaTargetList]\r
+        if len(self.ResultFileList) == 0 and len(MyAgo.SourceFileList) != 0:\r
+            EdkLogger.error("build", AUTOGEN_ERROR, "Nothing to build",\r
+                            ExtraData="[%s]" % str(MyAgo))\r
+\r
+        self.ProcessBuildTargetList()\r
+        self.ParserGenerateFfsCmd()\r
+\r
+        # Generate macros used to represent input files\r
+        FileMacroList = [] # macro name = file list\r
+        for FileListMacro in self.FileListMacros:\r
+            FileMacro = self._FILE_MACRO_TEMPLATE.Replace(\r
+                                                    {\r
+                                                        "macro_name"  : FileListMacro,\r
+                                                        "source_file" : self.FileListMacros[FileListMacro]\r
+                                                    }\r
+                                                    )\r
+            FileMacroList.append(FileMacro)\r
+\r
+        # INC_LIST is special\r
+        FileMacro = ""\r
+        IncludePathList = []\r
+        for P in  MyAgo.IncludePathList:\r
+            IncludePathList.append(IncPrefix + self.PlaceMacro(P, self.Macros))\r
+            if FileBuildRule.INC_LIST_MACRO in self.ListFileMacros:\r
+                self.ListFileMacros[FileBuildRule.INC_LIST_MACRO].append(IncPrefix + P)\r
+        FileMacro += self._FILE_MACRO_TEMPLATE.Replace(\r
+                                                {\r
+                                                    "macro_name"   : "INC",\r
+                                                    "source_file" : IncludePathList\r
+                                                }\r
+                                                )\r
+        FileMacroList.append(FileMacro)\r
+        # Add support when compiling .nasm source files\r
+        for File in self.FileCache.keys():\r
+            if not str(File).endswith('.nasm'):\r
+                continue\r
+            IncludePathList = []\r
+            for P in  MyAgo.IncludePathList:\r
+                IncludePath = self._INC_FLAG_['NASM'] + self.PlaceMacro(P, self.Macros)\r
+                if IncludePath.endswith(os.sep):\r
+                    IncludePath = IncludePath.rstrip(os.sep)\r
+                # When compiling .nasm files, need to add a literal backslash at each path\r
+                # To specify a literal backslash at the end of the line, precede it with a caret (^)\r
+                if P == MyAgo.IncludePathList[-1] and os.sep == '\\':\r
+                    IncludePath = ''.join([IncludePath, '^', os.sep])\r
+                else:\r
+                    IncludePath = os.path.join(IncludePath, '')\r
+                IncludePathList.append(IncludePath)\r
+            FileMacroList.append(self._FILE_MACRO_TEMPLATE.Replace({"macro_name": "NASM_INC", "source_file": IncludePathList}))\r
+            break\r
+\r
+        # Generate macros used to represent files containing list of input files\r
+        for ListFileMacro in self.ListFileMacros:\r
+            ListFileName = os.path.join(MyAgo.OutputDir, "%s.lst" % ListFileMacro.lower()[:len(ListFileMacro) - 5])\r
+            FileMacroList.append("%s = %s" % (ListFileMacro, ListFileName))\r
+            SaveFileOnChange(\r
+                ListFileName,\r
+                "\n".join(self.ListFileMacros[ListFileMacro]),\r
+                False\r
+                )\r
+\r
+        # Generate objlist used to create .obj file\r
+        for Type in self.ObjTargetDict:\r
+            NewLine = ' '.join(list(self.ObjTargetDict[Type]))\r
+            FileMacroList.append("OBJLIST_%s = %s" % (list(self.ObjTargetDict.keys()).index(Type), NewLine))\r
+\r
+        BcTargetList = []\r
+\r
+        MakefileName = self._FILE_NAME_[self._FileType]\r
+        LibraryMakeCommandList = []\r
+        for D in self.LibraryBuildDirectoryList:\r
+            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}\r
+            LibraryMakeCommandList.append(Command)\r
+\r
+        package_rel_dir = MyAgo.SourceDir\r
+        current_dir = self.Macros["WORKSPACE"]\r
+        found = False\r
+        while not found and os.sep in package_rel_dir:\r
+            index = package_rel_dir.index(os.sep)\r
+            current_dir = mws.join(current_dir, package_rel_dir[:index])\r
+            if os.path.exists(current_dir):\r
+                for fl in os.listdir(current_dir):\r
+                    if fl.endswith('.dec'):\r
+                        found = True\r
+                        break\r
+            package_rel_dir = package_rel_dir[index + 1:]\r
+\r
+        MakefileTemplateDict = {\r
+            "makefile_header"           : self._FILE_HEADER_[self._FileType],\r
+            "makefile_path"             : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),\r
+            "makefile_name"             : MakefileName,\r
+            "platform_name"             : self.PlatformInfo.Name,\r
+            "platform_guid"             : self.PlatformInfo.Guid,\r
+            "platform_version"          : self.PlatformInfo.Version,\r
+            "platform_relative_directory": self.PlatformInfo.SourceDir,\r
+            "platform_output_directory" : self.PlatformInfo.OutputDir,\r
+            "ffs_output_directory"      : MyAgo.Macros["FFS_OUTPUT_DIR"],\r
+            "platform_dir"              : MyAgo.Macros["PLATFORM_DIR"],\r
+\r
+            "module_name"               : MyAgo.Name,\r
+            "module_guid"               : MyAgo.Guid,\r
+            "module_name_guid"          : MyAgo.UniqueBaseName,\r
+            "module_version"            : MyAgo.Version,\r
+            "module_type"               : MyAgo.ModuleType,\r
+            "module_file"               : MyAgo.MetaFile.Name,\r
+            "module_file_base_name"     : MyAgo.MetaFile.BaseName,\r
+            "module_relative_directory" : MyAgo.SourceDir,\r
+            "module_dir"                : mws.join (self.Macros["WORKSPACE"], MyAgo.SourceDir),\r
+            "package_relative_directory": package_rel_dir,\r
+            "module_extra_defines"      : ["%s = %s" % (k, v) for k, v in MyAgo.Module.Defines.items()],\r
+\r
+            "architecture"              : MyAgo.Arch,\r
+            "toolchain_tag"             : MyAgo.ToolChain,\r
+            "build_target"              : MyAgo.BuildTarget,\r
+\r
+            "platform_build_directory"  : self.PlatformInfo.BuildDir,\r
+            "module_build_directory"    : MyAgo.BuildDir,\r
+            "module_output_directory"   : MyAgo.OutputDir,\r
+            "module_debug_directory"    : MyAgo.DebugDir,\r
+\r
+            "separator"                 : Separator,\r
+            "module_tool_definitions"   : ToolsDef,\r
+\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._FileType].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._FileType].values()),\r
+\r
+            "module_entry_point"        : ModuleEntryPoint,\r
+            "image_entry_point"         : ImageEntryPoint,\r
+            "arch_entry_point"          : ArchEntryPoint,\r
+            "remaining_build_target"    : self.ResultFileList,\r
+            "common_dependency_file"    : self.CommonFileDependency,\r
+            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),\r
+            "clean_command"             : self.GetRemoveDirectoryCommand(["$(OUTPUT_DIR)"]),\r
+            "cleanall_command"          : self.GetRemoveDirectoryCommand(["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]),\r
+            "dependent_library_build_directory" : self.LibraryBuildDirectoryList,\r
+            "library_build_command"     : LibraryMakeCommandList,\r
+            "file_macro"                : FileMacroList,\r
+            "file_build_target"         : self.BuildTargetList,\r
+            "backward_compatible_target": BcTargetList,\r
+        }\r
+\r
+        return MakefileTemplateDict\r
+\r
+    def ParserGenerateFfsCmd(self):\r
+        #Add Ffs cmd to self.BuildTargetList\r
+        OutputFile = ''\r
+        DepsFileList = []\r
+\r
+        for Cmd in self.GenFfsList:\r
+            if Cmd[2]:\r
+                for CopyCmd in Cmd[2]:\r
+                    Src, Dst = CopyCmd\r
+                    Src = self.ReplaceMacro(Src)\r
+                    Dst = self.ReplaceMacro(Dst)\r
+                    if Dst not in self.ResultFileList:\r
+                        self.ResultFileList.append(Dst)\r
+                    if '%s :' %(Dst) not in self.BuildTargetList:\r
+                        self.BuildTargetList.append("%s :" %(Dst))\r
+                        self.BuildTargetList.append('\t' + self._CP_TEMPLATE_[self._FileType] %{'Src': Src, 'Dst': Dst})\r
+\r
+            FfsCmdList = Cmd[0]\r
+            for index, Str in enumerate(FfsCmdList):\r
+                if '-o' == Str:\r
+                    OutputFile = FfsCmdList[index + 1]\r
+                if '-i' == Str or "-oi" == Str:\r
+                    if DepsFileList == []:\r
+                        DepsFileList = [FfsCmdList[index + 1]]\r
+                    else:\r
+                        DepsFileList.append(FfsCmdList[index + 1])\r
+            DepsFileString = ' '.join(DepsFileList).strip()\r
+            if DepsFileString == '':\r
+                continue\r
+            OutputFile = self.ReplaceMacro(OutputFile)\r
+            self.ResultFileList.append(OutputFile)\r
+            DepsFileString = self.ReplaceMacro(DepsFileString)\r
+            self.BuildTargetList.append('%s : %s' % (OutputFile, DepsFileString))\r
+            CmdString = ' '.join(FfsCmdList).strip()\r
+            CmdString = self.ReplaceMacro(CmdString)\r
+            self.BuildTargetList.append('\t%s' % CmdString)\r
+\r
+            self.ParseSecCmd(DepsFileList, Cmd[1])\r
+            for SecOutputFile, SecDepsFile, SecCmd in self.FfsOutputFileList :\r
+                self.BuildTargetList.append('%s : %s' % (self.ReplaceMacro(SecOutputFile), self.ReplaceMacro(SecDepsFile)))\r
+                self.BuildTargetList.append('\t%s' % self.ReplaceMacro(SecCmd))\r
+            self.FfsOutputFileList = []\r
+\r
+    def ParseSecCmd(self, OutputFileList, CmdTuple):\r
+        for OutputFile in OutputFileList:\r
+            for SecCmdStr in CmdTuple:\r
+                SecDepsFileList = []\r
+                SecCmdList = SecCmdStr.split()\r
+                CmdName = SecCmdList[0]\r
+                for index, CmdItem in enumerate(SecCmdList):\r
+                    if '-o' == CmdItem and OutputFile == SecCmdList[index + 1]:\r
+                        index = index + 1\r
+                        while index + 1 < len(SecCmdList):\r
+                            if not SecCmdList[index+1].startswith('-'):\r
+                                SecDepsFileList.append(SecCmdList[index + 1])\r
+                            index = index + 1\r
+                        if CmdName == 'Trim':\r
+                            SecDepsFileList.append(os.path.join('$(DEBUG_DIR)', os.path.basename(OutputFile).replace('offset', 'efi')))\r
+                        if OutputFile.endswith('.ui') or OutputFile.endswith('.ver'):\r
+                            SecDepsFileList.append(os.path.join('$(MODULE_DIR)', '$(MODULE_FILE)'))\r
+                        self.FfsOutputFileList.append((OutputFile, ' '.join(SecDepsFileList), SecCmdStr))\r
+                        if len(SecDepsFileList) > 0:\r
+                            self.ParseSecCmd(SecDepsFileList, CmdTuple)\r
+                        break\r
+                    else:\r
+                        continue\r
+\r
+    def ReplaceMacro(self, str):\r
+        for Macro in self.MacroList:\r
+            if self._AutoGenObject.Macros[Macro] and self._AutoGenObject.Macros[Macro] in str:\r
+                str = str.replace(self._AutoGenObject.Macros[Macro], '$(' + Macro + ')')\r
+        return str\r
+\r
+    def CommandExceedLimit(self):\r
+        FlagDict = {\r
+                    'CC'    :  { 'Macro' : '$(CC_FLAGS)',    'Value' : False},\r
+                    'PP'    :  { 'Macro' : '$(PP_FLAGS)',    'Value' : False},\r
+                    'APP'   :  { 'Macro' : '$(APP_FLAGS)',   'Value' : False},\r
+                    'ASLPP' :  { 'Macro' : '$(ASLPP_FLAGS)', 'Value' : False},\r
+                    'VFRPP' :  { 'Macro' : '$(VFRPP_FLAGS)', 'Value' : False},\r
+                    'ASM'   :  { 'Macro' : '$(ASM_FLAGS)',   'Value' : False},\r
+                    'ASLCC' :  { 'Macro' : '$(ASLCC_FLAGS)', 'Value' : False},\r
+                   }\r
+\r
+        RespDict = {}\r
+        FileTypeList = []\r
+        IncPrefix = self._INC_FLAG_[self._AutoGenObject.ToolChainFamily]\r
+\r
+        # base on the source files to decide the file type\r
+        for File in self._AutoGenObject.SourceFileList:\r
+            for type in self._AutoGenObject.FileTypes:\r
+                if File in self._AutoGenObject.FileTypes[type]:\r
+                    if type not in FileTypeList:\r
+                        FileTypeList.append(type)\r
+\r
+        # calculate the command-line length\r
+        if FileTypeList:\r
+            for type in FileTypeList:\r
+                BuildTargets = self._AutoGenObject.BuildRules[type].BuildTargets\r
+                for Target in BuildTargets:\r
+                    CommandList = BuildTargets[Target].Commands\r
+                    for SingleCommand in CommandList:\r
+                        Tool = ''\r
+                        SingleCommandLength = len(SingleCommand)\r
+                        SingleCommandList = SingleCommand.split()\r
+                        if len(SingleCommandList) > 0:\r
+                            for Flag in FlagDict:\r
+                                if '$('+ Flag +')' in SingleCommandList[0]:\r
+                                    Tool = Flag\r
+                                    break\r
+                        if Tool:\r
+                            if 'PATH' not in self._AutoGenObject.BuildOption[Tool]:\r
+                                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))\r
+                            SingleCommandLength += len(self._AutoGenObject.BuildOption[Tool]['PATH'])\r
+                            for item in SingleCommandList[1:]:\r
+                                if FlagDict[Tool]['Macro'] in item:\r
+                                    if 'FLAGS' not in self._AutoGenObject.BuildOption[Tool]:\r
+                                        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))\r
+                                    Str = self._AutoGenObject.BuildOption[Tool]['FLAGS']\r
+                                    for Option in self._AutoGenObject.BuildOption:\r
+                                        for Attr in self._AutoGenObject.BuildOption[Option]:\r
+                                            if Str.find(Option + '_' + Attr) != -1:\r
+                                                Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])\r
+                                    while(Str.find('$(') != -1):\r
+                                        for macro in self._AutoGenObject.Macros:\r
+                                            MacroName = '$('+ macro + ')'\r
+                                            if (Str.find(MacroName) != -1):\r
+                                                Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])\r
+                                                break\r
+                                        else:\r
+                                            break\r
+                                    SingleCommandLength += len(Str)\r
+                                elif '$(INC)' in item:\r
+                                    SingleCommandLength += self._AutoGenObject.IncludePathLength + len(IncPrefix) * len(self._AutoGenObject.IncludePathList)\r
+                                elif item.find('$(') != -1:\r
+                                    Str = item\r
+                                    for Option in self._AutoGenObject.BuildOption:\r
+                                        for Attr in self._AutoGenObject.BuildOption[Option]:\r
+                                            if Str.find(Option + '_' + Attr) != -1:\r
+                                                Str = Str.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])\r
+                                    while(Str.find('$(') != -1):\r
+                                        for macro in self._AutoGenObject.Macros:\r
+                                            MacroName = '$('+ macro + ')'\r
+                                            if (Str.find(MacroName) != -1):\r
+                                                Str = Str.replace(MacroName, self._AutoGenObject.Macros[macro])\r
+                                                break\r
+                                        else:\r
+                                            break\r
+                                    SingleCommandLength += len(Str)\r
+\r
+                            if SingleCommandLength > GlobalData.gCommandMaxLength:\r
+                                FlagDict[Tool]['Value'] = True\r
+\r
+                # generate the response file content by combine the FLAGS and INC\r
+                for Flag in FlagDict:\r
+                    if FlagDict[Flag]['Value']:\r
+                        Key = Flag + '_RESP'\r
+                        RespMacro = FlagDict[Flag]['Macro'].replace('FLAGS', 'RESP')\r
+                        Value = self._AutoGenObject.BuildOption[Flag]['FLAGS']\r
+                        for inc in self._AutoGenObject.IncludePathList:\r
+                            Value += ' ' + IncPrefix + inc\r
+                        for Option in self._AutoGenObject.BuildOption:\r
+                            for Attr in self._AutoGenObject.BuildOption[Option]:\r
+                                if Value.find(Option + '_' + Attr) != -1:\r
+                                    Value = Value.replace('$(' + Option + '_' + Attr + ')', self._AutoGenObject.BuildOption[Option][Attr])\r
+                        while (Value.find('$(') != -1):\r
+                            for macro in self._AutoGenObject.Macros:\r
+                                MacroName = '$('+ macro + ')'\r
+                                if (Value.find(MacroName) != -1):\r
+                                    Value = Value.replace(MacroName, self._AutoGenObject.Macros[macro])\r
+                                    break\r
+                            else:\r
+                                break\r
+\r
+                        if self._AutoGenObject.ToolChainFamily == 'GCC':\r
+                            RespDict[Key] = Value.replace('\\', '/')\r
+                        else:\r
+                            RespDict[Key] = Value\r
+                        for Target in BuildTargets:\r
+                            for i, SingleCommand in enumerate(BuildTargets[Target].Commands):\r
+                                if FlagDict[Flag]['Macro'] in SingleCommand:\r
+                                    BuildTargets[Target].Commands[i] = SingleCommand.replace('$(INC)', '').replace(FlagDict[Flag]['Macro'], RespMacro)\r
+        return RespDict\r
+\r
+    def ProcessBuildTargetList(self):\r
+        #\r
+        # Search dependency file list for each source file\r
+        #\r
+        ForceIncludedFile = []\r
+        for File in self._AutoGenObject.AutoGenFileList:\r
+            if File.Ext == '.h':\r
+                ForceIncludedFile.append(File)\r
+        SourceFileList = []\r
+        OutPutFileList = []\r
+        for Target in self._AutoGenObject.IntroTargetList:\r
+            SourceFileList.extend(Target.Inputs)\r
+            OutPutFileList.extend(Target.Outputs)\r
+\r
+        if OutPutFileList:\r
+            for Item in OutPutFileList:\r
+                if Item in SourceFileList:\r
+                    SourceFileList.remove(Item)\r
+\r
+        FileDependencyDict = self.GetFileDependency(\r
+                                    SourceFileList,\r
+                                    ForceIncludedFile,\r
+                                    self._AutoGenObject.IncludePathList + self._AutoGenObject.BuildOptionIncPathList\r
+                                    )\r
+\r
+        # Check if header files are listed in metafile\r
+        # Get a list of unique module header source files from MetaFile\r
+        headerFilesInMetaFileSet = set()\r
+        for aFile in self._AutoGenObject.SourceFileList:\r
+            aFileName = str(aFile)\r
+            if not aFileName.endswith('.h'):\r
+                continue\r
+            headerFilesInMetaFileSet.add(aFileName.lower())\r
+\r
+        # Get a list of unique module autogen files\r
+        localAutoGenFileSet = set()\r
+        for aFile in self._AutoGenObject.AutoGenFileList:\r
+            localAutoGenFileSet.add(str(aFile).lower())\r
+\r
+        # Get a list of unique module dependency header files\r
+        # Exclude autogen files and files not in the source directory\r
+        headerFileDependencySet = set()\r
+        localSourceDir = str(self._AutoGenObject.SourceDir).lower()\r
+        for Dependency in FileDependencyDict.values():\r
+            for aFile in Dependency:\r
+                aFileName = str(aFile).lower()\r
+                if not aFileName.endswith('.h'):\r
+                    continue\r
+                if aFileName in localAutoGenFileSet:\r
+                    continue\r
+                if localSourceDir not in aFileName:\r
+                    continue\r
+                headerFileDependencySet.add(aFileName)\r
+\r
+        # Check if a module dependency header file is missing from the module's MetaFile\r
+        for aFile in headerFileDependencySet:\r
+            if aFile in headerFilesInMetaFileSet:\r
+                continue\r
+            EdkLogger.warn("build","Module MetaFile [Sources] is missing local header!",\r
+                        ExtraData = "Local Header: " + aFile + " not found in " + self._AutoGenObject.MetaFile.Path\r
+                        )\r
+\r
+        DepSet = None\r
+        for File,Dependency in FileDependencyDict.items():\r
+            if not Dependency:\r
+                FileDependencyDict[File] = ['$(FORCE_REBUILD)']\r
+                continue\r
+\r
+            self._AutoGenObject.AutoGenDepSet |= set(Dependency)\r
+\r
+            # skip non-C files\r
+            if File.Ext not in [".c", ".C"] or File.Name == "AutoGen.c":\r
+                continue\r
+            elif DepSet is None:\r
+                DepSet = set(Dependency)\r
+            else:\r
+                DepSet &= set(Dependency)\r
+        # in case nothing in SourceFileList\r
+        if DepSet is None:\r
+            DepSet = set()\r
+        #\r
+        # Extract common files list in the dependency files\r
+        #\r
+        for File in DepSet:\r
+            self.CommonFileDependency.append(self.PlaceMacro(File.Path, self.Macros))\r
+\r
+        CmdSumDict = {}\r
+        CmdTargetDict = {}\r
+        CmdCppDict = {}\r
+        DependencyDict = FileDependencyDict.copy()\r
+        for File in FileDependencyDict:\r
+            # skip non-C files\r
+            if File.Ext not in [".c", ".C"] or File.Name == "AutoGen.c":\r
+                continue\r
+            NewDepSet = set(FileDependencyDict[File])\r
+            NewDepSet -= DepSet\r
+            FileDependencyDict[File] = ["$(COMMON_DEPS)"] + list(NewDepSet)\r
+            DependencyDict[File] = list(NewDepSet)\r
+\r
+        # Convert target description object to target string in makefile\r
+        if self._AutoGenObject.BuildRuleFamily == TAB_COMPILER_MSFT and TAB_C_CODE_FILE in self._AutoGenObject.Targets:\r
+            for T in self._AutoGenObject.Targets[TAB_C_CODE_FILE]:\r
+                NewFile = self.PlaceMacro(str(T), self.Macros)\r
+                if not self.ObjTargetDict.get(T.Target.SubDir):\r
+                    self.ObjTargetDict[T.Target.SubDir] = set()\r
+                self.ObjTargetDict[T.Target.SubDir].add(NewFile)\r
+        for Type in self._AutoGenObject.Targets:\r
+            for T in self._AutoGenObject.Targets[Type]:\r
+                # Generate related macros if needed\r
+                if T.GenFileListMacro and T.FileListMacro not in self.FileListMacros:\r
+                    self.FileListMacros[T.FileListMacro] = []\r
+                if T.GenListFile and T.ListFileMacro not in self.ListFileMacros:\r
+                    self.ListFileMacros[T.ListFileMacro] = []\r
+                if T.GenIncListFile and T.IncListFileMacro not in self.ListFileMacros:\r
+                    self.ListFileMacros[T.IncListFileMacro] = []\r
+\r
+                Deps = []\r
+                CCodeDeps = []\r
+                # Add force-dependencies\r
+                for Dep in T.Dependencies:\r
+                    Deps.append(self.PlaceMacro(str(Dep), self.Macros))\r
+                    if Dep != '$(MAKE_FILE)':\r
+                        CCodeDeps.append(self.PlaceMacro(str(Dep), self.Macros))\r
+                # Add inclusion-dependencies\r
+                if len(T.Inputs) == 1 and T.Inputs[0] in FileDependencyDict:\r
+                    for F in FileDependencyDict[T.Inputs[0]]:\r
+                        Deps.append(self.PlaceMacro(str(F), self.Macros))\r
+                # Add source-dependencies\r
+                for F in T.Inputs:\r
+                    NewFile = self.PlaceMacro(str(F), self.Macros)\r
+                    # In order to use file list macro as dependency\r
+                    if T.GenListFile:\r
+                        # gnu tools need forward slash path separator, even on Windows\r
+                        self.ListFileMacros[T.ListFileMacro].append(str(F).replace ('\\', '/'))\r
+                        self.FileListMacros[T.FileListMacro].append(NewFile)\r
+                    elif T.GenFileListMacro:\r
+                        self.FileListMacros[T.FileListMacro].append(NewFile)\r
+                    else:\r
+                        Deps.append(NewFile)\r
+\r
+                # Use file list macro as dependency\r
+                if T.GenFileListMacro:\r
+                    Deps.append("$(%s)" % T.FileListMacro)\r
+                    if Type in [TAB_OBJECT_FILE, TAB_STATIC_LIBRARY]:\r
+                        Deps.append("$(%s)" % T.ListFileMacro)\r
+\r
+                if self._AutoGenObject.BuildRuleFamily == TAB_COMPILER_MSFT and Type == TAB_C_CODE_FILE:\r
+                    T, CmdTarget, CmdTargetDict, CmdCppDict = self.ParserCCodeFile(T, Type, CmdSumDict, CmdTargetDict, CmdCppDict, DependencyDict)\r
+                    TargetDict = {"target": self.PlaceMacro(T.Target.Path, self.Macros), "cmd": "\n\t".join(T.Commands),"deps": CCodeDeps}\r
+                    CmdLine = self._BUILD_TARGET_TEMPLATE.Replace(TargetDict).rstrip().replace('\t$(OBJLIST', '$(OBJLIST')\r
+                    if T.Commands:\r
+                        CmdLine = '%s%s' %(CmdLine, TAB_LINE_BREAK)\r
+                    if CCodeDeps or CmdLine:\r
+                        self.BuildTargetList.append(CmdLine)\r
+                else:\r
+                    TargetDict = {"target": self.PlaceMacro(T.Target.Path, self.Macros), "cmd": "\n\t".join(T.Commands),"deps": Deps}\r
+                    self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))\r
+\r
+    def ParserCCodeFile(self, T, Type, CmdSumDict, CmdTargetDict, CmdCppDict, DependencyDict):\r
+        if not CmdSumDict:\r
+            for item in self._AutoGenObject.Targets[Type]:\r
+                CmdSumDict[item.Target.SubDir] = item.Target.BaseName\r
+                for CppPath in item.Inputs:\r
+                    Path = self.PlaceMacro(CppPath.Path, self.Macros)\r
+                    if CmdCppDict.get(item.Target.SubDir):\r
+                        CmdCppDict[item.Target.SubDir].append(Path)\r
+                    else:\r
+                        CmdCppDict[item.Target.SubDir] = ['$(MAKE_FILE)', Path]\r
+                    if CppPath.Path in DependencyDict:\r
+                        for Temp in DependencyDict[CppPath.Path]:\r
+                            try:\r
+                                Path = self.PlaceMacro(Temp.Path, self.Macros)\r
+                            except:\r
+                                continue\r
+                            if Path not in (self.CommonFileDependency + CmdCppDict[item.Target.SubDir]):\r
+                                CmdCppDict[item.Target.SubDir].append(Path)\r
+        if T.Commands:\r
+            CommandList = T.Commands[:]\r
+            for Item in CommandList[:]:\r
+                SingleCommandList = Item.split()\r
+                if len(SingleCommandList) > 0 and self.CheckCCCmd(SingleCommandList):\r
+                    for Temp in SingleCommandList:\r
+                        if Temp.startswith('/Fo'):\r
+                            CmdSign = '%s%s' % (Temp.rsplit(TAB_SLASH, 1)[0], TAB_SLASH)\r
+                            break\r
+                    else: continue\r
+                    if CmdSign not in list(CmdTargetDict.keys()):\r
+                        CmdTargetDict[CmdSign] = Item.replace(Temp, CmdSign)\r
+                    else:\r
+                        CmdTargetDict[CmdSign] = "%s %s" % (CmdTargetDict[CmdSign], SingleCommandList[-1])\r
+                    Index = CommandList.index(Item)\r
+                    CommandList.pop(Index)\r
+                    if SingleCommandList[-1].endswith("%s%s.c" % (TAB_SLASH, CmdSumDict[CmdSign.lstrip('/Fo').rsplit(TAB_SLASH, 1)[0]])):\r
+                        Cpplist = CmdCppDict[T.Target.SubDir]\r
+                        Cpplist.insert(0, '$(OBJLIST_%d): $(COMMON_DEPS)' % list(self.ObjTargetDict.keys()).index(T.Target.SubDir))\r
+                        T.Commands[Index] = '%s\n\t%s' % (' \\\n\t'.join(Cpplist), CmdTargetDict[CmdSign])\r
+                    else:\r
+                        T.Commands.pop(Index)\r
+        return T, CmdSumDict, CmdTargetDict, CmdCppDict\r
+\r
+    def CheckCCCmd(self, CommandList):\r
+        for cmd in CommandList:\r
+            if '$(CC)' in cmd:\r
+                return True\r
+        return False\r
+    ## For creating makefile targets for dependent libraries\r
+    def ProcessDependentLibrary(self):\r
+        for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:\r
+            if not LibraryAutoGen.IsBinaryModule and not LibraryAutoGen.CanSkipbyHash():\r
+                self.LibraryBuildDirectoryList.append(self.PlaceMacro(LibraryAutoGen.BuildDir, self.Macros))\r
+\r
+    ## Return a list containing source file's dependencies\r
+    #\r
+    #   @param      FileList        The list of source files\r
+    #   @param      ForceInculeList The list of files which will be included forcely\r
+    #   @param      SearchPathList  The list of search path\r
+    #\r
+    #   @retval     dict            The mapping between source file path and its dependencies\r
+    #\r
+    def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):\r
+        Dependency = {}\r
+        for F in FileList:\r
+            Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)\r
+        return Dependency\r
+\r
+    ## Find dependencies for one source file\r
+    #\r
+    #  By searching recursively "#include" directive in file, find out all the\r
+    #  files needed by given source file. The dependencies will be only searched\r
+    #  in given search path list.\r
+    #\r
+    #   @param      File            The source file\r
+    #   @param      ForceInculeList The list of files which will be included forcely\r
+    #   @param      SearchPathList  The list of search path\r
+    #\r
+    #   @retval     list            The list of files the given source file depends on\r
+    #\r
+    def GetDependencyList(self, File, ForceList, SearchPathList):\r
+        EdkLogger.debug(EdkLogger.DEBUG_1, "Try to get dependency files for %s" % File)\r
+        FileStack = [File] + ForceList\r
+        DependencySet = set()\r
+\r
+        if self._AutoGenObject.Arch not in gDependencyDatabase:\r
+            gDependencyDatabase[self._AutoGenObject.Arch] = {}\r
+        DepDb = gDependencyDatabase[self._AutoGenObject.Arch]\r
+\r
+        while len(FileStack) > 0:\r
+            F = FileStack.pop()\r
+\r
+            FullPathDependList = []\r
+            if F in self.FileCache:\r
+                for CacheFile in self.FileCache[F]:\r
+                    FullPathDependList.append(CacheFile)\r
+                    if CacheFile not in DependencySet:\r
+                        FileStack.append(CacheFile)\r
+                DependencySet.update(FullPathDependList)\r
+                continue\r
+\r
+            CurrentFileDependencyList = []\r
+            if F in DepDb:\r
+                CurrentFileDependencyList = DepDb[F]\r
+            else:\r
+                try:\r
+                    Fd = open(F.Path, 'rb')\r
+                    FileContent = Fd.read()\r
+                    Fd.close()\r
+                except BaseException as X:\r
+                    EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X))\r
+                if len(FileContent) == 0:\r
+                    continue\r
+                try:\r
+                    if FileContent[0] == 0xff or FileContent[0] == 0xfe:\r
+                        FileContent = FileContent.decode('utf-16')\r
+                    else:\r
+                        FileContent = FileContent.decode()\r
+                except:\r
+                    # The file is not txt file. for example .mcb file\r
+                    continue\r
+                IncludedFileList = gIncludePattern.findall(FileContent)\r
+\r
+                for Inc in IncludedFileList:\r
+                    Inc = Inc.strip()\r
+                    # if there's macro used to reference header file, expand it\r
+                    HeaderList = gMacroPattern.findall(Inc)\r
+                    if len(HeaderList) == 1 and len(HeaderList[0]) == 2:\r
+                        HeaderType = HeaderList[0][0]\r
+                        HeaderKey = HeaderList[0][1]\r
+                        if HeaderType in gIncludeMacroConversion:\r
+                            Inc = gIncludeMacroConversion[HeaderType] % {"HeaderKey" : HeaderKey}\r
+                        else:\r
+                            # not known macro used in #include, always build the file by\r
+                            # returning a empty dependency\r
+                            self.FileCache[File] = []\r
+                            return []\r
+                    Inc = os.path.normpath(Inc)\r
+                    CurrentFileDependencyList.append(Inc)\r
+                DepDb[F] = CurrentFileDependencyList\r
+\r
+            CurrentFilePath = F.Dir\r
+            PathList = [CurrentFilePath] + SearchPathList\r
+            for Inc in CurrentFileDependencyList:\r
+                for SearchPath in PathList:\r
+                    FilePath = os.path.join(SearchPath, Inc)\r
+                    if FilePath in gIsFileMap:\r
+                        if not gIsFileMap[FilePath]:\r
+                            continue\r
+                    # If isfile is called too many times, the performance is slow down.\r
+                    elif not os.path.isfile(FilePath):\r
+                        gIsFileMap[FilePath] = False\r
+                        continue\r
+                    else:\r
+                        gIsFileMap[FilePath] = True\r
+                    FilePath = PathClass(FilePath)\r
+                    FullPathDependList.append(FilePath)\r
+                    if FilePath not in DependencySet:\r
+                        FileStack.append(FilePath)\r
+                    break\r
+                else:\r
+                    EdkLogger.debug(EdkLogger.DEBUG_9, "%s included by %s was not found "\\r
+                                    "in any given path:\n\t%s" % (Inc, F, "\n\t".join(SearchPathList)))\r
+\r
+            self.FileCache[F] = FullPathDependList\r
+            DependencySet.update(FullPathDependList)\r
+\r
+        DependencySet.update(ForceList)\r
+        if File in DependencySet:\r
+            DependencySet.remove(File)\r
+        DependencyList = list(DependencySet)  # remove duplicate ones\r
+\r
+        return DependencyList\r
+\r
+## CustomMakefile class\r
+#\r
+#  This class encapsules makefie and its generation for module. It uses template to generate\r
+#  the content of makefile. The content of makefile will be got from ModuleAutoGen object.\r
+#\r
+class CustomMakefile(BuildFile):\r
+    ## template used to generate the makefile for module with custom makefile\r
+    _TEMPLATE_ = TemplateString('''\\r
+${makefile_header}\r
+\r
+#\r
+# Platform Macro Definition\r
+#\r
+PLATFORM_NAME = ${platform_name}\r
+PLATFORM_GUID = ${platform_guid}\r
+PLATFORM_VERSION = ${platform_version}\r
+PLATFORM_RELATIVE_DIR = ${platform_relative_directory}\r
+PLATFORM_DIR = ${platform_dir}\r
+PLATFORM_OUTPUT_DIR = ${platform_output_directory}\r
+\r
+#\r
+# Module Macro Definition\r
+#\r
+MODULE_NAME = ${module_name}\r
+MODULE_GUID = ${module_guid}\r
+MODULE_NAME_GUID = ${module_name_guid}\r
+MODULE_VERSION = ${module_version}\r
+MODULE_TYPE = ${module_type}\r
+MODULE_FILE = ${module_file}\r
+MODULE_FILE_BASE_NAME = ${module_file_base_name}\r
+BASE_NAME = $(MODULE_NAME)\r
+MODULE_RELATIVE_DIR = ${module_relative_directory}\r
+MODULE_DIR = ${module_dir}\r
+\r
+#\r
+# Build Configuration Macro Definition\r
+#\r
+ARCH = ${architecture}\r
+TOOLCHAIN = ${toolchain_tag}\r
+TOOLCHAIN_TAG = ${toolchain_tag}\r
+TARGET = ${build_target}\r
+\r
+#\r
+# Build Directory Macro Definition\r
+#\r
+# PLATFORM_BUILD_DIR = ${platform_build_directory}\r
+BUILD_DIR = ${platform_build_directory}\r
+BIN_DIR = $(BUILD_DIR)${separator}${architecture}\r
+LIB_DIR = $(BIN_DIR)\r
+MODULE_BUILD_DIR = ${module_build_directory}\r
+OUTPUT_DIR = ${module_output_directory}\r
+DEBUG_DIR = ${module_debug_directory}\r
+DEST_DIR_OUTPUT = $(OUTPUT_DIR)\r
+DEST_DIR_DEBUG = $(DEBUG_DIR)\r
+\r
+#\r
+# Tools definitions specific to this module\r
+#\r
+${BEGIN}${module_tool_definitions}\r
+${END}\r
+MAKE_FILE = ${makefile_path}\r
+\r
+#\r
+# Shell Command Macro\r
+#\r
+${BEGIN}${shell_command_code} = ${shell_command}\r
+${END}\r
+\r
+${custom_makefile_content}\r
+\r
+#\r
+# Target used when called from platform makefile, which will bypass the build of dependent libraries\r
+#\r
+\r
+pbuild: init all\r
+\r
+\r
+#\r
+# ModuleTarget\r
+#\r
+\r
+mbuild: init all\r
+\r
+#\r
+# Build Target used in multi-thread build mode, which no init target is needed\r
+#\r
+\r
+tbuild: all\r
+\r
+#\r
+# Initialization target: print build information and create necessary directories\r
+#\r
+init:\r
+\t-@echo Building ... $(MODULE_DIR)${separator}$(MODULE_FILE) [$(ARCH)]\r
+${BEGIN}\t-@${create_directory_command}\n${END}\\r
+\r
+''')\r
+\r
+    ## Constructor of CustomMakefile\r
+    #\r
+    #   @param  ModuleAutoGen   Object of ModuleAutoGen class\r
+    #\r
+    def __init__(self, ModuleAutoGen):\r
+        BuildFile.__init__(self, ModuleAutoGen)\r
+        self.PlatformInfo = self._AutoGenObject.PlatformInfo\r
+        self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]\r
+\r
+    # Compose a dict object containing information used to do replacement in template\r
+    @property\r
+    def _TemplateDict(self):\r
+        Separator = self._SEP_[self._FileType]\r
+        MyAgo = self._AutoGenObject\r
+        if self._FileType not in MyAgo.CustomMakefile:\r
+            EdkLogger.error('build', OPTION_NOT_SUPPORTED, "No custom makefile for %s" % self._FileType,\r
+                            ExtraData="[%s]" % str(MyAgo))\r
+        MakefilePath = mws.join(\r
+                                MyAgo.WorkspaceDir,\r
+                                MyAgo.CustomMakefile[self._FileType]\r
+                                )\r
+        try:\r
+            CustomMakefile = open(MakefilePath, 'r').read()\r
+        except:\r
+            EdkLogger.error('build', FILE_OPEN_FAILURE, File=str(MyAgo),\r
+                            ExtraData=MyAgo.CustomMakefile[self._FileType])\r
+\r
+        # tools definitions\r
+        ToolsDef = []\r
+        for Tool in MyAgo.BuildOption:\r
+            # Don't generate MAKE_FLAGS in makefile. It's put in environment variable.\r
+            if Tool == "MAKE":\r
+                continue\r
+            for Attr in MyAgo.BuildOption[Tool]:\r
+                if Attr == "FAMILY":\r
+                    continue\r
+                elif Attr == "PATH":\r
+                    ToolsDef.append("%s = %s" % (Tool, MyAgo.BuildOption[Tool][Attr]))\r
+                else:\r
+                    ToolsDef.append("%s_%s = %s" % (Tool, Attr, MyAgo.BuildOption[Tool][Attr]))\r
+            ToolsDef.append("")\r
+\r
+        MakefileName = self._FILE_NAME_[self._FileType]\r
+        MakefileTemplateDict = {\r
+            "makefile_header"           : self._FILE_HEADER_[self._FileType],\r
+            "makefile_path"             : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),\r
+            "platform_name"             : self.PlatformInfo.Name,\r
+            "platform_guid"             : self.PlatformInfo.Guid,\r
+            "platform_version"          : self.PlatformInfo.Version,\r
+            "platform_relative_directory": self.PlatformInfo.SourceDir,\r
+            "platform_output_directory" : self.PlatformInfo.OutputDir,\r
+            "platform_dir"              : MyAgo.Macros["PLATFORM_DIR"],\r
+\r
+            "module_name"               : MyAgo.Name,\r
+            "module_guid"               : MyAgo.Guid,\r
+            "module_name_guid"          : MyAgo.UniqueBaseName,\r
+            "module_version"            : MyAgo.Version,\r
+            "module_type"               : MyAgo.ModuleType,\r
+            "module_file"               : MyAgo.MetaFile,\r
+            "module_file_base_name"     : MyAgo.MetaFile.BaseName,\r
+            "module_relative_directory" : MyAgo.SourceDir,\r
+            "module_dir"                : mws.join (MyAgo.WorkspaceDir, MyAgo.SourceDir),\r
+\r
+            "architecture"              : MyAgo.Arch,\r
+            "toolchain_tag"             : MyAgo.ToolChain,\r
+            "build_target"              : MyAgo.BuildTarget,\r
+\r
+            "platform_build_directory"  : self.PlatformInfo.BuildDir,\r
+            "module_build_directory"    : MyAgo.BuildDir,\r
+            "module_output_directory"   : MyAgo.OutputDir,\r
+            "module_debug_directory"    : MyAgo.DebugDir,\r
+\r
+            "separator"                 : Separator,\r
+            "module_tool_definitions"   : ToolsDef,\r
+\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._FileType].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._FileType].values()),\r
+\r
+            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),\r
+            "custom_makefile_content"   : CustomMakefile\r
+        }\r
+\r
+        return MakefileTemplateDict\r
+\r
+## PlatformMakefile class\r
+#\r
+#  This class encapsules makefie and its generation for platform. It uses\r
+# template to generate the content of makefile. The content of makefile will be\r
+# got from PlatformAutoGen object.\r
+#\r
+class PlatformMakefile(BuildFile):\r
+    ## template used to generate the makefile for platform\r
+    _TEMPLATE_ = TemplateString('''\\r
+${makefile_header}\r
+\r
+#\r
+# Platform Macro Definition\r
+#\r
+PLATFORM_NAME = ${platform_name}\r
+PLATFORM_GUID = ${platform_guid}\r
+PLATFORM_VERSION = ${platform_version}\r
+PLATFORM_FILE = ${platform_file}\r
+PLATFORM_DIR = ${platform_dir}\r
+PLATFORM_OUTPUT_DIR = ${platform_output_directory}\r
+\r
+#\r
+# Build Configuration Macro Definition\r
+#\r
+TOOLCHAIN = ${toolchain_tag}\r
+TOOLCHAIN_TAG = ${toolchain_tag}\r
+TARGET = ${build_target}\r
+\r
+#\r
+# Build Directory Macro Definition\r
+#\r
+BUILD_DIR = ${platform_build_directory}\r
+FV_DIR = ${platform_build_directory}${separator}FV\r
+\r
+#\r
+# Shell Command Macro\r
+#\r
+${BEGIN}${shell_command_code} = ${shell_command}\r
+${END}\r
+\r
+MAKE = ${make_path}\r
+MAKE_FILE = ${makefile_path}\r
+\r
+#\r
+# Default target\r
+#\r
+all: init build_libraries build_modules\r
+\r
+#\r
+# Initialization target: print build information and create necessary directories\r
+#\r
+init:\r
+\t-@echo Building ... $(PLATFORM_FILE) [${build_architecture_list}]\r
+\t${BEGIN}-@${create_directory_command}\r
+\t${END}\r
+#\r
+# library build target\r
+#\r
+libraries: init build_libraries\r
+\r
+#\r
+# module build target\r
+#\r
+modules: init build_libraries build_modules\r
+\r
+#\r
+# Build all libraries:\r
+#\r
+build_libraries:\r
+${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${library_makefile_list} pbuild\r
+${END}\t@cd $(BUILD_DIR)\r
+\r
+#\r
+# Build all modules:\r
+#\r
+build_modules:\r
+${BEGIN}\t@"$(MAKE)" $(MAKE_FLAGS) -f ${module_makefile_list} pbuild\r
+${END}\t@cd $(BUILD_DIR)\r
+\r
+#\r
+# Clean intermediate files\r
+#\r
+clean:\r
+\t${BEGIN}-@${library_build_command} clean\r
+\t${END}${BEGIN}-@${module_build_command} clean\r
+\t${END}@cd $(BUILD_DIR)\r
+\r
+#\r
+# Clean all generated files except to makefile\r
+#\r
+cleanall:\r
+${BEGIN}\t${cleanall_command}\r
+${END}\r
+\r
+#\r
+# Clean all library files\r
+#\r
+cleanlib:\r
+\t${BEGIN}-@${library_build_command} cleanall\r
+\t${END}@cd $(BUILD_DIR)\n\r
+''')\r
+\r
+    ## Constructor of PlatformMakefile\r
+    #\r
+    #   @param  ModuleAutoGen   Object of PlatformAutoGen class\r
+    #\r
+    def __init__(self, PlatformAutoGen):\r
+        BuildFile.__init__(self, PlatformAutoGen)\r
+        self.ModuleBuildCommandList = []\r
+        self.ModuleMakefileList = []\r
+        self.IntermediateDirectoryList = []\r
+        self.ModuleBuildDirectoryList = []\r
+        self.LibraryBuildDirectoryList = []\r
+        self.LibraryMakeCommandList = []\r
+\r
+    # Compose a dict object containing information used to do replacement in template\r
+    @property\r
+    def _TemplateDict(self):\r
+        Separator = self._SEP_[self._FileType]\r
+\r
+        MyAgo = self._AutoGenObject\r
+        if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:\r
+            EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",\r
+                            ExtraData="[%s]" % str(MyAgo))\r
+\r
+        self.IntermediateDirectoryList = ["$(BUILD_DIR)"]\r
+        self.ModuleBuildDirectoryList = self.GetModuleBuildDirectoryList()\r
+        self.LibraryBuildDirectoryList = self.GetLibraryBuildDirectoryList()\r
+\r
+        MakefileName = self._FILE_NAME_[self._FileType]\r
+        LibraryMakefileList = []\r
+        LibraryMakeCommandList = []\r
+        for D in self.LibraryBuildDirectoryList:\r
+            D = self.PlaceMacro(D, {"BUILD_DIR":MyAgo.BuildDir})\r
+            Makefile = os.path.join(D, MakefileName)\r
+            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}\r
+            LibraryMakefileList.append(Makefile)\r
+            LibraryMakeCommandList.append(Command)\r
+        self.LibraryMakeCommandList = LibraryMakeCommandList\r
+\r
+        ModuleMakefileList = []\r
+        ModuleMakeCommandList = []\r
+        for D in self.ModuleBuildDirectoryList:\r
+            D = self.PlaceMacro(D, {"BUILD_DIR":MyAgo.BuildDir})\r
+            Makefile = os.path.join(D, MakefileName)\r
+            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}\r
+            ModuleMakefileList.append(Makefile)\r
+            ModuleMakeCommandList.append(Command)\r
+\r
+        MakefileTemplateDict = {\r
+            "makefile_header"           : self._FILE_HEADER_[self._FileType],\r
+            "makefile_path"             : os.path.join("$(BUILD_DIR)", MakefileName),\r
+            "make_path"                 : MyAgo.ToolDefinition["MAKE"]["PATH"],\r
+            "makefile_name"             : MakefileName,\r
+            "platform_name"             : MyAgo.Name,\r
+            "platform_guid"             : MyAgo.Guid,\r
+            "platform_version"          : MyAgo.Version,\r
+            "platform_file"             : MyAgo.MetaFile,\r
+            "platform_relative_directory": MyAgo.SourceDir,\r
+            "platform_output_directory" : MyAgo.OutputDir,\r
+            "platform_build_directory"  : MyAgo.BuildDir,\r
+            "platform_dir"              : MyAgo.Macros["PLATFORM_DIR"],\r
+\r
+            "toolchain_tag"             : MyAgo.ToolChain,\r
+            "build_target"              : MyAgo.BuildTarget,\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._FileType].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._FileType].values()),\r
+            "build_architecture_list"   : MyAgo.Arch,\r
+            "architecture"              : MyAgo.Arch,\r
+            "separator"                 : Separator,\r
+            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),\r
+            "cleanall_command"          : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),\r
+            "library_makefile_list"     : LibraryMakefileList,\r
+            "module_makefile_list"      : ModuleMakefileList,\r
+            "library_build_command"     : LibraryMakeCommandList,\r
+            "module_build_command"      : ModuleMakeCommandList,\r
+        }\r
+\r
+        return MakefileTemplateDict\r
+\r
+    ## Get the root directory list for intermediate files of all modules build\r
+    #\r
+    #   @retval     list    The list of directory\r
+    #\r
+    def GetModuleBuildDirectoryList(self):\r
+        DirList = []\r
+        for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:\r
+            if not ModuleAutoGen.IsBinaryModule:\r
+                DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))\r
+        return DirList\r
+\r
+    ## Get the root directory list for intermediate files of all libraries build\r
+    #\r
+    #   @retval     list    The list of directory\r
+    #\r
+    def GetLibraryBuildDirectoryList(self):\r
+        DirList = []\r
+        for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:\r
+            if not LibraryAutoGen.IsBinaryModule and not LibraryAutoGen.CanSkipbyHash():\r
+                DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))\r
+        return DirList\r
+\r
+## TopLevelMakefile class\r
+#\r
+#  This class encapsules makefie and its generation for entrance makefile. It\r
+# uses template to generate the content of makefile. The content of makefile\r
+# will be got from WorkspaceAutoGen object.\r
+#\r
+class TopLevelMakefile(BuildFile):\r
+    ## template used to generate toplevel makefile\r
+    _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}''')\r
+\r
+    ## Constructor of TopLevelMakefile\r
+    #\r
+    #   @param  Workspace   Object of WorkspaceAutoGen class\r
+    #\r
+    def __init__(self, Workspace):\r
+        BuildFile.__init__(self, Workspace)\r
+        self.IntermediateDirectoryList = []\r
+\r
+    # Compose a dict object containing information used to do replacement in template\r
+    @property\r
+    def _TemplateDict(self):\r
+        Separator = self._SEP_[self._FileType]\r
+\r
+        # any platform autogen object is ok because we just need common information\r
+        MyAgo = self._AutoGenObject\r
+\r
+        if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:\r
+            EdkLogger.error("build", OPTION_MISSING, "No MAKE command defined. Please check your tools_def.txt!",\r
+                            ExtraData="[%s]" % str(MyAgo))\r
+\r
+        for Arch in MyAgo.ArchList:\r
+            self.IntermediateDirectoryList.append(Separator.join(["$(BUILD_DIR)", Arch]))\r
+        self.IntermediateDirectoryList.append("$(FV_DIR)")\r
+\r
+        # TRICK: for not generating GenFds call in makefile if no FDF file\r
+        MacroList = []\r
+        if MyAgo.FdfFile is not None and MyAgo.FdfFile != "":\r
+            FdfFileList = [MyAgo.FdfFile]\r
+            # macros passed to GenFds\r
+            MacroDict = {}\r
+            MacroDict.update(GlobalData.gGlobalDefines)\r
+            MacroDict.update(GlobalData.gCommandLineDefines)\r
+            for MacroName in MacroDict:\r
+                if MacroDict[MacroName] != "":\r
+                    MacroList.append('"%s=%s"' % (MacroName, MacroDict[MacroName].replace('\\', '\\\\')))\r
+                else:\r
+                    MacroList.append('"%s"' % MacroName)\r
+        else:\r
+            FdfFileList = []\r
+\r
+        # pass extra common options to external program called in makefile, currently GenFds.exe\r
+        ExtraOption = ''\r
+        LogLevel = EdkLogger.GetLevel()\r
+        if LogLevel == EdkLogger.VERBOSE:\r
+            ExtraOption += " -v"\r
+        elif LogLevel <= EdkLogger.DEBUG_9:\r
+            ExtraOption += " -d %d" % (LogLevel - 1)\r
+        elif LogLevel == EdkLogger.QUIET:\r
+            ExtraOption += " -q"\r
+\r
+        if GlobalData.gCaseInsensitive:\r
+            ExtraOption += " -c"\r
+        if GlobalData.gEnableGenfdsMultiThread:\r
+            ExtraOption += " --genfds-multi-thread"\r
+        if GlobalData.gIgnoreSource:\r
+            ExtraOption += " --ignore-sources"\r
+\r
+        for pcd in GlobalData.BuildOptionPcd:\r
+            if pcd[2]:\r
+                pcdname = '.'.join(pcd[0:3])\r
+            else:\r
+                pcdname = '.'.join(pcd[0:2])\r
+            if pcd[3].startswith('{'):\r
+                ExtraOption += " --pcd " + pcdname + '=' + 'H' + '"' + pcd[3] + '"'\r
+            else:\r
+                ExtraOption += " --pcd " + pcdname + '=' + pcd[3]\r
+\r
+        MakefileName = self._FILE_NAME_[self._FileType]\r
+        SubBuildCommandList = []\r
+        for A in MyAgo.ArchList:\r
+            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join("$(BUILD_DIR)", A, MakefileName)}\r
+            SubBuildCommandList.append(Command)\r
+\r
+        MakefileTemplateDict = {\r
+            "makefile_header"           : self._FILE_HEADER_[self._FileType],\r
+            "makefile_path"             : os.path.join("$(BUILD_DIR)", MakefileName),\r
+            "make_path"                 : MyAgo.ToolDefinition["MAKE"]["PATH"],\r
+            "platform_name"             : MyAgo.Name,\r
+            "platform_guid"             : MyAgo.Guid,\r
+            "platform_version"          : MyAgo.Version,\r
+            "platform_build_directory"  : MyAgo.BuildDir,\r
+            "conf_directory"            : GlobalData.gConfDirectory,\r
+\r
+            "toolchain_tag"             : MyAgo.ToolChain,\r
+            "build_target"              : MyAgo.BuildTarget,\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._FileType].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._FileType].values()),\r
+            'arch'                      : list(MyAgo.ArchList),\r
+            "build_architecture_list"   : ','.join(MyAgo.ArchList),\r
+            "separator"                 : Separator,\r
+            "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),\r
+            "cleanall_command"          : self.GetRemoveDirectoryCommand(self.IntermediateDirectoryList),\r
+            "sub_build_command"         : SubBuildCommandList,\r
+            "fdf_file"                  : FdfFileList,\r
+            "active_platform"           : str(MyAgo),\r
+            "fd"                        : MyAgo.FdTargetList,\r
+            "fv"                        : MyAgo.FvTargetList,\r
+            "cap"                       : MyAgo.CapTargetList,\r
+            "extra_options"             : ExtraOption,\r
+            "macro"                     : MacroList,\r
+        }\r
+\r
+        return MakefileTemplateDict\r
+\r
+    ## Get the root directory list for intermediate files of all modules build\r
+    #\r
+    #   @retval     list    The list of directory\r
+    #\r
+    def GetModuleBuildDirectoryList(self):\r
+        DirList = []\r
+        for ModuleAutoGen in self._AutoGenObject.ModuleAutoGenList:\r
+            if not ModuleAutoGen.IsBinaryModule:\r
+                DirList.append(os.path.join(self._AutoGenObject.BuildDir, ModuleAutoGen.BuildDir))\r
+        return DirList\r
+\r
+    ## Get the root directory list for intermediate files of all libraries build\r
+    #\r
+    #   @retval     list    The list of directory\r
+    #\r
+    def GetLibraryBuildDirectoryList(self):\r
+        DirList = []\r
+        for LibraryAutoGen in self._AutoGenObject.LibraryAutoGenList:\r
+            if not LibraryAutoGen.IsBinaryModule and not LibraryAutoGen.CanSkipbyHash():\r
+                DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))\r
+        return DirList\r
+\r
+# This acts like the main() function for the script, unless it is 'import'ed into another script.\r
+if __name__ == '__main__':\r
+    pass\r
+\r