cmCPackDebGenerator.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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 "cmList.h"
  20. #include "cmStringAlgorithms.h"
  21. #include "cmSystemTools.h"
  22. #include "cmValue.h"
  23. namespace {
  24. class DebGenerator
  25. {
  26. public:
  27. DebGenerator(cmCPackLog* logger, std::string outputName, std::string workDir,
  28. std::string topLevelDir, std::string temporaryDir,
  29. cmValue debianCompressionType, cmValue numThreads,
  30. cmValue debianArchiveType,
  31. std::map<std::string, std::string> controlValues,
  32. bool genShLibs, std::string shLibsFilename, bool genPostInst,
  33. std::string postInst, bool genPostRm, std::string postRm,
  34. cmValue controlExtra, bool permissionStrctPolicy,
  35. std::vector<std::string> packageFiles);
  36. bool generate() const;
  37. private:
  38. void generateDebianBinaryFile() const;
  39. void generateControlFile() const;
  40. bool generateDataTar() const;
  41. std::string generateMD5File() const;
  42. bool generateControlTar(std::string const& md5Filename) const;
  43. bool generateDeb() const;
  44. cmCPackLog* Logger;
  45. const std::string OutputName;
  46. const std::string WorkDir;
  47. std::string CompressionSuffix;
  48. const std::string TopLevelDir;
  49. const std::string TemporaryDir;
  50. const std::string DebianArchiveType;
  51. long NumThreads;
  52. const std::map<std::string, std::string> ControlValues;
  53. const bool GenShLibs;
  54. const std::string ShLibsFilename;
  55. const bool GenPostInst;
  56. const std::string PostInst;
  57. const bool GenPostRm;
  58. const std::string PostRm;
  59. cmValue ControlExtra;
  60. const bool PermissionStrictPolicy;
  61. const std::vector<std::string> PackageFiles;
  62. cmArchiveWrite::Compress TarCompressionType;
  63. };
  64. DebGenerator::DebGenerator(
  65. cmCPackLog* logger, std::string outputName, std::string workDir,
  66. std::string topLevelDir, std::string temporaryDir,
  67. cmValue debCompressionType, cmValue numThreads, cmValue debianArchiveType,
  68. std::map<std::string, std::string> controlValues, bool genShLibs,
  69. std::string shLibsFilename, bool genPostInst, std::string postInst,
  70. bool genPostRm, std::string postRm, cmValue controlExtra,
  71. bool permissionStrictPolicy, std::vector<std::string> packageFiles)
  72. : Logger(logger)
  73. , OutputName(std::move(outputName))
  74. , WorkDir(std::move(workDir))
  75. , TopLevelDir(std::move(topLevelDir))
  76. , TemporaryDir(std::move(temporaryDir))
  77. , DebianArchiveType(debianArchiveType ? *debianArchiveType : "gnutar")
  78. , ControlValues(std::move(controlValues))
  79. , GenShLibs(genShLibs)
  80. , ShLibsFilename(std::move(shLibsFilename))
  81. , GenPostInst(genPostInst)
  82. , PostInst(std::move(postInst))
  83. , GenPostRm(genPostRm)
  84. , PostRm(std::move(postRm))
  85. , ControlExtra(controlExtra)
  86. , PermissionStrictPolicy(permissionStrictPolicy)
  87. , PackageFiles(std::move(packageFiles))
  88. {
  89. std::string debianCompressionType = "gzip";
  90. if (debCompressionType) {
  91. debianCompressionType = *debCompressionType;
  92. }
  93. if (debianCompressionType == "lzma") {
  94. this->CompressionSuffix = ".lzma";
  95. this->TarCompressionType = cmArchiveWrite::CompressLZMA;
  96. } else if (debianCompressionType == "xz") {
  97. this->CompressionSuffix = ".xz";
  98. this->TarCompressionType = cmArchiveWrite::CompressXZ;
  99. } else if (debianCompressionType == "bzip2") {
  100. this->CompressionSuffix = ".bz2";
  101. this->TarCompressionType = cmArchiveWrite::CompressBZip2;
  102. } else if (debianCompressionType == "gzip") {
  103. this->CompressionSuffix = ".gz";
  104. this->TarCompressionType = cmArchiveWrite::CompressGZip;
  105. } else if (debianCompressionType == "zstd") {
  106. this->CompressionSuffix = ".zst";
  107. this->TarCompressionType = cmArchiveWrite::CompressZstd;
  108. } else if (debianCompressionType == "none") {
  109. this->CompressionSuffix.clear();
  110. this->TarCompressionType = cmArchiveWrite::CompressNone;
  111. } else {
  112. cmCPackLogger(cmCPackLog::LOG_ERROR,
  113. "Error unrecognized compression type: "
  114. << debianCompressionType << std::endl);
  115. }
  116. if (numThreads) {
  117. if (!cmStrToLong(*numThreads, &this->NumThreads)) {
  118. this->NumThreads = 1;
  119. cmCPackLogger(cmCPackLog::LOG_ERROR,
  120. "Unrecognized number of threads: " << numThreads
  121. << std::endl);
  122. }
  123. } else {
  124. this->NumThreads = 1;
  125. }
  126. }
  127. bool DebGenerator::generate() const
  128. {
  129. this->generateDebianBinaryFile();
  130. this->generateControlFile();
  131. if (!this->generateDataTar()) {
  132. return false;
  133. }
  134. std::string md5Filename = this->generateMD5File();
  135. if (!this->generateControlTar(md5Filename)) {
  136. return false;
  137. }
  138. return this->generateDeb();
  139. }
  140. void DebGenerator::generateDebianBinaryFile() const
  141. {
  142. // debian-binary file
  143. const std::string dbfilename = this->WorkDir + "/debian-binary";
  144. cmGeneratedFileStream out;
  145. out.Open(dbfilename, false, true);
  146. out << "2.0\n"; // required for valid debian package
  147. }
  148. void DebGenerator::generateControlFile() const
  149. {
  150. std::string ctlfilename = this->WorkDir + "/control";
  151. cmGeneratedFileStream out;
  152. out.Open(ctlfilename, false, true);
  153. for (auto const& kv : this->ControlValues) {
  154. out << kv.first << ": " << kv.second << "\n";
  155. }
  156. unsigned long totalSize = 0;
  157. {
  158. for (std::string const& file : this->PackageFiles) {
  159. totalSize += cmSystemTools::FileLength(file);
  160. }
  161. }
  162. out << "Installed-Size: " << (totalSize + 1023) / 1024 << "\n\n";
  163. }
  164. bool DebGenerator::generateDataTar() const
  165. {
  166. std::string filename_data_tar =
  167. this->WorkDir + "/data.tar" + this->CompressionSuffix;
  168. cmGeneratedFileStream fileStream_data_tar;
  169. fileStream_data_tar.Open(filename_data_tar, false, true);
  170. if (!fileStream_data_tar) {
  171. cmCPackLogger(cmCPackLog::LOG_ERROR,
  172. "Error opening the file \""
  173. << filename_data_tar << "\" for writing" << std::endl);
  174. return false;
  175. }
  176. cmArchiveWrite data_tar(fileStream_data_tar, this->TarCompressionType,
  177. this->DebianArchiveType, 0,
  178. static_cast<int>(this->NumThreads));
  179. if (!data_tar.Open()) {
  180. cmCPackLogger(cmCPackLog::LOG_ERROR,
  181. "Error opening the archive \""
  182. << filename_data_tar
  183. << "\", ERROR = " << data_tar.GetError() << std::endl);
  184. return false;
  185. }
  186. // uid/gid should be the one of the root user, and this root user has
  187. // always uid/gid equal to 0.
  188. data_tar.SetUIDAndGID(0U, 0U);
  189. data_tar.SetUNAMEAndGNAME("root", "root");
  190. // now add all directories which have to be compressed
  191. // collect all top level install dirs for that
  192. // e.g. /opt/bin/foo, /usr/bin/bar and /usr/bin/baz would
  193. // give /usr and /opt
  194. size_t topLevelLength = this->WorkDir.length();
  195. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  196. "WDIR: \"" << this->WorkDir
  197. << "\", length = " << topLevelLength << std::endl);
  198. std::set<std::string> orderedFiles;
  199. // we have to reconstruct the parent folders as well
  200. for (std::string currentPath : this->PackageFiles) {
  201. while (currentPath != this->WorkDir) {
  202. // the last one IS WorkDir, but we do not want this one:
  203. // XXX/application/usr/bin/myprogram with GEN_WDIR=XXX/application
  204. // should not add XXX/application
  205. orderedFiles.insert(currentPath);
  206. currentPath = cmSystemTools::GetFilenamePath(currentPath);
  207. }
  208. }
  209. for (std::string const& file : orderedFiles) {
  210. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  211. "FILEIT: \"" << file << "\"" << std::endl);
  212. std::string::size_type slashPos = file.find('/', topLevelLength + 1);
  213. std::string relativeDir =
  214. file.substr(topLevelLength, slashPos - topLevelLength);
  215. cmCPackLogger(cmCPackLog::LOG_DEBUG,
  216. "RELATIVEDIR: \"" << relativeDir << "\"" << std::endl);
  217. #ifdef _WIN32
  218. std::string mode_t_adt_filename = file + ":cmake_mode_t";
  219. cmsys::ifstream permissionStream(mode_t_adt_filename.c_str());
  220. mode_t permissions = 0;
  221. if (permissionStream) {
  222. permissionStream >> std::oct >> permissions;
  223. }
  224. if (permissions != 0) {
  225. data_tar.SetPermissions(permissions);
  226. } else if (cmSystemTools::FileIsDirectory(file)) {
  227. data_tar.SetPermissions(0755);
  228. } else {
  229. data_tar.ClearPermissions();
  230. }
  231. #endif
  232. // do not recurse because the loop will do it
  233. if (!data_tar.Add(file, topLevelLength, ".", false)) {
  234. cmCPackLogger(cmCPackLog::LOG_ERROR,
  235. "Problem adding file to tar:\n"
  236. "#top level directory: "
  237. << this->WorkDir
  238. << "\n"
  239. "#file: "
  240. << file
  241. << "\n"
  242. "#error:"
  243. << data_tar.GetError() << std::endl);
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. std::string DebGenerator::generateMD5File() const
  250. {
  251. std::string md5filename = this->WorkDir + "/md5sums";
  252. cmGeneratedFileStream out;
  253. out.Open(md5filename, false, true);
  254. std::string topLevelWithTrailingSlash = cmStrCat(this->TemporaryDir, '/');
  255. for (std::string const& file : this->PackageFiles) {
  256. // hash only regular files
  257. if (cmSystemTools::FileIsDirectory(file) ||
  258. cmSystemTools::FileIsSymlink(file)) {
  259. continue;
  260. }
  261. cmCryptoHash hasher(cmCryptoHash::AlgoMD5);
  262. std::string output = hasher.HashFile(file);
  263. if (output.empty()) {
  264. cmCPackLogger(cmCPackLog::LOG_ERROR,
  265. "Problem computing the md5 of " << file << std::endl);
  266. }
  267. output += " " + file + "\n";
  268. // debian md5sums entries are like this:
  269. // 014f3604694729f3bf19263bac599765 usr/bin/ccmake
  270. // thus strip the full path (with the trailing slash)
  271. cmSystemTools::ReplaceString(output, topLevelWithTrailingSlash.c_str(),
  272. "");
  273. out << output;
  274. }
  275. // each line contains a eol.
  276. // Do not end the md5sum file with yet another (invalid)
  277. return md5filename;
  278. }
  279. bool DebGenerator::generateControlTar(std::string const& md5Filename) const
  280. {
  281. std::string filename_control_tar = this->WorkDir + "/control.tar.gz";
  282. cmGeneratedFileStream fileStream_control_tar;
  283. fileStream_control_tar.Open(filename_control_tar, false, true);
  284. if (!fileStream_control_tar) {
  285. cmCPackLogger(cmCPackLog::LOG_ERROR,
  286. "Error opening the file \""
  287. << filename_control_tar << "\" for writing" << std::endl);
  288. return false;
  289. }
  290. cmArchiveWrite control_tar(fileStream_control_tar,
  291. cmArchiveWrite::CompressGZip,
  292. this->DebianArchiveType);
  293. if (!control_tar.Open()) {
  294. cmCPackLogger(cmCPackLog::LOG_ERROR,
  295. "Error opening the archive \""
  296. << filename_control_tar
  297. << "\", ERROR = " << control_tar.GetError() << std::endl);
  298. return false;
  299. }
  300. // sets permissions and uid/gid for the files
  301. control_tar.SetUIDAndGID(0u, 0u);
  302. control_tar.SetUNAMEAndGNAME("root", "root");
  303. /* permissions are set according to
  304. https://www.debian.org/doc/debian-policy/ch-files.html#s-permissions-owners
  305. and
  306. https://lintian.debian.org/tags/control-file-has-bad-permissions.html
  307. */
  308. const mode_t permission644 = 0644;
  309. const mode_t permissionExecute = 0111;
  310. const mode_t permission755 = permission644 | permissionExecute;
  311. // for md5sum and control (that we have generated here), we use 644
  312. // (RW-R--R--)
  313. // so that deb lintian doesn't warn about it
  314. control_tar.SetPermissions(permission644);
  315. // adds control and md5sums
  316. if (!control_tar.Add(md5Filename, this->WorkDir.length(), ".") ||
  317. !control_tar.Add(this->WorkDir + "/control", this->WorkDir.length(),
  318. ".")) {
  319. cmCPackLogger(cmCPackLog::LOG_ERROR,
  320. "Error adding file to tar:\n"
  321. "#top level directory: "
  322. << this->WorkDir
  323. << "\n"
  324. "#file: \"control\" or \"md5sums\"\n"
  325. "#error:"
  326. << control_tar.GetError() << std::endl);
  327. return false;
  328. }
  329. // adds generated shlibs file
  330. if (this->GenShLibs) {
  331. if (!control_tar.Add(this->ShLibsFilename, this->WorkDir.length(), ".")) {
  332. cmCPackLogger(cmCPackLog::LOG_ERROR,
  333. "Error adding file to tar:\n"
  334. "#top level directory: "
  335. << this->WorkDir
  336. << "\n"
  337. "#file: \"shlibs\"\n"
  338. "#error:"
  339. << control_tar.GetError() << std::endl);
  340. return false;
  341. }
  342. }
  343. // adds LDCONFIG related files
  344. if (this->GenPostInst) {
  345. control_tar.SetPermissions(permission755);
  346. if (!control_tar.Add(this->PostInst, this->WorkDir.length(), ".")) {
  347. cmCPackLogger(cmCPackLog::LOG_ERROR,
  348. "Error adding file to tar:\n"
  349. "#top level directory: "
  350. << this->WorkDir
  351. << "\n"
  352. "#file: \"postinst\"\n"
  353. "#error:"
  354. << control_tar.GetError() << std::endl);
  355. return false;
  356. }
  357. control_tar.SetPermissions(permission644);
  358. }
  359. if (this->GenPostRm) {
  360. control_tar.SetPermissions(permission755);
  361. if (!control_tar.Add(this->PostRm, this->WorkDir.length(), ".")) {
  362. cmCPackLogger(cmCPackLog::LOG_ERROR,
  363. "Error adding file to tar:\n"
  364. "#top level directory: "
  365. << this->WorkDir
  366. << "\n"
  367. "#file: \"postinst\"\n"
  368. "#error:"
  369. << control_tar.GetError() << std::endl);
  370. return false;
  371. }
  372. control_tar.SetPermissions(permission644);
  373. }
  374. // for the other files, we use
  375. // -either the original permission on the files
  376. // -either a permission strictly defined by the Debian policies
  377. if (this->ControlExtra) {
  378. // permissions are now controlled by the original file permissions
  379. static const char* strictFiles[] = { "config", "postinst", "postrm",
  380. "preinst", "prerm" };
  381. std::set<std::string> setStrictFiles(
  382. strictFiles, strictFiles + sizeof(strictFiles) / sizeof(strictFiles[0]));
  383. // default
  384. control_tar.ClearPermissions();
  385. cmList controlExtraList{ 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 (this->GetOption("CPACK_SET_DESTDIR").IsOff()) {
  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. // Determine the sanitized packaging directory-name that can be used on the
  481. // file-system.
  482. std::string sanitizedPkgDirName =
  483. this->GetSanitizedDirOrFileName(packageName);
  484. // Begin the archive for this pack
  485. std::string localToplevel(initialTopLevel);
  486. std::string packageFileName(
  487. cmSystemTools::GetParentDirectory(this->toplevel));
  488. std::string outputFileName(*this->GetOption("CPACK_PACKAGE_FILE_NAME") +
  489. "-" + packageName + this->GetOutputExtension());
  490. localToplevel += "/" + sanitizedPkgDirName;
  491. /* replace the TEMP DIRECTORY with the component one */
  492. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel);
  493. packageFileName += "/" + outputFileName;
  494. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  495. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName);
  496. /* replace the TEMPORARY package file name */
  497. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME", packageFileName);
  498. // Tell CPackDeb.cmake the name of the component GROUP.
  499. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT", packageName);
  500. // Tell CPackDeb.cmake the path where the component is.
  501. std::string component_path = cmStrCat('/', sanitizedPkgDirName);
  502. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH", component_path);
  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) {
  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);
  575. packageFileName += "/" + outputFileName;
  576. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  577. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName);
  578. /* replace the TEMPORARY package file name */
  579. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME", packageFileName);
  580. if (!compInstDirName.empty()) {
  581. // Tell CPackDeb.cmake the path where the component is.
  582. std::string component_path = cmStrCat('/', compInstDirName);
  583. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH", component_path);
  584. }
  585. if (!this->ReadListFile("Internal/CPack/CPackDeb.cmake")) {
  586. cmCPackLogger(cmCPackLog::LOG_ERROR,
  587. "Error while execution CPackDeb.cmake" << std::endl);
  588. return 0;
  589. }
  590. return this->createDebPackages();
  591. }
  592. int cmCPackDebGenerator::PackageFiles()
  593. {
  594. /* Are we in the component packaging case */
  595. if (this->WantsComponentInstallation()) {
  596. // CASE 1 : COMPONENT ALL-IN-ONE package
  597. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  598. // then the package file is unique and should be open here.
  599. if (this->componentPackageMethod == ONE_PACKAGE) {
  600. return this->PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  601. }
  602. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  603. // There will be 1 package for each component group
  604. // however one may require to ignore component group and
  605. // in this case you'll get 1 package for each component.
  606. return this->PackageComponents(this->componentPackageMethod ==
  607. ONE_PACKAGE_PER_COMPONENT);
  608. }
  609. // CASE 3 : NON COMPONENT package.
  610. return this->PackageComponentsAllInOne("");
  611. }
  612. bool cmCPackDebGenerator::createDebPackages()
  613. {
  614. auto make_package = [this](const std::string& path,
  615. const char* const output_var,
  616. bool (cmCPackDebGenerator::*creator)()) -> bool {
  617. try {
  618. this->packageFiles = findFilesIn(path);
  619. } catch (const std::runtime_error& ex) {
  620. cmCPackLogger(cmCPackLog::LOG_ERROR, ex.what() << std::endl);
  621. return false;
  622. }
  623. if ((this->*creator)()) {
  624. // add the generated package to package file names list
  625. this->packageFileNames.emplace_back(
  626. cmStrCat(this->GetOption("CPACK_TOPLEVEL_DIRECTORY"), '/',
  627. this->GetOption(output_var)));
  628. return true;
  629. }
  630. return false;
  631. };
  632. bool retval =
  633. make_package(this->GetOption("GEN_WDIR"), "GEN_CPACK_OUTPUT_FILE_NAME",
  634. &cmCPackDebGenerator::createDeb);
  635. cmValue dbgsymdir_path = this->GetOption("GEN_DBGSYMDIR");
  636. if (this->IsOn("GEN_CPACK_DEBIAN_DEBUGINFO_PACKAGE") && dbgsymdir_path) {
  637. retval = make_package(*dbgsymdir_path, "GEN_CPACK_DBGSYM_OUTPUT_FILE_NAME",
  638. &cmCPackDebGenerator::createDbgsymDDeb) &&
  639. retval;
  640. }
  641. return static_cast<int>(retval);
  642. }
  643. bool cmCPackDebGenerator::createDeb()
  644. {
  645. std::map<std::string, std::string> controlValues;
  646. // debian policy enforce lower case for package name
  647. controlValues["Package"] = cmsys::SystemTools::LowerCase(
  648. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME"));
  649. controlValues["Version"] =
  650. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  651. controlValues["Section"] =
  652. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SECTION");
  653. controlValues["Priority"] =
  654. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PRIORITY");
  655. controlValues["Architecture"] =
  656. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  657. controlValues["Maintainer"] =
  658. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  659. controlValues["Description"] =
  660. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  661. cmValue debian_pkg_source =
  662. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  663. if (cmNonempty(debian_pkg_source)) {
  664. controlValues["Source"] = *debian_pkg_source;
  665. }
  666. cmValue debian_pkg_dep = this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DEPENDS");
  667. if (cmNonempty(debian_pkg_dep)) {
  668. controlValues["Depends"] = *debian_pkg_dep;
  669. }
  670. cmValue debian_pkg_rec =
  671. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  672. if (cmNonempty(debian_pkg_rec)) {
  673. controlValues["Recommends"] = *debian_pkg_rec;
  674. }
  675. cmValue debian_pkg_sug =
  676. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SUGGESTS");
  677. if (cmNonempty(debian_pkg_sug)) {
  678. controlValues["Suggests"] = *debian_pkg_sug;
  679. }
  680. cmValue debian_pkg_url =
  681. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  682. if (cmNonempty(debian_pkg_url)) {
  683. controlValues["Homepage"] = *debian_pkg_url;
  684. }
  685. cmValue 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. cmValue 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. cmValue 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. cmValue 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. cmValue 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. cmValue 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. cmValue debian_pkg_multiarch =
  716. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MULTIARCH");
  717. if (cmNonempty(debian_pkg_multiarch)) {
  718. // check for valid values: same, foreign, allowed
  719. if (*debian_pkg_multiarch != "same" &&
  720. *debian_pkg_multiarch != "foreign" &&
  721. *debian_pkg_multiarch != "allowed") {
  722. cmCPackLogger(cmCPackLog::LOG_ERROR,
  723. "Error: invalid value for Multi-Arch: "
  724. << *debian_pkg_multiarch
  725. << ". Valid values are: same, foreign, allowed\n");
  726. return false;
  727. }
  728. controlValues["Multi-Arch"] = *debian_pkg_multiarch;
  729. }
  730. const std::string strGenWDIR(this->GetOption("GEN_WDIR"));
  731. const std::string shlibsfilename = strGenWDIR + "/shlibs";
  732. cmValue debian_pkg_shlibs =
  733. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SHLIBS");
  734. const bool gen_shibs = this->IsOn("CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS") &&
  735. cmNonempty(debian_pkg_shlibs);
  736. if (gen_shibs) {
  737. cmGeneratedFileStream out;
  738. out.Open(shlibsfilename, false, true);
  739. out << debian_pkg_shlibs;
  740. out << '\n';
  741. }
  742. const std::string postinst = strGenWDIR + "/postinst";
  743. const std::string postrm = strGenWDIR + "/postrm";
  744. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST")) {
  745. cmGeneratedFileStream out;
  746. out.Open(postinst, false, true);
  747. out << "#!/bin/sh\n\n"
  748. "set -e\n\n"
  749. "if [ \"$1\" = \"configure\" ]; then\n"
  750. "\tldconfig\n"
  751. "fi\n";
  752. }
  753. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM")) {
  754. cmGeneratedFileStream out;
  755. out.Open(postrm, false, true);
  756. out << "#!/bin/sh\n\n"
  757. "set -e\n\n"
  758. "if [ \"$1\" = \"remove\" ]; then\n"
  759. "\tldconfig\n"
  760. "fi\n";
  761. }
  762. DebGenerator gen(
  763. this->Logger, this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME"), strGenWDIR,
  764. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  765. this->GetOption("CPACK_TEMPORARY_DIRECTORY"),
  766. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE"),
  767. this->GetOption("CPACK_THREADS"),
  768. this->GetOption("GEN_CPACK_DEBIAN_ARCHIVE_TYPE"), controlValues, gen_shibs,
  769. shlibsfilename, this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST"), postinst,
  770. this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM"), postrm,
  771. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA"),
  772. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION"),
  773. this->packageFiles);
  774. return gen.generate();
  775. }
  776. bool cmCPackDebGenerator::createDbgsymDDeb()
  777. {
  778. // Packages containing debug symbols follow the same structure as .debs
  779. // but have different metadata and content.
  780. std::map<std::string, std::string> controlValues;
  781. // debian policy enforce lower case for package name
  782. std::string packageNameLower = cmsys::SystemTools::LowerCase(
  783. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME"));
  784. cmValue debian_pkg_version =
  785. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  786. controlValues["Package"] = packageNameLower + "-dbgsym";
  787. controlValues["Package-Type"] = "ddeb";
  788. controlValues["Version"] = *debian_pkg_version;
  789. controlValues["Auto-Built-Package"] = "debug-symbols";
  790. controlValues["Depends"] =
  791. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME") + std::string(" (= ") +
  792. *debian_pkg_version + ")";
  793. controlValues["Section"] = "debug";
  794. controlValues["Priority"] = "optional";
  795. controlValues["Architecture"] =
  796. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  797. controlValues["Maintainer"] =
  798. *this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  799. controlValues["Description"] =
  800. std::string("debug symbols for ") + packageNameLower;
  801. cmValue debian_pkg_source =
  802. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  803. if (cmNonempty(debian_pkg_source)) {
  804. controlValues["Source"] = *debian_pkg_source;
  805. }
  806. cmValue debian_build_ids = this->GetOption("GEN_BUILD_IDS");
  807. if (cmNonempty(debian_build_ids)) {
  808. controlValues["Build-Ids"] = *debian_build_ids;
  809. }
  810. DebGenerator gen(
  811. this->Logger, this->GetOption("GEN_CPACK_DBGSYM_OUTPUT_FILE_NAME"),
  812. this->GetOption("GEN_DBGSYMDIR"),
  813. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  814. this->GetOption("CPACK_TEMPORARY_DIRECTORY"),
  815. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE"),
  816. this->GetOption("CPACK_THREADS"),
  817. this->GetOption("GEN_CPACK_DEBIAN_ARCHIVE_TYPE"), controlValues, false, "",
  818. false, "", false, "", nullptr,
  819. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION"),
  820. this->packageFiles);
  821. return gen.generate();
  822. }
  823. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  824. {
  825. return this->IsOn("CPACK_DEB_COMPONENT_INSTALL");
  826. }
  827. std::string cmCPackDebGenerator::GetComponentInstallSuffix(
  828. const std::string& componentName)
  829. {
  830. if (this->componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  831. return componentName;
  832. }
  833. if (this->componentPackageMethod == ONE_PACKAGE) {
  834. return { "ALL_COMPONENTS_IN_ONE" };
  835. }
  836. // We have to find the name of the COMPONENT GROUP
  837. // the current COMPONENT belongs to.
  838. std::string groupVar =
  839. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  840. if (this->GetOption(groupVar)) {
  841. return *this->GetOption(groupVar);
  842. }
  843. return componentName;
  844. }
  845. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  846. const std::string& componentName)
  847. {
  848. return this->GetSanitizedDirOrFileName(
  849. this->GetComponentInstallSuffix(componentName));
  850. }