resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmExportFileGenerator.h
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
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <iosfwd>
8 #include <map>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "cmGeneratorExpression.h"
14 #include "cmStateTypes.h"
15 #include "cmVersion.h"
16 #include "cmVersionConfig.h"
17
18 class cmFileSet;
19 class cmGeneratorTarget;
20 class cmLocalGenerator;
21 class cmTargetExport;
22
23 #define STRINGIFY_HELPER(X) #X
24 #define STRINGIFY(X) STRINGIFY_HELPER(X)
25
26 #define DEVEL_CMAKE_VERSION(major, minor)                                     \
27   (CMake_VERSION_ENCODE(major, minor, 0) >                                    \
28        CMake_VERSION_ENCODE(CMake_VERSION_MAJOR, CMake_VERSION_MINOR, 0)      \
29      ? STRINGIFY(CMake_VERSION_MAJOR) "." STRINGIFY(                          \
30          CMake_VERSION_MINOR) "." STRINGIFY(CMake_VERSION_PATCH)              \
31      : #major "." #minor ".0")
32
33 /** \class cmExportFileGenerator
34  * \brief Generate a file exporting targets from a build or install tree.
35  *
36  * cmExportFileGenerator is the superclass for
37  * cmExportBuildFileGenerator and cmExportInstallFileGenerator.  It
38  * contains common code generation routines for the two kinds of
39  * export implementations.
40  */
41 class cmExportFileGenerator
42 {
43 public:
44   cmExportFileGenerator();
45   virtual ~cmExportFileGenerator() = default;
46
47   /** Set the full path to the export file to generate.  */
48   void SetExportFile(const char* mainFile);
49   const std::string& GetMainExportFileName() const;
50
51   /** Set the namespace in which to place exported target names.  */
52   void SetNamespace(const std::string& ns) { this->Namespace = ns; }
53   std::string GetNamespace() const { return this->Namespace; }
54
55   void SetExportOld(bool exportOld) { this->ExportOld = exportOld; }
56
57   /** Add a configuration to be exported.  */
58   void AddConfiguration(const std::string& config);
59
60   /** Actually generate the export file.  Returns whether there was an
61       error.  */
62   bool GenerateImportFile();
63
64 protected:
65   using ImportPropertyMap = std::map<std::string, std::string>;
66
67   // Generate per-configuration target information to the given output
68   // stream.
69   void GenerateImportConfig(std::ostream& os, const std::string& config);
70
71   // Methods to implement export file code generation.
72   virtual void GeneratePolicyHeaderCode(std::ostream& os);
73   virtual void GeneratePolicyFooterCode(std::ostream& os);
74   virtual void GenerateImportHeaderCode(std::ostream& os,
75                                         const std::string& config = "");
76   virtual void GenerateImportFooterCode(std::ostream& os);
77   void GenerateImportVersionCode(std::ostream& os);
78   virtual void GenerateImportTargetCode(std::ostream& os,
79                                         cmGeneratorTarget const* target,
80                                         cmStateEnums::TargetType targetType);
81   virtual void GenerateImportPropertyCode(std::ostream& os,
82                                           const std::string& config,
83                                           cmGeneratorTarget const* target,
84                                           ImportPropertyMap const& properties);
85   virtual void GenerateImportedFileChecksCode(
86     std::ostream& os, cmGeneratorTarget* target,
87     ImportPropertyMap const& properties,
88     const std::set<std::string>& importedLocations);
89   virtual void GenerateImportedFileCheckLoop(std::ostream& os);
90   virtual void GenerateMissingTargetsCheckCode(std::ostream& os);
91
92   virtual void GenerateExpectedTargetsCode(std::ostream& os,
93                                            const std::string& expectedTargets);
94
95   // Collect properties with detailed information about targets beyond
96   // their location on disk.
97   void SetImportDetailProperties(const std::string& config,
98                                  std::string const& suffix,
99                                  cmGeneratorTarget* target,
100                                  ImportPropertyMap& properties);
101
102   enum class ImportLinkPropertyTargetNames
103   {
104     Yes,
105     No,
106   };
107   template <typename T>
108   void SetImportLinkProperty(std::string const& suffix,
109                              cmGeneratorTarget const* target,
110                              const std::string& propName,
111                              std::vector<T> const& entries,
112                              ImportPropertyMap& properties,
113                              ImportLinkPropertyTargetNames targetNames);
114
115   /** Each subclass knows how to generate its kind of export file.  */
116   virtual bool GenerateMainFile(std::ostream& os) = 0;
117
118   /** Each subclass knows where the target files are located.  */
119   virtual void GenerateImportTargetsConfig(std::ostream& os,
120                                            const std::string& config,
121                                            std::string const& suffix) = 0;
122
123   /** Each subclass knows how to deal with a target that is  missing from an
124    *  export set.  */
125   virtual void HandleMissingTarget(std::string& link_libs,
126                                    cmGeneratorTarget const* depender,
127                                    cmGeneratorTarget* dependee) = 0;
128   void PopulateInterfaceProperty(const std::string&,
129                                  cmGeneratorTarget const* target,
130                                  cmGeneratorExpression::PreprocessContext,
131                                  ImportPropertyMap& properties);
132   bool PopulateInterfaceLinkLibrariesProperty(
133     cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
134     ImportPropertyMap& properties);
135   void PopulateInterfaceProperty(const std::string& propName,
136                                  cmGeneratorTarget const* target,
137                                  ImportPropertyMap& properties);
138   void PopulateCompatibleInterfaceProperties(cmGeneratorTarget const* target,
139                                              ImportPropertyMap& properties);
140   virtual void GenerateInterfaceProperties(
141     cmGeneratorTarget const* target, std::ostream& os,
142     const ImportPropertyMap& properties);
143   void PopulateIncludeDirectoriesInterface(
144     cmGeneratorTarget const* target,
145     cmGeneratorExpression::PreprocessContext preprocessRule,
146     ImportPropertyMap& properties, cmTargetExport const& te);
147   void PopulateSourcesInterface(
148     cmGeneratorTarget const* target,
149     cmGeneratorExpression::PreprocessContext preprocessRule,
150     ImportPropertyMap& properties);
151   void PopulateLinkDirectoriesInterface(
152     cmGeneratorTarget const* target,
153     cmGeneratorExpression::PreprocessContext preprocessRule,
154     ImportPropertyMap& properties);
155   void PopulateLinkDependsInterface(
156     cmGeneratorTarget const* target,
157     cmGeneratorExpression::PreprocessContext preprocessRule,
158     ImportPropertyMap& properties);
159
160   void SetImportLinkInterface(
161     const std::string& config, std::string const& suffix,
162     cmGeneratorExpression::PreprocessContext preprocessRule,
163     cmGeneratorTarget const* target, ImportPropertyMap& properties);
164
165   enum FreeTargetsReplace
166   {
167     ReplaceFreeTargets,
168     NoReplaceFreeTargets
169   };
170
171   void ResolveTargetsInGeneratorExpressions(
172     std::string& input, cmGeneratorTarget const* target,
173     FreeTargetsReplace replace = NoReplaceFreeTargets);
174
175   virtual void GenerateRequiredCMakeVersion(std::ostream& os,
176                                             const char* versionString);
177
178   bool PopulateExportProperties(cmGeneratorTarget const* gte,
179                                 ImportPropertyMap& properties,
180                                 std::string& errorMessage);
181
182   void GenerateTargetFileSets(cmGeneratorTarget* gte, std::ostream& os,
183                               cmTargetExport* te = nullptr);
184
185   void GenerateCxxModuleInformation(std::ostream& os);
186
187   virtual std::string GetFileSetDirectories(cmGeneratorTarget* gte,
188                                             cmFileSet* fileSet,
189                                             cmTargetExport* te) = 0;
190   virtual std::string GetFileSetFiles(cmGeneratorTarget* gte,
191                                       cmFileSet* fileSet,
192                                       cmTargetExport* te) = 0;
193
194   // The namespace in which the exports are placed in the generated file.
195   std::string Namespace;
196
197   bool ExportOld;
198
199   // The set of configurations to export.
200   std::vector<std::string> Configurations;
201
202   // The file to generate.
203   std::string MainImportFile;
204   std::string FileDir;
205   std::string FileBase;
206   std::string FileExt;
207   bool AppendMode;
208
209   // The set of targets included in the export.
210   std::set<cmGeneratorTarget*> ExportedTargets;
211
212   std::vector<std::string> MissingTargets;
213
214 private:
215   void PopulateInterfaceProperty(const std::string&, const std::string&,
216                                  cmGeneratorTarget const* target,
217                                  cmGeneratorExpression::PreprocessContext,
218                                  ImportPropertyMap& properties);
219
220   bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
221                           cmLocalGenerator const* lg);
222
223   void ResolveTargetsInGeneratorExpression(std::string& input,
224                                            cmGeneratorTarget const* target,
225                                            cmLocalGenerator const* lg);
226
227   virtual void ReplaceInstallPrefix(std::string& input);
228
229   virtual std::string InstallNameDir(cmGeneratorTarget const* target,
230                                      const std::string& config) = 0;
231
232   virtual std::string GetCxxModulesDirectory() const = 0;
233   virtual void GenerateCxxModuleConfigInformation(std::ostream& os) const = 0;
234 };