cmCPackZIPGenerator.cxx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. cmOStringStream dmgCmd;
  83. switch ( this->ZipStyle )
  84. {
  85. case cmCPackZIPGenerator::StyleWinZip:
  86. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  87. << "\" -P \"" << outFileName
  88. << "\"";
  89. break;
  90. case cmCPackZIPGenerator::StyleUnixZip:
  91. dmgCmd << "\"" << this->GetOption("CPACK_INSTALLER_PROGRAM")
  92. << "\" \"" << outFileName
  93. << "\"";
  94. break;
  95. default:
  96. cmCPackLogger(cmCPackLog::LOG_ERROR, "Unknown ZIP style"
  97. << std::endl);
  98. return 0;
  99. }
  100. std::vector<std::string>::const_iterator fileIt;
  101. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  102. {
  103. dmgCmd << " \""
  104. << cmSystemTools::RelativePath(toplevel, fileIt->c_str())
  105. << "\"";
  106. }
  107. std::string output;
  108. int retVal = -1;
  109. int res = cmSystemTools::RunSingleCommand(dmgCmd.str().c_str(), &output,
  110. &retVal, toplevel, this->GeneratorVerbose, 0);
  111. if ( !res || retVal )
  112. {
  113. std::string tmpFile = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  114. tmpFile += "/CompressZip.log";
  115. cmGeneratedFileStream ofs(tmpFile.c_str());
  116. ofs << "# Run command: " << dmgCmd.str().c_str() << std::endl
  117. << "# Output:" << std::endl
  118. << output.c_str() << std::endl;
  119. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running zip command: "
  120. << dmgCmd.str().c_str() << std::endl
  121. << "Please check " << tmpFile.c_str() << " for errors" << std::endl);
  122. return 0;
  123. }
  124. return 1;
  125. }