Coverage Report

Created: 2026-04-29 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmExportFileGenerator.h
Line
Count
Source
1
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2
   file LICENSE.rst 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 <cm/string_view>
14
15
#include "cmDiagnostics.h"
16
#include "cmGeneratorExpression.h"
17
#include "cmMessageType.h"
18
19
class cmExportSet;
20
class cmGeneratorTarget;
21
class cmGeneratorFileSet;
22
class cmLocalGenerator;
23
24
/** \class cmExportFileGenerator
25
 * \brief Generate files exporting targets from a build or install tree.
26
 *
27
 * cmExportFileGenerator is the interface class for generating export files.
28
 */
29
class cmExportFileGenerator
30
{
31
public:
32
  cmExportFileGenerator();
33
0
  virtual ~cmExportFileGenerator() = default;
34
35
  /** Set the full path to the export file to generate.  */
36
  void SetExportFile(char const* mainFile);
37
  std::string const& GetMainExportFileName() const;
38
39
  /** Set the namespace in which to place exported target names.  */
40
0
  void SetNamespace(std::string const& ns) { this->Namespace = ns; }
41
0
  std::string GetNamespace() const { return this->Namespace; }
42
43
  /** Add a configuration to be exported.  */
44
  void AddConfiguration(std::string const& config);
45
46
  /** Create and actually generate the export file.  Returns whether there was
47
      an error.  */
48
  bool GenerateImportFile();
49
50
protected:
51
  using ImportPropertyMap = std::map<std::string, std::string>;
52
  using ImportFileSetPropertyMap = std::map<std::string, ImportPropertyMap>;
53
54
  // Collect properties with detailed information about targets beyond
55
  // their location on disk.
56
  void SetImportDetailProperties(std::string const& config,
57
                                 std::string const& suffix,
58
                                 cmGeneratorTarget const* target,
59
                                 ImportPropertyMap& properties);
60
61
  enum class ImportLinkPropertyTargetNames
62
  {
63
    Yes,
64
    No,
65
  };
66
  template <typename T>
67
  void SetImportLinkProperty(std::string const& suffix,
68
                             cmGeneratorTarget const* target,
69
                             std::string const& propName,
70
                             std::vector<T> const& entries,
71
                             ImportPropertyMap& properties,
72
                             ImportLinkPropertyTargetNames targetNames);
73
74
  /** Generate the export file to the given output stream.  Returns whether
75
      there was an error.  */
76
  virtual bool GenerateImportFile(std::ostream& os) = 0;
77
78
  /** Each subclass knows how to generate its kind of export file.  */
79
  virtual bool GenerateMainFile(std::ostream& os) = 0;
80
81
  /** Generate per-configuration target information to the given output
82
      stream.  */
83
  virtual void GenerateImportConfig(std::ostream& os,
84
                                    std::string const& config);
85
86
  /** Each subclass knows where the target files are located.  */
87
  virtual void GenerateImportTargetsConfig(std::ostream& os,
88
                                           std::string const& config,
89
                                           std::string const& suffix) = 0;
90
91
  /** Record a target referenced by an exported target. */
92
  virtual bool NoteLinkedTarget(cmGeneratorTarget const* target,
93
                                std::string const& linkedName,
94
                                cmGeneratorTarget const* linkedTarget);
95
96
  /** Each subclass knows how to deal with a target that is  missing from an
97
   *  export set.  */
98
  virtual void HandleMissingTarget(std::string& link_libs,
99
                                   cmGeneratorTarget const* depender,
100
                                   cmGeneratorTarget* dependee) = 0;
101
102
  /** Complain when a duplicate target is encountered.  */
103
  virtual void ComplainAboutDuplicateTarget(
104
    std::string const& targetName) const = 0;
105
106
  virtual cm::string_view GetImportPrefixWithSlash() const = 0;
107
108
  void AddImportPrefix(std::string& exportDirs) const;
109
110
  void PopulateInterfaceProperty(std::string const& propName,
111
                                 cmGeneratorTarget const* target,
112
                                 ImportPropertyMap& properties) const;
113
  void PopulateInterfaceProperty(std::string const& propName,
114
                                 cmGeneratorTarget const* target,
115
                                 cmGeneratorExpression::PreprocessContext,
116
                                 ImportPropertyMap& properties);
117
  void PopulateFileSetInterfaceProperty(
118
    std::string const& propName, cmGeneratorTarget const* target,
119
    cmGeneratorFileSet const* fileSet,
120
    cmGeneratorExpression::PreprocessContext, ImportPropertyMap& properties);
121
  bool PopulateInterfaceLinkLibrariesProperty(
122
    cmGeneratorTarget const* target, cmGeneratorExpression::PreprocessContext,
123
    ImportPropertyMap& properties);
124
125
  bool PopulateInterfaceProperties(
126
    cmGeneratorTarget const* target,
127
    std::string const& includesDestinationDirs,
128
    cmGeneratorExpression::PreprocessContext preprocessRule,
129
    ImportPropertyMap& properties);
130
131
  bool PopulateFileSetInterfaceProperties(
132
    cmGeneratorTarget const* target, cmGeneratorFileSet const* fileSet,
133
    cmGeneratorExpression::PreprocessContext preprocessRule,
134
    ImportPropertyMap& properties);
135
136
  virtual void IssueMessage(MessageType type,
137
                            std::string const& message) const = 0;
138
  virtual void IssueDiagnostic(cmDiagnosticCategory category,
139
                               std::string const& message) const = 0;
140
141
  void ReportError(std::string const& errorMessage) const
142
0
  {
143
0
    this->IssueMessage(MessageType::FATAL_ERROR, errorMessage);
144
0
  }
145
146
  struct ExportInfo
147
  {
148
    std::vector<std::string> Files;
149
    std::set<std::string> Sets;
150
    std::set<std::string> Namespaces;
151
  };
152
153
  /** Find the set of export files and the unique namespace (if any) for a
154
   *  target. */
155
  virtual ExportInfo FindExportInfo(cmGeneratorTarget const* target) const = 0;
156
157
  enum FreeTargetsReplace
158
  {
159
    ReplaceFreeTargets,
160
    NoReplaceFreeTargets
161
  };
162
163
  void ResolveTargetsInGeneratorExpressions(
164
    std::string& input, cmGeneratorTarget const* target,
165
    FreeTargetsReplace replace = NoReplaceFreeTargets);
166
167
0
  virtual cmExportSet* GetExportSet() const { return nullptr; }
168
169
  virtual void ReplaceInstallPrefix(std::string& input) const;
170
171
  virtual std::string InstallNameDir(cmGeneratorTarget const* target,
172
                                     std::string const& config) = 0;
173
174
  /** Get the temporary location of the config-agnostic C++ module file.  */
175
  virtual std::string GetCxxModuleFile(std::string const& name) const = 0;
176
177
  virtual std::string GetCxxModulesDirectory() const = 0;
178
  virtual void GenerateCxxModuleConfigInformation(std::string const&,
179
                                                  std::ostream& os) const = 0;
180
181
  bool AddTargetNamespace(std::string& input, cmGeneratorTarget const* target,
182
                          cmLocalGenerator const* lg);
183
184
  static std::string PropertyConfigSuffix(std::string const& config);
185
186
  // The namespace in which the exports are placed in the generated file.
187
  std::string Namespace;
188
189
  // The set of configurations to export.
190
  std::vector<std::string> Configurations;
191
192
  // The file to generate.
193
  std::string MainImportFile;
194
  std::string FileDir;
195
  std::string FileBase;
196
  std::string FileExt;
197
  bool AppendMode = false;
198
199
  // The set of targets included in the export.
200
  std::set<cmGeneratorTarget const*> ExportedTargets;
201
202
  std::vector<std::string> MissingTargets;
203
204
  std::set<cmGeneratorTarget const*> ExternalTargets;
205
206
private:
207
  void PopulateInterfaceProperty(std::string const& propName,
208
                                 std::string const& outputName,
209
                                 cmGeneratorTarget const* target,
210
                                 cmGeneratorExpression::PreprocessContext,
211
                                 ImportPropertyMap& properties);
212
213
  void PopulateCompatibleInterfaceProperties(
214
    cmGeneratorTarget const* target, ImportPropertyMap& properties) const;
215
  void PopulateCustomTransitiveInterfaceProperties(
216
    cmGeneratorTarget const* target,
217
    cmGeneratorExpression::PreprocessContext preprocessRule,
218
    ImportPropertyMap& properties);
219
  bool PopulateCxxModuleExportProperties(
220
    cmGeneratorTarget const* gte, ImportPropertyMap& properties,
221
    cmGeneratorExpression::PreprocessContext ctx,
222
    std::string const& includesDestinationDirs, std::string& errorMessage);
223
  bool PopulateExportProperties(cmGeneratorTarget const* gte,
224
                                ImportPropertyMap& properties,
225
                                std::string& errorMessage) const;
226
227
  void ResolveTargetsInGeneratorExpression(std::string& input,
228
                                           cmGeneratorTarget const* target,
229
                                           cmLocalGenerator const* lg);
230
};
231
232
extern template void cmExportFileGenerator::SetImportLinkProperty<std::string>(
233
  std::string const&, cmGeneratorTarget const*, std::string const&,
234
  std::vector<std::string> const&, ImportPropertyMap& properties,
235
  ImportLinkPropertyTargetNames);
236
237
extern template void cmExportFileGenerator::SetImportLinkProperty<cmLinkItem>(
238
  std::string const&, cmGeneratorTarget const*, std::string const&,
239
  std::vector<cmLinkItem> const&, ImportPropertyMap& properties,
240
  ImportLinkPropertyTargetNames);