cmCPackArchiveGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCPackArchiveGenerator.h"
  4. #include "cmCPackComponentGroup.h"
  5. #include "cmCPackGenerator.h"
  6. #include "cmCPackLog.h"
  7. #include "cmGeneratedFileStream.h"
  8. #include "cmSystemTools.h"
  9. #include "cmWorkingDirectory.h"
  10. #include <map>
  11. #include <ostream>
  12. #include <utility>
  13. #include <vector>
  14. cmCPackArchiveGenerator::cmCPackArchiveGenerator(cmArchiveWrite::Compress t,
  15. std::string const& format)
  16. {
  17. this->Compress = t;
  18. this->ArchiveFormat = format;
  19. }
  20. cmCPackArchiveGenerator::~cmCPackArchiveGenerator()
  21. {
  22. }
  23. int cmCPackArchiveGenerator::InitializeInternal()
  24. {
  25. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  26. return this->Superclass::InitializeInternal();
  27. }
  28. int cmCPackArchiveGenerator::addOneComponentToArchive(
  29. cmArchiveWrite& archive, cmCPackComponent* component)
  30. {
  31. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  32. " - packaging component: " << component->Name << std::endl);
  33. // Add the files of this component to the archive
  34. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  35. localToplevel += "/" + component->Name;
  36. // Change to local toplevel
  37. cmWorkingDirectory workdir(localToplevel);
  38. std::string filePrefix;
  39. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  40. filePrefix = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  41. filePrefix += "/";
  42. }
  43. const char* installPrefix =
  44. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  45. if (installPrefix && installPrefix[0] == '/' && installPrefix[1] != 0) {
  46. // add to file prefix and remove the leading '/'
  47. filePrefix += installPrefix + 1;
  48. filePrefix += "/";
  49. }
  50. std::vector<std::string>::const_iterator fileIt;
  51. for (fileIt = component->Files.begin(); fileIt != component->Files.end();
  52. ++fileIt) {
  53. std::string rp = filePrefix + *fileIt;
  54. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file: " << rp << std::endl);
  55. archive.Add(rp, 0, CM_NULLPTR, false);
  56. if (!archive) {
  57. cmCPackLogger(cmCPackLog::LOG_ERROR, "ERROR while packaging files: "
  58. << archive.GetError() << std::endl);
  59. return 0;
  60. }
  61. }
  62. return 1;
  63. }
  64. /*
  65. * The macro will open/create a file 'filename'
  66. * an declare and open the associated
  67. * cmArchiveWrite 'archive' object.
  68. */
  69. #define DECLARE_AND_OPEN_ARCHIVE(filename, archive) \
  70. cmGeneratedFileStream gf; \
  71. gf.Open((filename).c_str(), false, true); \
  72. if (!GenerateHeader(&gf)) { \
  73. cmCPackLogger(cmCPackLog::LOG_ERROR, \
  74. "Problem to generate Header for archive < " \
  75. << (filename) << ">." << std::endl); \
  76. return 0; \
  77. } \
  78. cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat); \
  79. if (!(archive)) { \
  80. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < " \
  81. << (filename) << ">. ERROR =" << (archive).GetError() \
  82. << std::endl); \
  83. return 0; \
  84. }
  85. int cmCPackArchiveGenerator::PackageComponents(bool ignoreGroup)
  86. {
  87. packageFileNames.clear();
  88. // The default behavior is to have one package by component group
  89. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  90. if (!ignoreGroup) {
  91. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  92. for (compGIt = this->ComponentGroups.begin();
  93. compGIt != this->ComponentGroups.end(); ++compGIt) {
  94. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  95. << compGIt->first << std::endl);
  96. // Begin the archive for this group
  97. std::string packageFileName = std::string(toplevel);
  98. packageFileName += "/" +
  99. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  100. compGIt->first, true) +
  101. this->GetOutputExtension();
  102. // open a block in order to automatically close archive
  103. // at the end of the block
  104. {
  105. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  106. // now iterate over the component of this group
  107. std::vector<cmCPackComponent*>::iterator compIt;
  108. for (compIt = (compGIt->second).Components.begin();
  109. compIt != (compGIt->second).Components.end(); ++compIt) {
  110. // Add the files of this component to the archive
  111. addOneComponentToArchive(archive, *compIt);
  112. }
  113. }
  114. // add the generated package to package file names list
  115. packageFileNames.push_back(packageFileName);
  116. }
  117. // Handle Orphan components (components not belonging to any groups)
  118. std::map<std::string, cmCPackComponent>::iterator compIt;
  119. for (compIt = this->Components.begin(); compIt != this->Components.end();
  120. ++compIt) {
  121. // Does the component belong to a group?
  122. if (compIt->second.Group == CM_NULLPTR) {
  123. cmCPackLogger(
  124. cmCPackLog::LOG_VERBOSE, "Component <"
  125. << compIt->second.Name
  126. << "> does not belong to any group, package it separately."
  127. << std::endl);
  128. std::string localToplevel(
  129. this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  130. std::string packageFileName = std::string(toplevel);
  131. localToplevel += "/" + compIt->first;
  132. packageFileName += "/" + GetComponentPackageFileName(
  133. this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  134. compIt->first, false) +
  135. this->GetOutputExtension();
  136. {
  137. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  138. // Add the files of this component to the archive
  139. addOneComponentToArchive(archive, &(compIt->second));
  140. }
  141. // add the generated package to package file names list
  142. packageFileNames.push_back(packageFileName);
  143. }
  144. }
  145. }
  146. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  147. // We build 1 package per component
  148. else {
  149. std::map<std::string, cmCPackComponent>::iterator compIt;
  150. for (compIt = this->Components.begin(); compIt != this->Components.end();
  151. ++compIt) {
  152. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  153. std::string packageFileName = std::string(toplevel);
  154. localToplevel += "/" + compIt->first;
  155. packageFileName += "/" +
  156. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  157. compIt->first, 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. return 1;
  169. }
  170. int cmCPackArchiveGenerator::PackageComponentsAllInOne()
  171. {
  172. // reset the package file names
  173. packageFileNames.clear();
  174. packageFileNames.push_back(std::string(toplevel));
  175. packageFileNames[0] += "/" +
  176. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  177. this->GetOutputExtension();
  178. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  179. "Packaging all groups in one package..."
  180. "(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
  181. << std::endl);
  182. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
  183. // The ALL COMPONENTS in ONE package case
  184. std::map<std::string, cmCPackComponent>::iterator compIt;
  185. for (compIt = this->Components.begin(); compIt != this->Components.end();
  186. ++compIt) {
  187. // Add the files of this component to the archive
  188. addOneComponentToArchive(archive, &(compIt->second));
  189. }
  190. // archive goes out of scope so it will finalized and closed.
  191. return 1;
  192. }
  193. int cmCPackArchiveGenerator::PackageFiles()
  194. {
  195. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
  196. if (WantsComponentInstallation()) {
  197. // CASE 1 : COMPONENT ALL-IN-ONE package
  198. // If ALL COMPONENTS in ONE package has been requested
  199. // then the package file is unique and should be open here.
  200. if (componentPackageMethod == ONE_PACKAGE) {
  201. return PackageComponentsAllInOne();
  202. }
  203. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  204. // There will be 1 package for each component group
  205. // however one may require to ignore component group and
  206. // in this case you'll get 1 package for each component.
  207. return PackageComponents(componentPackageMethod ==
  208. ONE_PACKAGE_PER_COMPONENT);
  209. }
  210. // CASE 3 : NON COMPONENT package.
  211. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
  212. std::vector<std::string>::const_iterator fileIt;
  213. cmWorkingDirectory workdir(toplevel);
  214. for (fileIt = files.begin(); fileIt != files.end(); ++fileIt) {
  215. // Get the relative path to the file
  216. std::string rp =
  217. cmSystemTools::RelativePath(toplevel.c_str(), fileIt->c_str());
  218. archive.Add(rp, 0, CM_NULLPTR, false);
  219. if (!archive) {
  220. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
  221. << *fileIt << "> to archive <" << packageFileNames[0]
  222. << "> .ERROR =" << archive.GetError() << std::endl);
  223. return 0;
  224. }
  225. }
  226. // The destructor of cmArchiveWrite will close and finish the write
  227. return 1;
  228. }
  229. int cmCPackArchiveGenerator::GenerateHeader(std::ostream* /*unused*/)
  230. {
  231. return 1;
  232. }
  233. bool cmCPackArchiveGenerator::SupportsComponentInstallation() const
  234. {
  235. // The Component installation support should only
  236. // be activated if explicitly requested by the user
  237. // (for backward compatibility reason)
  238. return IsOn("CPACK_ARCHIVE_COMPONENT_INSTALL");
  239. }