cmCPackDebGenerator.cxx 32 KB

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