Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmLocalGenerator.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 cmLocalGenerator_h
13 #define cmLocalGenerator_h
14
15 #include "cmStandardIncludes.h"
16
17 class cmMakefile;
18 class cmGlobalGenerator;
19 class cmTarget;
20 class cmTargetManifest;
21 class cmSourceFile;
22 class cmCustomCommand;
23
24 /** \class cmLocalGenerator
25  * \brief Create required build files for a directory.
26  *
27  * Subclasses of this abstract class generate makefiles, DSP, etc for various
28  * platforms. This class should never be constructed directly. A
29  * GlobalGenerator will create it and invoke the appropriate commands on it.
30  */
31 class cmLocalGenerator
32 {
33 public:
34   cmLocalGenerator();
35   virtual ~cmLocalGenerator();
36
37   /**
38    * Generate the makefile for this directory.
39    */
40   virtual void Generate() {}
41
42   /**
43    * Process the CMakeLists files for this directory to fill in the
44    * Makefile ivar
45    */
46   virtual void Configure();
47
48   /**
49    * Calls TraceVSDependencies() on all targets of this generator.
50    */
51   virtual void TraceDependencies();
52
53   virtual void AddHelperCommands() {}
54
55   /**
56    * Perform any final calculations prior to generation
57    */
58   virtual void ConfigureFinalPass();
59
60   /**
61    * Generate the install rules files in this directory.
62    */
63   virtual void GenerateInstallRules();
64
65   /**
66    * Generate the test files for tests.
67    */
68   virtual void GenerateTestFiles();
69
70   /**
71    * Generate a manifest of target files that will be built.
72    */
73   virtual void GenerateTargetManifest();
74
75   ///! Get the makefile for this generator
76   cmMakefile *GetMakefile() {
77     return this->Makefile; };
78
79   ///! Get the makefile for this generator, const version
80     const cmMakefile *GetMakefile() const {
81       return this->Makefile; };
82
83   ///! Get the GlobalGenerator this is associated with
84   cmGlobalGenerator *GetGlobalGenerator() {
85     return this->GlobalGenerator; };
86   const cmGlobalGenerator *GetGlobalGenerator() const {
87     return this->GlobalGenerator; };
88
89   ///! Set the Global Generator, done on creation by the GlobalGenerator
90   void SetGlobalGenerator(cmGlobalGenerator *gg);
91
92   /**
93    * Convert something to something else. This is a centralized conversion
94    * routine used by the generators to handle relative paths and the like.
95    * The flags determine what is actually done.
96    *
97    * relative: treat the argument as a directory and convert it to make it
98    * relative or full or unchanged. If relative (HOME, START etc) then that
99    * specifies what it should be relative to.
100    *
101    * output: make the result suitable for output to a...
102    *
103    * optional: should any relative path operation be controlled by the rel
104    * path setting
105    */
106   enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
107   enum OutputFormat { UNCHANGED, MAKEFILE, SHELL, RESPONSE };
108   std::string ConvertToOutputFormat(const char* source, OutputFormat output);
109   std::string Convert(const char* remote, RelativeRoot local,
110                       OutputFormat output = UNCHANGED,
111                       bool optional = false);
112   std::string Convert(RelativeRoot remote, const char* local,
113                       OutputFormat output = UNCHANGED,
114                       bool optional = false);
115
116   /**
117     * Get path for the specified relative root.
118     */
119   const char* GetRelativeRootPath(RelativeRoot relroot);
120
121   /**
122    * Convert the given path to an output path that is optionally
123    * relative based on the cache option CMAKE_USE_RELATIVE_PATHS.  The
124    * remote path must use forward slashes and not already be escaped
125    * or quoted.
126    */
127   std::string ConvertToOptionallyRelativeOutputPath(const char* remote);
128
129   ///! set/get the parent generator
130   cmLocalGenerator* GetParent(){return this->Parent;}
131   void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
132
133   ///! set/get the children
134   void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
135   std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; };
136
137
138   void AddArchitectureFlags(std::string& flags, cmTarget* target,
139                             const char *lang, const char* config);
140
141   void AddLanguageFlags(std::string& flags, const char* lang,
142                         const char* config);
143   void AddCMP0018Flags(std::string &flags, cmTarget* target,
144                        std::string const& lang);
145   void AddConfigVariableFlags(std::string& flags, const char* var,
146                               const char* config);
147   ///! Append flags to a string.
148   virtual void AppendFlags(std::string& flags, const char* newFlags);
149   ///! Get the include flags for the current makefile and language
150   std::string GetIncludeFlags(const std::vector<std::string> &includes,
151                               const char* lang, bool forResponseFile = false);
152
153   /**
154    * Encode a list of preprocessor definitions for the compiler
155    * command line.
156    */
157   void AppendDefines(std::string& defines, const char* defines_list,
158                      const char* lang);
159
160   /** Lookup and append options associated with a particular feature.  */
161   void AppendFeatureOptions(std::string& flags, const char* lang,
162                             const char* feature);
163
164   /** \brief Get absolute path to dependency \a name
165    *
166    * Translate a dependency as given in CMake code to the name to
167    * appear in a generated build file.
168    * - If \a name is a utility target, returns false.
169    * - If \a name is a CMake target, it will be transformed to the real output
170    *   location of that target for the given configuration.
171    * - If \a name is the full path to a file, it will be returned.
172    * - Otherwise \a name is treated as a relative path with respect to
173    *   the source directory of this generator.  This should only be
174    *   used for dependencies of custom commands.
175    */
176   bool GetRealDependency(const char* name, const char* config,
177                          std::string& dep);
178
179   ///! for existing files convert to output path and short path if spaces
180   std::string ConvertToOutputForExisting(const char* remote,
181                                          RelativeRoot local = START_OUTPUT);
182
183   /** For existing path identified by RelativeRoot convert to output
184       path and short path if spaces.  */
185   std::string ConvertToOutputForExisting(RelativeRoot remote,
186                                          const char* local = 0);
187
188   virtual std::string ConvertToIncludeReference(std::string const& path);
189
190   /** Called from command-line hook to clear dependencies.  */
191   virtual void ClearDependencies(cmMakefile* /* mf */,
192                                  bool /* verbose */) {}
193
194   /** Called from command-line hook to update dependencies.  */
195   virtual bool UpdateDependencies(const char* /* tgtInfo */,
196                                   bool /*verbose*/,
197                                   bool /*color*/)
198     { return true; }
199
200   /** Get the include flags for the current makefile and language.  */
201   void GetIncludeDirectories(std::vector<std::string>& dirs,
202                              cmTarget* target,
203                              const char* lang = "C");
204
205   /** Compute the language used to compile the given source file.  */
206   const char* GetSourceFileLanguage(const cmSourceFile& source);
207
208   // Fill the vector with the target names for the object files,
209   // preprocessed files and assembly files.
210   virtual void GetIndividualFileTargets(std::vector<std::string>&) {}
211
212   // Create a struct to hold the varibles passed into
213   // ExpandRuleVariables
214   struct RuleVariables
215   {
216     RuleVariables()
217       {
218         memset(this, 0,  sizeof(*this));
219       }
220     cmTarget* CMTarget;
221     const char* TargetPDB;
222     const char* TargetVersionMajor;
223     const char* TargetVersionMinor;
224     const char* Language;
225     const char* Objects;
226     const char* Target;
227     const char* LinkLibraries;
228     const char* Source;
229     const char* AssemblySource;
230     const char* PreprocessedSource;
231     const char* Output;
232     const char* Object;
233     const char* ObjectDir;
234     const char* Flags;
235     const char* ObjectsQuoted;
236     const char* SONameFlag;
237     const char* TargetSOName;
238     const char* TargetInstallNameDir;
239     const char* LinkFlags;
240     const char* LanguageCompileFlags;
241     const char* Defines;
242     const char* RuleLauncher;
243     const char* DependencyFile;
244   };
245
246   /** Set whether to treat conversions to SHELL as a link script shell.  */
247   void SetLinkScriptShell(bool b) { this->LinkScriptShell = b; }
248
249   /** Escape the given string to be used as a command line argument in
250       the native build system shell.  Optionally allow the build
251       system to replace make variable references.  Optionally adjust
252       escapes for the special case of passing to the native echo
253       command.  */
254   std::string EscapeForShell(const char* str, bool makeVars = false,
255                              bool forEcho = false);
256
257   /** Backwards-compatibility version of EscapeForShell.  */
258   std::string EscapeForShellOldStyle(const char* str);
259
260   /** Escape the given string as an argument in a CMake script.  */
261   static std::string EscapeForCMake(const char* str);
262
263   enum FortranFormat
264     {
265     FortranFormatNone,
266     FortranFormatFixed,
267     FortranFormatFree
268     };
269   FortranFormat GetFortranFormat(const char* value);
270
271   /**
272    * Convert the given remote path to a relative path with respect to
273    * the given local path.  The local path must be given in component
274    * form (see SystemTools::SplitPath) without a trailing slash.  The
275    * remote path must use forward slashes and not already be escaped
276    * or quoted.
277    */
278   std::string ConvertToRelativePath(const std::vector<std::string>& local,
279                                     const char* remote, bool force=false);
280
281   /**
282    * Get the relative path from the generator output directory to a
283    * per-target support directory.
284    */
285   virtual std::string GetTargetDirectory(cmTarget const& target) const;
286
287   /**
288    * Get the level of backwards compatibility requested by the project
289    * in this directory.  This is the value of the CMake variable
290    * CMAKE_BACKWARDS_COMPATIBILITY whose format is
291    * "major.minor[.patch]".  The returned integer is encoded as
292    *
293    *   CMake_VERSION_ENCODE(major, minor, patch)
294    *
295    * and is monotonically increasing with the CMake version.
296    */
297   unsigned int GetBackwardsCompatibility();
298
299   /**
300    * Test whether compatibility is set to a given version or lower.
301    */
302   bool NeedBackwardsCompatibility(unsigned int major,
303                                   unsigned int minor,
304                                   unsigned int patch = 0xFFu);
305
306   /**
307    * Generate a Mac OS X application bundle Info.plist file.
308    */
309   void GenerateAppleInfoPList(cmTarget* target, const char* targetName,
310                               const char* fname);
311
312   /**
313    * Generate a Mac OS X framework Info.plist file.
314    */
315   void GenerateFrameworkInfoPList(cmTarget* target,
316                                   const char* targetName,
317                                   const char* fname);
318   /** Construct a comment for a custom command.  */
319   std::string ConstructComment(const cmCustomCommand& cc,
320                                const char* default_comment = "");
321   // Compute object file names.
322   std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
323                                              std::string const& dir_max,
324                                              bool* hasSourceExtension = 0);
325
326   /** Fill out these strings for the given target.  Libraries to link,
327    *  flags, and linkflags. */
328   void GetTargetFlags(std::string& linkLibs,
329                       std::string& flags,
330                       std::string& linkFlags,
331                       cmTarget&target);
332
333 protected:
334   ///! put all the libraries for a target on into the given stream
335   virtual void OutputLinkLibraries(std::ostream&, cmTarget&, bool relink);
336
337   // Expand rule variables in CMake of the type found in language rules
338   void ExpandRuleVariables(std::string& string,
339                            const RuleVariables& replaceValues);
340   // Expand rule variables in a single string
341   std::string ExpandRuleVariable(std::string const& variable,
342                                  const RuleVariables& replaceValues);
343
344   const char* GetRuleLauncher(cmTarget* target, const char* prop);
345   void InsertRuleLauncher(std::string& s, cmTarget* target,
346                           const char* prop);
347
348
349   /** Convert a target to a utility target for unsupported
350    *  languages of a generator */
351   void AddBuildTargetRule(const char* llang, cmTarget& target);
352   ///! add a custom command to build a .o file that is part of a target
353   void AddCustomCommandToCreateObject(const char* ofname,
354                                       const char* lang,
355                                       cmSourceFile& source,
356                                       cmTarget& target);
357   // Create Custom Targets and commands for unsupported languages
358   // The set passed in should contain the languages supported by the
359   // generator directly.  Any targets containing files that are not
360   // of the types listed will be compiled as custom commands and added
361   // to a custom target.
362   void CreateCustomTargetsAndCommands(std::set<cmStdString> const&);
363
364   // Handle old-style install rules stored in the targets.
365   void GenerateTargetInstallRules(
366     std::ostream& os, const char* config,
367     std::vector<std::string> const& configurationTypes);
368
369   std::string& CreateSafeUniqueObjectFileName(const char* sin,
370                                               std::string const& dir_max);
371   void ComputeObjectMaxPath();
372
373   void ConfigureRelativePaths();
374   std::string FindRelativePathTopSource();
375   std::string FindRelativePathTopBinary();
376   void SetupPathConversions();
377
378   virtual std::string ConvertToLinkReference(std::string const& lib);
379
380   /** Check whether the native build system supports the given
381       definition.  Issues a warning.  */
382   virtual bool CheckDefinition(std::string const& define) const;
383
384   /** Read the input CMakeLists.txt file.  */
385   void ReadInputFile();
386
387   cmMakefile *Makefile;
388   cmGlobalGenerator *GlobalGenerator;
389   // members used for relative path function ConvertToMakefilePath
390   std::string RelativePathToSourceDir;
391   std::string RelativePathToBinaryDir;
392   std::vector<std::string> HomeDirectoryComponents;
393   std::vector<std::string> StartDirectoryComponents;
394   std::vector<std::string> HomeOutputDirectoryComponents;
395   std::vector<std::string> StartOutputDirectoryComponents;
396   cmLocalGenerator* Parent;
397   std::vector<cmLocalGenerator*> Children;
398   std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
399   std::string::size_type ObjectPathMax;
400   std::set<cmStdString> ObjectMaxPathViolations;
401   bool WindowsShell;
402   bool WindowsVSIDE;
403   bool WatcomWMake;
404   bool MinGWMake;
405   bool NMake;
406   bool ForceUnixPath;
407   bool MSYSShell;
408   bool LinkScriptShell;
409   bool UseRelativePaths;
410   bool IgnoreLibPrefix;
411   bool Configured;
412   bool EmitUniversalBinaryFlags;
413   // A type flag is not nice. It's used only in TraceDependencies().
414   bool IsMakefileGenerator;
415   // Hack for ExpandRuleVariable until object-oriented version is
416   // committed.
417   std::string TargetImplib;
418
419   // The top-most directories for relative path conversion.  Both the
420   // source and destination location of a relative path conversion
421   // must be underneath one of these directories (both under source or
422   // both under binary) in order for the relative path to be evaluated
423   // safely by the build tools.
424   std::string RelativePathTopSource;
425   std::string RelativePathTopBinary;
426   bool RelativePathsConfigured;
427   bool PathConversionsSetup;
428
429   unsigned int BackwardsCompatibility;
430   bool BackwardsCompatibilityFinal;
431 private:
432   std::string ConvertToOutputForExistingCommon(const char* remote,
433                                                std::string const& result);
434
435   void AddSharedFlags(std::string& flags, const char* lang, bool shared);
436   bool GetShouldUseOldFlags(bool shared, const std::string &lang) const;
437   void AddPositionIndependentFlags(std::string& flags, std::string const& l,
438                                    int targetType);
439 };
440
441 #endif