cmCPackRPMGenerator.cxx 16 KB

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