1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
15 #include "cmCustomCommand.h"
16 #include "cmPropertyMap.h"
17 #include "cmPolicies.h"
18 #include "cmListFileCache.h"
20 #include <cmsys/auto_ptr.hxx>
22 #define CM_FOR_EACH_TARGET_POLICY(F) \
33 class cmGlobalGenerator;
34 class cmComputeLinkInformation;
35 class cmListFileBacktrace;
38 struct cmTargetLinkInformationMap:
39 public std::map<std::pair<cmTarget*, std::string>, cmComputeLinkInformation*>
41 typedef std::map<std::pair<cmTarget*, std::string>,
42 cmComputeLinkInformation*> derived;
43 cmTargetLinkInformationMap() {}
44 cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r);
45 ~cmTargetLinkInformationMap();
48 class cmTargetInternals;
49 class cmTargetInternalPointer
52 cmTargetInternalPointer();
53 cmTargetInternalPointer(cmTargetInternalPointer const& r);
54 ~cmTargetInternalPointer();
55 cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
56 cmTargetInternals* operator->() const { return this->Pointer; }
57 cmTargetInternals* Get() const { return this->Pointer; }
59 cmTargetInternals* Pointer;
63 * \brief Represent a library or executable target loaded from a makefile.
65 * cmTarget represents a target loaded from
72 enum TargetType { EXECUTABLE, STATIC_LIBRARY,
73 SHARED_LIBRARY, MODULE_LIBRARY,
74 OBJECT_LIBRARY, UTILITY, GLOBAL_TARGET,
76 static const char* GetTargetTypeName(TargetType targetType);
77 enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
80 * Return the type of target.
82 TargetType GetType() const
84 return this->TargetTypeValue;
90 void SetType(TargetType f, const char* name);
92 void MarkAsImported();
94 ///! Set/Get the name of the target
95 const char* GetName() const {return this->Name.c_str();}
96 const char* GetExportName();
98 ///! Set the cmMakefile that owns this target
99 void SetMakefile(cmMakefile *mf);
100 cmMakefile *GetMakefile() const { return this->Makefile;};
102 #define DECLARE_TARGET_POLICY(POLICY) \
103 cmPolicies::PolicyStatus GetPolicyStatus ## POLICY () const \
104 { return this->PolicyStatus ## POLICY; }
106 CM_FOR_EACH_TARGET_POLICY(DECLARE_TARGET_POLICY)
108 #undef DECLARE_TARGET_POLICY
111 * Get the list of the custom commands for this target
113 std::vector<cmCustomCommand> &GetPreBuildCommands()
114 {return this->PreBuildCommands;}
115 std::vector<cmCustomCommand> &GetPreLinkCommands()
116 {return this->PreLinkCommands;}
117 std::vector<cmCustomCommand> &GetPostBuildCommands()
118 {return this->PostBuildCommands;}
121 * Get the list of the source files used by this target
123 std::vector<cmSourceFile*> const& GetSourceFiles();
124 void AddSourceFile(cmSourceFile* sf);
125 std::vector<std::string> const& GetObjectLibraries() const
127 return this->ObjectLibraries;
130 /** Get sources that must be built before the given source. */
131 std::vector<cmSourceFile*> const* GetSourceDepends(cmSourceFile* sf);
134 * Flags for a given source file as used in this target. Typically assigned
135 * via SET_TARGET_PROPERTIES when the property is a list of source files.
139 SourceFileTypeNormal,
140 SourceFileTypePrivateHeader, // is in "PRIVATE_HEADER" target property
141 SourceFileTypePublicHeader, // is in "PUBLIC_HEADER" target property
142 SourceFileTypeResource, // is in "RESOURCE" target property *or*
143 // has MACOSX_PACKAGE_LOCATION=="Resources"
144 SourceFileTypeMacContent // has MACOSX_PACKAGE_LOCATION!="Resources"
146 struct SourceFileFlags
148 SourceFileFlags(): Type(SourceFileTypeNormal), MacFolder(0) {}
149 SourceFileFlags(SourceFileFlags const& r):
150 Type(r.Type), MacFolder(r.MacFolder) {}
152 const char* MacFolder; // location inside Mac content folders
156 * Get the flags for a given source file as used in this target
158 struct SourceFileFlags GetTargetSourceFileFlags(const cmSourceFile* sf);
161 * Add sources to the target.
163 void AddSources(std::vector<std::string> const& srcs);
164 cmSourceFile* AddSource(const char* src);
166 enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
168 //* how we identify a library, by name and type
169 typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
171 typedef std::vector<LibraryID > LinkLibraryVectorType;
172 const LinkLibraryVectorType &GetLinkLibraries() const {
173 return this->LinkLibraries;}
174 const LinkLibraryVectorType &GetOriginalLinkLibraries() const
175 {return this->OriginalLinkLibraries;}
176 void GetDirectLinkLibraries(const char *config,
177 std::vector<std::string> &,
180 /** Compute the link type to use for the given configuration. */
181 LinkLibraryType ComputeLinkType(const char* config);
184 * Clear the dependency information recorded for this target, if any.
186 void ClearDependencyInformation(cmMakefile& mf, const char* target);
188 // Check to see if a library is a framework and treat it different on Mac
189 bool NameResolvesToFramework(const std::string& libname);
190 void AddLinkLibrary(cmMakefile& mf,
191 const char *target, const char* lib,
192 LinkLibraryType llt);
197 bool PushTLLCommandTrace(TLLSignature signature);
198 void GetTllSignatureTraces(cmOStringStream &s, TLLSignature sig) const;
200 void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
201 const LinkLibraryVectorType& libs );
203 const std::vector<std::string>& GetLinkDirectories();
205 void AddLinkDirectory(const char* d);
208 * Set the path where this target should be installed. This is relative to
211 std::string GetInstallPath() {return this->InstallPath;}
212 void SetInstallPath(const char *name) {this->InstallPath = name;}
215 * Set the path where this target (if it has a runtime part) should be
216 * installed. This is relative to INSTALL_PREFIX
218 std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
219 void SetRuntimeInstallPath(const char *name) {
220 this->RuntimeInstallPath = name; }
223 * Get/Set whether there is an install rule for this target.
225 bool GetHaveInstallRule() { return this->HaveInstallRule; }
226 void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
228 /** Add a utility on which this project depends. A utility is an executable
229 * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
230 * commands. It is not a full path nor does it have an extension.
232 void AddUtility(const char* u) { this->Utilities.insert(u);}
233 ///! Get the utilities used by this target
234 std::set<cmStdString>const& GetUtilities() const { return this->Utilities; }
236 /** Finalize the target at the end of the Configure step. */
237 void FinishConfigure();
239 ///! Set/Get a property of this target file
240 void SetProperty(const char *prop, const char *value);
241 void AppendProperty(const char* prop, const char* value,bool asString=false);
242 const char *GetProperty(const char *prop);
243 const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
244 bool GetPropertyAsBool(const char *prop);
245 void CheckProperty(const char* prop, cmMakefile* context);
247 const char* GetFeature(const char* feature, const char* config);
249 bool IsImported() const {return this->IsImportedTarget;}
251 /** The link interface specifies transitive library dependencies and
252 other information needed by targets that link to this target. */
255 // Languages whose runtime libraries must be linked.
256 std::vector<std::string> Languages;
258 // Libraries listed in the interface.
259 std::vector<std::string> Libraries;
261 // Shared library dependencies needed for linking on some platforms.
262 std::vector<std::string> SharedDeps;
264 // Number of repetitions of a strongly connected component of two
265 // or more static libraries.
268 // Libraries listed for other configurations.
269 // Needed only for OLD behavior of CMP0003.
270 std::vector<std::string> WrongConfigLibraries;
272 bool ImplementationIsInterface;
274 LinkInterface(): Multiplicity(0), ImplementationIsInterface(false) {}
277 /** Get the link interface for the given configuration. Returns 0
278 if the target cannot be linked. */
279 LinkInterface const* GetLinkInterface(const char* config,
280 cmTarget *headTarget);
281 void GetTransitivePropertyLinkLibraries(const char* config,
282 cmTarget *headTarget,
283 std::vector<std::string> &libs);
285 /** The link implementation specifies the direct library
286 dependencies needed by the object files of the target. */
287 struct LinkImplementation
289 // Languages whose runtime libraries must be linked.
290 std::vector<std::string> Languages;
292 // Libraries linked directly in this configuration.
293 std::vector<std::string> Libraries;
295 // Libraries linked directly in other configurations.
296 // Needed only for OLD behavior of CMP0003.
297 std::vector<std::string> WrongConfigLibraries;
299 LinkImplementation const* GetLinkImplementation(const char* config,
302 /** Link information from the transitive closure of the link
303 implementation and the interfaces of its dependencies. */
306 // The preferred linker language.
307 std::string LinkerLanguage;
309 // Languages whose runtime libraries must be linked.
310 std::vector<std::string> Languages;
312 LinkClosure const* GetLinkClosure(const char* config, cmTarget *head);
314 /** Strip off leading and trailing whitespace from an item named in
315 the link dependencies of this target. */
316 std::string CheckCMP0004(std::string const& item);
318 /** Get the directory in which this target will be built. If the
319 configuration name is given then the generator will add its
320 subdirectory for that configuration. Otherwise just the canonical
321 output directory is given. */
322 std::string GetDirectory(const char* config = 0, bool implib = false);
324 /** Get the directory in which this targets .pdb files will be placed.
325 If the configuration name is given then the generator will add its
326 subdirectory for that configuration. Otherwise just the canonical
327 pdb output directory is given. */
328 std::string GetPDBDirectory(const char* config = 0);
330 /** Get the location of the target in the build tree for the given
331 configuration. This location is suitable for use as the LOCATION
333 const char* GetLocation(const char* config);
335 /** Get the target major and minor version numbers interpreted from
336 the VERSION property. Version 0 is returned if the property is
337 not set or cannot be parsed. */
338 void GetTargetVersion(int& major, int& minor);
340 /** Get the target major, minor, and patch version numbers
341 interpreted from the VERSION or SOVERSION property. Version 0
342 is returned if the property is not set or cannot be parsed. */
343 void GetTargetVersion(bool soversion, int& major, int& minor, int& patch);
346 * Trace through the source files in this target and add al source files
347 * that they depend on, used by all generators
349 void TraceDependencies();
352 * Make sure the full path to all source files is known.
354 bool FindSourceFiles();
356 ///! Return the preferred linker language for this target
357 const char* GetLinkerLanguage(const char* config = 0, cmTarget *head = 0);
359 /** Get the full name of the target according to the settings in its
361 std::string GetFullName(const char* config=0, bool implib = false);
362 void GetFullNameComponents(std::string& prefix,
363 std::string& base, std::string& suffix,
364 const char* config=0, bool implib = false);
366 /** Get the name of the pdb file for the target. */
367 std::string GetPDBName(const char* config=0);
369 /** Whether this library has soname enabled and platform supports it. */
370 bool HasSOName(const char* config);
372 /** Get the soname of the target. Allowed only for a shared library. */
373 std::string GetSOName(const char* config);
375 /** Whether this library has @rpath and platform supports it. */
376 bool HasMacOSXRpath(const char* config);
378 /** Test for special case of a third-party shared library that has
380 bool IsImportedSharedLibWithoutSOName(const char* config);
382 /** Get the full path to the target according to the settings in its
383 makefile and the configuration type. */
384 std::string GetFullPath(const char* config=0, bool implib = false,
385 bool realname = false);
387 /** Get the names of the library needed to generate a build rule
388 that takes into account shared library version numbers. This
389 should be called only on a library target. */
390 void GetLibraryNames(std::string& name, std::string& soName,
391 std::string& realName, std::string& impName,
392 std::string& pdbName, const char* config);
394 /** Get the names of the executable needed to generate a build rule
395 that takes into account executable version numbers. This should
396 be called only on an executable target. */
397 void GetExecutableNames(std::string& name, std::string& realName,
398 std::string& impName,
399 std::string& pdbName, const char* config);
401 /** Does this target have a GNU implib to convert to MS format? */
402 bool HasImplibGNUtoMS();
404 /** Convert the given GNU import library name (.dll.a) to a name with a new
405 extension (.lib or ${CMAKE_IMPORT_LIBRARY_SUFFIX}). */
406 bool GetImplibGNUtoMS(std::string const& gnuName, std::string& out,
407 const char* newExt = 0);
409 /** Add the target output files to the global generator manifest. */
410 void GenerateTargetManifest(const char* config);
413 * Compute whether this target must be relinked before installing.
415 bool NeedRelinkBeforeInstall(const char* config);
417 bool HaveBuildTreeRPATH(const char *config);
418 bool HaveInstallTreeRPATH();
420 /** Return true if builtin chrpath will work for this target */
421 bool IsChrpathUsed(const char* config);
423 /** Return the install name directory for the target in the
424 * build tree. For example: "@rpath/", "@loader_path/",
425 * or "/full/path/to/library". */
426 std::string GetInstallNameDirForBuildTree(const char* config);
428 /** Return the install name directory for the target in the
429 * install tree. For example: "@rpath/" or "@loader_path/". */
430 std::string GetInstallNameDirForInstallTree();
432 cmComputeLinkInformation* GetLinkInformation(const char* config,
435 // Get the properties
436 cmPropertyMap &GetProperties() { return this->Properties; };
438 bool GetMappedConfig(std::string const& desired_config,
441 std::string& suffix);
443 // Define the properties
444 static void DefineProperties(cmake *cm);
446 /** Get the macro to define when building sources in this target.
447 If no macro should be defined null is returned. */
448 const char* GetExportMacro();
450 void GetCompileDefinitions(std::vector<std::string> &result,
453 // Compute the set of languages compiled by the target. This is
454 // computed every time it is called because the languages can change
455 // when source file properties are changed and we do not have enough
456 // information to forward these property changes to the targets
457 // until we have per-target object file properties.
458 void GetLanguages(std::set<cmStdString>& languages) const;
460 /** Return whether this target is an executable with symbol exports
462 bool IsExecutableWithExports();
464 /** Return whether this target may be used to link another target. */
467 /** Return whether or not the target is for a DLL platform. */
468 bool IsDLLPlatform() { return this->DLLPlatform; }
470 /** Return whether or not the target has a DLL import library. */
471 bool HasImportLibrary();
473 /** Return whether this target is a shared library Framework on
475 bool IsFrameworkOnApple();
477 /** Return whether this target is a CFBundle (plugin) on Apple. */
478 bool IsCFBundleOnApple();
480 /** Return whether this target is an executable Bundle on Apple. */
481 bool IsAppBundleOnApple();
483 /** Return whether this target is an executable Bundle, a framework
484 or CFBundle on Apple. */
485 bool IsBundleOnApple();
487 /** Return the framework version string. Undefined if
488 IsFrameworkOnApple returns false. */
489 std::string GetFrameworkVersion();
491 /** Get a backtrace from the creation of the target. */
492 cmListFileBacktrace const& GetBacktrace() const;
494 /** Get a build-tree directory in which to place target support files. */
495 std::string GetSupportDirectory() const;
497 /** Return whether this target uses the default value for its output
499 bool UsesDefaultOutputDir(const char* config, bool implib);
501 /** @return the mac content directory for this target. */
502 std::string GetMacContentDirectory(const char* config,
505 /** @return whether this target have a well defined output file name. */
506 bool HaveWellDefinedOutputFiles();
508 /** @return the Mac framework directory without the base. */
509 std::string GetFrameworkDirectory(const char* config, bool rootDir);
511 /** @return the Mac CFBundle directory without the base */
512 std::string GetCFBundleDirectory(const char* config, bool contentOnly);
514 /** @return the Mac App directory without the base */
515 std::string GetAppBundleDirectory(const char* config, bool contentOnly);
517 std::vector<std::string> GetIncludeDirectories(const char *config);
518 void InsertInclude(const cmValueWithOrigin &entry,
519 bool before = false);
520 void InsertCompileOption(const cmValueWithOrigin &entry,
521 bool before = false);
522 void InsertCompileDefinition(const cmValueWithOrigin &entry,
523 bool before = false);
525 void AppendBuildInterfaceIncludes();
527 void GetCompileOptions(std::vector<std::string> &result,
530 bool IsNullImpliedByLinkLibraries(const std::string &p);
531 bool IsLinkInterfaceDependentBoolProperty(const std::string &p,
533 bool IsLinkInterfaceDependentStringProperty(const std::string &p,
536 bool GetLinkInterfaceDependentBoolProperty(const std::string &p,
539 const char *GetLinkInterfaceDependentStringProperty(const std::string &p,
542 std::string GetDebugGeneratorExpressions(const std::string &value,
543 cmTarget::LinkLibraryType llt);
545 void AddSystemIncludeDirectories(const std::set<cmStdString> &incs);
546 void AddSystemIncludeDirectories(const std::vector<std::string> &incs);
547 std::set<cmStdString> const & GetSystemIncludeDirectories() const
548 { return this->SystemIncludeDirectories; }
550 void FinalizeSystemIncludeDirectories();
552 bool LinkLanguagePropagatesToDependents() const
553 { return this->TargetTypeValue == STATIC_LIBRARY; }
556 // The set of include directories that are marked as system include
558 std::set<cmStdString> SystemIncludeDirectories;
560 std::vector<std::pair<TLLSignature, cmListFileBacktrace> > TLLCommands;
563 * A list of direct dependencies. Use in conjunction with DependencyMap.
565 typedef std::vector< LibraryID > DependencyList;
568 * This map holds the dependency graph. map[x] returns a set of
569 * direct dependencies of x. Note that the direct depenencies are
570 * ordered. This is necessary to handle direct dependencies that
571 * themselves have no dependency information.
573 typedef std::map< LibraryID, DependencyList > DependencyMap;
576 * Inserts \a dep at the end of the dependency list of \a lib.
578 void InsertDependency( DependencyMap& depMap,
579 const LibraryID& lib,
580 const LibraryID& dep);
583 * Deletes \a dep from the dependency list of \a lib.
585 void DeleteDependency( DependencyMap& depMap,
586 const LibraryID& lib,
587 const LibraryID& dep);
590 * Emits the library \a lib and all its dependencies into link_line.
591 * \a emitted keeps track of the libraries that have been emitted to
592 * avoid duplicates--it is more efficient than searching
593 * link_line. \a visited is used detect cycles. Note that \a
594 * link_line is in reverse order, in that the dependencies of a
595 * library are listed before the library itself.
597 void Emit( const LibraryID lib,
598 const DependencyMap& dep_map,
599 std::set<LibraryID>& emitted,
600 std::set<LibraryID>& visited,
601 DependencyList& link_line);
604 * Finds the dependencies for \a lib and inserts them into \a
607 void GatherDependencies( const cmMakefile& mf,
608 const LibraryID& lib,
609 DependencyMap& dep_map);
611 void AnalyzeLibDependencies( const cmMakefile& mf );
613 const char* GetSuffixVariableInternal(bool implib);
614 const char* GetPrefixVariableInternal(bool implib);
615 std::string GetFullNameInternal(const char* config, bool implib);
616 void GetFullNameInternal(const char* config, bool implib,
617 std::string& outPrefix, std::string& outBase,
618 std::string& outSuffix);
620 // Use a makefile variable to set a default for the given property.
621 // If the variable is not defined use the given default instead.
622 void SetPropertyDefault(const char* property, const char* default_value);
624 // Returns ARCHIVE, LIBRARY, or RUNTIME based on platform and type.
625 const char* GetOutputTargetType(bool implib);
627 // Get the target base name.
628 std::string GetOutputName(const char* config, bool implib);
630 const char* ImportedGetLocation(const char* config);
631 const char* NormalGetLocation(const char* config);
633 std::string GetFullNameImported(const char* config, bool implib);
635 std::string ImportedGetFullPath(const char* config, bool implib);
636 std::string NormalGetFullPath(const char* config, bool implib,
639 /** Get the real name of the target. Allowed only for non-imported
640 targets. When a library or executable file is versioned this is
641 the full versioned name. If the target is not versioned this is
642 the same as GetFullName. */
643 std::string NormalGetRealName(const char* config);
645 /** Append to @a base the mac content directory and return it. */
646 std::string BuildMacContentDirectory(const std::string& base,
652 std::vector<cmCustomCommand> PreBuildCommands;
653 std::vector<cmCustomCommand> PreLinkCommands;
654 std::vector<cmCustomCommand> PostBuildCommands;
655 TargetType TargetTypeValue;
656 std::vector<cmSourceFile*> SourceFiles;
657 std::vector<std::string> ObjectLibraries;
658 LinkLibraryVectorType LinkLibraries;
659 LinkLibraryVectorType PrevLinkedLibraries;
660 bool LinkLibrariesAnalyzed;
661 std::vector<std::string> LinkDirectories;
662 std::set<cmStdString> LinkDirectoriesEmmitted;
663 bool HaveInstallRule;
664 std::string InstallPath;
665 std::string RuntimeInstallPath;
666 std::string Location;
667 std::string ExportMacro;
668 std::set<cmStdString> Utilities;
669 bool RecordDependencies;
670 cmPropertyMap Properties;
671 LinkLibraryVectorType OriginalLinkLibraries;
674 bool IsImportedTarget;
675 bool DebugIncludesDone;
676 bool DebugCompileOptionsDone;
677 bool DebugCompileDefinitionsDone;
678 mutable std::set<std::string> LinkImplicitNullProperties;
679 bool BuildInterfaceIncludesAppended;
681 // Cache target output paths for each configuration.
683 OutputInfo const* GetOutputInfo(const char* config);
684 bool ComputeOutputDir(const char* config, bool implib, std::string& out);
685 bool ComputePDBOutputDir(const char* config, std::string& out);
687 // Cache import information from properties for each configuration.
689 ImportInfo const* GetImportInfo(const char* config,
690 cmTarget *workingTarget);
691 void ComputeImportInfo(std::string const& desired_config, ImportInfo& info,
694 cmTargetLinkInformationMap LinkInformation;
695 void CheckPropertyCompatibility(cmComputeLinkInformation *info,
698 bool ComputeLinkInterface(const char* config, LinkInterface& iface,
701 void ComputeLinkImplementation(const char* config,
702 LinkImplementation& impl, cmTarget *head);
703 void ComputeLinkClosure(const char* config, LinkClosure& lc, cmTarget *head);
705 void ClearLinkMaps();
707 void MaybeInvalidatePropertyCache(const char* prop);
709 void ProcessSourceExpression(std::string const& expr);
711 // The cmMakefile instance that owns this target. This should
713 cmMakefile* Makefile;
715 // Policy status recorded when target was created.
716 #define TARGET_POLICY_MEMBER(POLICY) \
717 cmPolicies::PolicyStatus PolicyStatus ## POLICY;
719 CM_FOR_EACH_TARGET_POLICY(TARGET_POLICY_MEMBER)
721 #undef TARGET_POLICY_MEMBER
723 // Internal representation details.
724 friend class cmTargetInternals;
725 cmTargetInternalPointer Internal;
727 void ConstructSourceFileFlags();
728 void ComputeVersionedName(std::string& vName,
729 std::string const& prefix,
730 std::string const& base,
731 std::string const& suffix,
732 std::string const& name,
733 const char* version);
736 typedef std::map<cmStdString,cmTarget> cmTargets;
738 class cmTargetSet: public std::set<cmStdString> {};
739 class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};