cmCPackGenerator.h 12 KB

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