Coverage Report

Created: 2026-10-28 06:38

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