cmCPackDebGenerator.cxx 32 KB

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