]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools:Change the path of the file that Binary Cache
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / AutoGen.py
index f9ce17cf77b480a0c98c89826709e143b0a22e20..a5bef4f7c62a0ec3ab7346460bce44c61cc93349 100644 (file)
@@ -5,13 +5,7 @@
 # Copyright (c) 2018, Hewlett Packard Enterprise Development, L.P.<BR>\r
 # Copyright (c) 2019, American Megatrends, Inc. All rights reserved.<BR>\r
 #\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
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
 #\r
 \r
 ## Import Modules\r
@@ -474,7 +468,7 @@ class WorkspaceAutoGen(AutoGen):
 \r
             # generate the SourcePcdDict and BinaryPcdDict\r
             PGen = PlatformAutoGen(self, self.MetaFile, Target, Toolchain, Arch)\r
-            for BuildData in PGen.BuildDatabase._CACHE_.values():\r
+            for BuildData in list(PGen.BuildDatabase._CACHE_.values()):\r
                 if BuildData.Arch != Arch:\r
                     continue\r
                 if BuildData.MetaFile.Ext == '.inf':\r
@@ -580,7 +574,7 @@ class WorkspaceAutoGen(AutoGen):
                                 if NewPcd2 not in GlobalData.MixedPcd[item]:\r
                                     GlobalData.MixedPcd[item].append(NewPcd2)\r
 \r
-            for BuildData in PGen.BuildDatabase._CACHE_.values():\r
+            for BuildData in list(PGen.BuildDatabase._CACHE_.values()):\r
                 if BuildData.Arch != Arch:\r
                     continue\r
                 for key in BuildData.Pcds:\r
@@ -661,7 +655,7 @@ class WorkspaceAutoGen(AutoGen):
             #\r
             # Generate Package level hash value\r
             #\r
-            GlobalData.gPackageHash[Arch] = {}\r
+            GlobalData.gPackageHash = {}\r
             if GlobalData.gUseHashCache:\r
                 for Pkg in Pkgs:\r
                     self._GenPkgLevelHash(Pkg)\r
@@ -726,11 +720,11 @@ class WorkspaceAutoGen(AutoGen):
             for files in AllWorkSpaceMetaFiles:\r
                 if files.endswith('.dec'):\r
                     continue\r
-                f = open(files, 'r')\r
+                f = open(files, 'rb')\r
                 Content = f.read()\r
                 f.close()\r
                 m.update(Content)\r
-            SaveFileOnChange(os.path.join(self.BuildDir, 'AutoGen.hash'), m.hexdigest(), True)\r
+            SaveFileOnChange(os.path.join(self.BuildDir, 'AutoGen.hash'), m.hexdigest(), False)\r
             GlobalData.gPlatformHash = m.hexdigest()\r
 \r
         #\r
@@ -747,7 +741,7 @@ class WorkspaceAutoGen(AutoGen):
         return True\r
 \r
     def _GenPkgLevelHash(self, Pkg):\r
-        if Pkg.PackageName in GlobalData.gPackageHash[Pkg.Arch]:\r
+        if Pkg.PackageName in GlobalData.gPackageHash:\r
             return\r
 \r
         PkgDir = os.path.join(self.BuildDir, Pkg.Arch, Pkg.PackageName)\r
@@ -755,7 +749,7 @@ class WorkspaceAutoGen(AutoGen):
         HashFile = os.path.join(PkgDir, Pkg.PackageName + '.hash')\r
         m = hashlib.md5()\r
         # Get .dec file's hash value\r
-        f = open(Pkg.MetaFile.Path, 'r')\r
+        f = open(Pkg.MetaFile.Path, 'rb')\r
         Content = f.read()\r
         f.close()\r
         m.update(Content)\r
@@ -765,12 +759,12 @@ class WorkspaceAutoGen(AutoGen):
                 for Root, Dirs, Files in os.walk(str(inc)):\r
                     for File in sorted(Files):\r
                         File_Path = os.path.join(Root, File)\r
-                        f = open(File_Path, 'r')\r
+                        f = open(File_Path, 'rb')\r
                         Content = f.read()\r
                         f.close()\r
                         m.update(Content)\r
-        SaveFileOnChange(HashFile, m.hexdigest(), True)\r
-        GlobalData.gPackageHash[Pkg.Arch][Pkg.PackageName] = m.hexdigest()\r
+        SaveFileOnChange(HashFile, m.hexdigest(), False)\r
+        GlobalData.gPackageHash[Pkg.PackageName] = m.hexdigest()\r
 \r
     def _GetMetaFiles(self, Target, Toolchain, Arch):\r
         AllWorkSpaceMetaFiles = set()\r
@@ -929,7 +923,7 @@ class WorkspaceAutoGen(AutoGen):
     def _CheckAllPcdsTokenValueConflict(self):\r
         for Pa in self.AutoGenObjectList:\r
             for Package in Pa.PackageList:\r
