cmTarget.h 10.4 KB
Newer Older
1
2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3
4
5
#ifndef cmTarget_h
#define cmTarget_h

6
#include <cmConfigure.h> // IWYU pragma: keep
Brad King's avatar
Brad King committed
7

8
#include "cmAlgorithms.h"
9
#include "cmCustomCommand.h"
10
#include "cmListFileCache.h"
11
12
#include "cmPolicies.h"
#include "cmPropertyMap.h"
13
14
15
16
17
18
19
20
21
#include "cmState.h"
#include "cmTargetLinkLibraryType.h"

#include <iosfwd>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
22

23
#if defined(CMAKE_BUILD_WITH_CMAKE)
24
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
25
26
27
28
#include <unordered_map>
#else
#include <cmsys/hash_map.hxx>
#endif
29
#endif
30

31
class cmMakefile;
Ken Martin's avatar
Ken Martin committed
32
class cmSourceFile;
33
class cmGlobalGenerator;
34
class cmTargetInternals;
35

36
37
38
39
40
41
42
43
class cmTargetInternalPointer
{
public:
  cmTargetInternalPointer();
  cmTargetInternalPointer(cmTargetInternalPointer const& r);
  ~cmTargetInternalPointer();
  cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
  cmTargetInternals* operator->() const { return this->Pointer; }
44
  cmTargetInternals* Get() const { return this->Pointer; }
45
46
47
48
private:
  cmTargetInternals* Pointer;
};

49
50
51
/** \class cmTarget
 * \brief Represent a library or executable target loaded from a makefile.
 *
52
 * cmTarget represents a target loaded from
53
54
55
56
57
 * a makefile.
 */
class cmTarget
{
public:
58
59
60
61
62
63
64
65
66
67
  enum Visibility
  {
    VisibilityNormal,
    VisibilityImported,
    VisibilityImportedGlobally
  };

  cmTarget(std::string const& name, cmState::TargetType type, Visibility vis,
           cmMakefile* mf);

68
69
70
71
72
73
  enum CustomCommandType
  {
    PRE_BUILD,
    PRE_LINK,
    POST_BUILD
  };
74

75
  /**
76
   * Return the type of target.
77
   */
78
  cmState::TargetType GetType() const { return this->TargetTypeValue; }
79

80
81
  cmGlobalGenerator* GetGlobalGenerator() const;

82
  ///! Set/Get the name of the target
83
  const std::string& GetName() const { return this->Name; }
84

85
  /** Get the cmMakefile that owns this target.  */
86
  cmMakefile* GetMakefile() const { return this->Makefile; }
87

88
89
90
91
92
#define DECLARE_TARGET_POLICY(POLICY)                                         \
  cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const                    \
  {                                                                           \
    return this->PolicyMap.Get(cmPolicies::POLICY);                           \
  }
93

94
  CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
95

96
#undef DECLARE_TARGET_POLICY
97

98
99
100
  /**
   * Get the list of the custom commands for this target
   */
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  std::vector<cmCustomCommand> const& GetPreBuildCommands() const
  {
    return this->PreBuildCommands;
  }
  std::vector<cmCustomCommand> const& GetPreLinkCommands() const
  {
    return this->PreLinkCommands;
  }
  std::vector<cmCustomCommand> const& GetPostBuildCommands() const
  {
    return this->PostBuildCommands;
  }
  void AddPreBuildCommand(cmCustomCommand const& cmd)
  {
    this->PreBuildCommands.push_back(cmd);
  }
  void AddPreLinkCommand(cmCustomCommand const& cmd)
  {
    this->PreLinkCommands.push_back(cmd);
  }
  void AddPostBuildCommand(cmCustomCommand const& cmd)
  {
    this->PostBuildCommands.push_back(cmd);
  }
125

126
127
128
129
  /**
   * Add sources to the target.
   */
  void AddSources(std::vector<std::string> const& srcs);
130
  void AddTracedSources(std::vector<std::string> const& srcs);
131
  cmSourceFile* AddSourceCMP0049(const std::string& src);
132
  cmSourceFile* AddSource(const std::string& src);
133

134
  //* how we identify a library, by name and type
135
  typedef std::pair<std::string, cmTargetLinkLibraryType> LibraryID;
136

137
138
139
140
141
  typedef std::vector<LibraryID> LinkLibraryVectorType;
  const LinkLibraryVectorType& GetOriginalLinkLibraries() const
  {
    return this->OriginalLinkLibraries;
  }
142

143
144
145
  /**
   * Clear the dependency information recorded for this target, if any.
   */
146
  void ClearDependencyInformation(cmMakefile& mf, const std::string& target);
147

148
149
  void AddLinkLibrary(cmMakefile& mf, const std::string& lib,
                      cmTargetLinkLibraryType llt);
150
151
  enum TLLSignature
  {
152
153
154
    KeywordTLLSignature,
    PlainTLLSignature
  };
155
156
  bool PushTLLCommandTrace(TLLSignature signature,
                           cmListFileContext const& lfc);
157
  void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
158

159
  const std::vector<std::string>& GetLinkDirectories() const;
160

161
  void AddLinkDirectory(const std::string& d);
Ken Martin's avatar
Ken Martin committed
162
163
164
165
166

