cmCPackCygwinSourceGenerator.cxx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "cmCPackCygwinSourceGenerator.h"
  11. #include "cmake.h"
  12. #include "cmGlobalGenerator.h"
  13. #include "cmLocalGenerator.h"
  14. #include "cmSystemTools.h"
  15. #include "cmMakefile.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmCPackLog.h"
  18. #include <cmsys/SystemTools.hxx>
  19. // Includes needed for implementation of RenameFile. This is not in
  20. // system tools because it is not implemented robustly enough to move
  21. // files across directories.
  22. #ifdef _WIN32
  23. # include <windows.h>
  24. # include <sys/stat.h>
  25. #endif
  26. //----------------------------------------------------------------------
  27. cmCPackCygwinSourceGenerator::cmCPackCygwinSourceGenerator()
  28. {
  29. this->Compress = false;
  30. }
  31. //----------------------------------------------------------------------
  32. cmCPackCygwinSourceGenerator::~cmCPackCygwinSourceGenerator()
  33. {
  34. }
  35. //----------------------------------------------------------------------
  36. int cmCPackCygwinSourceGenerator::InitializeInternal()
  37. {
  38. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
  39. std::vector<std::string> path;
  40. std::string pkgPath = cmSystemTools::FindProgram("bzip2", path, false);
  41. if ( pkgPath.empty() )
  42. {
  43. cmCPackLogger(cmCPackLog::LOG_ERROR, "Cannot find BZip2" << std::endl);
  44. return 0;
  45. }
  46. this->SetOptionIfNotSet("CPACK_INSTALLER_PROGRAM", pkgPath.c_str());
  47. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Found Compress program: "
  48. << pkgPath.c_str()
  49. << std::endl);
  50. return this->Superclass::InitializeInternal();
  51. }
  52. //----------------------------------------------------------------------
  53. int cmCPackCygwinSourceGenerator::CompressFiles(const char* outFileName,
  54. const char* toplevel, const std::vector<std::string>& files)
  55. {
  56. // Create a tar file of the sources
  57. std::string packageDirFileName
  58. = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  59. packageDirFileName += ".tar";
  60. std::string output;
  61. // skip one parent up to the cmCPackTGZGenerator to create tar file
  62. this->Compress = false; // just create tar not tar.gz
  63. if ( !this->cmCPackTGZGenerator::CompressFiles(packageDirFileName.c_str(),
  64. toplevel, files) )
  65. {
  66. return 0;
  67. }
  68. // Now bzip2 the source tar file
  69. if(!this->BZip2File(packageDirFileName.c_str()))
  70. {
  71. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem running BZip2 on file: "
  72. << packageDirFileName.c_str());
  73. return 0;
  74. }
  75. // Now create a tar file that contains the above .tar.bz2 file
  76. // and the CPACK_CYGWIN_PATCH_FILE and CPACK_TOPLEVEL_DIRECTORY
  77. // files
  78. std::string compressOutFile = packageDirFileName + ".bz2";
  79. // at this point compressOutFile is the full path to
  80. // _CPack_Package/.../package-2.5.0.tar.bz2
  81. // we want to create a tar _CPack_Package/.../package-2.5.0-1-src.tar.bz2
  82. // with these
  83. // _CPack_Package/.../package-2.5.0-1.patch
  84. // _CPack_Package/.../package-2.5.0-1.sh
  85. // _CPack_Package/.../package-2.5.0.tar.bz2
  86. // the -1 is CPACK_CYGWIN_PATCH_NUMBER
  87. // copy the patch file into place
  88. if(!cmSystemTools::CopyFileAlways(
  89. this->GetOption("CPACK_CYGWIN_PATCH_FILE"),
  90. this->GetOption("CPACK_TOPLEVEL_DIRECTORY")))
  91. {
  92. cmCPackLogger(cmCPackLog::LOG_ERROR, "problem copying: ["
  93. << this->GetOption("CPACK_CYGWIN_PATCH_FILE") << "]\nto\n["
  94. << this->GetOption("CPACK_TOPLEVEL_DIRECTORY") << "]\n");
  95. return 0;
  96. }
  97. // copy the build script into place
  98. if(!cmSystemTools::CopyFileAlways(
  99. this->GetOption("CPACK_CYGWIN_BUILD_SCRIPT"),
  100. this->GetOption("CPACK_TOPLEVEL_DIRECTORY")))
  101. {
  102. cmCPackLogger(cmCPackLog::LOG_ERROR, "problem copying: "
  103. << this->GetOption("CPACK_CYGWIN_BUILD_SCRIPT") << "\nto\n"
  104. << this->GetOption("CPACK_TOPLEVEL_DIRECTORY") << "]\n");
  105. return 0;
  106. }
  107. // create the tar file
  108. std::string outerTarFile
  109. = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  110. outerTarFile += "-";
  111. const char* patch = this->GetOption("CPACK_CYGWIN_PATCH_NUMBER");
  112. if(!patch)
  113. {
  114. cmCPackLogger(cmCPackLog::LOG_WARNING, "CPACK_CYGWIN_PATCH_NUMBER"
  115. << " not specified, defaulting to 1\n");
  116. patch = "1";
  117. }
  118. outerTarFile += patch;
  119. outerTarFile += "-src.tar";
  120. std::string buildScript = cmSystemTools::GetFilenameName(
  121. this->GetOption("CPACK_CYGWIN_BUILD_SCRIPT"));
  122. std::string patchFile = cmSystemTools::GetFilenameName(
  123. this->GetOption("CPACK_CYGWIN_PATCH_FILE"));
  124. std::vector<cmStdString> outerFiles;
  125. std::string file = cmSystemTools::GetFilenameName(compressOutFile);
  126. std::string path = cmSystemTools::GetFilenamePath(compressOutFile);
  127. // a source release in cygwin should have the build script used
  128. // to build the package, the patch file that is different from the
  129. // regular upstream version of the sources, and a bziped tar file
  130. // of the original sources
  131. outerFiles.push_back(buildScript);
  132. outerFiles.push_back(patchFile);
  133. outerFiles.push_back(file);
  134. std::string saveDir= cmSystemTools::GetCurrentWorkingDirectory();
  135. cmSystemTools::ChangeDirectory(path.c_str());
  136. cmSystemTools::CreateTar(outerTarFile.c_str(),
  137. outerFiles, false, false);
  138. cmSystemTools::ChangeDirectory(saveDir.c_str());
  139. // now compress the outer tar file
  140. if(!this->BZip2File(outerTarFile.c_str()))
  141. {
  142. return 0;
  143. }
  144. compressOutFile = outerTarFile;
  145. compressOutFile += ".bz2";
  146. // now rename the file to its final name
  147. if ( !cmSystemTools::SameFile(compressOutFile.c_str(), outFileName ) )
  148. {
  149. if ( !this->RenameFile(compressOutFile.c_str(), outFileName) )
  150. {
  151. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem renaming: \""
  152. << compressOutFile.c_str() << "\" to \""
  153. << (outFileName ? outFileName : "(NULL)") << std::endl);
  154. return 0;
  155. }
  156. }
  157. return 1;
  158. }
  159. const char* cmCPackCygwinSourceGenerator::GetPackagingInstallPrefix()
  160. {
  161. this->InstallPrefix = "/";
  162. this->InstallPrefix += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  163. return this->InstallPrefix.c_str();
  164. }
  165. const char* cmCPackCygwinSourceGenerator::GetOutputExtension()
  166. {
  167. this->OutputExtension = "-";
  168. const char* patch = this->GetOption("CPACK_CYGWIN_PATCH_NUMBER");
  169. if(!patch)
  170. {
  171. cmCPackLogger(cmCPackLog::LOG_WARNING, "CPACK_CYGWIN_PATCH_NUMBER"
  172. << " not specified, defaulting to 1\n");
  173. patch = "1";
  174. }
  175. this->OutputExtension += patch;
  176. this->OutputExtension += "-src.tar.bz2";
  177. return this->OutputExtension.c_str();
  178. }