-                PcdList = Package.Pcds.values()\r
+                PcdList = list(Package.Pcds.values())\r
                 PcdList.sort(key=lambda x: int(x.TokenValue, 0))\r
                 Count = 0\r
                 while (Count < len(PcdList) - 1) :\r
@@ -975,7 +969,7 @@ class WorkspaceAutoGen(AutoGen):
                         Count += SameTokenValuePcdListCount\r
                     Count += 1\r
 \r
-                PcdList = Package.Pcds.values()\r
+                PcdList = list(Package.Pcds.values())\r
                 PcdList.sort(key=lambda x: "%s.%s" % (x.TokenSpaceGuidCName, x.TokenCName))\r
                 Count = 0\r
                 while (Count < len(PcdList) - 1) :\r
@@ -1141,7 +1135,6 @@ class PlatformAutoGen(AutoGen):
         self.BuildTarget = Target\r
         self.Arch = Arch\r
         self.SourceDir = PlatformFile.SubDir\r
-        self.SourceOverrideDir = None\r
         self.FdTargetList = self.Workspace.FdTargetList\r
         self.FvTargetList = self.Workspace.FvTargetList\r
         # get the original module/package/platform objects\r
@@ -1187,7 +1180,7 @@ class PlatformAutoGen(AutoGen):
     #\r
     @cached_class_function\r
     def CreateCodeFile(self, CreateModuleCodeFile=False):\r
-        # only module has code to be greated, so do nothing if CreateModuleCodeFile is False\r
+        # only module has code to be created, so do nothing if CreateModuleCodeFile is False\r
         if not CreateModuleCodeFile:\r
             return\r
 \r
@@ -1300,7 +1293,7 @@ class PlatformAutoGen(AutoGen):
                 if os.path.exists(VpdMapFilePath):\r
                     OrgVpdFile.Read(VpdMapFilePath)\r
                     PcdItems = OrgVpdFile.GetOffset(PcdNvStoreDfBuffer[0])\r
-                    NvStoreOffset = PcdItems.values()[0].strip() if PcdItems else '0'\r
+                    NvStoreOffset = list(PcdItems.values())[0].strip() if PcdItems else '0'\r
                 else:\r
                     EdkLogger.error("build", FILE_READ_FAILURE, "Can not find VPD map file %s to fix up VPD offset." % VpdMapFilePath)\r
 \r
@@ -1499,7 +1492,7 @@ class PlatformAutoGen(AutoGen):
         if (self.Workspace.ArchList[-1] == self.Arch):\r
             for Pcd in self._DynamicPcdList:\r
                 # just pick the a value to determine whether is unicode string type\r
-                Sku = Pcd.SkuInfoList.values()[0]\r
+                Sku = Pcd.SkuInfoList.get(TAB_DEFAULT)\r
                 Sku.VpdOffset = Sku.VpdOffset.strip()\r
 \r
                 if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:\r
@@ -1605,7 +1598,7 @@ class PlatformAutoGen(AutoGen):
                         if not FoundFlag :\r
                             # just pick the a value to determine whether is unicode string type\r
                             SkuValueMap = {}\r
-                            SkuObjList = DscPcdEntry.SkuInfoList.items()\r
+                            SkuObjList = list(DscPcdEntry.SkuInfoList.items())\r
                             DefaultSku = DscPcdEntry.SkuInfoList.get(TAB_DEFAULT)\r
                             if DefaultSku:\r
                                 defaultindex = SkuObjList.index((TAB_DEFAULT, DefaultSku))\r
@@ -1631,7 +1624,7 @@ class PlatformAutoGen(AutoGen):
                                             DscPcdEntry.TokenSpaceGuidValue = eachDec.Guids[DecPcdEntry.TokenSpaceGuidCName]\r
                                             # Only fix the value while no value provided in DSC file.\r
                                             if not Sku.DefaultValue:\r
-                                                DscPcdEntry.SkuInfoList[DscPcdEntry.SkuInfoList.keys()[0]].DefaultValue = DecPcdEntry.DefaultValue\r
+                                                DscPcdEntry.SkuInfoList[list(DscPcdEntry.SkuInfoList.keys())[0]].DefaultValue = DecPcdEntry.DefaultValue\r
 \r
                                 if DscPcdEntry not in self._DynamicPcdList:\r
                                     self._DynamicPcdList.append(DscPcdEntry)\r
@@ -1688,7 +1681,7 @@ class PlatformAutoGen(AutoGen):
                     PcdName,PcdGuid = PcdNvStoreDfBuffer[0].TokenCName, PcdNvStoreDfBuffer[0].TokenSpaceGuidCName\r
                     if (PcdName,PcdGuid) in VpdSkuMap:\r
                         DefaultSku = PcdNvStoreDfBuffer[0].SkuInfoList.get(TAB_DEFAULT)\r
