1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.h,v $
6 Date: $Date: 2008-03-01 17:51:07 $
7 Version: $Revision: 1.108 $
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"
26 class cmGlobalGenerator
;
27 class cmComputeLinkInformation
;
29 struct cmTargetLinkInformationMap
:
30 public std::map
<cmStdString
, cmComputeLinkInformation
*>
32 typedef std::map
<cmStdString
, cmComputeLinkInformation
*> derived
;
33 cmTargetLinkInformationMap() {}
34 cmTargetLinkInformationMap(cmTargetLinkInformationMap
const& r
);
35 ~cmTargetLinkInformationMap();
38 struct cmTargetLinkInterface
40 // Libraries listed in the interface.
41 std::vector
<std::string
> Libraries
;
43 // Shared library dependencies needed for linking on some platforms.
44 std::vector
<std::string
> SharedDeps
;
47 struct cmTargetLinkInterfaceMap
:
48 public std::map
<cmStdString
, cmTargetLinkInterface
*>
50 typedef std::map
<cmStdString
, cmTargetLinkInterface
*> derived
;
51 cmTargetLinkInterfaceMap() {}
52 cmTargetLinkInterfaceMap(cmTargetLinkInterfaceMap
const& r
);
53 ~cmTargetLinkInterfaceMap();
56 class cmTargetInternals
;
57 class cmTargetInternalPointer
60 cmTargetInternalPointer();
61 cmTargetInternalPointer(cmTargetInternalPointer
const& r
);
62 ~cmTargetInternalPointer();
63 cmTargetInternalPointer
& operator=(cmTargetInternalPointer
const& r
);
64 cmTargetInternals
* operator->() const { return this->Pointer
; }
66 cmTargetInternals
* Pointer
;
70 * \brief Represent a library or executable target loaded from a makefile.
72 * cmTarget represents a target loaded from
79 enum TargetType
{ EXECUTABLE
, STATIC_LIBRARY
,
80 SHARED_LIBRARY
, MODULE_LIBRARY
, UTILITY
, GLOBAL_TARGET
,
81 INSTALL_FILES
, INSTALL_PROGRAMS
, INSTALL_DIRECTORY
};
82 static const char* TargetTypeNames
[];
83 enum CustomCommandType
{ PRE_BUILD
, PRE_LINK
, POST_BUILD
};
86 * Return the type of target.
88 TargetType
GetType() const
90 return this->TargetTypeValue
;
96 void SetType(TargetType f
, const char* name
);
98 void MarkAsImported();
100 ///! Set/Get the name of the target
101 const char* GetName() const {return this->Name
.c_str();}
103 ///! Set the cmMakefile that owns this target
104 void SetMakefile(cmMakefile
*mf
);
105 cmMakefile
*GetMakefile() const { return this->Makefile
;};
108 * Get the list of the custom commands for this target
110 std::vector
<cmCustomCommand
> &GetPreBuildCommands()
111 {return this->PreBuildCommands
;}
112 std::vector
<cmCustomCommand
> &GetPreLinkCommands()
113 {return this->PreLinkCommands
;}
114 std::vector
<cmCustomCommand
> &GetPostBuildCommands()
115 {return this->PostBuildCommands
;}
117 ///! Return the list of frameworks being linked to this target
118 std::vector
<std::string
> &GetFrameworks() {return this->Frameworks
;}
121 * Get the list of the source files used by this target
123 std::vector
<cmSourceFile
*> const &GetSourceFiles()
124 {return this->SourceFiles
;}
125 void AddSourceFile(cmSourceFile
* sf
) { this->SourceFiles
.push_back(sf
); }
128 * Flags for a given source file as used in this target. Typically assigned
129 * via SET_TARGET_PROPERTIES when the property is a list of source files.
133 SourceFileTypeNormal
,
134 SourceFileTypePrivateHeader
, // is in "PRIVATE_HEADER" target property
135 SourceFileTypePublicHeader
, // is in "PUBLIC_HEADER" target property
136 SourceFileTypeResource
, // is in "RESOURCE" target property *or*
137 // has MACOSX_PACKAGE_LOCATION=="Resources"
138 SourceFileTypeMacContent
// has MACOSX_PACKAGE_LOCATION!="Resources"
140 struct SourceFileFlags
142 SourceFileFlags(): Type(SourceFileTypeNormal
), MacFolder(0) {}
143 SourceFileFlags(SourceFileFlags
const& r
):
144 Type(r
.Type
), MacFolder(r
.MacFolder
) {}
146 const char* MacFolder
; // location inside Mac content folders
150 * Get the flags for a given source file as used in this target
152 struct SourceFileFlags
GetTargetSourceFileFlags(const cmSourceFile
* sf
);
155 * Add sources to the target.
157 void AddSources(std::vector
<std::string
> const& srcs
);
158 cmSourceFile
* AddSource(const char* src
);
161 * Get the list of the source files used by this target
163 enum LinkLibraryType
{GENERAL
, DEBUG
, OPTIMIZED
};
165 //* how we identify a library, by name and type
166 typedef std::pair
<cmStdString
, LinkLibraryType
> LibraryID
;
168 typedef std::vector
<LibraryID
> LinkLibraryVectorType
;
169 const LinkLibraryVectorType
&GetLinkLibraries() const {
170 return this->LinkLibraries
;}
171 const LinkLibraryVectorType
&GetOriginalLinkLibraries() const
172 {return this->OriginalLinkLibraries
;}
175 * Clear the dependency information recorded for this target, if any.
177 void ClearDependencyInformation(cmMakefile
& mf
, const char* target
);
179 // Check to see if a library is a framework and treat it different on Mac
180 bool NameResolvesToFramework(const std::string
& libname
);
181 bool AddFramework(const std::string
& lib
, LinkLibraryType llt
);
182 void AddLinkLibrary(cmMakefile
& mf
,
183 const char *target
, const char* lib
,
184 LinkLibraryType llt
);
186 void AddLinkLibrary(const std::string
& lib
,
187 LinkLibraryType llt
);
189 void MergeLinkLibraries( cmMakefile
& mf
, const char* selfname
,
190 const LinkLibraryVectorType
& libs
);
192 const std::vector
<std::string
>& GetLinkDirectories();
194 void AddLinkDirectory(const char* d
);
197 * Set the path where this target should be installed. This is relative to
200 std::string
GetInstallPath() {return this->InstallPath
;}
201 void SetInstallPath(const char *name
) {this->InstallPath
= name
;}
204 * Set the path where this target (if it has a runtime part) should be
205 * installed. This is relative to INSTALL_PREFIX
207 std::string
GetRuntimeInstallPath() {return this->RuntimeInstallPath
;}
208 void SetRuntimeInstallPath(const char *name
) {
209 this->RuntimeInstallPath
= name
; }
212 * Get/Set whether there is an install rule for this target.
214 bool GetHaveInstallRule() { return this->HaveInstallRule
; }
215 void SetHaveInstallRule(bool h
) { this->HaveInstallRule
= h
; }
217 /** Add a utility on which this project depends. A utility is an executable
218 * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
219 * commands. It is not a full path nor does it have an extension.
221 void AddUtility(const char* u
) { this->Utilities
.insert(u
);}
222 ///! Get the utilities used by this target
223 std::set
<cmStdString
>const& GetUtilities() const { return this->Utilities
; }
225 void AnalyzeLibDependencies( const cmMakefile
& mf
);
227 ///! Set/Get a property of this target file
228 void SetProperty(const char *prop
, const char *value
);
229 void AppendProperty(const char* prop
, const char* value
);
230 const char *GetProperty(const char *prop
);
231 const char *GetProperty(const char *prop
, cmProperty::ScopeType scope
);
232 bool GetPropertyAsBool(const char *prop
);
234 bool IsImported() const {return this->IsImportedTarget
;}
236 /** Get the library interface dependencies. This is the set of
237 libraries from which something that links to this target may
238 also receive symbols. Returns 0 if the user has not specified
239 such dependencies or for static libraries. */
240 cmTargetLinkInterface
const* GetLinkInterface(const char* config
);
242 /** Get the directory in which this target will be built. If the
243 configuration name is given then the generator will add its
244 subdirectory for that configuration. Otherwise just the canonical
245 output directory is given. */
246 const char* GetDirectory(const char* config
= 0, bool implib
= false);
248 /** Get the location of the target in the build tree for the given
249 configuration. This location is suitable for use as the LOCATION
251 const char* GetLocation(const char* config
);
253 /** Get the target major and minor version numbers interpreted from
254 the VERSION property. Version 0 is returned if the property is
255 not set or cannot be parsed. */
256 void GetTargetVersion(int& major
, int& minor
);
259 * Trace through the source files in this target and add al source files
260 * that they depend on, used by all generators
262 void TraceDependencies(const char* vsProjectFile
);
264 ///! Return the prefered linker language for this target
265 const char* GetLinkerLanguage(cmGlobalGenerator
*);
267 ///! Return the rule variable used to create this type of target,
268 // need to add CMAKE_(LANG) for full name.
269 const char* GetCreateRuleVariable();
271 /** Get the full name of the target according to the settings in its
273 std::string
GetFullName(const char* config
=0, bool implib
= false);
274 void GetFullNameComponents(std::string
& prefix
,
275 std::string
& base
, std::string
& suffix
,
276 const char* config
=0, bool implib
= false);
278 /** Get the name of the pdb file for the target. */
279 std::string
GetPDBName(const char* config
=0);
281 /** Get the soname of the target. Allowed only for a shared library. */
282 std::string
GetSOName(const char* config
);
284 /** Test for special case of a third-party shared library that has
286 bool IsImportedSharedLibWithoutSOName(const char* config
);
288 /** Get the full path to the target according to the settings in its
289 makefile and the configuration type. */
290 std::string
GetFullPath(const char* config
=0, bool implib
= false,
291 bool realname
= false);
293 /** Get the names of the library needed to generate a build rule
294 that takes into account shared library version numbers. This
295 should be called only on a library target. */
296 void GetLibraryNames(std::string
& name
, std::string
& soName
,
297 std::string
& realName
, std::string
& impName
,
298 std::string
& pdbName
, const char* config
);
300 /** Get the names of the library used to remove existing copies of
301 the library from the build tree either before linking or during
302 a clean step. This should be called only on a library
304 void GetLibraryCleanNames(std::string
& staticName
,
305 std::string
& sharedName
,
306 std::string
& sharedSOName
,
307 std::string
& sharedRealName
,
308 std::string
& importName
,
309 std::string
& pdbName
,
312 /** Get the names of the executable needed to generate a build rule
313 that takes into account executable version numbers. This should
314 be called only on an executable target. */
315 void GetExecutableNames(std::string
& name
, std::string
& realName
,
316 std::string
& impName
,
317 std::string
& pdbName
, const char* config
);
319 /** Get the names of the executable used to remove existing copies
320 of the executable from the build tree either before linking or
321 during a clean step. This should be called only on an
322 executable target. */
323 void GetExecutableCleanNames(std::string
& name
, std::string
& realName
,
324 std::string
& impName
,
325 std::string
& pdbName
, const char* config
);
327 /** Add the target output files to the global generator manifest. */
328 void GenerateTargetManifest(const char* config
);
331 * Compute whether this target must be relinked before installing.
333 bool NeedRelinkBeforeInstall();
335 bool HaveBuildTreeRPATH();
336 bool HaveInstallTreeRPATH();
338 /** Return true if builtin chrpath will work for this target */
339 bool IsChrpathUsed();
341 std::string
GetInstallNameDirForBuildTree(const char* config
);
342 std::string
GetInstallNameDirForInstallTree(const char* config
);
344 cmComputeLinkInformation
* GetLinkInformation(const char* config
);
346 // Get the properties
347 cmPropertyMap
&GetProperties() { return this->Properties
; };
349 // Define the properties
350 static void DefineProperties(cmake
*cm
);
352 // Compute the OBJECT_FILES property only when requested
353 void ComputeObjectFiles();
355 /** Get the macro to define when building sources in this target.
356 If no macro should be defined null is returned. */
357 const char* GetExportMacro();
359 // Compute the set of languages compiled by the target. This is
360 // computed every time it is called because the languages can change
361 // when source file properties are changed and we do not have enough
362 // information to forward these property changes to the targets
363 // until we have per-target object file properties.
364 void GetLanguages(std::set
<cmStdString
>& languages
) const;
366 /** Return whether this target is an executable with symbol exports
368 bool IsExecutableWithExports();
370 /** Return whether this target is a shared library Framework on
372 bool IsFrameworkOnApple();
374 /** Return whether this target is an executable Bundle on Apple. */
375 bool IsAppBundleOnApple();
379 * A list of direct dependencies. Use in conjunction with DependencyMap.
381 typedef std::vector
< LibraryID
> DependencyList
;
384 * This map holds the dependency graph. map[x] returns a set of
385 * direct dependencies of x. Note that the direct depenencies are
386 * ordered. This is necessary to handle direct dependencies that
387 * themselves have no dependency information.
389 typedef std::map
< LibraryID
, DependencyList
> DependencyMap
;
392 * Inserts \a dep at the end of the dependency list of \a lib.
394 void InsertDependency( DependencyMap
& depMap
,
395 const LibraryID
& lib
,
396 const LibraryID
& dep
);
399 * Deletes \a dep from the dependency list of \a lib.
401 void DeleteDependency( DependencyMap
& depMap
,
402 const LibraryID
& lib
,
403 const LibraryID
& dep
);
406 * Emits the library \a lib and all its dependencies into link_line.
407 * \a emitted keeps track of the libraries that have been emitted to
408 * avoid duplicates--it is more efficient than searching
409 * link_line. \a visited is used detect cycles. Note that \a
410 * link_line is in reverse order, in that the dependencies of a
411 * library are listed before the library itself.
413 void Emit( const LibraryID lib
,
414 const DependencyMap
& dep_map
,
415 std::set
<LibraryID
>& emitted
,
416 std::set
<LibraryID
>& visited
,
417 DependencyList
& link_line
);
420 * Finds the dependencies for \a lib and inserts them into \a
423 void GatherDependencies( const cmMakefile
& mf
,
424 const LibraryID
& lib
,
425 DependencyMap
& dep_map
);
427 const char* GetSuffixVariableInternal(TargetType type
, bool implib
);
428 const char* GetPrefixVariableInternal(TargetType type
, bool implib
);
429 std::string
GetFullNameInternal(TargetType type
, const char* config
,
431 void GetFullNameInternal(TargetType type
, const char* config
, bool implib
,
432 std::string
& outPrefix
, std::string
& outBase
,
433 std::string
& outSuffix
);
435 void GetLibraryNamesInternal(std::string
& name
,
437 std::string
& realName
,
438 std::string
& impName
,
439 std::string
& pdbName
,
442 void GetExecutableNamesInternal(std::string
& name
,
443 std::string
& realName
,
444 std::string
& impName
,
445 std::string
& pdbName
,
449 // Use a makefile variable to set a default for the given property.
450 // If the variable is not defined use the given default instead.
451 void SetPropertyDefault(const char* property
, const char* default_value
);
453 // Get the full path to the target output directory.
454 const char* GetAndCreateOutputDir(bool implib
, bool create
);
456 // Get the full path to the target output directory.
457 const char* GetOutputDir(bool implib
);
459 const char* ImportedGetLocation(const char* config
);
460 const char* NormalGetLocation(const char* config
);
462 std::string
GetFullNameImported(const char* config
, bool implib
);
464 const char* ImportedGetDirectory(const char* config
, bool implib
);
465 const char* NormalGetDirectory(const char* config
, bool implib
);
467 std::string
ImportedGetFullPath(const char* config
, bool implib
);
468 std::string
NormalGetFullPath(const char* config
, bool implib
,
471 /** Get the real name of the target. Allowed only for non-imported
472 targets. When a library or executable file is versioned this is
473 the full versioned name. If the target is not versioned this is
474 the same as GetFullName. */
475 std::string
NormalGetRealName(const char* config
);
479 std::vector
<cmCustomCommand
> PreBuildCommands
;
480 std::vector
<cmCustomCommand
> PreLinkCommands
;
481 std::vector
<cmCustomCommand
> PostBuildCommands
;
482 TargetType TargetTypeValue
;
483 std::vector
<cmSourceFile
*> SourceFiles
;
484 LinkLibraryVectorType LinkLibraries
;
485 LinkLibraryVectorType PrevLinkedLibraries
;
486 bool LinkLibrariesAnalyzed
;
487 std::vector
<std::string
> Frameworks
;
488 std::vector
<std::string
> LinkDirectories
;
489 std::set
<cmStdString
> LinkDirectoriesEmmitted
;
490 bool HaveInstallRule
;
491 std::string InstallPath
;
492 std::string RuntimeInstallPath
;
493 std::string OutputDir
;
494 std::string OutputDirImplib
;
495 std::string Directory
;
496 std::string Location
;
497 std::string ExportMacro
;
498 std::set
<cmStdString
> Utilities
;
499 bool RecordDependencies
;
500 cmPropertyMap Properties
;
501 LinkLibraryVectorType OriginalLinkLibraries
;
503 bool IsImportedTarget
;
505 // Cache import information from properties for each configuration.
509 std::string Location
;
511 std::string ImportLibrary
;
512 cmTargetLinkInterface LinkInterface
;
514 typedef std::map
<cmStdString
, ImportInfo
> ImportInfoMapType
;
515 ImportInfoMapType ImportInfoMap
;
516 ImportInfo
const* GetImportInfo(const char* config
);
517 void ComputeImportInfo(std::string
const& desired_config
, ImportInfo
& info
);
519 cmTargetLinkInformationMap LinkInformation
;
522 cmTargetLinkInterface
* ComputeLinkInterface(const char* config
);
523 cmTargetLinkInterfaceMap LinkInterface
;
525 // The cmMakefile instance that owns this target. This should
527 cmMakefile
* Makefile
;
529 // Internal representation details.
530 friend class cmTargetInternals
;
531 cmTargetInternalPointer Internal
;
533 void ConstructSourceFileFlags();
536 typedef std::map
<cmStdString
,cmTarget
> cmTargets
;
538 class cmTargetSet
: public std::set
<cmStdString
> {};
539 class cmTargetManifest
: public std::map
<cmStdString
, cmTargetSet
> {};