]> git.proxmox.com Git - mirror_edk2.git/blobdiff - Tools/Java/Source/GenBuild/org/tianocore/build/autogen/AutoGen.java
Fixed EDKT538; Removed the some code generation for UEFI_APPLICATION type of module...
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / autogen / AutoGen.java
index bf04cc260f1e799e0f53dc3e06c445f76f0958da..b1414a7c75be072b7c0a81766caaddc677db018f 100644 (file)
@@ -82,22 +82,10 @@ public class AutoGen {
     private CommonDefinition.PCD_DRIVER_TYPE pcdDriverType;\r
 \r
     ///\r
-    /// The protocl list which records in module or library surface area and\r
-    /// it's dependence on library instance surface area.\r
+    /// The Guid CName list which recoreded in module or library surface area \r
+    /// and it's dependence on library instance surface area.\r
     ///\r
-    private Set<String> mProtocolList = new HashSet<String>();\r
-\r
-    ///\r
-    /// The Ppi list which recorded in module or library surface area and its\r
-    /// dependency on library instance surface area.\r
-    ///\r
-    private Set<String> mPpiList = new HashSet<String>();\r
-\r
-    ///\r
-    /// The Guid list which recoreded in module or library surface area and it's\r
-    /// dependence on library instance surface area.\r
-    ///\r
-    private Set<String> mGuidList = new HashSet<String>();\r
+    private Set<String> mGuidCNameList = new HashSet<String>();\r
 \r
     ///\r
     /// The dependence package list which recoreded in module or library surface\r
@@ -119,6 +107,9 @@ public class AutoGen {
     private List<String> setVirtalAddList = new ArrayList<String>();\r
     private List<String> exitBootServiceList = new ArrayList<String>();\r
 \r
+    private StringBuffer functionDeclarations = new StringBuffer(10240);\r
+    private StringBuffer globalDeclarations = new StringBuffer(10240);\r
+\r
     //\r
     // flag of PcdComponentNameDisable, PcdDriverDiagnosticDisable \r
     //\r
@@ -345,23 +336,37 @@ public class AutoGen {
         //\r
         // Write library class's related *.h file to autogen.h.\r
         //\r
-        String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED,this.arch);\r
-        if (libClassList != null) {\r
-            libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
-            item = libClassIncludeH.iterator();\r
-            while (item.hasNext()) {\r
-                fileBuffer.append(item.next().toString());\r
-            }\r
+        List<String> libClasses = new ArrayList<String>(100);\r
+        String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, this.arch, null);\r
+        for (int i = 0; i < libClassList.length; ++i) {\r
+            libClasses.add(libClassList[i]);\r
         }\r
 \r
-        libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch);\r
-        if (libClassList != null) {\r
-            libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
-            item = libClassIncludeH.iterator();\r
-            while (item.hasNext()) {\r
-                fileBuffer.append(item.next().toString());\r
+        libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch, null);\r
+        for (int i = 0; i < libClassList.length; ++i) {\r
+            libClasses.add(libClassList[i]);\r
+        }\r
+        //\r
+        // Add AutoGen used library class\r
+        // \r
+        int moduleTypeId = CommonDefinition.getModuleType(moduleType);\r
+        if (!libClasses.contains("DebugLib") && moduleTypeId != CommonDefinition.ModuleTypeUnknown\r
+            && moduleTypeId != CommonDefinition.ModuleTypeBase) {\r
+            libClasses.add("DebugLib");\r
+        }\r
+        switch (moduleTypeId) {\r
+        case CommonDefinition.ModuleTypeDxeDriver:\r
+        case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
+        case CommonDefinition.ModuleTypeDxeSalDriver:\r
+        case CommonDefinition.ModuleTypeDxeSmmDriver:\r
+        case CommonDefinition.ModuleTypeUefiDriver:\r
+        case CommonDefinition.ModuleTypeUefiApplication:\r
+            if (!libClasses.contains("UefiBootServicesTableLib")) {\r
+                libClasses.add("UefiBootServicesTableLib");\r
             }\r
+            break;\r
         }\r
+        LibraryClassToAutogenH(fileBuffer, libClasses.toArray(new String[libClasses.size()]));\r
         fileBuffer.append("\r\n");\r
 \r
         //\r
@@ -382,6 +387,8 @@ public class AutoGen {
             fileBuffer.append(this.myPcdAutogen.getHAutoGenString());\r
         }\r
 \r
+        fileBuffer.append(globalDeclarations);\r
+        fileBuffer.append(functionDeclarations);\r
         //\r
         // Append the #endif at AutoGen.h\r
         //\r
@@ -411,11 +418,6 @@ public class AutoGen {
         //\r
         fileBuffer.append(CommonDefinition.AUTOGENCNOTATION);\r
 \r
-        //\r
-        // Write #include <AutoGen.h> at beginning of AutoGen.c\r
-        //\r
-        fileBuffer.append(CommonDefinition.INCLUDEAUTOGENH);\r
-\r
         //\r
         // Get the native MSA file infomation. Since before call autogen,\r
         // the MSA native <Externs> information were overrided. So before\r
@@ -428,8 +430,9 @@ public class AutoGen {
         // DriverBinding/ComponentName/DriverConfiguration/DriverDialog\r
         // to AutoGen.c\r
         //\r
-\r
-        ExternsDriverBindingToAutoGenC(fileBuffer);\r
+        if (!moduleId.getModuleType().equalsIgnoreCase("UEFI_APPLICATION")) {\r
+            ExternsDriverBindingToAutoGenC(fileBuffer);\r
+        }\r
 \r
         //\r
         // Write DriverExitBootServicesEvent/DriverSetVirtualAddressMapEvent\r
@@ -458,8 +461,10 @@ public class AutoGen {
         //\r
         String guid = CommonDefinition.formatGuidName(saq.getModuleGuid());\r
         if (this.moduleId.getModuleType().equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {\r
+            globalDeclarations.append("extern GUID gEfiCallerIdGuid;\r\n");\r
             fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED GUID gEfiCallerIdGuid = {");\r
-        } else {\r
+        } else if (!this.moduleId.getModuleType().equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_USER_DEFINED)) {\r
+            globalDeclarations.append("extern EFI_GUID gEfiCallerIdGuid;\r\n");\r
             fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID gEfiCallerIdGuid = {");\r
         }\r
 \r
@@ -496,11 +501,9 @@ public class AutoGen {
         }\r
 \r
         //\r
-        // Write consumed ppi, guid, protocol to autogen.c\r
+        // Write consumed ppi, guid, protocol, etc to autogen.c\r
         //\r
-        ProtocolGuidToAutogenC(fileBuffer);\r
-        PpiGuidToAutogenC(fileBuffer);\r
-        GuidGuidToAutogenC(fileBuffer);\r
+        CNameToAutogenC(fileBuffer);\r
 \r
         //\r
         // Call pcd autogen.\r
@@ -568,8 +571,7 @@ public class AutoGen {
         fileBuffer.append(CommonDefinition.AUTOGENHBEGIN);\r
         String[] specList = saq.getExternSpecificaiton();\r
         for (int i = 0; i < specList.length; i++) {\r
-            fileBuffer.append(CommonDefinition.DEFINE + specList[i]\r
-                              + "\r\n");\r
+            fileBuffer.append(CommonDefinition.DEFINE + specList[i] + "\r\n");\r
         }\r
         // fileBuffer.append(CommonDefinition.autoGenHLine1);\r
         // fileBuffer.append(CommonDefinition.autoGenHLine2);\r
@@ -578,8 +580,7 @@ public class AutoGen {
         // Write consumed package's mdouleInfo related *.h file to autogen.h.\r
         //\r
         moduleType = saq.getModuleType();\r
-        PackageIdentification[] cosumedPkglist = saq\r
-                                                 .getDependencePkg(this.arch);\r
+        PackageIdentification[] cosumedPkglist = saq.getDependencePkg(this.arch);\r
         headerFileList = depPkgToAutogenH(cosumedPkglist, moduleType);\r
         item = headerFileList.iterator();\r
         while (item.hasNext()) {\r
@@ -588,23 +589,37 @@ public class AutoGen {
         //\r
         // Write library class's related *.h file to autogen.h\r
         //\r
-        String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, this.arch);\r
-        if (libClassList != null) {\r
-            libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
-            item = libClassIncludeH.iterator();\r
-            while (item.hasNext()) {\r
-                fileBuffer.append(item.next().toString());\r
-            }\r
+        List<String> libClasses = new ArrayList<String>(100);\r
+        String[] libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSCONSUMED, this.arch, null);\r
+        for (int i = 0; i < libClassList.length; ++i) {\r
+            libClasses.add(libClassList[i]);\r
         }\r
 \r
-        libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch);\r
-        if (libClassList != null) {\r
-            libClassIncludeH = LibraryClassToAutogenH(libClassList);\r
-            item = libClassIncludeH.iterator();\r
-            while (item.hasNext()) {\r
-                fileBuffer.append(item.next().toString());\r
+        libClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED, this.arch, null);\r
+        for (int i = 0; i < libClassList.length; ++i) {\r
+            libClasses.add(libClassList[i]);\r
+        }\r
+        //\r
+        // Add AutoGen used library class\r
+        // \r
+        int moduleTypeId = CommonDefinition.getModuleType(moduleType);\r
+        if (!libClasses.contains("DebugLib") && moduleTypeId != CommonDefinition.ModuleTypeUnknown\r
+            && moduleTypeId != CommonDefinition.ModuleTypeBase) {\r
+            libClasses.add("DebugLib");\r
+        }\r
+        switch (moduleTypeId) {\r
+        case CommonDefinition.ModuleTypeDxeDriver:\r
+        case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
+        case CommonDefinition.ModuleTypeDxeSalDriver:\r
+        case CommonDefinition.ModuleTypeDxeSmmDriver:\r
+        case CommonDefinition.ModuleTypeUefiDriver:\r
+        case CommonDefinition.ModuleTypeUefiApplication:\r
+            if (!libClasses.contains("UefiBootServicesTableLib")) {\r
+                libClasses.add("UefiBootServicesTableLib");\r
             }\r
+            break;\r
         }\r
+        LibraryClassToAutogenH(fileBuffer, libClasses.toArray(new String[libClasses.size()]));\r
         fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);\r
 \r
         //\r
@@ -625,12 +640,17 @@ public class AutoGen {
             fileBuffer.append("\r\n");\r
             fileBuffer.append(this.myPcdAutogen.getHAutoGenString());\r
         }\r
-\r
+        //\r
+        // generate function prototype for constructor and destructor\r
+        // \r
+        LibConstructorToAutogenH(moduleType);\r
+        LibDestructorToAutogenH(moduleType);\r
+        ExternCallBackToAutoGenH(moduleType);\r
+        fileBuffer.append(functionDeclarations);\r
         //\r
         // Append the #endif at AutoGen.h\r
         //\r
         fileBuffer.append("#endif\r\n");\r
-\r
         //\r
         // Save content of string buffer to AutoGen.h file.\r
         //\r
@@ -672,10 +692,6 @@ public class AutoGen {
             fileBuffer.append(ToolDefinitions.LINE_SEPARATOR);\r
             fileBuffer.append(this.myPcdAutogen.getCAutoGenString());\r
         }\r
-\r
-        if (!saveFile(outputPath + File.separatorChar + "AutoGen.c", fileBuffer)) {\r
-            throw new AutoGenException("Failed to generate AutoGen.c !!!");\r
-        }\r
     }\r
 \r
     /**\r
@@ -689,9 +705,8 @@ public class AutoGen {
                  build module or library.\r
       @return includeStrList List of *.h file.\r
     **/\r
-    Set<String> LibraryClassToAutogenH(String[] libClassList)\r
+    void LibraryClassToAutogenH(StringBuffer fileBuffer, String[] libClassList)\r
     throws EdkException {\r
-        Set<String> includeStrList = new LinkedHashSet<String>();\r
         String includeName[];\r
         String str = "";\r
 \r
@@ -710,14 +725,14 @@ public class AutoGen {
             for (int j = 0; j < includeName.length; j++) {\r
                 String includeNameStr = includeName[j];\r
                 if (includeNameStr != null) {\r
-                    str = CommonDefinition.INCLUDE + " " + "<";\r
-                    str = str + includeNameStr + ">\r\n";\r
-                    includeStrList.add(str);\r
+                    fileBuffer.append(CommonDefinition.INCLUDE);\r
+                    fileBuffer.append(" <");\r
+                    fileBuffer.append(includeNameStr);\r
+                    fileBuffer.append(">\r\n");\r
                     includeNameStr = null;\r
                 }\r
             }\r
         }\r
-        return includeStrList;\r
     }\r
 \r
     /**\r
@@ -749,8 +764,7 @@ public class AutoGen {
                                            + packageNameList[i]\r
                                            + "] declaration in any SPD package. ");\r
             } else if (!pkgHeader.equalsIgnoreCase("")) {\r
-                includeStr = CommonDefinition.INCLUDE + " <" + pkgHeader\r
-                             + ">\r\n";\r
+                includeStr = CommonDefinition.INCLUDE + " <" + pkgHeader + ">\r\n";\r
                 includeStrList.add(includeStr);\r
             }\r
         }\r
@@ -774,7 +788,7 @@ public class AutoGen {
     throws EdkException {\r
 \r
         String typeStr = saq.getModuleType();\r
-        String debugStr = "DEBUG ((EFI_D_INFO | EFI_D_LOAD, \"Module Entry Point (%s) 0x%%p\\n\", (VOID *)(UINTN)%s));\n";\r
+        String debugStr = "DEBUG ((EFI_D_INFO | EFI_D_LOAD, \"Module Entry Point (%s) 0x%%p\\n\", (VOID *)(UINTN)%s));\r\n";\r
         int unloadImageCount = 0;\r
         int entryPointCount  = 0;\r
 \r
@@ -786,15 +800,15 @@ public class AutoGen {
         \r
         case CommonDefinition.ModuleTypePeiCore:\r
             if (entryPointList == null ||entryPointList.length != 1 ) {\r
-                throw new AutoGenException(\r
-                                          "Module type = 'PEI_CORE', can have only one module entry point!");\r
+                throw new AutoGenException("Module type = 'PEI_CORE', can have only one module entry point!");\r
             } else {\r
-                fileBuffer.append("EFI_STATUS\r\n");\r
-                fileBuffer.append(entryPointList[0]);\r
-                fileBuffer.append(" (\r\n");\r
-                fileBuffer.append("  IN EFI_PEI_STARTUP_DESCRIPTOR  *PeiStartupDescriptor,\r\n");\r
-                fileBuffer.append("  IN VOID                        *OldCoreData\r\n");\r
-                fileBuffer.append("  );\r\n\r\n");\r
+                functionDeclarations.append("EFI_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(entryPointList[0]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_PEI_STARTUP_DESCRIPTOR  *PeiStartupDescriptor,\r\n");\r
+                functionDeclarations.append("  IN VOID                        *OldCoreData\r\n");\r
+                functionDeclarations.append("  );\r\n\r\n");\r
 \r
                 fileBuffer.append("EFI_STATUS\r\n");\r
                 fileBuffer.append("EFIAPI\r\n");\r
@@ -803,8 +817,6 @@ public class AutoGen {
                 fileBuffer.append("  IN VOID                        *OldCoreData\r\n");\r
                 fileBuffer.append("  )\r\n\r\n");\r
                 fileBuffer.append("{\r\n");\r
-                // fileBuffer.append("  DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[0] + "));\n");\r
-                fileBuffer.append(String.format("  " + debugStr, entryPointList[0], entryPointList[0]));\r
                 fileBuffer.append("  return ");\r
                 fileBuffer.append(entryPointList[0]);\r
                 fileBuffer.append(" (PeiStartupDescriptor, OldCoreData);\r\n");\r
@@ -817,11 +829,12 @@ public class AutoGen {
             if (entryPointList == null || entryPointList.length != 1) {\r
                 throw new AutoGenException("Module type = 'DXE_CORE', can have only one module entry point!");\r
             } else {\r
-                fileBuffer.append("VOID\r\n");\r
-                fileBuffer.append(entryPointList[0]);\r
-                fileBuffer.append(" (\n");\r
-                fileBuffer.append("  IN VOID  *HobStart\r\n");\r
-                fileBuffer.append("  );\r\n\r\n");\r
+                functionDeclarations.append("VOID\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(entryPointList[0]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN VOID  *HobStart\r\n");\r
+                functionDeclarations.append("  );\r\n\r\n");\r
 \r
                 fileBuffer.append("VOID\r\n");\r
                 fileBuffer.append("EFIAPI\r\n");\r
@@ -829,8 +842,6 @@ public class AutoGen {
                 fileBuffer.append("  IN VOID  *HobStart\r\n");\r
                 fileBuffer.append("  )\r\n\r\n");\r
                 fileBuffer.append("{\r\n");\r
-                //fileBuffer.append("  DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[0] + "));\n");\r
-                fileBuffer.append(String.format("  " + debugStr, entryPointList[0], entryPointList[0]));\r
                 fileBuffer.append("  ");\r
                 fileBuffer.append(entryPointList[0]);\r
                 fileBuffer.append(" (HobStart);\r\n");\r
@@ -854,12 +865,13 @@ public class AutoGen {
                 break;\r
             }\r
             for (int i = 0; i < entryPointList.length; i++) {\r
-                fileBuffer.append("EFI_STATUS\r\n");\r
-                fileBuffer.append(entryPointList[i]);\r
-                fileBuffer.append(" (\r\n");\r
-                fileBuffer.append("  IN EFI_FFS_FILE_HEADER  *FfsHeader,\r\n");\r
-                fileBuffer.append("  IN EFI_PEI_SERVICES     **PeiServices\r\n");\r
-                fileBuffer.append("  );\r\n");\r
+                functionDeclarations.append("EFI_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(entryPointList[i]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_FFS_FILE_HEADER  *FfsHeader,\r\n");\r
+                functionDeclarations.append("  IN EFI_PEI_SERVICES     **PeiServices\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
                 entryPointCount++;\r
             }\r
 \r
@@ -871,7 +883,6 @@ public class AutoGen {
             fileBuffer.append("  )\r\n\r\n");\r
             fileBuffer.append("{\r\n");\r
             if (entryPointCount == 1) {\r
-                //fileBuffer.append("  DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[0] + "));\n");\r
                 fileBuffer.append(String.format("  " + debugStr, entryPointList[0], entryPointList[0]));\r
                 fileBuffer.append("  return ");\r
                 fileBuffer.append(entryPointList[0]);\r
@@ -882,7 +893,6 @@ public class AutoGen {
                 fileBuffer.append("  CombinedStatus = EFI_LOAD_ERROR;\r\n\r\n");\r
                 for (int i = 0; i < entryPointList.length; i++) {\r
                     if (!entryPointList[i].equals("")) {\r
-                        //fileBuffer.append("  DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[i] + "));\n");\r
                         fileBuffer.append(String.format("  " + debugStr, entryPointList[i], entryPointList[i]));\r
                         fileBuffer.append("  Status = ");\r
                         fileBuffer.append(entryPointList[i]);\r
@@ -921,12 +931,13 @@ public class AutoGen {
 \r
             } else {\r
                 for (int i = 0; i < entryPointList.length; i++) {\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append(entryPointList[i]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
-                    fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(entryPointList[i]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                    functionDeclarations.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     entryPointCount++;\r
                 }\r
                 fileBuffer.append("GLOBAL_REMOVE_IF_UNREFERENCED  const UINT8  _gDriverEntryPointCount = ");\r
@@ -945,7 +956,6 @@ public class AutoGen {
 \r
                 for (int i = 0; i < entryPointList.length; i++) {\r
                     fileBuffer.append("  if (SetJump (&mJumpContext) == 0) {\r\n");\r
-                    //fileBuffer.append("    DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[i] + "));\n");\r
                     fileBuffer.append(String.format("    " + debugStr, entryPointList[i], entryPointList[i]));\r
                     fileBuffer.append("    ExitDriver (");\r
                     fileBuffer.append(entryPointList[i]);\r
@@ -959,7 +969,7 @@ public class AutoGen {
                 fileBuffer.append("VOID\r\n");\r
                 fileBuffer.append("EFIAPI\r\n");\r
                 fileBuffer.append("ExitDriver (\r\n");\r
-                fileBuffer.append("  IN EFI_STATUS  Status\n");\r
+                fileBuffer.append("  IN EFI_STATUS  Status\r\n");\r
                 fileBuffer.append("  )\r\n\r\n");\r
                 fileBuffer.append("{\r\n");\r
                 fileBuffer.append("  if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {\r\n");\r
@@ -974,18 +984,15 @@ public class AutoGen {
             //\r
             // Add "ModuleUnloadImage" for DxeSmmDriver module type;\r
             //\r
-            //entryPointList = SurfaceAreaQuery.getModuleUnloadImageArray();\r
-            //entryPointList = CommonDefinition.remDupString(entryPointList);\r
-            //entryPointCount = 0;\r
 \r
             unloadImageCount = 0;\r
             if (unloadImageList != null) {\r
                 for (int i = 0; i < unloadImageList.length; i++) {\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append(unloadImageList[i]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append(unloadImageList[i]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_HANDLE        ImageHandle\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     unloadImageCount++;\r
                 }\r
             }\r
@@ -1056,13 +1063,13 @@ public class AutoGen {
 \r
             } else {\r
                 for (int i = 0; i < entryPointList.length; i++) {\r
-\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append(entryPointList[i]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
-                    fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(entryPointList[i]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                    functionDeclarations.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     entryPointCount++;\r
                 }\r
 \r
@@ -1073,7 +1080,7 @@ public class AutoGen {
                     fileBuffer.append("static BASE_LIBRARY_JUMP_BUFFER  mJumpContext;\r\n");\r
                     fileBuffer.append("static EFI_STATUS  mDriverEntryPointStatus = EFI_LOAD_ERROR;\r\n");\r
                 }\r
-                fileBuffer.append("\n");\r
+                fileBuffer.append("\r\n");\r
 \r
                 fileBuffer.append("EFI_STATUS\r\n");\r
                 fileBuffer.append("EFIAPI\r\n");\r
@@ -1084,7 +1091,6 @@ public class AutoGen {
                 fileBuffer.append("{\r\n");\r
 \r
                 if (entryPointCount == 1) {\r
-                    //fileBuffer.append("  DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[0] + "));\n");\r
                     fileBuffer.append(String.format("  " + debugStr, entryPointList[0], entryPointList[0]));\r
                     fileBuffer.append("  return ");\r
                     fileBuffer.append(entryPointList[0]);\r
@@ -1093,7 +1099,6 @@ public class AutoGen {
                     for (int i = 0; i < entryPointList.length; i++) {\r
                         if (!entryPointList[i].equals("")) {\r
                             fileBuffer.append("  if (SetJump (&mJumpContext) == 0) {\r\n");\r
-                            //fileBuffer.append("    DEBUG ((EFI_D_INFO, \"Module Entry Point 0x%08x\\n\", (UINTN)" + entryPointList[i] + "));\n");\r
                             fileBuffer.append(String.format("    " + debugStr, entryPointList[i], entryPointList[i]));\r
                             fileBuffer.append("    ExitDriver (");\r
                             fileBuffer.append(entryPointList[i]);\r
@@ -1118,8 +1123,7 @@ public class AutoGen {
                     fileBuffer.append("  if (EFI_ERROR (Status)) {\r\n");\r
                     fileBuffer.append("    ProcessLibraryDestructorList (gImageHandle, gST);\r\n");\r
                     fileBuffer.append("  }\r\n");\r
-                    fileBuffer\r
-                    .append("  gBS->Exit (gImageHandle, Status, 0, NULL);\r\n");\r
+                    fileBuffer.append("  gBS->Exit (gImageHandle, Status, 0, NULL);\r\n");\r
                 } else {\r
                     fileBuffer.append("  if (!EFI_ERROR (Status) || EFI_ERROR (mDriverEntryPointStatus)) {\r\n");\r
                     fileBuffer.append("    mDriverEntryPointStatus = Status;\r\n");\r
@@ -1130,6 +1134,9 @@ public class AutoGen {
                 fileBuffer.append("}\r\n\r\n");\r
             }\r
 \r
+            if (CommonDefinition.getModuleType(typeStr) == CommonDefinition.ModuleTypeUefiApplication) {\r
+                break;\r
+            }\r
             //\r
             // Add ModuleUnloadImage for DxeDriver and UefiDriver module type.\r
             //\r
@@ -1142,12 +1149,12 @@ public class AutoGen {
             unloadImageCount = 0;\r
             if (unloadImageList != null) {\r
                 for (int i = 0; i < unloadImageList.length; i++) {\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(unloadImageList[i]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(unloadImageList[i]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_HANDLE        ImageHandle\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     unloadImageCount++;\r
                 }\r
             }\r
@@ -1156,7 +1163,7 @@ public class AutoGen {
             fileBuffer.append(Integer.toString(unloadImageCount));\r
             fileBuffer.append(";\r\n\r\n");\r
 \r
-            fileBuffer.append("EFI_STATUS\n");\r
+            fileBuffer.append("EFI_STATUS\r\n");\r
             fileBuffer.append("EFIAPI\r\n");\r
             fileBuffer.append("ProcessModuleUnloadList (\r\n");\r
             fileBuffer.append("  IN EFI_HANDLE  ImageHandle\r\n");\r
@@ -1197,178 +1204,220 @@ public class AutoGen {
     }\r
 \r
     /**\r
-      PpiGuidToAutogenc\r
+      CNameToAutogenc\r
      \r
-      This function gets GUIDs from SPD file accrodeing to <PPIs> information\r
-      and write those GUIDs to AutoGen.c.\r
+      This function gets GUIDs from SPD file accrodeing to <Protocols> <Ppis> \r
+      <Guids> information and write those GUIDs to AutoGen.c.\r
      \r
       @param fileBuffer\r
                  String Buffer for Autogen.c file.\r
-      @throws BuildException\r
-                  Guid must set value!\r
+     \r
     **/\r
-    void PpiGuidToAutogenC(StringBuffer fileBuffer) throws AutoGenException {\r
+    void CNameToAutogenC(StringBuffer fileBuffer) throws AutoGenException {\r
         String[] cNameGuid = null;\r
+        String guidKeyWord = null;\r
 \r
-        //\r
-        // Get the all PPI adn PPI Notify from MSA file,\r
-        // then add those PPI ,and PPI Notify name to list.\r
-        //\r
-\r
-        String[] ppiList = saq.getPpiArray(this.arch);\r
-        for (int i = 0; i < ppiList.length; i++) {\r
-            this.mPpiList.add(ppiList[i]);\r
+        String[] cnameList = saq.getCNameArray(this.arch);\r
+        for (int i = 0; i < cnameList.length; i++) {\r
+            this.mGuidCNameList.add(cnameList[i]);\r
         }\r
 \r
-        String[] ppiNotifyList = saq.getPpiNotifyArray(this.arch);\r
-        for (int i = 0; i < ppiNotifyList.length; i++) {\r
-            this.mPpiList.add(ppiNotifyList[i]);\r
-        }\r
 \r
-        //\r
-        // Find CNAME and GUID from dependence SPD file and write to Autogen.c\r
-        //\r
-        Iterator ppiIterator = this.mPpiList.iterator();\r
-        String ppiKeyWord = null;\r
-        while (ppiIterator.hasNext()) {\r
-            ppiKeyWord = ppiIterator.next().toString();\r
-            cNameGuid = GlobalData.getPpiGuid(this.mDepPkgList, ppiKeyWord);\r
-            if (cNameGuid != null) {\r
-                fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");\r
-                fileBuffer.append(cNameGuid[0]);\r
-                fileBuffer.append(" =     { ");\r
-                fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));\r
-                fileBuffer.append(" } ;");\r
-            } else {\r
-                //\r
-                // If can't find Ppi GUID declaration in every package\r
-                //\r
-                throw new AutoGenException("Can not find Ppi GUID ["\r
-                                           + ppiKeyWord + "] declaration in any SPD package!");\r
+        Iterator guidIterator = this.mGuidCNameList.iterator();\r
+        while (guidIterator.hasNext()) {\r
+            guidKeyWord = guidIterator.next().toString();\r
+            cNameGuid = GlobalData.getGuid(this.mDepPkgList, guidKeyWord);\r
+            if (cNameGuid == null) {\r
+                cNameGuid = GlobalData.getProtocolGuid(this.mDepPkgList, guidKeyWord);\r
+                if (cNameGuid == null) {\r
+                    cNameGuid = GlobalData.getPpiGuid(this.mDepPkgList, guidKeyWord);\r
+                    if (cNameGuid == null) {\r
+                        //\r
+                        // If can't find GUID declaration in every package, stop the build\r
+                        //\r
+                        EdkLog.log(EdkLog.EDK_INFO,"WARN: Can not find Guid [" + guidKeyWord + "] declaration in any SPD file.");\r
+                        continue;\r
+                        //throw new AutoGenException("Can not find Guid [" + guidKeyWord\r
+                        //                           + "] declaration in any SPD package. ");\r
+                    }\r
+                }\r
             }\r
+\r
+            fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");\r
+            fileBuffer.append(cNameGuid[0]);\r
+            fileBuffer.append(" =     { ");\r
+            fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));\r
+            fileBuffer.append("} ;");\r
         }\r
     }\r
 \r
     /**\r
-      ProtocolGuidToAutogenc\r
+      LibInstanceToAutogenC\r
      \r
-      This function gets GUIDs from SPD file accrodeing to <Protocol>\r
-      information and write those GUIDs to AutoGen.c.\r
+      This function adds dependent library instance to autogen.c,which\r
+      includeing library's constructor, destructor, and library dependent ppi,\r
+      protocol, guid, pcd information.\r
      \r
       @param fileBuffer\r
-                 String Buffer for Autogen.c file.\r
+                 String buffer for AutoGen.c\r
       @throws BuildException\r
-                  Protocol name must set.\r
     **/\r
-    void ProtocolGuidToAutogenC(StringBuffer fileBuffer) throws EdkException {\r
-        String[] cNameGuid = null;\r
-\r
-        String[] protocolList = saq.getProtocolArray(this.arch);\r
-\r
+    void LibInstanceToAutogenC(StringBuffer fileBuffer) throws EdkException {\r
+        String moduleType = this.moduleId.getModuleType();\r
         //\r
-        // Add result to Autogen global list.\r
+        // Add library constructor to AutoGen.c\r
         //\r
-        for (int i = 0; i < protocolList.length; i++) {\r
-            this.mProtocolList.add(protocolList[i]);\r
-        }\r
-\r
-        String[] protocolNotifyList = saq.getProtocolNotifyArray(this.arch);\r
-\r
-        for (int i = 0; i < protocolNotifyList.length; i++) {\r
-            this.mProtocolList.add(protocolNotifyList[i]);\r
-        }\r
-\r
+        LibConstructorToAutogenC(libConstructList, moduleType, fileBuffer);\r
         //\r
-        // Get the NAME and GUID from dependence SPD and write to Autogen.c\r
+        // Add library destructor to AutoGen.c\r
         //\r
-        Iterator protocolIterator = this.mProtocolList.iterator();\r
-        String protocolKeyWord = null;\r
-\r
-\r
-        while (protocolIterator.hasNext()) {\r
-            protocolKeyWord = protocolIterator.next().toString();\r
-            cNameGuid = GlobalData.getProtocolGuid(this.mDepPkgList, protocolKeyWord);\r
-            if (cNameGuid != null) {\r
-                fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");\r
-                fileBuffer.append(cNameGuid[0]);\r
-                fileBuffer.append(" =     { ");\r
-                fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));\r
-                fileBuffer.append(" } ;");\r
-            } else {\r
-                //\r
-                // If can't find protocol GUID declaration in every package\r
-                //\r
-                throw new AutoGenException("Can not find protocol Guid ["\r
-                                           + protocolKeyWord + "] declaration in any SPD package!");\r
-            }\r
-        }\r
+        LibDestructorToAutogenC(libDestructList, moduleType, fileBuffer);\r
     }\r
 \r
     /**\r
-      GuidGuidToAutogenc\r
+      LibConstructorToAutogenH\r
      \r
-      This function gets GUIDs from SPD file accrodeing to <Guids> information\r
-      and write those GUIDs to AutoGen.c.\r
+      This function writes library constructor declarations AutoGen.h. The library\r
+      constructor's parameter and return value depend on module type.\r
      \r
+      @param libInstanceList\r
+                 List of library construct name.\r
+      @param moduleType\r
+                 Module type.\r
       @param fileBuffer\r
-                 String Buffer for Autogen.c file.\r
-     \r
+                 String buffer for AutoGen.c\r
+      @throws Exception\r
     **/\r
-    void GuidGuidToAutogenC(StringBuffer fileBuffer) throws AutoGenException {\r
-        String[] cNameGuid = null;\r
-        String guidKeyWord = null;\r
-\r
-        String[] guidList = saq.getGuidEntryArray(this.arch);\r
+    void LibConstructorToAutogenH(String moduleType) throws EdkException {\r
+        boolean isFirst = true;\r
 \r
-        for (int i = 0; i < guidList.length; i++) {\r
-            this.mGuidList.add(guidList[i]);\r
+        //\r
+        // If not yet parse this library instance's constructor\r
+        // element,parse it.\r
+        //\r
+        String libConstructName = saq.getLibConstructorName();\r
+        if (libConstructName == null) {\r
+            return;\r
         }\r
 \r
+        //\r
+        // The library constructor's parameter and return value depend on\r
+        // module type.\r
+        //\r
+        if (moduleType.equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {\r
+            functionDeclarations.append("RETURN_STATUS\r\n");\r
+            functionDeclarations.append("EFIAPI\r\n");\r
+            functionDeclarations.append(libConstructName);\r
+            functionDeclarations.append(" (\r\n");\r
+            functionDeclarations.append("  VOID\r\n");\r
+            functionDeclarations.append("  );\r\n");\r
+        } else {\r
+            switch (CommonDefinition.getModuleType(moduleType)) {\r
+            case CommonDefinition.ModuleTypeBase:\r
+                functionDeclarations.append("RETURN_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libConstructName);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  VOID\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
+                break;\r
 \r
-        Iterator guidIterator = this.mGuidList.iterator();\r
-        while (guidIterator.hasNext()) {\r
-            guidKeyWord = guidIterator.next().toString();\r
-            cNameGuid = GlobalData.getGuid(this.mDepPkgList, guidKeyWord);\r
+            case CommonDefinition.ModuleTypePeiCore:\r
+            case CommonDefinition.ModuleTypePeim:\r
+                functionDeclarations.append("EFI_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libConstructName);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
+                functionDeclarations.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
+                break;\r
 \r
-            if (cNameGuid != null) {\r
-                fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED EFI_GUID ");\r
-                fileBuffer.append(cNameGuid[0]);\r
-                fileBuffer.append(" =     { ");\r
-                fileBuffer.append(CommonDefinition.formatGuidName(cNameGuid[1]));\r
-                fileBuffer.append("} ;");\r
-            } else {\r
-                //\r
-                // If can't find GUID declaration in every package\r
-                //\r
-                throw new AutoGenException("Can not find Guid [" + guidKeyWord\r
-                                           + "] declaration in any SPD package. ");\r
-            }\r
+            case CommonDefinition.ModuleTypeDxeCore:\r
+            case CommonDefinition.ModuleTypeDxeDriver:\r
+            case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
+            case CommonDefinition.ModuleTypeDxeSmmDriver:\r
+            case CommonDefinition.ModuleTypeDxeSalDriver:\r
+            case CommonDefinition.ModuleTypeUefiDriver:\r
+            case CommonDefinition.ModuleTypeUefiApplication:\r
+                functionDeclarations.append("EFI_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libConstructName);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                functionDeclarations.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
+                break;\r
 \r
+            }\r
         }\r
     }\r
 \r
     /**\r
-      LibInstanceToAutogenC\r
+      LibDestructorToAutogenH\r
      \r
-      This function adds dependent library instance to autogen.c,which\r
-      includeing library's constructor, destructor, and library dependent ppi,\r
-      protocol, guid, pcd information.\r
+      This function writes library destructor declarations AutoGen.h. The library\r
+      destructor's parameter and return value depend on module type.\r
      \r
+      @param libInstanceList\r
+                 List of library destructor name.\r
+      @param moduleType\r
+                 Module type.\r
       @param fileBuffer\r
                  String buffer for AutoGen.c\r
-      @throws BuildException\r
+      @throws Exception\r
     **/\r
-    void LibInstanceToAutogenC(StringBuffer fileBuffer) throws EdkException {\r
-        String moduleType = this.moduleId.getModuleType();\r
-        //\r
-        // Add library constructor to AutoGen.c\r
-        //\r
-        LibConstructorToAutogenC(libConstructList, moduleType,\r
-                                 fileBuffer/* autogenC */);\r
-        //\r
-        // Add library destructor to AutoGen.c\r
-        //\r
-        LibDestructorToAutogenC(libDestructList, moduleType, fileBuffer/* autogenC */);\r
+    void LibDestructorToAutogenH(String moduleType) throws EdkException {\r
+        boolean isFirst = true;\r
+        String libDestructName = saq.getLibDestructorName();\r
+        if (libDestructName == null) {\r
+            return;\r
+        }\r
+\r
+        if (moduleType.equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {\r
+            functionDeclarations.append("RETURN_STATUS\r\n");\r
+            functionDeclarations.append("EFIAPI\r\n");\r
+            functionDeclarations.append(libDestructName);\r
+            functionDeclarations.append(" (\r\n");\r
+            functionDeclarations.append("  VOID\r\n");\r
+            functionDeclarations.append("  );\r\n");\r
+        } else {\r
+            switch (CommonDefinition.getModuleType(moduleType)) {\r
+            case CommonDefinition.ModuleTypeBase:\r
+                functionDeclarations.append("RETURN_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libDestructName);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  VOID\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
+                break;\r
+            case CommonDefinition.ModuleTypePeiCore:\r
+            case CommonDefinition.ModuleTypePeim:\r
+                functionDeclarations.append("EFI_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libDestructName);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
+                functionDeclarations.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
+                break;\r
+            case CommonDefinition.ModuleTypeDxeCore:\r
+            case CommonDefinition.ModuleTypeDxeDriver:\r
+            case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
+            case CommonDefinition.ModuleTypeDxeSmmDriver:\r
+            case CommonDefinition.ModuleTypeDxeSalDriver:\r
+            case CommonDefinition.ModuleTypeUefiDriver:\r
+            case CommonDefinition.ModuleTypeUefiApplication:\r
+                functionDeclarations.append("EFI_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libDestructName);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                functionDeclarations.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
+                break;\r
+            }\r
+        }\r
     }\r
 \r
     /**\r
@@ -1395,32 +1444,32 @@ public class AutoGen {
         //\r
         for (int i = 0; i < libInstanceList.size(); i++) {\r
             if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {\r
-                fileBuffer.append("RETURN_STATUS\r\n");\r
-                fileBuffer.append("EFIAPI\r\n");\r
-                fileBuffer.append(libInstanceList.get(i)[0]);\r
-                fileBuffer.append(" (\r\n");\r
-                fileBuffer.append("  VOID\r\n");\r
-                fileBuffer.append("  );\r\n");\r
+                functionDeclarations.append("RETURN_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  VOID\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
             } else {\r
                 switch (CommonDefinition.getModuleType(moduleType)) {\r
                 case CommonDefinition.ModuleTypeBase:\r
-                    fileBuffer.append("RETURN_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(libInstanceList.get(i)[0]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  VOID\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("RETURN_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  VOID\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     break;\r
 \r
                 case CommonDefinition.ModuleTypePeiCore:\r
                 case CommonDefinition.ModuleTypePeim:\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(libInstanceList.get(i)[0]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
-                    fileBuffer.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
+                    functionDeclarations.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     break;\r
 \r
                 case CommonDefinition.ModuleTypeDxeCore:\r
@@ -1430,13 +1479,13 @@ public class AutoGen {
                 case CommonDefinition.ModuleTypeDxeSalDriver:\r
                 case CommonDefinition.ModuleTypeUefiDriver:\r
                 case CommonDefinition.ModuleTypeUefiApplication:\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(libInstanceList.get(i)[0]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
-                    fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                    functionDeclarations.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     break;\r
 \r
                 }\r
@@ -1457,8 +1506,7 @@ public class AutoGen {
         case CommonDefinition.ModuleTypePeiCore:\r
         case CommonDefinition.ModuleTypePeim:\r
             fileBuffer.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
-            fileBuffer\r
-            .append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
+            fileBuffer.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
             break;\r
 \r
         case CommonDefinition.ModuleTypeDxeCore:\r
@@ -1478,9 +1526,6 @@ public class AutoGen {
         //\r
         // If no constructor function, return EFI_SUCCESS.\r
         //\r
-        //if (libInstanceList.size() == 0){\r
-        //   fileBuffer.append("  return EFI_SUCCESS;\r\n");\r
-        //}\r
         for (int i = 0; i < libInstanceList.size(); i++) {\r
             if (isFirst) {\r
                 fileBuffer.append("  EFI_STATUS  Status;\r\n");\r
@@ -1545,31 +1590,31 @@ public class AutoGen {
         boolean isFirst = true;\r
         for (int i = 0; i < libInstanceList.size(); i++) {\r
             if (libInstanceList.get(i)[1].equalsIgnoreCase(EdkDefinitions.MODULE_TYPE_BASE)) {\r
-                fileBuffer.append("RETURN_STATUS\r\n");\r
-                fileBuffer.append("EFIAPI\r\n");\r
-                fileBuffer.append(libInstanceList.get(i)[0]);\r
-                fileBuffer.append(" (\r\n");\r
-                fileBuffer.append("  VOID\r\n");\r
-                fileBuffer.append("  );\r\n");\r
+                functionDeclarations.append("RETURN_STATUS\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  VOID\r\n");\r
+                functionDeclarations.append("  );\r\n");\r
             } else {\r
                 switch (CommonDefinition.getModuleType(moduleType)) {\r
                 case CommonDefinition.ModuleTypeBase:\r
-                    fileBuffer.append("RETURN_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(libInstanceList.get(i)[0]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  VOID\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("RETURN_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  VOID\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     break;\r
                 case CommonDefinition.ModuleTypePeiCore:\r
                 case CommonDefinition.ModuleTypePeim:\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(libInstanceList.get(i)[0]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
-                    fileBuffer.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_FFS_FILE_HEADER       *FfsHeader,\r\n");\r
+                    functionDeclarations.append("  IN EFI_PEI_SERVICES          **PeiServices\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     break;\r
                 case CommonDefinition.ModuleTypeDxeCore:\r
                 case CommonDefinition.ModuleTypeDxeDriver:\r
@@ -1578,13 +1623,13 @@ public class AutoGen {
                 case CommonDefinition.ModuleTypeDxeSalDriver:\r
                 case CommonDefinition.ModuleTypeUefiDriver:\r
                 case CommonDefinition.ModuleTypeUefiApplication:\r
-                    fileBuffer.append("EFI_STATUS\r\n");\r
-                    fileBuffer.append("EFIAPI\r\n");\r
-                    fileBuffer.append(libInstanceList.get(i)[0]);\r
-                    fileBuffer.append(" (\r\n");\r
-                    fileBuffer.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
-                    fileBuffer.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
-                    fileBuffer.append("  );\r\n");\r
+                    functionDeclarations.append("EFI_STATUS\r\n");\r
+                    functionDeclarations.append("EFIAPI\r\n");\r
+                    functionDeclarations.append(libInstanceList.get(i)[0]);\r
+                    functionDeclarations.append(" (\r\n");\r
+                    functionDeclarations.append("  IN EFI_HANDLE        ImageHandle,\r\n");\r
+                    functionDeclarations.append("  IN EFI_SYSTEM_TABLE  *SystemTable\r\n");\r
+                    functionDeclarations.append("  );\r\n");\r
                     break;\r
                 }\r
             }\r
@@ -1669,9 +1714,9 @@ public class AutoGen {
         //\r
         for (int i = 0; i < driverBindingGroup.length; i++) {\r
             if (driverBindingGroup[i][0] != null) {\r
-                fileBuffer.append("extern EFI_DRIVER_BINDING_PROTOCOL ");\r
-                fileBuffer.append(driverBindingGroup[i][0]);\r
-                fileBuffer.append(";\r\n");\r
+                globalDeclarations.append("extern EFI_DRIVER_BINDING_PROTOCOL ");\r
+                globalDeclarations.append(driverBindingGroup[i][0]);\r
+                globalDeclarations.append(";\r\n");\r
             }\r
         }\r
 \r
@@ -1683,9 +1728,9 @@ public class AutoGen {
                 if (driverBindingGroup[i][1]!= null) {\r
                     if (driverBindingGroup[i][0] != null) {\r
                         BitMask |= 0x01;\r
-                        fileBuffer.append("extern EFI_COMPONENT_NAME_PROTOCOL ");\r
-                        fileBuffer.append(driverBindingGroup[i][1]);\r
-                        fileBuffer.append(";\r\n");\r
+                        globalDeclarations.append("extern EFI_COMPONENT_NAME_PROTOCOL ");\r
+                        globalDeclarations.append(driverBindingGroup[i][1]);\r
+                        globalDeclarations.append(";\r\n");\r
                     } else {\r
                         throw new AutoGenException("DriverBinding can't be empty!!");\r
                     }\r
@@ -1700,9 +1745,9 @@ public class AutoGen {
             if (driverBindingGroup[i][2] != null) {\r
                 if (driverBindingGroup[i][0] != null) {\r
                     BitMask |= 0x02;\r
-                    fileBuffer.append("extern EFI_DRIVER_CONFIGURATION_PROTOCOL ");\r
-                    fileBuffer.append(driverBindingGroup[i][2]);\r
-                    fileBuffer.append(";\r\n");\r
+                    globalDeclarations.append("extern EFI_DRIVER_CONFIGURATION_PROTOCOL ");\r
+                    globalDeclarations.append(driverBindingGroup[i][2]);\r
+                    globalDeclarations.append(";\r\n");\r
                 } else {\r
                     throw new AutoGenException("DriverBinding can't be empty!!");\r
                 }\r
@@ -1717,9 +1762,9 @@ public class AutoGen {
                 if (driverBindingGroup[i][3] != null) {\r
                     if (driverBindingGroup[i][0] != null) {\r
                         BitMask |= 0x04;\r
-                        fileBuffer.append("extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL ");\r
-                        fileBuffer.append(driverBindingGroup[i][3]);\r
-                        fileBuffer.append(";\r\n");\r
+                        globalDeclarations.append("extern EFI_DRIVER_DIAGNOSTICS_PROTOCOL ");\r
+                        globalDeclarations.append(driverBindingGroup[i][3]);\r
+                        globalDeclarations.append(";\r\n");\r
                     } else {\r
                         throw new AutoGenException("DriverBinding can't be empty!!");\r
                     }\r
@@ -1803,6 +1848,73 @@ public class AutoGen {
         }\r
     }\r
 \r
+    /**\r
+      ExternCallBackToAutoGenC\r
+     \r
+      This function adds <SetVirtualAddressMapCallBack> and\r
+      <ExitBootServicesCallBack> infomation to AutoGen.c\r
+     \r
+      @param fileBuffer\r
+                 String buffer for AutoGen.c\r
+      @throws BuildException\r
+    **/\r
+    void ExternCallBackToAutoGenH(String moduleType)\r
+    throws EdkException {\r
+        //\r
+        // Collect module's <SetVirtualAddressMapCallBack> and\r
+        // <ExitBootServiceCallBack> and add to setVirtualAddList\r
+        //  exitBootServiceList.\r
+        //\r
+        String[] setVirtuals = saq.getSetVirtualAddressMapCallBackArray();\r
+        String[] exitBoots = saq.getExitBootServicesCallBackArray();\r
+        //\r
+        //  Add c code in autogen.c which relate to <SetVirtualAddressMapCallBack>\r
+        //  and <ExitBootServicesCallBack>\r
+        //\r
+        switch (CommonDefinition.getModuleType(moduleType)) {\r
+        case CommonDefinition.ModuleTypeDxeDriver:\r
+        case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
+        case CommonDefinition.ModuleTypeDxeSalDriver:\r
+        case CommonDefinition.ModuleTypeUefiDriver:\r
+        case CommonDefinition.ModuleTypeUefiApplication:\r
+            //\r
+            // Write SetVirtualAddressMap function definition.\r
+            //\r
+            for (int i = 0; setVirtuals != null && i < setVirtuals.length; i++) {\r
+                if (setVirtuals[i].equalsIgnoreCase("")) {\r
+                    continue;\r
+                }\r
+                functionDeclarations.append("VOID\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(setVirtuals[i]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_EVENT  Event,\r\n");\r
+                functionDeclarations.append("  IN VOID       *Context\r\n");\r
+                functionDeclarations.append("  );\r\n\r\n");\r
+            }\r
+\r
+            //\r
+            // Write DriverExitBootServices function definition.\r
+            //\r
+            for (int i = 0; exitBoots != null && i < exitBoots.length; i++) {\r
+                if (exitBoots[i].equalsIgnoreCase("")) {\r
+                    continue;\r
+                }\r
+\r
+                functionDeclarations.append("VOID\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(exitBoots[i]);\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_EVENT  Event,\r\n");\r
+                functionDeclarations.append("  IN VOID       *Context\r\n");\r
+                functionDeclarations.append("  );\r\n\r\n");\r
+            }\r
+            break;\r
+        default:\r
+            break;\r
+        }\r
+    }\r
+\r
     /**\r
       ExternCallBackToAutoGenC\r
      \r
@@ -1842,7 +1954,6 @@ public class AutoGen {
         case CommonDefinition.ModuleTypeDxeRuntimeDriver:\r
         case CommonDefinition.ModuleTypeDxeSalDriver:\r
         case CommonDefinition.ModuleTypeUefiDriver:\r
-        case CommonDefinition.ModuleTypeUefiApplication:\r
             //\r
             //  If moduleType is one of above, call setVirtualAddressToAutogenC,\r
             //  and setExitBootServiceToAutogenC.\r
@@ -1905,6 +2016,7 @@ public class AutoGen {
     private void collectLibInstanceInfo() throws EdkException{\r
         int index;\r
 \r
+        String moduleType = moduleId.getModuleType();\r
         String libConstructName = null;\r
         String libDestructName = null;\r
         String libModuleType   = null;\r
@@ -1912,114 +2024,102 @@ public class AutoGen {
         String[] exitBoots = null;\r
 \r
         ModuleIdentification[] libraryIdList = saq.getLibraryInstance(this.arch);\r
+        if (libraryIdList.length <= 0) {\r
+            return;\r
+        }\r
+        //\r
+        // Reorder library instance sequence.\r
+        //\r
+        AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList, this.arch);\r
+        List<ModuleIdentification> orderList = libOrder.orderLibInstance();\r
+        //\r
+        // Process library instance one by one.\r
+        //\r
+        for (int i = 0; i < orderList.size(); i++) {\r
+            //\r
+            // Get library instance basename.\r
+            //\r
+            ModuleIdentification libInstanceId = orderList.get(i);\r
 \r
-        if (libraryIdList != null) {\r
             //\r
-            // Reorder library instance sequence.\r
+            // Get override map\r
             //\r
-            AutogenLibOrder libOrder = new AutogenLibOrder(libraryIdList,\r
-                                                           this.arch);\r
-            List<ModuleIdentification> orderList = libOrder\r
-                                                   .orderLibInstance();\r
-\r
-            if (orderList != null) {\r
-                //\r
-                // Process library instance one by one.\r
-                //\r
-                for (int i = 0; i < orderList.size(); i++) {\r
-                    //\r
-                    // Get library instance basename.\r
-                    //\r
-                    ModuleIdentification libInstanceId = orderList.get(i);\r
-\r
-                    //\r
-                    // Get override map\r
-                    //\r
-\r
-                    Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstanceId, this.arch);\r
-                    saq.push(libDoc);\r
-                    //\r
-                    // Get <PPis>, <Protocols>, <Guids> list of this library\r
-                    // instance.\r
-                    //\r
-                    String[] ppiList = saq.getPpiArray(this.arch);\r
-                    String[] ppiNotifyList = saq.getPpiNotifyArray(this.arch);\r
-                    String[] protocolList = saq.getProtocolArray(this.arch);\r
-                    String[] protocolNotifyList = saq.getProtocolNotifyArray(this.arch);\r
-                    String[] guidList = saq.getGuidEntryArray(this.arch);\r
-                    PackageIdentification[] pkgList = saq.getDependencePkg(this.arch);\r
-\r
-                    //\r
-                    // Add those ppi, protocol, guid in global ppi,\r
-                    // protocol, guid\r
-                    // list.\r
-                    //\r
-                    for (index = 0; index < ppiList.length; index++) {\r
-                        this.mPpiList.add(ppiList[index]);\r
-                    }\r
 \r
-                    for (index = 0; index < ppiNotifyList.length; index++) {\r
-                        this.mPpiList.add(ppiNotifyList[index]);\r
-                    }\r
+            Map<String, XmlObject> libDoc = GlobalData.getDoc(libInstanceId, this.arch);\r
+            saq.push(libDoc);\r
+            //\r
+            // check if the library instance support current module\r
+            // \r
+            String[] libraryClassList = saq.getLibraryClasses(CommonDefinition.ALWAYSPRODUCED,\r
+                                                              this.arch,\r
+                                                              moduleType\r
+                                                             );\r
+            if (libraryClassList.length <= 0) {\r
+                throw new EdkException("Library instance " + libInstanceId.getName() \r
+                                       + " doesn't support module type " + moduleType);\r
+            }\r
+            //\r
+            // Get CName list from <PPis>, <Protocols>, <Guids>, etc. of this library\r
+            // instance.\r
+            //\r
+            String[] guidCNameList = saq.getCNameArray(this.arch);            \r
+            PackageIdentification[] pkgList = saq.getDependencePkg(this.arch);\r
 \r
-                    for (index = 0; index < protocolList.length; index++) {\r
-                        this.mProtocolList.add(protocolList[index]);\r
-                    }\r
+            //\r
+            // Add those ppi, protocol, guid in global ppi,\r
+            // protocol, guid\r
+            // list.\r
+            //\r
+            for (index = 0; index < guidCNameList.length; index++) {\r
+                this.mGuidCNameList.add(guidCNameList[index]);\r
+            }\r
 \r
-                    for (index = 0; index < protocolNotifyList.length; index++) {\r
-                        this.mProtocolList.add(protocolNotifyList[index]);\r
-                    }\r
+            for (index = 0; index < pkgList.length; index++) {\r
+                if (!this.mDepPkgList.contains(pkgList[index])) {\r
+                    this.mDepPkgList.add(pkgList[index]);\r
+                }\r
+            }\r
 \r
-                    for (index = 0; index < guidList.length; index++) {\r
-                        this.mGuidList.add(guidList[index]);\r
-                    }\r
-                    for (index = 0; index < pkgList.length; index++) {\r
-                        if (!this.mDepPkgList.contains(pkgList[index])) {\r
-                            this.mDepPkgList.add(pkgList[index]);\r
-                        }\r
-                    }\r
+            //\r
+            // If not yet parse this library instance's constructor\r
+            // element,parse it.\r
+            //\r
+            libConstructName = saq.getLibConstructorName();\r
+            libDestructName = saq.getLibDestructorName();\r
+            libModuleType = saq.getModuleType();\r
 \r
-                    //\r
-                    // If not yet parse this library instance's constructor\r
-                    // element,parse it.\r
-                    //\r
-                    libConstructName = saq.getLibConstructorName();\r
-                    libDestructName = saq.getLibDestructorName();\r
-                    libModuleType = saq.getModuleType();\r
-\r
-                    //\r
-                    // Collect SetVirtualAddressMapCallBack and\r
-                    // ExitBootServiceCallBack.\r
-                    //\r
-                    setVirtuals = saq.getSetVirtualAddressMapCallBackArray();\r
-                    exitBoots = saq.getExitBootServicesCallBackArray();\r
-                    if (setVirtuals != null) {\r
-                        for (int j = 0; j < setVirtuals.length; j++) {\r
-                            this.setVirtalAddList.add(setVirtuals[j]);\r
-                        }\r
-                    }\r
-                    if (exitBoots != null) {\r
-                        for (int k = 0; k < exitBoots.length; k++) {\r
-                            this.exitBootServiceList.add(exitBoots[k]);\r
-                        }\r
-                    }\r
-                    saq.pop();\r
-                    //\r
-                    // Add dependent library instance constructor function.\r
-                    //\r
-                    if (libConstructName != null) {\r
-                        this.libConstructList.add(new String[] {libConstructName, libModuleType});\r
-                    }\r
-                    //\r
-                    // Add dependent library instance destructor fuction.\r
-                    //\r
-                    if (libDestructName != null) {\r
-                        this.libDestructList.add(new String[] {libDestructName, libModuleType});\r
-                    }\r
+            //\r
+            // Collect SetVirtualAddressMapCallBack and\r
+            // ExitBootServiceCallBack.\r
+            //\r
+            setVirtuals = saq.getSetVirtualAddressMapCallBackArray();\r
+            exitBoots = saq.getExitBootServicesCallBackArray();\r
+            if (setVirtuals != null) {\r
+                for (int j = 0; j < setVirtuals.length; j++) {\r
+                    this.setVirtalAddList.add(setVirtuals[j]);\r
                 }\r
             }\r
+            if (exitBoots != null) {\r
+                for (int k = 0; k < exitBoots.length; k++) {\r
+                    this.exitBootServiceList.add(exitBoots[k]);\r
+                }\r
+            }\r
+            saq.pop();\r
+            //\r
+            // Add dependent library instance constructor function.\r
+            //\r
+            if (libConstructName != null) {\r
+                this.libConstructList.add(new String[] {libConstructName, libModuleType});\r
+            }\r
+            //\r
+            // Add dependent library instance destructor fuction.\r
+            //\r
+            if (libDestructName != null) {\r
+                this.libDestructList.add(new String[] {libDestructName, libModuleType});\r
+            }\r
         }\r
     }\r
+\r
     private void setVirtualAddressToAutogenC(StringBuffer fileBuffer){\r
         //\r
         // Entry point lib for these module types needs to know the count\r
@@ -2059,13 +2159,13 @@ public class AutoGen {
                 if (this.setVirtalAddList.get(i).equalsIgnoreCase("")) {\r
                     break;\r
                 }\r
-                fileBuffer.append("VOID\r\n");\r
-                fileBuffer.append("EFIAPI\r\n");\r
-                fileBuffer.append(this.setVirtalAddList.get(i));\r
-                fileBuffer.append(" (\r\n");\r
-                fileBuffer.append("  IN EFI_EVENT  Event,\r\n");\r
-                fileBuffer.append("  IN VOID       *Context\r\n");\r
-                fileBuffer.append("  );\r\n\r\n");\r
+                functionDeclarations.append("VOID\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(this.setVirtalAddList.get(i));\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_EVENT  Event,\r\n");\r
+                functionDeclarations.append("  IN VOID       *Context\r\n");\r
+                functionDeclarations.append("  );\r\n\r\n");\r
             }\r
 \r
             //\r
@@ -2098,8 +2198,7 @@ public class AutoGen {
         //\r
         // Entry point lib for these module types needs to know the count.\r
         //\r
-        fileBuffer\r
-        .append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED  const UINTN _gDriverExitBootServicesEventCount = ");\r
+        fileBuffer.append("\r\nGLOBAL_REMOVE_IF_UNREFERENCED  const UINTN _gDriverExitBootServicesEventCount = ");\r
 \r
         //\r
         // If the list is not valid or has no entries set count to zero else\r
@@ -2134,13 +2233,13 @@ public class AutoGen {
                     break;\r
                 }\r
 \r
-                fileBuffer.append("VOID\r\n");\r
-                fileBuffer.append("EFIAPI\r\n");\r
-                fileBuffer.append(this.exitBootServiceList.get(i));\r
-                fileBuffer.append(" (\r\n");\r
-                fileBuffer.append("  IN EFI_EVENT  Event,\r\n");\r
-                fileBuffer.append("  IN VOID       *Context\r\n");\r
-                fileBuffer.append("  );\r\n\r\n");\r
+                functionDeclarations.append("VOID\r\n");\r
+                functionDeclarations.append("EFIAPI\r\n");\r
+                functionDeclarations.append(this.exitBootServiceList.get(i));\r
+                functionDeclarations.append(" (\r\n");\r
+                functionDeclarations.append("  IN EFI_EVENT  Event,\r\n");\r
+                functionDeclarations.append("  IN VOID       *Context\r\n");\r
+                functionDeclarations.append("  );\r\n\r\n");\r
             }\r
 \r
             //\r