Coverage Report

Created: 2026-02-09 06:05

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