1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
13 #include "cmGeneratorExpression.h"
14 #include "cmStateTypes.h"
15 #include "cmVersion.h"
16 #include "cmVersionConfig.h"
19 class cmGeneratorTarget;
20 class cmLocalGenerator;
23 #define STRINGIFY_HELPER(X) #X
24 #define STRINGIFY(X) STRINGIFY_HELPER(X)
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")
33 /** \class cmExportFileGenerator
34 * \brief Generate a file exporting targets from a build or install tree.
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.
41 class cmExportFileGenerator
44 cmExportFileGenerator();
45 virtual ~cmExportFileGenerator() = default;
47 /** Set the full path to the export file to generate. */
48 void SetExportFile(const char* mainFile);
49 const std::string& GetMainExportFileName() const;
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; }
55 void SetExportOld(bool exportOld) { this->ExportOld = exportOld; }
57 /** Add a configuration to be exported. */
58 void AddConfiguration(const std::string& config);
60 /** Actually generate the export file. Returns whether there was an
62 bool GenerateImportFile();
65 using ImportPropertyMap = std::map<std::string, std::string>;
67 // Generate per-configuration target information to the given output
69 void GenerateImportConfig(std::ostream& os, const std::string& config);
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);
92 virtual void GenerateExpectedTargetsCode(std::ostream& os,
93 const std::string& expectedTargets);
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);
102 enum class ImportLinkPropertyTargetNames
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);
115 /** Each subclass knows how to generate its kind of export file. */
116 virtual bool GenerateMainFile(std::ostream& os) = 0;
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;
123 /** Each subclass knows how to deal with a target that is missing from an
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);
160 void SetImportLinkInterface(
161 const std::string& config, std::string const& suffix,
162 cmGeneratorExpression::PreprocessContext preprocessRule,
163 cmGeneratorTarget const* target, ImportPropertyMap& properties);
165 enum FreeTargetsReplace
171 void ResolveTargetsInGeneratorExpressions(
172 std::string& input, cmGeneratorTarget const* target,
173 FreeTargetsReplace replace = NoReplaceFreeTargets);
175 virtual void GenerateRequiredCMakeVersion(std::ostream& os,
176 const char* versionString);
178 bool PopulateExportProperties(cmGeneratorTarget const* gte,
179 ImportPropertyMap& properties,
180 std::string& errorMessage);
182 void GenerateTargetFileSets(cmGeneratorTarget* gte, std::ostream& os,
183 cmTargetExport* te = nullptr);
185 void GenerateCxxModuleInformation(std::ostream& os);
187 virtual std::string GetFileSetDirectories(cmGeneratorTarget* gte,
189 cmTargetExport* te) = 0;
190 virtual std::string GetFileSetFiles(cmGeneratorTarget* gte,
192 cmTargetExport* te) = 0;
194 // The namespace in which the exports are placed in the generated file.
195 std::string Namespace;
199 // The set of configurations to export.
200 std::vector<std::string> Configurations;
202 // The file to generate.
203 std::string MainImportFile;
205 std::string FileBase;
209 // The set of targets included in the export.
210 std::set<cmGeneratorTarget*> ExportedTargets;
212 std::vector<std::string> MissingTargets;
215 void PopulateInterfaceProperty(const std::string&, const std::string&,
216 cmGeneratorTarget const* target,
217 cmGeneratorExpression::PreprocessContext,
218 ImportPropertyMap& properties);
220 bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
221 cmLocalGenerator const* lg);
223 void ResolveTargetsInGeneratorExpression(std::string& input,
224 cmGeneratorTarget const* target,
225 cmLocalGenerator const* lg);
227 virtual void ReplaceInstallPrefix(std::string& input);
229 virtual std::string InstallNameDir(cmGeneratorTarget const* target,
230 const std::string& config) = 0;
232 virtual std::string GetCxxModulesDirectory() const = 0;
233 virtual void GenerateCxxModuleConfigInformation(std::ostream& os) const = 0;