Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Source/CPack/cmCPackGenerator.h
4998 views
1
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2
file LICENSE.rst or https://cmake.org/licensing for details. */
3
#pragma once
4
5
#include "cmConfigure.h" // IWYU pragma: keep
6
7
#include <map>
8
#include <sstream>
9
#include <string>
10
#include <vector>
11
12
#include <cm/optional>
13
#include <cm/string_view>
14
15
#include "cm_sys_stat.h"
16
17
#include "cmCPackComponentGroup.h"
18
#include "cmSystemTools.h"
19
#include "cmValue.h"
20
21
class cmCPackLog;
22
class cmCryptoHash;
23
class cmGlobalGenerator;
24
class cmInstalledFile;
25
class cmMakefile;
26
27
/** \class cmCPackGenerator
28
* \brief A superclass of all CPack Generators
29
*
30
*/
31
class cmCPackGenerator
32
{
33
public:
34
virtual char const* GetNameOfClass() = 0;
35
/**
36
* If verbose then more information is printed out
37
*/
38
void SetVerbose(bool val)
39
{
40
this->GeneratorVerbose =
41
val ? cmSystemTools::OUTPUT_MERGE : cmSystemTools::OUTPUT_NONE;
42
}
43
44
/**
45
* Put underlying cmake scripts in trace mode.
46
*/
47
void SetTrace(bool val) { this->Trace = val; }
48
49
/**
50
* Put underlying cmake scripts in expanded trace mode.
51
*/
52
void SetTraceExpand(bool val) { this->TraceExpand = val; }
53
54
/**
55
* Returns true if the generator may work on this system.
56
* Rational:
57
* Some CPack generator may run on some host and may not on others
58
* (with the same system) because some tools are missing. If the tool
59
* is missing then CPack won't activate (in the CPackGeneratorFactory)
60
* this particular generator.
61
*/
62
static bool CanGenerate() { return true; }
63
64
/**
65
* Do the actual whole package processing.
66
* Subclass may redefine it but its usually enough
67
* to redefine @ref PackageFiles, because in fact
68
* this method do call:
69
* - PrepareName
70
* - clean-up temp dirs
71
* - InstallProject (with the appropriate method)
72
* - prepare list of files and/or components to be package
73
* - PackageFiles
74
* - Copy produced packages at the expected place
75
* @return 0 if error.
76
*/
77
virtual int DoPackage();
78
79
/**
80
* Initialize generator
81
*/
82
int Initialize(std::string const& name, cmMakefile* mf);
83
84
/**
85
* Construct generator
86
*/
87
cmCPackGenerator();
88
virtual ~cmCPackGenerator();
89
90
//! Set and get the options
91
void SetOption(std::string const& op, char const* value);
92
void SetOption(std::string const& op, std::string const& value)
93
{
94
this->SetOption(op, cmValue(value));
95
}
96
void SetOption(std::string const& op, cmValue value);
97
void SetOptionIfNotSet(std::string const& op, char const* value);
98
void SetOptionIfNotSet(std::string const& op, std::string const& value)
99
{
100
this->SetOptionIfNotSet(op, cmValue(value));
101
}
102
void SetOptionIfNotSet(std::string const& op, cmValue value);
103
cmValue GetOption(std::string const& op) const;
104
std::vector<std::string> GetOptions() const;
105
bool IsSet(std::string const& name) const;
106
cmValue GetOptionIfSet(std::string const& name) const;
107
bool IsOn(std::string const& name) const;
108
bool IsSetToOff(std::string const& op) const;
109
bool IsSetToEmpty(std::string const& op) const;
110
111
//! Set the logger
112
void SetLogger(cmCPackLog* log) { this->Logger = log; }
113
114
//! Display verbose information via logger
115
void DisplayVerboseOutput(std::string const& msg, float progress);
116
117
bool ReadListFile(char const* moduleName);
118
119
protected:
120
/**
121
* Prepare common used names by inspecting
122
* several CPACK_xxx var values.
123
*/
124
int PrepareNames();
125
126
/**
127
* Install the project using appropriate method.
128
*/
129
int InstallProject();
130
131
int CleanTemporaryDirectory();
132
133
cmInstalledFile const* GetInstalledFile(std::string const& name) const;
134
135
virtual char const* GetOutputExtension() { return ".cpack"; }
136
virtual char const* GetOutputPostfix() { return nullptr; }
137
138
/**
139
* Prepare requested grouping kind from CPACK_xxx vars
140
* CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE
141
* CPACK_COMPONENTS_IGNORE_GROUPS
142
* or
143
* CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP
144
* @return 1 on success 0 on failure.
145
*/
146
virtual int PrepareGroupingKind();
147
148
/**
149
* Ensures that the given name only contains characters that can cleanly be
150
* used as directory or file name and returns this sanitized name. Possibly,
151
* this name might be replaced by its hash.
152
* @param[in] name the name for a directory or file that shall be sanitized.
153
* @param[in] isFullName true if the result is used as the full name for a
154
* directory or file. (Defaults to true.)
155
* @return the sanitized name.
156
*/
157
virtual std::string GetSanitizedDirOrFileName(std::string const& name,
158
bool isFullName = true) const;
159
160
/**
161
* Some CPack generators may prefer to have
162
* CPack install all components belonging to the same
163
* [component] group to be install in the same directory.
164
* The default behavior is to install each component in
165
* a separate directory.
166
* @param[in] componentName the name of the component to be installed
167
* @return the name suffix the generator wants for the specified component
168
* default is "componentName"
169
*/
170
virtual std::string GetComponentInstallSuffix(
171
std::string const& componentName);
172
173
/**
174
* The value that GetComponentInstallSuffix returns, but sanitized.
175
* @param[in] componentName the name of the component to be installed
176
* @return the name suffix the generator wants for the specified component
177
* (but sanitized, so that it can be used on the file-system).
178
* default is "componentName".
179
*/
180
virtual std::string GetComponentInstallDirNameSuffix(
181
std::string const& componentName);
182
183
/**
184
* CPack specific generator may mangle CPACK_PACKAGE_FILE_NAME
185
* with CPACK_COMPONENT_xxxx_<NAME>_DISPLAY_NAME if
186
* CPACK_<GEN>_USE_DISPLAY_NAME_IN_FILENAME is ON.
187
* @param[in] initialPackageFileName the initial package name to be mangled
188
* @param[in] groupOrComponentName the name of the group/component
189
* @param[in] isGroupName true if previous name refers to a group,
190
* false otherwise
191
*/
192
virtual std::string GetComponentPackageFileName(
193
std::string const& initialPackageFileName,
194
std::string const& groupOrComponentName, bool isGroupName);
195
196
/**
197
* Package the list of files and/or components which
198
* has been prepared by the beginning of DoPackage.
199
* @pre the @ref toplevel has been filled-in
200
* @pre the list of file @ref files has been populated
201
* @pre packageFileNames contains at least 1 entry
202
* @post packageFileNames may have been updated and contains
203
* the list of packages generated by the specific generator.
204
*/
205
virtual int PackageFiles();
206
virtual char const* GetInstallPath();
207
virtual char const* GetPackagingInstallPrefix();
208
209
bool GenerateChecksumFile(cmCryptoHash& crypto,
210
cm::string_view filename) const;
211
bool CopyPackageFile(std::string const& srcFilePath,
212
cm::string_view filename) const;
213
214
std::string FindTemplate(cm::string_view name,
215
cm::optional<cm::string_view> alt = cm::nullopt);
216
virtual bool ConfigureFile(std::string const& inName,
217
std::string const& outName,
218
bool copyOnly = false);
219
virtual bool ConfigureString(std::string const& input, std::string& output);
220
virtual int InitializeInternal();
221
222
//! Run install commands if specified
223
virtual int InstallProjectViaInstallCommands(
224
bool setDestDir, std::string const& tempInstallDirectory);
225
virtual int InstallProjectViaInstallScript(
226
bool setDestDir, std::string const& tempInstallDirectory);
227
virtual int InstallProjectViaInstalledDirectories(
228
bool setDestDir, std::string const& tempInstallDirectory,
229
mode_t const* default_dir_mode);
230
virtual int InstallProjectViaInstallCMakeProjects(
231
bool setDestDir, std::string const& tempInstallDirectory,
232
mode_t const* default_dir_mode);
233
234
virtual int RunPreinstallTarget(std::string const& installProjectName,
235
std::string const& installDirectory,
236
cmGlobalGenerator* globalGenerator,
237
std::string const& buildConfig);
238
virtual int InstallCMakeProject(
239
bool setDestDir, std::string const& installDirectory,
240
std::string const& baseTempInstallDirectory,
241
mode_t const* default_dir_mode, std::string const& component,
242
bool componentInstall, std::string const& installSubDirectory,
243
std::string const& buildConfig, std::string& absoluteDestFiles);
244
245
/**
246
* The various level of support of
247
* CPACK_SET_DESTDIR used by the generator.
248
*/
249
enum CPackSetDestdirSupport
250
{
251
/* the generator works with or without it */
252
SETDESTDIR_SUPPORTED,
253
/* the generator works best if automatically handled */
254
SETDESTDIR_INTERNALLY_SUPPORTED,
255
/* no official support, use at your own risk */
256
SETDESTDIR_SHOULD_NOT_BE_USED,
257
/* officially NOT supported */
258
SETDESTDIR_UNSUPPORTED
259
};
260
261
/**
262
* Does the CPack generator support CPACK_SET_DESTDIR?
263
* The default legacy value is 'SETDESTDIR_SUPPORTED' generator
264
* have to override it in order change this.
265
* @return CPackSetDestdirSupport
266
*/
267
virtual enum CPackSetDestdirSupport SupportsSetDestdir() const;
268
269
/**
270
* Does the CPack generator support absolute path
271
* in INSTALL DESTINATION?
272
* The default legacy value is 'true' generator
273
* have to override it in order change this.
274
* @return true if supported false otherwise
275
*/
276
virtual bool SupportsAbsoluteDestination() const;
277
278
/**
279
* Does the CPack generator support component installation?.
280
* Some Generators requires the user to set
281
* CPACK_<GENNAME>_COMPONENT_INSTALL in order to make this
282
* method return true.
283
* @return true if supported, false otherwise
284
*/
285
virtual bool SupportsComponentInstallation() const;
286
/**
287
* Does the currently running generator want a component installation.
288
* The generator may support component installation but he may
289
* be requiring monolithic install using CPACK_MONOLITHIC_INSTALL.
290
* @return true if component installation is supported and wanted.
291
*/
292
virtual bool WantsComponentInstallation() const;
293
virtual cmCPackInstallationType* GetInstallationType(
294
std::string const& projectName, std::string const& name);
295
virtual cmCPackComponent* GetComponent(std::string const& projectName,
296
std::string const& name);
297
virtual cmCPackComponentGroup* GetComponentGroup(
298
std::string const& projectName, std::string const& name);
299
300
cmSystemTools::OutputOption GeneratorVerbose;
301
std::string Name;
302
303
std::string InstallPath;
304
305
/**
306
* The list of package file names.
307
* At beginning of DoPackage the (generic) generator will populate
308
* the list of desired package file names then it will
309
* call the redefined method PackageFiles which is may
310
* either use this set of names (usually on entry there should be
311
* only a single name) or update the vector with the list
312
* of created package file names.
313
*/
314
std::vector<std::string> packageFileNames;
315
316
/**
317
* The directory where all the files to be packaged reside.
318
* If the installer support components there will be one
319
* sub-directory for each component. In those directories
320
* one will find the file belonging to the specified component.
321
*/
322
std::string toplevel;
323
324
/**
325
* The complete list of files to be packaged.
326
* This list will be populated by DoPackage before
327
* PackageFiles is called.
328
*/
329
std::vector<std::string> files;
330
331
std::vector<cmCPackInstallCMakeProject> CMakeProjects;
332
std::map<std::string, cmCPackInstallationType> InstallationTypes;
333
/**
334
* The set of components.
335
* If component installation is supported then this map
336
* contains the component specified in CPACK_COMPONENTS_ALL
337
*/
338
std::map<std::string, cmCPackComponent> Components;
339
std::map<std::string, cmCPackComponentGroup> ComponentGroups;
340
341
/**
342
* If components are enabled, this enum represents the different
343
* ways of mapping components to package files.
344
*/
345
enum ComponentPackageMethod
346
{
347
/* one package for all components */
348
ONE_PACKAGE,
349
/* one package for each component */
350
ONE_PACKAGE_PER_COMPONENT,
351
/* one package for each group,
352
* with left over components in their own package */
353
ONE_PACKAGE_PER_GROUP,
354
UNKNOWN_COMPONENT_PACKAGE_METHOD
355
};
356
357
/**
358
* The component package method
359
* The default is ONE_PACKAGE_PER_GROUP,
360
* and generators may override the default
361
* before PrepareGroupingKind() is called.
362
*/
363
ComponentPackageMethod componentPackageMethod;
364
365
cmCPackLog* Logger;
366
bool Trace;
367
bool TraceExpand;
368
369
cmMakefile* MakefileMap;
370
371
private:
372
template <typename ValueType>
373
void StoreOption(std::string const& op, ValueType value);
374
template <typename ValueType>
375
void StoreOptionIfNotSet(std::string const& op, ValueType value);
376
};
377
378
#define cmCPackTypeMacro(klass, superclass) \
379
using Superclass = superclass; \
380
const char* GetNameOfClass() override \
381
{ \
382
return #klass; \
383
} \
384
static cmCPackGenerator* CreateGenerator() \
385
{ \
386
return new klass; \
387
} \
388
class cmCPackTypeMacro_UseTrailingSemicolon
389
390
#define cmCPackLogger(logType, msg) \
391
do { \
392
std::ostringstream cmCPackLog_msg; \
393
cmCPackLog_msg << msg; \
394
this->Logger->Log(logType, __FILE__, __LINE__, \
395
cmCPackLog_msg.str().c_str()); \
396
} while (false)
397
398