cmCPackDebGenerator.cxx 31 KB

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