cmCPackSTGZGenerator.cxx 3.3 KB

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