cmCPackGenerator.h 10 KB

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