]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/GenMake.py
BaseTools: Limit command line length.
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / GenMake.py
old mode 100644 (file)
new mode 100755 (executable)
index 35ee98c..1cfac1c
@@ -1,14 +1,9 @@
 ## @file\r
 # Create makefile for MS nmake and GNU make\r
 #\r
-# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r
-# This program and the accompanying materials\r
-# are licensed and made available under the terms and conditions of the BSD License\r
-# which accompanies this distribution.  The full text of the license may be found at\r
-# http://opensource.org/licenses/bsd-license.php\r
-#\r
-# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+# Copyright (c) 2007 - 2020, Intel Corporation. All rights reserved.<BR>\r
+# Copyright (c) 2020, ARM Limited. All rights reserved.<BR>\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
 ## Import Modules\r
@@ -30,7 +25,7 @@ from collections import OrderedDict
 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
+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
@@ -58,13 +53,10 @@ gIncludeMacroConversion = {
   "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
+NMAKE_FILETYPE = "nmake"\r
+GMAKE_FILETYPE = "gmake"\r
+WIN32_PLATFORM = "win32"\r
+POSIX_PLATFORM = "posix"\r
 \r
 ## BuildFile class\r
 #\r
@@ -79,10 +71,17 @@ class BuildFile(object):
 \r
     ## default file name for each type of build file\r
     _FILE_NAME_ = {\r
-        "nmake" :   "Makefile",\r
-        "gmake" :   "GNUmakefile"\r
+        NMAKE_FILETYPE :   "Makefile",\r
+        GMAKE_FILETYPE :   "GNUmakefile"\r
     }\r
 \r
+    # Get Makefile name.\r
+    def getMakefileName(self):\r
+        if not self._FileType:\r
+            return self._DEFAULT_FILE_NAME_\r
+        else:\r
+            return self._FILE_NAME_[self._FileType]\r
+\r
     ## Fixed header string for makefile\r
     _MAKEFILE_HEADER = '''#\r
 # DO NOT EDIT\r
@@ -100,8 +99,8 @@ class BuildFile(object):
 \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
+        NMAKE_FILETYPE :   _MAKEFILE_HEADER % _FILE_NAME_[NMAKE_FILETYPE],\r
+        GMAKE_FILETYPE :   _MAKEFILE_HEADER % _FILE_NAME_[GMAKE_FILETYPE]\r
     }\r
 \r
     ## shell commands which can be used in build file in the form of macro\r
@@ -112,7 +111,7 @@ class BuildFile(object):
     #   $(RD)     remove dir command\r
     #\r
     _SHELL_CMD_ = {\r
-        "nmake" : {\r
+        WIN32_PLATFORM : {\r
             "CP"    :   "copy /y",\r
             "MV"    :   "move /y",\r
             "RM"    :   "del /f /q",\r
@@ -120,7 +119,7 @@ class BuildFile(object):
             "RD"    :   "rmdir /s /q",\r
         },\r
 \r
-        "gmake" : {\r
+        POSIX_PLATFORM : {\r
             "CP"    :   "cp -f",\r
             "MV"    :   "mv -f",\r
             "RM"    :   "rm -f",\r
@@ -131,43 +130,43 @@ class BuildFile(object):
 \r
     ## directory separator\r
     _SEP_ = {\r
-        "nmake" :   "\\",\r
-        "gmake" :   "/"\r
+        WIN32_PLATFORM :   "\\",\r
+        POSIX_PLATFORM :   "/"\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
+        WIN32_PLATFORM :   'if not exist %(dir)s $(MD) %(dir)s',\r
+        POSIX_PLATFORM :   "$(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
+        WIN32_PLATFORM :   'if exist %(dir)s $(RD) %(dir)s',\r
+        POSIX_PLATFORM :   "$(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
+        WIN32_PLATFORM :   'if exist %(Src)s $(CP) %(Src)s %(Dst)s',\r
+        POSIX_PLATFORM :   "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
+        WIN32_PLATFORM :   'if exist %(dir)s cd %(dir)s',\r
+        POSIX_PLATFORM :   "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
+        WIN32_PLATFORM :   'if exist %(file)s "$(MAKE)" $(MAKE_FLAGS) -f %(file)s',\r
+        POSIX_PLATFORM :   'test -e %(file)s && "$(MAKE)" $(MAKE_FLAGS) -f %(file)s'\r
     }\r
 \r
     _INCLUDE_CMD_ = {\r
-        "nmake" :   '!INCLUDE',\r
-        "gmake" :   "include"\r
+        NMAKE_FILETYPE :   '!INCLUDE',\r
+        GMAKE_FILETYPE :   "include"\r
     }\r
 \r
-    _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I", "GCC" : "-I", "INTEL" : "-I", "RVCT" : "-I"}\r
+    _INC_FLAG_ = {TAB_COMPILER_MSFT : "/I", "GCC" : "-I", "INTEL" : "-I", "RVCT" : "-I", "NASM" : "-I"}\r
 \r
     ## Constructor of BuildFile\r
     #\r
@@ -175,22 +174,39 @@ class BuildFile(object):
     #\r
     def __init__(self, AutoGenObject):\r
         self._AutoGenObject = AutoGenObject\r
-        self._FileType = gMakeType\r
 \r
-    ## Create build file\r
+        MakePath = AutoGenObject.BuildOption.get('MAKE', {}).get('PATH')\r
+        if not MakePath:\r
+            self._FileType = ""\r
+        elif "nmake" in MakePath:\r
+            self._FileType = NMAKE_FILETYPE\r
+        else:\r
+            self._FileType = "gmake"\r
+\r
+        if sys.platform == "win32":\r
+            self._Platform = WIN32_PLATFORM\r
+        else:\r
+            self._Platform = POSIX_PLATFORM\r
+\r
+    ## Create build file.\r
     #\r
-    #   @param  FileType    Type of build file. Only nmake and gmake are supported now.\r
+    #  Only nmake and gmake are supported.\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
+    #  @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
+    def Generate(self):\r
         FileContent = self._TEMPLATE_.Replace(self._TemplateDict)\r
-        FileName = self._FILE_NAME_[FileType]\r
+        FileName = self.getMakefileName()\r
+        if not os.path.exists(os.path.join(self._AutoGenObject.MakeFileDir, "deps.txt")):\r
+            with open(os.path.join(self._AutoGenObject.MakeFileDir, "deps.txt"),"w+") as fd:\r
+                fd.write("")\r
+        if not os.path.exists(os.path.join(self._AutoGenObject.MakeFileDir, "dependency")):\r
+            with open(os.path.join(self._AutoGenObject.MakeFileDir, "dependency"),"w+") as fd:\r
+                fd.write("")\r
+        if not os.path.exists(os.path.join(self._AutoGenObject.MakeFileDir, "deps_target")):\r
+            with open(os.path.join(self._AutoGenObject.MakeFileDir, "deps_target"),"w+") as fd:\r
+                fd.write("")\r
         return SaveFileOnChange(os.path.join(self._AutoGenObject.MakeFileDir, FileName), FileContent, False)\r
 \r
     ## Return a list of directory creation command string\r
@@ -200,7 +216,7 @@ class BuildFile(object):
     #   @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
+        return [self._MD_TEMPLATE_[self._Platform] % {'dir':Dir} for Dir in DirList]\r
 \r
     ## Return a list of directory removal command string\r
     #\r
@@ -209,12 +225,14 @@ class BuildFile(object):
     #   @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
+        return [self._RD_TEMPLATE_[self._Platform] % {'dir':Dir} for Dir in DirList]\r
 \r
-    def PlaceMacro(self, Path, MacroDefinitions={}):\r
+    def PlaceMacro(self, Path, MacroDefinitions=None):\r
         if Path.startswith("$("):\r
             return Path\r
         else:\r
+            if MacroDefinitions is None:\r
+                MacroDefinitions = {}\r
             PathLength = len(Path)\r
             for MacroName in MacroDefinitions:\r
                 MacroValue = MacroDefinitions[MacroName]\r
@@ -308,9 +326,6 @@ MAKE_FILE = ${makefile_path}
 ${BEGIN}${file_macro}\r
 ${END}\r
 \r
-COMMON_DEPS = ${BEGIN}${common_dependency_file} \\\r
-              ${END}\r
-\r
 #\r
 # Overridable Target Macro Definitions\r
 #\r
@@ -386,6 +401,8 @@ gen_fds:
 \t@"$(MAKE)" $(MAKE_FLAGS) -f $(BUILD_DIR)${separator}${makefile_name} fds\r
 \t@cd $(MODULE_BUILD_DIR)\r
 \r
+${INCLUDETAG}\r
+\r
 #\r
 # Individual Object Build Targets\r
 #\r
@@ -435,7 +452,7 @@ cleanlib:
         self.CommonFileDependency = []\r
         self.FileListMacros = {}\r
         self.ListFileMacros = {}\r
-\r
+        self.ObjTargetDict = OrderedDict()\r
         self.FileCache = {}\r
         self.LibraryBuildCommandList = []\r
         self.LibraryFileList = []\r
@@ -453,14 +470,13 @@ cleanlib:
         self.GenFfsList                 = ModuleAutoGen.GenFfsList\r
         self.MacroList = ['FFS_OUTPUT_DIR', 'MODULE_GUID', 'OUTPUT_DIR']\r
         self.FfsOutputFileList = []\r
+        self.DependencyHeaderFileSet = set()\r
 \r
     # Compose a dict object containing information used to do replacement in template\r
-    def _CreateTemplateDict(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
+    @property\r
+    def _TemplateDict(self):\r
         MyAgo = self._AutoGenObject\r
-        Separator = self._SEP_[self._FileType]\r
+        Separator = self._SEP_[self._Platform]\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
@@ -475,23 +491,16 @@ cleanlib:
         else:\r
             ModuleEntryPoint = "_ModuleEntryPoint"\r
 \r
-        # Intel EBC compiler enforces EfiMain\r
-        if MyAgo.AutoGenVersion < 0x00010005 and MyAgo.Arch == "EBC":\r
-            ArchEntryPoint = "EfiMain"\r
-        else:\r
-            ArchEntryPoint = ModuleEntryPoint\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
-        elif MyAgo.AutoGenVersion < 0x00010005:\r
-            # Edk modules use entry point specified in INF file\r
-            ImageEntryPoint = ModuleEntryPoint\r
         else:\r
             # EdkII modules always use "_ModuleEntryPoint" as entry point\r
             ImageEntryPoint = "_ModuleEntryPoint"\r
 \r
-        for k, v in MyAgo.Module.Defines.iteritems():\r
+        for k, v in MyAgo.Module.Defines.items():\r
             if k not in MyAgo.Macros:\r
                 MyAgo.Macros[k] = v\r
 \r
@@ -503,7 +512,7 @@ cleanlib:
             MyAgo.Macros['IMAGE_ENTRY_POINT'] = ImageEntryPoint\r
 \r
         PCI_COMPRESS_Flag = False\r
-        for k, v in MyAgo.Module.Defines.iteritems():\r
+        for k, v in MyAgo.Module.Defines.items():\r
             if 'PCI_COMPRESS' == k and 'TRUE' == v:\r
                 PCI_COMPRESS_Flag = True\r
 \r
@@ -546,7 +555,7 @@ cleanlib:
                 UnexpandMacro = []\r
                 NewStr = []\r
                 for Str in StrList:\r
-                    if '$' in Str:\r
+                    if '$' in Str or '-MMD' in Str or '-MF' in Str:\r
                         UnexpandMacro.append(Str)\r
                     else:\r
                         NewStr.append(Str)\r
@@ -554,8 +563,8 @@ cleanlib:
                 NewRespStr = ' '.join(NewStr)\r
                 SaveFileOnChange(RespFile, NewRespStr, False)\r
                 ToolsDef.append("%s = %s" % (Resp, UnexpandMacroStr + ' @' + RespFile))\r
-                RespFileListContent += '@' + RespFile + os.linesep\r
-                RespFileListContent += NewRespStr + os.linesep\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
@@ -567,7 +576,7 @@ cleanlib:
             EdkLogger.error("build", AUTOGEN_ERROR, "Nothing to build",\r
                             ExtraData="[%s]" % str(MyAgo))\r
 \r
-        self.ProcessBuildTargetList()\r
+        self.ProcessBuildTargetList(MyAgo.OutputDir, ToolsDef)\r
         self.ParserGenerateFfsCmd()\r
 \r
         # Generate macros used to represent input files\r
@@ -595,6 +604,23 @@ cleanlib:
                                                 }\r
                                                 )\r
         FileMacroList.append(FileMacro)\r
+        # Add support when compiling .nasm source files\r
+        IncludePathList = []\r
+        asmsource = [item for item in MyAgo.SourceFileList if item.File.upper().endswith((".NASM",".ASM",".NASMB","S"))]\r
+        if asmsource:\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
+                # In nmake makfiles, a trailing literal backslash must be escaped with a caret ('^').\r
+                # It is otherwise replaced with a space (' '). This is not necessary for GNU makfefiles.\r
+                if P == MyAgo.IncludePathList[-1] and self._Platform == WIN32_PLATFORM and self._FileType == NMAKE_FILETYPE:\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
 \r
         # Generate macros used to represent files containing list of input files\r
         for ListFileMacro in self.ListFileMacros:\r
@@ -606,17 +632,17 @@ cleanlib:
                 False\r
                 )\r
 \r
-        # Edk modules need <BaseName>StrDefs.h for string ID\r
-        #if MyAgo.AutoGenVersion < 0x00010005 and len(MyAgo.UnicodeFileList) > 0:\r
-        #    BcTargetList = ['strdefs']\r
-        #else:\r
-        #    BcTargetList = []\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
+        MakefileName = self.getMakefileName()\r
         LibraryMakeCommandList = []\r
         for D in self.LibraryBuildDirectoryList:\r
-            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join(D, MakefileName)}\r
+            Command = self._MAKE_TEMPLATE_[self._Platform] % {"file":os.path.join(D, MakefileName)}\r
             LibraryMakeCommandList.append(Command)\r
 \r
         package_rel_dir = MyAgo.SourceDir\r
@@ -654,7 +680,7 @@ cleanlib:
             "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.iteritems()],\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
@@ -668,8 +694,8 @@ cleanlib:
             "separator"                 : Separator,\r
             "module_tool_definitions"   : ToolsDef,\r
 \r
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),\r
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._Platform].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._Platform].values()),\r
 \r
             "module_entry_point"        : ModuleEntryPoint,\r
             "image_entry_point"         : ImageEntryPoint,\r
@@ -684,6 +710,9 @@ cleanlib:
             "file_macro"                : FileMacroList,\r
             "file_build_target"         : self.BuildTargetList,\r
             "backward_compatible_target": BcTargetList,\r
+            "INCLUDETAG"                   : "\n".join([self._INCLUDE_CMD_[self._FileType] + " " + os.path.join("$(MODULE_BUILD_DIR)","dependency"),\r
+                                                              self._INCLUDE_CMD_[self._FileType] + " " + os.path.join("$(MODULE_BUILD_DIR)","deps_target")\r
+                                                              ])\r
         }\r
 \r
         return MakefileTemplateDict\r
@@ -702,14 +731,14 @@ cleanlib:
                     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
+                        self.BuildTargetList.append("%s : %s" %(Dst,Src))\r
+                        self.BuildTargetList.append('\t' + self._CP_TEMPLATE_[self._Platform] %{'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:\r
+                if '-i' == Str or "-oi" == Str:\r
                     if DepsFileList == []:\r
                         DepsFileList = [FfsCmdList[index + 1]]\r
                     else:\r
@@ -757,8 +786,10 @@ cleanlib:
 \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
+            if self._AutoGenObject.Macros[Macro] and os.path.normcase(self._AutoGenObject.Macros[Macro]) in os.path.normcase(str):\r
+                replace_dir = str[os.path.normcase(str).index(os.path.normcase(self._AutoGenObject.Macros[Macro])): os.path.normcase(str).index(\r
+                    os.path.normcase(self._AutoGenObject.Macros[Macro])) + len(self._AutoGenObject.Macros[Macro])]\r
+                str = str.replace(replace_dir, '$(' + Macro + ')')\r
         return str\r
 \r
     def CommandExceedLimit(self):\r
@@ -872,7 +903,7 @@ cleanlib:
                                     BuildTargets[Target].Commands[i] = SingleCommand.replace('$(INC)', '').replace(FlagDict[Flag]['Macro'], RespMacro)\r
         return RespDict\r
 \r
-    def ProcessBuildTargetList(self):\r
+    def ProcessBuildTargetList(self, RespFile, ToolsDef):\r
         #\r
         # Search dependency file list for each source file\r
         #\r
@@ -891,45 +922,89 @@ cleanlib:
                 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
-        DepSet = None\r
+        FileDependencyDict = {item:ForceIncludedFile for item in SourceFileList}\r
+\r
+        for Dependency in FileDependencyDict.values():\r
+            self.DependencyHeaderFileSet.update(set(Dependency))\r
+\r
+        # Get a set of unique package includes from MetaFile\r
+        parentMetaFileIncludes = set()\r
+        for aInclude in self._AutoGenObject.PackageIncludePathList:\r
+            aIncludeName = str(aInclude)\r
+            parentMetaFileIncludes.add(aIncludeName.lower())\r
+\r
+        # Check if header files are listed in metafile\r
+        # Get a set 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 set 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 set of unique module dependency header files\r
+        # Exclude autogen files and files not in the source directory\r
+        # and files that are under the package include list\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
+                # Exclude non-header files\r
+                if not aFileName.endswith('.h'):\r
+                    continue\r
+                # Exclude autogen files\r
+                if aFileName in localAutoGenFileSet:\r
+                    continue\r
+                # Exclude include out of local scope\r
+                if localSourceDir not in aFileName:\r
+                    continue\r
+                # Exclude files covered by package includes\r
+                pathNeeded = True\r
+                for aIncludePath in parentMetaFileIncludes:\r
+                    if aIncludePath in aFileName:\r
+                        pathNeeded = False\r
+                        break\r
+                if not pathNeeded:\r
+                    continue\r
+                # Keep the file to be checked\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
+            if GlobalData.gUseHashCache:\r
+                GlobalData.gModuleBuildTracking[self._AutoGenObject] = 'FAIL_METAFILE'\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
         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
-        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
+        CmdSumDict = {}\r
+        CmdTargetDict = {}\r
+        CmdCppDict = {}\r
+        DependencyDict = FileDependencyDict.copy()\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
+            resp_file_number = 0\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
@@ -940,9 +1015,12 @@ cleanlib:
                     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
@@ -952,27 +1030,110 @@ cleanlib:
                     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 separater, even on Windows\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
+                for key in self.FileListMacros:\r
+                    self.FileListMacros[key].sort()\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
-                TargetDict = {\r
-                    "target"    :   self.PlaceMacro(T.Target.Path, self.Macros),\r
-                    "cmd"       :   "\n\t".join(T.Commands),\r
-                    "deps"      :   Deps\r
-                }\r
-                self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(TargetDict))\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,\r
+                                                                                   CmdCppDict, DependencyDict, RespFile,\r
+                                                                                   ToolsDef, resp_file_number)\r
+                    resp_file_number += 1\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
+                    # Add a Makefile rule for targets generating multiple files.\r
+                    # The main output is a prerequisite for the other output files.\r
+                    for i in T.Outputs[1:]:\r
+                        AnnexeTargetDict = {"target": self.PlaceMacro(i.Path, self.Macros), "cmd": "", "deps": self.PlaceMacro(T.Target.Path, self.Macros)}\r
+                        self.BuildTargetList.append(self._BUILD_TARGET_TEMPLATE.Replace(AnnexeTargetDict))\r
+\r
+    def ParserCCodeFile(self, T, Type, CmdSumDict, CmdTargetDict, CmdCppDict, DependencyDict, RespFile, ToolsDef,\r
+                            resp_file_number):\r
+        SaveFilePath = os.path.join(RespFile, "cc_resp_%s.txt" % resp_file_number)\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:\r
+                        continue\r
+                    if CmdSign not in list(CmdTargetDict.keys()):\r
+                        cmd = Item.replace(Temp, CmdSign)\r
+                        if SingleCommandList[-1] in cmd:\r
+                            CmdTargetDict[CmdSign] = [cmd.replace(SingleCommandList[-1], "").rstrip(), SingleCommandList[-1]]\r
+                    else:\r
+                        # CmdTargetDict[CmdSign] = "%s %s" % (CmdTargetDict[CmdSign], SingleCommandList[-1])\r
+                        CmdTargetDict[CmdSign].append(SingleCommandList[-1])\r
+                    Index = CommandList.index(Item)\r
+                    CommandList.pop(Index)\r
+                    if SingleCommandList[-1].endswith("%s%s.c" % (TAB_SLASH, CmdSumDict[CmdSign[3:].rsplit(TAB_SLASH, 1)[0]])):\r
+                        Cpplist = CmdCppDict[T.Target.SubDir]\r
+                        Cpplist.insert(0, '$(OBJLIST_%d): ' % list(self.ObjTargetDict.keys()).index(T.Target.SubDir))\r
+                        source_files = CmdTargetDict[CmdSign][1:]\r
+                        source_files.insert(0, " ")\r
+                        if len(source_files)>2:\r
+                            SaveFileOnChange(SaveFilePath, " ".join(source_files), False)\r
+                            T.Commands[Index] = '%s\n\t%s $(cc_resp_%s)' % (\r
+                            ' \\\n\t'.join(Cpplist), CmdTargetDict[CmdSign][0], resp_file_number)\r
+                            ToolsDef.append("cc_resp_%s = @%s" % (resp_file_number, SaveFilePath))\r
+\r
+                        elif len(source_files)<=2 and len(" ".join(CmdTargetDict[CmdSign][:2]))>GlobalData.gCommandMaxLength:\r
+                            SaveFileOnChange(SaveFilePath, " ".join(source_files), False)\r
+                            T.Commands[Index] = '%s\n\t%s $(cc_resp_%s)' % (\r
+                                ' \\\n\t'.join(Cpplist), CmdTargetDict[CmdSign][0], resp_file_number)\r
+                            ToolsDef.append("cc_resp_%s = @%s" % (resp_file_number, SaveFilePath))\r
 \r
+                        else:\r
+                            T.Commands[Index] = '%s\n\t%s' % (' \\\n\t'.join(Cpplist), " ".join(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
@@ -990,112 +1151,9 @@ cleanlib:
     def GetFileDependency(self, FileList, ForceInculeList, SearchPathList):\r
         Dependency = {}\r
         for F in FileList:\r
-            Dependency[F] = self.GetDependencyList(F, ForceInculeList, SearchPathList)\r
+            Dependency[F] = GetDependencyList(self._AutoGenObject, self.FileCache, 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 dependecies 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, 'r')\r
-                except BaseException as X:\r
-                    EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F.Path + "\n\t" + str(X))\r
-\r
-                FileContent = Fd.read()\r
-                Fd.close()\r
-                if len(FileContent) == 0:\r
-                    continue\r
-\r
-                if FileContent[0] == 0xff or FileContent[0] == 0xfe:\r
-                    FileContent = unicode(FileContent, "utf-16")\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
-    _TemplateDict = property(_CreateTemplateDict)\r
 \r
 ## CustomMakefile class\r
 #\r
@@ -1203,10 +1261,12 @@ ${BEGIN}\t-@${create_directory_command}\n${END}\
         BuildFile.__init__(self, ModuleAutoGen)\r
         self.PlatformInfo = self._AutoGenObject.PlatformInfo\r
         self.IntermediateDirectoryList = ["$(DEBUG_DIR)", "$(OUTPUT_DIR)"]\r
+        self.DependencyHeaderFileSet = set()\r
 \r
     # Compose a dict object containing information used to do replacement in template\r
-    def _CreateTemplateDict(self):\r
-        Separator = self._SEP_[self._FileType]\r
+    @property\r
+    def _TemplateDict(self):\r
+        Separator = self._SEP_[self._Platform]\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
@@ -1236,7 +1296,7 @@ ${BEGIN}\t-@${create_directory_command}\n${END}\
                     ToolsDef.append("%s_%s = %s" % (Tool, Attr, MyAgo.BuildOption[Tool][Attr]))\r
             ToolsDef.append("")\r
 \r
-        MakefileName = self._FILE_NAME_[self._FileType]\r
+        MakefileName = self.getMakefileName()\r
         MakefileTemplateDict = {\r
             "makefile_header"           : self._FILE_HEADER_[self._FileType],\r
             "makefile_path"             : os.path.join("$(MODULE_BUILD_DIR)", MakefileName),\r
@@ -1269,8 +1329,8 @@ ${BEGIN}\t-@${create_directory_command}\n${END}\
             "separator"                 : Separator,\r
             "module_tool_definitions"   : ToolsDef,\r
 \r
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),\r
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._Platform].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._Platform].values()),\r
 \r
             "create_directory_command"  : self.GetCreateDirectoryCommand(self.IntermediateDirectoryList),\r
             "custom_makefile_content"   : CustomMakefile\r
@@ -1278,8 +1338,6 @@ ${BEGIN}\t-@${create_directory_command}\n${END}\
 \r
         return MakefileTemplateDict\r
 \r
-    _TemplateDict = property(_CreateTemplateDict)\r
-\r
 ## PlatformMakefile class\r
 #\r
 #  This class encapsules makefie and its generation for platform. It uses\r
@@ -1394,10 +1452,12 @@ cleanlib:
         self.ModuleBuildDirectoryList = []\r
         self.LibraryBuildDirectoryList = []\r
         self.LibraryMakeCommandList = []\r
+        self.DependencyHeaderFileSet = set()\r
 \r
     # Compose a dict object containing information used to do replacement in template\r
-    def _CreateTemplateDict(self):\r
-        Separator = self._SEP_[self._FileType]\r
+    @property\r
+    def _TemplateDict(self):\r
+        Separator = self._SEP_[self._Platform]\r
 \r
         MyAgo = self._AutoGenObject\r
         if "MAKE" not in MyAgo.ToolDefinition or "PATH" not in MyAgo.ToolDefinition["MAKE"]:\r
@@ -1408,13 +1468,13 @@ cleanlib:
         self.ModuleBuildDirectoryList = self.GetModuleBuildDirectoryList()\r
         self.LibraryBuildDirectoryList = self.GetLibraryBuildDirectoryList()\r
 \r
-        MakefileName = self._FILE_NAME_[self._FileType]\r
+        MakefileName = self.getMakefileName()\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
+            Command = self._MAKE_TEMPLATE_[self._Platform] % {"file":Makefile}\r
             LibraryMakefileList.append(Makefile)\r
             LibraryMakeCommandList.append(Command)\r
         self.LibraryMakeCommandList = LibraryMakeCommandList\r
@@ -1424,7 +1484,7 @@ cleanlib:
         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
+            Command = self._MAKE_TEMPLATE_[self._Platform] % {"file":Makefile}\r
             ModuleMakefileList.append(Makefile)\r
             ModuleMakeCommandList.append(Command)\r
 \r
@@ -1444,8 +1504,8 @@ cleanlib:
 \r
             "toolchain_tag"             : MyAgo.ToolChain,\r
             "build_target"              : MyAgo.BuildTarget,\r
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),\r
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._Platform].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._Platform].values()),\r
             "build_architecture_list"   : MyAgo.Arch,\r
             "architecture"              : MyAgo.Arch,\r
             "separator"                 : Separator,\r
@@ -1481,8 +1541,6 @@ cleanlib:
                 DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))\r
         return DirList\r
 \r
-    _TemplateDict = property(_CreateTemplateDict)\r
-\r
 ## TopLevelMakefile class\r
 #\r
 #  This class encapsules makefie and its generation for entrance makefile. It\r
@@ -1500,10 +1558,12 @@ class TopLevelMakefile(BuildFile):
     def __init__(self, Workspace):\r
         BuildFile.__init__(self, Workspace)\r
         self.IntermediateDirectoryList = []\r
+        self.DependencyHeaderFileSet = set()\r
 \r
     # Compose a dict object containing information used to do replacement in template\r
-    def _CreateTemplateDict(self):\r
-        Separator = self._SEP_[self._FileType]\r
+    @property\r
+    def _TemplateDict(self):\r
+        Separator = self._SEP_[self._Platform]\r
 \r
         # any platform autogen object is ok because we just need common information\r
         MyAgo = self._AutoGenObject\r
@@ -1521,13 +1581,9 @@ class TopLevelMakefile(BuildFile):
         if MyAgo.FdfFile is not None and MyAgo.FdfFile != "":\r
             FdfFileList = [MyAgo.FdfFile]\r
             # macros passed to GenFds\r
-            MacroList.append('"%s=%s"' % ("EFI_SOURCE", GlobalData.gEfiSource.replace('\\', '\\\\')))\r
-            MacroList.append('"%s=%s"' % ("EDK_SOURCE", GlobalData.gEdkSource.replace('\\', '\\\\')))\r
             MacroDict = {}\r
             MacroDict.update(GlobalData.gGlobalDefines)\r
             MacroDict.update(GlobalData.gCommandLineDefines)\r
-            MacroDict.pop("EFI_SOURCE", "dummy")\r
-            MacroDict.pop("EDK_SOURCE", "dummy")\r
             for MacroName in MacroDict:\r
                 if MacroDict[MacroName] != "":\r
                     MacroList.append('"%s=%s"' % (MacroName, MacroDict[MacroName].replace('\\', '\\\\')))\r
@@ -1548,8 +1604,8 @@ class TopLevelMakefile(BuildFile):
 \r
         if GlobalData.gCaseInsensitive:\r
             ExtraOption += " -c"\r
-        if GlobalData.gEnableGenfdsMultiThread:\r
-            ExtraOption += " --genfds-multi-thread"\r
+        if not GlobalData.gEnableGenfdsMultiThread:\r
+            ExtraOption += " --no-genfds-multi-thread"\r
         if GlobalData.gIgnoreSource:\r
             ExtraOption += " --ignore-sources"\r
 \r
@@ -1563,10 +1619,10 @@ class TopLevelMakefile(BuildFile):
             else:\r
                 ExtraOption += " --pcd " + pcdname + '=' + pcd[3]\r
 \r
-        MakefileName = self._FILE_NAME_[self._FileType]\r
+        MakefileName = self.getMakefileName()\r
         SubBuildCommandList = []\r
         for A in MyAgo.ArchList:\r
-            Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":os.path.join("$(BUILD_DIR)", A, MakefileName)}\r
+            Command = self._MAKE_TEMPLATE_[self._Platform] % {"file":os.path.join("$(BUILD_DIR)", A, MakefileName)}\r
             SubBuildCommandList.append(Command)\r
 \r
         MakefileTemplateDict = {\r
@@ -1581,8 +1637,8 @@ class TopLevelMakefile(BuildFile):
 \r
             "toolchain_tag"             : MyAgo.ToolChain,\r
             "build_target"              : MyAgo.BuildTarget,\r
-            "shell_command_code"        : self._SHELL_CMD_[self._FileType].keys(),\r
-            "shell_command"             : self._SHELL_CMD_[self._FileType].values(),\r
+            "shell_command_code"        : list(self._SHELL_CMD_[self._Platform].keys()),\r
+            "shell_command"             : list(self._SHELL_CMD_[self._Platform].values()),\r
             'arch'                      : list(MyAgo.ArchList),\r
             "build_architecture_list"   : ','.join(MyAgo.ArchList),\r
             "separator"                 : Separator,\r
@@ -1622,9 +1678,112 @@ class TopLevelMakefile(BuildFile):
                 DirList.append(os.path.join(self._AutoGenObject.BuildDir, LibraryAutoGen.BuildDir))\r
         return DirList\r
 \r
-    _TemplateDict = property(_CreateTemplateDict)\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(AutoGenObject, FileCache, 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 AutoGenObject.Arch not in gDependencyDatabase:\r
+        gDependencyDatabase[AutoGenObject.Arch] = {}\r
+    DepDb = gDependencyDatabase[AutoGenObject.Arch]\r
+\r
+    while len(FileStack) > 0:\r
+        F = FileStack.pop()\r
+\r
+        FullPathDependList = []\r
+        if F in FileCache:\r
+            for CacheFile in 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
+                        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
+        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
 # 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