cmCPackDebGenerator.cxx 33 KB

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