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 <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 "cmSystemTools.h"
  14. cmCPackRPMGenerator::cmCPackRPMGenerator() = default;
  15. cmCPackRPMGenerator::~cmCPackRPMGenerator() = default;
  16. int cmCPackRPMGenerator::InitializeInternal()
  17. {
  18. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  19. if (cmSystemTools::IsOff(this->GetOption("CPACK_SET_DESTDIR"))) {
  20. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  21. }
  22. /* Replace space in CPACK_PACKAGE_NAME in order to avoid
  23. * rpmbuild scream on unwanted space in filename issue
  24. * Moreover RPM file do not usually embed space in filename
  25. */
  26. if (this->GetOption("CPACK_PACKAGE_NAME")) {
  27. std::string packageName = this->GetOption("CPACK_PACKAGE_NAME");
  28. std::replace(packageName.begin(), packageName.end(), ' ', '-');
  29. this->SetOption("CPACK_PACKAGE_NAME", packageName.c_str());
  30. }
  31. /* same for CPACK_PACKAGE_FILE_NAME */
  32. if (this->GetOption("CPACK_PACKAGE_FILE_NAME")) {
  33. std::string packageName = this->GetOption("CPACK_PACKAGE_FILE_NAME");
  34. std::replace(packageName.begin(), packageName.end(), ' ', '-');
  35. this->SetOption("CPACK_PACKAGE_FILE_NAME", packageName.c_str());
  36. }
  37. return this->Superclass::InitializeInternal();
  38. }
  39. void cmCPackRPMGenerator::AddGeneratedPackageNames()
  40. {
  41. // add the generated packages to package file names list
  42. std::string fileNames(this->GetOption("GEN_CPACK_OUTPUT_FILES"));
  43. const char sep = ';';
  44. std::string::size_type pos1 = 0;
  45. std::string::size_type pos2 = fileNames.find(sep, pos1 + 1);
  46. while (pos2 != std::string::npos) {
  47. packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  48. pos1 = pos2 + 1;
  49. pos2 = fileNames.find(sep, pos1 + 1);
  50. }
  51. packageFileNames.push_back(fileNames.substr(pos1, pos2 - pos1));
  52. }
  53. int cmCPackRPMGenerator::PackageOnePack(std::string const& initialToplevel,
  54. std::string const& packageName)
  55. {
  56. int retval = 1;
  57. // Begin the archive for this pack
  58. std::string localToplevel(initialToplevel);
  59. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  60. std::string outputFileName(
  61. GetComponentPackageFileName(this->GetOption("CPACK_PACKAGE_FILE_NAME"),
  62. packageName, true) +
  63. this->GetOutputExtension());
  64. localToplevel += "/" + packageName;
  65. /* replace the TEMP DIRECTORY with the component one */
  66. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  67. packageFileName += "/" + outputFileName;
  68. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  69. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  70. /* replace the TEMPORARY package file name */
  71. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  72. packageFileName.c_str());
  73. // Tell CPackRPM.cmake the name of the component NAME.
  74. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT", packageName.c_str());
  75. // Tell CPackRPM.cmake the path where the component is.
  76. std::string component_path = "/";
  77. component_path += 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. std::map<std::string, cmCPackComponentGroup>::iterator mainCompGIt =
  167. 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 &= PackageOnePack(initialTopLevel, compGIt->first);
  183. }
  184. // Handle Orphan components (components not belonging to any groups)
  185. std::map<std::string, cmCPackComponent>::iterator mainCompIt =
  186. 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 &= PackageOnePack(initialTopLevel, compIt->first);
  207. }
  208. }
  209. if (retval) {
  210. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  211. if (mainCompGIt != this->ComponentGroups.end()) {
  212. retval &= PackageOnePack(initialTopLevel, mainCompGIt->first);
  213. } else if (mainCompIt != this->Components.end()) {
  214. retval &= 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. std::map<std::string, cmCPackComponent>::iterator mainCompIt =
  227. this->Components.end();
  228. std::map<std::string, cmCPackComponent>::iterator compIt;
  229. for (compIt = this->Components.begin(); compIt != this->Components.end();
  230. ++compIt) {
  231. std::string component(compIt->first);
  232. std::transform(component.begin(), component.end(), component.begin(),
  233. ::toupper);
  234. if (mainComponentUpper == component) {
  235. // main component will be handled last
  236. mainCompIt = compIt;
  237. continue;
  238. }
  239. retval &= PackageOnePack(initialTopLevel, compIt->first);
  240. }
  241. if (retval) {
  242. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  243. if (mainCompIt != this->Components.end()) {
  244. retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
  245. } else {
  246. cmCPackLogger(cmCPackLog::LOG_ERROR,
  247. "CPACK_RPM_MAIN_COMPONENT set"
  248. << " to non existing component.\n");
  249. retval = 0;
  250. }
  251. }
  252. }
  253. } else if (!this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") ||
  254. this->Components.size() == 1) {
  255. // The default behavior is to have one package by component group
  256. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  257. if (!ignoreGroup) {
  258. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  259. for (compGIt = this->ComponentGroups.begin();
  260. compGIt != this->ComponentGroups.end(); ++compGIt) {
  261. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  262. "Packaging component group: " << compGIt->first
  263. << std::endl);
  264. retval &= PackageOnePack(initialTopLevel, compGIt->first);
  265. }
  266. // Handle Orphan components (components not belonging to any groups)
  267. std::map<std::string, cmCPackComponent>::iterator compIt;
  268. for (compIt = this->Components.begin(); compIt != this->Components.end();
  269. ++compIt) {
  270. // Does the component belong to a group?
  271. if (compIt->second.Group == nullptr) {
  272. cmCPackLogger(
  273. cmCPackLog::LOG_VERBOSE,
  274. "Component <"
  275. << compIt->second.Name
  276. << "> does not belong to any group, package it separately."
  277. << std::endl);
  278. retval &= PackageOnePack(initialTopLevel, compIt->first);
  279. }
  280. }
  281. }
  282. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  283. // We build 1 package per component
  284. else {
  285. std::map<std::string, cmCPackComponent>::iterator compIt;
  286. for (compIt = this->Components.begin(); compIt != this->Components.end();
  287. ++compIt) {
  288. retval &= PackageOnePack(initialTopLevel, compIt->first);
  289. }
  290. }
  291. } else {
  292. cmCPackLogger(
  293. cmCPackLog::LOG_ERROR,
  294. "CPACK_RPM_MAIN_COMPONENT not set but"
  295. << " it is mandatory with CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE"
  296. << " being set.\n");
  297. retval = 0;
  298. }
  299. if (retval) {
  300. AddGeneratedPackageNames();
  301. }
  302. return retval;
  303. }
  304. int cmCPackRPMGenerator::PackageComponentsAllInOne(
  305. const std::string& compInstDirName)
  306. {
  307. int retval = 1;
  308. /* Reset package file name list it will be populated during the
  309. * component packaging run*/
  310. packageFileNames.clear();
  311. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  312. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  313. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  314. }
  315. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  316. "Packaging all groups in one package..."
  317. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  318. << std::endl);
  319. // The ALL GROUPS in ONE package case
  320. std::string localToplevel(initialTopLevel);
  321. std::string packageFileName(cmSystemTools::GetParentDirectory(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 = "/";
  338. component_path += compInstDirName;
  339. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  340. component_path.c_str());
  341. }
  342. if (this->ReadListFile("Internal/CPack/CPackRPM.cmake")) {
  343. AddGeneratedPackageNames();
  344. } else {
  345. cmCPackLogger(cmCPackLog::LOG_ERROR,
  346. "Error while execution CPackRPM.cmake" << std::endl);
  347. retval = 0;
  348. }
  349. return retval;
  350. }
  351. int cmCPackRPMGenerator::PackageFiles()
  352. {
  353. cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
  354. /* Are we in the component packaging case */
  355. if (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 (componentPackageMethod == ONE_PACKAGE) {
  360. return 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 PackageComponents(componentPackageMethod ==
  367. ONE_PACKAGE_PER_COMPONENT);
  368. }
  369. // CASE 3 : NON COMPONENT package.
  370. return PackageComponentsAllInOne("");
  371. }
  372. bool cmCPackRPMGenerator::SupportsComponentInstallation() const
  373. {
  374. return IsOn("CPACK_RPM_COMPONENT_INSTALL");
  375. }
  376. std::string cmCPackRPMGenerator::GetComponentInstallDirNameSuffix(
  377. const std::string& componentName)
  378. {
  379. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  380. return componentName;
  381. }
  382. if (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 != GetOption(groupVar)) {
  390. return std::string(GetOption(groupVar));
  391. }
  392. return componentName;
  393. }