cmCPackArchiveGenerator.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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(); ++fileIt )
  54. {
  55. cmCPackLogger(cmCPackLog::LOG_DEBUG,"Adding file: " << (*fileIt) << std::endl);
  56. archive.Add(*fileIt);
  57. if (!archive)
  58. {
  59. cmCPackLogger(cmCPackLog::LOG_ERROR, "ERROR while packaging files: "
  60. << archive.GetError()
  61. << std::endl);
  62. return 0;
  63. }
  64. }
  65. // Go back to previous dir
  66. cmSystemTools::ChangeDirectory(dir.c_str());
  67. return 1;
  68. }
  69. /*
  70. * The macro will open/create a file 'filename'
  71. * an declare and open the associated
  72. * cmArchiveWrite 'archive' object.
  73. */
  74. #define DECLARE_AND_OPEN_ARCHIVE(filename,archive) \
  75. cmGeneratedFileStream gf; \
  76. gf.Open(filename.c_str(), false, true); \
  77. if (!GenerateHeader(&gf)) \
  78. { \
  79. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to generate Header for archive < " \
  80. << filename \
  81. << ">." << std::endl); \
  82. return 0; \
  83. } \
  84. cmArchiveWrite archive(gf,this->Compress, this->Archive); \
  85. if (!archive) \
  86. { \
  87. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < " \
  88. << filename \
  89. << ">. ERROR =" \
  90. << archive.GetError() \
  91. << std::endl); \
  92. return 0; \
  93. }
  94. //----------------------------------------------------------------------
  95. int cmCPackArchiveGenerator::PackageComponents(bool ignoreComponentGroup)
  96. {
  97. packageFileNames.clear();
  98. // The default behavior is to have one package by component group
  99. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  100. if (!ignoreComponentGroup)
  101. {
  102. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  103. for (compGIt=this->ComponentGroups.begin();
  104. compGIt!=this->ComponentGroups.end(); ++compGIt)
  105. {
  106. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  107. << compGIt->first
  108. << std::endl);
  109. // Begin the archive for this group
  110. std::string packageFileName= std::string(toplevel);
  111. packageFileName += "/"+std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))+"-"+compGIt->first + this->GetOutputExtension();
  112. // open a block in order to automatically close archive
  113. // at the end of the block
  114. {
  115. DECLARE_AND_OPEN_ARCHIVE(packageFileName,archive);
  116. // now iterate over the component of this group
  117. std::vector<cmCPackComponent*>::iterator compIt;
  118. for (compIt=(compGIt->second).Components.begin();
  119. compIt!=(compGIt->second).Components.end();
  120. ++compIt)
  121. {
  122. // Add the files of this component to the archive
  123. addOneComponentToArchive(archive,*compIt);
  124. }
  125. }
  126. // add the generated package to package file names list
  127. packageFileNames.push_back(packageFileName);
  128. }
  129. }
  130. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  131. // We build 1 package per component
  132. else
  133. {
  134. std::map<std::string, cmCPackComponent>::iterator compIt;
  135. for (compIt=this->Components.begin();compIt!=this->Components.end(); ++compIt )
  136. {
  137. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  138. std::string packageFileName = std::string(toplevel);
  139. localToplevel += "/"+ compIt->first;
  140. packageFileName += "/"+std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))+"-"+compIt->first + this->GetOutputExtension();
  141. {
  142. DECLARE_AND_OPEN_ARCHIVE(packageFileName,archive);
  143. // Add the files of this component to the archive
  144. addOneComponentToArchive(archive,&(compIt->second));
  145. }
  146. // add the generated package to package file names list
  147. packageFileNames.push_back(packageFileName);
  148. }
  149. }
  150. return 1;
  151. }
  152. //----------------------------------------------------------------------
  153. int cmCPackArchiveGenerator::PackageComponentsAllInOne(bool allComponentInOne)
  154. {
  155. // reset the package file names
  156. packageFileNames.clear();
  157. packageFileNames.push_back(std::string(toplevel));
  158. packageFileNames[0] += "/"+std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME"))+"-ALL" + this->GetOutputExtension();
  159. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging all groups in one package...(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
  160. << std::endl);
  161. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0],archive);
  162. // The ALL GROUP in ONE package case
  163. if (! allComponentInOne) {
  164. // iterate over the component groups
  165. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  166. for (compGIt=this->ComponentGroups.begin();
  167. compGIt!=this->ComponentGroups.end(); ++compGIt)
  168. {
  169. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  170. << compGIt->first
  171. << std::endl);
  172. // now iterate over the component of this group
  173. std::vector<cmCPackComponent*>::iterator compIt;
  174. for (compIt=(compGIt->second).Components.begin();
  175. compIt!=(compGIt->second).Components.end();
  176. ++compIt)
  177. {
  178. // Add the files of this component to the archive
  179. addOneComponentToArchive(archive,*compIt);
  180. }
  181. }
  182. }
  183. // The ALL COMPONENT in ONE package case
  184. else
  185. {
  186. std::map<std::string, cmCPackComponent>::iterator compIt;
  187. for (compIt=this->Components.begin();compIt!=this->Components.end(); ++compIt )
  188. {
  189. // Add the files of this component to the archive
  190. addOneComponentToArchive(archive,&(compIt->second));
  191. }
  192. }
  193. // archive goes out of scope so it will finalized and closed.
  194. return 1;
  195. }
  196. //----------------------------------------------------------------------
  197. int cmCPackArchiveGenerator::PackageFiles()
  198. {
  199. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: "
  200. << toplevel << std::endl);
  201. // The default behavior is to create 1 package by component group
  202. // unless the user asked to put all COMPONENTS in a single package
  203. bool allGroupInOne = (NULL != (this->GetOption("CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE")));
  204. bool allComponentInOne = (NULL != (this->GetOption("CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE")));
  205. bool ignoreComponentGroup = ( NULL != (this->GetOption("CPACK_COMPONENTS_IGNORE_GROUPS")));
  206. std::string groupingType;
  207. // Second way to specify grouping
  208. if (NULL != this->GetOption("CPACK_COMPONENTS_GROUPING")) {
  209. groupingType = this->GetOption("CPACK_COMPONENTS_GROUPING");
  210. }
  211. if (groupingType.length()>0)
  212. {
  213. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "["
  214. << this->Name << "]"
  215. << " requested component grouping = "<< groupingType <<std::endl);
  216. if (groupingType == "ALL_GROUP_IN_ONE")
  217. {
  218. allGroupInOne = true;
  219. }
  220. else if (groupingType == "ALL_COMPONENT_IN_ONE")
  221. {
  222. allComponentInOne = true;
  223. }
  224. else if (groupingType == "IGNORE")
  225. {
  226. ignoreComponentGroup = true;
  227. }
  228. else
  229. {
  230. cmCPackLogger(cmCPackLog::LOG_WARNING, "["
  231. << this->Name << "]"
  232. << " requested component grouping type <"<< groupingType
  233. << "> UNKNOWN not in (ALL_GROUP_IN_ONE,ALL_COMPONENT_IN_ONE,IGNORE)" <<std::endl);
  234. }
  235. }
  236. // Some components were defined but NO group
  237. // force ignoreGroups
  238. if (this->ComponentGroups.empty() && (!this->Components.empty()) && (!ignoreComponentGroup)) {
  239. cmCPackLogger(cmCPackLog::LOG_WARNING, "["
  240. << this->Name << "]"
  241. << " Some Components defined but NO component group:"
  242. << " Ignoring component group."
  243. << std::endl);
  244. ignoreComponentGroup = true;
  245. }
  246. // CASE 1 : COMPONENT ALL-IN-ONE package
  247. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  248. // then the package file is unique and should be open here.
  249. if (allComponentInOne || (allGroupInOne && (!this->ComponentGroups.empty())))
  250. {
  251. return PackageComponentsAllInOne(allComponentInOne);
  252. }
  253. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  254. // There will be 1 package for each component group
  255. // however one may require to ignore component group and
  256. // in this case you'll get 1 package for each component.
  257. else if ((!this->ComponentGroups.empty()) || (ignoreComponentGroup))
  258. {
  259. return PackageComponents(ignoreComponentGroup);
  260. }
  261. // CASE 3 : NON COMPONENT package.
  262. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0],archive);
  263. std::vector<std::string>::const_iterator fileIt;
  264. std::string dir = cmSystemTools::GetCurrentWorkingDirectory();
  265. cmSystemTools::ChangeDirectory(toplevel.c_str());
  266. for ( fileIt = files.begin(); fileIt != files.end(); ++ fileIt )
  267. {
  268. // Get the relative path to the file
  269. std::string rp = cmSystemTools::RelativePath(toplevel.c_str(), fileIt->c_str());
  270. archive.Add(rp);
  271. if(!archive)
  272. {
  273. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
  274. << *fileIt
  275. << "> to archive <"
  276. << packageFileNames[0] << "> .ERROR ="
  277. << archive.GetError()
  278. << std::endl);
  279. return 0;
  280. }
  281. }
  282. cmSystemTools::ChangeDirectory(dir.c_str());
  283. // The destructor of cmArchiveWrite will close and finish the write
  284. return 1;
  285. }
  286. //----------------------------------------------------------------------
  287. int cmCPackArchiveGenerator::GenerateHeader(std::ostream*)
  288. {
  289. return 1;
  290. }
  291. bool cmCPackArchiveGenerator::SupportsComponentInstallation() const {
  292. return true;
  293. }