1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
15 #include <cm/optional>
17 #include "cmAlgorithms.h"
18 #include "cmFileSet.h"
19 #include "cmPolicies.h"
20 #include "cmStateTypes.h"
21 #include "cmStringAlgorithms.h"
22 #include "cmTargetLinkLibraryType.h"
25 class cmCustomCommand;
26 class cmGlobalGenerator;
27 class cmInstallTargetGenerator;
28 class cmListFileBacktrace;
29 class cmListFileContext;
34 class cmTargetInternals;
42 * \brief Represent a library or executable target loaded from a makefile.
44 * cmTarget represents a target loaded from a makefile.
53 VisibilityImportedGlobally
62 cmTarget(std::string const& name, cmStateEnums::TargetType type,
63 Visibility vis, cmMakefile* mf, PerConfig perConfig);
65 cmTarget(cmTarget const&) = delete;
66 cmTarget(cmTarget&&) noexcept;
69 cmTarget& operator=(cmTarget const&) = delete;
70 cmTarget& operator=(cmTarget&&) noexcept;
72 //! Return the type of target.
73 cmStateEnums::TargetType GetType() const;
75 //! Get the cmMakefile that owns this target.
76 cmMakefile* GetMakefile() const;
78 //! Return the global generator.
79 cmGlobalGenerator* GetGlobalGenerator() const;
81 //! Set/Get the name of the target
82 const std::string& GetName() const;
84 //! Get the policy map
85 cmPolicies::PolicyMap const& GetPolicyMap() const;
88 cmPolicies::PolicyStatus GetPolicyStatus(cmPolicies::PolicyID policy) const;
90 #define DECLARE_TARGET_POLICY(POLICY) \
91 cmPolicies::PolicyStatus GetPolicyStatus##POLICY() const \
93 return this->GetPolicyStatus(cmPolicies::POLICY); \
96 CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
98 #undef DECLARE_TARGET_POLICY
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);
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);
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);
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);
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;
126 //! Clear the dependency information recorded for this target, if any.
127 void ClearDependencyInformation(cmMakefile& mf) const;
129 void AddLinkLibrary(cmMakefile& mf, std::string const& lib,
130 cmTargetLinkLibraryType llt);
137 bool PushTLLCommandTrace(TLLSignature signature,
138 cmListFileContext const& lfc);
139 void GetTllSignatureTraces(std::ostream& s, TLLSignature sig) const;
142 * Set the path where this target should be installed. This is relative to
145 std::string const& GetInstallPath() const;
146 void SetInstallPath(std::string const& name);
149 * Set the path where this target (if it has a runtime part) should be
150 * installed. This is relative to INSTALL_PREFIX
152 std::string const& GetRuntimeInstallPath() const;
153 void SetRuntimeInstallPath(std::string const& name);
156 * Get/Set whether there is an install rule for this target.
158 bool GetHaveInstallRule() const;
159 void SetHaveInstallRule(bool hir);
161 void AddInstallGenerator(cmInstallTargetGenerator* g);
162 std::vector<cmInstallTargetGenerator*> const& GetInstallGenerators() const;
165 * Get/Set whether this target was auto-created by a generator.
167 bool GetIsGeneratorProvided() const;
168 void SetIsGeneratorProvided(bool igp);
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.
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;
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)
186 this->SetProperty(prop, cmValue(value));
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;
202 //! Return whether or not the target is for a DLL platform.
203 bool IsDLLPlatform() const;
205 //! Return whether or not we are targeting AIX.
208 bool IsImported() const;
209 bool IsImportedGloballyVisible() const;
210 bool IsPerConfig() const;
211 bool CanCompileSources() const;
213 bool GetMappedConfig(std::string const& desired_config, cmValue& loc,
214 cmValue& imp, std::string& suffix) const;
216 //! Return whether this target is an executable with symbol exports enabled.
217 bool IsExecutableWithExports() const;
219 //! Return whether this target is a shared library Framework on Apple.
220 bool IsFrameworkOnApple() const;
222 //! Return whether this target is an executable Bundle on Apple.
223 bool IsAppBundleOnApple() const;
225 //! Return whether this target is a GUI executable on Android.
226 bool IsAndroidGuiExecutable() const;
228 bool HasKnownObjectFileLocation(std::string* reason = nullptr) const;
230 //! Get a backtrace from the creation of the target.
231 cmListFileBacktrace const& GetBacktrace() const;
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);
240 void AppendBuildInterfaceIncludes();
241 void FinalizeTargetConfiguration(
242 const cmBTStringRange& noConfigCompileDefinitions,
243 cm::optional<std::map<std::string, cmValue>>& perConfigCompileDefinitions);
245 std::string GetDebugGeneratorExpressions(const std::string& value,
246 cmTargetLinkLibraryType llt) const;
248 void AddSystemIncludeDirectories(std::set<std::string> const& incs);
249 std::set<std::string> const& GetSystemIncludeDirectories() const;
251 void AddInstallIncludeDirectories(cmTargetExport const& te,
252 cmStringRange const& incs);
253 cmStringRange GetInstallIncludeDirectoriesEntries(
254 cmTargetExport const& te) const;
256 BTs<std::string> const* GetLanguageStandardProperty(
257 const std::string& propertyName) const;
259 void SetLanguageStandardProperty(std::string const& lang,
260 std::string const& value,
261 const std::string& feature);
263 cmBTStringRange GetIncludeDirectoriesEntries() const;
265 cmBTStringRange GetCompileOptionsEntries() const;
267 cmBTStringRange GetCompileFeaturesEntries() const;
269 cmBTStringRange GetCompileDefinitionsEntries() const;
271 cmBTStringRange GetPrecompileHeadersEntries() const;
273 cmBTStringRange GetSourceEntries() const;
275 cmBTStringRange GetLinkOptionsEntries() const;
277 cmBTStringRange GetLinkDirectoriesEntries() const;
279 cmBTStringRange GetLinkImplementationEntries() const;
281 cmBTStringRange GetLinkInterfaceEntries() const;
282 cmBTStringRange GetLinkInterfaceDirectEntries() const;
283 cmBTStringRange GetLinkInterfaceDirectExcludeEntries() const;
285 cmBTStringRange GetHeaderSetsEntries() const;
286 cmBTStringRange GetCxxModuleSetsEntries() const;
287 cmBTStringRange GetCxxModuleHeaderSetsEntries() const;
289 cmBTStringRange GetInterfaceHeaderSetsEntries() const;
290 cmBTStringRange GetInterfaceCxxModuleSetsEntries() const;
291 cmBTStringRange GetInterfaceCxxModuleHeaderSetsEntries() const;
293 std::string ImportedGetFullPath(const std::string& config,
294 cmStateEnums::ArtifactType artifact) const;
296 struct StrictTargetComparison
298 bool operator()(cmTarget const* t1, cmTarget const* t2) const;
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);
307 std::vector<std::string> GetAllFileSetNames() const;
308 std::vector<std::string> GetAllInterfaceFileSets() const;
310 static std::string GetFileSetsPropertyName(const std::string& type);
311 static std::string GetInterfaceFileSetsPropertyName(const std::string& type);
314 template <typename ValueType>
315 void StoreProperty(const std::string& prop, ValueType value);
317 // Internal representation details.
318 friend class cmGeneratorTarget;
320 const char* GetSuffixVariableInternal(
321 cmStateEnums::ArtifactType artifact) const;
322 const char* GetPrefixVariableInternal(
323 cmStateEnums::ArtifactType artifact) const;
325 std::unique_ptr<cmTargetInternals> impl;