resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmTarget.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <iosfwd>
8 #include <map>
9 #include <memory>
10 #include <set>
11 #include <string>
12 #include <utility>
13 #include <vector>
14
15 #include <cm/optional>
16
17 #include "cmAlgorithms.h"
18 #include "cmFileSet.h"
19 #include "cmPolicies.h"
20 #include "cmStateTypes.h"
21 #include "cmStringAlgorithms.h"
22 #include "cmTargetLinkLibraryType.h"
23 #include "cmValue.h"
24
25 class cmCustomCommand;
26 class cmGlobalGenerator;
27 class cmInstallTargetGenerator;
28 class cmListFileBacktrace;
29 class cmListFileContext;
30 class cmMakefile;
31 class cmPropertyMap;
32 class cmSourceFile;
33 class cmTargetExport;
34 class cmTargetInternals;
35
36 template <typename T>
37 class BT;
38 template <typename T>
39 class BTs;
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 Visibility
50   {
51     VisibilityNormal,
52     VisibilityImported,
53     VisibilityImportedGlobally
54   };
55
56   enum class PerConfig
57   {
58     Yes,
59     No
60   };
61
62   cmTarget(std::string const& name, cmStateEnums::TargetType type,
63            Visibility vis, cmMakefile* mf, PerConfig perConfig);
64
65   cmTarget(cmTarget const&) = delete;
66   cmTarget(cmTarget&&) noexcept;
67   ~cmTarget();
68
69   cmTarget& operator=(cmTarget const&) = delete;
70   cmTarget& operator=(cmTarget&&) noexcept;
71
72   //! Return the type of target.
73   cmStateEnums::TargetType GetType() const;
74
75   //! Get the cmMakefile that owns this target.
76   cmMakefile* GetMakefile() const;
77
78   //! Return the global generator.
79   cmGlobalGenerator* GetGlobalGenerator() const;
80
81   //! Set/Get the name of the target
82   const std::string& GetName() const;
83
84   //! Get the policy map
85   cmPolicies::PolicyMap const& GetPolicyMap() const;
86
87   //! Get policy status
88   cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
89
90 #define DECLARE_TARGET_POLICY(POLICY)                                         \
91   cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const                    \
92   {                                                                           \
93     return this->GetPolicyStatus(cmPolicies::POLICY);                         \
94   }
95
96   CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
97
98 #undef DECLARE_TARGET_POLICY
99
100   //! Get the list of the PRE_BUILD custom commands for this target
101   std::vector<cmCustomCommand> const& GetPreBuildCommands() const;
102   void AddPreBuildCommand(cmCustomCommand const& cmd);
103   void AddPreBuildCommand(cmCustomCommand&& cmd);
104
105   //! Get the list of the PRE_LINK custom commands for this target
106   std::vector<cmCustomCommand> const& GetPreLinkCommands() const;
107   void AddPreLinkCommand(cmCustomCommand const& cmd);
108   void AddPreLinkCommand(cmCustomCommand&& cmd);
109
110   //! Get the list of the POST_BUILD custom commands for this target
111   std::vector<cmCustomCommand> const& GetPostBuildCommands() const;
112   void AddPostBuildCommand(cmCustomCommand const& cmd);
113   void AddPostBuildCommand(cmCustomCommand&& cmd);
114
115   //! Add sources to the target.
116   void AddSources(std::vector<std::string> const& srcs);
117   void AddTracedSources(std::vector<std::string> const& srcs);
118   std::string GetSourceCMP0049(const std::string& src);
119   cmSourceFile* AddSource(const std::string& src, bool before = false);
120
121   //! how we identify a library, by name and type
122   using LibraryID = std::pair<std::string, cmTargetLinkLibraryType>;
123   using LinkLibraryVectorType = std::vector<LibraryID>;
124   LinkLibraryVectorType const& GetOriginalLinkLibraries() const;
125
126   //! Clear the dependency information recorded for this target, if any.
127   void ClearDependencyInformation(cmMakefile& mf) const;
128
129   void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
130                       cmTargetLinkLibraryType llt);
131
132   enum TLLSignature
133   {
134     KeywordTLLSignature,
135     PlainTLLSignature
136   };
137   bool PushTLLCommandTrace(TLLSignature signature,
138                            cmListFileContext const& lfc);
139   void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
140
141   /**
142    * Set the path where this target should be installed. This is relative to
143    * INSTALL_PREFIX
144    */
145   std::string const& GetInstallPath() const;
146   void SetInstallPath(std::string const& name);
147
148   /**
149    * Set the path where this target (if it has a runtime part) should be
150    * installed. This is relative to INSTALL_PREFIX
151    */
152   std::string const& GetRuntimeInstallPath() const;
153   void SetRuntimeInstallPath(std::string const& name);
154
155   /**
156    * Get/Set whether there is an install rule for this target.
157    */
158   bool GetHaveInstallRule() const;
159   void SetHaveInstallRule(bool hir);
160
161   void AddInstallGenerator(cmInstallTargetGenerator* g);
162   std::vector<cmInstallTargetGenerator*> const& GetInstallGenerators() const;
163
164   /**
165    * Get/Set whether this target was auto-created by a generator.
166    */
167   bool GetIsGeneratorProvided() const;
168   void SetIsGeneratorProvided(bool igp);
169
170   /**
171    * Add a utility on which this project depends. A utility is an executable
172    * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
173    * commands. It is not a full path nor does it have an extension.
174    */
175   void AddUtility(std::string const& name, bool cross,
176                   cmMakefile* mf = nullptr);
177   void AddUtility(BT<std::pair<std::string, bool>> util);
178   //! Get the utilities used by this target
179   std::set<BT<std::pair<std::string, bool>>> const& GetUtilities() const;
180
181   //! Set/Get a property of this target file
182   void SetProperty(const std::string& prop, const char* value);
183   void SetProperty(const std::string& prop, cmValue value);
184   void SetProperty(const std::string& prop, const std::string& value)
185   {
186     this->SetProperty(prop, cmValue(value));
187   }
188   void AppendProperty(
189     const std::string& prop, const std::string& value,
190     cm::optional<cmListFileBacktrace> const& bt = cm::nullopt,
191     bool asString = false);
192   //! Might return a nullptr if the property is not set or invalid
193   cmValue GetProperty(const std::string& prop) const;
194   //! Always returns a valid pointer
195   std::string const& GetSafeProperty(std::string const& prop) const;
196   bool GetPropertyAsBool(const std::string& prop) const;
197   void CheckProperty(const std::string& prop, cmMakefile* context) const;
198   cmValue GetComputedProperty(const std::string& prop, cmMakefile& mf) const;
199   //! Get all properties
200   cmPropertyMap const& GetProperties() const;
201
202   //! Return whether or not the target is for a DLL platform.
203   bool IsDLLPlatform() const;
204
205   //! Return whether or not we are targeting AIX.
206   bool IsAIX() const;
207
208   bool IsImported() const;
209   bool IsImportedGloballyVisible() const;
210   bool IsPerConfig() const;
211   bool CanCompileSources() const;
212
213   bool GetMappedConfig(std::string const& desired_config, cmValue& loc,
214                        cmValue& imp, std::string& suffix) const;
215
216   //! Return whether this target is an executable with symbol exports enabled.
217   bool IsExecutableWithExports() const;
218
219   //! Return whether this target is a shared library Framework on Apple.
220   bool IsFrameworkOnApple() const;
221
222   //! Return whether this target is an executable Bundle on Apple.
223   bool IsAppBundleOnApple() const;
224
225   //! Return whether this target is a GUI executable on Android.
226   bool IsAndroidGuiExecutable() const;
227
228   bool HasKnownObjectFileLocation(std::string* reason = nullptr) const;
229
230   //! Get a backtrace from the creation of the target.
231   cmListFileBacktrace const& GetBacktrace() const;
232
233   void InsertInclude(BT<std::string> const& entry, bool before = false);
234   void InsertCompileOption(BT<std::string> const& entry, bool before = false);
235   void InsertCompileDefinition(BT<std::string> const& entry);
236   void InsertLinkOption(BT<std::string> const& entry, bool before = false);
237   void InsertLinkDirectory(BT<std::string> const& entry, bool before = false);
238   void InsertPrecompileHeader(BT<std::string> const& entry);
239
240   void AppendBuildInterfaceIncludes();
241   void FinalizeTargetConfiguration(
242     const cmBTStringRange& noConfigCompileDefinitions,
243     cm::optional<std::map<std::string, cmValue>>& perConfigCompileDefinitions);
244
245   std::string GetDebugGeneratorExpressions(const std::string& value,
246                                            cmTargetLinkLibraryType llt) const;
247
248   void AddSystemIncludeDirectories(std::set<std::string> const& incs);
249   std::set<std::string> const& GetSystemIncludeDirectories() const;
250
251   void AddInstallIncludeDirectories(cmTargetExport const& te,
252                                     cmStringRange const& incs);
253   cmStringRange GetInstallIncludeDirectoriesEntries(
254     cmTargetExport const& te) const;
255
256   BTs<std::string> const* GetLanguageStandardProperty(
257     const std::string& propertyName) const;
258
259   void SetLanguageStandardProperty(std::string const& lang,
260                                    std::string const& value,
261                                    const std::string& feature);
262
263   cmBTStringRange GetIncludeDirectoriesEntries() const;
264
265   cmBTStringRange GetCompileOptionsEntries() const;
266
267   cmBTStringRange GetCompileFeaturesEntries() const;
268
269   cmBTStringRange GetCompileDefinitionsEntries() const;
270
271   cmBTStringRange GetPrecompileHeadersEntries() const;
272
273   cmBTStringRange GetSourceEntries() const;
274
275   cmBTStringRange GetLinkOptionsEntries() const;
276
277   cmBTStringRange GetLinkDirectoriesEntries() const;
278
279   cmBTStringRange GetLinkImplementationEntries() const;
280
281   cmBTStringRange GetLinkInterfaceEntries() const;
282   cmBTStringRange GetLinkInterfaceDirectEntries() const;
283   cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
284
285   cmBTStringRange GetHeaderSetsEntries() const;
286   cmBTStringRange GetCxxModuleSetsEntries() const;
287   cmBTStringRange GetCxxModuleHeaderSetsEntries() const;
288
289   cmBTStringRange GetInterfaceHeaderSetsEntries() const;
290   cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
291   cmBTStringRange GetInterfaceCxxModuleHeaderSetsEntries() const;
292
293   std::string ImportedGetFullPath(const std::string& config,
294                                   cmStateEnums::ArtifactType artifact) const;
295
296   struct StrictTargetComparison
297   {
298     bool operator()(cmTarget const* t1, cmTarget const* t2) const;
299   };
300
301   const cmFileSet* GetFileSet(const std::string& name) const;
302   cmFileSet* GetFileSet(const std::string& name);
303   std::pair<cmFileSet*, bool> GetOrCreateFileSet(const std::string& name,
304                                                  const std::string& type,
305                                                  cmFileSetVisibility vis);
306
307   std::vector<std::string> GetAllFileSetNames() const;
308   std::vector<std::string> GetAllInterfaceFileSets() const;
309
310   static std::string GetFileSetsPropertyName(const std::string& type);
311   static std::string GetInterfaceFileSetsPropertyName(const std::string& type);
312
313 private:
314   template <typename ValueType>
315   void StoreProperty(const std::string& prop, ValueType value);
316
317   // Internal representation details.
318   friend class cmGeneratorTarget;
319
320   const char* GetSuffixVariableInternal(
321     cmStateEnums::ArtifactType artifact) const;
322   const char* GetPrefixVariableInternal(
323     cmStateEnums::ArtifactType artifact) const;
324
325   std::unique_ptr<cmTargetInternals> impl;
326 };