Coverage Report

Created: 2026-07-14 07:09

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