cmCPackSTGZGenerator.cxx 3.2 KB

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