cmCPackZIPGenerator.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. path.erase(path.begin(), path.end());
  52. pkgPath = "c:/cygwin/bin";
  53. path.push_back(pkgPath);
  54. pkgPath = cmSystemTools::FindProgram("zip", path, false);
  55. if ( pkgPath.empty() )
  56. {
  57. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find unix ZIP"
  58. << std::endl);
  59. }
  60. else
  61. {
  62. this->ZipStyle = cmCPackZIPGenerator::StyleUnixZip;
  63. found = true;
  64. }
  65. }
  66. if ( !found )
  67. {
  68. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find a sutable ZIP program"
  69. << std::endl);
  70. return 0;
  71. }
  72. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  73. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found ZIP program: "
  74. << pkgPath.c_str()
  75. << std::endl);
  76. return this->Superclass::InitializeInternal();
  77. }
  78. //----------------------------------------------------------------------
  79. int cmCPackZIPGenerator::CompressFiles(const char* outFileName,
  80. const char* toplevel, const std::vector<std::string>& files)
  81. {
  82. std::string tempFileName;
  83. tempFileName = toplevel;
  84. tempFileName += "/winZip.filelist";
  85. bool needQuotesInFile = false;
  86. cmOStringStream dmgCmd;
  87. switch ( this->ZipStyle )
  88. {
  89. case cmCPackZIPGenerator::StyleWinZip:
  90. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  91. << "\" -P \"" << outFileName
  92. << "\" @\"" << tempFileName.c_str() << "\"";
  93. needQuotesInFile = true;
  94. break;
  95. case cmCPackZIPGenerator::StyleUnixZip:
  96. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  97. << "\" -r \"" << outFileName
  98. << "\" . [email protected]";
  99. break;
  100. default:
  101. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unknown ZIP style"
  102. << std::endl);
  103. return 0;
  104. }
  105. if(tempFileName.size())
  106. {
  107. cmGeneratedFileStream out(tempFileName.c_str());
  108. std::vector<std::string>::const_iterator fileIt;
  109. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  110. {
  111. if ( needQuotesInFile )
  112. {
  113. out << "\"";
  114. }
  115. out << cmSystemTools::RelativePath(toplevel, fileIt->c_str());
  116. if ( needQuotesInFile )
  117. {
  118. out << "\"";
  119. }
  120. out << std::endl;
  121. }
  122. }
  123. else
  124. {
  125. std::vector<std::string>::const_iterator fileIt;
  126. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  127. {
  128. dmgCmd << " \""
  129. << cmSystemTools::RelativePath(toplevel, fileIt->c_str())
  130. << "\"";
  131. }
  132. }
  133. std::string output;
  134. int retVal = -1;
  135. int res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
  136. &retVal, toplevel, this->GeneratorVerbose, 0);
  137. if ( !res || retVal )
  138. {
  139. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  140. tmpFile += "/CompressZip.log";
  141. cmGeneratedFileStream ofs(tmpFile.c_str());
  142. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  143. << "# Output:" << std::endl
  144. << output.c_str() << std::endl;
  145. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
  146. << dmgCmd.str().c_str() << std::endl
  147. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  148. return 0;
  149. }
  150. return 1;
  151. }