cmCPackArchiveGenerator.cxx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "cmCPackArchiveGenerator.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 <errno.h>
  19. #include <cmsys/SystemTools.hxx>
  20. #include <cmsys/Directory.hxx>
  21. #include <cm_libarchive.h>
  22. //----------------------------------------------------------------------
  23. cmCPackArchiveGenerator::cmCPackArchiveGenerator(cmArchiveWrite::Compress t,
  24. cmArchiveWrite::Type at)
  25. {
  26. this->Compress = t;
  27. this->Archive = at;
  28. }
  29. //----------------------------------------------------------------------
  30. cmCPackArchiveGenerator::~cmCPackArchiveGenerator()
  31. {
  32. }
  33. //----------------------------------------------------------------------
  34. int cmCPackArchiveGenerator::InitializeInternal()
  35. {
  36. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  37. return this->Superclass::InitializeInternal();
  38. }
  39. //----------------------------------------------------------------------
  40. int cmCPackArchiveGenerator::addOneComponentToArchive(cmArchiveWrite& archive,
  41. cmCPackComponent* component)
  42. {
  43. cmCPackLogger(cmCPackLog::LOG_VERBOSE, " - packaging component: "
  44. << component->Name
  45. << std::endl);
  46. // Add the files of this component to the archive
  47. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  48. localToplevel += "/"+ component->Name;
  49. std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
  50. // Change to local toplevel
  51. cmSystemTools::ChangeDirectory(localToplevel.c_str());
  52. std::vector<std::string>::const_iterator fileIt;
  53. for (fileIt = component->Files.begin(); fileIt != component->Files.end();
  54. ++fileIt )
  55. {
  56. cmCPackLogger(cmCPackLog::LOG_DEBUG,"Adding file: "
  57. << (*fileIt) << std::endl);
  58. archive.Add(*fileIt);
  59. if (!archive)
  60. {
  61. cmCPackLogger(cmCPackLog::LOG_ERROR, "ERROR while packaging files: "
  62. << archive.GetError()
  63. << std::endl);
  64. return 0;
  65. }
  66. }
  67. // Go back to previous dir
  68. cmSystemTools::ChangeDirectory(dir.c_str());
  69. return 1;
  70. }
  71. /*
  72. * The macro will open/create a file 'filename'
  73. * an declare and open the associated
  74. * cmArchiveWrite 'archive' object.
  75. */
  76. #define DECLARE_AND_OPEN_ARCHIVE(filename,archive) \
  77. cmGeneratedFileStream gf; \
  78. gf.Open(filename.c_str(), false, true); \
  79. if (!GenerateHeader(&gf)) \
  80. { \
  81. cmCPackLogger(cmCPackLog::LOG_ERROR, \
  82. "Problem to generate Header for archive < " \
  83. << filename \
  84. << ">." << std::endl); \
  85. return 0; \
  86. } \
  87. cmArchiveWrite archive(gf,this->Compress, this->Archive); \
  88. if (!archive) \
  89. { \
  90. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < " \
  91. << filename \
  92. << ">. ERROR =" \
  93. << archive.GetError() \
  94. << std::endl); \
  95. return 0; \
  96. }
  97. //----------------------------------------------------------------------
  98. int cmCPackArchiveGenerator::PackageComponents(bool ignoreGroup)
  99. {
  100. packageFileNames.clear();
  101. // The default behavior is to have one package by component group
  102. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  103. if (!ignoreGroup)
  104. {
  105. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  106. for (compGIt=this->ComponentGroups.begin();
  107. compGIt!=this->ComponentGroups.end(); ++compGIt)
  108. {
  109. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  110. << compGIt->first
  111. << std::endl);
  112. // Begin the archive for this group
  113. std::string packageFileName= std::string(toplevel);
  114. packageFileName += "/"
  115. +std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  116. +"-"+compGIt->first + this->GetOutputExtension();
  117. // open a block in order to automatically close archive
  118. // at the end of the block
  119. {
  120. DECLARE_AND_OPEN_ARCHIVE(packageFileName,archive);
  121. // now iterate over the component of this group
  122. std::vector<cmCPackComponent*>::iterator compIt;
  123. for (compIt=(compGIt->second).Components.begin();
  124. compIt!=(compGIt->second).Components.end();
  125. ++compIt)
  126. {
  127. // Add the files of this component to the archive
  128. addOneComponentToArchive(archive,*compIt);
  129. }
  130. }
  131. // add the generated package to package file names list
  132. packageFileNames.push_back(packageFileName);
  133. }
  134. }
  135. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  136. // We build 1 package per component
  137. else
  138. {
  139. std::map<std::string, cmCPackComponent>::iterator compIt;
  140. for (compIt=this->Components.begin();
  141. compIt!=this->Components.end(); ++compIt )
  142. {
  143. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  144. std::string packageFileName = std::string(toplevel);
  145. localToplevel += "/"+ compIt->first;
  146. packageFileName += "/"
  147. +std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  148. +"-"+compIt->first + this->GetOutputExtension();
  149. {
  150. DECLARE_AND_OPEN_ARCHIVE(packageFileName,archive);
  151. // Add the files of this component to the archive
  152. addOneComponentToArchive(archive,&(compIt->second));
  153. }
  154. // add the generated package to package file names list
  155. packageFileNames.push_back(packageFileName);
  156. }
  157. }
  158. return 1;
  159. }
  160. //----------------------------------------------------------------------
  161. int cmCPackArchiveGenerator::PackageComponentsAllInOne(bool allComponent)
  162. {
  163. // reset the package file names
  164. packageFileNames.clear();
  165. packageFileNames.push_back(std::string(toplevel));
  166. packageFileNames[0] += "/"
  167. +std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  168. + this->GetOutputExtension();
  169. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  170. "Packaging all groups in one package..."
  171. "(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
  172. << std::endl);
  173. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0],archive);
  174. // The ALL GROUP in ONE package case
  175. if (! allComponent) {
  176. // iterate over the component groups
  177. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  178. for (compGIt=this->ComponentGroups.begin();
  179. compGIt!=this->ComponentGroups.end(); ++compGIt)
  180. {
  181. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  182. << compGIt->first
  183. << std::endl);
  184. // now iterate over the component of this group
  185. std::vector<cmCPackComponent*>::iterator compIt;
  186. for (compIt=(compGIt->second).Components.begin();
  187. compIt!=(compGIt->second).Components.end();
  188. ++compIt)
  189. {
  190. // Add the files of this component to the archive
  191. addOneComponentToArchive(archive,*compIt);
  192. }
  193. }
  194. }
  195. // The ALL COMPONENT in ONE package case
  196. else
  197. {
  198. std::map<std::string, cmCPackComponent>::iterator compIt;
  199. for (compIt=this->Components.begin();compIt!=this->Components.end();
  200. ++compIt )
  201. {
  202. // Add the files of this component to the archive
  203. addOneComponentToArchive(archive,&(compIt->second));
  204. }
  205. }
  206. // archive goes out of scope so it will finalized and closed.
  207. return 1;
  208. }
  209. //----------------------------------------------------------------------
  210. int cmCPackArchiveGenerator::PackageFiles()
  211. {
  212. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
  213. << toplevel << std::endl);
  214. if (SupportsComponentInstallation()) {
  215. // CASE 1 : COMPONENT ALL-IN-ONE package
  216. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  217. // then the package file is unique and should be open here.
  218. if (allComponentInOne ||
  219. (allGroupInOne && (!this->ComponentGroups.empty()))
  220. )
  221. {
  222. return PackageComponentsAllInOne(allComponentInOne);
  223. }
  224. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  225. // There will be 1 package for each component group
  226. // however one may require to ignore component group and
  227. // in this case you'll get 1 package for each component.
  228. else if ((!this->ComponentGroups.empty()) || (ignoreComponentGroup))
  229. {
  230. return PackageComponents(ignoreComponentGroup);
  231. }
  232. }
  233. // CASE 3 : NON COMPONENT package.
  234. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0],archive);
  235. std::vector<std::string>::const_iterator fileIt;
  236. std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
  237. cmSystemTools::ChangeDirectory(toplevel.c_str());
  238. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  239. {
  240. // Get the relative path to the file
  241. std::string rp = cmSystemTools::RelativePath(toplevel.c_str(),
  242. fileIt->c_str());
  243. archive.Add(rp);
  244. if(!archive)
  245. {
  246. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
  247. << *fileIt
  248. << "> to archive <"
  249. << packageFileNames[0] << "> .ERROR ="
  250. << archive.GetError()
  251. << std::endl);
  252. return 0;
  253. }
  254. }
  255. cmSystemTools::ChangeDirectory(dir.c_str());
  256. // The destructor of cmArchiveWrite will close and finish the write
  257. return 1;
  258. }
  259. //----------------------------------------------------------------------
  260. int cmCPackArchiveGenerator::GenerateHeader(std::ostream*)
  261. {
  262. return 1;
  263. }
  264. bool cmCPackArchiveGenerator::SupportsComponentInstallation() const {
  265. // The Component installation support should only
  266. // be activated if explicitly requested by the user
  267. // (for backward compatibility reason)
  268. if (IsOn("CPACK_ARCHIVE_COMPONENT_INSTALL"))
  269. {
  270. return true;
  271. }
  272. else
  273. {
  274. return false;
  275. }
  276. }