cmCPackSTGZGenerator.cxx 3.9 KB

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