Index: Python/AutoGen/AutoGen.py
===================================================================
--- Python/AutoGen/AutoGen.py	(revision 15809)
+++ Python/AutoGen/AutoGen.py	(working copy)
@@ -655,6 +655,8 @@
                                     )
                     Count += 1
                                       
+    def _GenFdsCommand(self):
+        return (GenMake.TopLevelMakefile(self)._TEMPLATE_.Replace(GenMake.TopLevelMakefile(self)._TemplateDict)).strip()
 
     ## Create makefile for the platform and modules in it
     #
@@ -662,15 +664,6 @@
     #                                       modules will be created as well
     #
     def CreateMakeFile(self, CreateDepsMakeFile=False):
-        # create makefile for platform
-        Makefile = GenMake.TopLevelMakefile(self)
-        if Makefile.Generate():
-            EdkLogger.debug(EdkLogger.DEBUG_9, "Generated makefile for platform [%s] %s\n" %
-                            (self.MetaFile, self.ArchList))
-        else:
-            EdkLogger.debug(EdkLogger.DEBUG_9, "Skipped the generation of makefile for platform [%s] %s\n" %
-                            (self.MetaFile, self.ArchList))
-
         if CreateDepsMakeFile:
             for Pa in self.AutoGenObjectList:
                 Pa.CreateMakeFile(CreateDepsMakeFile)
@@ -705,6 +698,7 @@
     FvDir               = property(_GetFvDir)
     MakeFileDir         = property(_GetMakeFileDir)
     BuildCommand        = property(_GetBuildCommand)
+    GenFdsCommand       = property(_GenFdsCommand)
 
 ## AutoGen class for platform
 #
@@ -801,6 +795,9 @@
         self._LibraryAutoGenList = None
         self._BuildCommand = None
 
+        # get library/modules for build
+        self.LibraryBuildDirectoryList = []
+        self.ModuleBuildDirectoryList = []
         # get the original module/package/platform objects
         self.BuildDatabase = Workspace.BuildDatabase
         return True
@@ -827,6 +824,10 @@
         # don't do this twice
         self.IsCodeFileCreated = True
 
+    ## Generate Fds Command
+    def _GenFdsCommand(self):
+        return self.Workspace.GenFdsCommand
+		
     ## Create makefile for the platform and mdoules in it
     #
     #   @param      CreateModuleMakeFile    Flag indicating if the makefile for
@@ -844,14 +845,11 @@
         if self.IsMakeFileCreated:
             return
 
-        # create makefile for platform
+        # create library/module build dirs for platform
         Makefile = GenMake.PlatformMakefile(self)
-        if Makefile.Generate():
-            EdkLogger.debug(EdkLogger.DEBUG_9, "Generated makefile for platform [%s] [%s]\n" %
-                            (self.MetaFile, self.Arch))
-        else:
-            EdkLogger.debug(EdkLogger.DEBUG_9, "Skipped the generation of makefile for platform [%s] [%s]\n" %
-                            (self.MetaFile, self.Arch))
+        self.LibraryBuildDirectoryList = Makefile.GetLibraryBuildDirectoryList()
+        self.ModuleBuildDirectoryList = Makefile.GetModuleBuildDirectoryList()
+
         self.IsMakeFileCreated = True
 
     ## Deal with Shared FixedAtBuild Pcds
Index: Python/AutoGen/GenMake.py
===================================================================
--- Python/AutoGen/GenMake.py	(revision 15809)
+++ Python/AutoGen/GenMake.py	(working copy)
@@ -1105,6 +1105,7 @@
         self.IntermediateDirectoryList = []
         self.ModuleBuildDirectoryList = []
         self.LibraryBuildDirectoryList = []
+        self.LibraryMakeCommandList = []
 
     # Compose a dict object containing information used to do replacement in template
     def _CreateTemplateDict(self):
@@ -1128,6 +1129,7 @@
             Command = self._MAKE_TEMPLATE_[self._FileType] % {"file":Makefile}
             LibraryMakefileList.append(Makefile)
             LibraryMakeCommandList.append(Command)
+        self.LibraryMakeCommandList = LibraryMakeCommandList
 
         ModuleMakefileList = []
         ModuleMakeCommandList = []
@@ -1198,100 +1200,8 @@
 #
 class TopLevelMakefile(BuildFile):
     ## template used to generate toplevel makefile
