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. cmCPackSTGZGenerator::cmCPackSTGZGenerator()
  15. : cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip, "paxr", ".sh")
  16. {
  17. }
  18. cmCPackSTGZGenerator::~cmCPackSTGZGenerator() = default;
  19. int cmCPackSTGZGenerator::InitializeInternal()
  20. {
  21. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
  22. std::string inFile = this->FindTemplate("CPack.STGZ_Header.sh.in");
  23. if (inFile.empty()) {
  24. cmCPackLogger(cmCPackLog::LOG_ERROR,
  25. "Cannot find template file: " << inFile << std::endl);
  26. return 0;
  27. }
  28. this->SetOptionIfNotSet("CPACK_STGZ_HEADER_FILE", inFile.c_str());
  29. this->SetOptionIfNotSet("CPACK_AT_SIGN", "@");
  30. return this->Superclass::InitializeInternal();
  31. }
  32. int cmCPackSTGZGenerator::PackageFiles()
  33. {
  34. bool retval = true;
  35. if (!this->Superclass::PackageFiles()) {
  36. return 0;
  37. }
  38. /* TGZ generator (our Superclass) may
  39. * have generated several packages (component packaging)
  40. * so we must iterate over generated packages.
  41. */
  42. for (std::string const& pfn : this->packageFileNames) {
  43. retval &= static_cast<bool>(
  44. cmSystemTools::SetPermissions(pfn.c_str(),
  45. #if defined(_MSC_VER) || defined(__MINGW32__)
  46. S_IREAD | S_IWRITE | S_IEXEC
  47. #else
  48. S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
  49. S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
  50. S_IXOTH
  51. #endif
  52. ));
  53. }
  54. return retval;
  55. }
  56. int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
  57. {
  58. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
  59. std::ostringstream str;
  60. int counter = 0;
  61. std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  62. std::string line;
  63. cmsys::ifstream ilfs(inLicFile.c_str());
  64. std::string licenseText;
  65. while (cmSystemTools::GetLineFromStream(ilfs, line)) {
  66. licenseText += line + "\n";
  67. }
  68. this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT",
  69. licenseText.c_str());
  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. sprintf(buffer, "%d", counter);
  94. cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
  95. // Write in file
  96. *os << res;
  97. return this->Superclass::GenerateHeader(os);
  98. }