cmCPackDebGenerator.cxx 31 KB

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