-                        VpdSkuMap[(PcdName,PcdGuid)] = {DefaultSku.DefaultValue:[DefaultSku]}\r
+                        VpdSkuMap[(PcdName,PcdGuid)] = {DefaultSku.DefaultValue:[SkuObj for SkuObj in PcdNvStoreDfBuffer[0].SkuInfoList.values() ]}\r
 \r
                 # Process VPD map file generated by third party BPDG tool\r
                 if NeedProcessVpdMapFile:\r
@@ -1713,7 +1706,7 @@ class PlatformAutoGen(AutoGen):
             # Delete the DynamicPcdList At the last time enter into this function\r
             for Pcd in self._DynamicPcdList:\r
                 # just pick the a value to determine whether is unicode string type\r
-                Sku = Pcd.SkuInfoList.values()[0]\r
+                Sku = Pcd.SkuInfoList.get(TAB_DEFAULT)\r
                 Sku.VpdOffset = Sku.VpdOffset.strip()\r
 \r
                 if Pcd.DatumType not in [TAB_UINT8, TAB_UINT16, TAB_UINT32, TAB_UINT64, TAB_VOID, "BOOLEAN"]:\r
@@ -1736,7 +1729,7 @@ class PlatformAutoGen(AutoGen):
         for pcd in self._DynamicPcdList:\r
             if len(pcd.SkuInfoList) == 1:\r
                 for (SkuName, SkuId) in allskuset:\r
-                    if type(SkuId) in (str, unicode) and eval(SkuId) == 0 or SkuId == 0:\r
+                    if isinstance(SkuId, str) and eval(SkuId) == 0 or SkuId == 0:\r
                         continue\r
                     pcd.SkuInfoList[SkuName] = copy.deepcopy(pcd.SkuInfoList[TAB_DEFAULT])\r
                     pcd.SkuInfoList[SkuName].SkuId = SkuId\r
@@ -1849,7 +1842,7 @@ class PlatformAutoGen(AutoGen):
 \r
     ## Get tool chain definition\r
     #\r
-    #  Get each tool defition for given tool chain from tools_def.txt and platform\r
+    #  Get each tool definition for given tool chain from tools_def.txt and platform\r
     #\r
     @cached_property\r
     def ToolDefinition(self):\r
@@ -1906,7 +1899,7 @@ class PlatformAutoGen(AutoGen):
                         ToolsDef += "%s_%s = %s\n" % (Tool, Attr, Value)\r
             ToolsDef += "\n"\r
 \r
-        SaveFileOnChange(self.ToolDefinitionFile, ToolsDef)\r
+        SaveFileOnChange(self.ToolDefinitionFile, ToolsDef, False)\r
         for DllPath in DllPathList:\r
             os.environ["PATH"] = DllPath + os.pathsep + os.environ["PATH"]\r
         os.environ["MAKE_FLAGS"] = MakeFlags\r
@@ -2130,8 +2123,8 @@ class PlatformAutoGen(AutoGen):
 \r
     ## Override PCD setting (type, value, ...)\r
     #\r
-    #   @param  ToPcd       The PCD to be overrided\r
-    #   @param  FromPcd     The PCD overrideing from\r
+    #   @param  ToPcd       The PCD to be overridden\r
+    #   @param  FromPcd     The PCD overriding from\r
     #\r
     def _OverridePcd(self, ToPcd, FromPcd, Module="", Msg="", Library=""):\r
         #\r
@@ -2218,7 +2211,7 @@ class PlatformAutoGen(AutoGen):
 \r
     ## Apply PCD setting defined platform to a module\r
     #\r
-    #   @param  Module  The module from which the PCD setting will be overrided\r
+    #   @param  Module  The module from which the PCD setting will be overridden\r
     #\r
     #   @retval PCD_list    The list PCDs with settings from platform\r
     #\r
@@ -2286,7 +2279,7 @@ class PlatformAutoGen(AutoGen):
                     Pcd.MaxDatumSize = str(len(Value.split(',')))\r
                 else:\r
                     Pcd.MaxDatumSize = str(len(Value) - 1)\r
-        return Pcds.values()\r
+        return list(Pcds.values())\r
 \r
 \r
 \r
@@ -2355,7 +2348,7 @@ class PlatformAutoGen(AutoGen):
         # Use the highest priority value.\r
         #\r
         if (len(OverrideList) >= 2):\r
-            KeyList = OverrideList.keys()\r
+            KeyList = list(OverrideList.keys())\r
             for Index in range(len(KeyList)):\r
                 NowKey = KeyList[Index]\r
                 Target1, ToolChain1, Arch1, CommandType1, Attr1 = NowKey.split("_")\r
@@ -2450,7 +2443,7 @@ class PlatformAutoGen(AutoGen):
 \r
     ## Append build options in platform to a module\r
     #\r
-    #   @param  Module  The module to which the build options will be appened\r
+    #   @param  Module  The module to which the build options will be appended\r
     #\r
     #   @retval options     The options appended with build options in platform\r
     #\r
