cmCPackRPMGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <algorithm>
  5. #include <ctype.h>
  6. #include <map>
  7. #include <ostream>
  8. #include <utility>
  9. #include <vector>
  10. #include "cmCPackComponentGroup.h"
  11. #include "cmCPackGenerator.h"
  12. #include "cmCPackLog.h"
  13. #include "cmStringAlgorithms.h"
  14. #include "cmSystemTools.h"
  15. cmCPackRPMGenerator::cmCPackRPMGenerator() = default;
  16. cmCPackRPMGenerator::~cmCPackRPMGenerator() = default;
  17. int cmCPackRPMGenerator::InitializeInternal()
  18. {
  19. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  20. if (cmIsOff(this->GetOption("CPACK_SET_DESTDIR"))) {
  21. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  22. }
  23. /* Replace space in CPACK_PACKAGE_NAME in order to avoid
  24. * rpmbuild scream on unwanted space in filename issue
  25. * Moreover RPM file do not usually embed space in filename
  26. */
  27. if (this->GetOption("CPACK_PACKAGE_NAME")) {
  28. std::string packageName = this->GetOption("CPACK_PACKAGE_NAME");
  29. std::replace(packageName.begin(), packageName.end(), ' ', '-');
  30. this->SetOption("CPACK_PACKAGE_NAME", packageName.c_str());
  31. }
  32. /* same for CPACK_PACKAGE_FILE_NAME */
  33. if (this->GetOption("CPACK_PACKAGE_FILE_NAME")) {
  34. std::string packageName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  35. std::replace(packageName.begin(), packageName.end(), ' ', '-');
  36. this->SetOption("CPACK_PACKAGE_FILE_NAME", packageName.c_str());
  37. }
  38. return this->Superclass::InitializeInternal();
  39. }
  40. void cmCPackRPMGenerator::AddGeneratedPackageNames()
  41. {
  42. // add the generated packages to package file names list
  43. std::string fileNames(this->GetOption("GEN_CPACK_OUTPUT_FILES"));
  44. const char sep = ';';
  45. std::string::size_type pos1 = 0;
  46. std::string::size_type pos2 = fileNames.find(sep, pos1 + 1);
  47. while (pos2 != std::string::npos) {
  48. packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  49. pos1 = pos2 + 1;
  50. pos2 = fileNames.find(sep, pos1 + 1);
  51. }
  52. packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  53. }
  54. int cmCPackRPMGenerator::PackageOnePack(std::string const& initialToplevel,
  55. std::string const& packageName)
  56. {
  57. int retval = 1;
  58. // Begin the archive for this pack
  59. std::string localToplevel(initialToplevel);
  60. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  61. std::string outputFileName(
  62. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  63. packageName, true) +
  64. this->GetOutputExtension());
  65. localToplevel += "/" + packageName;
  66. /* replace the TEMP DIRECTORY with the component one */
  67. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  68. packageFileName += "/" + outputFileName;
  69. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  70. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  71. /* replace the TEMPORARY package file name */
  72. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  73. packageFileName.c_str());
  74. // Tell CPackRPM.cmake the name of the component NAME.
  75. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT", packageName.c_str());
  76. // Tell CPackRPM.cmake the path where the component is.
  77. std::string component_path = "/";
  78. component_path += packageName;
  79. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  80. component_path.c_str());
  81. if (!this->ReadListFile("Internal/CPack/CPackRPM.cmake")) {
  82. cmCPackLogger(cmCPackLog::LOG_ERROR,
  83. "Error while execution CPackRPM.cmake" << std::endl);
  84. retval = 0;
  85. }
  86. return retval;
  87. }
  88. int cmCPackRPMGenerator::PackageComponents(bool ignoreGroup)
  89. {
  90. int retval = 1;
  91. /* Reset package file name list it will be populated during the
  92. * component packaging run*/
  93. packageFileNames.clear();
  94. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  95. const char* mainComponent = this->GetOption("CPACK_RPM_MAIN_COMPONENT");
  96. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") &&
  97. !this->IsOn("CPACK_RPM_DEBUGINFO_PACKAGE")) {
  98. // check if we need to set CPACK_RPM_DEBUGINFO_PACKAGE because non of
  99. // the components is setting per component debuginfo package variable
  100. bool shouldSet = true;
  101. if (ignoreGroup) {
  102. std::map<std::string, cmCPackComponent>::iterator compIt;
  103. for (compIt = this->Components.begin(); compIt != this->Components.end();
  104. ++compIt) {
  105. std::string component(compIt->first);
  106. std::transform(component.begin(), component.end(), component.begin(),
  107. ::toupper);
  108. if (this->IsOn("CPACK_RPM_" + compIt->first + "_DEBUGINFO_PACKAGE") ||
  109. this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
  110. shouldSet = false;
  111. break;
  112. }
  113. }
  114. } else {
  115. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  116. for (compGIt = this->ComponentGroups.begin();
  117. compGIt != this->ComponentGroups.end(); ++compGIt) {
  118. std::string component(compGIt->first);
  119. std::transform(component.begin(), component.end(), component.begin(),
  120. ::toupper);
  121. if (this->IsOn("CPACK_RPM_" + compGIt->first + "_DEBUGINFO_PACKAGE") ||
  122. this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
  123. shouldSet = false;
  124. break;
  125. }
  126. }
  127. if (shouldSet) {
  128. std::map<std::string, cmCPackComponent>::iterator compIt;
  129. for (compIt = this->Components.begin();
  130. compIt != this->Components.end(); ++compIt) {
  131. // Does the component belong to a group?
  132. if (compIt->second.Group == nullptr) {
  133. std::string component(compIt->first);
  134. std::transform(component.begin(), component.end(),
  135. component.begin(), ::toupper);
  136. if (this->IsOn("CPACK_RPM_" + compIt->first +
  137. "_DEBUGINFO_PACKAGE") ||
  138. this->IsOn("CPACK_RPM_" + component + "_DEBUGINFO_PACKAGE")) {
  139. shouldSet = false;
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. if (shouldSet) {
  147. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  148. "Setting "
  149. << "CPACK_RPM_DEBUGINFO_PACKAGE because "
  150. << "CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE is set but "
  151. << " none of the "
  152. << "CPACK_RPM_<component>_DEBUGINFO_PACKAGE variables "
  153. << "are set." << std::endl);
  154. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  155. }
  156. }
  157. if (mainComponent) {
  158. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  159. this->SetOption("GENERATE_SPEC_PARTS", "ON");
  160. }
  161. std::string mainComponentUpper(mainComponent);
  162. std::transform(mainComponentUpper.begin(), mainComponentUpper.end(),
  163. mainComponentUpper.begin(), ::toupper);
  164. // The default behavior is to have one package by component group
  165. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  166. if (!ignoreGroup) {
  167. std::map<std::string, cmCPackComponentGroup>::iterator mainCompGIt =
  168. this->ComponentGroups.end();
  169. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  170. for (compGIt = this->ComponentGroups.begin();
  171. compGIt != this->ComponentGroups.end(); ++compGIt) {
  172. std::string component(compGIt->first);
  173. std::transform(component.begin(), component.end(), component.begin(),
  174. ::toupper);
  175. if (mainComponentUpper == component) {
  176. // main component will be handled last
  177. mainCompGIt = compGIt;
  178. continue;
  179. }
  180. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  181. "Packaging component group: " << compGIt->first
  182. << 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 == 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,
  203. "Component <"
  204. << compIt->second.Name
  205. << "> does not belong to any group, package it separately."
  206. << std::endl);
  207. retval &= PackageOnePack(initialTopLevel, compIt->first);
  208. }
  209. }
  210. if (retval) {
  211. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  212. if (mainCompGIt != this->ComponentGroups.end()) {
  213. retval &= PackageOnePack(initialTopLevel, mainCompGIt->first);
  214. } else if (mainCompIt != this->Components.end()) {
  215. retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
  216. } else {
  217. cmCPackLogger(cmCPackLog::LOG_ERROR,
  218. "CPACK_RPM_MAIN_COMPONENT set"
  219. << " to non existing component.\n");
  220. retval = 0;
  221. }
  222. }
  223. }
  224. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  225. // We build 1 package per component
  226. else {
  227. std::map<std::string, cmCPackComponent>::iterator mainCompIt =
  228. this->Components.end();
  229. std::map<std::string, cmCPackComponent>::iterator compIt;
  230. for (compIt = this->Components.begin(); compIt != this->Components.end();
  231. ++compIt) {
  232. std::string component(compIt->first);
  233. std::transform(component.begin(), component.end(), component.begin(),
  234. ::toupper);
  235. if (mainComponentUpper == component) {
  236. // main component will be handled last
  237. mainCompIt = compIt;
  238. continue;
  239. }
  240. retval &= PackageOnePack(initialTopLevel, compIt->first);
  241. }
  242. if (retval) {
  243. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  244. if (mainCompIt != this->Components.end()) {
  245. retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
  246. } else {
  247. cmCPackLogger(cmCPackLog::LOG_ERROR,
  248. "CPACK_RPM_MAIN_COMPONENT set"
  249. << " to non existing component.\n");
  250. retval = 0;
  251. }
  252. }
  253. }
  254. } else if (!this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") ||
  255. this->Components.size() == 1) {
  256. // The default behavior is to have one package by component group
  257. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  258. if (!ignoreGroup) {
  259. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  260. for (compGIt = this->ComponentGroups.begin();
  261. compGIt != this->ComponentGroups.end(); ++compGIt) {
  262. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  263. "Packaging component group: " << compGIt->first
  264. << std::endl);
  265. retval &= PackageOnePack(initialTopLevel, compGIt->first);
  266. }
  267. // Handle Orphan components (components not belonging to any groups)
  268. std::map<std::string, cmCPackComponent>::iterator compIt;
  269. for (compIt = this->Components.begin(); compIt != this->Components.end();
  270. ++compIt) {
  271. // Does the component belong to a group?
  272. if (compIt->second.Group == nullptr) {
  273. cmCPackLogger(
  274. cmCPackLog::LOG_VERBOSE,
  275. "Component <"
  276. << compIt->second.Name
  277. << "> does not belong to any group, package it separately."
  278. << std::endl);
  279. retval &= PackageOnePack(initialTopLevel, compIt->first);
  280. }
  281. }
  282. }
  283. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  284. // We build 1 package per component
  285. else {
  286. std::map<std::string, cmCPackComponent>::iterator compIt;
  287. for (compIt = this->Components.begin(); compIt != this->Components.end();
  288. ++compIt) {
  289. retval &= PackageOnePack(initialTopLevel, compIt->first);
  290. }
  291. }
  292. } else {
  293. cmCPackLogger(
  294. cmCPackLog::LOG_ERROR,
  295. "CPACK_RPM_MAIN_COMPONENT not set but"
  296. << " it is mandatory with CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE"
  297. << " being set.\n");
  298. retval = 0;
  299. }
  300. if (retval) {
  301. AddGeneratedPackageNames();
  302. }
  303. return retval;
  304. }
  305. int cmCPackRPMGenerator::PackageComponentsAllInOne(
  306. const std::string& compInstDirName)
  307. {
  308. int retval = 1;
  309. /* Reset package file name list it will be populated during the
  310. * component packaging run*/
  311. packageFileNames.clear();
  312. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  313. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  314. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  315. }
  316. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  317. "Packaging all groups in one package..."
  318. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  319. << std::endl);
  320. // The ALL GROUPS in ONE package case
  321. std::string localToplevel(initialTopLevel);
  322. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  323. std::string outputFileName(
  324. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  325. this->GetOutputExtension());
  326. // all GROUP in one vs all COMPONENT in one
  327. localToplevel += "/" + compInstDirName;
  328. /* replace the TEMP DIRECTORY with the component one */
  329. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  330. packageFileName += "/" + outputFileName;
  331. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  332. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  333. /* replace the TEMPORARY package file name */
  334. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  335. packageFileName.c_str());
  336. if (!compInstDirName.empty()) {
  337. // Tell CPackRPM.cmake the path where the component is.
  338. std::string component_path = "/";
  339. component_path += compInstDirName;
  340. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  341. component_path.c_str());
  342. }
  343. if (this->ReadListFile("Internal/CPack/CPackRPM.cmake")) {
  344. AddGeneratedPackageNames();
  345. } else {
  346. cmCPackLogger(cmCPackLog::LOG_ERROR,
  347. "Error while execution CPackRPM.cmake" << std::endl);
  348. retval = 0;
  349. }
  350. return retval;
  351. }
  352. int cmCPackRPMGenerator::PackageFiles()
  353. {
  354. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
  355. /* Are we in the component packaging case */
  356. if (WantsComponentInstallation()) {
  357. // CASE 1 : COMPONENT ALL-IN-ONE package
  358. // If ALL COMPONENTS in ONE package has been requested
  359. // then the package file is unique and should be open here.
  360. if (componentPackageMethod == ONE_PACKAGE) {
  361. return PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  362. }
  363. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  364. // There will be 1 package for each component group
  365. // however one may require to ignore component group and
  366. // in this case you'll get 1 package for each component.
  367. return PackageComponents(componentPackageMethod ==
  368. ONE_PACKAGE_PER_COMPONENT);
  369. }
  370. // CASE 3 : NON COMPONENT package.
  371. return PackageComponentsAllInOne("");
  372. }
  373. bool cmCPackRPMGenerator::SupportsComponentInstallation() const
  374. {
  375. return IsOn("CPACK_RPM_COMPONENT_INSTALL");
  376. }
  377. std::string cmCPackRPMGenerator::GetComponentInstallDirNameSuffix(
  378. const std::string& componentName)
  379. {
  380. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  381. return componentName;
  382. }
  383. if (componentPackageMethod == ONE_PACKAGE) {
  384. return std::string("ALL_COMPONENTS_IN_ONE");
  385. }
  386. // We have to find the name of the COMPONENT GROUP
  387. // the current COMPONENT belongs to.
  388. std::string groupVar =
  389. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  390. if (nullptr != GetOption(groupVar)) {
  391. return std::string(GetOption(groupVar));
  392. }
  393. return componentName;
  394. }