Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmTarget.h
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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 ============================================================================*/
12 #ifndef cmTarget_h
13 #define cmTarget_h
14
15 #include "cmCustomCommand.h"
16 #include "cmPropertyMap.h"
17 #include "cmPolicies.h"
18
19 #include <cmsys/auto_ptr.hxx>
20
21 class cmake;
22 class cmMakefile;
23 class cmSourceFile;
24 class cmGlobalGenerator;
25 class cmComputeLinkInformation;
26 class cmListFileBacktrace;
27
28 struct cmTargetLinkInformationMap:
29   public std::map<cmStdString, cmComputeLinkInformation*>
30 {
31   typedef std::map<cmStdString, cmComputeLinkInformation*> derived;
32   cmTargetLinkInformationMap() {}
33   cmTargetLinkInformationMap(cmTargetLinkInformationMap const& r);
34   ~cmTargetLinkInformationMap();
35 };
36
37 class cmTargetInternals;
38 class cmTargetInternalPointer
39 {
40 public:
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; }
47 private:
48   cmTargetInternals* Pointer;
49 };
50
51 /** \class cmTarget
52  * \brief Represent a library or executable target loaded from a makefile.
53  *
54  * cmTarget represents a target loaded from
55  * a makefile.
56  */
57 class cmTarget
58 {
59 public:
60   cmTarget();
61   enum TargetType { EXECUTABLE, STATIC_LIBRARY,
62                     SHARED_LIBRARY, MODULE_LIBRARY,
63                     OBJECT_LIBRARY, UTILITY, GLOBAL_TARGET,
64                     UNKNOWN_LIBRARY};
65   static const char* GetTargetTypeName(TargetType targetType);
66   enum CustomCommandType { PRE_BUILD, PRE_LINK, POST_BUILD };
67
68   /**
69    * Return the type of target.
70    */
71   TargetType GetType() const
72     {
73     return this->TargetTypeValue;
74     }
75
76   /**
77    * Set the target type
78    */
79   void SetType(TargetType f, const char* name);
80
81   void MarkAsImported();
82
83   ///! Set/Get the name of the target
84   const char* GetName() const {return this->Name.c_str();}
85
86   ///! Set the cmMakefile that owns this target
87   void SetMakefile(cmMakefile *mf);
88   cmMakefile *GetMakefile() const { return this->Makefile;};
89
90   /** Get the status of policy CMP0003 when the target was created.  */
91   cmPolicies::PolicyStatus GetPolicyStatusCMP0003() const
92     { return this->PolicyStatusCMP0003; }
93
94   /** Get the status of policy CMP0004 when the target was created.  */
95   cmPolicies::PolicyStatus GetPolicyStatusCMP0004() const
96     { return this->PolicyStatusCMP0004; }
97
98   /** Get the status of policy CMP0008 when the target was created.  */
99   cmPolicies::PolicyStatus GetPolicyStatusCMP0008() const
100     { return this->PolicyStatusCMP0008; }
101
102   /**
103    * Get the list of the custom commands for this target
104    */
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;}
111
112   ///! Return the list of frameworks being linked to this target
113   std::vector<std::string> &GetFrameworks() {return this->Frameworks;}
114
115   /**
116    * Get the list of the source files used by this target
117    */
118   std::vector<cmSourceFile*> const& GetSourceFiles();
119   void AddSourceFile(cmSourceFile* sf);
120   std::vector<std::string> const& GetObjectLibraries() const
121     {
122     return this->ObjectLibraries;
123     }
124
125   /** Get sources that must be built before the given source.  */
126   std::vector<cmSourceFile*> const* GetSourceDepends(cmSourceFile* sf);
127
128   /**
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.
131    */
132   enum SourceFileType
133   {
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"
140   };
141   struct SourceFileFlags
142   {
143     SourceFileFlags(): Type(SourceFileTypeNormal), MacFolder(0) {}
144     SourceFileFlags(SourceFileFlags const& r):
145       Type(r.Type), MacFolder(r.MacFolder) {}
146     SourceFileType Type;
147     const char* MacFolder; // location inside Mac content folders
148   };
149
150   /**
151    * Get the flags for a given source file as used in this target
152    */
153   struct SourceFileFlags GetTargetSourceFileFlags(const cmSourceFile* sf);
154
155   /**
156    * Add sources to the target.
157    */
158   void AddSources(std::vector<std::string> const& srcs);
159   cmSourceFile* AddSource(const char* src);
160
161   /**
162    * Get the list of the source files used by this target
163    */
164   enum LinkLibraryType {GENERAL, DEBUG, OPTIMIZED};
165
166   //* how we identify a library, by name and type
167   typedef std::pair<cmStdString, LinkLibraryType> LibraryID;
168
169   typedef std::vector<LibraryID > LinkLibraryVectorType;
170   const LinkLibraryVectorType &GetLinkLibraries() const {
171   return this->LinkLibraries;}
172   const LinkLibraryVectorType &GetOriginalLinkLibraries() const
173     {return this->OriginalLinkLibraries;}
174
175   /** Compute the link type to use for the given configuration.  */
176   LinkLibraryType ComputeLinkType(const char* config);
177
178   /**
179    * Clear the dependency information recorded for this target, if any.
180    */
181   void ClearDependencyInformation(cmMakefile& mf, const char* target);
182
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);
189
190   void MergeLinkLibraries( cmMakefile& mf, const char* selfname,
191                            const LinkLibraryVectorType& libs );
192
193   const std::vector<std::string>& GetLinkDirectories();
194
195   void AddLinkDirectory(const char* d);
196
197   /**
198    * Set the path where this target should be installed. This is relative to
199    * INSTALL_PREFIX
200    */
201   std::string GetInstallPath() {return this->InstallPath;}
202   void SetInstallPath(const char *name) {this->InstallPath = name;}
203
204   /**
205    * Set the path where this target (if it has a runtime part) should be
206    * installed. This is relative to INSTALL_PREFIX
207    */
208   std::string GetRuntimeInstallPath() {return this->RuntimeInstallPath;}
209   void SetRuntimeInstallPath(const char *name) {
210     this->RuntimeInstallPath = name; }
211
212   /**
213    * Get/Set whether there is an install rule for this target.
214    */
215   bool GetHaveInstallRule() { return this->HaveInstallRule; }
216   void SetHaveInstallRule(bool h) { this->HaveInstallRule = h; }
217
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.
221    */
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; }
225
226   /** Finalize the target at the end of the Configure step.  */
227   void FinishConfigure();
228
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);
236
237   const char* GetFeature(const char* feature, const char* config);
238
239   bool IsImported() const {return this->IsImportedTarget;}
240
241   /** The link interface specifies transitive library dependencies and
242       other information needed by targets that link to this target.  */
243   struct LinkInterface
244   {
245     // Languages whose runtime libraries must be linked.
246     std::vector<std::string> Languages;
247
248     // Libraries listed in the interface.
249     std::vector<std::string> Libraries;
250
251     // Shared library dependencies needed for linking on some platforms.
252     std::vector<std::string> SharedDeps;
253
254     // Number of repetitions of a strongly connected component of two
255     // or more static libraries.
256     int Multiplicity;
257
258     // Libraries listed for other configurations.
259     // Needed only for OLD behavior of CMP0003.
260     std::vector<std::string> WrongConfigLibraries;
261
262     LinkInterface(): Multiplicity(0) {}
263   };
264
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);
268
269   /** The link implementation specifies the direct library
270       dependencies needed by the object files of the target.  */
271   struct LinkImplementation
272   {
273     // Languages whose runtime libraries must be linked.
274     std::vector<std::string> Languages;
275
276     // Libraries linked directly in this configuration.
277     std::vector<std::string> Libraries;
278
279     // Libraries linked directly in other configurations.
280     // Needed only for OLD behavior of CMP0003.
281     std::vector<std::string> WrongConfigLibraries;
282   };
283   LinkImplementation const* GetLinkImplementation(const char* config);
284
285   /** Link information from the transitive closure of the link
286       implementation and the interfaces of its dependencies.  */
287   struct LinkClosure
288   {
289     // The preferred linker language.
290     std::string LinkerLanguage;
291
292     // Languages whose runtime libraries must be linked.
293     std::vector<std::string> Languages;
294   };
295   LinkClosure const* GetLinkClosure(const char* config);
296
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);
300
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);
306
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
309       target property.  */
310   const char* GetLocation(const char* config);
311
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);
316
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);
321
322   /**
323    * Trace through the source files in this target and add al source files
324    * that they depend on, used by all generators
325    */
326   void TraceDependencies(const char* vsProjectFile);
327
328   /**
329    * Make sure the full path to all source files is known.
330    */
331   bool FindSourceFiles();
332
333   ///! Return the preferred linker language for this target
334   const char* GetLinkerLanguage(const char* config = 0);
335
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();
339
340   /** Get the full name of the target according to the settings in its
341       makefile.  */
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);
346
347   /** Get the name of the pdb file for the target.  */
348   std::string GetPDBName(const char* config=0);
349
350   /** Whether this library has soname enabled and platform supports it.  */
351   bool HasSOName(const char* config);
352
353   /** Get the soname of the target.  Allowed only for a shared library.  */
354   std::string GetSOName(const char* config);
355
356   /** Test for special case of a third-party shared library that has
357       no soname at all.  */
358   bool IsImportedSharedLibWithoutSOName(const char* config);
359
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);
364
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);
371
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);
378
379   /** Does this target have a GNU implib to convert to MS format?  */
380   bool HasImplibGNUtoMS();
381
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);
386
387   /** Add the target output files to the global generator manifest.  */
388   void GenerateTargetManifest(const char* config);
389
390   /**
391    * Compute whether this target must be relinked before installing.
392    */
393   bool NeedRelinkBeforeInstall(const char* config);
394
395   bool HaveBuildTreeRPATH();
396   bool HaveInstallTreeRPATH();
397
398   /** Return true if builtin chrpath will work for this target */
399   bool IsChrpathUsed(const char* config);
400
401   std::string GetInstallNameDirForBuildTree(const char* config,
402                                             bool for_xcode = false);
403   std::string GetInstallNameDirForInstallTree(const char* config,
404                                               bool for_xcode = false);
405
406   cmComputeLinkInformation* GetLinkInformation(const char* config);
407
408   // Get the properties
409   cmPropertyMap &GetProperties() { return this->Properties; };
410
411   // Define the properties
412   static void DefineProperties(cmake *cm);
413
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();
417
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;
424
425   /** Get the list of OS X target architectures to be built.  */
426   void GetAppleArchs(const char* config, std::vector<std::string>& archVec);
427
428   /** Return whether this target is an executable with symbol exports
429       enabled.  */
430   bool IsExecutableWithExports();
431
432   /** Return whether this target may be used to link another target.  */
433   bool IsLinkable();
434
435   /** Return whether or not the target is for a DLL platform.  */
436   bool IsDLLPlatform() { return this->DLLPlatform; }
437
438   /** Return whether or not the target has a DLL import library.  */
439   bool HasImportLibrary();
440
441   /** Return whether this target is a shared library Framework on
442       Apple.  */
443   bool IsFrameworkOnApple();
444
445   /** Return whether this target is a CFBundle (plugin) on Apple.  */
446   bool IsCFBundleOnApple();
447
448   /** Return whether this target is an executable Bundle on Apple.  */
449   bool IsAppBundleOnApple();
450
451   /** Return the framework version string.  Undefined if
452       IsFrameworkOnApple returns false.  */
453   std::string GetFrameworkVersion();
454
455   /** Get a backtrace from the creation of the target.  */
456   cmListFileBacktrace const& GetBacktrace() const;
457
458   /** Get a build-tree directory in which to place target support files.  */
459   std::string GetSupportDirectory() const;
460
461   /** Return whether this target uses the default value for its output
462       directory.  */
463   bool UsesDefaultOutputDir(const char* config, bool implib);
464
465   /** Get the include directories for this target.  */
466   std::vector<std::string> GetIncludeDirectories();
467
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);
472
473   /** @return the mac content directory for this target. */
474   std::string GetMacContentDirectory(const char* config = 0,
475                                      bool implib = false,
476                                      bool includeMacOS = true);
477
478   /** @return whether this target have a well defined output file name. */
479   bool HaveWellDefinedOutputFiles();
480
481   /** @return the Mac framework directory without the base. */
482   std::string GetFrameworkDirectory(const char* config = 0);
483
484 private:
485   /**
486    * A list of direct dependencies. Use in conjunction with DependencyMap.
487    */
488   typedef std::vector< LibraryID > DependencyList;
489
490   /**
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.
495    */
496   typedef std::map< LibraryID, DependencyList > DependencyMap;
497
498   /**
499    * Inserts \a dep at the end of the dependency list of \a lib.
500    */
501   void InsertDependency( DependencyMap& depMap,
502                          const LibraryID& lib,
503                          const LibraryID& dep);
504
505   /*
506    * Deletes \a dep from the dependency list of \a lib.
507    */
508   void DeleteDependency( DependencyMap& depMap,
509                          const LibraryID& lib,
510                          const LibraryID& dep);
511
512   /**
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.
519    */
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);
525
526   /**
527    * Finds the dependencies for \a lib and inserts them into \a
528    * dep_map.
529    */
530   void GatherDependencies( const cmMakefile& mf,
531                            const LibraryID& lib,
532                            DependencyMap& dep_map);
533
534   void AnalyzeLibDependencies( const cmMakefile& mf );
535
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);
542
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);
546
547   // Returns ARCHIVE, LIBRARY, or RUNTIME based on platform and type.
548   const char* GetOutputTargetType(bool implib);
549
550   // Get the target base name.
551   std::string GetOutputName(const char* config, bool implib);
552
553   const char* ImportedGetLocation(const char* config);
554   const char* NormalGetLocation(const char* config);
555
556   std::string GetFullNameImported(const char* config, bool implib);
557
558   std::string ImportedGetFullPath(const char* config, bool implib);
559   std::string NormalGetFullPath(const char* config, bool implib,
560                                 bool realname);
561
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);
567
568 private:
569   std::string Name;
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;
591   bool DLLPlatform;
592   bool IsApple;
593   bool IsImportedTarget;
594
595   // Cache target output paths for each configuration.
596   struct OutputInfo;
597   OutputInfo const* GetOutputInfo(const char* config);
598   bool ComputeOutputDir(const char* config, bool implib, std::string& out);
599
600   // Cache import information from properties for each configuration.
601   struct ImportInfo;
602   ImportInfo const* GetImportInfo(const char* config);
603   void ComputeImportInfo(std::string const& desired_config, ImportInfo& info);
604
605   cmTargetLinkInformationMap LinkInformation;
606
607   bool ComputeLinkInterface(const char* config, LinkInterface& iface);
608
609   void ComputeLinkImplementation(const char* config,
610                                  LinkImplementation& impl);
611   void ComputeLinkClosure(const char* config, LinkClosure& lc);
612
613   void ClearLinkMaps();
614
615   void MaybeInvalidatePropertyCache(const char* prop);
616
617   void ProcessSourceExpression(std::string const& expr);
618
619   // The cmMakefile instance that owns this target.  This should
620   // always be set.
621   cmMakefile* Makefile;
622
623   // Policy status recorded when target was created.
624   cmPolicies::PolicyStatus PolicyStatusCMP0003;
625   cmPolicies::PolicyStatus PolicyStatusCMP0004;
626   cmPolicies::PolicyStatus PolicyStatusCMP0008;
627
628   // Internal representation details.
629   friend class cmTargetInternals;
630   cmTargetInternalPointer Internal;
631
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);
639 };
640
641 typedef std::map<cmStdString,cmTarget> cmTargets;
642
643 class cmTargetSet: public std::set<cmStdString> {};
644 class cmTargetManifest: public std::map<cmStdString, cmTargetSet> {};
645
646 #endif