cmCPackRPMGenerator.cxx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 "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. this->packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  49. pos1 = pos2 + 1;
  50. pos2 = fileNames.find(sep, pos1 + 1);
  51. }
  52. this->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(
  61. cmSystemTools::GetParentDirectory(this->toplevel));
  62. std::string outputFileName(
  63. this->GetComponentPackageFileName(
  64. this->GetOption("CPACK_PACKAGE_FILE_NAME"), packageName, true) +
  65. this->GetOutputExtension());
  66. localToplevel += "/" + packageName;
  67. /* replace the TEMP DIRECTORY with the component one */
  68. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  69. packageFileName += "/" + outputFileName;
  70. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  71. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  72. /* replace the TEMPORARY package file name */
  73. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  74. packageFileName.c_str());
  75. // Tell CPackRPM.cmake the name of the component NAME.
  76. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT", packageName.c_str());
  77. // Tell CPackRPM.cmake the path where the component is.
  78. std::string component_path = cmStrCat('/', 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. this->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. auto mainCompGIt = this->ComponentGroups.end();
  168. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  169. for (compGIt = this->ComponentGroups.begin();
  170. compGIt != this->ComponentGroups.end(); ++compGIt) {
  171. std::string component(compGIt->first);
  172. std::transform(component.begin(), component.end(), component.begin(),
  173. ::toupper);
  174. if (mainComponentUpper == component) {
  175. // main component will be handled last
  176. mainCompGIt = compGIt;
  177. continue;
  178. }
  179. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  180. "Packaging component group: " << compGIt->first
  181. << std::endl);
  182. retval &= this->PackageOnePack(initialTopLevel, compGIt->first);
  183. }
  184. // Handle Orphan components (components not belonging to any groups)
  185. auto mainCompIt = this->Components.end();
  186. std::map<std::string, cmCPackComponent>::iterator compIt;
  187. for (compIt = this->Components.begin(); compIt != this->Components.end();
  188. ++compIt) {
  189. // Does the component belong to a group?
  190. if (compIt->second.Group == nullptr) {
  191. std::string component(compIt->first);
  192. std::transform(component.begin(), component.end(), component.begin(),
  193. ::toupper);
  194. if (mainComponentUpper == component) {
  195. // main component will be handled last
  196. mainCompIt = compIt;
  197. continue;
  198. }
  199. cmCPackLogger(
  200. cmCPackLog::LOG_VERBOSE,
  201. "Component <"
  202. << compIt->second.Name
  203. << "> does not belong to any group, package it separately."
  204. << std::endl);
  205. retval &= this->PackageOnePack(initialTopLevel, compIt->first);
  206. }
  207. }
  208. if (retval) {
  209. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  210. if (mainCompGIt != this->ComponentGroups.end()) {
  211. retval &= this->PackageOnePack(initialTopLevel, mainCompGIt->first);
  212. } else if (mainCompIt != this->Components.end()) {
  213. retval &= this->PackageOnePack(initialTopLevel, mainCompIt->first);
  214. } else {
  215. cmCPackLogger(cmCPackLog::LOG_ERROR,
  216. "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. auto mainCompIt = this->Components.end();
  226. std::map<std::string, cmCPackComponent>::iterator compIt;
  227. for (compIt = this->Components.begin(); compIt != this->Components.end();
  228. ++compIt) {
  229. std::string component(compIt->first);
  230. std::transform(component.begin(), component.end(), component.begin(),
  231. ::toupper);
  232. if (mainComponentUpper == component) {
  233. // main component will be handled last
  234. mainCompIt = compIt;
  235. continue;
  236. }
  237. retval &= this->PackageOnePack(initialTopLevel, compIt->first);
  238. }
  239. if (retval) {
  240. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  241. if (mainCompIt != this->Components.end()) {
  242. retval &= this->PackageOnePack(initialTopLevel, mainCompIt->first);
  243. } else {
  244. cmCPackLogger(cmCPackLog::LOG_ERROR,
  245. "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,
  260. "Packaging component group: " << compGIt->first
  261. << std::endl);
  262. retval &= this->PackageOnePack(initialTopLevel, compGIt->first);
  263. }
  264. // Handle Orphan components (components not belonging to any groups)
  265. std::map<std::string, cmCPackComponent>::iterator compIt;
  266. for (compIt = this->Components.begin(); compIt != this->Components.end();
  267. ++compIt) {
  268. // Does the component belong to a group?
  269. if (compIt->second.Group == nullptr) {
  270. cmCPackLogger(
  271. cmCPackLog::LOG_VERBOSE,
  272. "Component <"
  273. << compIt->second.Name
  274. << "> does not belong to any group, package it separately."
  275. << std::endl);
  276. retval &= this->PackageOnePack(initialTopLevel, compIt->first);
  277. }
  278. }
  279. }
  280. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  281. // We build 1 package per component
  282. else {
  283. std::map<std::string, cmCPackComponent>::iterator compIt;
  284. for (compIt = this->Components.begin(); compIt != this->Components.end();
  285. ++compIt) {
  286. retval &= this->PackageOnePack(initialTopLevel, compIt->first);
  287. }
  288. }
  289. } else {
  290. cmCPackLogger(
  291. cmCPackLog::LOG_ERROR,
  292. "CPACK_RPM_MAIN_COMPONENT not set but"
  293. << " it is mandatory with CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE"
  294. << " being set.\n");
  295. retval = 0;
  296. }
  297. if (retval) {
  298. this->AddGeneratedPackageNames();
  299. }
  300. return retval;
  301. }
  302. int cmCPackRPMGenerator::PackageComponentsAllInOne(
  303. const std::string& compInstDirName)
  304. {
  305. int retval = 1;
  306. /* Reset package file name list it will be populated during the
  307. * component packaging run*/
  308. this->packageFileNames.clear();
  309. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  310. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  311. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  312. }
  313. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  314. "Packaging all groups in one package..."
  315. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  316. << std::endl);
  317. // The ALL GROUPS in ONE package case
  318. std::string localToplevel(initialTopLevel);
  319. std::string packageFileName(
  320. cmSystemTools::GetParentDirectory(this->toplevel));
  321. std::string outputFileName(
  322. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  323. this->GetOutputExtension());
  324. // all GROUP in one vs all COMPONENT in one
  325. localToplevel += "/" + compInstDirName;
  326. /* replace the TEMP DIRECTORY with the component one */
  327. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  328. packageFileName += "/" + outputFileName;
  329. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  330. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  331. /* replace the TEMPORARY package file name */
  332. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  333. packageFileName.c_str());
  334. if (!compInstDirName.empty()) {
  335. // Tell CPackRPM.cmake the path where the component is.
  336. std::string component_path = cmStrCat('/', compInstDirName);
  337. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  338. component_path.c_str());
  339. }
  340. if (this->ReadListFile("Internal/CPack/CPackRPM.cmake")) {
  341. this->AddGeneratedPackageNames();
  342. } else {
  343. cmCPackLogger(cmCPackLog::LOG_ERROR,
  344. "Error while execution CPackRPM.cmake" << std::endl);
  345. retval = 0;
  346. }
  347. return retval;
  348. }
  349. int cmCPackRPMGenerator::PackageFiles()
  350. {
  351. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  352. "Toplevel: " << this->toplevel << std::endl);
  353. /* Are we in the component packaging case */
  354. if (this->WantsComponentInstallation()) {
  355. // CASE 1 : COMPONENT ALL-IN-ONE package
  356. // If ALL COMPONENTS in ONE package has been requested
  357. // then the package file is unique and should be open here.
  358. if (this->componentPackageMethod == ONE_PACKAGE) {
  359. return this->PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  360. }
  361. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  362. // There will be 1 package for each component group
  363. // however one may require to ignore component group and
  364. // in this case you'll get 1 package for each component.
  365. return this->PackageComponents(this->componentPackageMethod ==
  366. ONE_PACKAGE_PER_COMPONENT);
  367. }
  368. // CASE 3 : NON COMPONENT package.
  369. return this->PackageComponentsAllInOne("");
  370. }
  371. bool cmCPackRPMGenerator::SupportsComponentInstallation() const
  372. {
  373. return this->IsOn("CPACK_RPM_COMPONENT_INSTALL");
  374. }
  375. std::string cmCPackRPMGenerator::GetComponentInstallDirNameSuffix(
  376. const std::string& componentName)
  377. {
  378. if (this->componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  379. return componentName;
  380. }
  381. if (this->componentPackageMethod == ONE_PACKAGE) {
  382. return std::string("ALL_COMPONENTS_IN_ONE");
  383. }
  384. // We have to find the name of the COMPONENT GROUP
  385. // the current COMPONENT belongs to.
  386. std::string groupVar =
  387. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  388. if (nullptr != this->GetOption(groupVar)) {
  389. return std::string(this->GetOption(groupVar));
  390. }
  391. return componentName;
  392. }