cmCPackSTGZGenerator.cxx 3.2 KB

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