cmCPackDebGenerator.cxx 26 KB

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