Browse Source

cmCPackGenerator: Refactor generation of checksum file into own function

Deniz Bahadir 1 year ago
parent
commit
d488efa0b4
2 changed files with 24 additions and 12 deletions
  1. 20 12
      Source/CPack/cmCPackGenerator.cxx
  2. 4 0
      Source/CPack/cmCPackGenerator.h

+ 20 - 12
Source/CPack/cmCPackGenerator.cxx

@@ -974,6 +974,25 @@ int cmCPackGenerator::InstallCMakeProject(
   return 1;
 }
 
+bool cmCPackGenerator::GenerateChecksumFile(cmCryptoHash& crypto,
+                                            cm::string_view filename) const
+{
+  std::string packageFileName =
+    cmStrCat(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"), "/", filename);
+  std::string hashFile = cmStrCat(
+    packageFileName, "." + cmSystemTools::LowerCase(crypto.GetHashAlgoName()));
+  cmsys::ofstream outF(hashFile.c_str());
+  if (!outF) {
+    cmCPackLogger(cmCPackLog::LOG_ERROR,
+                  "Cannot create checksum file: " << hashFile << std::endl);
+    return false;
+  }
+  outF << crypto.HashFile(packageFileName) << "  " << filename << "\n";
+  cmCPackLogger(cmCPackLog::LOG_OUTPUT,
+                "- checksum file: " << hashFile << " generated." << std::endl);
+  return true;
+}
+
 bool cmCPackGenerator::ReadListFile(const char* moduleName)
 {
   bool retval;
@@ -1177,20 +1196,9 @@ int cmCPackGenerator::DoPackage()
 
     /* Generate checksum file */
     if (crypto) {
-      std::string hashFile(this->GetOption("CPACK_OUTPUT_FILE_PREFIX"));
-      hashFile += "/" + filename;
-      hashFile += "." + cmSystemTools::LowerCase(algo);
-      cmsys::ofstream outF(hashFile.c_str());
-      if (!outF) {
-        cmCPackLogger(cmCPackLog::LOG_ERROR,
-                      "Cannot create checksum file: " << hashFile
-                                                      << std::endl);
+      if (!this->GenerateChecksumFile(*crypto, filename)) {
         return 0;
       }
-      outF << crypto->HashFile(packageFileName) << "  " << filename << "\n";
-      cmCPackLogger(cmCPackLog::LOG_OUTPUT,
-                    "- checksum file: " << hashFile << " generated."
-                                        << std::endl);
     }
   }
 

+ 4 - 0
Source/CPack/cmCPackGenerator.h

@@ -19,6 +19,7 @@
 #include "cmValue.h"
 
 class cmCPackLog;
+class cmCryptoHash;
 class cmGlobalGenerator;
 class cmInstalledFile;
 class cmMakefile;
@@ -182,6 +183,9 @@ protected:
   virtual const char* GetInstallPath();
   virtual const char* GetPackagingInstallPrefix();
 
+  bool GenerateChecksumFile(cmCryptoHash& crypto,
+                            cm::string_view filename) const;
+
   std::string FindTemplate(cm::string_view name,
                            cm::optional<cm::string_view> alt = cm::nullopt);
   virtual bool ConfigureFile(const std::string& inName,