cmCPackArchiveGenerator.cxx 11 KB

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