Fix memory and process leak in ctest_run_script.
[cmake.git] / Source / cmTarget.h
blobcf99404217ef28545e4311e5efb3d92dcde562d4
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmTarget.h,v $
5 Language: C++
6 Date: $Date: 2009-09-04 16:39:05 $
7 Version: $Revision: 1.141 $
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 private:
52 cmTargetInternals* Pointer;
55 /** \class cmTarget
56 * \brief Represent a library or executable target loaded from a makefile.
58 * cmTarget represents a target loaded from
59 * a makefile.
61 class cmTarget
63 public:
64 cmTarget();
65 enum TargetType { EXECUTABLE, STATIC_LIBRARY,
66 SHARED_LIBRARY, MODULE_LIBRARY, UTILITY, GLOBAL_TARGET,
67 INSTALL_FILES, INSTALL_PROGRAMS, INSTALL_DIRECTORY,
68 UNKNOWN_LIBRARY};
69 static const char* TargetTypeNames[];
70 enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
72 /**
73 * Return the type of target.
75 TargetType GetType() const
77 return this->TargetTypeValue;
80 /**
81 * Set the target type
83 void SetType(TargetType f, const char* name);
85 void MarkAsImported();
87 ///! Set/Get the name of the target
88 const char* GetName() const {return this->Name.c_str();}
90 ///! Set the cmMakefile that owns this target
91 void SetMakefile(cmMakefile *mf);
92 cmMakefile *GetMakefile() const { return this->Makefile;};
94 /** Get the status of policy CMP0003 when the target was created. */
95 cmPolicies::PolicyStatus GetPolicyStatusCMP0003() const
96 { return this->PolicyStatusCMP0003; }
98 /** Get the status of policy CMP0004 when the target was created. */
99 cmPolicies::PolicyStatus GetPolicyStatusCMP0004() const
100 { return this->PolicyStatusCMP0004; }
102 /** Get the status of policy CMP0008 when the target was created. */
103 cmPolicies::PolicyStatus GetPolicyStatusCMP0008() const
104 { return this->PolicyStatusCMP0008; }
107 * Get the list of the custom commands for this target
109 std::vector<cmCustomCommand> &GetPreBuildCommands()
110 {return this->PreBuildCommands;}
111 std::vector<cmCustomCommand> &GetPreLinkCommands()
112 {return this->PreLinkCommands;}
113 std::vector<cmCustomCommand> &GetPostBuildCommands()
114 {return this->PostBuildCommands;}
116 ///! Return the list of frameworks being linked to this target
117 std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
120 * Get the list of the source files used by this target
122 std::vector<cmSourceFile*> const& GetSourceFiles();
123 void AddSourceFile(cmSourceFile* sf);
126 * Flags for a given source file as used in this target. Typically assigned
127 * via SET_TARGET_PROPERTIES when the property is a list of source files.
129 enum SourceFileType
131 SourceFileTypeNormal,
132 SourceFileTypePrivateHeader, // is in "PRIVATE_HEADER" target property
133 SourceFileTypePublicHeader, // is in "PUBLIC_HEADER" target property
134 SourceFileTypeResource, // is in "RESOURCE" target property *or*
135 // has MACOSX_PACKAGE_LOCATION=="Resources"
136 SourceFileTypeMacContent // has MACOSX_PACKAGE_LOCATION!="Resources"
138 struct SourceFileFlags
140 SourceFileFlags(): Type(SourceFileTypeNormal), MacFolder(0) {}
141 SourceFileFlags(SourceFileFlags const& r):
142 Type(r.Type), MacFolder(r.MacFolder) {}
143 SourceFileType Type;
144 const char* MacFolder; // location inside Mac content folders
148 * Get the flags for a given source file as used in this target
150 struct SourceFileFlags GetTargetSourceFileFlags(const cmSourceFile* sf);
153 * Add sources to the target.
155 void AddSources(std::vector<std::string> const& srcs);
156 cmSourceFile* AddSource(const char* src);
159 * Get the list of the source files used by this target
161 enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
163 //* how we identify a library, by name and type
164 typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
166 typedef std::vector<LibraryID > LinkLibraryVectorType;
167 const LinkLibraryVectorType &GetLinkLibraries() const {
168 return this->LinkLibraries;}
169 const LinkLibraryVectorType &GetOriginalLinkLibraries() const
170 {return this->OriginalLinkLibraries;}
172 /** Compute the link type to use for the given configuration. */
173 LinkLibraryType ComputeLinkType(const char* config);
176 * Clear the dependency information recorded for this target, if any.
178 void ClearDependencyInformation(cmMakefile& mf, const char* target);
180 // Check to see if a library is a framework and treat it different on Mac
181 bool NameResolvesToFramework(const std::string& libname);
182 bool AddFramework(const std::string& lib, LinkLibraryType llt);
183 void AddLinkLibrary(cmMakefile& mf,
184 const char *target, const char* lib,
185 LinkLibraryType llt);
187 void AddLinkLibrary(const std::string& 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
199 * INSTALL_PREFIX
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 void AnalyzeLibDependencies( const cmMakefile& mf );
228 ///! Set/Get a property of this target file
229 void SetProperty(const char *prop, const char *value);
230 void AppendProperty(const char* prop, const char* value);
231 const char *GetProperty(const char *prop);
232 const char *GetProperty(const char *prop, cmProperty::ScopeType scope);
233 bool GetPropertyAsBool(const char *prop);
234 void CheckProperty(const char* prop, cmMakefile* context);
236 bool IsImported() const {return this->IsImportedTarget;}
238 /** The link interface specifies transitive library dependencies and
239 other information needed by targets that link to this target. */
240 struct LinkInterface
242 // Languages whose runtime libraries must be linked.
243 std::vector<std::string> Languages;
245 // Libraries listed in the interface.
246 std::vector<std::string> Libraries;
248 // Shared library dependencies needed for linking on some platforms.
249 std::vector<std::string> SharedDeps;
251 // Number of repetitions of a strongly connected component of two
252 // or more static libraries.
253 int Multiplicity;
255 // Libraries listed for other configurations.
256 // Needed only for OLD behavior of CMP0003.
257 std::vector<std::string> WrongConfigLibraries;
259 LinkInterface(): Multiplicity(0) {}
262 /** Get the link interface for the given configuration. Returns 0
263 if the target cannot be linked. */
264 LinkInterface const* GetLinkInterface(const char* config);
266 /** The link implementation specifies the direct library
267 dependencies needed by the object files of the target. */
268 struct LinkImplementation
270 // Languages whose runtime libraries must be linked.
271 std::vector<std::string> Languages;
273 // Libraries linked directly in this configuration.
274 std::vector<std::string> Libraries;
276 // Libraries linked directly in other configurations.
277 // Needed only for OLD behavior of CMP0003.
278 std::vector<std::string> WrongConfigLibraries;
280 LinkImplementation const* GetLinkImplementation(const char* config);
282 /** Link information from the transitive closure of the link
283 implementation and the interfaces of its dependencies. */
284 struct LinkClosure
286 // The preferred linker language.
287 std::string LinkerLanguage;
289 // Languages whose runtime libraries must be linked.
290 std::vector<std::string> Languages;
292 LinkClosure const* GetLinkClosure(const char* config);
294 /** Strip off leading and trailing whitespace from an item named in
295 the link dependencies of this target. */
296 std::string CheckCMP0004(std::string const& item);
298 /** Get the directory in which this target will be built. If the
299 configuration name is given then the generator will add its
300 subdirectory for that configuration. Otherwise just the canonical
301 output directory is given. */
302 std::string GetDirectory(const char* config = 0, bool implib = false);
304 /** Get the location of the target in the build tree for the given
305 configuration. This location is suitable for use as the LOCATION
306 target property. */
307 const char* GetLocation(const char* config);
309 /** Get the target major and minor version numbers interpreted from
310 the VERSION property. Version 0 is returned if the property is
311 not set or cannot be parsed. */
312 void GetTargetVersion(int& major, int& minor);
314 /** Get the target major, minor, and patch version numbers
315 interpreted from the VERSION or SOVERSION property. Version 0
316 is returned if the property is not set or cannot be parsed. */
317 void GetTargetVersion(bool soversion, int& major, int& minor, int& patch);
320 * Trace through the source files in this target and add al source files
321 * that they depend on, used by all generators
323 void TraceDependencies(const char* vsProjectFile);
326 * Make sure the full path to all source files is known.
328 bool FindSourceFiles();
330 ///! Return the prefered linker language for this target
331 const char* GetLinkerLanguage(const char* config = 0);
333 ///! Return the rule variable used to create this type of target,
334 // need to add CMAKE_(LANG) for full name.
335 const char* GetCreateRuleVariable();
337 /** Get the full name of the target according to the settings in its
338 makefile. */
339 std::string GetFullName(const char* config=0, bool implib = false);
340 void GetFullNameComponents(std::string& prefix,
341 std::string& base, std::string& suffix,
342 const char* config=0, bool implib = false);
344 /** Get the name of the pdb file for the target. */
345 std::string GetPDBName(const char* config=0);
347 /** Get the soname of the target. Allowed only for a shared library. */
348 std::string GetSOName(const char* config);
350 /** Test for special case of a third-party shared library that has
351 no soname at all. */
352 bool IsImportedSharedLibWithoutSOName(const char* config);
354 /** Get the full path to the target according to the settings in its
355 makefile and the configuration type. */
356 std::string GetFullPath(const char* config=0, bool implib = false,
357 bool realname = false);
359 /** Get the names of the library needed to generate a build rule
360 that takes into account shared library version numbers. This
361 should be called only on a library target. */
362 void GetLibraryNames(std::string& name, std::string& soName,
363 std::string& realName, std::string& impName,
364 std::string& pdbName, const char* config);
366 /** Get the names of the executable needed to generate a build rule
367 that takes into account executable version numbers. This should
368 be called only on an executable target. */
369 void GetExecutableNames(std::string& name, std::string& realName,
370 std::string& impName,
371 std::string& pdbName, const char* config);
373 /** Add the target output files to the global generator manifest. */
374 void GenerateTargetManifest(const char* config);
377 * Compute whether this target must be relinked before installing.
379 bool NeedRelinkBeforeInstall(const char* config);
381 bool HaveBuildTreeRPATH();
382 bool HaveInstallTreeRPATH();
384 /** Return true if builtin chrpath will work for this target */
385 bool IsChrpathUsed(const char* config);
387 std::string GetInstallNameDirForBuildTree(const char* config,
388 bool for_xcode = false);
389 std::string GetInstallNameDirForInstallTree(const char* config,
390 bool for_xcode = false);
392 cmComputeLinkInformation* GetLinkInformation(const char* config);
394 // Get the properties
395 cmPropertyMap &GetProperties() { return this->Properties; };
397 // Define the properties
398 static void DefineProperties(cmake *cm);
400 // Compute the OBJECT_FILES property only when requested
401 void ComputeObjectFiles();
403 /** Get the macro to define when building sources in this target.
404 If no macro should be defined null is returned. */
405 const char* GetExportMacro();
407 // Compute the set of languages compiled by the target. This is
408 // computed every time it is called because the languages can change
409 // when source file properties are changed and we do not have enough
410 // information to forward these property changes to the targets
411 // until we have per-target object file properties.
412 void GetLanguages(std::set<cmStdString>& languages) const;
414 /** Return whether this target is an executable with symbol exports
415 enabled. */
416 bool IsExecutableWithExports();
418 /** Return whether this target may be used to link another target. */
419 bool IsLinkable();
421 /** Return whether or not the target is for a DLL platform. */
422 bool IsDLLPlatform() { return this->DLLPlatform; }
424 /** Return whether or not the target has a DLL import library. */
425 bool HasImportLibrary();
427 /** Return whether this target is a shared library Framework on
428 Apple. */
429 bool IsFrameworkOnApple();
431 /** Return whether this target is an executable Bundle on Apple. */
432 bool IsAppBundleOnApple();
434 /** Return the framework version string. Undefined if
435 IsFrameworkOnApple returns false. */
436 std::string GetFrameworkVersion();
438 /** Get a backtrace from the creation of the target. */
439 cmListFileBacktrace const& GetBacktrace() const;
441 /** Get a build-tree directory in which to place target support files. */
442 std::string GetSupportDirectory() const;
444 private:
446 * A list of direct dependencies. Use in conjunction with DependencyMap.
448 typedef std::vector< LibraryID > DependencyList;
451 * This map holds the dependency graph. map[x] returns a set of
452 * direct dependencies of x. Note that the direct depenencies are
453 * ordered. This is necessary to handle direct dependencies that
454 * themselves have no dependency information.
456 typedef std::map< LibraryID, DependencyList > DependencyMap;
459 * Inserts \a dep at the end of the dependency list of \a lib.
461 void InsertDependency( DependencyMap& depMap,
462 const LibraryID& lib,
463 const LibraryID& dep);
466 * Deletes \a dep from the dependency list of \a lib.
468 void DeleteDependency( DependencyMap& depMap,
469 const LibraryID& lib,
470 const LibraryID& dep);
473 * Emits the library \a lib and all its dependencies into link_line.
474 * \a emitted keeps track of the libraries that have been emitted to
475 * avoid duplicates--it is more efficient than searching
476 * link_line. \a visited is used detect cycles. Note that \a
477 * link_line is in reverse order, in that the dependencies of a
478 * library are listed before the library itself.
480 void Emit( const LibraryID lib,
481 const DependencyMap& dep_map,
482 std::set<LibraryID>& emitted,
483 std::set<LibraryID>& visited,
484 DependencyList& link_line);
487 * Finds the dependencies for \a lib and inserts them into \a
488 * dep_map.
490 void GatherDependencies( const cmMakefile& mf,
491 const LibraryID& lib,
492 DependencyMap& dep_map);
494 const char* GetSuffixVariableInternal(bool implib);
495 const char* GetPrefixVariableInternal(bool implib);
496 std::string GetFullNameInternal(const char* config, bool implib);
497 void GetFullNameInternal(const char* config, bool implib,
498 std::string& outPrefix, std::string& outBase,
499 std::string& outSuffix);
501 // Use a makefile variable to set a default for the given property.
502 // If the variable is not defined use the given default instead.
503 void SetPropertyDefault(const char* property, const char* default_value);
505 // Returns ARCHIVE, LIBRARY, or RUNTIME based on platform and type.
506 const char* GetOutputTargetType(bool implib);
508 // Get the target base name.
509 std::string GetOutputName(const char* config, bool implib);
511 const char* ImportedGetLocation(const char* config);
512 const char* NormalGetLocation(const char* config);
514 std::string GetFullNameImported(const char* config, bool implib);
516 std::string ImportedGetFullPath(const char* config, bool implib);
517 std::string NormalGetFullPath(const char* config, bool implib,
518 bool realname);
520 /** Get the real name of the target. Allowed only for non-imported
521 targets. When a library or executable file is versioned this is
522 the full versioned name. If the target is not versioned this is
523 the same as GetFullName. */
524 std::string NormalGetRealName(const char* config);
526 private:
527 std::string Name;
528 std::vector<cmCustomCommand> PreBuildCommands;
529 std::vector<cmCustomCommand> PreLinkCommands;
530 std::vector<cmCustomCommand> PostBuildCommands;
531 TargetType TargetTypeValue;
532 std::vector<cmSourceFile*> SourceFiles;
533 std::set<cmSourceFile*> SourceFileSet;
534 LinkLibraryVectorType LinkLibraries;
535 LinkLibraryVectorType PrevLinkedLibraries;
536 bool LinkLibrariesAnalyzed;
537 std::vector<std::string> Frameworks;
538 std::vector<std::string> LinkDirectories;
539 std::set<cmStdString> LinkDirectoriesEmmitted;
540 bool HaveInstallRule;
541 std::string InstallPath;
542 std::string RuntimeInstallPath;
543 std::string Location;
544 std::string ExportMacro;
545 std::set<cmStdString> Utilities;
546 bool RecordDependencies;
547 cmPropertyMap Properties;
548 LinkLibraryVectorType OriginalLinkLibraries;
549 bool DLLPlatform;
550 bool IsImportedTarget;
552 // Cache target output paths for each configuration.
553 struct OutputInfo;
554 OutputInfo const* GetOutputInfo(const char* config);
555 void ComputeOutputDir(const char* config, bool implib, std::string& out);
557 // Cache import information from properties for each configuration.
558 struct ImportInfo;
559 ImportInfo const* GetImportInfo(const char* config);
560 void ComputeImportInfo(std::string const& desired_config, ImportInfo& info);
562 cmTargetLinkInformationMap LinkInformation;
564 bool ComputeLinkInterface(const char* config, LinkInterface& iface);
566 void ComputeLinkImplementation(const char* config,
567 LinkImplementation& impl);
568 void ComputeLinkClosure(const char* config, LinkClosure& lc);
570 // The cmMakefile instance that owns this target. This should
571 // always be set.
572 cmMakefile* Makefile;
574 // Policy status recorded when target was created.
575 cmPolicies::PolicyStatus PolicyStatusCMP0003;
576 cmPolicies::PolicyStatus PolicyStatusCMP0004;
577 cmPolicies::PolicyStatus PolicyStatusCMP0008;
579 // Internal representation details.
580 friend class cmTargetInternals;
581 cmTargetInternalPointer Internal;
583 void ConstructSourceFileFlags();
586 typedef std::map<cmStdString,cmTarget> cmTargets;
588 class cmTargetSet: public std::set<cmStdString> {};
589 class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
591 #endif