cmCPackSTGZGenerator.cxx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCPackSTGZGenerator.h"
  11. #include "cmake.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmSystemTools.h"
  15. #include "cmMakefile.h"
  16. #include "cmCPackLog.h"
  17. #include <cmsys/ios/sstream>
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. //----------------------------------------------------------------------
  21. cmCPackSTGZGenerator::cmCPackSTGZGenerator()
  22. {
  23. }
  24. //----------------------------------------------------------------------
  25. cmCPackSTGZGenerator::~cmCPackSTGZGenerator()
  26. {
  27. }
  28. //----------------------------------------------------------------------
  29. int cmCPackSTGZGenerator::InitializeInternal()
  30. {
  31. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
  32. std::string inFile = this->FindTemplate("CPack.STGZ_Header.sh.in");
  33. if ( inFile.empty() )
  34. {
  35. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find template file: "
  36. << inFile.c_str() << std::endl);
  37. return 0;
  38. }
  39. this->SetOptionIfNotSet("CPACK_STGZ_HEADER_FILE", inFile.c_str());
  40. this->SetOptionIfNotSet("CPACK_AT_SIGN", "@");
  41. return this->Superclass::InitializeInternal();
  42. }
  43. //----------------------------------------------------------------------
  44. int cmCPackSTGZGenerator::CompressFiles(const char* outFileName,
  45. const char* toplevel, const std::vector<std::string>& files)
  46. {
  47. if ( !this->Superclass::CompressFiles(outFileName, toplevel, files) )
  48. {
  49. return 0;
  50. }
  51. return cmSystemTools::SetPermissions(outFileName,
  52. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  53. S_IREAD | S_IWRITE | S_IEXEC
  54. #elif defined( __BORLANDC__ )
  55. S_IRUSR | S_IWUSR | S_IXUSR
  56. #else
  57. S_IRUSR | S_IWUSR | S_IXUSR |
  58. S_IRGRP | S_IWGRP | S_IXGRP |
  59. S_IROTH | S_IWOTH | S_IXOTH
  60. #endif
  61. );
  62. }
  63. //----------------------------------------------------------------------
  64. int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
  65. {
  66. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
  67. cmsys_ios::ostringstream str;
  68. int counter = 0;
  69. std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  70. std::string line;
  71. std::ifstream ilfs(inLicFile.c_str());
  72. std::string licenseText;
  73. while ( cmSystemTools::GetLineFromStream(ilfs, line) )
  74. {
  75. licenseText += line + "\n";
  76. }
  77. this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT",
  78. licenseText.c_str());
  79. const char headerLengthTag[] = "###CPACK_HEADER_LENGTH###";
  80. // Create the header
  81. std::string inFile = this->GetOption("CPACK_STGZ_HEADER_FILE");
  82. std::ifstream ifs(inFile.c_str());
  83. std::string packageHeaderText;
  84. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  85. {
  86. packageHeaderText += line + "\n";
  87. }
  88. // Configure in the values
  89. std::string res;
  90. this->ConfigureString(packageHeaderText, res);
  91. // Count the lines
  92. const char* ptr = res.c_str();
  93. while ( *ptr )
  94. {
  95. if ( *ptr == '\n' )
  96. {
  97. counter ++;
  98. }
  99. ++ptr;
  100. }
  101. counter ++;
  102. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  103. "Number of lines: " << counter << std::endl);
  104. char buffer[1024];
  105. sprintf(buffer, "%d", counter);
  106. cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
  107. // Write in file
  108. *os << res.c_str();
  109. return this->Superclass::GenerateHeader(os);
  110. }