cmCPackGenerator.h 14 KB

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