cmCPackArchiveGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  116. compGIt->first,
  117. true)
  118. + this->GetOutputExtension();
  119. // open a block in order to automatically close archive
  120. // at the end of the block
  121. {
  122. DECLARE_AND_OPEN_ARCHIVE(packageFileName,archive);
  123. // now iterate over the component of this group
  124. std::vector<cmCPackComponent*>::iterator compIt;
  125. for (compIt=(compGIt->second).Components.begin();
  126. compIt!=(compGIt->second).Components.end();
  127. ++compIt)
  128. {
  129. // Add the files of this component to the archive
  130. addOneComponentToArchive(archive,*compIt);
  131. }
  132. }
  133. // add the generated package to package file names list
  134. packageFileNames.push_back(packageFileName);
  135. }
  136. }
  137. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  138. // We build 1 package per component
  139. else
  140. {
  141. std::map<std::string, cmCPackComponent>::iterator compIt;
  142. for (compIt=this->Components.begin();
  143. compIt!=this->Components.end(); ++compIt )
  144. {
  145. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  146. std::string packageFileName = std::string(toplevel);
  147. localToplevel += "/"+ compIt->first;
  148. packageFileName += "/"+
  149. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  150. compIt->first,
  151. false)
  152. + this->GetOutputExtension();
  153. {
  154. DECLARE_AND_OPEN_ARCHIVE(packageFileName,archive);
  155. // Add the files of this component to the archive
  156. addOneComponentToArchive(archive,&(compIt->second));
  157. }
  158. // add the generated package to package file names list
  159. packageFileNames.push_back(packageFileName);
  160. }
  161. }
  162. return 1;
  163. }
  164. //----------------------------------------------------------------------
  165. int cmCPackArchiveGenerator::PackageComponentsAllInOne(bool allComponent)
  166. {
  167. // reset the package file names
  168. packageFileNames.clear();
  169. packageFileNames.push_back(std::string(toplevel));
  170. packageFileNames[0] += "/"
  171. +std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))
  172. + this->GetOutputExtension();
  173. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  174. "Packaging all groups in one package..."
  175. "(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
  176. << std::endl);
  177. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0],archive);
  178. // The ALL GROUP in ONE package case
  179. if (! allComponent) {
  180. // iterate over the component groups
  181. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  182. for (compGIt=this->ComponentGroups.begin();
  183. compGIt!=this->ComponentGroups.end(); ++compGIt)
  184. {
  185. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  186. << compGIt->first
  187. << std::endl);
  188. // now iterate over the component of this group
  189. std::vector<cmCPackComponent*>::iterator compIt;
  190. for (compIt=(compGIt->second).Components.begin();
  191. compIt!=(compGIt->second).Components.end();
  192. ++compIt)
  193. {
  194. // Add the files of this component to the archive
  195. addOneComponentToArchive(archive,*compIt);
  196. }
  197. }
  198. }
  199. // The ALL COMPONENT in ONE package case
  200. else
  201. {
  202. std::map<std::string, cmCPackComponent>::iterator compIt;
  203. for (compIt=this->Components.begin();compIt!=this->Components.end();
  204. ++compIt )
  205. {
  206. // Add the files of this component to the archive
  207. addOneComponentToArchive(archive,&(compIt->second));
  208. }
  209. }
  210. // archive goes out of scope so it will finalized and closed.
  211. return 1;
  212. }
  213. //----------------------------------------------------------------------
  214. int cmCPackArchiveGenerator::PackageFiles()
  215. {
  216. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
  217. << toplevel << std::endl);
  218. if (SupportsComponentInstallation()) {
  219. // CASE 1 : COMPONENT ALL-IN-ONE package
  220. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  221. // then the package file is unique and should be open here.
  222. if (allComponentInOne ||
  223. (allGroupInOne && (!this->ComponentGroups.empty()))
  224. )
  225. {
  226. return PackageComponentsAllInOne(allComponentInOne);
  227. }
  228. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  229. // There will be 1 package for each component group
  230. // however one may require to ignore component group and
  231. // in this case you'll get 1 package for each component.
  232. else if ((!this->ComponentGroups.empty()) || (ignoreComponentGroup))
  233. {
  234. return PackageComponents(ignoreComponentGroup);
  235. }
  236. }
  237. // CASE 3 : NON COMPONENT package.
  238. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0],archive);
  239. std::vector<std::string>::const_iterator fileIt;
  240. std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
  241. cmSystemTools::ChangeDirectory(toplevel.c_str());
  242. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  243. {
  244. // Get the relative path to the file
  245. std::string rp = cmSystemTools::RelativePath(toplevel.c_str(),
  246. fileIt->c_str());
  247. archive.Add(rp);
  248. if(!archive)
  249. {
  250. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
  251. << *fileIt
  252. << "> to archive <"
  253. << packageFileNames[0] << "> .ERROR ="
  254. << archive.GetError()
  255. << std::endl);
  256. return 0;
  257. }
  258. }
  259. cmSystemTools::ChangeDirectory(dir.c_str());
  260. // The destructor of cmArchiveWrite will close and finish the write
  261. return 1;
  262. }
  263. //----------------------------------------------------------------------
  264. int cmCPackArchiveGenerator::GenerateHeader(std::ostream*)
  265. {
  266. return 1;
  267. }
  268. bool cmCPackArchiveGenerator::SupportsComponentInstallation() const {
  269. // The Component installation support should only
  270. // be activated if explicitly requested by the user
  271. // (for backward compatibility reason)
  272. if (IsOn("CPACK_ARCHIVE_COMPONENT_INSTALL"))
  273. {
  274. return true;
  275. }
  276. else
  277. {
  278. return false;
  279. }
  280. }