Coverage Report

Created: 2026-02-09 06:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Source/cmTarget.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 <cstddef>
8
#include <iosfwd>
9
#include <memory>
10
#include <set>
11
#include <string>
12
#include <utility>
13
#include <vector>
14
15
#include <cm/optional>
16
17
#include "cmAlgorithms.h"
18
#include "cmListFileCache.h"
19
#include "cmPolicies.h"
20
#include "cmStateTypes.h"
21
#include "cmStringAlgorithms.h"
22
#include "cmTargetLinkLibraryType.h"
23
#include "cmValue.h"
24
25
class cmCustomCommand;
26
class cmFileSet;
27
class cmFindPackageStack;
28
class cmGlobalGenerator;
29
class cmInstallTargetGenerator;
30
class cmMakefile;
31
class cmPropertyMap;
32
class cmSourceFile;
33
class cmTargetExport;
34
class cmTargetInternals;
35
36
enum class cmFileSetVisibility;
37
38
/** \class cmTarget
39
 * \brief Represent a library or executable target loaded from a makefile.
40
 *
41
 * cmTarget represents a target loaded from a makefile.
42
 */
43
class cmTarget
44
{
45
public:
46
  enum class Visibility
47
  {
48
    Normal,
49
    Generated,
50
    Imported,
51
    ImportedGlobally,
52
    Foreign,
53
  };
54
55
  enum class Origin
56
  {
57
    Cps,
58
    Unknown,
59
  };
60
61
  enum class PerConfig
62
  {
63
    Yes,
64
    No
65
  };
66
67
  cmTarget(std::string const& name, cmStateEnums::TargetType type,
68
           Visibility vis, cmMakefile* mf, PerConfig perConfig);
69
70
  cmTarget(cmTarget const&) = delete;
71
  cmTarget(cmTarget&&) noexcept;
72
  ~cmTarget();
73
74
  cmTarget& operator=(cmTarget const&) = delete;
75
  cmTarget& operator=(cmTarget&&) noexcept;
76
77
  //! Return the type of target.
78
  cmStateEnums::TargetType GetType() const;
79
80
  //! Set the origin of the target.
81
  void SetOrigin(Origin origin);
82
83
  //! Return the origin of the target.
84
  Origin GetOrigin() const;
85
86
  //! Get the cmMakefile that owns this target.
87
  cmMakefile* GetMakefile() const;
88
89
  //! Return the global generator.
90
  cmGlobalGenerator* GetGlobalGenerator() const;
91
92
  //! Set/Get the name of the target
93
  std::string const& GetName() const;
94
  std::string const& GetTemplateName() const;
95
96
  //! Get the policy map
97
  cmPolicies::PolicyMap const& GetPolicyMap() const;
98
99
  //! Get policy status
100
  cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
101
102
#define DECLARE_TARGET_POLICY(POLICY)                                         \
103
  cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const                    \
104
0
  {                                                                           \
105
0
    return this->GetPolicyStatus(cmPolicies::POLICY);                         \
106
0
  }
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0003() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0004() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0008() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0020() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0021() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0022() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0027() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0037() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0038() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0041() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0042() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0046() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0052() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0060() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0063() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0065() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0068() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0069() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0073() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0076() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0081() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0083() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0095() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0099() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0104() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0105() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0108() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0112() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0113() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0119() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0131() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0142() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0154() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0155() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0156() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0157() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0160() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0162() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0179() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0181() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0182() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0195() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0199() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0200() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0202() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0203() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0204() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0209() const
Unexecuted instantiation: cmTarget::GetPolicyStatusCMP0210() const
107
108
  CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
109
110
#undef DECLARE_TARGET_POLICY
111
112
  //! Get the list of the PRE_BUILD custom commands for this target
113
  std::vector<cmCustomCommand> const& GetPreBuildCommands() const;
114
  void AddPreBuildCommand(cmCustomCommand const& cmd);
115
  void AddPreBuildCommand(cmCustomCommand&& cmd);
116
117
  //! Get the list of the PRE_LINK custom commands for this target
118
  std::vector<cmCustomCommand> const& GetPreLinkCommands() const;
119
  void AddPreLinkCommand(cmCustomCommand const& cmd);
120
  void AddPreLinkCommand(cmCustomCommand&& cmd);
121
122
  //! Get the list of the POST_BUILD custom commands for this target
123
  std::vector<cmCustomCommand> const& GetPostBuildCommands() const;
124
  void AddPostBuildCommand(cmCustomCommand const& cmd);
125
  void AddPostBuildCommand(cmCustomCommand&& cmd);
126
127
  //! Add sources to the target.
128
  void AddSources(std::vector<std::string> const& srcs);
129
  void AddTracedSources(std::vector<std::string> const& srcs);
130
  cmSourceFile* AddSource(std::string const& src, bool before = false);
131
132
  //! how we identify a library, by name and type
133
  using LibraryID = std::pair<std::string, cmTargetLinkLibraryType>;
134
  using LinkLibraryVectorType = std::vector<LibraryID>;
135
  LinkLibraryVectorType const& GetOriginalLinkLibraries() const;
136
137
  //! Clear the dependency information recorded for this target, if any.
138
  void ClearDependencyInformation(cmMakefile& mf) const;
139
140
  void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
141
                      cmTargetLinkLibraryType llt);
