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