cmCPackZIPGenerator.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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::Initialize(const char* name, cmMakefile* mf)
  32. {
  33. int res = this->Superclass::Initialize(name, mf);
  34. if ( !res )
  35. {
  36. return res;
  37. }
  38. std::vector<std::string> path;
  39. std::string pkgPath = "c:/Program Files/WinZip";
  40. path.push_back(pkgPath);
  41. pkgPath = cmSystemTools::FindProgram("wzzip", path, false);
  42. this->ZipStyle = cmCPackZIPGenerator::StyleUnkown;
  43. bool found = false;
  44. if ( pkgPath.empty() )
  45. {
  46. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find WinZip" << std::endl);
  47. }
  48. else
  49. {
  50. this->ZipStyle = cmCPackZIPGenerator::StyleWinZip;
  51. found = true;
  52. }
  53. if ( !found )
  54. {
  55. path.erase(path.begin(), path.end());
  56. pkgPath = "c:/cygwin/bin";
  57. path.push_back(pkgPath);
  58. pkgPath = cmSystemTools::FindProgram("zip", path, false);
  59. if ( pkgPath.empty() )
  60. {
  61. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Cannot find unix ZIP" << std::endl);
  62. }
  63. else
  64. {
  65. this->ZipStyle = cmCPackZIPGenerator::StyleUnixZip;
  66. found = true;
  67. }
  68. }
  69. if ( !found )
  70. {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find a sutable ZIP program"
  72. << std::endl);
  73. return 0;
  74. }
  75. this->SetOption("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  76. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found ZIP program: " << pkgPath.c_str()
  77. << std::endl);
  78. return 1;
  79. }
  80. //----------------------------------------------------------------------
  81. int cmCPackZIPGenerator::CompressFiles(const char* outFileName,
  82. const char* toplevel, const std::vector<std::string>& files)
  83. {
  84. cmOStringStream dmgCmd;
  85. switch ( this->ZipStyle )
  86. {
  87. case cmCPackZIPGenerator::StyleWinZip:
  88. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  89. << "\" -P \"" << outFileName
  90. << "\"";
  91. break;
  92. case cmCPackZIPGenerator::StyleUnixZip:
  93. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  94. << "\" \"" << outFileName
  95. << "\"";
  96. break;
  97. default:
  98. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unknown ZIP style"
  99. << std::endl);
  100. return 0;
  101. }
  102. std::vector<std::string>::const_iterator fileIt;
  103. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  104. {
  105. dmgCmd << " \""
  106. << cmSystemTools::RelativePath(toplevel, fileIt->c_str())
  107. << "\"";
  108. }
  109. std::string output;
  110. int retVal = -1;
  111. int res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
  112. &retVal, toplevel, this->GeneratorVerbose, 0);
  113. if ( !res || retVal )
  114. {
  115. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  116. tmpFile += "/CompressZip.log";
  117. cmGeneratedFileStream ofs(tmpFile.c_str());
  118. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  119. << "# Output:" << std::endl
  120. << output.c_str() << std::endl;
  121. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
  122. << dmgCmd.str().c_str() << std::endl
  123. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  124. return 0;
  125. }
  126. return 1;
  127. }