cmCPackDebGenerator.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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 "cmCPackDebGenerator.h"
  4. #include "cmArchiveWrite.h"
  5. #include "cmCPackComponentGroup.h"
  6. #include "cmCPackGenerator.h"
  7. #include "cmCPackLog.h"
  8. #include "cmCryptoHash.h"
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmSystemTools.h"
  11. #include "cm_sys_stat.h"
  12. #include "cmsys/Glob.hxx"
  13. #include <ostream>
  14. #include <set>
  15. #include <string.h>
  16. #include <utility>
  17. cmCPackDebGenerator::cmCPackDebGenerator()
  18. {
  19. }
  20. cmCPackDebGenerator::~cmCPackDebGenerator()
  21. {
  22. }
  23. int cmCPackDebGenerator::InitializeInternal()
  24. {
  25. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  26. if (cmSystemTools::IsOff(this->GetOption("CPACK_SET_DESTDIR"))) {
  27. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  28. }
  29. return this->Superclass::InitializeInternal();
  30. }
  31. int cmCPackDebGenerator::PackageOnePack(std::string const& initialTopLevel,
  32. std::string const& packageName)
  33. {
  34. int retval = 1;
  35. // Begin the archive for this pack
  36. std::string localToplevel(initialTopLevel);
  37. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  38. std::string outputFileName(
  39. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) + "-" +
  40. packageName + this->GetOutputExtension());
  41. localToplevel += "/" + packageName;
  42. /* replace the TEMP DIRECTORY with the component one */
  43. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  44. packageFileName += "/" + outputFileName;
  45. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  46. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  47. /* replace the TEMPORARY package file name */
  48. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  49. packageFileName.c_str());
  50. // Tell CPackDeb.cmake the name of the component GROUP.
  51. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT", packageName.c_str());
  52. // Tell CPackDeb.cmake the path where the component is.
  53. std::string component_path = "/";
  54. component_path += packageName;
  55. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  56. component_path.c_str());
  57. if (!this->ReadListFile("CPackDeb.cmake")) {
  58. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackDeb.cmake"
  59. << std::endl);
  60. retval = 0;
  61. return retval;
  62. }
  63. cmsys::Glob gl;
  64. std::string findExpr(this->GetOption("GEN_WDIR"));
  65. findExpr += "/*";
  66. gl.RecurseOn();
  67. gl.SetRecurseListDirs(true);
  68. if (!gl.FindFiles(findExpr)) {
  69. cmCPackLogger(cmCPackLog::LOG_ERROR,
  70. "Cannot find any files in the installed directory"
  71. << std::endl);
  72. return 0;
  73. }
  74. packageFiles = gl.GetFiles();
  75. int res = createDeb();
  76. if (res != 1) {
  77. retval = 0;
  78. }
  79. // add the generated package to package file names list
  80. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  81. packageFileName += "/";
  82. packageFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  83. packageFileNames.push_back(std::move(packageFileName));
  84. return retval;
  85. }
  86. int cmCPackDebGenerator::PackageComponents(bool ignoreGroup)
  87. {
  88. int retval = 1;
  89. /* Reset package file name list it will be populated during the
  90. * component packaging run*/
  91. packageFileNames.clear();
  92. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  93. // The default behavior is to have one package by component group
  94. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  95. if (!ignoreGroup) {
  96. for (auto const& compG : this->ComponentGroups) {
  97. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  98. "Packaging component group: " << compG.first << std::endl);
  99. // Begin the archive for this group
  100. retval &= PackageOnePack(initialTopLevel, compG.first);
  101. }
  102. // Handle Orphan components (components not belonging to any groups)
  103. for (auto const& comp : this->Components) {
  104. // Does the component belong to a group?
  105. if (comp.second.Group == nullptr) {
  106. cmCPackLogger(
  107. cmCPackLog::LOG_VERBOSE, "Component <"
  108. << comp.second.Name
  109. << "> does not belong to any group, package it separately."
  110. << std::endl);
  111. // Begin the archive for this orphan component
  112. retval &= PackageOnePack(initialTopLevel, comp.first);
  113. }
  114. }
  115. }
  116. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  117. // We build 1 package per component
  118. else {
  119. for (auto const& comp : this->Components) {
  120. retval &= PackageOnePack(initialTopLevel, comp.first);
  121. }
  122. }
  123. return retval;
  124. }
  125. //----------------------------------------------------------------------
  126. int cmCPackDebGenerator::PackageComponentsAllInOne(
  127. const std::string& compInstDirName)
  128. {
  129. int retval = 1;
  130. /* Reset package file name list it will be populated during the
  131. * component packaging run*/
  132. packageFileNames.clear();
  133. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  134. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  135. "Packaging all groups in one package..."
  136. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  137. << std::endl);
  138. // The ALL GROUPS in ONE package case
  139. std::string localToplevel(initialTopLevel);
  140. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  141. std::string outputFileName(
  142. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  143. this->GetOutputExtension());
  144. // all GROUP in one vs all COMPONENT in one
  145. // if must be here otherwise non component paths have a trailing / while
  146. // components don't
  147. if (!compInstDirName.empty()) {
  148. localToplevel += "/" + compInstDirName;
  149. }
  150. /* replace the TEMP DIRECTORY with the component one */
  151. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  152. packageFileName += "/" + outputFileName;
  153. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  154. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  155. /* replace the TEMPORARY package file name */
  156. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  157. packageFileName.c_str());
  158. if (!compInstDirName.empty()) {
  159. // Tell CPackDeb.cmake the path where the component is.
  160. std::string component_path = "/";
  161. component_path += compInstDirName;
  162. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  163. component_path.c_str());
  164. }
  165. if (!this->ReadListFile("CPackDeb.cmake")) {
  166. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error while execution CPackDeb.cmake"
  167. << std::endl);
  168. retval = 0;
  169. return retval;
  170. }
  171. cmsys::Glob gl;
  172. std::string findExpr(this->GetOption("GEN_WDIR"));
  173. findExpr += "/*";
  174. gl.RecurseOn();
  175. gl.SetRecurseListDirs(true);
  176. if (!gl.FindFiles(findExpr)) {
  177. cmCPackLogger(cmCPackLog::LOG_ERROR,
  178. "Cannot find any files in the installed directory"
  179. << std::endl);
  180. return 0;
  181. }
  182. packageFiles = gl.GetFiles();
  183. int res = createDeb();
  184. if (res != 1) {
  185. retval = 0;
  186. }
  187. // add the generated package to package file names list
  188. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  189. packageFileName += "/";
  190. packageFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  191. packageFileNames.push_back(std::move(packageFileName));
  192. return retval;
  193. }
  194. int cmCPackDebGenerator::PackageFiles()
  195. {
  196. /* Are we in the component packaging case */
  197. if (WantsComponentInstallation()) {
  198. // CASE 1 : COMPONENT ALL-IN-ONE package
  199. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  200. // then the package file is unique and should be open here.
  201. if (componentPackageMethod == ONE_PACKAGE) {
  202. return PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  203. }
  204. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  205. // There will be 1 package for each component group
  206. // however one may require to ignore component group and
  207. // in this case you'll get 1 package for each component.
  208. return PackageComponents(componentPackageMethod ==
  209. ONE_PACKAGE_PER_COMPONENT);
  210. }
  211. // CASE 3 : NON COMPONENT package.
  212. return PackageComponentsAllInOne("");
  213. }
  214. int cmCPackDebGenerator::createDeb()
  215. {
  216. // debian-binary file
  217. const std::string strGenWDIR(this->GetOption("GEN_WDIR"));
  218. const std::string dbfilename = strGenWDIR + "/debian-binary";
  219. { // the scope is needed for cmGeneratedFileStream
  220. cmGeneratedFileStream out(dbfilename.c_str());
  221. out << "2.0";
  222. out << std::endl; // required for valid debian package
  223. }
  224. // control file
  225. std::string ctlfilename = strGenWDIR + "/control";
  226. // debian policy enforce lower case for package name
  227. // mandatory entries:
  228. std::string debian_pkg_name = cmsys::SystemTools::LowerCase(
  229. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME"));
  230. const char* debian_pkg_version =
  231. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  232. const char* debian_pkg_section =
  233. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SECTION");
  234. const char* debian_pkg_priority =
  235. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PRIORITY");
  236. const char* debian_pkg_arch =
  237. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  238. const char* maintainer =
  239. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  240. const char* desc = this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  241. // optional entries
  242. const char* debian_pkg_dep =
  243. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DEPENDS");
  244. const char* debian_pkg_rec =
  245. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  246. const char* debian_pkg_sug =
  247. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SUGGESTS");
  248. const char* debian_pkg_url =
  249. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  250. const char* debian_pkg_predep =
  251. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PREDEPENDS");
  252. const char* debian_pkg_enhances =
  253. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ENHANCES");
  254. const char* debian_pkg_breaks =
  255. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_BREAKS");
  256. const char* debian_pkg_conflicts =
  257. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONFLICTS");
  258. const char* debian_pkg_provides =
  259. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PROVIDES");
  260. const char* debian_pkg_replaces =
  261. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_REPLACES");
  262. const char* debian_pkg_source =
  263. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  264. { // the scope is needed for cmGeneratedFileStream
  265. cmGeneratedFileStream out(ctlfilename.c_str());
  266. out << "Package: " << debian_pkg_name << "\n";
  267. out << "Version: " << debian_pkg_version << "\n";
  268. out << "Section: " << debian_pkg_section << "\n";
  269. out << "Priority: " << debian_pkg_priority << "\n";
  270. out << "Architecture: " << debian_pkg_arch << "\n";
  271. if (debian_pkg_source && *debian_pkg_source) {
  272. out << "Source: " << debian_pkg_source << "\n";
  273. }
  274. if (debian_pkg_dep && *debian_pkg_dep) {
  275. out << "Depends: " << debian_pkg_dep << "\n";
  276. }
  277. if (debian_pkg_rec && *debian_pkg_rec) {
  278. out << "Recommends: " << debian_pkg_rec << "\n";
  279. }
  280. if (debian_pkg_sug && *debian_pkg_sug) {
  281. out << "Suggests: " << debian_pkg_sug << "\n";
  282. }
  283. if (debian_pkg_url && *debian_pkg_url) {
  284. out << "Homepage: " << debian_pkg_url << "\n";
  285. }
  286. if (debian_pkg_predep && *debian_pkg_predep) {
  287. out << "Pre-Depends: " << debian_pkg_predep << "\n";
  288. }
  289. if (debian_pkg_enhances && *debian_pkg_enhances) {
  290. out << "Enhances: " << debian_pkg_enhances << "\n";
  291. }
  292. if (debian_pkg_breaks && *debian_pkg_breaks) {
  293. out << "Breaks: " << debian_pkg_breaks << "\n";
  294. }
  295. if (debian_pkg_conflicts && *debian_pkg_conflicts) {
  296. out << "Conflicts: " << debian_pkg_conflicts << "\n";
  297. }
  298. if (debian_pkg_provides && *debian_pkg_provides) {
  299. out << "Provides: " << debian_pkg_provides << "\n";
  300. }
  301. if (debian_pkg_replaces && *debian_pkg_replaces) {
  302. out << "Replaces: " << debian_pkg_replaces << "\n";
  303. }
  304. unsigned long totalSize = 0;
  305. {
  306. std::string dirName = this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  307. dirName += '/';
  308. for (std::string const& file : packageFiles) {
  309. totalSize += cmSystemTools::FileLength(file);
  310. }
  311. }
  312. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n";
  313. out << "Maintainer: " << maintainer << "\n";
  314. out << "Description: " << desc << "\n";
  315. out << std::endl;
  316. }
  317. const std::string shlibsfilename = strGenWDIR + "/shlibs";
  318. const char* debian_pkg_shlibs =
  319. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SHLIBS");
  320. const bool gen_shibs = this->IsOn("CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS") &&
  321. debian_pkg_shlibs && *debian_pkg_shlibs;
  322. if (gen_shibs) {
  323. cmGeneratedFileStream out(shlibsfilename.c_str());
  324. out << debian_pkg_shlibs;
  325. out << std::endl;
  326. }
  327. const std::string postinst = strGenWDIR + "/postinst";
  328. const std::string postrm = strGenWDIR + "/postrm";
  329. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST")) {
  330. cmGeneratedFileStream out(postinst.c_str());
  331. out << "#!/bin/sh\n\n"
  332. "set -e\n\n"
  333. "if [ \"$1\" = \"configure\" ]; then\n"
  334. "\tldconfig\n"
  335. "fi\n";
  336. }
  337. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM")) {
  338. cmGeneratedFileStream out(postrm.c_str());
  339. out << "#!/bin/sh\n\n"
  340. "set -e\n\n"
  341. "if [ \"$1\" = \"remove\" ]; then\n"
  342. "\tldconfig\n"
  343. "fi\n";
  344. }
  345. cmArchiveWrite::Compress tar_compression_type = cmArchiveWrite::CompressGZip;
  346. const char* debian_compression_type =
  347. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE");
  348. if (!debian_compression_type) {
  349. debian_compression_type = "gzip";
  350. }
  351. std::string compression_suffix;
  352. if (!strcmp(debian_compression_type, "lzma")) {
  353. compression_suffix = ".lzma";
  354. tar_compression_type = cmArchiveWrite::CompressLZMA;
  355. } else if (!strcmp(debian_compression_type, "xz")) {
  356. compression_suffix = ".xz";
  357. tar_compression_type = cmArchiveWrite::CompressXZ;
  358. } else if (!strcmp(debian_compression_type, "bzip2")) {
  359. compression_suffix = ".bz2";
  360. tar_compression_type = cmArchiveWrite::CompressBZip2;
  361. } else if (!strcmp(debian_compression_type, "gzip")) {
  362. compression_suffix = ".gz";
  363. tar_compression_type = cmArchiveWrite::CompressGZip;
  364. } else if (!strcmp(debian_compression_type, "none")) {
  365. compression_suffix.clear();
  366. tar_compression_type = cmArchiveWrite::CompressNone;
  367. } else {
  368. cmCPackLogger(cmCPackLog::LOG_ERROR,
  369. "Error unrecognized compression type: "
  370. << debian_compression_type << std::endl);
  371. }
  372. const char* debian_archive_type =
  373. this->GetOption("GEN_CPACK_DEBIAN_ARCHIVE_TYPE");
  374. if (!debian_archive_type) {
  375. debian_archive_type = "paxr";
  376. }
  377. std::string filename_data_tar =
  378. strGenWDIR + "/data.tar" + compression_suffix;
  379. // atomic file generation for data.tar
  380. {
  381. cmGeneratedFileStream fileStream_data_tar;
  382. fileStream_data_tar.Open(filename_data_tar.c_str(), false, true);
  383. if (!fileStream_data_tar) {
  384. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error opening the file \""
  385. << filename_data_tar << "\" for writing" << std::endl);
  386. return 0;
  387. }
  388. cmArchiveWrite data_tar(fileStream_data_tar, tar_compression_type,
  389. debian_archive_type);
  390. // uid/gid should be the one of the root user, and this root user has
  391. // always uid/gid equal to 0.
  392. data_tar.SetUIDAndGID(0u, 0u);
  393. data_tar.SetUNAMEAndGNAME("root", "root");
  394. // now add all directories which have to be compressed
  395. // collect all top level install dirs for that
  396. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would
  397. // give /usr and /opt
  398. size_t topLevelLength = strGenWDIR.length();
  399. cmCPackLogger(cmCPackLog::LOG_DEBUG, "WDIR: \""
  400. << strGenWDIR << "\", length = " << topLevelLength
  401. << std::endl);
  402. std::set<std::string> orderedFiles;
  403. // we have to reconstruct the parent folders as well
  404. for (std::string currentPath : packageFiles) {
  405. while (currentPath != strGenWDIR) {
  406. // the last one IS strGenWDIR, but we do not want this one:
  407. // XXX/application/usr/bin/myprogram with GEN_WDIR=XXX/application
  408. // should not add XXX/application
  409. orderedFiles.insert(currentPath);
  410. currentPath = cmSystemTools::CollapseCombinedPath(currentPath, "..");
  411. }
  412. }
  413. for (std::string const& file : orderedFiles) {
  414. cmCPackLogger(cmCPackLog::LOG_DEBUG, "FILEIT: \"" << file << "\""
  415. << std::endl);
  416. std::string::size_type slashPos = file.find('/', topLevelLength + 1);
  417. std::string relativeDir =
  418. file.substr(topLevelLength, slashPos - topLevelLength);
  419. cmCPackLogger(cmCPackLog::LOG_DEBUG, "RELATIVEDIR: \""
  420. << relativeDir << "\"" << std::endl);
  421. #ifdef WIN32
  422. std::string mode_t_adt_filename = file + ":cmake_mode_t";
  423. cmsys::ifstream permissionStream(mode_t_adt_filename.c_str());
  424. mode_t permissions = 0;
  425. if (permissionStream) {
  426. permissionStream >> std::oct >> permissions;
  427. }
  428. if (permissions != 0) {
  429. data_tar.SetPermissions(permissions);
  430. } else if (cmSystemTools::FileIsDirectory(file)) {
  431. data_tar.SetPermissions(0755);
  432. } else {
  433. data_tar.ClearPermissions();
  434. }
  435. #endif
  436. // do not recurse because the loop will do it
  437. if (!data_tar.Add(file, topLevelLength, ".", false)) {
  438. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem adding file to tar:"
  439. << std::endl
  440. << "#top level directory: " << strGenWDIR << std::endl
  441. << "#file: " << file << std::endl
  442. << "#error:" << data_tar.GetError() << std::endl);
  443. return 0;
  444. }
  445. }
  446. } // scope for file generation
  447. std::string md5filename = strGenWDIR + "/md5sums";
  448. {
  449. // the scope is needed for cmGeneratedFileStream
  450. cmGeneratedFileStream out(md5filename.c_str());
  451. std::string topLevelWithTrailingSlash =
  452. this->GetOption("CPACK_TEMPORARY_DIRECTORY");
  453. topLevelWithTrailingSlash += '/';
  454. for (std::string const& file : packageFiles) {
  455. // hash only regular files
  456. if (cmSystemTools::FileIsDirectory(file) ||
  457. cmSystemTools::FileIsSymlink(file)) {
  458. continue;
  459. }
  460. std::string output =
  461. cmSystemTools::ComputeFileHash(file, cmCryptoHash::AlgoMD5);
  462. if (output.empty()) {
  463. cmCPackLogger(cmCPackLog::LOG_ERROR, "Problem computing the md5 of "
  464. << file << std::endl);
  465. }
  466. output += " " + file + "\n";
  467. // debian md5sums entries are like this:
  468. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  469. // thus strip the full path (with the trailing slash)
  470. cmSystemTools::ReplaceString(output, topLevelWithTrailingSlash.c_str(),
  471. "");
  472. out << output;
  473. }
  474. // each line contains a eol.
  475. // Do not end the md5sum file with yet another (invalid)
  476. }
  477. std::string filename_control_tar = strGenWDIR + "/control.tar.gz";
  478. // atomic file generation for control.tar
  479. {
  480. cmGeneratedFileStream fileStream_control_tar;
  481. fileStream_control_tar.Open(filename_control_tar.c_str(), false, true);
  482. if (!fileStream_control_tar) {
  483. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error opening the file \""
  484. << filename_control_tar << "\" for writing"
  485. << std::endl);
  486. return 0;
  487. }
  488. cmArchiveWrite control_tar(fileStream_control_tar,
  489. cmArchiveWrite::CompressGZip,
  490. debian_archive_type);
  491. // sets permissions and uid/gid for the files
  492. control_tar.SetUIDAndGID(0u, 0u);
  493. control_tar.SetUNAMEAndGNAME("root", "root");
  494. /* permissions are set according to
  495. https://www.debian.org/doc/debian-policy/ch-files.html#s-permissions-owners
  496. and
  497. https://lintian.debian.org/tags/control-file-has-bad-permissions.html
  498. */
  499. const mode_t permission644 = 0644;
  500. const mode_t permissionExecute = 0111;
  501. const mode_t permission755 = permission644 | permissionExecute;
  502. // for md5sum and control (that we have generated here), we use 644
  503. // (RW-R--R--)
  504. // so that deb lintian doesn't warn about it
  505. control_tar.SetPermissions(permission644);
  506. // adds control and md5sums
  507. if (!control_tar.Add(md5filename, strGenWDIR.length(), ".") ||
  508. !control_tar.Add(strGenWDIR + "/control", strGenWDIR.length(), ".")) {
  509. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  510. << std::endl
  511. << "#top level directory: " << strGenWDIR << std::endl
  512. << "#file: \"control\" or \"md5sums\"" << std::endl
  513. << "#error:" << control_tar.GetError() << std::endl);
  514. return 0;
  515. }
  516. // adds generated shlibs file
  517. if (gen_shibs) {
  518. if (!control_tar.Add(shlibsfilename, strGenWDIR.length(), ".")) {
  519. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  520. << std::endl
  521. << "#top level directory: " << strGenWDIR << std::endl
  522. << "#file: \"shlibs\"" << std::endl
  523. << "#error:" << control_tar.GetError() << std::endl);
  524. return 0;
  525. }
  526. }
  527. // adds LDCONFIG related files
  528. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST")) {
  529. control_tar.SetPermissions(permission755);
  530. if (!control_tar.Add(postinst, strGenWDIR.length(), ".")) {
  531. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  532. << std::endl
  533. << "#top level directory: " << strGenWDIR << std::endl
  534. << "#file: \"postinst\"" << std::endl
  535. << "#error:" << control_tar.GetError() << std::endl);
  536. return 0;
  537. }
  538. control_tar.SetPermissions(permission644);
  539. }
  540. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM")) {
  541. control_tar.SetPermissions(permission755);
  542. if (!control_tar.Add(postrm, strGenWDIR.length(), ".")) {
  543. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error adding file to tar:"
  544. << std::endl
  545. << "#top level directory: " << strGenWDIR << std::endl
  546. << "#file: \"postinst\"" << std::endl
  547. << "#error:" << control_tar.GetError() << std::endl);
  548. return 0;
  549. }
  550. control_tar.SetPermissions(permission644);
  551. }
  552. // for the other files, we use
  553. // -either the original permission on the files
  554. // -either a permission strictly defined by the Debian policies
  555. const char* controlExtra =
  556. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA");
  557. if (controlExtra) {
  558. // permissions are now controlled by the original file permissions
  559. const bool permissionStrictPolicy =
  560. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION");
  561. static const char* strictFiles[] = { "config", "postinst", "postrm",
  562. "preinst", "prerm" };
  563. std::set<std::string> setStrictFiles(
  564. strictFiles,
  565. strictFiles + sizeof(strictFiles) / sizeof(strictFiles[0]));
  566. // default
  567. control_tar.ClearPermissions();
  568. std::vector<std::string> controlExtraList;
  569. cmSystemTools::ExpandListArgument(controlExtra, controlExtraList);
  570. for (std::string const& i : controlExtraList) {
  571. std::string filenamename = cmsys::SystemTools::GetFilenameName(i);
  572. std::string localcopy = strGenWDIR + "/" + filenamename;
  573. if (permissionStrictPolicy) {
  574. control_tar.SetPermissions(setStrictFiles.count(filenamename)
  575. ? permission755
  576. : permission644);
  577. }
  578. // if we can copy the file, it means it does exist, let's add it:
  579. if (cmsys::SystemTools::CopyFileIfDifferent(i, localcopy)) {
  580. control_tar.Add(localcopy, strGenWDIR.length(), ".");
  581. }
  582. }
  583. }
  584. }
  585. // ar -r your-package-name.deb debian-binary control.tar.* data.tar.*
  586. // A debian package .deb is simply an 'ar' archive. The only subtle
  587. // difference is that debian uses the BSD ar style archive whereas most
  588. // Linux distro have a GNU ar.
  589. // See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=161593 for more info
  590. std::string const outputDir = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  591. std::string const outputName = this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  592. std::string const outputPath = outputDir + "/" + outputName;
  593. std::string const tlDir = strGenWDIR + "/";
  594. cmGeneratedFileStream debStream;
  595. debStream.Open(outputPath.c_str(), false, true);
  596. cmArchiveWrite deb(debStream, cmArchiveWrite::CompressNone, "arbsd");
  597. if (!deb.Add(tlDir + "debian-binary", tlDir.length()) ||
  598. !deb.Add(tlDir + "control.tar.gz", tlDir.length()) ||
  599. !deb.Add(tlDir + "data.tar" + compression_suffix, tlDir.length())) {
  600. cmCPackLogger(cmCPackLog::LOG_ERROR, "Error creating debian package:"
  601. << std::endl
  602. << "#top level directory: " << outputDir << std::endl
  603. << "#file: " << outputName << std::endl
  604. << "#error:" << deb.GetError() << std::endl);
  605. return 0;
  606. }
  607. return 1;
  608. }
  609. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  610. {
  611. return IsOn("CPACK_DEB_COMPONENT_INSTALL");
  612. }
  613. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  614. const std::string& componentName)
  615. {
  616. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  617. return componentName;
  618. }
  619. if (componentPackageMethod == ONE_PACKAGE) {
  620. return std::string("ALL_COMPONENTS_IN_ONE");
  621. }
  622. // We have to find the name of the COMPONENT GROUP
  623. // the current COMPONENT belongs to.
  624. std::string groupVar =
  625. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  626. if (nullptr != GetOption(groupVar)) {
  627. return std::string(GetOption(groupVar));
  628. }
  629. return componentName;
  630. }