cmCPackSTGZGenerator.cxx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackSTGZGenerator.h"
  4. #include <cstdio>
  5. #include <sstream>
  6. #include <string>
  7. #include <vector>
  8. #include "cmsys/FStream.hxx"
  9. #include "cm_sys_stat.h"
  10. #include "cmArchiveWrite.h"
  11. #include "cmCPackGenerator.h"
  12. #include "cmCPackLog.h"
  13. #include "cmSystemTools.h"
  14. #include "cmValue.h"
  15. cmCPackSTGZGenerator::cmCPackSTGZGenerator()
  16. : cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip, "paxr", ".sh")
  17. {
  18. }
  19. cmCPackSTGZGenerator::~cmCPackSTGZGenerator() = default;
  20. int cmCPackSTGZGenerator::InitializeInternal()
  21. {
  22. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
  23. std::string inFile = this->FindTemplate("CPack.STGZ_Header.sh.in");
  24. if (inFile.empty()) {
  25. cmCPackLogger(cmCPackLog::LOG_ERROR,
  26. "Cannot find template file: " << inFile << std::endl);
  27. return 0;
  28. }
  29. this->SetOptionIfNotSet("CPACK_STGZ_HEADER_FILE", inFile);
  30. this->SetOptionIfNotSet("CPACK_AT_SIGN", "@");
  31. return this->Superclass::InitializeInternal();
  32. }
  33. int cmCPackSTGZGenerator::PackageFiles()
  34. {
  35. bool retval = true;
  36. if (!this->Superclass::PackageFiles()) {
  37. return 0;
  38. }
  39. /* TGZ generator (our Superclass) may
  40. * have generated several packages (component packaging)
  41. * so we must iterate over generated packages.
  42. */
  43. for (std::string const& pfn : this->packageFileNames) {
  44. retval &= static_cast<bool>(
  45. cmSystemTools::SetPermissions(pfn.c_str(),
  46. #if defined(_MSC_VER) || defined(__MINGW32__)
  47. S_IREAD | S_IWRITE | S_IEXEC
  48. #else
  49. S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
  50. S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
  51. S_IXOTH
  52. #endif
  53. ));
  54. }
  55. return retval;
  56. }
  57. int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
  58. {
  59. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
  60. std::ostringstream str;
  61. int counter = 0;
  62. std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  63. std::string line;
  64. cmsys::ifstream ilfs(inLicFile.c_str());
  65. std::string licenseText;
  66. while (cmSystemTools::GetLineFromStream(ilfs, line)) {
  67. licenseText += line + "\n";
  68. }
  69. this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT", licenseText);
  70. const char headerLengthTag[] = "###CPACK_HEADER_LENGTH###";
  71. // Create the header
  72. std::string inFile = this->GetOption("CPACK_STGZ_HEADER_FILE");
  73. cmsys::ifstream ifs(inFile.c_str());
  74. std::string packageHeaderText;
  75. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  76. packageHeaderText += line + "\n";
  77. }
  78. // Configure in the values
  79. std::string res;
  80. this->ConfigureString(packageHeaderText, res);
  81. // Count the lines
  82. const char* ptr = res.c_str();
  83. while (*ptr) {
  84. if (*ptr == '\n') {
  85. counter++;
  86. }
  87. ++ptr;
  88. }
  89. counter++;
  90. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  91. "Number of lines: " << counter << std::endl);
  92. char buffer[1024];
  93. snprintf(buffer, sizeof(buffer), "%d", counter);
  94. cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
  95. // Write in file
  96. *os << res;
  97. return this->Superclass::GenerateHeader(os);
  98. }