@@ -2473,9 +2466,9 @@ class PlatformAutoGen(AutoGen):
                     if Attr == TAB_TOD_DEFINES_BUILDRULEORDER:\r
                         BuildRuleOrder = Options[Tool][Attr]\r
 \r
-        AllTools = set(ModuleOptions.keys() + PlatformOptions.keys() +\r
-                       PlatformModuleOptions.keys() + ModuleTypeOptions.keys() +\r
-                       self.ToolDefinition.keys())\r
+        AllTools = set(list(ModuleOptions.keys()) + list(PlatformOptions.keys()) +\r
+                       list(PlatformModuleOptions.keys()) + list(ModuleTypeOptions.keys()) +\r
+                       list(self.ToolDefinition.keys()))\r
         BuildOptions = defaultdict(lambda: defaultdict(str))\r
         for Tool in AllTools:\r
             for Options in [self.ToolDefinition, ModuleOptions, PlatformOptions, ModuleTypeOptions, PlatformModuleOptions]:\r
@@ -2559,11 +2552,6 @@ class ModuleAutoGen(AutoGen):
         self.SourceDir = self.MetaFile.SubDir\r
         self.SourceDir = mws.relpath(self.SourceDir, self.WorkspaceDir)\r
 \r
-        self.SourceOverrideDir = None\r
-        # use overrided path defined in DSC file\r
-        if self.MetaFile.Key in GlobalData.gOverrideDir:\r
-            self.SourceOverrideDir = GlobalData.gOverrideDir[self.MetaFile.Key]\r
-\r
         self.ToolChain = Toolchain\r
         self.BuildTarget = Target\r
         self.Arch = Arch\r
@@ -2685,7 +2673,7 @@ class ModuleAutoGen(AutoGen):
     def Guid(self):\r
         #\r
         # To build same module more than once, the module path with FILE_GUID overridden has\r
-        # the file name FILE_GUIDmodule.inf, but the relative path (self.MetaFile.File) is the realy path\r
+        # the file name FILE_GUIDmodule.inf, but the relative path (self.MetaFile.File) is the real path\r
         # in DSC. The overridden GUID can be retrieved from file name\r
         #\r
         if os.path.basename(self.MetaFile.File) != os.path.basename(self.MetaFile.Path):\r
@@ -2745,7 +2733,7 @@ class ModuleAutoGen(AutoGen):
                                     self.MetaFile.BaseName\r
             ))\r
 \r
-    ## Return the directory to store the intermediate object files of the mdoule\r
+    ## Return the directory to store the intermediate object files of the module\r
     @cached_property\r
     def OutputDir(self):\r
         return _MakeDir((self.BuildDir, "OUTPUT"))\r
@@ -2757,7 +2745,7 @@ class ModuleAutoGen(AutoGen):
             return path.join(self.PlatformInfo.BuildDir, TAB_FV_DIRECTORY, "Ffs", self.Guid + self.Name)\r
         return ''\r
 \r
-    ## Return the directory to store auto-gened source files of the mdoule\r
+    ## Return the directory to store auto-gened source files of the module\r
     @cached_property\r
     def DebugDir(self):\r
         return _MakeDir((self.BuildDir, "DEBUG"))\r
@@ -2768,12 +2756,7 @@ class ModuleAutoGen(AutoGen):
         RetVal = {}\r
         for Type in self.Module.CustomMakefile:\r
             MakeType = gMakeTypeMap[Type] if Type in gMakeTypeMap else 'nmake'\r
-            if self.SourceOverrideDir is not None:\r
-                File = os.path.join(self.SourceOverrideDir, self.Module.CustomMakefile[Type])\r
-                if not os.path.exists(File):\r
-                    File = os.path.join(self.SourceDir, self.Module.CustomMakefile[Type])\r
-            else:\r
-                File = os.path.join(self.SourceDir, self.Module.CustomMakefile[Type])\r
+            File = os.path.join(self.SourceDir, self.Module.CustomMakefile[Type])\r
             RetVal[MakeType] = File\r
         return RetVal\r
 \r
@@ -2809,7 +2792,7 @@ class ModuleAutoGen(AutoGen):
 \r
     ## Get the depex string\r
     #\r
-    # @return : a string contain all depex expresion.\r
+    # @return : a string contain all depex expression.\r
     def _GetDepexExpresionString(self):\r
         DepexStr = ''\r
         DepexList = []\r
@@ -2819,11 +2802,11 @@ class ModuleAutoGen(AutoGen):
         for M in [self.Module] + self.DependentLibraryList:\r
             Filename = M.MetaFile.Path\r
             InfObj = InfSectionParser.InfSectionParser(Filename)\r
-            DepexExpresionList = InfObj.GetDepexExpresionList()\r
-            for DepexExpresion in DepexExpresionList:\r
-                for key in DepexExpresion:\r
+            DepexExpressionList = InfObj.GetDepexExpresionList()\r
+            for DepexExpression in DepexExpressionList:\r
+                for key in DepexExpression:\r
                     Arch, ModuleType = key\r