142
143
  enum TLLSignature
144
  {
145
    KeywordTLLSignature,
146
    PlainTLLSignature
147
  };
148
  bool PushTLLCommandTrace(TLLSignature signature,
149
                           cmListFileContext const& lfc);
150
  void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
151
152
  /**
153
   * Set the path where this target should be installed. This is relative to
154
   * INSTALL_PREFIX
155
   */
156
  std::string const& GetInstallPath() const;
157
  void SetInstallPath(std::string const& name);
158
159
  /**
160
   * Set the path where this target (if it has a runtime part) should be
161
   * installed. This is relative to INSTALL_PREFIX
162
   */
163
  std::string const& GetRuntimeInstallPath() const;
164
  void SetRuntimeInstallPath(std::string const& name);
165
166
  /**
167
   * Get/Set whether there is an install rule for this target.
168
   */
169
  bool GetHaveInstallRule() const;
170
  void SetHaveInstallRule(bool hir);
171
172
  void AddInstallGenerator(cmInstallTargetGenerator* g);
173
  std::vector<cmInstallTargetGenerator*> const& GetInstallGenerators() const;
174
175
  /**
176
   * Get/Set whether this target was auto-created by a generator.
177
   */
178
  bool GetIsGeneratorProvided() const;
179
  void SetIsGeneratorProvided(bool igp);
180
181
  /**
182
   * Add a utility on which this project depends. A utility is an executable
183
   * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
184
   * commands. It is not a full path nor does it have an extension.
185
   */
186
  void AddUtility(std::string const& name, bool cross,
187
                  cmMakefile const* mf = nullptr);
188
  void AddUtility(BT<std::pair<std::string, bool>> util);
189
190
  void AddCodegenDependency(std::string const& name);
191
192
  std::set<std::string> const& GetCodegenDeps() const;
193
194
  //! Get the utilities used by this target
195
  std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
196
197
  void SetSymbolic(bool value);
198
199
  //! Set/Get a property of this target file
200
  void SetProperty(std::string const& prop, cmValue value);
201
  void SetProperty(std::string const& prop, std::nullptr_t)
202
0
  {
203
0
    this->SetProperty(prop, cmValue{ nullptr });
204
0
  }
205
  void SetProperty(std::string const& prop, std::string const& value)
206
0
  {
207
0
    this->SetProperty(prop, cmValue(value));
208
0
  }
209
  void AppendProperty(
210
    std::string const& prop, std::string const& value,
211
    cm::optional<cmListFileBacktrace> const& bt = cm::nullopt,
212
    bool asString = false);
213
  //! Might return a nullptr if the property is not set or invalid
214
  cmValue GetProperty(std::string const& prop) const;
215
  //! Always returns a valid pointer
216
  std::string const& GetSafeProperty(std::string const& prop) const;
217
  bool GetPropertyAsBool(std::string const& prop) const;
218
  void CheckProperty(std::string const& prop, cmMakefile* context) const;
219
  cmValue GetComputedProperty(std::string const& prop, cmMakefile& mf) const;
220
  //! Get all properties
221
  cmPropertyMap const& GetProperties() const;
222
223
  //! Return whether or not the target is for a DLL platform.
224
  bool IsDLLPlatform() const;
225
226
  //! Return whether or not we are targeting AIX.
227
  bool IsAIX() const;
228
  //! Return whether or not we are targeting Apple.
229
  bool IsApple() const;
230
231
  bool IsNormal() const;
232
  bool IsSynthetic() const;
233
  bool IsImported() const;
234
  bool IsImportedGloballyVisible() const;
235
  bool IsForeign() const;
236
  bool IsPerConfig() const;
237
  bool IsRuntimeBinary() const;
238
  bool IsSymbolic() const;
239
  bool CanCompileSources() const;
240
  void SetIsForTryCompile();
241
  bool IsForTryCompile() const;
242
243
  bool GetMappedConfig(std::string const& desiredConfig, cmValue& loc,
244
                       cmValue& imp, std::string& suffix) const;
245
246
  //! Return whether this target is an executable with symbol exports enabled.
247
  bool IsExecutableWithExports() const;
248
249
  //! Return whether this target is a shared library with symbol exports
250
  //! enabled.
251
  bool IsSharedLibraryWithExports() const;
252
253
  //! Return whether this target is a shared library Framework on Apple.
254
  bool IsFrameworkOnApple() const;
255
256
  //! Return whether to archive shared library or not on AIX.
257
  bool IsArchivedAIXSharedLibrary() const;
258
259
  //! Return whether this target is an executable Bundle on Apple.
260
  bool IsAppBundleOnApple() const;
261
262
  //! Return whether this target is a GUI executable on Android.
263
  bool IsAndroidGuiExecutable() const;
264
265
  bool HasKnownObjectFileLocation(std::string* reason = nullptr) const;
