CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / CPack / cmCPackSTGZGenerator.cxx
blob2673feefe5b02c21a8f6b2266dbd79f88ebe7627
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 "cmCPackSTGZGenerator.h"
5 #include <cstdio>
6 #include <string>
7 #include <vector>
9 #include <fcntl.h>
11 #include "cmsys/FStream.hxx"
13 #include "cm_sys_stat.h"
15 #include "cmArchiveWrite.h"
16 #include "cmCPackGenerator.h"
17 #include "cmCPackLog.h"
18 #include "cmSystemTools.h"
19 #include "cmValue.h"
21 cmCPackSTGZGenerator::cmCPackSTGZGenerator()
22 : cmCPackArchiveGenerator(cmArchiveWrite::CompressGZip, "paxr", ".sh")
26 cmCPackSTGZGenerator::~cmCPackSTGZGenerator() = default;
28 int cmCPackSTGZGenerator::InitializeInternal()
30 this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
32 std::string inFile = this->FindTemplate("CPack.STGZ_Header.sh.in");
33 if (inFile.empty()) {
34 cmCPackLogger(cmCPackLog::LOG_ERROR,
35 "Cannot find template file: " << inFile << std::endl);
36 return 0;
38 this->SetOptionIfNotSet("CPACK_STGZ_HEADER_FILE", inFile);
39 this->SetOptionIfNotSet("CPACK_AT_SIGN", "@");
41 return this->Superclass::InitializeInternal();
44 int cmCPackSTGZGenerator::PackageFiles()
46 bool retval = true;
47 if (!this->Superclass::PackageFiles()) {
48 return 0;
51 /* TGZ generator (our Superclass) may
52 * have generated several packages (component packaging)
53 * so we must iterate over generated packages.
55 for (std::string const& pfn : this->packageFileNames) {
56 retval &= static_cast<bool>(
57 cmSystemTools::SetPermissions(pfn.c_str(),
58 #if defined(_MSC_VER) || defined(__MINGW32__)
59 S_IREAD | S_IWRITE | S_IEXEC
60 #else
61 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
62 S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH |
63 S_IXOTH
64 #endif
65 ));
67 return retval;
70 int cmCPackSTGZGenerator::GenerateHeader(std::ostream* os)
72 cmCPackLogger(cmCPackLog::LOG_DEBUG, "Writing header" << std::endl);
73 int counter = 0;
75 std::string inLicFile = this->GetOption("CPACK_RESOURCE_FILE_LICENSE");
76 std::string line;
77 cmsys::ifstream ilfs(inLicFile.c_str());
78 std::string licenseText;
79 while (cmSystemTools::GetLineFromStream(ilfs, line)) {
80 licenseText += line + "\n";
82 this->SetOptionIfNotSet("CPACK_RESOURCE_FILE_LICENSE_CONTENT", licenseText);
84 const char headerLengthTag[] = "###CPACK_HEADER_LENGTH###";
86 // Create the header
87 std::string inFile = this->GetOption("CPACK_STGZ_HEADER_FILE");
88 cmsys::ifstream ifs(inFile.c_str());
89 std::string packageHeaderText;
90 while (cmSystemTools::GetLineFromStream(ifs, line)) {
91 packageHeaderText += line + "\n";
94 // Configure in the values
95 std::string res;
96 this->ConfigureString(packageHeaderText, res);
98 // Count the lines
99 const char* ptr = res.c_str();
100 while (*ptr) {
101 if (*ptr == '\n') {
102 counter++;
104 ++ptr;
106 counter++;
107 cmCPackLogger(cmCPackLog::LOG_DEBUG,
108 "Number of lines: " << counter << std::endl);
109 char buffer[1024];
110 snprintf(buffer, sizeof(buffer), "%d", counter);
111 cmSystemTools::ReplaceString(res, headerLengthTag, buffer);
113 // Write in file
114 *os << res;
115 return this->Superclass::GenerateHeader(os);