cmCPackRPMGenerator.cxx 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "cmCPackRPMGenerator.h"
  11. #include "cmCPackLog.h"
  12. #include "cmSystemTools.h"
  13. cmCPackRPMGenerator::cmCPackRPMGenerator()
  14. {
  15. }
  16. cmCPackRPMGenerator::~cmCPackRPMGenerator()
  17. {
  18. }
  19. int cmCPackRPMGenerator::InitializeInternal()
  20. {
  21. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  22. if (cmSystemTools::IsOff(this->GetOption("CPACK_SET_DESTDIR"))) {
  23. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  24. }
  25. /* Replace space in CPACK_PACKAGE_NAME in order to avoid
  26. * rpmbuild scream on unwanted space in filename issue
  27. * Moreover RPM file do not usually embed space in filename
  28. */
  29. if (this->GetOption("CPACK_PACKAGE_NAME")) {
  30. std::string packageName = this->GetOption("CPACK_PACKAGE_NAME");
  31. std::replace(packageName.begin(), packageName.end(), ' ', '-');
  32. this->SetOption("CPACK_PACKAGE_NAME", packageName.c_str());
  33. }
  34. /* same for CPACK_PACKAGE_FILE_NAME */
  35. if (this->GetOption("CPACK_PACKAGE_FILE_NAME")) {
  36. std::string packageName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  37. std::replace(packageName.begin(), packageName.end(), ' ', '-');
  38. this->SetOption("CPACK_PACKAGE_FILE_NAME", packageName.c_str());
  39. }
  40. return this->Superclass::InitializeInternal();
  41. }
  42. void cmCPackRPMGenerator::AddGeneratedPackageNames()
  43. {
  44. // add the generated packages to package file names list
  45. std::string fileNames(this->GetOption("GEN_CPACK_OUTPUT_FILES"));
  46. const char sep = ';';
  47. std::string::size_type pos1 = 0;
  48. std::string::size_type pos2 = fileNames.find(sep, pos1 + 1);
  49. while (pos2 != std::string::npos) {
  50. packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  51. pos1 = pos2 + 1;
  52. pos2 = fileNames.find(sep, pos1 + 1);
  53. }
  54. packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  55. }
  56. int cmCPackRPMGenerator::PackageOnePack(std::string const& initialToplevel,
  57. std::string const& packageName)
  58. {
  59. int retval = 1;
  60. // Begin the archive for this pack
  61. std::string localToplevel(initialToplevel);
  62. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  63. std::string outputFileName(
  64. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  65. packageName, true) +
  66. this->GetOutputExtension());
  67. localToplevel += "/" + packageName;
  68. /* replace the TEMP DIRECTORY with the component one */
  69. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  70. packageFileName += "/" + outputFileName;
  71. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  72. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  73. /* replace the TEMPORARY package file name */
  74. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  75. packageFileName.c_str());
  76. // Tell CPackRPM.cmake the name of the component NAME.
  77. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT", packageName.c_str());
  78. // Tell CPackRPM.cmake the path where the component is.
  79. std::string component_path = "/";
  80. component_path += packageName;
  81. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  82. component_path.c_str());
  83. if (!this->ReadListFile("CPackRPM.cmake")) {
  84. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackRPM.cmake"
  85. << std::endl);
  86. retval = 0;
  87. }
  88. return retval;
  89. }
  90. int cmCPackRPMGenerator::PackageComponents(bool ignoreGroup)
  91. {
  92. int retval = 1;
  93. /* Reset package file name list it will be populated during the
  94. * component packaging run*/
  95. packageFileNames.clear();
  96. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  97. // The default behavior is to have one package by component group
  98. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  99. if (!ignoreGroup) {
  100. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  101. for (compGIt = this->ComponentGroups.begin();
  102. compGIt != this->ComponentGroups.end(); ++compGIt) {
  103. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  104. << compGIt->first << std::endl);
  105. retval &= PackageOnePack(initialTopLevel, compGIt->first);
  106. }
  107. // Handle Orphan components (components not belonging to any groups)
  108. std::map<std::string, cmCPackComponent>::iterator compIt;
  109. for (compIt = this->Components.begin(); compIt != this->Components.end();
  110. ++compIt) {
  111. // Does the component belong to a group?
  112. if (compIt->second.Group == NULL) {
  113. cmCPackLogger(
  114. cmCPackLog::LOG_VERBOSE, "Component <"
  115. << compIt->second.Name
  116. << "> does not belong to any group, package it separately."
  117. << std::endl);
  118. retval &= PackageOnePack(initialTopLevel, compIt->first);
  119. }
  120. }
  121. }
  122. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  123. // We build 1 package per component
  124. else {
  125. std::map<std::string, cmCPackComponent>::iterator compIt;
  126. for (compIt = this->Components.begin(); compIt != this->Components.end();
  127. ++compIt) {
  128. retval &= PackageOnePack(initialTopLevel, compIt->first);
  129. }
  130. }
  131. if (retval) {
  132. AddGeneratedPackageNames();
  133. }
  134. return retval;
  135. }
  136. int cmCPackRPMGenerator::PackageComponentsAllInOne(
  137. const std::string& compInstDirName)
  138. {
  139. int retval = 1;
  140. /* Reset package file name list it will be populated during the
  141. * component packaging run*/
  142. packageFileNames.clear();
  143. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  144. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  145. "Packaging all groups in one package..."
  146. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  147. << std::endl);
  148. // The ALL GROUPS in ONE package case
  149. std::string localToplevel(initialTopLevel);
  150. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  151. std::string outputFileName(
  152. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  153. this->GetOutputExtension());
  154. // all GROUP in one vs all COMPONENT in one
  155. localToplevel += "/" + compInstDirName;
  156. /* replace the TEMP DIRECTORY with the component one */
  157. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  158. packageFileName += "/" + outputFileName;
  159. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  160. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  161. /* replace the TEMPORARY package file name */
  162. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  163. packageFileName.c_str());
  164. if (!compInstDirName.empty()) {
  165. // Tell CPackRPM.cmake the path where the component is.
  166. std::string component_path = "/";
  167. component_path += compInstDirName;
  168. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  169. component_path.c_str());
  170. }
  171. if (this->ReadListFile("CPackRPM.cmake")) {
  172. AddGeneratedPackageNames();
  173. } else {
  174. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackRPM.cmake"
  175. << std::endl);
  176. retval = 0;
  177. }
  178. return retval;
  179. }
  180. int cmCPackRPMGenerator::PackageFiles()
  181. {
  182. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
  183. /* Are we in the component packaging case */
  184. if (WantsComponentInstallation()) {
  185. // CASE 1 : COMPONENT ALL-IN-ONE package
  186. // If ALL COMPONENTS in ONE package has been requested
  187. // then the package file is unique and should be open here.
  188. if (componentPackageMethod == ONE_PACKAGE) {
  189. return PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  190. }
  191. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  192. // There will be 1 package for each component group
  193. // however one may require to ignore component group and
  194. // in this case you'll get 1 package for each component.
  195. else {
  196. return PackageComponents(componentPackageMethod ==
  197. ONE_PACKAGE_PER_COMPONENT);
  198. }
  199. }
  200. // CASE 3 : NON COMPONENT package.
  201. else {
  202. return PackageComponentsAllInOne("");
  203. }
  204. }
  205. bool cmCPackRPMGenerator::SupportsComponentInstallation() const
  206. {
  207. return IsOn("CPACK_RPM_COMPONENT_INSTALL");
  208. }
  209. std::string cmCPackRPMGenerator::GetComponentInstallDirNameSuffix(
  210. const std::string& componentName)
  211. {
  212. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  213. return componentName;
  214. }
  215. if (componentPackageMethod == ONE_PACKAGE) {
  216. return std::string("ALL_COMPONENTS_IN_ONE");
  217. }
  218. // We have to find the name of the COMPONENT GROUP
  219. // the current COMPONENT belongs to.
  220. std::string groupVar =
  221. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  222. if (NULL != GetOption(groupVar)) {
  223. return std::string(GetOption(groupVar));
  224. } else {
  225. return componentName;
  226. }
  227. }