]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/Python/AutoGen/ModuleAutoGen.py
BaseTools: Improve the file saving and copying reliability
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / ModuleAutoGen.py
index ffa4727e59eda6004f8b9d81a446a4fc52d68666..2cd0d3859e181ec736fb5e696c7723ea17151983 100755 (executable)
@@ -28,6 +28,7 @@ from Common.caching import cached_class_function
 from AutoGen.ModuleAutoGenHelper import PlatformInfo,WorkSpaceInfo\r
 from AutoGen.CacheIR import ModuleBuildCacheIR\r
 import json\r
+import tempfile\r
 \r
 ## Mapping Makefile type\r
 gMakeTypeMap = {TAB_COMPILER_MSFT:"nmake", "GCC":"gmake"}\r
@@ -1702,9 +1703,8 @@ class ModuleAutoGen(AutoGen):
         try:\r
             ModuleHashPairList = [] # tuple list: [tuple(PreMakefileHash, MakeHash)]\r
             if os.path.exists(ModuleHashPair):\r
-                f = open(ModuleHashPair, 'r')\r
-                ModuleHashPairList = json.load(f)\r
-                f.close()\r
+                with open(ModuleHashPair, 'r') as f:\r
+                    ModuleHashPairList = json.load(f)\r
             PreMakeHash = gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest\r
             MakeHash = gDict[(self.MetaFile.Path, self.Arch)].MakeHashHexDigest\r
             ModuleHashPairList.append((PreMakeHash, MakeHash))\r
@@ -1766,10 +1766,12 @@ class ModuleAutoGen(AutoGen):
 \r
             if os.path.exists (self.TimeStampPath):\r
                 os.remove (self.TimeStampPath)\r
-            with open(self.TimeStampPath, 'w+') as fd:\r
+            with tempfile.NamedTemporaryFile('w+', dir=os.path.dirname(self.TimeStampPath), delete=False) as tf:\r
                 for f in FileSet:\r
-                    fd.write(f)\r
-                    fd.write("\n")\r
+                    tf.write(f)\r
+                    tf.write("\n")\r
+                tempname = tf.name\r
+            SaveFileOnChange(self.TimeStampPath, tempname, False)\r
 \r
         # Ignore generating makefile when it is a binary module\r
         if self.IsBinaryModule:\r
@@ -1806,7 +1808,7 @@ class ModuleAutoGen(AutoGen):
         MewIR.MakefilePath = MakefilePath\r
         MewIR.DependencyHeaderFileSet = Makefile.DependencyHeaderFileSet\r
         MewIR.CreateMakeFileDone = True\r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             try:\r
                 IR = gDict[(self.MetaFile.Path, self.Arch)]\r
                 IR.MakefilePath = MakefilePath\r
@@ -1891,7 +1893,7 @@ class ModuleAutoGen(AutoGen):
         self.IsCodeFileCreated = True\r
         MewIR = ModuleBuildCacheIR(self.MetaFile.Path, self.Arch)\r
         MewIR.CreateCodeFileDone = True\r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             try:\r
                 IR = gDict[(self.MetaFile.Path, self.Arch)]\r
                 IR.CreateCodeFileDone = True\r
@@ -1951,9 +1953,8 @@ class ModuleAutoGen(AutoGen):
                 m.update(GlobalData.gModuleHash[self.Arch][Lib.Name].encode('utf-8'))\r
 \r
         # Add Module self\r
-        f = open(str(self.MetaFile), 'rb')\r
-        Content = f.read()\r
-        f.close()\r
+        with open(str(self.MetaFile), 'rb') as f:\r
+            Content = f.read()\r
         m.update(Content)\r
 \r
         # Add Module's source files\r
@@ -1974,6 +1975,11 @@ class ModuleAutoGen(AutoGen):
             if gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesChain:\r
                 return gDict[(self.MetaFile.Path, self.Arch)]\r
 \r
+        # skip if the module cache already crashed\r
+        if (self.MetaFile.Path, self.Arch) in gDict and \\r
+          gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return\r
+\r
         DependencyFileSet = set()\r
         # Add Module Meta file\r
         DependencyFileSet.add(self.MetaFile)\r
@@ -2021,9 +2027,8 @@ class ModuleAutoGen(AutoGen):
             if not os.path.exists(str(File)):\r
                 EdkLogger.quiet("[cache warning]: header file %s is missing for module: %s[%s]" % (File, self.MetaFile.Path, self.Arch))\r
                 continue\r
