cmCPackGenerator.h 10 KB

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