cmCPackGenerator.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 char const* 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(std::string const& name, cmMakefile* mf);
  71. /**
  72. * Construct generator
  73. */
  74. cmCPackGenerator();
  75. virtual ~cmCPackGenerator();
  76. //! Set and get the options
  77. void SetOption(std::string const& op, char const* value);
  78. void SetOption(std::string const& op, std::string const& value)
  79. {
  80. this->SetOption(op, cmValue(value));
  81. }
  82. void SetOption(std::string const& op, cmValue value);
  83. void SetOptionIfNotSet(std::string const& op, char const* value);
  84. void SetOptionIfNotSet(std::string const& op, std::string const& value)
  85. {
  86. this->SetOptionIfNotSet(op, cmValue(value));
  87. }
  88. void SetOptionIfNotSet(std::string const& op, cmValue value);
  89. cmValue GetOption(std::string const& op) const;
  90. std::vector<std::string> GetOptions() const;
  91. bool IsSet(std::string const& name) const;
  92. cmValue GetOptionIfSet(std::string const& name) const;
  93. bool IsOn(std::string const& name) const;
  94. bool IsSetToOff(std::string const& op) const;
  95. bool IsSetToEmpty(std::string const& op) const;
  96. //! Set the logger
  97. void SetLogger(cmCPackLog* log) { this->Logger = log; }
  98. //! Display verbose information via logger
  99. void DisplayVerboseOutput(std::string const& msg, float progress);
  100. bool ReadListFile(char const* moduleName);
  101. protected:
  102. /**
  103. * Prepare common used names by inspecting
  104. * several CPACK_xxx var values.
  105. */
  106. int PrepareNames();
  107. /**
  108. * Install the project using appropriate method.
  109. */
  110. int InstallProject();
  111. int CleanTemporaryDirectory();
  112. cmInstalledFile const* GetInstalledFile(std::string const& name) const;
  113. virtual char const* GetOutputExtension() { return ".cpack"; }
  114. virtual char const* GetOutputPostfix() { return nullptr; }
  115. /**
  116. * Prepare requested grouping kind from CPACK_xxx vars
  117. * CPACK_COMPONENTS_ALL_IN_ONE_PACKAGE
  118. * CPACK_COMPONENTS_IGNORE_GROUPS
  119. * or
  120. * CPACK_COMPONENTS_ONE_PACKAGE_PER_GROUP
  121. * @return 1 on success 0 on failure.
  122. */
  123. virtual int PrepareGroupingKind();
  124. /**
  125. * Ensures that the given name only contains characters that can cleanly be
  126. * used as directory or file name and returns this sanitized name. Possibly,
  127. * this name might be replaced by its hash.
  128. * @param[in] name the name for a directory or file that shall be sanitized.
  129. * @param[in] isFullName true if the result is used as the full name for a
  130. * directory or file. (Defaults to true.)
  131. * @return the sanitized name.
  132. */
  133. virtual std::string GetSanitizedDirOrFileName(std::string const& name,
  134. bool isFullName = true) const;
  135. /**
  136. * Some CPack generators may prefer to have
  137. * CPack install all components belonging to the same
  138. * [component] group to be install in the same directory.
  139. * The default behavior is to install each component in
  140. * a separate directory.
  141. * @param[in] componentName the name of the component to be installed
  142. * @return the name suffix the generator wants for the specified component
  143. * default is "componentName"
  144. */
  145. virtual std::string GetComponentInstallSuffix(
  146. std::string const& componentName);
  147. /**
  148. * The value that GetComponentInstallSuffix returns, but sanitized.
  149. * @param[in] componentName the name of the component to be installed
  150. * @return the name suffix the generator wants for the specified component
  151. * (but sanitized, so that it can be used on the file-system).
  152. * default is "componentName".
  153. */
  154. virtual std::string GetComponentInstallDirNameSuffix(
  155. std::string const& componentName);
  156. /**
  157. * CPack specific generator may mangle CPACK_PACKAGE_FILE_NAME
  158. * with CPACK_COMPONENT_xxxx_<NAME>_DISPLAY_NAME if
  159. * CPACK_<GEN>_USE_DISPLAY_NAME_IN_FILENAME is ON.
  160. * @param[in] initialPackageFileName the initial package name to be mangled
  161. * @param[in] groupOrComponentName the name of the group/component
  162. * @param[in] isGroupName true if previous name refers to a group,
  163. * false otherwise
  164. */
  165. virtual std::string GetComponentPackageFileName(
  166. std::string const& initialPackageFileName,
  167. std::string const& groupOrComponentName, bool isGroupName);
  168. /**
  169. * Package the list of files and/or components which
  170. * has been prepared by the beginning of DoPackage.
  171. * @pre the @ref toplevel has been filled-in
  172. * @pre the list of file @ref files has been populated
  173. * @pre packageFileNames contains at least 1 entry
  174. * @post packageFileNames may have been updated and contains
  175. * the list of packages generated by the specific generator.
  176. */
  177. virtual int PackageFiles();
  178. virtual char const* GetInstallPath();
  179. virtual char const* GetPackagingInstallPrefix();
  180. bool GenerateChecksumFile(cmCryptoHash& crypto,
  181. cm::string_view filename) const;
  182. bool CopyPackageFile(std::string const& srcFilePath,
  183. cm::string_view filename) const;
  184. std::string FindTemplate(cm::string_view name,
  185. cm::optional<cm::string_view> alt = cm::nullopt);
  186. virtual bool ConfigureFile(std::string const& inName,
  187. std::string const& outName,
  188. bool copyOnly = false);
  189. virtual bool ConfigureString(std::string const& input, std::string& output);
  190. virtual int InitializeInternal();
  191. //! Run install commands if specified
  192. virtual int InstallProjectViaInstallCommands(
  193. bool setDestDir, std::string const& tempInstallDirectory);
  194. virtual int InstallProjectViaInstallScript(
  195. bool setDestDir, std::string const& tempInstallDirectory);
  196. virtual int InstallProjectViaInstalledDirectories(
  197. bool setDestDir, std::string const& tempInstallDirectory,
  198. mode_t const* default_dir_mode);
  199. virtual int InstallProjectViaInstallCMakeProjects(
  200. bool setDestDir, std::string const& tempInstallDirectory,
  201. mode_t const* default_dir_mode);
  202. virtual int RunPreinstallTarget(std::string const& installProjectName,
  203. std::string const& installDirectory,
  204. cmGlobalGenerator* globalGenerator,
  205. std::string const& buildConfig);
  206. virtual int InstallCMakeProject(
  207. bool setDestDir, std::string const& installDirectory,
  208. std::string const& baseTempInstallDirectory,
  209. mode_t const* default_dir_mode, std::string const& component,
  210. bool componentInstall, std::string const& installSubDirectory,
  211. std::string const& buildConfig, std::string& absoluteDestFiles);
  212. /**
  213. * The various level of support of
  214. * CPACK_SET_DESTDIR used by the generator.
  215. */
  216. enum CPackSetDestdirSupport
  217. {
  218. /* the generator works with or without it */
  219. SETDESTDIR_SUPPORTED,
  220. /* the generator works best if automatically handled */
  221. SETDESTDIR_INTERNALLY_SUPPORTED,
  222. /* no official support, use at your own risk */
  223. SETDESTDIR_SHOULD_NOT_BE_USED,
  224. /* officially NOT supported */
  225. SETDESTDIR_UNSUPPORTED
  226. };
  227. /**
  228. * Does the CPack generator support CPACK_SET_DESTDIR?
  229. * The default legacy value is 'SETDESTDIR_SUPPORTED' generator
  230. * have to override it in order change this.
  231. * @return CPackSetDestdirSupport
  232. */
  233. virtual enum CPackSetDestdirSupport SupportsSetDestdir() const;
  234. /**
  235. * Does the CPack generator support absolute path
  236. * in INSTALL DESTINATION?
  237. * The default legacy value is 'true' generator
  238. * have to override it in order change this.
  239. * @return true if supported false otherwise
  240. */
  241. virtual bool SupportsAbsoluteDestination() const;
  242. /**
  243. * Does the CPack generator support component installation?.
  244. * Some Generators requires the user to set
  245. * CPACK_<GENNAME>_COMPONENT_INSTALL in order to make this
  246. * method return true.
  247. * @return true if supported, false otherwise
  248. */
  249. virtual bool SupportsComponentInstallation() const;
  250. /**
  251. * Does the currently running generator want a component installation.
  252. * The generator may support component installation but he may
  253. * be requiring monolithic install using CPACK_MONOLITHIC_INSTALL.
  254. * @return true if component installation is supported and wanted.
  255. */
  256. virtual bool WantsComponentInstallation() const;
  257. virtual cmCPackInstallationType* GetInstallationType(
  258. std::string const& projectName, std::string const& name);
  259. virtual cmCPackComponent* GetComponent(std::string const& projectName,
  260. std::string const& name);
  261. virtual cmCPackComponentGroup* GetComponentGroup(
  262. std::string const& projectName, std::string const& name);
  263. cmSystemTools::OutputOption GeneratorVerbose;
  264. std::string Name;
  265. std::string InstallPath;
  266. /**
  267. * The list of package file names.
  268. * At beginning of DoPackage the (generic) generator will populate
  269. * the list of desired package file names then it will
  270. * call the redefined method PackageFiles which is may
  271. * either use this set of names (usually on entry there should be
  272. * only a single name) or update the vector with the list
  273. * of created package file names.
  274. */
  275. std::vector<std::string> packageFileNames;
  276. /**
  277. * The directory where all the files to be packaged reside.
  278. * If the installer support components there will be one
  279. * sub-directory for each component. In those directories
  280. * one will find the file belonging to the specified component.
  281. */
  282. std::string toplevel;
  283. /**
  284. * The complete list of files to be packaged.
  285. * This list will be populated by DoPackage before
  286. * PackageFiles is called.
  287. */
  288. std::vector<std::string> files;
  289. std::vector<cmCPackInstallCMakeProject> CMakeProjects;
  290. std::map<std::string, cmCPackInstallationType> InstallationTypes;
  291. /**
  292. * The set of components.
  293. * If component installation is supported then this map
  294. * contains the component specified in CPACK_COMPONENTS_ALL
  295. */
  296. std::map<std::string, cmCPackComponent> Components;
  297. std::map<std::string, cmCPackComponentGroup> ComponentGroups;
  298. /**
  299. * If components are enabled, this enum represents the different
  300. * ways of mapping components to package files.
  301. */
  302. enum ComponentPackageMethod
  303. {
  304. /* one package for all components */
  305. ONE_PACKAGE,
  306. /* one package for each component */
  307. ONE_PACKAGE_PER_COMPONENT,
  308. /* one package for each group,
  309. * with left over components in their own package */
  310. ONE_PACKAGE_PER_GROUP,
  311. UNKNOWN_COMPONENT_PACKAGE_METHOD
  312. };
  313. /**
  314. * The component package method
  315. * The default is ONE_PACKAGE_PER_GROUP,
  316. * and generators may override the default
  317. * before PrepareGroupingKind() is called.
  318. */
  319. ComponentPackageMethod componentPackageMethod;
  320. cmCPackLog* Logger;
  321. bool Trace;
  322. bool TraceExpand;
  323. cmMakefile* MakefileMap;
  324. private:
  325. template <typename ValueType>
  326. void StoreOption(std::string const& op, ValueType value);
  327. template <typename ValueType>
  328. void StoreOptionIfNotSet(std::string const& op, ValueType value);
  329. };
  330. #define cmCPackTypeMacro(klass, superclass) \
  331. using Superclass = superclass; \
  332. const char* GetNameOfClass() override \
  333. { \
  334. return #klass; \
  335. } \
  336. static cmCPackGenerator* CreateGenerator() \
  337. { \
  338. return new klass; \
  339. } \
  340. class cmCPackTypeMacro_UseTrailingSemicolon
  341. #define cmCPackLogger(logType, msg) \
  342. do { \
  343. std::ostringstream cmCPackLog_msg; \
  344. cmCPackLog_msg << msg; \
  345. this->Logger->Log(logType, __FILE__, __LINE__, \
  346. cmCPackLog_msg.str().c_str()); \
  347. } while (false)