cmCPackRPMGenerator.cxx 16 KB

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