1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.h,v $
6 Date: $Date: 2008-04-08 20:13:43 $
7 Version: $Revision: 1.114 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
20 #include "cmCustomCommand.h"
21 #include "cmPropertyMap.h"
22 #include "cmPolicies.h"
27 class cmGlobalGenerator
;
28 class cmComputeLinkInformation
;
29 class cmListFileBacktrace
;
31 struct cmTargetLinkInformationMap
:
32 public std::map
<cmStdString
, cmComputeLinkInformation
*>
34 typedef std::map
<cmStdString
, cmComputeLinkInformation
*> derived
;
35 cmTargetLinkInformationMap() {}
36 cmTargetLinkInformationMap(cmTargetLinkInformationMap
const& r
);
37 ~cmTargetLinkInformationMap();
40 struct cmTargetLinkInterface
42 // Libraries listed in the interface.
43 std::vector
<std::string
> Libraries
;
45 // Shared library dependencies needed for linking on some platforms.
46 std::vector
<std::string
> SharedDeps
;
49 struct cmTargetLinkInterfaceMap
:
50 public std::map
<cmStdString
, cmTargetLinkInterface
*>
52 typedef std::map
<cmStdString
, cmTargetLinkInterface
*> derived
;
53 cmTargetLinkInterfaceMap() {}
54 cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap
const& r
);
55 ~cmTargetLinkInterfaceMap();
58 class cmTargetInternals
;
59 class cmTargetInternalPointer
62 cmTargetInternalPointer();
63 cmTargetInternalPointer(cmTargetInternalPointer
const& r
);
64 ~cmTargetInternalPointer();
65 cmTargetInternalPointer
& operator=(cmTargetInternalPointer
const& r
);
66 cmTargetInternals
* operator->() const { return this->Pointer
; }
68 cmTargetInternals
* Pointer
;
72 * \brief Represent a library or executable target loaded from a makefile.
74 * cmTarget represents a target loaded from
81 enum TargetType
{ EXECUTABLE
, STATIC_LIBRARY
,
82 SHARED_LIBRARY
, MODULE_LIBRARY
, UTILITY
, GLOBAL_TARGET
,
83 INSTALL_FILES
, INSTALL_PROGRAMS
, INSTALL_DIRECTORY
};
84 static const char* TargetTypeNames
[];
85 enum CustomCommandType
{ PRE_BUILD
, PRE_LINK
, POST_BUILD
};
88 * Return the type of target.
90 TargetType
GetType() const
92 return this->TargetTypeValue
;
98 void SetType(TargetType f
, const char* name
);
100 void MarkAsImported();
102 ///! Set/Get the name of the target
103 const char* GetName() const {return this->Name
.c_str();}
105 ///! Set the cmMakefile that owns this target
106 void SetMakefile(cmMakefile
*mf
);
107 cmMakefile
*GetMakefile() const { return this->Makefile
;};
109 /** Get the status of policy CMP0003 when the target was created. */
110 cmPolicies::PolicyStatus
GetPolicyStatusCMP0003() const
111 { return this->PolicyStatusCMP0003
; }
113 /** Get the status of policy CMP0004 when the target was created. */
114 cmPolicies::PolicyStatus
GetPolicyStatusCMP0004() const
115 { return this->PolicyStatusCMP0004
; }
118 * Get the list of the custom commands for this target
120 std::vector
<cmCustomCommand
> &GetPreBuildCommands()
121 {return this->PreBuildCommands
;}
122 std::vector
<cmCustomCommand
> &GetPreLinkCommands()
123 {return this->PreLinkCommands
;}
124 std::vector
<cmCustomCommand
> &GetPostBuildCommands()
125 {return this->PostBuildCommands
;}
127 ///! Return the list of frameworks being linked to this target
128 std::vector
<std::string
> &GetFrameworks() {return this->Frameworks
;}
131 * Get the list of the source files used by this target
133 std::vector
<cmSourceFile
*> const &GetSourceFiles()
134 {return this->SourceFiles
;}
135 void AddSourceFile(cmSourceFile
* sf
) { this->SourceFiles
.push_back(sf
); }
138 * Flags for a given source file as used in this target. Typically assigned
139 * via SET_TARGET_PROPERTIES when the property is a list of source files.
143 SourceFileTypeNormal
,
144 SourceFileTypePrivateHeader
, // is in "PRIVATE_HEADER" target property
145 SourceFileTypePublicHeader
, // is in "PUBLIC_HEADER" target property
146 SourceFileTypeResource
, // is in "RESOURCE" target property *or*
147 // has MACOSX_PACKAGE_LOCATION=="Resources"
148 SourceFileTypeMacContent
// has MACOSX_PACKAGE_LOCATION!="Resources"
150 struct SourceFileFlags
152 SourceFileFlags(): Type(SourceFileTypeNormal
), MacFolder(0) {}
153 SourceFileFlags(SourceFileFlags
const& r
):
154 Type(r
.Type
), MacFolder(r
.MacFolder
) {}
156 const char* MacFolder
; // location inside Mac content folders
160 * Get the flags for a given source file as used in this target
162 struct SourceFileFlags
GetTargetSourceFileFlags(const cmSourceFile
* sf
);
165 * Add sources to the target.
167 void AddSources(std::vector
<std::string
> const& srcs
);
168 cmSourceFile
* AddSource(const char* src
);
171 * Get the list of the source files used by this target
173 enum LinkLibraryType
{GENERAL
, DEBUG
, OPTIMIZED
};
175 //* how we identify a library, by name and type
176 typedef std::pair
<cmStdString
, LinkLibraryType
> LibraryID
;
178 typedef std::vector
<LibraryID
> LinkLibraryVectorType
;
179 const LinkLibraryVectorType
&GetLinkLibraries() const {
180 return this->LinkLibraries
;}
181 const LinkLibraryVectorType
&GetOriginalLinkLibraries() const
182 {return this->OriginalLinkLibraries
;}
185 * Clear the dependency information recorded for this target, if any.
187 void ClearDependencyInformation(cmMakefile
& mf
, const char* target
);
189 // Check to see if a library is a framework and treat it different on Mac
190 bool NameResolvesToFramework(const std::string
& libname
);
191 bool AddFramework(const std::string
& lib
, LinkLibraryType llt
);
192 void AddLinkLibrary(cmMakefile
& mf
,
193 const char *target
, const char* lib
,
194 LinkLibraryType llt
);
196 void AddLinkLibrary(const std::string
& lib
,
197 LinkLibraryType llt
);
199 void MergeLinkLibraries( cmMakefile
& mf
, const char* selfname
,
200 const LinkLibraryVectorType
& libs
);
202 const std::vector
<std::string
>& GetLinkDirectories();
204 void AddLinkDirectory(const char* d
);
207 * Set the path where this target should be installed. This is relative to
210 std::string
GetInstallPath() {return this->InstallPath
;}
211 void SetInstallPath(const char *name
) {this->InstallPath
= name
;}
214 * Set the path where this target (if it has a runtime part) should be
215 * installed. This is relative to INSTALL_PREFIX
217 std::string
GetRuntimeInstallPath() {return this->RuntimeInstallPath
;}
218 void SetRuntimeInstallPath(const char *name
) {
219 this->RuntimeInstallPath
= name
; }
222 * Get/Set whether there is an install rule for this target.
224 bool GetHaveInstallRule() { return this->HaveInstallRule
; }
225 void SetHaveInstallRule(bool h
) { this->HaveInstallRule
= h
; }
227 /** Add a utility on which this project depends. A utility is an executable
228 * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
229 * commands. It is not a full path nor does it have an extension.
231 void AddUtility(const char* u
) { this->Utilities
.insert(u
);}
232 ///! Get the utilities used by this target
233 std::set
<cmStdString
>const& GetUtilities() const { return this->Utilities
; }
235 void AnalyzeLibDependencies( const cmMakefile
& mf
);
237 ///! Set/Get a property of this target file
238 void SetProperty(const char *prop
, const char *value
);
239 void AppendProperty(const char* prop
, const char* value
);
240 const char *GetProperty(const char *prop
);
241 const char *GetProperty(const char *prop
, cmProperty::ScopeType scope
);
242 bool GetPropertyAsBool(const char *prop
);
244 bool IsImported() const {return this->IsImportedTarget
;}
246 /** Get the library interface dependencies. This is the set of
247 libraries from which something that links to this target may
248 also receive symbols. Returns 0 if the user has not specified
249 such dependencies or for static libraries. */
250 cmTargetLinkInterface
const* GetLinkInterface(const char* config
);
252 /** Get the directory in which this target will be built. If the
253 configuration name is given then the generator will add its
254 subdirectory for that configuration. Otherwise just the canonical
255 output directory is given. */
256 std::string
GetDirectory(const char* config
= 0, bool implib
= false);
258 /** Get the location of the target in the build tree for the given
259 configuration. This location is suitable for use as the LOCATION
261 const char* GetLocation(const char* config
);
263 /** Get the target major and minor version numbers interpreted from
264 the VERSION property. Version 0 is returned if the property is
265 not set or cannot be parsed. */
266 void GetTargetVersion(int& major
, int& minor
);
269 * Trace through the source files in this target and add al source files
270 * that they depend on, used by all generators
272 void TraceDependencies(const char* vsProjectFile
);
274 ///! Return the prefered linker language for this target
275 const char* GetLinkerLanguage(cmGlobalGenerator
*);
277 ///! Return the rule variable used to create this type of target,
278 // need to add CMAKE_(LANG) for full name.
279 const char* GetCreateRuleVariable();
281 /** Get the full name of the target according to the settings in its
283 std::string
GetFullName(const char* config
=0, bool implib
= false);
284 void GetFullNameComponents(std::string
& prefix
,
285 std::string
& base
, std::string
& suffix
,
286 const char* config
=0, bool implib
= false);
288 /** Get the name of the pdb file for the target. */
289 std::string
GetPDBName(const char* config
=0);
291 /** Get the soname of the target. Allowed only for a shared library. */
292 std::string
GetSOName(const char* config
);
294 /** Test for special case of a third-party shared library that has
296 bool IsImportedSharedLibWithoutSOName(const char* config
);
298 /** Get the full path to the target according to the settings in its
299 makefile and the configuration type. */
300 std::string
GetFullPath(const char* config
=0, bool implib
= false,
301 bool realname
= false);
303 /** Get the names of the library needed to generate a build rule
304 that takes into account shared library version numbers. This
305 should be called only on a library target. */
306 void GetLibraryNames(std::string
& name
, std::string
& soName
,
307 std::string
& realName
, std::string
& impName
,
308 std::string
& pdbName
, const char* config
);
310 /** Get the names of the library used to remove existing copies of
311 the library from the build tree either before linking or during
312 a clean step. This should be called only on a library
314 void GetLibraryCleanNames(std::string
& staticName
,
315 std::string
& sharedName
,
316 std::string
& sharedSOName
,
317 std::string
& sharedRealName
,
318 std::string
& importName
,
319 std::string
& pdbName
,
322 /** Get the names of the executable needed to generate a build rule
323 that takes into account executable version numbers. This should
324 be called only on an executable target. */
325 void GetExecutableNames(std::string
& name
, std::string
& realName
,
326 std::string
& impName
,
327 std::string
& pdbName
, const char* config
);
329 /** Get the names of the executable used to remove existing copies
330 of the executable from the build tree either before linking or
331 during a clean step. This should be called only on an
332 executable target. */
333 void GetExecutableCleanNames(std::string
& name
, std::string
& realName
,
334 std::string
& impName
,
335 std::string
& pdbName
, const char* config
);
337 /** Add the target output files to the global generator manifest. */
338 void GenerateTargetManifest(const char* config
);
341 * Compute whether this target must be relinked before installing.
343 bool NeedRelinkBeforeInstall();
345 bool HaveBuildTreeRPATH();
346 bool HaveInstallTreeRPATH();
348 /** Return true if builtin chrpath will work for this target */
349 bool IsChrpathUsed();
351 std::string
GetInstallNameDirForBuildTree(const char* config
,
352 bool for_xcode
= false);
353 std::string
GetInstallNameDirForInstallTree(const char* config
,
354 bool for_xcode
= false);
356 cmComputeLinkInformation
* GetLinkInformation(const char* config
);
358 // Get the properties
359 cmPropertyMap
&GetProperties() { return this->Properties
; };
361 // Define the properties
362 static void DefineProperties(cmake
*cm
);
364 // Compute the OBJECT_FILES property only when requested
365 void ComputeObjectFiles();
367 /** Get the macro to define when building sources in this target.
368 If no macro should be defined null is returned. */
369 const char* GetExportMacro();
371 // Compute the set of languages compiled by the target. This is
372 // computed every time it is called because the languages can change
373 // when source file properties are changed and we do not have enough
374 // information to forward these property changes to the targets
375 // until we have per-target object file properties.
376 void GetLanguages(std::set
<cmStdString
>& languages
) const;
378 /** Return whether this target is an executable with symbol exports
380 bool IsExecutableWithExports();
382 /** Return whether this target is a shared library Framework on
384 bool IsFrameworkOnApple();
386 /** Return whether this target is an executable Bundle on Apple. */
387 bool IsAppBundleOnApple();
389 /** Return the framework version string. Undefined if
390 IsFrameworkOnApple returns false. */
391 std::string
GetFrameworkVersion();
393 /** Get a backtrace from the creation of the target. */
394 cmListFileBacktrace
const& GetBacktrace() const;
398 * A list of direct dependencies. Use in conjunction with DependencyMap.
400 typedef std::vector
< LibraryID
> DependencyList
;
403 * This map holds the dependency graph. map[x] returns a set of
404 * direct dependencies of x. Note that the direct depenencies are
405 * ordered. This is necessary to handle direct dependencies that
406 * themselves have no dependency information.
408 typedef std::map
< LibraryID
, DependencyList
> DependencyMap
;
411 * Inserts \a dep at the end of the dependency list of \a lib.
413 void InsertDependency( DependencyMap
& depMap
,
414 const LibraryID
& lib
,
415 const LibraryID
& dep
);
418 * Deletes \a dep from the dependency list of \a lib.
420 void DeleteDependency( DependencyMap
& depMap
,
421 const LibraryID
& lib
,
422 const LibraryID
& dep
);
425 * Emits the library \a lib and all its dependencies into link_line.
426 * \a emitted keeps track of the libraries that have been emitted to
427 * avoid duplicates--it is more efficient than searching
428 * link_line. \a visited is used detect cycles. Note that \a
429 * link_line is in reverse order, in that the dependencies of a
430 * library are listed before the library itself.
432 void Emit( const LibraryID lib
,
433 const DependencyMap
& dep_map
,
434 std::set
<LibraryID
>& emitted
,
435 std::set
<LibraryID
>& visited
,
436 DependencyList
& link_line
);
439 * Finds the dependencies for \a lib and inserts them into \a
442 void GatherDependencies( const cmMakefile
& mf
,
443 const LibraryID
& lib
,
444 DependencyMap
& dep_map
);
446 const char* GetSuffixVariableInternal(TargetType type
, bool implib
);
447 const char* GetPrefixVariableInternal(TargetType type
, bool implib
);
448 std::string
GetFullNameInternal(TargetType type
, const char* config
,
450 void GetFullNameInternal(TargetType type
, const char* config
, bool implib
,
451 std::string
& outPrefix
, std::string
& outBase
,
452 std::string
& outSuffix
);
454 void GetLibraryNamesInternal(std::string
& name
,
456 std::string
& realName
,
457 std::string
& impName
,
458 std::string
& pdbName
,
461 void GetExecutableNamesInternal(std::string
& name
,
462 std::string
& realName
,
463 std::string
& impName
,
464 std::string
& pdbName
,
468 // Use a makefile variable to set a default for the given property.
469 // If the variable is not defined use the given default instead.
470 void SetPropertyDefault(const char* property
, const char* default_value
);
472 // Get the full path to the target output directory.
473 std::string
GetOutputDir(bool implib
);
474 std::string
const& ComputeBaseOutputDir(bool implib
);
476 const char* ImportedGetLocation(const char* config
);
477 const char* NormalGetLocation(const char* config
);
479 std::string
GetFullNameImported(const char* config
, bool implib
);
481 std::string
ImportedGetFullPath(const char* config
, bool implib
);
482 std::string
NormalGetFullPath(const char* config
, bool implib
,
485 /** Get the real name of the target. Allowed only for non-imported
486 targets. When a library or executable file is versioned this is
487 the full versioned name. If the target is not versioned this is
488 the same as GetFullName. */
489 std::string
NormalGetRealName(const char* config
);
493 std::vector
<cmCustomCommand
> PreBuildCommands
;
494 std::vector
<cmCustomCommand
> PreLinkCommands
;
495 std::vector
<cmCustomCommand
> PostBuildCommands
;
496 TargetType TargetTypeValue
;
497 std::vector
<cmSourceFile
*> SourceFiles
;
498 LinkLibraryVectorType LinkLibraries
;
499 LinkLibraryVectorType PrevLinkedLibraries
;
500 bool LinkLibrariesAnalyzed
;
501 std::vector
<std::string
> Frameworks
;
502 std::vector
<std::string
> LinkDirectories
;
503 std::set
<cmStdString
> LinkDirectoriesEmmitted
;
504 bool HaveInstallRule
;
505 std::string InstallPath
;
506 std::string RuntimeInstallPath
;
507 std::string BaseOutputDir
;
508 std::string BaseOutputDirImplib
;
509 std::string Location
;
510 std::string ExportMacro
;
511 std::set
<cmStdString
> Utilities
;
512 bool RecordDependencies
;
513 cmPropertyMap Properties
;
514 LinkLibraryVectorType OriginalLinkLibraries
;
516 bool IsImportedTarget
;
518 // Cache import information from properties for each configuration.
522 std::string Location
;
524 std::string ImportLibrary
;
525 cmTargetLinkInterface LinkInterface
;
527 typedef std::map
<cmStdString
, ImportInfo
> ImportInfoMapType
;
528 ImportInfoMapType ImportInfoMap
;
529 ImportInfo
const* GetImportInfo(const char* config
);
530 void ComputeImportInfo(std::string
const& desired_config
, ImportInfo
& info
);
532 cmTargetLinkInformationMap LinkInformation
;
535 cmTargetLinkInterface
* ComputeLinkInterface(const char* config
);
536 cmTargetLinkInterfaceMap LinkInterface
;
538 // The cmMakefile instance that owns this target. This should
540 cmMakefile
* Makefile
;
542 // Policy status recorded when target was created.
543 cmPolicies::PolicyStatus PolicyStatusCMP0003
;
544 cmPolicies::PolicyStatus PolicyStatusCMP0004
;
546 // Internal representation details.
547 friend class cmTargetInternals
;
548 cmTargetInternalPointer Internal
;
550 void ConstructSourceFileFlags();
553 typedef std::map
<cmStdString
,cmTarget
> cmTargets
;
555 class cmTargetSet
: public std::set
<cmStdString
> {};
556 class cmTargetManifest
: public std::map
<cmStdString
, cmTargetSet
> {};