cmCPackRPMGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "cmCPackRPMGenerator.h"
  4. #include "cmCPackComponentGroup.h"
  5. #include "cmCPackGenerator.h"
  6. #include "cmCPackLog.h"
  7. #include "cmSystemTools.h"
  8. #include <algorithm>
  9. #include <map>
  10. #include <ostream>
  11. #include <utility>
  12. #include <vector>
  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. const char* mainComponent = this->GetOption("CPACK_RPM_MAIN_COMPONENT");
  98. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") &&
  99. !this->IsOn("CPACK_RPM_DEBUGINFO_PACKAGE")) {
  100. // check if we need to set CPACK_RPM_DEBUGINFO_PACKAGE because non of
  101. // the components is setting per component debuginfo package variable
  102. bool shouldSet = true;
  103. if (ignoreGroup) {
  104. std::map<std::string, cmCPackComponent>::iterator compIt;
  105. for (compIt = this->Components.begin(); compIt != this->Components.end();
  106. ++compIt) {
  107. std::string component(compIt->first);
  108. std::transform(component.begin(), component.end(), component.begin(),
  109. ::toupper);
  110. if (this->IsOn("CPACK_RPM_" + compIt->first + "_DEBUGINFO_PACKAGE") ||
  111. this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
  112. shouldSet = false;
  113. break;
  114. }
  115. }
  116. } else {
  117. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  118. for (compGIt = this->ComponentGroups.begin();
  119. compGIt != this->ComponentGroups.end(); ++compGIt) {
  120. std::string component(compGIt->first);
  121. std::transform(component.begin(), component.end(), component.begin(),
  122. ::toupper);
  123. if (this->IsOn("CPACK_RPM_" + compGIt->first + "_DEBUGINFO_PACKAGE") ||
  124. this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
  125. shouldSet = false;
  126. break;
  127. }
  128. }
  129. if (shouldSet) {
  130. std::map<std::string, cmCPackComponent>::iterator compIt;
  131. for (compIt = this->Components.begin();
  132. compIt != this->Components.end(); ++compIt) {
  133. // Does the component belong to a group?
  134. if (compIt->second.Group == CM_NULLPTR) {
  135. std::string component(compIt->first);
  136. std::transform(component.begin(), component.end(),
  137. component.begin(), ::toupper);
  138. if (this->IsOn("CPACK_RPM_" + compIt->first +
  139. "_DEBUGINFO_PACKAGE") ||
  140. this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
  141. shouldSet = false;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. }
  148. if (shouldSet) {
  149. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Setting "
  150. << "CPACK_RPM_DEBUGINFO_PACKAGE because "
  151. << "CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE is set but "
  152. << " none of the "
  153. << "CPACK_RPM_<component>_DEBUGINFO_PACKAGE variables "
  154. << "are set." << std::endl);
  155. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  156. }
  157. }
  158. if (mainComponent) {
  159. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  160. this->SetOption("GENERATE_SPEC_PARTS", "ON");
  161. }
  162. std::string mainComponentUpper(mainComponent);
  163. std::transform(mainComponentUpper.begin(), mainComponentUpper.end(),
  164. mainComponentUpper.begin(), ::toupper);
  165. // The default behavior is to have one package by component group
  166. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  167. if (!ignoreGroup) {
  168. std::map<std::string, cmCPackComponentGroup>::iterator mainCompGIt =
  169. this->ComponentGroups.end();
  170. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  171. for (compGIt = this->ComponentGroups.begin();
  172. compGIt != this->ComponentGroups.end(); ++compGIt) {
  173. std::string component(compGIt->first);
  174. std::transform(component.begin(), component.end(), component.begin(),
  175. ::toupper);
  176. if (mainComponentUpper == component) {
  177. // main component will be handled last
  178. mainCompGIt = compGIt;
  179. continue;
  180. }
  181. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  182. << compGIt->first << std::endl);
  183. retval &= PackageOnePack(initialTopLevel, compGIt->first);
  184. }
  185. // Handle Orphan components (components not belonging to any groups)
  186. std::map<std::string, cmCPackComponent>::iterator mainCompIt =
  187. this->Components.end();
  188. std::map<std::string, cmCPackComponent>::iterator compIt;
  189. for (compIt = this->Components.begin(); compIt != this->Components.end();
  190. ++compIt) {
  191. // Does the component belong to a group?
  192. if (compIt->second.Group == CM_NULLPTR) {
  193. std::string component(compIt->first);
  194. std::transform(component.begin(), component.end(), component.begin(),
  195. ::toupper);
  196. if (mainComponentUpper == component) {
  197. // main component will be handled last
  198. mainCompIt = compIt;
  199. continue;
  200. }
  201. cmCPackLogger(
  202. cmCPackLog::LOG_VERBOSE, "Component <"
  203. << compIt->second.Name
  204. << "> does not belong to any group, package it separately."
  205. << std::endl);
  206. retval &= PackageOnePack(initialTopLevel, compIt->first);
  207. }
  208. }
  209. if (retval) {
  210. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  211. if (mainCompGIt != this->ComponentGroups.end()) {
  212. retval &= PackageOnePack(initialTopLevel, mainCompGIt->first);
  213. } else if (mainCompIt != this->Components.end()) {
  214. retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
  215. } else {
  216. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPACK_RPM_MAIN_COMPONENT set"
  217. << " to non existing component.\n");
  218. retval = 0;
  219. }
  220. }
  221. }
  222. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  223. // We build 1 package per component
  224. else {
  225. std::map<std::string, cmCPackComponent>::iterator mainCompIt =
  226. this->Components.end();
  227. std::map<std::string, cmCPackComponent>::iterator compIt;
  228. for (compIt = this->Components.begin(); compIt != this->Components.end();
  229. ++compIt) {
  230. std::string component(compIt->first);
  231. std::transform(component.begin(), component.end(), component.begin(),
  232. ::toupper);
  233. if (mainComponentUpper == component) {
  234. // main component will be handled last
  235. mainCompIt = compIt;
  236. continue;
  237. }
  238. retval &= PackageOnePack(initialTopLevel, compIt->first);
  239. }
  240. if (retval) {
  241. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  242. if (mainCompIt != this->Components.end()) {
  243. retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
  244. } else {
  245. cmCPackLogger(cmCPackLog::LOG_ERROR, "CPACK_RPM_MAIN_COMPONENT set"
  246. << " to non existing component.\n");
  247. retval = 0;
  248. }
  249. }
  250. }
  251. } else if (!this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") ||
  252. this->Components.size() == 1) {
  253. // The default behavior is to have one package by component group
  254. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  255. if (!ignoreGroup) {
  256. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  257. for (compGIt = this->ComponentGroups.begin();
  258. compGIt != this->ComponentGroups.end(); ++compGIt) {
  259. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  260. << compGIt->first << std::endl);
  261. retval &= PackageOnePack(initialTopLevel, compGIt->first);
  262. }
  263. // Handle Orphan components (components not belonging to any groups)
  264. std::map<std::string, cmCPackComponent>::iterator compIt;
  265. for (compIt = this->Components.begin(); compIt != this->Components.end();
  266. ++compIt) {
  267. // Does the component belong to a group?
  268. if (compIt->second.Group == CM_NULLPTR) {
  269. cmCPackLogger(
  270. cmCPackLog::LOG_VERBOSE, "Component <"
  271. << compIt->second.Name
  272. << "> does not belong to any group, package it separately."
  273. << std::endl);
  274. retval &= PackageOnePack(initialTopLevel, compIt->first);
  275. }
  276. }
  277. }
  278. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  279. // We build 1 package per component
  280. else {
  281. std::map<std::string, cmCPackComponent>::iterator compIt;
  282. for (compIt = this->Components.begin(); compIt != this->Components.end();
  283. ++compIt) {
  284. retval &= PackageOnePack(initialTopLevel, compIt->first);
  285. }
  286. }
  287. } else {
  288. cmCPackLogger(
  289. cmCPackLog::LOG_ERROR, "CPACK_RPM_MAIN_COMPONENT not set but"
  290. << " it is mandatory with CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE"
  291. << " being set.\n");
  292. retval = 0;
  293. }
  294. if (retval) {
  295. AddGeneratedPackageNames();
  296. }
  297. return retval;
  298. }
  299. int cmCPackRPMGenerator::PackageComponentsAllInOne(
  300. const std::string& compInstDirName)
  301. {
  302. int retval = 1;
  303. /* Reset package file name list it will be populated during the
  304. * component packaging run*/
  305. packageFileNames.clear();
  306. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  307. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  308. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  309. }
  310. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  311. "Packaging all groups in one package..."
  312. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  313. << std::endl);
  314. // The ALL GROUPS in ONE package case
  315. std::string localToplevel(initialTopLevel);
  316. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  317. std::string outputFileName(
  318. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  319. this->GetOutputExtension());
  320. // all GROUP in one vs all COMPONENT in one
  321. localToplevel += "/" + compInstDirName;
  322. /* replace the TEMP DIRECTORY with the component one */
  323. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  324. packageFileName += "/" + outputFileName;
  325. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  326. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  327. /* replace the TEMPORARY package file name */
  328. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  329. packageFileName.c_str());
  330. if (!compInstDirName.empty()) {
  331. // Tell CPackRPM.cmake the path where the component is.
  332. std::string component_path = "/";
  333. component_path += compInstDirName;
  334. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  335. component_path.c_str());
  336. }
  337. if (this->ReadListFile("CPackRPM.cmake")) {
  338. AddGeneratedPackageNames();
  339. } else {
  340. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackRPM.cmake"
  341. << std::endl);
  342. retval = 0;
  343. }
  344. return retval;
  345. }
  346. int cmCPackRPMGenerator::PackageFiles()
  347. {
  348. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
  349. /* Are we in the component packaging case */
  350. if (WantsComponentInstallation()) {
  351. // CASE 1 : COMPONENT ALL-IN-ONE package
  352. // If ALL COMPONENTS in ONE package has been requested
  353. // then the package file is unique and should be open here.
  354. if (componentPackageMethod == ONE_PACKAGE) {
  355. return PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  356. }
  357. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  358. // There will be 1 package for each component group
  359. // however one may require to ignore component group and
  360. // in this case you'll get 1 package for each component.
  361. return PackageComponents(componentPackageMethod ==
  362. ONE_PACKAGE_PER_COMPONENT);
  363. }
  364. // CASE 3 : NON COMPONENT package.
  365. return PackageComponentsAllInOne("");
  366. }
  367. bool cmCPackRPMGenerator::SupportsComponentInstallation() const
  368. {
  369. return IsOn("CPACK_RPM_COMPONENT_INSTALL");
  370. }
  371. std::string cmCPackRPMGenerator::GetComponentInstallDirNameSuffix(
  372. const std::string& componentName)
  373. {
  374. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  375. return componentName;
  376. }
  377. if (componentPackageMethod == ONE_PACKAGE) {
  378. return std::string("ALL_COMPONENTS_IN_ONE");
  379. }
  380. // We have to find the name of the COMPONENT GROUP
  381. // the current COMPONENT belongs to.
  382. std::string groupVar =
  383. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  384. if (CM_NULLPTR != GetOption(groupVar)) {
  385. return std::string(GetOption(groupVar));
  386. }
  387. return componentName;
  388. }