KWSys Nightly Date Stamp
[cmake.git] / Source / cmTarget.h
blob497cb7789b787d958cd4c5a7a2dfbf95686b2a01
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.h,v $
5 Language: C++
6 Date: $Date: 2009-09-07 14:11:43 $
7 Version: $Revision: 1.142 $
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 =========================================================================*/
17 #ifndef cmTarget_h
18 #define cmTarget_h
20 #include "cmCustomCommand.h"
21 #include "cmPropertyMap.h"
22 #include "cmPolicies.h"
24 #include <cmsys/auto_ptr.hxx>
26 class cmake;
27 class cmMakefile;
28 class cmSourceFile;
29 class cmGlobalGenerator;
30 class cmComputeLinkInformation;
31 class cmListFileBacktrace;
33 struct cmTargetLinkInformationMap:
34 public std::map<cmStdString, cmComputeLinkInformation*>
36 typedef std::map<cmStdString, cmComputeLinkInformation*> derived;
37 cmTargetLinkInformationMap() {}
38 cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r);
39 ~cmTargetLinkInformationMap();
42 class cmTargetInternals;
43 class cmTargetInternalPointer
45 public:
46 cmTargetInternalPointer();
47 cmTargetInternalPointer(cmTargetInternalPointer const& r);
48 ~cmTargetInternalPointer();
49 cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
50 cmTargetInternals* operator->() const { return this->Pointer; }
51 cmTargetInternals* Get() const { return this->Pointer; }
52 private:
53 cmTargetInternals* Pointer;
56 /** \class cmTarget
57 * \brief Represent a library or executable target loaded from a makefile.
59 * cmTarget represents a target loaded from
60 * a makefile.
62 class cmTarget
64 public:
65 cmTarget();
66 enum TargetType { EXECUTABLE, STATIC_LIBRARY,
67 SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, GLOBAL_TARGET,
68 INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY,
69 UNKNOWN_LIBRARY};
70 static const char* TargetTypeNames[];
71 enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
73 /**
74 * Return the type of target.
76 TargetType GetType() const
78 return this->TargetTypeValue;
81 /**
82 * Set the target type
84 void SetType(TargetType f, const char* name);
86 void MarkAsImported();
88 ///! Set/Get the name of the target
89 const char* GetName() const {return this->Name.c_str();}
91 ///! Set the cmMakefile that owns this target
92 void SetMakefile(cmMakefile *mf);
93 cmMakefile *GetMakefile() const { return this->Makefile;};
95 /** Get the status of policy CMP0003 when the target was created. */
96 cmPolicies::PolicyStatus GetPolicyStatusCMP0003() const
97 { return this->PolicyStatusCMP0003; }
99 /** Get the status of policy CMP0004 when the target was created. */
100 cmPolicies::PolicyStatus GetPolicyStatusCMP0004() const
101 { return this->PolicyStatusCMP0004; }
103 /** Get the status of policy CMP0008 when the target was created. */
104 cmPolicies::PolicyStatus GetPolicyStatusCMP0008() const
105 { return this->PolicyStatusCMP0008; }
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 void AddSourceFile(cmSourceFile* sf);
126 /** Get sources that must be built before the given source. */
127 std::vector<cmSourceFile*> const* GetSourceDepends(cmSourceFile* sf);
130 * Flags for a given source file as used in this target. Typically assigned
131 * via SET_TARGET_PROPERTIES when the property is a list of source files.
133 enum SourceFileType
135 SourceFileTypeNormal,
136 SourceFileTypePrivateHeader, // is in "PRIVATE_HEADER" target property
137 SourceFileTypePublicHeader, // is in "PUBLIC_HEADER" target property
138 SourceFileTypeResource, // is in "RESOURCE" target property *or*
139 // has MACOSX_PACKAGE_LOCATION=="Resources"
140 SourceFileTypeMacContent // has MACOSX_PACKAGE_LOCATION!="Resources"
142 struct SourceFileFlags
144 SourceFileFlags(): Type(SourceFileTypeNormal), MacFolder(0) {}
145 SourceFileFlags(SourceFileFlags const& r):
146 Type(r.Type), MacFolder(r.MacFolder) {}
147 SourceFileType Type;
148 const char* MacFolder; // location inside Mac content folders
152 * Get the flags for a given source file as used in this target
154 struct SourceFileFlags GetTargetSourceFileFlags(const cmSourceFile* sf);
157 * Add sources to the target.
159 void AddSources(std::vector<std::string> const& srcs);
160 cmSourceFile* AddSource(const char* src);
163 * Get the list of the source files used by this target
165 enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
167 //* how we identify a library, by name and type
168 typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
170 typedef std::vector<LibraryID > LinkLibraryVectorType;
171 const LinkLibraryVectorType &GetLinkLibraries() const {
172 return this->LinkLibraries;}
173 const LinkLibraryVectorType &GetOriginalLinkLibraries() const
174 {return this->OriginalLinkLibraries;}
176 /** Compute the link type to use for the given configuration. */
177 LinkLibraryType ComputeLinkType(const char* config);
180 * Clear the dependency information recorded for this target, if any.
182 void ClearDependencyInformation(cmMakefile& mf, const char* target);
184 // Check to see if a library is a framework and treat it different on Mac
185 bool NameResolvesToFramework(const std::string& libname);
186 bool AddFramework(const std::string& lib, LinkLibraryType llt);
187 void AddLinkLibrary(cmMakefile& mf,
188 const char *target, const char* lib,
189 LinkLibraryType llt);
191 void AddLinkLibrary(const std::string& lib,
192 LinkLibraryType llt);
194 void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
195 const LinkLibraryVectorType& libs );
197 const std::vector<std::string>& GetLinkDirectories();
199 void AddLinkDirectory(const char* d);
202 * Set the path where this target should be installed. This is relative to
203 * INSTALL_PREFIX
205 std::string GetInstallPath() {return this->InstallPath;}
206 void SetInstallPath(const char *name) {this->InstallPath = name;}
209 * Set the path where this target (if it has a runtime part) should be
210 * installed. This is relative to INSTALL_PREFIX
212 std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
213 void SetRuntimeInstallPath(const char *name) {
214 this->RuntimeInstallPath = name; }
217 * Get/Set whether there is an install rule for this target.
219 bool GetHaveInstallRule() { return this->HaveInstallRule; }
220 void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
222 /** Add a utility on which this project depends. A utility is an executable
223 * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
224 * commands. It is not a full path nor does it have an extension.
226 void AddUtility(const char* u) { this->Utilities.insert(u);}
227 ///! Get the utilities used by this target
228 std::set<cmStdString>const& GetUtilities() const { return this->Utilities; }
230 void AnalyzeLibDependencies( const cmMakefile& mf );
232 ///! Set/Get a property of this target file
233 void SetProperty(const char *prop, const char *value);
234 void AppendProperty(const char* prop, const char* value);
235 const char *GetProperty(const char *prop);
236 const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
237 bool GetPropertyAsBool(const char *prop);
238 void CheckProperty(const char* prop, cmMakefile* context);
240 bool IsImported() const {return this->IsImportedTarget;}
242 /** The link interface specifies transitive library dependencies and
243 other information needed by targets that link to this target. */
244 struct LinkInterface
246 // Languages whose runtime libraries must be linked.
247 std::vector<std::string> Languages;
249 // Libraries listed in the interface.
250 std::vector<std::string> Libraries;
252 // Shared library dependencies needed for linking on some platforms.
253 std::vector<std::string> SharedDeps;
255 // Number of repetitions of a strongly connected component of two
256 // or more static libraries.
257 int Multiplicity;
259 // Libraries listed for other configurations.
260 // Needed only for OLD behavior of CMP0003.
261 std::vector<std::string> WrongConfigLibraries;
263 LinkInterface(): Multiplicity(0) {}
266 /** Get the link interface for the given configuration. Returns 0
267 if the target cannot be linked. */
268 LinkInterface const* GetLinkInterface(const char* config);
270 /** The link implementation specifies the direct library
271 dependencies needed by the object files of the target. */
272 struct LinkImplementation
274 // Languages whose runtime libraries must be linked.
275 std::vector<std::string> Languages;
277 // Libraries linked directly in this configuration.
278 std::vector<std::string> Libraries;
280 // Libraries linked directly in other configurations.
281 // Needed only for OLD behavior of CMP0003.
282 std::vector<std::string> WrongConfigLibraries;
284 LinkImplementation const* GetLinkImplementation(const char* config);
286 /** Link information from the transitive closure of the link
287 implementation and the interfaces of its dependencies. */
288 struct LinkClosure
290 // The preferred linker language.
291 std::string LinkerLanguage;
293 // Languages whose runtime libraries must be linked.
294 std::vector<std::string> Languages;
296 LinkClosure const* GetLinkClosure(const char* config);
298 /** Strip off leading and trailing whitespace from an item named in
299 the link dependencies of this target. */
300 std::string CheckCMP0004(std::string const& item);
302 /** Get the directory in which this target will be built. If the
303 configuration name is given then the generator will add its
304 subdirectory for that configuration. Otherwise just the canonical
305 output directory is given. */
306 std::string GetDirectory(const char* config = 0, bool implib = false);
308 /** Get the location of the target in the build tree for the given
309 configuration. This location is suitable for use as the LOCATION
310 target property. */
311 const char* GetLocation(const char* config);
313 /** Get the target major and minor version numbers interpreted from
314 the VERSION property. Version 0 is returned if the property is
315 not set or cannot be parsed. */
316 void GetTargetVersion(int& major, int& minor);
318 /** Get the target major, minor, and patch version numbers
319 interpreted from the VERSION or SOVERSION property. Version 0
320 is returned if the property is not set or cannot be parsed. */
321 void GetTargetVersion(bool soversion, int& major, int& minor, int& patch);
324 * Trace through the source files in this target and add al source files
325 * that they depend on, used by all generators
327 void TraceDependencies(const char* vsProjectFile);
330 * Make sure the full path to all source files is known.
332 bool FindSourceFiles();
334 ///! Return the prefered linker language for this target
335 const char* GetLinkerLanguage(const char* config = 0);
337 ///! Return the rule variable used to create this type of target,
338 // need to add CMAKE_(LANG) for full name.
339 const char* GetCreateRuleVariable();
341 /** Get the full name of the target according to the settings in its
342 makefile. */
343 std::string GetFullName(const char* config=0, bool implib = false);
344 void GetFullNameComponents(std::string& prefix,
345 std::string& base, std::string& suffix,
346 const char* config=0, bool implib = false);
348 /** Get the name of the pdb file for the target. */
349 std::string GetPDBName(const char* config=0);
351 /** Get the soname of the target. Allowed only for a shared library. */
352 std::string GetSOName(const char* config);
354 /** Test for special case of a third-party shared library that has
355 no soname at all. */
356 bool IsImportedSharedLibWithoutSOName(const char* config);
358 /** Get the full path to the target according to the settings in its
359 makefile and the configuration type. */
360 std::string GetFullPath(const char* config=0, bool implib = false,
361 bool realname = false);
363 /** Get the names of the library needed to generate a build rule
364 that takes into account shared library version numbers. This
365 should be called only on a library target. */
366 void GetLibraryNames(std::string& name, std::string& soName,
367 std::string& realName, std::string& impName,
368 std::string& pdbName, const char* config);
370 /** Get the names of the executable needed to generate a build rule
371 that takes into account executable version numbers. This should
372 be called only on an executable target. */
373 void GetExecutableNames(std::string& name, std::string& realName,
374 std::string& impName,
375 std::string& pdbName, const char* config);
377 /** Add the target output files to the global generator manifest. */
378 void GenerateTargetManifest(const char* config);
381 * Compute whether this target must be relinked before installing.
383 bool NeedRelinkBeforeInstall(const char* config);
385 bool HaveBuildTreeRPATH();
386 bool HaveInstallTreeRPATH();
388 /** Return true if builtin chrpath will work for this target */
389 bool IsChrpathUsed(const char* config);
391 std::string GetInstallNameDirForBuildTree(const char* config,
392 bool for_xcode = false);
393 std::string GetInstallNameDirForInstallTree(const char* config,
394 bool for_xcode = false);
396 cmComputeLinkInformation* GetLinkInformation(const char* config);
398 // Get the properties
399 cmPropertyMap &GetProperties() { return this->Properties; };
401 // Define the properties
402 static void DefineProperties(cmake *cm);
404 // Compute the OBJECT_FILES property only when requested
405 void ComputeObjectFiles();
407 /** Get the macro to define when building sources in this target.
408 If no macro should be defined null is returned. */
409 const char* GetExportMacro();
411 // Compute the set of languages compiled by the target. This is
412 // computed every time it is called because the languages can change
413 // when source file properties are changed and we do not have enough
414 // information to forward these property changes to the targets
415 // until we have per-target object file properties.
416 void GetLanguages(std::set<cmStdString>& languages) const;
418 /** Return whether this target is an executable with symbol exports
419 enabled. */
420 bool IsExecutableWithExports();
422 /** Return whether this target may be used to link another target. */
423 bool IsLinkable();
425 /** Return whether or not the target is for a DLL platform. */
426 bool IsDLLPlatform() { return this->DLLPlatform; }
428 /** Return whether or not the target has a DLL import library. */
429 bool HasImportLibrary();
431 /** Return whether this target is a shared library Framework on
432 Apple. */
433 bool IsFrameworkOnApple();
435 /** Return whether this target is an executable Bundle on Apple. */
436 bool IsAppBundleOnApple();
438 /** Return the framework version string. Undefined if
439 IsFrameworkOnApple returns false. */
440 std::string GetFrameworkVersion();
442 /** Get a backtrace from the creation of the target. */
443 cmListFileBacktrace const& GetBacktrace() const;
445 /** Get a build-tree directory in which to place target support files. */
446 std::string GetSupportDirectory() const;
448 private:
450 * A list of direct dependencies. Use in conjunction with DependencyMap.
452 typedef std::vector< LibraryID > DependencyList;
455 * This map holds the dependency graph. map[x] returns a set of
456 * direct dependencies of x. Note that the direct depenencies are
457 * ordered. This is necessary to handle direct dependencies that
458 * themselves have no dependency information.
460 typedef std::map< LibraryID, DependencyList > DependencyMap;
463 * Inserts \a dep at the end of the dependency list of \a lib.
465 void InsertDependency( DependencyMap& depMap,
466 const LibraryID& lib,
467 const LibraryID& dep);
470 * Deletes \a dep from the dependency list of \a lib.
472 void DeleteDependency( DependencyMap& depMap,
473 const LibraryID& lib,
474 const LibraryID& dep);
477 * Emits the library \a lib and all its dependencies into link_line.
478 * \a emitted keeps track of the libraries that have been emitted to
479 * avoid duplicates--it is more efficient than searching
480 * link_line. \a visited is used detect cycles. Note that \a
481 * link_line is in reverse order, in that the dependencies of a
482 * library are listed before the library itself.
484 void Emit( const LibraryID lib,
485 const DependencyMap& dep_map,
486 std::set<LibraryID>& emitted,
487 std::set<LibraryID>& visited,
488 DependencyList& link_line);
491 * Finds the dependencies for \a lib and inserts them into \a
492 * dep_map.
494 void GatherDependencies( const cmMakefile& mf,
495 const LibraryID& lib,
496 DependencyMap& dep_map);
498 const char* GetSuffixVariableInternal(bool implib);
499 const char* GetPrefixVariableInternal(bool implib);
500 std::string GetFullNameInternal(const char* config, bool implib);
501 void GetFullNameInternal(const char* config, bool implib,
502 std::string& outPrefix, std::string& outBase,
503 std::string& outSuffix);
505 // Use a makefile variable to set a default for the given property.
506 // If the variable is not defined use the given default instead.
507 void SetPropertyDefault(const char* property, const char* default_value);
509 // Returns ARCHIVE, LIBRARY, or RUNTIME based on platform and type.
510 const char* GetOutputTargetType(bool implib);
512 // Get the target base name.
513 std::string GetOutputName(const char* config, bool implib);
515 const char* ImportedGetLocation(const char* config);
516 const char* NormalGetLocation(const char* config);
518 std::string GetFullNameImported(const char* config, bool implib);
520 std::string ImportedGetFullPath(const char* config, bool implib);
521 std::string NormalGetFullPath(const char* config, bool implib,
522 bool realname);
524 /** Get the real name of the target. Allowed only for non-imported
525 targets. When a library or executable file is versioned this is
526 the full versioned name. If the target is not versioned this is
527 the same as GetFullName. */
528 std::string NormalGetRealName(const char* config);
530 private:
531 std::string Name;
532 std::vector<cmCustomCommand> PreBuildCommands;
533 std::vector<cmCustomCommand> PreLinkCommands;
534 std::vector<cmCustomCommand> PostBuildCommands;
535 TargetType TargetTypeValue;
536 std::vector<cmSourceFile*> SourceFiles;
537 LinkLibraryVectorType LinkLibraries;
538 LinkLibraryVectorType PrevLinkedLibraries;
539 bool LinkLibrariesAnalyzed;
540 std::vector<std::string> Frameworks;
541 std::vector<std::string> LinkDirectories;
542 std::set<cmStdString> LinkDirectoriesEmmitted;
543 bool HaveInstallRule;
544 std::string InstallPath;
545 std::string RuntimeInstallPath;
546 std::string Location;
547 std::string ExportMacro;
548 std::set<cmStdString> Utilities;
549 bool RecordDependencies;
550 cmPropertyMap Properties;
551 LinkLibraryVectorType OriginalLinkLibraries;
552 bool DLLPlatform;
553 bool IsImportedTarget;
555 // Cache target output paths for each configuration.
556 struct OutputInfo;
557 OutputInfo const* GetOutputInfo(const char* config);
558 void ComputeOutputDir(const char* config, bool implib, std::string& out);
560 // Cache import information from properties for each configuration.
561 struct ImportInfo;
562 ImportInfo const* GetImportInfo(const char* config);
563 void ComputeImportInfo(std::string const& desired_config, ImportInfo& info);
565 cmTargetLinkInformationMap LinkInformation;
567 bool ComputeLinkInterface(const char* config, LinkInterface& iface);
569 void ComputeLinkImplementation(const char* config,
570 LinkImplementation& impl);
571 void ComputeLinkClosure(const char* config, LinkClosure& lc);
573 // The cmMakefile instance that owns this target. This should
574 // always be set.
575 cmMakefile* Makefile;
577 // Policy status recorded when target was created.
578 cmPolicies::PolicyStatus PolicyStatusCMP0003;
579 cmPolicies::PolicyStatus PolicyStatusCMP0004;
580 cmPolicies::PolicyStatus PolicyStatusCMP0008;
582 // Internal representation details.
583 friend class cmTargetInternals;
584 cmTargetInternalPointer Internal;
586 void ConstructSourceFileFlags();
589 typedef std::map<cmStdString,cmTarget> cmTargets;
591 class cmTargetSet: public std::set<cmStdString> {};
592 class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
594 #endif