cmCPackRPMGenerator.cxx 16 KB

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