cmCPackZIPGenerator.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmCPackZIPGenerator.h"
  14. #include "cmake.h"
  15. #include "cmGlobalGenerator.h"
  16. #include "cmLocalGenerator.h"
  17. #include "cmSystemTools.h"
  18. #include "cmMakefile.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmCPackLog.h"
  21. #include <cmsys/SystemTools.hxx>
  22. //----------------------------------------------------------------------
  23. cmCPackZIPGenerator::cmCPackZIPGenerator()
  24. {
  25. }
  26. //----------------------------------------------------------------------
  27. cmCPackZIPGenerator::~cmCPackZIPGenerator()
  28. {
  29. }
  30. //----------------------------------------------------------------------
  31. int cmCPackZIPGenerator::InitializeInternal()
  32. {
  33. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  34. std::vector<std::string> path;
  35. std::string pkgPath = "c:/Program Files/WinZip";
  36. path.push_back(pkgPath);
  37. pkgPath = cmSystemTools::FindProgram("wzzip", path, false);
  38. this->ZipStyle = cmCPackZIPGenerator::StyleUnkown;
  39. bool found = false;
  40. if ( pkgPath.empty() )
  41. {
  42. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find WinZip" << std::endl);
  43. }
  44. else
  45. {
  46. this->ZipStyle = cmCPackZIPGenerator::StyleWinZip;
  47. found = true;
  48. }
  49. if ( !found )
  50. {
  51. pkgPath = "c:/Program Files/7-Zip";
  52. path.push_back(pkgPath);
  53. pkgPath = cmSystemTools::FindProgram("7z", path, false);
  54. if ( pkgPath.empty() )
  55. {
  56. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find 7ZIP"
  57. << std::endl);
  58. }
  59. else
  60. {
  61. this->ZipStyle = cmCPackZIPGenerator::Style7Zip;
  62. found = true;
  63. }
  64. }
  65. if ( !found )
  66. {
  67. path.erase(path.begin(), path.end());
  68. pkgPath = "c:/cygwin/bin";
  69. path.push_back(pkgPath);
  70. pkgPath = cmSystemTools::FindProgram("zip", path, false);
  71. if ( pkgPath.empty() )
  72. {
  73. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find unix ZIP"
  74. << std::endl);
  75. }
  76. else
  77. {
  78. this->ZipStyle = cmCPackZIPGenerator::StyleUnixZip;
  79. found = true;
  80. }
  81. }
  82. if ( !found )
  83. {
  84. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find a suitable ZIP program"
  85. << std::endl);
  86. return 0;
  87. }
  88. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  89. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found ZIP program: "
  90. << pkgPath.c_str()
  91. << std::endl);
  92. return this->Superclass::InitializeInternal();
  93. }
  94. //----------------------------------------------------------------------
  95. int cmCPackZIPGenerator::CompressFiles(const char* outFileName,
  96. const char* toplevel, const std::vector<std::string>& files)
  97. {
  98. std::string tempFileName;
  99. tempFileName = toplevel;
  100. tempFileName += "/winZip.filelist";
  101. bool needQuotesInFile = false;
  102. cmOStringStream dmgCmd;
  103. switch ( this->ZipStyle )
  104. {
  105. case cmCPackZIPGenerator::StyleWinZip:
  106. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  107. << "\" -P \"" << outFileName
  108. << "\" @winZip.filelist";
  109. needQuotesInFile = true;
  110. break;
  111. case cmCPackZIPGenerator::Style7Zip:
  112. // this is the zip generator, so tell 7zip to generate zip files
  113. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  114. << "\" a -tzip \"" << outFileName
  115. << "\" @winZip.filelist";
  116. needQuotesInFile = true;
  117. break;
  118. case cmCPackZIPGenerator::StyleUnixZip:
  119. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  120. << "\" -r \"" << outFileName
  121. << "\" . [email protected]";
  122. break;
  123. default:
  124. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unknown ZIP style"
  125. << std::endl);
  126. return 0;
  127. }
  128. if(tempFileName.size())
  129. {
  130. cmGeneratedFileStream out(tempFileName.c_str());
  131. std::vector<std::string>::const_iterator fileIt;
  132. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  133. {
  134. if ( needQuotesInFile )
  135. {
  136. out << "\"";
  137. }
  138. out << cmSystemTools::RelativePath(toplevel, fileIt->c_str());
  139. if ( needQuotesInFile )
  140. {
  141. out << "\"";
  142. }
  143. out << std::endl;
  144. }
  145. }
  146. else
  147. {
  148. std::vector<std::string>::const_iterator fileIt;
  149. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  150. {
  151. dmgCmd << " \""
  152. << cmSystemTools::RelativePath(toplevel, fileIt->c_str())
  153. << "\"";
  154. }
  155. }
  156. std::string output;
  157. int retVal = -1;
  158. int res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
  159. &retVal, toplevel, this->GeneratorVerbose, 0);
  160. if ( !res || retVal )
  161. {
  162. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  163. tmpFile += "/CompressZip.log";
  164. cmGeneratedFileStream ofs(tmpFile.c_str());
  165. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  166. << "# Output:" << std::endl
  167. << output.c_str() << std::endl;
  168. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
  169. << dmgCmd.str().c_str() << std::endl
  170. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  171. return 0;
  172. }
  173. return 1;
  174. }