-            f = open(str(File), 'rb')\r
-            Content = f.read()\r
-            f.close()\r
+            with open(str(File), 'rb') as f:\r
+                Content = f.read()\r
             m.update(Content)\r
             FileList.append((str(File), hashlib.md5(Content).hexdigest()))\r
 \r
@@ -2032,7 +2037,7 @@ class ModuleAutoGen(AutoGen):
         MewIR.ModuleFilesHashDigest = m.digest()\r
         MewIR.ModuleFilesHashHexDigest = m.hexdigest()\r
         MewIR.ModuleFilesChain = FileList\r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             try:\r
                 IR = gDict[(self.MetaFile.Path, self.Arch)]\r
                 IR.ModuleFilesHashDigest = m.digest()\r
@@ -2050,6 +2055,11 @@ class ModuleAutoGen(AutoGen):
           gDict[(self.MetaFile.Path, self.Arch)].PreMakefileHashHexDigest:\r
             return gDict[(self.MetaFile.Path, self.Arch)]\r
 \r
+        # skip if the module cache already crashed\r
+        if (self.MetaFile.Path, self.Arch) in gDict and \\r
+          gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return\r
+\r
         # skip binary module\r
         if self.IsBinaryModule:\r
             return\r
@@ -2091,7 +2101,7 @@ class ModuleAutoGen(AutoGen):
         # Add Module self\r
         m.update(gDict[(self.MetaFile.Path, self.Arch)].ModuleFilesHashDigest)\r
 \r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             IR = gDict[(self.MetaFile.Path, self.Arch)]\r
             IR.PreMakefileHashHexDigest = m.hexdigest()\r
             gDict[(self.MetaFile.Path, self.Arch)] = IR\r
@@ -2104,6 +2114,11 @@ class ModuleAutoGen(AutoGen):
           gDict[(self.MetaFile.Path, self.Arch)].MakeHeaderFilesHashDigest:\r
             return gDict[(self.MetaFile.Path, self.Arch)]\r
 \r
+        # skip if the module cache already crashed\r
+        if (self.MetaFile.Path, self.Arch) in gDict and \\r
+          gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return\r
+\r
         # skip binary module\r
         if self.IsBinaryModule:\r
             return\r
@@ -2159,7 +2174,7 @@ class ModuleAutoGen(AutoGen):
             m.update(Content)\r
             FileList.append((str(File), hashlib.md5(Content).hexdigest()))\r
 \r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             IR = gDict[(self.MetaFile.Path, self.Arch)]\r
             IR.AutoGenFileList = self.AutoGenFileList.keys()\r
             IR.MakeHeaderFilesHashChain = FileList\r
@@ -2174,6 +2189,11 @@ class ModuleAutoGen(AutoGen):
           gDict[(self.MetaFile.Path, self.Arch)].MakeHashChain:\r
             return gDict[(self.MetaFile.Path, self.Arch)]\r
 \r
+        # skip if the module cache already crashed\r
+        if (self.MetaFile.Path, self.Arch) in gDict and \\r
+          gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return\r
+\r
         # skip binary module\r
         if self.IsBinaryModule:\r
             return\r
@@ -2222,7 +2242,7 @@ class ModuleAutoGen(AutoGen):
         New.sort(key=lambda x: str(x))\r
         MakeHashChain += New\r
 \r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             IR = gDict[(self.MetaFile.Path, self.Arch)]\r
             IR.MakeHashDigest = m.digest()\r
             IR.MakeHashHexDigest = m.hexdigest()\r
@@ -2236,6 +2256,12 @@ class ModuleAutoGen(AutoGen):
         if not GlobalData.gBinCacheSource:\r
             return False\r
 \r
+        if gDict[(self.MetaFile.Path, self.Arch)].PreMakeCacheHit:\r
+            return True\r
+\r
+        if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return False\r
+\r
         # If Module is binary, do not skip by cache\r
         if self.IsBinaryModule:\r
             return False\r
@@ -2255,12 +2281,15 @@ class ModuleAutoGen(AutoGen):
         ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair")\r
         if not os.path.exists(ModuleHashPair):\r
             EdkLogger.quiet("[cache warning]: Cannot find ModuleHashPair file: %s" % ModuleHashPair)\r
