cmCPackDebGenerator.cxx 32 KB

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