cmCPackArchiveGenerator.cxx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <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. std::string cmCPackArchiveGenerator::GetArchiveComponentFileName(
  23. const std::string& component, bool isGroupName)
  24. {
  25. std::string componentUpper(cmSystemTools::UpperCase(component));
  26. std::string packageFileName;
  27. if (this->IsSet("CPACK_ARCHIVE_" + componentUpper + "_FILE_NAME")) {
  28. packageFileName +=
  29. this->GetOption("CPACK_ARCHIVE_" + componentUpper + "_FILE_NAME");
  30. } else if (this->IsSet("CPACK_ARCHIVE_FILE_NAME")) {
  31. packageFileName += GetComponentPackageFileName(
  32. this->GetOption("CPACK_ARCHIVE_FILE_NAME"), component, isGroupName);
  33. } else {
  34. packageFileName += GetComponentPackageFileName(
  35. this->GetOption("CPACK_PACKAGE_FILE_NAME"), component, isGroupName);
  36. }
  37. packageFileName += this->GetOutputExtension();
  38. return packageFileName;
  39. }
  40. int cmCPackArchiveGenerator::InitializeInternal()
  41. {
  42. this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "1");
  43. return this->Superclass::InitializeInternal();
  44. }
  45. int cmCPackArchiveGenerator::addOneComponentToArchive(
  46. cmArchiveWrite& archive, cmCPackComponent* component)
  47. {
  48. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  49. " - packaging component: " << component->Name << std::endl);
  50. // Add the files of this component to the archive
  51. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  52. localToplevel += "/" + component->Name;
  53. // Change to local toplevel
  54. cmWorkingDirectory workdir(localToplevel);
  55. std::string filePrefix;
  56. if (this->IsOn("CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY")) {
  57. filePrefix = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  58. filePrefix += "/";
  59. }
  60. const char* installPrefix =
  61. this->GetOption("CPACK_PACKAGING_INSTALL_PREFIX");
  62. if (installPrefix && installPrefix[0] == '/' && installPrefix[1] != 0) {
  63. // add to file prefix and remove the leading '/'
  64. filePrefix += installPrefix + 1;
  65. filePrefix += "/";
  66. }
  67. for (std::string const& file : component->Files) {
  68. std::string rp = filePrefix + file;
  69. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Adding file: " << rp << std::endl);
  70. archive.Add(rp, 0, nullptr, false);
  71. if (!archive) {
  72. cmCPackLogger(cmCPackLog::LOG_ERROR, "ERROR while packaging files: "
  73. << archive.GetError() << std::endl);
  74. return 0;
  75. }
  76. }
  77. return 1;
  78. }
  79. /*
  80. * The macro will open/create a file 'filename'
  81. * an declare and open the associated
  82. * cmArchiveWrite 'archive' object.
  83. */
  84. #define DECLARE_AND_OPEN_ARCHIVE(filename, archive) \
  85. cmGeneratedFileStream gf; \
  86. gf.Open((filename).c_str(), false, true); \
  87. if (!GenerateHeader(&gf)) { \
  88. cmCPackLogger(cmCPackLog::LOG_ERROR, \
  89. "Problem to generate Header for archive < " \
  90. << (filename) << ">." << std::endl); \
  91. return 0; \
  92. } \
  93. cmArchiveWrite archive(gf, this->Compress, this->ArchiveFormat); \
  94. if (!(archive)) { \
  95. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem to create archive < " \
  96. << (filename) << ">. ERROR =" << (archive).GetError() \
  97. << std::endl); \
  98. return 0; \
  99. }
  100. int cmCPackArchiveGenerator::PackageComponents(bool ignoreGroup)
  101. {
  102. packageFileNames.clear();
  103. // The default behavior is to have one package by component group
  104. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  105. if (!ignoreGroup) {
  106. for (auto const& compG : this->ComponentGroups) {
  107. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  108. "Packaging component group: " << compG.first << std::endl);
  109. // Begin the archive for this group
  110. std::string packageFileName = std::string(toplevel) + "/" +
  111. this->GetArchiveComponentFileName(compG.first, true);
  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. for (cmCPackComponent* comp : (compG.second).Components) {
  118. // Add the files of this component to the archive
  119. addOneComponentToArchive(archive, comp);
  120. }
  121. }
  122. // add the generated package to package file names list
  123. packageFileNames.push_back(std::move(packageFileName));
  124. }
  125. // Handle Orphan components (components not belonging to any groups)
  126. for (auto& comp : this->Components) {
  127. // Does the component belong to a group?
  128. if (comp.second.Group == nullptr) {
  129. cmCPackLogger(
  130. cmCPackLog::LOG_VERBOSE, "Component <"
  131. << comp.second.Name
  132. << "> does not belong to any group, package it separately."
  133. << std::endl);
  134. std::string localToplevel(
  135. this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  136. std::string packageFileName = std::string(toplevel);
  137. localToplevel += "/" + comp.first;
  138. packageFileName +=
  139. "/" + this->GetArchiveComponentFileName(comp.first, false);
  140. {
  141. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  142. // Add the files of this component to the archive
  143. addOneComponentToArchive(archive, &(comp.second));
  144. }
  145. // add the generated package to package file names list
  146. packageFileNames.push_back(std::move(packageFileName));
  147. }
  148. }
  149. }
  150. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  151. // We build 1 package per component
  152. else {
  153. for (auto& comp : this->Components) {
  154. std::string localToplevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  155. std::string packageFileName = std::string(toplevel);
  156. localToplevel += "/" + comp.first;
  157. packageFileName +=
  158. "/" + this->GetArchiveComponentFileName(comp.first, false);
  159. {
  160. DECLARE_AND_OPEN_ARCHIVE(packageFileName, archive);
  161. // Add the files of this component to the archive
  162. addOneComponentToArchive(archive, &(comp.second));
  163. }
  164. // add the generated package to package file names list
  165. packageFileNames.push_back(std::move(packageFileName));
  166. }
  167. }
  168. return 1;
  169. }
  170. int cmCPackArchiveGenerator::PackageComponentsAllInOne()
  171. {
  172. // reset the package file names
  173. packageFileNames.clear();
  174. packageFileNames.emplace_back(toplevel);
  175. packageFileNames[0] += "/";
  176. if (this->IsSet("CPACK_ARCHIVE_FILE_NAME")) {
  177. packageFileNames[0] += this->GetOption("CPACK_ARCHIVE_FILE_NAME");
  178. } else {
  179. packageFileNames[0] += this->GetOption("CPACK_PACKAGE_FILE_NAME");
  180. }
  181. packageFileNames[0] += this->GetOutputExtension();
  182. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  183. "Packaging all groups in one package..."
  184. "(CPACK_COMPONENTS_ALL_GROUPS_IN_ONE_PACKAGE is set)"
  185. << std::endl);
  186. DECLARE_AND_OPEN_ARCHIVE(packageFileNames[0], archive);
  187. // The ALL COMPONENTS in ONE package case
  188. for (auto& comp : this->Components) {
  189. // Add the files of this component to the archive
  190. addOneComponentToArchive(archive, &(comp.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. cmWorkingDirectory workdir(toplevel);
  215. for (std::string const& file : files) {
  216. // Get the relative path to the file
  217. std::string rp = cmSystemTools::RelativePath(toplevel, file);
  218. archive.Add(rp, 0, nullptr, false);
  219. if (!archive) {
  220. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem while adding file< "
  221. << file << "> 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. }