Coverage Report

Created: 2026-03-12 06:35

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