cmCPackSTGZGenerator.cxx 3.1 KB

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