cmCPackSTGZGenerator.cxx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 : packageFileNames) {
  43. retval &= cmSystemTools::SetPermissions(pfn.c_str(),
  44. #if defined(_MSC_VER) || defined(__MINGW32__)
  45. S_IREAD | S_IWRITE | S_IEXEC
  46. #else
  47. S_IRUSR | S_IWUSR | S_IXUSR |
  48. S_IRGRP | S_IWGRP | S_IXGRP |
  49. S_IROTH | S_IWOTH | S_IXOTH
  50. #endif
  51. );
  52. }
  53. return retval;
  54. }
  55. int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
  56. {
  57. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
  58. std::ostringstream str;
  59. int counter = 0;
  60. std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  61. std::string line;
  62. cmsys::ifstream ilfs(inLicFile.c_str());
  63. std::string licenseText;
  64. while (cmSystemTools::GetLineFromStream(ilfs, line)) {
  65. licenseText += line + "\n";
  66. }
  67. this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT",
  68. licenseText.c_str());
  69. const char headerLengthTag[] = "###CPACK_HEADER_LENGTH###";
  70. // Create the header
  71. std::string inFile = this->GetOption("CPACK_STGZ_HEADER_FILE");
  72. cmsys::ifstream ifs(inFile.c_str());
  73. std::string packageHeaderText;
  74. while (cmSystemTools::GetLineFromStream(ifs, line)) {
  75. packageHeaderText += line + "\n";
  76. }
  77. // Configure in the values
  78. std::string res;
  79. this->ConfigureString(packageHeaderText, res);
  80. // Count the lines
  81. const char* ptr = res.c_str();
  82. while (*ptr) {
  83. if (*ptr == '\n') {
  84. counter++;
  85. }
  86. ++ptr;
  87. }
  88. counter++;
  89. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  90. "Number of lines: " << counter << std::endl);
  91. char buffer[1024];
  92. sprintf(buffer, "%d", counter);
  93. cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
  94. // Write in file
  95. *os << res;
  96. return this->Superclass::GenerateHeader(os);
  97. }