  /**
   * Set the path where this target should be installed. This is relative to
   * INSTALL_PREFIX
   */
167
168
  std::string GetInstallPath() const { return this->InstallPath; }
  void SetInstallPath(const char* name) { this->InstallPath = name; }
169

170
171
172
173
  /**
   * Set the path where this target (if it has a runtime part) should be
   * installed. This is relative to INSTALL_PREFIX
   */
174
175
176
177
178
179
180
181
  std::string GetRuntimeInstallPath() const
  {
    return this->RuntimeInstallPath;
  }
  void SetRuntimeInstallPath(const char* name)
  {
    this->RuntimeInstallPath = name;
  }
182
183
184
185

  /**
   * Get/Set whether there is an install rule for this target.
   */
186
  bool GetHaveInstallRule() const { return this->HaveInstallRule; }
Ken Martin's avatar
Ken Martin committed
187
  void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
188

189
190
  /** Add a utility on which this project depends. A utility is an executable
   * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
191
   * commands. It is not a full path nor does it have an extension.
192
   */
Daniel Pfeifer's avatar
Daniel Pfeifer committed
193
  void AddUtility(const std::string& u, cmMakefile* makefile = CM_NULLPTR);
194
  ///! Get the utilities used by this target
195
  std::set<std::string> const& GetUtilities() const { return this->Utilities; }
196
  cmListFileBacktrace const* GetUtilityBacktrace(const std::string& u) const;
197

Bill Hoffman's avatar
Bill Hoffman committed
198
  ///! Set/Get a property of this target file
199
200
201
202
203
  void SetProperty(const std::string& prop, const char* value);
  void AppendProperty(const std::string& prop, const char* value,
                      bool asString = false);
  const char* GetProperty(const std::string& prop) const;
  const char* GetProperty(const std::string& prop, cmMakefile* context) const;
204
205
  bool GetPropertyAsBool(const std::string& prop) const;
  void CheckProperty(const std::string& prop, cmMakefile* context) const;
206

207
  bool IsImported() const { return this->IsImportedTarget; }
208
  bool IsImportedGloballyVisible() const
209
210
211
  {
    return this->ImportedGloballyVisible;
  }
Bill Hoffman's avatar
Bill Hoffman committed
212

213
  // Get the properties
214
  cmPropertyMap const& GetProperties() const { return this->Properties; }
215

216
217
  bool GetMappedConfig(std::string const& desired_config, const char** loc,
                       const char** imp, std::string& suffix) const;
218

219
220
  /** Return whether this target is an executable with symbol exports
      enabled.  */
221
  bool IsExecutableWithExports() const;
222

223
224
  /** Return whether this target is a shared library Framework on
      Apple.  */
225
  bool IsFrameworkOnApple() const;
226

227
  /** Return whether this target is an executable Bundle on Apple.  */
228
  bool IsAppBundleOnApple() const;
229

230
231
232
  /** Get a backtrace from the creation of the target.  */
  cmListFileBacktrace const& GetBacktrace() const;

233
  void InsertInclude(std::string const& entry, cmListFileBacktrace const& bt,
234
                     bool before = false);
235
  void InsertCompileOption(std::string const& entry,
236
                           cmListFileBacktrace const& bt, bool before = false);
237
238
  void InsertCompileDefinition(std::string const& entry,
                               cmListFileBacktrace const& bt);
239

240
241
  void AppendBuildInterfaceIncludes();

242
243
  std::string GetDebugGeneratorExpressions(const std::string& value,
                                           cmTargetLinkLibraryType llt) const;
244

245
246
247
248
249
  void AddSystemIncludeDirectories(const std::set<std::string>& incs);
  std::set<std::string> const& GetSystemIncludeDirectories() const
  {
    return this->SystemIncludeDirectories;
  }
250

251
252
253
  cmStringRange GetIncludeDirectoriesEntries() const;
  cmBacktraceRange GetIncludeDirectoriesBacktraces() const;

254
255
256
  cmStringRange GetCompileOptionsEntries() const;
  cmBacktraceRange GetCompileOptionsBacktraces() const;

257
258
259
  cmStringRange GetCompileFeaturesEntries() const;
  cmBacktraceRange GetCompileFeaturesBacktraces() const;

260
261
262
  cmStringRange GetCompileDefinitionsEntries() const;
  cmBacktraceRange GetCompileDefinitionsBacktraces() const;

263
264
  cmStringRange GetSourceEntries() const;
  cmBacktraceRange GetSourceBacktraces() const;
265
266
267
  cmStringRange GetLinkImplementationEntries() const;
  cmBacktraceRange GetLinkImplementationBacktraces() const;

268
269
  struct StrictTargetComparison
  {
270
271
272
    bool operator()(cmTarget const* t1, cmTarget const* t2) const;
  };

273
274
  std::string ImportedGetFullPath(const std::string& config,
                                  bool implib) const;
275

276
private:
277
278
  const char* GetSuffixVariableInternal(bool implib) const;
  const char* GetPrefixVariableInternal(bool implib) const;
279

280
281
  // Use a makefile variable to set a default for the given property.
  // If the variable is not defined use the given default instead.
282
283
  void SetPropertyDefault(const std::string& property,
                          const char* default_value);
284

285
private:
286
  cmPropertyMap Properties;
287
288
289
290
291
  std::set<std::string> SystemIncludeDirectories;
  std::set<std::string> LinkDirectoriesEmmitted;
  std::set<std::string> Utilities;
  std::map<std::string, cmListFileBacktrace> UtilityBacktraces;
  cmPolicies::PolicyMap PolicyMap;
Ken Martin's avatar
Ken Martin committed
292
  std::string Name;
293
294
295
  std::string InstallPath;
  std::string RuntimeInstallPath;
  std::vector<std::string> LinkDirectories;
Ken Martin's avatar
Ken Martin committed
296
297
298
  std::vector<cmCustomCommand> PreBuildCommands;
  std::vector<cmCustomCommand> PreLinkCommands;
  std::vector<cmCustomCommand> PostBuildCommands;
299
300
301
302
  std::vector<std::pair<TLLSignature, cmListFileContext> > TLLCommands;
  LinkLibraryVectorType OriginalLinkLibraries;
  cmMakefile* Makefile;
  cmTargetInternalPointer Internal;
303
  cmState::TargetType TargetTypeValue;
Ken Martin's avatar
Ken Martin committed
304
  bool HaveInstallRule;
305
  bool RecordDependencies;
306
  bool DLLPlatform;
307
  bool IsAndroid;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
308
  bool IsImportedTarget;
309
  bool ImportedGloballyVisible;
310
  bool BuildInterfaceIncludesAppended;
311

312
313
  std::string ProcessSourceItemCMP0049(const std::string& s);

314
315
316
  /** Return whether or not the target has a DLL import library.  */
  bool HasImportLibrary() const;

317
318
  // Internal representation details.
  friend class cmTargetInternals;
319
320
  friend class cmGeneratorTarget;
  friend class cmTargetTraceDependencies;
321

322
  cmListFileBacktrace Backtrace;
323
324
};

325
#ifdef CMAKE_BUILD_WITH_CMAKE
326
#ifdef CMake_HAVE_CXX_UNORDERED_MAP
327
328
329
330
typedef std::unordered_map<std::string, cmTarget> cmTargets;
#else
typedef cmsys::hash_map<std::string, cmTarget> cmTargets;
#endif
331
#else
332
typedef std::map<std::string, cmTarget> cmTargets;
333
#endif
334

335
336
337
338
339
340
class cmTargetSet : public std::set<std::string>
{
};
class cmTargetManifest : public std::map<std::string, cmTargetSet>
{
};
341

342
#endif