Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmGlobalGenerator.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
13 #ifndef cmGlobalGenerator_h
14 #define cmGlobalGenerator_h
15
16 #include "cmStandardIncludes.h"
17
18 #include "cmTarget.h" // For cmTargets
19 #include "cmTargetDepend.h" // For cmTargetDependSet
20 #include "cmSystemTools.h" // for cmSystemTools::OutputOption
21 class cmake;
22 class cmGeneratorTarget;
23 class cmMakefile;
24 class cmLocalGenerator;
25 class cmExternalMakefileProjectGenerator;
26 class cmTarget;
27 class cmTargetExport;
28 class cmInstallTargetGenerator;
29 class cmInstallFilesGenerator;
30
31 /** \class cmGlobalGenerator
32  * \brief Responable for overseeing the generation process for the entire tree
33  *
34  * Subclasses of this class generate makefiles for various
35  * platforms.
36  */
37 class cmGlobalGenerator
38 {
39 public:
40   ///! Free any memory allocated with the GlobalGenerator
41   cmGlobalGenerator();
42   virtual ~cmGlobalGenerator();
43
44   ///! Create a local generator appropriate to this Global Generator
45   virtual cmLocalGenerator *CreateLocalGenerator();
46
47   ///! Get the name for this generator
48   virtual const char *GetName() const { return "Generic"; };
49
50   /** Get the documentation entry for this generator.  */
51   virtual void GetDocumentation(cmDocumentationEntry& entry) const;
52
53   /**
54    * Create LocalGenerators and process the CMakeLists files. This does not
55    * actually produce any makefiles, DSPs, etc.
56    */
57   virtual void Configure();
58
59   /**
60    * Generate the all required files for building this project/tree. This
61    * basically creates a series of LocalGenerators for each directory and
62    * requests that they Generate.
63    */
64   virtual void Generate();
65
66   /**
67    * Set/Get and Clear the enabled languages.
68    */
69   void SetLanguageEnabled(const char*, cmMakefile* mf);
70   bool GetLanguageEnabled(const char*) const;
71   void ClearEnabledLanguages();
72   void GetEnabledLanguages(std::vector<std::string>& lang);
73   /**
74    * Try to determine system infomation such as shared library
75    * extension, pthreads, byte order etc.
76    */
77   virtual void EnableLanguage(std::vector<std::string>const& languages,
78                               cmMakefile *, bool optional);
79
80   /**
81    * Resolve the CMAKE_<lang>_COMPILER setting for the given language.
82    * Intended to be called from EnableLanguage.
83    */
84   void ResolveLanguageCompiler(const std::string &lang, cmMakefile *mf,
85                                bool optional);
86
87   /**
88    * Try to determine system infomation, get it from another generator
89    */
90   virtual void EnableLanguagesFromGenerator(cmGlobalGenerator *gen,
91                                             cmMakefile* mf);
92
93   /**
94    * Try running cmake and building a file. This is used for dynamically
95    * loaded commands, not as part of the usual build process.
96    */
97   virtual int TryCompile(const char *srcdir, const char *bindir,
98                          const char *projectName, const char *targetName,
99                          bool fast, std::string *output, cmMakefile* mf);
100
101
102   /**
103    * Build a file given the following information. This is a more direct call
104    * that is used by both CTest and TryCompile. If target name is NULL or
105    * empty then all is assumed. clean indicates if a "make clean" should be
106    * done first.
107    */
108   int Build(const char *srcdir, const char *bindir,
109             const char *projectName, const char *targetName,
110             std::string *output,
111             const char *makeProgram, const char *config,
112             bool clean, bool fast,
113             double timeout,
114             cmSystemTools::OutputOption outputflag=cmSystemTools::OUTPUT_NONE,
115             const char* extraOptions = 0,
116             std::vector<std::string> const& nativeOptions =
117             std::vector<std::string>());
118
119   virtual std::string GenerateBuildCommand(
120     const char* makeProgram,
121     const char *projectName, const char* additionalOptions,
122     const char *targetName,
123     const char* config, bool ignoreErrors, bool fast);
124
125
126   ///! Set the CMake instance
127   void SetCMakeInstance(cmake *cm);
128
129   ///! Get the CMake instance
130   cmake *GetCMakeInstance() { return this->CMakeInstance; };
131   const cmake *GetCMakeInstance() const { return this->CMakeInstance; };
132
133   void SetConfiguredFilesPath(cmGlobalGenerator* gen);
134   const std::vector<cmLocalGenerator *>& GetLocalGenerators() const {
135     return this->LocalGenerators;}
136
137   cmLocalGenerator* GetCurrentLocalGenerator()
138                                           {return this->CurrentLocalGenerator;}
139
140   void SetCurrentLocalGenerator(cmLocalGenerator* lg)
141                                             {this->CurrentLocalGenerator = lg;}
142
143   void AddLocalGenerator(cmLocalGenerator *lg);
144
145   ///! Set an generator for an "external makefile based project"
146   void SetExternalMakefileProjectGenerator(
147                            cmExternalMakefileProjectGenerator *extraGenerator);
148
149   const char* GetExtraGeneratorName() const;
150
151   void AddInstallComponent(const char* component);
152
153   const std::set<cmStdString>* GetInstallComponents() const
154   { return &InstallComponents; }
155
156   ///! Add one installed target to the sets of the exports
157   void AddTargetToExports(const char* exportSet, cmTarget* target,
158                           cmInstallTargetGenerator* archive,
159                           cmInstallTargetGenerator* runTime,
160                           cmInstallTargetGenerator* library,
161                           cmInstallTargetGenerator* framework,
162                           cmInstallTargetGenerator* bundle,
163                           cmInstallFilesGenerator* publicHeaders);
164   ///! Get the export target set with the   given name
165   const std::vector<cmTargetExport*>* GetExportSet(const char* name) const;
166
167   /** Add a file to the manifest of generated targets for a configuration.  */
168   void AddToManifest(const char* config, std::string const& f);
169
170   void EnableInstallTarget();
171
172   int TryCompileTimeout;
173
174   bool GetForceUnixPaths() const { return this->ForceUnixPaths; }
175   bool GetToolSupportsColor() const { return this->ToolSupportsColor; }
176
177   ///! return the language for the given extension
178   const char* GetLanguageFromExtension(const char* ext);
179   ///! is an extension to be ignored
180   bool IgnoreFile(const char* ext);
181   ///! What is the preference for linkers and this language (None or Prefered)
182   int GetLinkerPreference(const char* lang);
183   ///! What is the object file extension for a given source file?
184   const char* GetLanguageOutputExtension(cmSourceFile const&);
185
186   ///! What is the configurations directory variable called?
187   virtual const char* GetCMakeCFGIntDir() const { return "."; }
188
189   /** Get whether the generator should use a script for link commands.  */
190   bool GetUseLinkScript() const { return this->UseLinkScript; }
191
192   /** Get whether the generator should produce special marks on rules
193       producing symbolic (non-file) outputs.  */
194   bool GetNeedSymbolicMark() const { return this->NeedSymbolicMark; }
195
196   /*
197    * Determine what program to use for building the project.
198    */
199   void FindMakeProgram(cmMakefile*);
200
201   ///! Find a target by name by searching the local generators.
202   cmTarget* FindTarget(const char* project, const char* name);
203
204   /** Determine if a name resolves to a framework on disk or a built target
205       that is a framework. */
206   bool NameResolvesToFramework(const std::string& libname);
207
208   /** If check to see if the target is linked to by any other
209       target in the project */
210   bool IsDependedOn(const char* project, cmTarget* target);
211   ///! Find a local generator by its startdirectory
212   cmLocalGenerator* FindLocalGenerator(const char* start_dir);
213
214   /** Append the subdirectory for the given configuration.  If anything is
215       appended the given prefix and suffix will be appended around it, which
216       is useful for leading or trailing slashes.  */
217   virtual void AppendDirectoryForConfig(const char* prefix,
218                                         const char* config,
219                                         const char* suffix,
220                                         std::string& dir);
221
222   /** Get the manifest of all targets that will be built for each
223       configuration.  This is valid during generation only.  */
224   cmTargetManifest const& GetTargetManifest() const
225   { return this->TargetManifest; }
226
227   /** Get the content of a directory.  Directory listings are loaded
228       from disk at most once and cached.  During the generation step
229       the content will include the target files to be built even if
230       they do not yet exist.  */
231   std::set<cmStdString> const& GetDirectoryContent(std::string const& dir,
232                                                    bool needDisk = true);
233
234   void AddTarget(cmTarget* t);
235
236   virtual const char* GetAllTargetName()         const { return "ALL_BUILD"; }
237   virtual const char* GetInstallTargetName()       const { return "INSTALL"; }
238   virtual const char* GetInstallLocalTargetName()  const { return 0; }
239   virtual const char* GetInstallStripTargetName()  const { return 0; }
240   virtual const char* GetPreinstallTargetName()    const { return 0; }
241   virtual const char* GetTestTargetName()        const { return "RUN_TESTS"; }
242   virtual const char* GetPackageTargetName()       const { return "PACKAGE"; }
243   virtual const char* GetPackageSourceTargetName() const { return 0; }
244   virtual const char* GetEditCacheTargetName()     const { return 0; }
245   virtual const char* GetRebuildCacheTargetName()  const { return 0; }
246   virtual const char* GetCleanTargetName()         const { return 0; }
247
248   // Class to track a set of dependencies.
249   typedef cmTargetDependSet TargetDependSet;
250
251   // what targets does the specified target depend on directly
252   // via a target_link_libraries or add_dependencies
253   TargetDependSet const& GetTargetDirectDepends(cmTarget & target);
254
255   /** Get per-target generator information.  */
256   cmGeneratorTarget* GetGeneratorTarget(cmTarget*) const;
257
258   const std::map<cmStdString, std::vector<cmLocalGenerator*> >& GetProjectMap()
259                                                const {return this->ProjectMap;}
260
261   // track files replaced during a Generate
262   void FileReplacedDuringGenerate(const std::string& filename);
263   void GetFilesReplacedDuringGenerate(std::vector<std::string>& filenames);
264
265   void AddRuleHash(const std::vector<std::string>& outputs,
266                    std::string const& content);
267
268   /** Return whether the given binary directory is unused.  */
269   bool BinaryDirectoryIsNew(const char* dir)
270     {
271     return this->BinaryDirectories.insert(dir).second;
272     }
273   /** Supported systems creates a GUID for the given name */
274   virtual void CreateGUID(const char*) {}
275
276   /** Return true if the generated build tree may contain multiple builds.
277       i.e. "Can I build Debug and Release in the same tree?" */
278   virtual bool IsMultiConfig() { return false; }
279
280   std::string GetSharedLibFlagsForLanguage(std::string const& lang);
281
282   /** Generate an <output>.rule file path for a given command output.  */
283   virtual std::string GenerateRuleFile(std::string const& output) const;
284
285   static std::string EscapeJSON(const std::string& s);
286
287 protected:
288   typedef std::vector<cmLocalGenerator*> GeneratorVector;
289   // for a project collect all its targets by following depend
290   // information, and also collect all the targets
291   virtual void GetTargetSets(TargetDependSet& projectTargets,
292                              TargetDependSet& originalTargets,
293                              cmLocalGenerator* root, GeneratorVector const&);
294   virtual bool IsRootOnlyTarget(cmTarget* target);
295   void AddTargetDepends(cmTarget* target, TargetDependSet& projectTargets);
296   void SetLanguageEnabledFlag(const char* l, cmMakefile* mf);
297   void SetLanguageEnabledMaps(const char* l, cmMakefile* mf);
298   void FillExtensionToLanguageMap(const char* l, cmMakefile* mf);
299
300   virtual bool ComputeTargetDepends();
301
302   virtual bool CheckALLOW_DUPLICATE_CUSTOM_TARGETS();
303
304   bool CheckTargets();
305   void CreateAutomocTargets();
306
307
308   // Fill the ProjectMap, this must be called after LocalGenerators
309   // has been populated.
310   void FillProjectMap();
311   void CheckLocalGenerators();
312   bool IsExcluded(cmLocalGenerator* root, cmLocalGenerator* gen);
313   bool IsExcluded(cmLocalGenerator* root, cmTarget& target);
314   void FillLocalGeneratorToTargetMap();
315   void CreateDefaultGlobalTargets(cmTargets* targets);
316   cmTarget CreateGlobalTarget(const char* name, const char* message,
317     const cmCustomCommandLines* commandLines,
318     std::vector<std::string> depends, const char* workingDir);
319
320   bool NeedSymbolicMark;
321   bool UseLinkScript;
322   bool ForceUnixPaths;
323   bool ToolSupportsColor;
324   cmStdString FindMakeProgramFile;
325   cmStdString ConfiguredFilesPath;
326   cmake *CMakeInstance;
327   std::vector<cmLocalGenerator *> LocalGenerators;
328   cmLocalGenerator* CurrentLocalGenerator;
329   // map from project name to vector of local generators in that project
330   std::map<cmStdString, std::vector<cmLocalGenerator*> > ProjectMap;
331   std::map<cmLocalGenerator*, std::set<cmTarget *> >
332   LocalGeneratorToTargetMap;
333
334   // Set of named installation components requested by the project.
335   std::set<cmStdString> InstallComponents;
336   bool InstallTargetEnabled;
337   // Sets of named target exports
338   std::map<cmStdString, std::vector<cmTargetExport*> > ExportSets;
339   void ClearExportSets();
340
341   // Manifest of all targets that will be built for each configuration.
342   // This is computed just before local generators generate.
343   cmTargetManifest TargetManifest;
344
345   // All targets in the entire project.
346   std::map<cmStdString,cmTarget *> TotalTargets;
347   std::map<cmStdString,cmTarget *> ImportedTargets;
348
349   virtual const char* GetPredefinedTargetsFolder();
350   virtual bool UseFolderProperty();
351
352 private:
353   cmMakefile* TryCompileOuterMakefile;
354   float FirstTimeProgress;
355   // If you add a new map here, make sure it is copied
356   // in EnableLanguagesFromGenerator
357   std::map<cmStdString, bool> IgnoreExtensions;
358   std::map<cmStdString, bool> LanguageEnabled;
359   std::set<cmStdString> LanguagesReady; // Ready for try_compile
360   std::map<cmStdString, cmStdString> OutputExtensions;
361   std::map<cmStdString, cmStdString> LanguageToOutputExtension;
362   std::map<cmStdString, cmStdString> ExtensionToLanguage;
363   std::map<cmStdString, int> LanguageToLinkerPreference;
364   std::map<cmStdString, cmStdString> LanguageToOriginalSharedLibFlags;
365
366   // Record hashes for rules and outputs.
367   struct RuleHash { char Data[32]; };
368   std::map<cmStdString, RuleHash> RuleHashes;
369   void CheckRuleHashes();
370   void CheckRuleHashes(std::string const& pfile, std::string const& home);
371   void WriteRuleHashes(std::string const& pfile);
372
373   void WriteSummary();
374   void WriteSummary(cmTarget* target);
375
376   cmExternalMakefileProjectGenerator* ExtraGenerator;
377
378   // track files replaced during a Generate
379   std::vector<std::string> FilesReplacedDuringGenerate;
380
381   // Store computed inter-target dependencies.
382   typedef std::map<cmTarget *, TargetDependSet> TargetDependMap;
383   TargetDependMap TargetDependencies;
384
385   // Per-target generator information.
386   typedef std::map<cmTarget*, cmGeneratorTarget*> GeneratorTargetsType;
387   GeneratorTargetsType GeneratorTargets;
388   void CreateGeneratorTargets();
389   void ClearGeneratorTargets();
390   virtual void ComputeTargetObjects(cmGeneratorTarget* gt) const;
391
392   // Cache directory content and target files to be built.
393   struct DirectoryContent: public std::set<cmStdString>
394   {
395     typedef std::set<cmStdString> derived;
396     bool LoadedFromDisk;
397     DirectoryContent(): LoadedFromDisk(false) {}
398     DirectoryContent(DirectoryContent const& dc):
399       derived(dc), LoadedFromDisk(dc.LoadedFromDisk) {}
400   };
401   std::map<cmStdString, DirectoryContent> DirectoryContentMap;
402
403   // Set of binary directories on disk.
404   std::set<cmStdString> BinaryDirectories;
405 };
406
407 #endif