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