-                    DepexExpr = [x for x in DepexExpresion[key] if not str(x).startswith('#')]\r
+                    DepexExpr = [x for x in DepexExpression[key] if not str(x).startswith('#')]\r
                     # the type of build module is USER_DEFINED.\r
                     # All different DEPEX section tags would be copied into the As Built INF file\r
                     # and there would be separate DEPEX section tags\r
@@ -2878,7 +2861,7 @@ class ModuleAutoGen(AutoGen):
 \r
         DepexList = []\r
         #\r
-        # Append depex from dependent libraries, if not "BEFORE", "AFTER" expresion\r
+        # Append depex from dependent libraries, if not "BEFORE", "AFTER" expression\r
         #\r
         for M in [self.Module] + self.DependentLibraryList:\r
             Inherited = False\r
@@ -2892,10 +2875,16 @@ class ModuleAutoGen(AutoGen):
                     if '.' not in item:\r
                         NewList.append(item)\r
                     else:\r
-                        if item not in self.FixedVoidTypePcds:\r
+                        FixedVoidTypePcds = {}\r
+                        if item in self.FixedVoidTypePcds:\r
+                            FixedVoidTypePcds = self.FixedVoidTypePcds\r
+                        elif M in self.PlatformInfo.LibraryAutoGenList:\r
+                            Index = self.PlatformInfo.LibraryAutoGenList.index(M)\r
+                            FixedVoidTypePcds = self.PlatformInfo.LibraryAutoGenList[Index].FixedVoidTypePcds\r
+                        if item not in FixedVoidTypePcds:\r
                             EdkLogger.error("build", FORMAT_INVALID, "{} used in [Depex] section should be used as FixedAtBuild type and VOID* datum type in the module.".format(item))\r
                         else:\r
-                            Value = self.FixedVoidTypePcds[item]\r
+                            Value = FixedVoidTypePcds[item]\r
                             if len(Value.split(',')) != 16:\r
                                 EdkLogger.error("build", FORMAT_INVALID,\r
                                                 "{} used in [Depex] section should be used as FixedAtBuild type and VOID* datum type and 16 bytes in the module.".format(item))\r
@@ -3031,13 +3020,14 @@ class ModuleAutoGen(AutoGen):
             # EDK II modules must not reference header files outside of the packages they depend on or\r
             # within the module's directory tree. Report error if violation.\r
             #\r
-            for Path in IncPathList:\r
-                if (Path not in self.IncludePathList) and (CommonPath([Path, self.MetaFile.Dir]) != self.MetaFile.Dir):\r
-                    ErrMsg = "The include directory for the EDK II module in this line is invalid %s specified in %s FLAGS '%s'" % (Path, Tool, FlagOption)\r
-                    EdkLogger.error("build",\r
-                                    PARAMETER_INVALID,\r
-                                    ExtraData=ErrMsg,\r
-                                    File=str(self.MetaFile))\r
+            if GlobalData.gDisableIncludePathCheck == False:\r
+                for Path in IncPathList:\r
+                    if (Path not in self.IncludePathList) and (CommonPath([Path, self.MetaFile.Dir]) != self.MetaFile.Dir):\r
+                        ErrMsg = "The include directory for the EDK II module in this line is invalid %s specified in %s FLAGS '%s'" % (Path, Tool, FlagOption)\r
+                        EdkLogger.error("build",\r
+                                        PARAMETER_INVALID,\r
+                                        ExtraData=ErrMsg,\r
+                                        File=str(self.MetaFile))\r
             RetVal += IncPathList\r
         return RetVal\r
 \r
@@ -3303,7 +3293,7 @@ class ModuleAutoGen(AutoGen):
             AutoFile = PathClass(gAutoGenStringFileName % {"module_name":self.Name}, self.DebugDir)\r
             RetVal[AutoFile] = str(StringH)\r
             self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)\r
-        if UniStringBinBuffer is not None and UniStringBinBuffer.getvalue() != "":\r
+        if UniStringBinBuffer is not None and UniStringBinBuffer.getvalue() != b"":\r
             AutoFile = PathClass(gAutoGenStringFormFileName % {"module_name":self.Name}, self.OutputDir)\r
             RetVal[AutoFile] = UniStringBinBuffer.getvalue()\r
             AutoFile.IsBinary = True\r
@@ -3314,7 +3304,7 @@ class ModuleAutoGen(AutoGen):
             AutoFile = PathClass(gAutoGenImageDefFileName % {"module_name":self.Name}, self.DebugDir)\r
             RetVal[AutoFile] = str(StringIdf)\r
             self._ApplyBuildRule(AutoFile, TAB_UNKNOWN_FILE)\r
