cmCPackSTGZGenerator.cxx 4.0 KB

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