cmCPackArchiveGenerator.cxx 11 KB

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