-        if IdfGenBinBuffer is not None and IdfGenBinBuffer.getvalue() != "":\r
+        if IdfGenBinBuffer is not None and IdfGenBinBuffer.getvalue() != b"":\r
             AutoFile = PathClass(gAutoGenIdfFileName % {"module_name":self.Name}, self.OutputDir)\r
             RetVal[AutoFile] = IdfGenBinBuffer.getvalue()\r
             AutoFile.IsBinary = True\r
@@ -3323,7 +3313,7 @@ class ModuleAutoGen(AutoGen):
             IdfGenBinBuffer.close()\r
         return RetVal\r
 \r
-    ## Return the list of library modules explicitly or implicityly used by this module\r
+    ## Return the list of library modules explicitly or implicitly used by this module\r
     @cached_property\r
     def DependentLibraryList(self):\r
         # only merge library classes and PCD for non-library module\r
@@ -3426,7 +3416,7 @@ class ModuleAutoGen(AutoGen):
                 RetVal.append(PackageDir)\r
             IncludesList = Package.Includes\r
             if Package._PrivateIncludes:\r
-                if not self.MetaFile.Path.startswith(PackageDir):\r
+                if not self.MetaFile.OriginalPath.Path.startswith(PackageDir):\r
                     IncludesList = list(set(Package.Includes).difference(set(Package._PrivateIncludes)))\r
             for Inc in IncludesList:\r
                 if Inc not in RetVal:\r
@@ -3519,7 +3509,7 @@ class ModuleAutoGen(AutoGen):
             return None\r
         MapFileName = os.path.join(self.OutputDir, self.Name + ".map")\r
         EfiFileName = os.path.join(self.OutputDir, self.Name + ".efi")\r
-        VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, VfrUniBaseName.values())\r
+        VfrUniOffsetList = GetVariableOffset(MapFileName, EfiFileName, list(VfrUniBaseName.values()))\r
         if not VfrUniOffsetList:\r
             return None\r
 \r
@@ -3532,7 +3522,7 @@ class ModuleAutoGen(AutoGen):
             EdkLogger.error("build", FILE_OPEN_FAILURE, "File open failed for %s" % UniVfrOffsetFileName, None)\r
 \r
         # Use a instance of BytesIO to cache data\r
-        fStringIO = BytesIO('')\r
+        fStringIO = BytesIO()\r
 \r
         for Item in VfrUniOffsetList:\r
             if (Item[0].find("Strings") != -1):\r
@@ -3541,9 +3531,8 @@ class ModuleAutoGen(AutoGen):
                 # GUID + Offset\r
                 # { 0x8913c5e0, 0x33f6, 0x4d86, { 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66 } }\r
                 #\r
-                UniGuid = [0xe0, 0xc5, 0x13, 0x89, 0xf6, 0x33, 0x86, 0x4d, 0x9b, 0xf1, 0x43, 0xef, 0x89, 0xfc, 0x6, 0x66]\r
-                UniGuid = [chr(ItemGuid) for ItemGuid in UniGuid]\r
-                fStringIO.write(''.join(UniGuid))\r
+                UniGuid = b'\xe0\xc5\x13\x89\xf63\x86M\x9b\xf1C\xef\x89\xfc\x06f'\r
+                fStringIO.write(UniGuid)\r
                 UniValue = pack ('Q', int (Item[1], 16))\r
                 fStringIO.write (UniValue)\r
             else:\r
@@ -3552,9 +3541,8 @@ class ModuleAutoGen(AutoGen):
                 # GUID + Offset\r
                 # { 0xd0bc7cb4, 0x6a47, 0x495f, { 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2 } };\r
                 #\r
-                VfrGuid = [0xb4, 0x7c, 0xbc, 0xd0, 0x47, 0x6a, 0x5f, 0x49, 0xaa, 0x11, 0x71, 0x7, 0x46, 0xda, 0x6, 0xa2]\r
-                VfrGuid = [chr(ItemGuid) for ItemGuid in VfrGuid]\r
-                fStringIO.write(''.join(VfrGuid))\r
+                VfrGuid = b'\xb4|\xbc\xd0Gj_I\xaa\x11q\x07F\xda\x06\xa2'\r
+                fStringIO.write(VfrGuid)\r
                 VfrValue = pack ('Q', int (Item[1], 16))\r
                 fStringIO.write (VfrValue)\r
         #\r
@@ -3814,7 +3802,7 @@ class ModuleAutoGen(AutoGen):
                         Padding = '0x00, '\r
                         if Unicode:\r
                             Padding = Padding * 2\r
