cmCPackDebGenerator.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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() = default;
  385. cmCPackDebGenerator::~cmCPackDebGenerator() = default;
  386. int cmCPackDebGenerator::InitializeInternal()
  387. {
  388. this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr");
  389. if (cmSystemTools::IsOff(this->GetOption("CPACK_SET_DESTDIR"))) {
  390. this->SetOption("CPACK_SET_DESTDIR", "I_ON");
  391. }
  392. return this->Superclass::InitializeInternal();
  393. }
  394. int cmCPackDebGenerator::PackageOnePack(std::string const& initialTopLevel,
  395. std::string const& packageName)
  396. {
  397. int retval = 1;
  398. // Begin the archive for this pack
  399. std::string localToplevel(initialTopLevel);
  400. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  401. std::string outputFileName(
  402. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) + "-" +
  403. packageName + this->GetOutputExtension());
  404. localToplevel += "/" + packageName;
  405. /* replace the TEMP DIRECTORY with the component one */
  406. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  407. packageFileName += "/" + outputFileName;
  408. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  409. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  410. /* replace the TEMPORARY package file name */
  411. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  412. packageFileName.c_str());
  413. // Tell CPackDeb.cmake the name of the component GROUP.
  414. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT", packageName.c_str());
  415. // Tell CPackDeb.cmake the path where the component is.
  416. std::string component_path = "/";
  417. component_path += packageName;
  418. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  419. component_path.c_str());
  420. if (!this->ReadListFile("Internal/CPack/CPackDeb.cmake")) {
  421. cmCPackLogger(cmCPackLog::LOG_ERROR,
  422. "Error while execution CPackDeb.cmake" << std::endl);
  423. retval = 0;
  424. return retval;
  425. }
  426. { // Isolate globbing of binaries vs. dbgsyms
  427. cmsys::Glob gl;
  428. std::string findExpr(this->GetOption("GEN_WDIR"));
  429. findExpr += "/*";
  430. gl.RecurseOn();
  431. gl.SetRecurseListDirs(true);
  432. if (!gl.FindFiles(findExpr)) {
  433. cmCPackLogger(cmCPackLog::LOG_ERROR,
  434. "Cannot find any files in the installed directory"
  435. << std::endl);
  436. return 0;
  437. }
  438. packageFiles = gl.GetFiles();
  439. }
  440. int res = createDeb();
  441. if (res != 1) {
  442. retval = 0;
  443. }
  444. // add the generated package to package file names list
  445. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  446. packageFileName += "/";
  447. packageFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  448. packageFileNames.push_back(std::move(packageFileName));
  449. if (this->IsOn("GEN_CPACK_DEBIAN_DEBUGINFO_PACKAGE")) {
  450. cmsys::Glob gl;
  451. std::string findExpr(this->GetOption("GEN_DBGSYMDIR"));
  452. findExpr += "/*";
  453. gl.RecurseOn();
  454. gl.SetRecurseListDirs(true);
  455. if (!gl.FindFiles(findExpr)) {
  456. cmCPackLogger(cmCPackLog::LOG_ERROR,
  457. "Cannot find any files in the installed directory"
  458. << std::endl);
  459. return 0;
  460. }
  461. packageFiles = gl.GetFiles();
  462. res = createDbgsymDDeb();
  463. if (res != 1) {
  464. retval = 0;
  465. }
  466. // add the generated package to package file names list
  467. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  468. packageFileName += "/";
  469. packageFileName += this->GetOption("GEN_CPACK_DBGSYM_OUTPUT_FILE_NAME");
  470. packageFileNames.push_back(std::move(packageFileName));
  471. }
  472. return retval;
  473. }
  474. int cmCPackDebGenerator::PackageComponents(bool ignoreGroup)
  475. {
  476. int retval = 1;
  477. /* Reset package file name list it will be populated during the
  478. * component packaging run*/
  479. packageFileNames.clear();
  480. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  481. // The default behavior is to have one package by component group
  482. // unless CPACK_COMPONENTS_IGNORE_GROUP is specified.
  483. if (!ignoreGroup) {
  484. for (auto const& compG : this->ComponentGroups) {
  485. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  486. "Packaging component group: " << compG.first << std::endl);
  487. // Begin the archive for this group
  488. retval &= PackageOnePack(initialTopLevel, compG.first);
  489. }
  490. // Handle Orphan components (components not belonging to any groups)
  491. for (auto const& comp : this->Components) {
  492. // Does the component belong to a group?
  493. if (comp.second.Group == nullptr) {
  494. cmCPackLogger(
  495. cmCPackLog::LOG_VERBOSE,
  496. "Component <"
  497. << comp.second.Name
  498. << "> does not belong to any group, package it separately."
  499. << std::endl);
  500. // Begin the archive for this orphan component
  501. retval &= PackageOnePack(initialTopLevel, comp.first);
  502. }
  503. }
  504. }
  505. // CPACK_COMPONENTS_IGNORE_GROUPS is set
  506. // We build 1 package per component
  507. else {
  508. for (auto const& comp : this->Components) {
  509. retval &= PackageOnePack(initialTopLevel, comp.first);
  510. }
  511. }
  512. return retval;
  513. }
  514. //----------------------------------------------------------------------
  515. int cmCPackDebGenerator::PackageComponentsAllInOne(
  516. const std::string& compInstDirName)
  517. {
  518. int retval = 1;
  519. /* Reset package file name list it will be populated during the
  520. * component packaging run*/
  521. packageFileNames.clear();
  522. std::string initialTopLevel(this->GetOption("CPACK_TEMPORARY_DIRECTORY"));
  523. cmCPackLogger(cmCPackLog::LOG_VERBOSE,
  524. "Packaging all groups in one package..."
  525. "(CPACK_COMPONENTS_ALL_[GROUPS_]IN_ONE_PACKAGE is set)"
  526. << std::endl);
  527. // The ALL GROUPS in ONE package case
  528. std::string localToplevel(initialTopLevel);
  529. std::string packageFileName(cmSystemTools::GetParentDirectory(toplevel));
  530. std::string outputFileName(
  531. std::string(this->GetOption("CPACK_PACKAGE_FILE_NAME")) +
  532. this->GetOutputExtension());
  533. // all GROUP in one vs all COMPONENT in one
  534. // if must be here otherwise non component paths have a trailing / while
  535. // components don't
  536. if (!compInstDirName.empty()) {
  537. localToplevel += "/" + compInstDirName;
  538. }
  539. /* replace the TEMP DIRECTORY with the component one */
  540. this->SetOption("CPACK_TEMPORARY_DIRECTORY", localToplevel.c_str());
  541. packageFileName += "/" + outputFileName;
  542. /* replace proposed CPACK_OUTPUT_FILE_NAME */
  543. this->SetOption("CPACK_OUTPUT_FILE_NAME", outputFileName.c_str());
  544. /* replace the TEMPORARY package file name */
  545. this->SetOption("CPACK_TEMPORARY_PACKAGE_FILE_NAME",
  546. packageFileName.c_str());
  547. if (!compInstDirName.empty()) {
  548. // Tell CPackDeb.cmake the path where the component is.
  549. std::string component_path = "/";
  550. component_path += compInstDirName;
  551. this->SetOption("CPACK_DEB_PACKAGE_COMPONENT_PART_PATH",
  552. component_path.c_str());
  553. }
  554. if (!this->ReadListFile("Internal/CPack/CPackDeb.cmake")) {
  555. cmCPackLogger(cmCPackLog::LOG_ERROR,
  556. "Error while execution CPackDeb.cmake" << std::endl);
  557. retval = 0;
  558. return retval;
  559. }
  560. cmsys::Glob gl;
  561. std::string findExpr(this->GetOption("GEN_WDIR"));
  562. findExpr += "/*";
  563. gl.RecurseOn();
  564. gl.SetRecurseListDirs(true);
  565. if (!gl.FindFiles(findExpr)) {
  566. cmCPackLogger(cmCPackLog::LOG_ERROR,
  567. "Cannot find any files in the installed directory"
  568. << std::endl);
  569. return 0;
  570. }
  571. packageFiles = gl.GetFiles();
  572. int res = createDeb();
  573. if (res != 1) {
  574. retval = 0;
  575. }
  576. // add the generated package to package file names list
  577. packageFileName = this->GetOption("CPACK_TOPLEVEL_DIRECTORY");
  578. packageFileName += "/";
  579. packageFileName += this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME");
  580. packageFileNames.push_back(std::move(packageFileName));
  581. return retval;
  582. }
  583. int cmCPackDebGenerator::PackageFiles()
  584. {
  585. /* Are we in the component packaging case */
  586. if (WantsComponentInstallation()) {
  587. // CASE 1 : COMPONENT ALL-IN-ONE package
  588. // If ALL GROUPS or ALL COMPONENTS in ONE package has been requested
  589. // then the package file is unique and should be open here.
  590. if (componentPackageMethod == ONE_PACKAGE) {
  591. return PackageComponentsAllInOne("ALL_COMPONENTS_IN_ONE");
  592. }
  593. // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
  594. // There will be 1 package for each component group
  595. // however one may require to ignore component group and
  596. // in this case you'll get 1 package for each component.
  597. return PackageComponents(componentPackageMethod ==
  598. ONE_PACKAGE_PER_COMPONENT);
  599. }
  600. // CASE 3 : NON COMPONENT package.
  601. return PackageComponentsAllInOne("");
  602. }
  603. int cmCPackDebGenerator::createDeb()
  604. {
  605. std::map<std::string, std::string> controlValues;
  606. // debian policy enforce lower case for package name
  607. controlValues["Package"] = cmsys::SystemTools::LowerCase(
  608. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME"));
  609. controlValues["Version"] =
  610. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  611. controlValues["Section"] =
  612. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SECTION");
  613. controlValues["Priority"] =
  614. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PRIORITY");
  615. controlValues["Architecture"] =
  616. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  617. controlValues["Maintainer"] =
  618. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  619. controlValues["Description"] =
  620. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DESCRIPTION");
  621. const char* debian_pkg_source =
  622. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  623. if (debian_pkg_source && *debian_pkg_source) {
  624. controlValues["Source"] = debian_pkg_source;
  625. }
  626. const char* debian_pkg_dep =
  627. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_DEPENDS");
  628. if (debian_pkg_dep && *debian_pkg_dep) {
  629. controlValues["Depends"] = debian_pkg_dep;
  630. }
  631. const char* debian_pkg_rec =
  632. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_RECOMMENDS");
  633. if (debian_pkg_rec && *debian_pkg_rec) {
  634. controlValues["Recommends"] = debian_pkg_rec;
  635. }
  636. const char* debian_pkg_sug =
  637. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SUGGESTS");
  638. if (debian_pkg_sug && *debian_pkg_sug) {
  639. controlValues["Suggests"] = debian_pkg_sug;
  640. }
  641. const char* debian_pkg_url =
  642. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_HOMEPAGE");
  643. if (debian_pkg_url && *debian_pkg_url) {
  644. controlValues["Homepage"] = debian_pkg_url;
  645. }
  646. const char* debian_pkg_predep =
  647. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PREDEPENDS");
  648. if (debian_pkg_predep && *debian_pkg_predep) {
  649. controlValues["Pre-Depends"] = debian_pkg_predep;
  650. }
  651. const char* debian_pkg_enhances =
  652. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ENHANCES");
  653. if (debian_pkg_enhances && *debian_pkg_enhances) {
  654. controlValues["Enhances"] = debian_pkg_enhances;
  655. }
  656. const char* debian_pkg_breaks =
  657. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_BREAKS");
  658. if (debian_pkg_breaks && *debian_pkg_breaks) {
  659. controlValues["Breaks"] = debian_pkg_breaks;
  660. }
  661. const char* debian_pkg_conflicts =
  662. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONFLICTS");
  663. if (debian_pkg_conflicts && *debian_pkg_conflicts) {
  664. controlValues["Conflicts"] = debian_pkg_conflicts;
  665. }
  666. const char* debian_pkg_provides =
  667. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_PROVIDES");
  668. if (debian_pkg_provides && *debian_pkg_provides) {
  669. controlValues["Provides"] = debian_pkg_provides;
  670. }
  671. const char* debian_pkg_replaces =
  672. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_REPLACES");
  673. if (debian_pkg_replaces && *debian_pkg_replaces) {
  674. controlValues["Replaces"] = debian_pkg_replaces;
  675. }
  676. const std::string strGenWDIR(this->GetOption("GEN_WDIR"));
  677. const std::string shlibsfilename = strGenWDIR + "/shlibs";
  678. const char* debian_pkg_shlibs =
  679. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SHLIBS");
  680. const bool gen_shibs = this->IsOn("CPACK_DEBIAN_PACKAGE_GENERATE_SHLIBS") &&
  681. debian_pkg_shlibs && *debian_pkg_shlibs;
  682. if (gen_shibs) {
  683. cmGeneratedFileStream out(shlibsfilename);
  684. out << debian_pkg_shlibs;
  685. out << std::endl;
  686. }
  687. const std::string postinst = strGenWDIR + "/postinst";
  688. const std::string postrm = strGenWDIR + "/postrm";
  689. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST")) {
  690. cmGeneratedFileStream out(postinst);
  691. out << "#!/bin/sh\n\n"
  692. "set -e\n\n"
  693. "if [ \"$1\" = \"configure\" ]; then\n"
  694. "\tldconfig\n"
  695. "fi\n";
  696. }
  697. if (this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM")) {
  698. cmGeneratedFileStream out(postrm);
  699. out << "#!/bin/sh\n\n"
  700. "set -e\n\n"
  701. "if [ \"$1\" = \"remove\" ]; then\n"
  702. "\tldconfig\n"
  703. "fi\n";
  704. }
  705. DebGenerator gen(
  706. Logger, this->GetOption("GEN_CPACK_OUTPUT_FILE_NAME"), strGenWDIR,
  707. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  708. this->GetOption("CPACK_TEMPORARY_DIRECTORY"),
  709. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE"),
  710. this->GetOption("GEN_CPACK_DEBIAN_ARCHIVE_TYPE"), controlValues, gen_shibs,
  711. shlibsfilename, this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTINST"), postinst,
  712. this->IsOn("GEN_CPACK_DEBIAN_GENERATE_POSTRM"), postrm,
  713. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA"),
  714. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION"),
  715. packageFiles);
  716. if (!gen.generate()) {
  717. return 0;
  718. }
  719. return 1;
  720. }
  721. int cmCPackDebGenerator::createDbgsymDDeb()
  722. {
  723. // Packages containing debug symbols follow the same structure as .debs
  724. // but have different metadata and content.
  725. std::map<std::string, std::string> controlValues;
  726. // debian policy enforce lower case for package name
  727. std::string packageNameLower = cmsys::SystemTools::LowerCase(
  728. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME"));
  729. const char* debian_pkg_version =
  730. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_VERSION");
  731. controlValues["Package"] = packageNameLower + "-dbgsym";
  732. controlValues["Package-Type"] = "ddeb";
  733. controlValues["Version"] = debian_pkg_version;
  734. controlValues["Auto-Built-Package"] = "debug-symbols";
  735. controlValues["Depends"] = this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_NAME") +
  736. std::string(" (= ") + debian_pkg_version + ")";
  737. controlValues["Section"] = "debug";
  738. controlValues["Priority"] = "optional";
  739. controlValues["Architecture"] =
  740. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_ARCHITECTURE");
  741. controlValues["Maintainer"] =
  742. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_MAINTAINER");
  743. controlValues["Description"] =
  744. std::string("debug symbols for ") + packageNameLower;
  745. const char* debian_pkg_source =
  746. this->GetOption("GEN_CPACK_DEBIAN_PACKAGE_SOURCE");
  747. if (debian_pkg_source && *debian_pkg_source) {
  748. controlValues["Source"] = debian_pkg_source;
  749. }
  750. const char* debian_build_ids = this->GetOption("GEN_BUILD_IDS");
  751. if (debian_build_ids && *debian_build_ids) {
  752. controlValues["Build-Ids"] = debian_build_ids;
  753. }
  754. DebGenerator gen(
  755. Logger, this->GetOption("GEN_CPACK_DBGSYM_OUTPUT_FILE_NAME"),
  756. this->GetOption("GEN_DBGSYMDIR"),
  757. this->GetOption("CPACK_TOPLEVEL_DIRECTORY"),
  758. this->GetOption("CPACK_TEMPORARY_DIRECTORY"),
  759. this->GetOption("GEN_CPACK_DEBIAN_COMPRESSION_TYPE"),
  760. this->GetOption("GEN_CPACK_DEBIAN_ARCHIVE_TYPE"), controlValues, false, "",
  761. false, "", false, "", nullptr,
  762. this->IsSet("GEN_CPACK_DEBIAN_PACKAGE_CONTROL_STRICT_PERMISSION"),
  763. packageFiles);
  764. if (!gen.generate()) {
  765. return 0;
  766. }
  767. return 1;
  768. }
  769. bool cmCPackDebGenerator::SupportsComponentInstallation() const
  770. {
  771. return IsOn("CPACK_DEB_COMPONENT_INSTALL");
  772. }
  773. std::string cmCPackDebGenerator::GetComponentInstallDirNameSuffix(
  774. const std::string& componentName)
  775. {
  776. if (componentPackageMethod == ONE_PACKAGE_PER_COMPONENT) {
  777. return componentName;
  778. }
  779. if (componentPackageMethod == ONE_PACKAGE) {
  780. return std::string("ALL_COMPONENTS_IN_ONE");
  781. }
  782. // We have to find the name of the COMPONENT GROUP
  783. // the current COMPONENT belongs to.
  784. std::string groupVar =
  785. "CPACK_COMPONENT_" + cmSystemTools::UpperCase(componentName) + "_GROUP";
  786. if (nullptr != GetOption(groupVar)) {
  787. return std::string(GetOption(groupVar));
  788. }
  789. return componentName;
  790. }