]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/AutoGen.py
BaseTools: Library hashing fix and optimization for --hash feature
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / AutoGen.py
index 57992d5886804a4a89d9ed54f8cbb7087ac9e2f8..a843f294b94a5e9d643147fc008ee42789583d4d 100644 (file)
@@ -3416,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
@@ -3897,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.Name, 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
@@ -3914,7 +3915,11 @@ 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
@@ -3924,12 +3929,13 @@ class ModuleAutoGen(AutoGen):
         for f_ext in self.SourceFileList:\r
             if '.inc' in str(f_ext):\r
                 return False\r
-        FileDir = path.join(GlobalData.gBinCacheSource, self.Arch, self.SourceDir, self.MetaFile.BaseName)\r
+        FileDir = path.join(GlobalData.gBinCacheSource, self.PlatformInfo.Name, 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
@@ -3938,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
@@ -4084,13 +4094,20 @@ 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
-        if self.Name in GlobalData.gModuleHash[self.Arch] and GlobalData.gBinCacheSource and self.AttemptModuleCacheCopy():\r
-            return False\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.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
@@ -4109,6 +4126,7 @@ class ModuleAutoGen(AutoGen):
         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
@@ -4117,27 +4135,45 @@ class ModuleAutoGen(AutoGen):
                 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 and self.AttemptModuleCacheCopy():\r
-            return False\r
-        return SaveFileOnChange(ModuleHashFile, m.hexdigest(), False)\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
+        # 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
-        if GlobalData.gUseHashCache:\r
-            # If there is a valid hash or function generated a valid hash; function will return False\r
-            # and the statement below will return True\r
-            return not self.GenModuleHash()\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