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"
19 #include <cmsys/auto_ptr.hxx>
24 class cmGlobalGenerator;
25 class cmComputeLinkInformation;
26 class cmListFileBacktrace;
28 struct cmTargetLinkInformationMap:
29 public std::map<cmStdString, cmComputeLinkInformation*>
31 typedef std::map<cmStdString, cmComputeLinkInformation*> derived;
32 cmTargetLinkInformationMap() {}
33 cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r);
34 ~cmTargetLinkInformationMap();
37 class cmTargetInternals;
38 class cmTargetInternalPointer
41 cmTargetInternalPointer();
42 cmTargetInternalPointer(cmTargetInternalPointer const& r);
43 ~cmTargetInternalPointer();
44 cmTargetInternalPointer& operator=(cmTargetInternalPointer const& r);
45 cmTargetInternals* operator->() const { return this->Pointer; }
46 cmTargetInternals* Get() const { return this->Pointer; }
48 cmTargetInternals* Pointer;
52 * \brief Represent a library or executable target loaded from a makefile.
54 * cmTarget represents a target loaded from
61 enum TargetType { EXECUTABLE, STATIC_LIBRARY,
62 SHARED_LIBRARY, MODULE_LIBRARY,
63 OBJECT_LIBRARY, UTILITY, GLOBAL_TARGET,
65 static const char* GetTargetTypeName(TargetType targetType);
66 enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
69 * Return the type of target.
71 TargetType GetType() const
73 return this->TargetTypeValue;
79 void SetType(TargetType f, const char* name);
81 void MarkAsImported();
83 ///! Set/Get the name of the target
84 const char* GetName() const {return this->Name.c_str();}
86 ///! Set the cmMakefile that owns this target
87 void SetMakefile(cmMakefile *mf);
88 cmMakefile *GetMakefile() const { return this->Makefile;};
90 /** Get the status of policy CMP0003 when the target was created. */
91 cmPolicies::PolicyStatus GetPolicyStatusCMP0003() const
92 { return this->PolicyStatusCMP0003; }
94 /** Get the status of policy CMP0004 when the target was created. */
95 cmPolicies::PolicyStatus GetPolicyStatusCMP0004() const
96 { return this->PolicyStatusCMP0004; }
98 /** Get the status of policy CMP0008 when the target was created. */
99 cmPolicies::PolicyStatus GetPolicyStatusCMP0008() const
100 { return this->PolicyStatusCMP0008; }
103 * Get the list of the custom commands for this target
105 std::vector<cmCustomCommand> &GetPreBuildCommands()
106 {return this->PreBuildCommands;}
107 std::vector<cmCustomCommand> &GetPreLinkCommands()
108 {return this->PreLinkCommands;}
109 std::vector<cmCustomCommand> &GetPostBuildCommands()
110 {return this->PostBuildCommands;}
112 ///! Return the list of frameworks being linked to this target
113 std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
116 * Get the list of the source files used by this target
118 std::vector<cmSourceFile*> const& GetSourceFiles();
119 void AddSourceFile(cmSourceFile* sf);
120 std::vector<std::string> const& GetObjectLibraries() const
122 return this->ObjectLibraries;
125 /** Get sources that must be built before the given source. */
126 std::vector<cmSourceFile*> const* GetSourceDepends(cmSourceFile* sf);
129 * Flags for a given source file as used in this target. Typically assigned
130 * via SET_TARGET_PROPERTIES when the property is a list of source files.
134 SourceFileTypeNormal,
135 SourceFileTypePrivateHeader, // is in "PRIVATE_HEADER" target property
136 SourceFileTypePublicHeader, // is in "PUBLIC_HEADER" target property
137 SourceFileTypeResource, // is in "RESOURCE" target property *or*
138 // has MACOSX_PACKAGE_LOCATION=="Resources"
139 SourceFileTypeMacContent // has MACOSX_PACKAGE_LOCATION!="Resources"
141 struct SourceFileFlags
143 SourceFileFlags(): Type(SourceFileTypeNormal), MacFolder(0) {}
144 SourceFileFlags(SourceFileFlags const& r):
145 Type(r.Type), MacFolder(r.MacFolder) {}
147 const char* MacFolder; // location inside Mac content folders
151 * Get the flags for a given source file as used in this target
153 struct SourceFileFlags GetTargetSourceFileFlags(const cmSourceFile* sf);
156 * Add sources to the target.
158 void AddSources(std::vector<std::string> const& srcs);
159 cmSourceFile* AddSource(const char* src);
162 * Get the list of the source files used by this target
164 enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
166 //* how we identify a library, by name and type
167 typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
169 typedef std::vector<LibraryID > LinkLibraryVectorType;
170 const LinkLibraryVectorType &GetLinkLibraries() const {
171 return this->LinkLibraries;}
172 const LinkLibraryVectorType &GetOriginalLinkLibraries() const
173 {return this->OriginalLinkLibraries;}
175 /** Compute the link type to use for the given configuration. */
176 LinkLibraryType ComputeLinkType(const char* config);
179 * Clear the dependency information recorded for this target, if any.
181 void ClearDependencyInformation(cmMakefile& mf, const char* target);
183 // Check to see if a library is a framework and treat it different on Mac
184 bool NameResolvesToFramework(const std::string& libname);
185 bool AddFramework(const std::string& lib, LinkLibraryType llt);
186 void AddLinkLibrary(cmMakefile& mf,
187 const char *target, const char* lib,
188 LinkLibraryType llt);
190 void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
191 const LinkLibraryVectorType& libs );
193 const std::vector<std::string>& GetLinkDirectories();
195 void AddLinkDirectory(const char* d);
198 * Set the path where this target should be installed. This is relative to
201 std::string GetInstallPath() {return this->InstallPath;}
202 void SetInstallPath(const char *name) {this->InstallPath = name;}
205 * Set the path where this target (if it has a runtime part) should be
206 * installed. This is relative to INSTALL_PREFIX
208 std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
209 void SetRuntimeInstallPath(const char *name) {
210 this->RuntimeInstallPath = name; }
213 * Get/Set whether there is an install rule for this target.
215 bool GetHaveInstallRule() { return this->HaveInstallRule; }
216 void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
218 /** Add a utility on which this project depends. A utility is an executable
219 * name as would be specified to the ADD_EXECUTABLE or UTILITY_SOURCE
220 * commands. It is not a full path nor does it have an extension.
222 void AddUtility(const char* u) { this->Utilities.insert(u);}
223 ///! Get the utilities used by this target
224 std::set<cmStdString>const& GetUtilities() const { return this->Utilities; }
226 /** Finalize the target at the end of the Configure step. */
227 void FinishConfigure();
229 ///! Set/Get a property of this target file
230 void SetProperty(const char *prop, const char *value);
231 void AppendProperty(const char* prop, const char* value,bool asString=false);
232 const char *GetProperty(const char *prop);
233 const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
234 bool GetPropertyAsBool(const char *prop);
235 void CheckProperty(const char* prop, cmMakefile* context);
237 const char* GetFeature(const char* feature, const char* config);
239 bool IsImported() const {return this->IsImportedTarget;}
241 /** The link interface specifies transitive library dependencies and
242 other information needed by targets that link to this target. */
245 // Languages whose runtime libraries must be linked.
246 std::vector<std::string> Languages;
248 // Libraries listed in the interface.
249 std::vector<std::string> Libraries;
251 // Shared library dependencies needed for linking on some platforms.
252 std::vector<std::string> SharedDeps;
254 // Number of repetitions of a strongly connected component of two
255 // or more static libraries.
258 // Libraries listed for other configurations.
259 // Needed only for OLD behavior of CMP0003.
260 std::vector<std::string> WrongConfigLibraries;
262 LinkInterface(): Multiplicity(0) {}
265 /** Get the link interface for the given configuration. Returns 0
266 if the target cannot be linked. */
267 LinkInterface const* GetLinkInterface(const char* config);
269 /** The link implementation specifies the direct library
270 dependencies needed by the object files of the target. */
271 struct LinkImplementation
273 // Languages whose runtime libraries must be linked.
274 std::vector<std::string> Languages;
276 // Libraries linked directly in this configuration.
277 std::vector<std::string> Libraries;
279 // Libraries linked directly in other configurations.
280 // Needed only for OLD behavior of CMP0003.
281 std::vector<std::string> WrongConfigLibraries;
283 LinkImplementation const* GetLinkImplementation(const char* config);
285 /** Link information from the transitive closure of the link
286 implementation and the interfaces of its dependencies. */
289 // The preferred linker language.
290 std::string LinkerLanguage;
292 // Languages whose runtime libraries must be linked.
293 std::vector<std::string> Languages;
295 LinkClosure const* GetLinkClosure(const char* config);
297 /** Strip off leading and trailing whitespace from an item named in
298 the link dependencies of this target. */
299 std::string CheckCMP0004(std::string const& item);
301 /** Get the directory in which this target will be built. If the
302 configuration name is given then the generator will add its
303 subdirectory for that configuration. Otherwise just the canonical
304 output directory is given. */
305 std::string GetDirectory(const char* config = 0, bool implib = false);
307 /** Get the location of the target in the build tree for the given
308 configuration. This location is suitable for use as the LOCATION
310 const char* GetLocation(const char* config);
312 /** Get the target major and minor version numbers interpreted from
313 the VERSION property. Version 0 is returned if the property is
314 not set or cannot be parsed. */
315 void GetTargetVersion(int& major, int& minor);
317 /** Get the target major, minor, and patch version numbers
318 interpreted from the VERSION or SOVERSION property. Version 0
319 is returned if the property is not set or cannot be parsed. */
320 void GetTargetVersion(bool soversion, int& major, int& minor, int& patch);
323 * Trace through the source files in this target and add al source files
324 * that they depend on, used by all generators
326 void TraceDependencies(const char* vsProjectFile);
329 * Make sure the full path to all source files is known.
331 bool FindSourceFiles();
333 ///! Return the preferred linker language for this target
334 const char* GetLinkerLanguage(const char* config = 0);
336 ///! Return the rule variable used to create this type of target,
337 // need to add CMAKE_(LANG) for full name.
338 const char* GetCreateRuleVariable();
340 /** Get the full name of the target according to the settings in its
342 std::string GetFullName(const char* config=0, bool implib = false);
343 void GetFullNameComponents(std::string& prefix,
344 std::string& base, std::string& suffix,
345 const char* config=0, bool implib = false);
347 /** Get the name of the pdb file for the target. */
348 std::string GetPDBName(const char* config=0);
350 /** Whether this library has soname enabled and platform supports it. */
351 bool HasSOName(const char* config);
353 /** Get the soname of the target. Allowed only for a shared library. */
354 std::string GetSOName(const char* config);
356 /** Test for special case of a third-party shared library that has
358 bool IsImportedSharedLibWithoutSOName(const char* config);
360 /** Get the full path to the target according to the settings in its
361 makefile and the configuration type. */
362 std::string GetFullPath(const char* config=0, bool implib = false,
363 bool realname = false);
365 /** Get the names of the library needed to generate a build rule
366 that takes into account shared library version numbers. This
367 should be called only on a library target. */
368 void GetLibraryNames(std::string& name, std::string& soName,
369 std::string& realName, std::string& impName,
370 std::string& pdbName, const char* config);
372 /** Get the names of the executable needed to generate a build rule
373 that takes into account executable version numbers. This should
374 be called only on an executable target. */
375 void GetExecutableNames(std::string& name, std::string& realName,
376 std::string& impName,
377 std::string& pdbName, const char* config);
379 /** Does this target have a GNU implib to convert to MS format? */
380 bool HasImplibGNUtoMS();
382 /** Convert the given GNU import library name (.dll.a) to a name with a new
383 extension (.lib or ${CMAKE_IMPORT_LIBRARY_SUFFIX}). */
384 bool GetImplibGNUtoMS(std::string const& gnuName, std::string& out,
385 const char* newExt = 0);
387 /** Add the target output files to the global generator manifest. */
388 void GenerateTargetManifest(const char* config);
391 * Compute whether this target must be relinked before installing.
393 bool NeedRelinkBeforeInstall(const char* config);
395 bool HaveBuildTreeRPATH();
396 bool HaveInstallTreeRPATH();
398 /** Return true if builtin chrpath will work for this target */
399 bool IsChrpathUsed(const char* config);
401 std::string GetInstallNameDirForBuildTree(const char* config,
402 bool for_xcode = false);
403 std::string GetInstallNameDirForInstallTree(const char* config,
404 bool for_xcode = false);
406 cmComputeLinkInformation* GetLinkInformation(const char* config);
408 // Get the properties
409 cmPropertyMap &GetProperties() { return this->Properties; };
411 // Define the properties
412 static void DefineProperties(cmake *cm);
414 /** Get the macro to define when building sources in this target.
415 If no macro should be defined null is returned. */
416 const char* GetExportMacro();
418 // Compute the set of languages compiled by the target. This is
419 // computed every time it is called because the languages can change
420 // when source file properties are changed and we do not have enough
421 // information to forward these property changes to the targets
422 // until we have per-target object file properties.
423 void GetLanguages(std::set<cmStdString>& languages) const;
425 /** Get the list of OS X target architectures to be built. */
426 void GetAppleArchs(const char* config, std::vector<std::string>& archVec);
428 /** Return whether this target is an executable with symbol exports
430 bool IsExecutableWithExports();
432 /** Return whether this target may be used to link another target. */
435 /** Return whether or not the target is for a DLL platform. */
436 bool IsDLLPlatform() { return this->DLLPlatform; }
438 /** Return whether or not the target has a DLL import library. */
439 bool HasImportLibrary();
441 /** Return whether this target is a shared library Framework on
443 bool IsFrameworkOnApple();
445 /** Return whether this target is a CFBundle (plugin) on Apple. */
446 bool IsCFBundleOnApple();
448 /** Return whether this target is an executable Bundle on Apple. */
449 bool IsAppBundleOnApple();
451 /** Return the framework version string. Undefined if
452 IsFrameworkOnApple returns false. */
453 std::string GetFrameworkVersion();
455 /** Get a backtrace from the creation of the target. */
456 cmListFileBacktrace const& GetBacktrace() const;
458 /** Get a build-tree directory in which to place target support files. */
459 std::string GetSupportDirectory() const;
461 /** Return whether this target uses the default value for its output
463 bool UsesDefaultOutputDir(const char* config, bool implib);
465 /** Get the include directories for this target. */
466 std::vector<std::string> GetIncludeDirectories();
468 /** Append to @a base the mac content directory and return it. */
469 std::string BuildMacContentDirectory(const std::string& base,
470 const char* config = 0,
471 bool includeMacOS = true);
473 /** @return the mac content directory for this target. */
474 std::string GetMacContentDirectory(const char* config = 0,
476 bool includeMacOS = true);
478 /** @return whether this target have a well defined output file name. */
479 bool HaveWellDefinedOutputFiles();
481 /** @return the Mac framework directory without the base. */
482 std::string GetFrameworkDirectory(const char* config = 0);
486 * A list of direct dependencies. Use in conjunction with DependencyMap.
488 typedef std::vector< LibraryID > DependencyList;
491 * This map holds the dependency graph. map[x] returns a set of
492 * direct dependencies of x. Note that the direct depenencies are
493 * ordered. This is necessary to handle direct dependencies that
494 * themselves have no dependency information.
496 typedef std::map< LibraryID, DependencyList > DependencyMap;
499 * Inserts \a dep at the end of the dependency list of \a lib.
501 void InsertDependency( DependencyMap& depMap,
502 const LibraryID& lib,
503 const LibraryID& dep);
506 * Deletes \a dep from the dependency list of \a lib.
508 void DeleteDependency( DependencyMap& depMap,
509 const LibraryID& lib,
510 const LibraryID& dep);
513 * Emits the library \a lib and all its dependencies into link_line.
514 * \a emitted keeps track of the libraries that have been emitted to
515 * avoid duplicates--it is more efficient than searching
516 * link_line. \a visited is used detect cycles. Note that \a
517 * link_line is in reverse order, in that the dependencies of a
518 * library are listed before the library itself.
520 void Emit( const LibraryID lib,
521 const DependencyMap& dep_map,
522 std::set<LibraryID>& emitted,
523 std::set<LibraryID>& visited,
524 DependencyList& link_line);
527 * Finds the dependencies for \a lib and inserts them into \a
530 void GatherDependencies( const cmMakefile& mf,
531 const LibraryID& lib,
532 DependencyMap& dep_map);
534 void AnalyzeLibDependencies( const cmMakefile& mf );
536 const char* GetSuffixVariableInternal(bool implib);
537 const char* GetPrefixVariableInternal(bool implib);
538 std::string GetFullNameInternal(const char* config, bool implib);
539 void GetFullNameInternal(const char* config, bool implib,
540 std::string& outPrefix, std::string& outBase,
541 std::string& outSuffix);
543 // Use a makefile variable to set a default for the given property.
544 // If the variable is not defined use the given default instead.
545 void SetPropertyDefault(const char* property, const char* default_value);
547 // Returns ARCHIVE, LIBRARY, or RUNTIME based on platform and type.
548 const char* GetOutputTargetType(bool implib);
550 // Get the target base name.
551 std::string GetOutputName(const char* config, bool implib);
553 const char* ImportedGetLocation(const char* config);
554 const char* NormalGetLocation(const char* config);
556 std::string GetFullNameImported(const char* config, bool implib);
558 std::string ImportedGetFullPath(const char* config, bool implib);
559 std::string NormalGetFullPath(const char* config, bool implib,
562 /** Get the real name of the target. Allowed only for non-imported
563 targets. When a library or executable file is versioned this is
564 the full versioned name. If the target is not versioned this is
565 the same as GetFullName. */
566 std::string NormalGetRealName(const char* config);
570 std::vector<cmCustomCommand> PreBuildCommands;
571 std::vector<cmCustomCommand> PreLinkCommands;
572 std::vector<cmCustomCommand> PostBuildCommands;
573 TargetType TargetTypeValue;
574 std::vector<cmSourceFile*> SourceFiles;
575 std::vector<std::string> ObjectLibraries;
576 LinkLibraryVectorType LinkLibraries;
577 LinkLibraryVectorType PrevLinkedLibraries;
578 bool LinkLibrariesAnalyzed;
579 std::vector<std::string> Frameworks;
580 std::vector<std::string> LinkDirectories;
581 std::set<cmStdString> LinkDirectoriesEmmitted;
582 bool HaveInstallRule;
583 std::string InstallPath;
584 std::string RuntimeInstallPath;
585 std::string Location;
586 std::string ExportMacro;
587 std::set<cmStdString> Utilities;
588 bool RecordDependencies;
589 cmPropertyMap Properties;
590 LinkLibraryVectorType OriginalLinkLibraries;
593 bool IsImportedTarget;
595 // Cache target output paths for each configuration.
597 OutputInfo const* GetOutputInfo(const char* config);
598 bool ComputeOutputDir(const char* config, bool implib, std::string& out);
600 // Cache import information from properties for each configuration.
602 ImportInfo const* GetImportInfo(const char* config);
603 void ComputeImportInfo(std::string const& desired_config, ImportInfo& info);
605 cmTargetLinkInformationMap LinkInformation;
607 bool ComputeLinkInterface(const char* config, LinkInterface& iface);
609 void ComputeLinkImplementation(const char* config,
610 LinkImplementation& impl);
611 void ComputeLinkClosure(const char* config, LinkClosure& lc);
613 void ClearLinkMaps();
615 void MaybeInvalidatePropertyCache(const char* prop);
617 void ProcessSourceExpression(std::string const& expr);
619 // The cmMakefile instance that owns this target. This should
621 cmMakefile* Makefile;
623 // Policy status recorded when target was created.
624 cmPolicies::PolicyStatus PolicyStatusCMP0003;
625 cmPolicies::PolicyStatus PolicyStatusCMP0004;
626 cmPolicies::PolicyStatus PolicyStatusCMP0008;
628 // Internal representation details.
629 friend class cmTargetInternals;
630 cmTargetInternalPointer Internal;
632 void ConstructSourceFileFlags();
633 void ComputeVersionedName(std::string& vName,
634 std::string const& prefix,
635 std::string const& base,
636 std::string const& suffix,
637 std::string const& name,
638 const char* version);
641 typedef std::map<cmStdString,cmTarget> cmTargets;
643 class cmTargetSet: public std::set<cmStdString> {};
644 class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};