-    _TEMPLATE_ = TemplateString('''\
-${makefile_header}
+    _TEMPLATE_ = TemplateString('''${BEGIN}\tGenFds -f "${fdf_file}" -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}''')
 
-#
-# 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} ${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
Index: Python/build/build.py
===================================================================
--- Python/build/build.py	(revision 15809)
+++ Python/build/build.py	(working copy)
@@ -866,7 +866,7 @@
     #   @param  CreateDepModuleMakeFile     Flag used to indicate creating makefile
     #                                       for dependent modules/Libraries
     #
-    def _Build(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True):
+    def _BuildPa(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False):
         if AutoGenObject == None:
             return False
 
@@ -883,7 +883,6 @@
             if not self.SkipAutoGen or Target == 'genmake':
                 self.Progress.Start("Generating makefile")
                 AutoGenObject.CreateMakeFile(CreateDepsMakeFile)
-                AutoGenObject.CreateAsBuiltInf()
                 self.Progress.Stop("done!")
             if Target == "genmake":
                 return True
@@ -903,8 +902,164 @@
                                 (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch),
                             ExtraData=str(AutoGenObject))
 
+        makefile = GenMake.BuildFile(AutoGenObject)._FILE_NAME_[GenMake.gMakeType]
+
+        # genfds
+        if Target == 'fds':
+            LaunchCommand(AutoGenObject.GenFdsCommand, AutoGenObject.MakeFileDir)
+            return True
+
+        # run
+        if Target == 'run':
+            RunDir = os.path.normpath(os.path.join(AutoGenObject.BuildDir, 'IA32'))
+            Command = '.\SecMain'
+            os.chdir(RunDir)
+            LaunchCommand(Command, RunDir)
+            return True
+
+        # build modules
+        if BuildModule:
+            BuildCommand = BuildCommand + [Target]
+            LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)
+            self.CreateAsBuiltInf()
+            return True
+
+        # build library
+        if Target == 'libraries':
+            for Lib in AutoGenObject.LibraryBuildDirectoryList:
+                NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, makefile)), 'pbuild']
+                LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
+            return True
+
+        # build module
+        if Target == 'modules':
+            for Lib in AutoGenObject.LibraryBuildDirectoryList:
+                NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Lib, makefile)), 'pbuild']
+                LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
+            for Mod in AutoGenObject.ModuleBuildDirectoryList:
+                NewBuildCommand = BuildCommand + ['-f', os.path.normpath(os.path.join(Mod, makefile)), 'pbuild']
+                LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
+            self.CreateAsBuiltInf()
+            return True
+
+        # cleanlib
+        if Target == 'cleanlib':
+            for Lib in AutoGenObject.LibraryBuildDirectoryList:
+                LibMakefile = os.path.normpath(os.path.join(Lib, makefile))
+                if os.path.exists(LibMakefile):
+                    NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall']
+                    LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
+            return True
+
+        # clean
+        if Target == 'clean':
+            for Mod in AutoGenObject.ModuleBuildDirectoryList:
+                ModMakefile = os.path.normpath(os.path.join(Mod, makefile))
+                if os.path.exists(ModMakefile):
+                    NewBuildCommand = BuildCommand + ['-f', ModMakefile, 'cleanall']
+                    LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
+            for Lib in AutoGenObject.LibraryBuildDirectoryList:
+                LibMakefile = os.path.normpath(os.path.join(Lib, makefile))
+                if os.path.exists(LibMakefile):
+                    NewBuildCommand = BuildCommand + ['-f', LibMakefile, 'cleanall']
+                    LaunchCommand(NewBuildCommand, AutoGenObject.MakeFileDir)
+            return True
+
+        # cleanall
+        if Target == 'cleanall':
+            try:
+                #os.rmdir(AutoGenObject.BuildDir)
+                RemoveDirectory(AutoGenObject.BuildDir, True)
+                #
+                # First should close DB.
+                #
+                self.Db.Close()
+                RemoveDirectory(os.path.dirname(GlobalData.gDatabasePath), True)
+            except WindowsError, X:
+                EdkLogger.error("build", FILE_DELETE_FAILURE, ExtraData=str(X))
+        return True
+
+    ## Build a module or platform
+    #
+    # Create autogen code and makefile for a module or platform, and the launch
+    # "make" command to build it
+    #
+    #   @param  Target                      The target of build command
+    #   @param  Platform                    The platform file
+    #   @param  Module                      The module file
+    #   @param  BuildTarget                 The name of build target, one of "DEBUG", "RELEASE"
+    #   @param  ToolChain                   The name of toolchain to build
+    #   @param  Arch                        The arch of the module/platform
+    #   @param  CreateDepModuleCodeFile     Flag used to indicate creating code
+    #                                       for dependent modules/Libraries
+    #   @param  CreateDepModuleMakeFile     Flag used to indicate creating makefile
+    #                                       for dependent modules/Libraries
+    #
+    def _Build(self, Target, AutoGenObject, CreateDepsCodeFile=True, CreateDepsMakeFile=True, BuildModule=False):
+        if AutoGenObject == None:
+            return False
+
+        # skip file generation for cleanxxx targets, run and fds target
+        if Target not in ['clean', 'cleanlib', 'cleanall', 'run', 'fds']:
+            # for target which must generate AutoGen code and makefile
+            if not self.SkipAutoGen or Target == 'genc':
+                self.Progress.Start("Generating code")
+                AutoGenObject.CreateCodeFile(CreateDepsCodeFile)
+                self.Progress.Stop("done!")
+            if Target == "genc":
+                return True
+
+            if not self.SkipAutoGen or Target == 'genmake':
+                self.Progress.Start("Generating makefile")
+                AutoGenObject.CreateMakeFile(CreateDepsMakeFile)
+                #AutoGenObject.CreateAsBuiltInf()
+                self.Progress.Stop("done!")
+            if Target == "genmake":
+                return True
+        else:
+            # always recreate top/platform makefile when clean, just in case of inconsistency
+            AutoGenObject.CreateCodeFile(False)
+            AutoGenObject.CreateMakeFile(False)
+
+        if EdkLogger.GetLevel() == EdkLogger.QUIET:
+            EdkLogger.quiet("Building ... %s" % repr(AutoGenObject))
+
+        BuildCommand = AutoGenObject.BuildCommand
+        if BuildCommand == None or len(BuildCommand) == 0:
+            EdkLogger.error("build", OPTION_MISSING,
+                            "No build command found for this module. "
+                            "Please check your setting of %s_%s_%s_MAKE_PATH in Conf/tools_def.txt file." %
+                                (AutoGenObject.BuildTarget, AutoGenObject.ToolChain, AutoGenObject.Arch),
+                            ExtraData=str(AutoGenObject))
+
+        # genfds
+        if Target == 'fds':
+            LaunchCommand(AutoGenObject.GenFdsCommand, AutoGenObject.MakeFileDir)
+            return True
+
+        # run
+        if Target == 'run':
+            RunDir = os.path.normpath(os.path.join(AutoGenObject.BuildDir, 'IA32'))
+            Command = '.\SecMain'
+            os.chdir(RunDir)
+            LaunchCommand(Command, RunDir)
+            return True
+
+        # build modules
         BuildCommand = BuildCommand + [Target]
-        LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)
+        if BuildModule:
+            LaunchCommand(BuildCommand, AutoGenObject.MakeFileDir)
+            self.CreateAsBuiltInf()
+            return True
+
+        # build library
+        if Target == 'libraries':
+            pass
+
+        # not build modules
+
+
+        # cleanall
         if Target == 'cleanall':
             try:
                 #os.rmdir(AutoGenObject.BuildDir)
@@ -1244,7 +1399,16 @@
                 self.LoadFixAddress = Wa.Platform.LoadFixAddress
                 self.BuildReport.AddPlatformReport(Wa)
                 self.Progress.Stop("done!")
-                self._Build(self.Target, Wa)
+                for Arch in Wa.ArchList:
+                    GlobalData.gGlobalDefines['ARCH'] = Arch
+                    Pa = PlatformAutoGen(Wa, self.PlatformFile, BuildTarget, ToolChain, Arch)
+                    for Module in Pa.Platform.Modules:
+                        # Get ModuleAutoGen object to generate C code file and makefile
+                        Ma = ModuleAutoGen(Wa, Module, BuildTarget, ToolChain, Arch, self.PlatformFile)
+                        if Ma == None:
+                            continue
+                        self.BuildModules.append(Ma)
+                    self._BuildPa(self.Target, Pa)
 
                 # Create MAP file when Load Fix Address is enabled.
                 if self.Target in ["", "all", "fds"]:
@@ -1327,7 +1491,9 @@
                     Ma = ModuleAutoGen(Wa, self.ModuleFile, BuildTarget, ToolChain, Arch, self.PlatformFile)
                     if Ma == None: continue
                     MaList.append(Ma)
-                    self._Build(self.Target, Ma)
+                    self.BuildModules.append(Ma)
+                    if not Ma.IsBinaryModule:
+                        self._Build(self.Target, Ma, BuildModule=True)
 
                 self.BuildReport.AddPlatformReport(Wa, MaList)
                 if MaList == []:
@@ -1508,7 +1674,8 @@
                         #
                         # Generate FD image if there's a FDF file found
                         #
-                        LaunchCommand(Wa.BuildCommand + ["fds"], Wa.MakeFileDir)
+                        LaunchCommand(Wa.GenFdsCommand, os.getcwd())
+
                         #
                         # Create MAP file for all platform FVs after GenFds.
                         #
