cmCPackGenerator.h 13 KB

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