cmCPackArchiveGenerator.cxx 10 KB

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