cmCPackDebGenerator.cxx 31 KB

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