cmCPackDebGenerator.cxx 32 KB

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