cmCPackGenerator.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCPackGenerator_h
  4. #define cmCPackGenerator_h
  5. #include "cmConfigure.h" // IWYU pragma: keep
  6. #include <map>
  7. #include <sstream>
  8. #include <string>
  9. #include <vector>
  10. #include "cmCPackComponentGroup.h"
  11. #include "cmSystemTools.h"
  12. #include "cm_sys_stat.h"
  13. class cmCPackLog;
  14. class cmInstalledFile;
  15. class cmMakefile;
  16. /** \class cmCPackGenerator
  17. * \brief A superclass of all CPack Generators
  18. *
  19. */
  20. class cmCPackGenerator
  21. {
  22. public:
  23. virtual const char* GetNameOfClass() = 0;
  24. /**
  25. * If verbose then more information is printed out
  26. */
  27. void SetVerbose(bool val)
  28. {
  29. this->GeneratorVerbose =
  30. val ? cmSystemTools::OUTPUT_MERGE : cmSystemTools::OUTPUT_NONE;
  31. }
  32. /**
  33. * Put underlying cmake scripts in trace mode.
  34. */
  35. void SetTrace(bool val) { this->Trace = val; }
  36. /**
  37. * Put underlying cmake scripts in expanded trace mode.
  38. */
  39. void SetTraceExpand(bool val) { this->TraceExpand = val; }
  40. /**
  41. * Returns true if the generator may work on this system.
  42. * Rational:
  43. * Some CPack generator may run on some host and may not on others
  44. * (with the same system) because some tools are missing. If the tool
  45. * is missing then CPack won't activate (in the CPackGeneratorFactory)
  46. * this particular generator.
  47. */
  48. static bool CanGenerate() { return true; }
  49. /**
  50. * Do the actual whole package processing.
  51. * Subclass may redefine it but its usually enough
  52. * to redefine @ref PackageFiles, because in fact
  53. * this method do call:
  54. * - PrepareName
  55. * - clean-up temp dirs
  56. * - InstallProject (with the appropriate method)
  57. * - prepare list of files and/or components to be package
  58. * - PackageFiles
  59. * - Copy produced packages at the expected place
  60. * @return 0 if error.
  61. */
  62. virtual int DoPackage();
  63. /**
  64. * Initialize generator
  65. */
  66. int Initialize(const std::string& name, cmMakefile* mf);
  67. /**
  68. * Construct generator
  69. */
  70. cmCPackGenerator();
  71. virtual ~cmCPackGenerator();
  72. //! Set and get the options
  73. void SetOption(const std::string& op, const char* value);
  74. void SetOptionIfNotSet(const std::string& op, const char* value);
  75. const char* GetOption(const std::string& op) const;
  76. std::vector<std::string> GetOptions() const;
  77. bool IsSet(const std::string& name) const;
  78. bool IsOn(const std::string& name) const;
  79. bool IsSetToOff(const std::string& op) const;
  80. bool IsSetToEmpty(const std::string& op) const;
  81. //! Set the logger
  82. void SetLogger(cmCPackLog* log) { this->Logger = log; }
  83. //! Display verbose information via logger
  84. void DisplayVerboseOutput(const char* msg, float progress);
  85. bool ReadListFile(const char* moduleName);
  86. protected:
  87. /**
  88. * Prepare common used names by inspecting
  89. * several CPACK_xxx var values.
  90. */
  91. int PrepareNames();
  92. /**
  93. * Install the project using appropriate method.
  94. */
  95. int InstallProject();
  96. int CleanTemporaryDirectory();
  97. cmInstalledFile const* GetInstalledFile(std::string const& name) const;
  98. virtual const char* GetOutputExtension() { return ".cpack"; }
  99. virtual const char* GetOutputPostfix() { return nullptr; }
  100. /**
  101. * Prepare requested grouping kind from CPACK_xxx vars
  102. * CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE
  103. * CPACK_COMPONENTS_IGNORE_GROUPS
  104. * or
  105. * CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP
  106. * @return 1 on success 0 on failure.
  107. */
  108. virtual int PrepareGroupingKind();
  109. /**
  110. * Some CPack generators may prefer to have
  111. * CPack install all components belonging to the same
  112. * [component] group to be install in the same directory.
  113. * The default behavior is to install each component in
  114. * a separate directory.
  115. * @param[in] componentName the name of the component to be installed
  116. * @return the name suffix the generator wants for the specified component
  117. * default is "componentName"
  118. */
  119. virtual std::string GetComponentInstallDirNameSuffix(
  120. const std::string& componentName);
  121. /**
  122. * CPack specific generator may mangle CPACK_PACKAGE_FILE_NAME
  123. * with CPACK_COMPONENT_xxxx_<NAME>_DISPLAY_NAME if
  124. * CPACK_<GEN>_USE_DISPLAY_NAME_IN_FILENAME is ON.
  125. * @param[in] initialPackageFileName the initial package name to be mangled
  126. * @param[in] groupOrComponentName the name of the group/component
  127. * @param[in] isGroupName true if previous name refers to a group,
  128. * false otherwise
  129. */
  130. virtual std::string GetComponentPackageFileName(
  131. const std::string& initialPackageFileName,
  132. const std::string& groupOrComponentName, bool isGroupName);
  133. /**
  134. * Package the list of files and/or components which
  135. * has been prepared by the beginning of DoPackage.
  136. * @pre the @ref toplevel has been filled-in
  137. * @pre the list of file @ref files has been populated
  138. * @pre packageFileNames contains at least 1 entry
  139. * @post packageFileNames may have been updated and contains
  140. * the list of packages generated by the specific generator.
  141. */
  142. virtual int PackageFiles();
  143. virtual const char* GetInstallPath();
  144. virtual const char* GetPackagingInstallPrefix();
  145. virtual std::string FindTemplate(const char* name);
  146. virtual bool ConfigureFile(const char* inName, const char* outName,
  147. bool copyOnly = false);
  148. virtual bool ConfigureString(const std::string& input, std::string& output);
  149. virtual int InitializeInternal();
  150. //! Run install commands if specified
  151. virtual int InstallProjectViaInstallCommands(
  152. bool setDestDir, const std::string& tempInstallDirectory);
  153. virtual int InstallProjectViaInstallScript(
  154. bool setDestDir, const std::string& tempInstallDirectory);
  155. virtual int InstallProjectViaInstalledDirectories(
  156. bool setDestDir, const std::string& tempInstallDirectory,
  157. const mode_t* default_dir_mode);
  158. virtual int InstallProjectViaInstallCMakeProjects(
  159. bool setDestDir, const std::string& tempInstallDirectory,
  160. const mode_t* default_dir_mode);
  161. /**
  162. * The various level of support of
  163. * CPACK_SET_DESTDIR used by the generator.
  164. */
  165. enum CPackSetDestdirSupport
  166. {
  167. /* the generator works with or without it */
  168. SETDESTDIR_SUPPORTED,
  169. /* the generator works best if automatically handled */
  170. SETDESTDIR_INTERNALLY_SUPPORTED,
  171. /* no official support, use at your own risk */
  172. SETDESTDIR_SHOULD_NOT_BE_USED,
  173. /* officially NOT supported */
  174. SETDESTDIR_UNSUPPORTED
  175. };
  176. /**
  177. * Does the CPack generator support CPACK_SET_DESTDIR?
  178. * The default legacy value is 'SETDESTDIR_SUPPORTED' generator
  179. * have to override it in order change this.
  180. * @return CPackSetDestdirSupport
  181. */
  182. virtual enum CPackSetDestdirSupport SupportsSetDestdir() const;
  183. /**
  184. * Does the CPack generator support absolute path
  185. * in INSTALL DESTINATION?
  186. * The default legacy value is 'true' generator
  187. * have to override it in order change this.
  188. * @return true if supported false otherwise
  189. */
  190. virtual bool SupportsAbsoluteDestination() const;
  191. /**
  192. * Does the CPack generator support component installation?.
  193. * Some Generators requires the user to set
  194. * CPACK_<GENNAME>_COMPONENT_INSTALL in order to make this
  195. * method return true.
  196. * @return true if supported, false otherwise
  197. */
  198. virtual bool SupportsComponentInstallation() const;
  199. /**
  200. * Does the currently running generator want a component installation.
  201. * The generator may support component installation but he may
  202. * be requiring monolithic install using CPACK_MONOLITHIC_INSTALL.
  203. * @return true if component installation is supported and wanted.
  204. */
  205. virtual bool WantsComponentInstallation() const;
  206. virtual cmCPackInstallationType* GetInstallationType(
  207. const std::string& projectName, const std::string& name);
  208. virtual cmCPackComponent* GetComponent(const std::string& projectName,
  209. const std::string& name);
  210. virtual cmCPackComponentGroup* GetComponentGroup(
  211. const std::string& projectName, const std::string& name);
  212. cmSystemTools::OutputOption GeneratorVerbose;
  213. std::string Name;
  214. std::string InstallPath;
  215. /**
  216. * The list of package file names.
  217. * At beginning of DoPackage the (generic) generator will populate
  218. * the list of desired package file names then it will
  219. * call the redefined method PackageFiles which is may
  220. * either use this set of names (usually on entry there should be
  221. * only a single name) or update the vector with the list
  222. * of created package file names.
  223. */
  224. std::vector<std::string> packageFileNames;
  225. /**
  226. * The directory where all the files to be packaged reside.
  227. * If the installer support components there will be one
  228. * sub-directory for each component. In those directories
  229. * one will find the file belonging to the specified component.
  230. */
  231. std::string toplevel;
  232. /**
  233. * The complete list of files to be packaged.
  234. * This list will be populated by DoPackage before
  235. * PackageFiles is called.
  236. */
  237. std::vector<std::string> files;
  238. std::map<std::string, cmCPackInstallationType> InstallationTypes;
  239. /**
  240. * The set of components.
  241. * If component installation is supported then this map
  242. * contains the component specified in CPACK_COMPONENTS_ALL
  243. */
  244. std::map<std::string, cmCPackComponent> Components;
  245. std::map<std::string, cmCPackComponentGroup> ComponentGroups;
  246. /**
  247. * If components are enabled, this enum represents the different
  248. * ways of mapping components to package files.
  249. */
  250. enum ComponentPackageMethod
  251. {
  252. /* one package for all components */
  253. ONE_PACKAGE,
  254. /* one package for each component */
  255. ONE_PACKAGE_PER_COMPONENT,
  256. /* one package for each group,
  257. * with left over components in their own package */
  258. ONE_PACKAGE_PER_GROUP,
  259. UNKNOWN_COMPONENT_PACKAGE_METHOD
  260. };
  261. /**
  262. * The component package method
  263. * The default is ONE_PACKAGE_PER_GROUP,
  264. * and generators may override the default
  265. * before PrepareGroupingKind() is called.
  266. */
  267. ComponentPackageMethod componentPackageMethod;
  268. cmCPackLog* Logger;
  269. bool Trace;
  270. bool TraceExpand;
  271. private:
  272. cmMakefile* MakefileMap;
  273. };
  274. #define cmCPackTypeMacro(klass, superclass) \
  275. typedef superclass Superclass; \
  276. const char* GetNameOfClass() override { return #klass; } \
  277. static cmCPackGenerator* CreateGenerator() { return new klass; } \
  278. class cmCPackTypeMacro_UseTrailingSemicolon
  279. #define cmCPackLogger(logType, msg) \
  280. do { \
  281. std::ostringstream cmCPackLog_msg; \
  282. cmCPackLog_msg << msg; \
  283. this->Logger->Log(logType, __FILE__, __LINE__, \
  284. cmCPackLog_msg.str().c_str()); \
  285. } while (false)
  286. #endif