cmCPackRPMGenerator.cxx 17 KB

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