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