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 "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("CPackRPM.cmake")) {
  85. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackRPM.cmake"
  86. << 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, "Setting "
  151. << "CPACK_RPM_DEBUGINFO_PACKAGE because "
  152. << "CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE is set but "
  153. << " none of the "
  154. << "CPACK_RPM_<component>_DEBUGINFO_PACKAGE variables "
  155. << "are set." << std::endl);
  156. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  157. }
  158. }
  159. if (mainComponent) {
  160. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  161. this->SetOption("GENERATE_SPEC_PARTS", "ON");
  162. }
  163. std::string mainComponentUpper(mainComponent);
  164. std::transform(mainComponentUpper.begin(), mainComponentUpper.end(),
  165. mainComponentUpper.begin(), ::toupper);
  166. // The default behavior is to have one package by component group
  167. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  168. if (!ignoreGroup) {
  169. std::map<std::string, cmCPackComponentGroup>::iterator mainCompGIt =
  170. this->ComponentGroups.end();
  171. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  172. for (compGIt = this->ComponentGroups.begin();
  173. compGIt != this->ComponentGroups.end(); ++compGIt) {
  174. std::string component(compGIt->first);
  175. std::transform(component.begin(), component.end(), component.begin(),
  176. ::toupper);
  177. if (mainComponentUpper == component) {
  178. // main component will be handled last
  179. mainCompGIt = compGIt;
  180. continue;
  181. }
  182. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  183. << compGIt->first << std::endl);
  184. retval &= PackageOnePack(initialTopLevel, compGIt->first);
  185. }
  186. // Handle Orphan components (components not belonging to any groups)
  187. std::map<std::string, cmCPackComponent>::iterator mainCompIt =
  188. this->Components.end();
  189. std::map<std::string, cmCPackComponent>::iterator compIt;
  190. for (compIt = this->Components.begin(); compIt != this->Components.end();
  191. ++compIt) {
  192. // Does the component belong to a group?
  193. if (compIt->second.Group == nullptr) {
  194. std::string component(compIt->first);
  195. std::transform(component.begin(), component.end(), component.begin(),
  196. ::toupper);
  197. if (mainComponentUpper == component) {
  198. // main component will be handled last
  199. mainCompIt = compIt;
  200. continue;
  201. }
  202. cmCPackLogger(
  203. cmCPackLog::LOG_VERBOSE, "Component <"
  204. << compIt->second.Name
  205. << "> does not belong to any group, package it separately."
  206. << std::endl);
  207. retval &= PackageOnePack(initialTopLevel, compIt->first);
  208. }
  209. }
  210. if (retval) {
  211. this->SetOption("GENERATE_SPEC_PARTS", "OFF");
  212. if (mainCompGIt != this->ComponentGroups.end()) {
  213. retval &= PackageOnePack(initialTopLevel, mainCompGIt->first);
  214. } else if (mainCompIt != this->Components.end()) {
  215. retval &= PackageOnePack(initialTopLevel, mainCompIt->first);
  216. } else {
  217. cmCPackLogger(cmCPackLog::LOG_ERROR, "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, "CPACK_RPM_MAIN_COMPONENT set"
  247. << " to non existing component.\n");
  248. retval = 0;
  249. }
  250. }
  251. }
  252. } else if (!this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE") ||
  253. this->Components.size() == 1) {
  254. // The default behavior is to have one package by component group
  255. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  256. if (!ignoreGroup) {
  257. std::map<std::string, cmCPackComponentGroup>::iterator compGIt;
  258. for (compGIt = this->ComponentGroups.begin();
  259. compGIt != this->ComponentGroups.end(); ++compGIt) {
  260. cmCPackLogger(cmCPackLog::LOG_VERBOSE, "Packaging component group: "
  261. << compGIt->first << std::endl);
  262. retval &= 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, "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, "CPACK_RPM_MAIN_COMPONENT not set but"
  291. << " it is mandatory with CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE"
  292. << " being set.\n");
  293. retval = 0;
  294. }
  295. if (retval) {
  296. AddGeneratedPackageNames();
  297. }
  298. return retval;
  299. }
  300. int cmCPackRPMGenerator::PackageComponentsAllInOne(
  301. const std::string& compInstDirName)
  302. {
  303. int retval = 1;
  304. /* Reset package file name list it will be populated during the
  305. * component packaging run*/
  306. packageFileNames.clear();
  307. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  308. if (this->IsOn("CPACK_RPM_DEBUGINFO_SINGLE_PACKAGE")) {
  309. this->SetOption("CPACK_RPM_DEBUGINFO_PACKAGE", "ON");
  310. }
  311. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  312. "Packaging all groups in one package..."
  313. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  314. << std::endl);
  315. // The ALL GROUPS in ONE package case
  316. std::string localToplevel(initialTopLevel);
  317. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  318. std::string outputFileName(
  319. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  320. this->GetOutputExtension());
  321. // all GROUP in one vs all COMPONENT in one
  322. localToplevel += "/" + compInstDirName;
  323. /* replace the TEMP DIRECTORY with the component one */
  324. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  325. packageFileName += "/" + outputFileName;
  326. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  327. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  328. /* replace the TEMPORARY package file name */
  329. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  330. packageFileName.c_str());
  331. if (!compInstDirName.empty()) {
  332. // Tell CPackRPM.cmake the path where the component is.
  333. std::string component_path = "/";
  334. component_path += compInstDirName;
  335. this->SetOption("CPACK_RPM_PACKAGE_COMPONENT_PART_PATH",
  336. component_path.c_str());
  337. }
  338. if (this->ReadListFile("CPackRPM.cmake")) {
  339. AddGeneratedPackageNames();
  340. } else {
  341. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackRPM.cmake"
  342. << 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. }