cmCPackSTGZGenerator.cxx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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::PackageFiles()
  45. {
  46. if ( !this->Superclass::PackageFiles() )
  47. {
  48. return 0;
  49. }
  50. return cmSystemTools::SetPermissions(packageFileNames[0].c_str(),
  51. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  52. S_IREAD | S_IWRITE | S_IEXEC
  53. #elif defined( __BORLANDC__ )
  54. S_IRUSR | S_IWUSR | S_IXUSR
  55. #else
  56. S_IRUSR | S_IWUSR | S_IXUSR |
  57. S_IRGRP | S_IWGRP | S_IXGRP |
  58. S_IROTH | S_IWOTH | S_IXOTH
  59. #endif
  60. );
  61. }
  62. //----------------------------------------------------------------------
  63. int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
  64. {
  65. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
  66. cmsys_ios::ostringstream str;
  67. int counter = 0;
  68. std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
  69. std::string line;
  70. std::ifstream ilfs(inLicFile.c_str());
  71. std::string licenseText;
  72. while ( cmSystemTools::GetLineFromStream(ilfs, line) )
  73. {
  74. licenseText += line + "\n";
  75. }
  76. this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT",
  77. licenseText.c_str());
  78. const char headerLengthTag[] = "###CPACK_HEADER_LENGTH###";
  79. // Create the header
  80. std::string inFile = this->GetOption("CPACK_STGZ_HEADER_FILE");
  81. std::ifstream ifs(inFile.c_str());
  82. std::string packageHeaderText;
  83. while ( cmSystemTools::GetLineFromStream(ifs, line) )
  84. {
  85. packageHeaderText += line + "\n";
  86. }
  87. // Configure in the values
  88. std::string res;
  89. this->ConfigureString(packageHeaderText, res);
  90. // Count the lines
  91. const char* ptr = res.c_str();
  92. while ( *ptr )
  93. {
  94. if ( *ptr == '\n' )
  95. {
  96. counter ++;
  97. }
  98. ++ptr;
  99. }
  100. counter ++;
  101. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  102. "Number of lines: " << counter << std::endl);
  103. char buffer[1024];
  104. sprintf(buffer, "%d", counter);
  105. cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
  106. // Write in file
  107. *os << res.c_str();
  108. return this->Superclass::GenerateHeader(os);
  109. }