266
267
  //! Get a backtrace from the creation of the target.
268
  cmListFileBacktrace const& GetBacktrace() const;
269
270
  //! Get a find_package call stack from the creation of the target.
271
  cmFindPackageStack const& GetFindPackageStack() const;
272
273
  void InsertInclude(BT<std::string> const& entry, bool before = false);
274
  void InsertCompileOption(BT<std::string> const& entry, bool before = false);
275
  void InsertCompileDefinition(BT<std::string> const& entry);
276
  void InsertLinkOption(BT<std::string> const& entry, bool before = false);
277
  void InsertLinkDirectory(BT<std::string> const& entry, bool before = false);
278
  void InsertPrecompileHeader(BT<std::string> const& entry);
279
280
  void AppendBuildInterfaceIncludes();
281
  void FinalizeTargetConfiguration(cmBTStringRange compileDefinitions);
282
283
  std::string GetDebugGeneratorExpressions(std::string const& value,
284
                                           cmTargetLinkLibraryType llt) const;
285
286
  void AddSystemIncludeDirectories(std::set<std::string> const& incs);
287
  std::set<std::string> const& GetSystemIncludeDirectories() const;
288
289
  void AddInstallIncludeDirectories(cmTargetExport const& te,
290
                                    cmStringRange incs);
291
  cmStringRange GetInstallIncludeDirectoriesEntries(
292
    cmTargetExport const& te) const;
293
294
  BTs<std::string> const* GetLanguageStandardProperty(
295
    std::string const& propertyName) const;
296
297
  void SetLanguageStandardProperty(std::string const& lang,
298
                                   std::string const& value,
299
                                   std::string const& feature);
300
301
  cmBTStringRange GetIncludeDirectoriesEntries() const;
302
303
  cmBTStringRange GetCompileOptionsEntries() const;
304
305
  cmBTStringRange GetCompileFeaturesEntries() const;
306
307
  cmBTStringRange GetCompileDefinitionsEntries() const;
308
309
  cmBTStringRange GetPrecompileHeadersEntries() const;
310
311
  cmBTStringRange GetSourceEntries() const;
312
313
  cmBTStringRange GetLinkOptionsEntries() const;
314
315
  cmBTStringRange GetLinkDirectoriesEntries() const;
316
317
  cmBTStringRange GetLinkImplementationEntries() const;
318
319
  cmBTStringRange GetLinkInterfaceEntries() const;
320
  cmBTStringRange GetLinkInterfaceDirectEntries() const;
321
  cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
322
323
  void CopyPolicyStatuses(cmTarget const* tgt);
324
  void CopyImportedCxxModulesEntries(cmTarget const* tgt);
325
  void CopyImportedCxxModulesProperties(cmTarget const* tgt);
326
327
  cmBTStringRange GetHeaderSetsEntries() const;
328
  cmBTStringRange GetCxxModuleSetsEntries() const;
329
330
  cmBTStringRange GetInterfaceHeaderSetsEntries() const;
331
  cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
332
333
  enum class ImportArtifactMissingOk
334
  {
335
    No,
336
    Yes
337
  };
338
339
  std::string ImportedGetFullPath(
340
    std::string const& config, cmStateEnums::ArtifactType artifact,
341
    ImportArtifactMissingOk missingOk = ImportArtifactMissingOk::No) const;
342
343
  struct StrictTargetComparison
344
  {
345
    bool operator()(cmTarget const* t1, cmTarget const* t2) const;
346
  };
347
348
  cmFileSet const* GetFileSet(std::string const& name) const;
349
  cmFileSet* GetFileSet(std::string const& name);
350
  std::pair<cmFileSet*, bool> GetOrCreateFileSet(std::string const& name,
351
                                                 std::string const& type,
352
                                                 cmFileSetVisibility vis);
353
354
  std::vector<std::string> GetAllFileSetNames() const;
355
  std::vector<std::string> GetAllInterfaceFileSets() const;
356
357
  static std::string GetFileSetsPropertyName(std::string const& type);
358
  static std::string GetInterfaceFileSetsPropertyName(std::string const& type);
359
360
  bool HasFileSets() const;
361
362
private:
363
  // Internal representation details.
364
  friend class cmGeneratorTarget;
365
366
  bool GetMappedConfigOld(std::string const& desired_config, cmValue& loc,
367
                          cmValue& imp, std::string& suffix) const;
368
  bool GetMappedConfigNew(std::string desiredConfig, cmValue& loc,
369
                          cmValue& imp, std::string& suffix) const;
370
  cmValue GetLocation(std::string const& base,
371
                      std::string const& suffix) const;
372
  bool GetLocation(std::string const& config, cmValue& loc, cmValue& imp,
373
                   std::string& suffix) const;
374
375
  char const* GetSuffixVariableInternal(
376
    cmStateEnums::ArtifactType artifact) const;
377
  char const* GetPrefixVariableInternal(
378
    cmStateEnums::ArtifactType artifact) const;
379
380
  std::unique_ptr<cmTargetInternals> impl;
381
};