Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Source / CPack / cmCPackFreeBSDGenerator.cxx
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 "cmCPackFreeBSDGenerator.h"
4
5 #include "cmArchiveWrite.h"
6 #include "cmCPackArchiveGenerator.h"
7 #include "cmCPackLog.h"
8 #include "cmGeneratedFileStream.h"
9 #include "cmStringAlgorithms.h"
10 #include "cmSystemTools.h"
11 #include "cmWorkingDirectory.h"
12
13 // Needed for ::open() and ::stat()
14 #include <algorithm>
15 #include <ostream>
16 #include <utility>
17 #include <vector>
18
19 #include <fcntl.h>
20 #include <pkg.h>
21
22 #include <sys/stat.h>
23
24 // Suffix used to tell libpkg what compression to use
25 static const char FreeBSDPackageCompression[] = "txz";
26 static const char FreeBSDPackageSuffix_17[] = ".pkg";
27
28 cmCPackFreeBSDGenerator::cmCPackFreeBSDGenerator()
29   : cmCPackArchiveGenerator(cmArchiveWrite::CompressXZ, "paxr",
30                             FreeBSDPackageSuffix_17)
31 {
32 }
33
34 int cmCPackFreeBSDGenerator::InitializeInternal()
35 {
36   this->SetOptionIfNotSet("CPACK_PACKAGING_INSTALL_PREFIX", "/usr/local");
37   this->SetOption("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
38   return this->Superclass::InitializeInternal();
39 }
40
41 cmCPackFreeBSDGenerator::~cmCPackFreeBSDGenerator() = default;
42
43 // This is a wrapper for struct pkg_create and pkg_create()
44 //
45 // Instantiate this class with suitable parameters, then
46 // check isValid() to check if it's ok. Afterwards, call
47 // Create() to do the actual work. This will leave a package
48 // in the given `output_dir`.
49 //
50 // This wrapper cleans up the struct pkg_create.
51 class PkgCreate
52 {
53 public:
54   PkgCreate()
55     : d(nullptr)
56   {
57   }
58   PkgCreate(const std::string& output_dir, const std::string& toplevel_dir,
59             const std::string& manifest_name)
60     : d(pkg_create_new())
61     , manifest(manifest_name)
62
63   {
64     if (d) {
65       pkg_create_set_format(d, FreeBSDPackageCompression);
66       pkg_create_set_compression_level(d, 0); // Explicitly set default
67       pkg_create_set_overwrite(d, false);
68       pkg_create_set_rootdir(d, toplevel_dir.c_str());
69       pkg_create_set_output_dir(d, output_dir.c_str());
70     }
71   }
72   ~PkgCreate()
73   {
74     if (d)
75       pkg_create_free(d);
76   }
77
78   bool isValid() const { return d; }
79
80   bool Create()
81   {
82     if (!isValid())
83       return false;
84     // The API in the FreeBSD sources (the header has no documentation),
85     // is as follows:
86     //
87     // int pkg_create(struct pkg_create *pc, const char *metadata, const char
88     // *plist, bool hash)
89     //
90     // We let the plist be determined from what is installed, and all
91     // the rest comes from the manifest data.
92     int r = pkg_create(d, manifest.c_str(), nullptr, false);
93     return r == 0;
94   }
95
96 private:
97   struct pkg_create* d;
98   std::string manifest;
99 };
100
101 // This is a wrapper, for use only in stream-based output,
102 // that will output a string in UCL escaped fashion (in particular,
103 // quotes and backslashes are escaped). The list of characters
104 // to escape is taken from https://github.com/vstakhov/libucl
105 // (which is the reference implementation pkg(8) refers to).
106 class EscapeQuotes
107 {
108 public:
109   const std::string& value;
110
111   EscapeQuotes(const std::string& s)
112     : value(s)
113   {
114   }
115 };
116
117 // Output a string as "string" with escaping applied.
118 cmGeneratedFileStream& operator<<(cmGeneratedFileStream& s,
119                                   const EscapeQuotes& v)
120 {
121   s << '"';
122   for (char c : v.value) {
123     switch (c) {
124       case '\n':
125         s << "\\n";
126         break;
127       case '\r':
128         s << "\\r";
129         break;
130       case '\b':
131         s << "\\b";
132         break;
133       case '\t':
134         s << "\\t";
135         break;
136       case '\f':
137         s << "\\f";
138         break;
139       case '\\':
140         s << "\\\\";
141         break;
142       case '"':
143         s << "\\\"";
144         break;
145       default:
146         s << c;
147         break;
148     }
149   }
150   s << '"';
151   return s;
152 }
153
154 // The following classes are all helpers for writing out the UCL
155 // manifest file (it also looks like JSON). ManifestKey just has
156 // a (string-valued) key; subclasses add a specific kind of
157 // value-type to the key, and implement write_value() to output
158 // the corresponding UCL.
159 class ManifestKey
160 {
161 public:
162   std::string key;
163
164   ManifestKey(std::string k)
165     : key(std::move(k))
166   {
167   }
168
169   virtual ~ManifestKey() = default;
170
171   // Output the value associated with this key to the stream @p s.
172   // Format is to be decided by subclasses.
173   virtual void write_value(cmGeneratedFileStream& s) const = 0;
174 };
175
176 // Basic string-value (e.g. "name": "cmake")
177 class ManifestKeyValue : public ManifestKey
178 {
179 public:
180   std::string value;
181
182   ManifestKeyValue(const std::string& k, std::string v)
183     : ManifestKey(k)
184     , value(std::move(v))
185   {
186   }
187
188   void write_value(cmGeneratedFileStream& s) const override
189   {
190     s << EscapeQuotes(value);
191   }
192 };
193
194 // List-of-strings values (e.g. "licenses": ["GPLv2", "LGPLv2"])
195 class ManifestKeyListValue : public ManifestKey
196 {
197 public:
198   using VList = std::vector<std::string>;
199   VList value;
200
201   ManifestKeyListValue(const std::string& k)
202     : ManifestKey(k)
203   {
204   }
205
206   ManifestKeyListValue& operator<<(const std::string& v)
207   {
208     value.push_back(v);
209     return *this;
210   }
211
212   ManifestKeyListValue& operator<<(const std::vector<std::string>& v)
213   {
214     for (std::string const& e : v) {
215       (*this) << e;
216     }
217     return *this;
218   }
219
220   void write_value(cmGeneratedFileStream& s) const override
221   {
222     bool with_comma = false;
223
224     s << '[';
225     for (std::string const& elem : value) {
226       s << (with_comma ? ',' : ' ');
227       s << EscapeQuotes(elem);
228       with_comma = true;
229     }
230     s << " ]";
231   }
232 };
233
234 // Deps: actually a dictionary, but we'll treat it as a
235 // list so we only name the deps, and produce dictionary-
236 // like output via write_value()
237 class ManifestKeyDepsValue : public ManifestKeyListValue
238 {
239 public:
240   ManifestKeyDepsValue(const std::string& k)
241     : ManifestKeyListValue(k)
242   {
243   }
244
245   void write_value(cmGeneratedFileStream& s) const override
246   {
247     s << "{\n";
248     for (std::string const& elem : value) {
249       s << "  \"" << elem << R"(": {"origin": ")" << elem << "\"},\n";
250     }
251     s << '}';
252   }
253 };
254
255 // Write one of the key-value classes (above) to the stream @p s
256 cmGeneratedFileStream& operator<<(cmGeneratedFileStream& s,
257                                   const ManifestKey& v)
258 {
259   s << '"' << v.key << "\": ";
260   v.write_value(s);
261   s << ",\n";
262   return s;
263 }
264
265 // Look up variable; if no value is set, returns an empty string;
266 // basically a wrapper that handles the NULL-ptr return from GetOption().
267 std::string cmCPackFreeBSDGenerator::var_lookup(const char* var_name)
268 {
269   cmValue pv = this->GetOption(var_name);
270   if (!pv) {
271     return {};
272   }
273   return *pv;
274 }
275
276 // Produce UCL in the given @p manifest file for the common
277 // manifest fields (common to the compact and regular formats),
278 // by reading the CPACK_FREEBSD_* variables.
279 void cmCPackFreeBSDGenerator::write_manifest_fields(
280   cmGeneratedFileStream& manifest)
281 {
282   manifest << ManifestKeyValue("name",
283                                var_lookup("CPACK_FREEBSD_PACKAGE_NAME"));
284   manifest << ManifestKeyValue("origin",
285                                var_lookup("CPACK_FREEBSD_PACKAGE_ORIGIN"));
286   manifest << ManifestKeyValue("version",
287                                var_lookup("CPACK_FREEBSD_PACKAGE_VERSION"));
288   manifest << ManifestKeyValue("maintainer",
289                                var_lookup("CPACK_FREEBSD_PACKAGE_MAINTAINER"));
290   manifest << ManifestKeyValue("comment",
291                                var_lookup("CPACK_FREEBSD_PACKAGE_COMMENT"));
292   manifest << ManifestKeyValue(
293     "desc", var_lookup("CPACK_FREEBSD_PACKAGE_DESCRIPTION"));
294   manifest << ManifestKeyValue("www", var_lookup("CPACK_FREEBSD_PACKAGE_WWW"));
295   std::vector<std::string> licenses =
296     cmExpandedList(var_lookup("CPACK_FREEBSD_PACKAGE_LICENSE"));
297   std::string licenselogic("single");
298   if (licenses.empty()) {
299     cmSystemTools::SetFatalErrorOccurred();
300   } else if (licenses.size() > 1) {
301     licenselogic = var_lookup("CPACK_FREEBSD_PACKAGE_LICENSE_LOGIC");
302   }
303   manifest << ManifestKeyValue("licenselogic", licenselogic);
304   manifest << (ManifestKeyListValue("licenses") << licenses);
305   std::vector<std::string> categories =
306     cmExpandedList(var_lookup("CPACK_FREEBSD_PACKAGE_CATEGORIES"));
307   manifest << (ManifestKeyListValue("categories") << categories);
308   manifest << ManifestKeyValue("prefix", var_lookup("CMAKE_INSTALL_PREFIX"));
309   std::vector<std::string> deps =
310     cmExpandedList(var_lookup("CPACK_FREEBSD_PACKAGE_DEPS"));
311   if (!deps.empty()) {
312     manifest << (ManifestKeyDepsValue("deps") << deps);
313   }
314 }
315
316 // Package only actual files; others are ignored (in particular,
317 // intermediate subdirectories are ignored).
318 static bool ignore_file(const std::string& filename)
319 {
320   struct stat statbuf;
321   return stat(filename.c_str(), &statbuf) < 0 ||
322     (statbuf.st_mode & S_IFMT) != S_IFREG;
323 }
324
325 // Write the given list of @p files to the manifest stream @p s,
326 // as the UCL field "files" (which is dictionary-valued, to
327 // associate filenames with hashes). All the files are transformed
328 // to paths relative to @p toplevel, with a leading / (since the paths
329 // in FreeBSD package files are supposed to be absolute).
330 void write_manifest_files(cmGeneratedFileStream& s,
331                           const std::string& toplevel,
332                           const std::vector<std::string>& files)
333 {
334   s << "\"files\": {\n";
335   for (std::string const& file : files) {
336     s << "  \"/" << cmSystemTools::RelativePath(toplevel, file) << "\": \""
337       << "<sha256>" // this gets replaced by libpkg by the actual SHA256
338       << "\",\n";
339   }
340   s << "  },\n";
341 }
342
343 int cmCPackFreeBSDGenerator::PackageFiles()
344 {
345   if (!this->ReadListFile("Internal/CPack/CPackFreeBSD.cmake")) {
346     cmCPackLogger(cmCPackLog::LOG_ERROR,
347                   "Error while executing CPackFreeBSD.cmake" << std::endl);
348     return 0;
349   }
350
351   cmWorkingDirectory wd(toplevel);
352
353   files.erase(std::remove_if(files.begin(), files.end(), ignore_file),
354               files.end());
355
356   std::string manifestname = toplevel + "/+MANIFEST";
357   {
358     cmGeneratedFileStream manifest(manifestname);
359     manifest << "{\n";
360     write_manifest_fields(manifest);
361     write_manifest_files(manifest, toplevel, files);
362     manifest << "}\n";
363   }
364
365   cmCPackLogger(cmCPackLog::LOG_DEBUG, "Toplevel: " << toplevel << std::endl);
366
367   if (WantsComponentInstallation()) {
368     // CASE 1 : COMPONENT ALL-IN-ONE package
369     // If ALL COMPONENTS in ONE package has been requested
370     // then the package file is unique and should be open here.
371     if (componentPackageMethod == ONE_PACKAGE) {
372       return PackageComponentsAllInOne();
373     }
374     // CASE 2 : COMPONENT CLASSICAL package(s) (i.e. not all-in-one)
375     // There will be 1 package for each component group
376     // however one may require to ignore component group and
377     // in this case you'll get 1 package for each component.
378     return PackageComponents(componentPackageMethod ==
379                              ONE_PACKAGE_PER_COMPONENT);
380   }
381
382   // There should be one name in the packageFileNames (already, see comment
383   // in cmCPackGenerator::DoPackage(), which holds what CPack guesses
384   // will be the package filename. libpkg does something else, though,
385   // so update the single filename to what we know will be right.
386   if (this->packageFileNames.size() == 1) {
387     std::string currentPackage = this->packageFileNames[0];
388     auto lastSlash = currentPackage.rfind('/');
389
390     // If there is a pathname, preserve that; libpkg will write out
391     // a file with the package name and version as specified in the
392     // manifest, so we look those up (again). lastSlash is the slash
393     // itself, we need that as path separator to the calculated package name.
394     std::string actualPackage =
395       ((lastSlash != std::string::npos)
396          ? std::string(currentPackage, 0, lastSlash + 1)
397          : std::string()) +
398       var_lookup("CPACK_FREEBSD_PACKAGE_NAME") + '-' +
399       var_lookup("CPACK_FREEBSD_PACKAGE_VERSION") + FreeBSDPackageSuffix_17;
400
401     this->packageFileNames.clear();
402     this->packageFileNames.emplace_back(actualPackage);
403   }
404
405   if (!pkg_initialized() && pkg_init(NULL, NULL) != EPKG_OK) {
406     cmCPackLogger(cmCPackLog::LOG_ERROR,
407                   "Can not initialize FreeBSD libpkg." << std::endl);
408     return 0;
409   }
410
411   const std::string output_dir =
412     cmSystemTools::CollapseFullPath("../", toplevel);
413   PkgCreate package(output_dir, toplevel, manifestname);
414   if (package.isValid()) {
415     if (!package.Create()) {
416       cmCPackLogger(cmCPackLog::LOG_ERROR,
417                     "Error during pkg_create()" << std::endl);
418       return 0;
419     }
420   } else {
421     cmCPackLogger(cmCPackLog::LOG_ERROR,
422                   "Error before pkg_create()" << std::endl);
423     return 0;
424   }
425
426   // Specifically looking for packages suffixed with the TAG
427   std::string broken_suffix_17 =
428     cmStrCat('-', var_lookup("CPACK_TOPLEVEL_TAG"), FreeBSDPackageSuffix_17);
429   for (std::string& name : packageFileNames) {
430     cmCPackLogger(cmCPackLog::LOG_DEBUG, "Packagefile " << name << std::endl);
431     if (cmHasSuffix(name, broken_suffix_17)) {
432       name.replace(name.size() - broken_suffix_17.size(), std::string::npos,
433                    FreeBSDPackageSuffix_17);
434       break;
435     }
436   }
437
438   const std::string packageFileName =
439     var_lookup("CPACK_PACKAGE_FILE_NAME") + FreeBSDPackageSuffix_17;
440   if (packageFileNames.size() == 1 && !packageFileName.empty() &&
441       packageFileNames[0] != packageFileName) {
442     // Since libpkg always writes <name>-<version>.<suffix>,
443     // if there is a CPACK_PACKAGE_FILE_NAME set, we need to
444     // rename, and then re-set the name.
445     const std::string sourceFile = packageFileNames[0];
446     const std::string packageSubDirectory =
447       cmSystemTools::GetParentDirectory(sourceFile);
448     const std::string targetFileName =
449       packageSubDirectory + '/' + packageFileName;
450     if (cmSystemTools::RenameFile(sourceFile, targetFileName)) {
451       this->packageFileNames.clear();
452       this->packageFileNames.emplace_back(targetFileName);
453     }
454   }
455
456   return 1;
457 }