-                            ArraySize = ArraySize / 2\r
+                            ArraySize = ArraySize // 2\r
                         if ArraySize < (len(PcdValue) + 1):\r
                             if Pcd.MaxSizeUserSet:\r
                                 EdkLogger.error("build", AUTOGEN_ERROR,\r
@@ -3896,8 +3884,8 @@ class ModuleAutoGen(AutoGen):
         AsBuiltInfDict['userextension_tianocore_item'] = UserExtStr\r
 \r
         # Generated depex expression section in comments.\r
-        DepexExpresion = self._GetDepexExpresionString()\r
-        AsBuiltInfDict['depexsection_item'] = DepexExpresion if DepexExpresion else ''\r
+        DepexExpression = self._GetDepexExpresionString()\r
+        AsBuiltInfDict['depexsection_item'] = DepexExpression if DepexExpression else ''\r
 \r
         AsBuiltInf = TemplateString()\r
         AsBuiltInf.Append(gAsBuiltInfHeaderString.Replace(AsBuiltInfDict))\r
@@ -3909,16 +3897,17 @@ class ModuleAutoGen(AutoGen):
             self.CopyModuleToCache()\r
 \r
     def CopyModuleToCache(self):\r
-        FileDir = path.join(GlobalData.gBinCacheDest, self.Arch, self.SourceDir, self.MetaFile.BaseName)\r
+        FileDir = path.join(GlobalData.gBinCacheDest, self.PlatformInfo.OutputDir, self.BuildTarget + "_" + self.ToolChain, self.Arch, self.SourceDir, self.MetaFile.BaseName)\r
         CreateDirectory (FileDir)\r
         HashFile = path.join(self.BuildDir, self.Name + '.hash')\r
-        ModuleFile = path.join(self.OutputDir, self.Name + '.inf')\r
         if os.path.exists(HashFile):\r
             shutil.copy2(HashFile, FileDir)\r
-        if os.path.exists(ModuleFile):\r
-            shutil.copy2(ModuleFile, FileDir)\r
+        if not self.IsLibrary:\r
+            ModuleFile = path.join(self.OutputDir, self.Name + '.inf')\r
+            if os.path.exists(ModuleFile):\r
+                shutil.copy2(ModuleFile, FileDir)\r
         if not self.OutputFile:\r
-            Ma = self.BuildDatabase[PathClass(ModuleFile), self.Arch, self.BuildTarget, self.ToolChain]\r
+            Ma = self.BuildDatabase[self.MetaFile, self.Arch, self.BuildTarget, self.ToolChain]\r
             self.OutputFile = Ma.Binaries\r
         if self.OutputFile:\r
             for File in self.OutputFile:\r
@@ -3926,17 +3915,27 @@ class ModuleAutoGen(AutoGen):
                 if not os.path.isabs(File):\r
                     File = os.path.join(self.OutputDir, File)\r
                 if os.path.exists(File):\r
-                    shutil.copy2(File, FileDir)\r
+                    sub_dir = os.path.relpath(File, self.OutputDir)\r
+                    destination_file = os.path.join(FileDir, sub_dir)\r
+                    destination_dir = os.path.dirname(destination_file)\r
+                    CreateDirectory(destination_dir)\r
+                    shutil.copy2(File, destination_dir)\r
 \r
     def AttemptModuleCacheCopy(self):\r
+        # If library or Module is binary do not skip by hash\r
         if self.IsBinaryModule:\r
             return False\r
-        FileDir = path.join(GlobalData.gBinCacheSource, self.Arch, self.SourceDir, self.MetaFile.BaseName)\r
+        # .inc is contains binary information so do not skip by hash as well\r
+        for f_ext in self.SourceFileList:\r
+            if '.inc' in str(f_ext):\r
+                return False\r
+        FileDir = path.join(GlobalData.gBinCacheSource, self.PlatformInfo.OutputDir, self.BuildTarget + "_" + self.ToolChain, self.Arch, self.SourceDir, self.MetaFile.BaseName)\r
         HashFile = path.join(FileDir, self.Name + '.hash')\r
         if os.path.exists(HashFile):\r
             f = open(HashFile, 'r')\r
             CacheHash = f.read()\r
             f.close()\r
+            self.GenModuleHash()\r
             if GlobalData.gModuleHash[self.Arch][self.Name]:\r
                 if CacheHash == GlobalData.gModuleHash[self.Arch][self.Name]:\r
                     for root, dir, files in os.walk(FileDir):\r
@@ -3945,7 +3944,11 @@ class ModuleAutoGen(AutoGen):
                                 shutil.copy2(HashFile, self.BuildDir)\r
                             else:\r
                                 File = path.join(root, f)\r
-                                shutil.copy2(File, self.OutputDir)\r
+                                sub_dir = os.path.relpath(File, FileDir)\r
+                                destination_file = os.path.join(self.OutputDir, sub_dir)\r
+                                destination_dir = os.path.dirname(destination_file)\r
+                                CreateDirectory(destination_dir)\r
+                                shutil.copy2(File, destination_dir)\r
                     if self.Name == "PcdPeim" or self.Name == "PcdDxe":\r
                         CreatePcdDatabaseCode(self, TemplateString(), TemplateString())\r
                     return True\r
@@ -4091,50 +4094,86 @@ class ModuleAutoGen(AutoGen):
         return RetVal\r
 \r
     def GenModuleHash(self):\r
+        # Initialize a dictionary for each arch type\r
         if self.Arch not in GlobalData.gModuleHash:\r
             GlobalData.gModuleHash[self.Arch] = {}\r
+\r
+        # Early exit if module or library has been hashed and is in memory\r
+        if self.Name in GlobalData.gModuleHash[self.Arch]:\r
+            return GlobalData.gModuleHash[self.Arch][self.Name].encode('utf-8')\r
+\r
+        # Initialze hash object\r
         m = hashlib.md5()\r
+\r
         # Add Platform level hash\r
-        m.update(GlobalData.gPlatformHash)\r
+        m.update(GlobalData.gPlatformHash.encode('utf-8'))\r
+\r
         # Add Package level hash\r
         if self.DependentPackageList:\r
             for Pkg in sorted(self.DependentPackageList, key=lambda x: x.PackageName):\r
-                if Pkg.PackageName in GlobalData.gPackageHash[self.Arch]:\r
-                    m.update(GlobalData.gPackageHash[self.Arch][Pkg.PackageName])\r
+                if Pkg.PackageName in GlobalData.gPackageHash:\r
+                    m.update(GlobalData.gPackageHash[Pkg.PackageName].encode('utf-8'))\r
 \r
         # Add Library hash\r
         if self.LibraryAutoGenList:\r
             for Lib in sorted(self.LibraryAutoGenList, key=lambda x: x.Name):\r
                 if Lib.Name not in GlobalData.gModuleHash[self.Arch]:\r
                     Lib.GenModuleHash()\r
-                m.update(GlobalData.gModuleHash[self.Arch][Lib.Name])\r
+                m.update(GlobalData.gModuleHash[self.Arch][Lib.Name].encode('utf-8'))\r
 \r
         # Add Module self\r
-        f = open(str(self.MetaFile), 'r')\r
+        f = open(str(self.MetaFile), 'rb')\r
         Content = f.read()\r
         f.close()\r
         m.update(Content)\r
+\r
         # Add Module's source files\r
         if self.SourceFileList:\r
             for File in sorted(self.SourceFileList, key=lambda x: str(x)):\r
-                f = open(str(File), 'r')\r
+                f = open(str(File), 'rb')\r
                 Content = f.read()\r
                 f.close()\r
                 m.update(Content)\r
 \r
-        ModuleHashFile = path.join(self.BuildDir, self.Name + ".hash")\r
-        if self.Name not in GlobalData.gModuleHash[self.Arch]:\r
-            GlobalData.gModuleHash[self.Arch][self.Name] = m.hexdigest()\r
-        if GlobalData.gBinCacheSource:\r
-            if self.AttemptModuleCacheCopy():\r
-                return False\r
-        return SaveFileOnChange(ModuleHashFile, m.hexdigest(), True)\r
+        GlobalData.gModuleHash[self.Arch][self.Name] = m.hexdigest()\r
+\r
+        return GlobalData.gModuleHash[self.Arch][self.Name].encode('utf-8')\r
 \r
     ## Decide whether we can skip the ModuleAutoGen process\r
     def CanSkipbyHash(self):\r
-        if GlobalData.gUseHashCache:\r
-            return not self.GenModuleHash()\r
-        return False\r
+        # Hashing feature is off\r
+        if not GlobalData.gUseHashCache:\r
+            return False\r
+\r
+        # Initialize a dictionary for each arch type\r
+        if self.Arch not in GlobalData.gBuildHashSkipTracking:\r
+            GlobalData.gBuildHashSkipTracking[self.Arch] = dict()\r
+\r
+        # If library or Module is binary do not skip by hash\r
+        if self.IsBinaryModule:\r
+            return False\r
+\r
+        # .inc is contains binary information so do not skip by hash as well\r
+        for f_ext in self.SourceFileList:\r
+            if '.inc' in str(f_ext):\r
+                return False\r
+\r
+        # Use Cache, if exists and if Module has a copy in cache\r
+        if GlobalData.gBinCacheSource and self.AttemptModuleCacheCopy():\r
+            return True\r
+\r
+        # Early exit for libraries that haven't yet finished building\r
+        HashFile = path.join(self.BuildDir, self.Name + ".hash")\r
+        if self.IsLibrary and not os.path.exists(HashFile):\r
+            return False\r
+\r
+        # Return a Boolean based on if can skip by hash, either from memory or from IO.\r
+        if self.Name not in GlobalData.gBuildHashSkipTracking[self.Arch]:\r
+            # If hashes are the same, SaveFileOnChange() will return False.\r
+            GlobalData.gBuildHashSkipTracking[self.Arch][self.Name] = not SaveFileOnChange(HashFile, self.GenModuleHash(), True)\r
+            return GlobalData.gBuildHashSkipTracking[self.Arch][self.Name]\r
+        else:\r
+            return GlobalData.gBuildHashSkipTracking[self.Arch][self.Name]\r
 \r
     ## Decide whether we can skip the ModuleAutoGen process\r
     #  If any source file is newer than the module than we cannot skip\r