cmCPackDebGenerator.cxx 27 KB

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