cmCPackDebGenerator.cxx 33 KB

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