+            with GlobalData.cache_lock:\r
+                IR = gDict[(self.MetaFile.Path, self.Arch)]\r
+                IR.CacheCrash = True\r
+                gDict[(self.MetaFile.Path, self.Arch)] = IR\r
             return False\r
 \r
         try:\r
-            f = open(ModuleHashPair, 'r')\r
-            ModuleHashPairList = json.load(f)\r
-            f.close()\r
+            with open(ModuleHashPair, 'r') as f:\r
+                ModuleHashPairList = json.load(f)\r
         except:\r
             EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair)\r
             return False\r
@@ -2300,7 +2329,7 @@ class ModuleAutoGen(AutoGen):
         if self.Name == "PcdPeim" or self.Name == "PcdDxe":\r
             CreatePcdDatabaseCode(self, TemplateString(), TemplateString())\r
 \r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             IR = gDict[(self.MetaFile.Path, self.Arch)]\r
             IR.PreMakeCacheHit = True\r
             gDict[(self.MetaFile.Path, self.Arch)] = IR\r
@@ -2313,6 +2342,12 @@ class ModuleAutoGen(AutoGen):
         if not GlobalData.gBinCacheSource:\r
             return False\r
 \r
+        if gDict[(self.MetaFile.Path, self.Arch)].MakeCacheHit:\r
+            return True\r
+\r
+        if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return False\r
+\r
         # If Module is binary, do not skip by cache\r
         if self.IsBinaryModule:\r
             print("[cache miss]: checkpoint_Makefile: binary module:", self.MetaFile.Path, self.Arch)\r
@@ -2321,7 +2356,7 @@ class ModuleAutoGen(AutoGen):
         # .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
-                with GlobalData.file_lock:\r
+                with GlobalData.cache_lock:\r
                     IR = gDict[(self.MetaFile.Path, self.Arch)]\r
                     IR.MakeCacheHit = False\r
                     gDict[(self.MetaFile.Path, self.Arch)] = IR\r
@@ -2338,12 +2373,15 @@ class ModuleAutoGen(AutoGen):
         ModuleHashPair = path.join(FileDir, self.Name + ".ModuleHashPair")\r
         if not os.path.exists(ModuleHashPair):\r
             EdkLogger.quiet("[cache warning]: Cannot find ModuleHashPair file: %s" % ModuleHashPair)\r
+            with GlobalData.cache_lock:\r
+                IR = gDict[(self.MetaFile.Path, self.Arch)]\r
+                IR.CacheCrash = True\r
+                gDict[(self.MetaFile.Path, self.Arch)] = IR\r
             return False\r
 \r
         try:\r
-            f = open(ModuleHashPair, 'r')\r
-            ModuleHashPairList = json.load(f)\r
-            f.close()\r
+            with open(ModuleHashPair, 'r') as f:\r
+                ModuleHashPairList = json.load(f)\r
         except:\r
             EdkLogger.quiet("[cache warning]: fail to load ModuleHashPair file: %s" % ModuleHashPair)\r
             return False\r
@@ -2383,7 +2421,7 @@ class ModuleAutoGen(AutoGen):
 \r
         if self.Name == "PcdPeim" or self.Name == "PcdDxe":\r
             CreatePcdDatabaseCode(self, TemplateString(), TemplateString())\r
-        with GlobalData.file_lock:\r
+        with GlobalData.cache_lock:\r
             IR = gDict[(self.MetaFile.Path, self.Arch)]\r
             IR.MakeCacheHit = True\r
             gDict[(self.MetaFile.Path, self.Arch)] = IR\r
@@ -2395,6 +2433,10 @@ class ModuleAutoGen(AutoGen):
         if not GlobalData.gBinCacheSource:\r
             return\r
 \r
+        # skip if the module cache already crashed\r
+        if gDict[(self.MetaFile.Path, self.Arch)].CacheCrash:\r
+            return\r
+\r
         # skip binary module\r
         if self.IsBinaryModule:\r
             return\r
@@ -2420,9 +2462,8 @@ class ModuleAutoGen(AutoGen):
             return\r
 \r
         try:\r
-            f = open(ModuleHashPair, 'r')\r
-            ModuleHashPairList = json.load(f)\r
-            f.close()\r
+            with open(ModuleHashPair, 'r') as f:\r
+                ModuleHashPairList = json.load(f)\r
         except:\r
             EdkLogger.quiet("[cache insight]: Cannot load ModuleHashPair file for module: %s[%s]" % (self.MetaFile.Path, self.Arch))\r
             return\r