Coverage Report

Created: 2026-09-14 06:43

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