cmLocalGenerator.h 13.8 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1
2
/*=========================================================================

3
  Program:   CMake - Cross-Platform Makefile Generator
Ken Martin's avatar
Ken Martin committed
4
5
6
7
8
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

9
10
  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
Ken Martin's avatar
Ken Martin committed
11
12
13
14
15
16
17
18
19
20
21
22
23

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef cmLocalGenerator_h
#define cmLocalGenerator_h

#include "cmStandardIncludes.h"

class cmMakefile;
class cmGlobalGenerator;
24
class cmTarget;
25
class cmTargetManifest;
26
class cmSourceFile;
27
class cmCustomCommand;
Ken Martin's avatar
Ken Martin committed
28
29
30
31
32
33
34
35
36
37
38
39

/** \class cmLocalGenerator
 * \brief Create required build files for a directory.
 *
 * Subclasses of this abstract class generate makefiles, DSP, etc for various
 * platforms. This class should never be constructued directly. A
 * GlobalGenerator will create it and invoke the appropriate commands on it.
 */
class cmLocalGenerator
{
public:
  cmLocalGenerator();
Ken Martin's avatar
Ken Martin committed
40
  virtual ~cmLocalGenerator();
Ken Martin's avatar
Ken Martin committed
41
42
  
  /**
43
   * Generate the makefile for this directory. 
Ken Martin's avatar
Ken Martin committed
44
   */
Alexander Neundorf's avatar
   
Alexander Neundorf committed
45
  virtual void Generate() {}
Ken Martin's avatar
Ken Martin committed
46
47
48

  /**
   * Process the CMakeLists files for this directory to fill in the
Ken Martin's avatar
Ken Martin committed
49
   * Makefile ivar 
Ken Martin's avatar
Ken Martin committed
50
51
52
   */
  virtual void Configure();

Alexander Neundorf's avatar
   
Alexander Neundorf committed
53
54
55
56
  /** 
   * Calls TraceVSDependencies() on all targets of this generator.
   */
  virtual void TraceDependencies();
Alexander Neundorf's avatar
   
Alexander Neundorf committed
57
58
59

  virtual void AddHelperCommands() {}

Ken Martin's avatar
Ken Martin committed
60
61
62
63
64
  /**
   * Perform any final calculations prior to generation
   */
  virtual void ConfigureFinalPass();

65
66
67
68
69
  /**
   * Generate the install rules files in this directory.
   */
  virtual void GenerateInstallRules();

70
71
72
73
  /**
   * Generate the test files for tests.
   */
  virtual void GenerateTestFiles();
74
75
76
77
78

  /**
   * Generate a manifest of target files that will be built.
   */
  virtual void GenerateTargetManifest(cmTargetManifest&);
79

Ken Martin's avatar
Ken Martin committed
80
81
  ///! Get the makefile for this generator
  cmMakefile *GetMakefile() {
Ken Martin's avatar
Ken Martin committed
82
    return this->Makefile; };
Ken Martin's avatar
Ken Martin committed
83
  
Alexander Neundorf's avatar
   
Alexander Neundorf committed
84
85
86
87
  ///! Get the makefile for this generator, const version
    const cmMakefile *GetMakefile() const {
      return this->Makefile; };
  
Ken Martin's avatar
Ken Martin committed
88
89
  ///! Get the GlobalGenerator this is associated with
  cmGlobalGenerator *GetGlobalGenerator() {
Ken Martin's avatar
Ken Martin committed
90
    return this->GlobalGenerator; };
Ken Martin's avatar
Ken Martin committed
91
92

  ///! Set the Global Generator, done on creation by the GlobalGenerator
Ken Martin's avatar
updates    
Ken Martin committed
93
  void SetGlobalGenerator(cmGlobalGenerator *gg);
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  /** 
   * Convert something to something else. This is a centralized coversion
   * routine used by the generators to handle relative paths and the like.
   * The flags determine what is actually done. 
   *
   * relative: treat the argument as a directory and convert it to make it
   * relative or full or unchanged. If relative (HOME, START etc) then that
   * specifies what it should be relative to.
   *
   * output: make the result suitable for output to a...
   *
   * optional: should any relative path operation be controlled by the rel
   * path setting
   */
  enum RelativeRoot { NONE, FULL, HOME, START, HOME_OUTPUT, START_OUTPUT };
  enum OutputFormat { UNCHANGED, MAKEFILE, SHELL };
  std::string Convert(const char* source, 
                      RelativeRoot relative, 
                      OutputFormat output = UNCHANGED,
                      bool optional = false);
  
116
117
118
119
120
121
122
123
  /**
   * Convert the given path to an output path that is optionally
   * relative based on the cache option CMAKE_USE_RELATIVE_PATHS.  The
   * remote path must use forward slashes and not already be escaped
   * or quoted.
   */
  std::string ConvertToOptionallyRelativeOutputPath(const char* remote);

124
  ///! set/get the parent generator 
Ken Martin's avatar
Ken Martin committed
125
126
  cmLocalGenerator* GetParent(){return this->Parent;}
  void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
127
128
129
130
131

  ///! set/get the children
  void AddChild(cmLocalGenerator* g) { this->Children.push_back(g); }
  std::vector<cmLocalGenerator*>& GetChildren() { return this->Children; };
    
132

133
134
  void AddLanguageFlags(std::string& flags, const char* lang,
                        const char* config);
135
  void AddSharedFlags(std::string& flags, const char* lang, bool shared);
136
137
  void AddConfigVariableFlags(std::string& flags, const char* var,
                              const char* config);
138
  virtual void AppendFlags(std::string& flags, const char* newFlags);
139
140
  ///! Get the include flags for the current makefile and language
  const char* GetIncludeFlags(const char* lang); 
141

142
143
144
  /** Translate a dependency as given in CMake code to the name to
      appear in a generated build file.  If the given name is that of
      a CMake target it will be transformed to the real output
145
146
147
148
149
150
      location of that target for the given configuration.  If the
      given name is the full path to a file it will be returned.
      Otherwise the name is treated as a relative path with respect to
      the source directory of this generator.  This should only be
      used for dependencies of custom commands.  */
  std::string GetRealDependency(const char* name, const char* config);
Alexander Neundorf's avatar
   
Alexander Neundorf committed
151
152
153
154
155
  
  /** Translate a command as given in CMake code to the location of the 
      executable if the command is the name of a CMake executable target.
      If that's not the case, just return the original name. */
  std::string GetRealLocation(const char* inName, const char* config);
156

Bill Hoffman's avatar
Bill Hoffman committed
157
158
159
  ///! for existing files convert to output path and short path if spaces
  std::string ConvertToOutputForExisting(const char* p);
  
160
161
162
  /** Called from command-line hook to clear dependencies.  */
  virtual void ClearDependencies(cmMakefile* /* mf */, 
                                 bool /* verbose */) {}
163
  
164
165
  /** Called from command-line hook to update dependencies.  */
  virtual bool UpdateDependencies(const char* /* tgtInfo */,
166
167
                                  bool /*verbose*/,
                                  bool /*color*/)
168
    { return true; }
Ken Martin's avatar
Ken Martin committed
169

170
171
172
173
174
175
176
  /** Compute the list of link libraries and directories for the given
      target and configuration.  */
  void ComputeLinkInformation(cmTarget& target, const char* config,
                              std::vector<cmStdString>& outLibs,
                              std::vector<cmStdString>& outDirs,
                              std::vector<cmStdString>* fullPathLibs=0);

177
  /** Get the include flags for the current makefile and language.  */
178
179
  void GetIncludeDirectories(std::vector<std::string>& dirs,
                             bool filter_system_dirs = true);
180

181
182
183
  /** Compute the language used to compile the given source file.  */
  const char* GetSourceFileLanguage(const cmSourceFile& source);

184
185
186
187
188
189
  // Create a struct to hold the varibles passed into
  // ExpandRuleVariables
  struct RuleVariables
  {
    RuleVariables()
      {
190
        memset(this, 0,  sizeof(*this));
191
      }
192
    const char* TargetPDB;
193
194
    const char* TargetVersionMajor;
    const char* TargetVersionMinor;
195
196
197
198
199
    const char* Language;
    const char* Objects;
    const char* Target;
    const char* LinkLibraries;
    const char* Source;
200
201
    const char* AssemblySource;
    const char* PreprocessedSource;
202
    const char* Object;
203
    const char* ObjectDir;
204
205
206
    const char* Flags;
    const char* ObjectsQuoted;
    const char* TargetSOName;
207
    const char* TargetInstallNameDir;
208
    const char* LinkFlags;
209
    const char* LanguageCompileFlags;
210
  };
Bill Hoffman's avatar
Bill Hoffman committed
211

212
  /** Escape the given string to be used as a command line argument in
213
      the native build system shell.  Optionally allow the build
214
215
216
217
218
      system to replace make variable references.  Optionally adjust
      escapes for the special case of passing to the native echo
      command.  */
  std::string EscapeForShell(const char* str, bool makeVars = false,
                             bool forEcho = false);
219
220
221

  /** Backwards-compatibility version of EscapeForShell.  */
  std::string EscapeForShellOldStyle(const char* str);
222
223
224
225
226
227
228
229
230
  
  /** Return the directories into which object files will be put.
   *  There maybe more than one for fat binary systems like OSX.
   */
  virtual void 
  GetTargetObjectFileDirectories(cmTarget* target,
                                 std::vector<std::string>& 
                                 dirs);
  
231
232
233
234
235
236
237
238
239
240
  /**
   * Convert the given remote path to a relative path with respect to
   * the given local path.  The local path must be given in component
   * form (see SystemTools::SplitPath) without a trailing slash.  The
   * remote path must use forward slashes and not already be escaped
   * or quoted.
   */
  std::string ConvertToRelativePath(const std::vector<std::string>& local,
                                    const char* remote);

241
242
243
244
245
246
  /**
   * Get the relative path from the generator output directory to a
   * per-target support directory.
   */
  virtual std::string GetTargetDirectory(cmTarget const& target) const;

247
248
249
  ///! Determine the arguments for the linker call, used also by 
  /// cmInstallTargetGenerator
  bool GetLinkerArgs(std::string& rpath, std::string& linkLibs,
Alexander Neundorf's avatar
   
Alexander Neundorf committed
250
                     cmTarget& tgt, bool relink, unsigned int minRpathSize);
251
252
253
  
  bool IsChrpathAvailable(const cmTarget& target);

254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
  /**
   * Get the level of backwards compatibility requested by the project
   * in this directory.  This is the value of the CMake variable
   * CMAKE_BACKWARDS_COMPATIBILITY whose format is
   * "major.minor[.patch]".  The returned integer is encoded as
   *
   *   CMake_VERSION_ENCODE(major, minor, patch)
   *
   * and is monotonically increasing with the CMake version.
   */
  unsigned int GetBackwardsCompatibility();

  /**
   * Test whether compatibility is set to a given version or lower.
   */
  bool NeedBackwardsCompatibility(unsigned int major,
                                  unsigned int minor,
                                  unsigned int patch = 0xFFu);
272
protected:
273
  void FixDefineFlags(std::string& defineFlags, const char* lang);
274
275
276
277
  /** Construct a comment for a custom command.  */
  std::string ConstructComment(const cmCustomCommand& cc,
                               const char* default_comment = "");

Bill Hoffman's avatar
Bill Hoffman committed
278
279
  /** Fill out these strings for the given target.  Libraries to link,
   *  flags, and linkflags. */
Bill Hoffman's avatar
Bill Hoffman committed
280
281
282
283
284
285
286
  void GetTargetFlags(std::string& linkLibs, 
                      std::string& flags,
                      std::string& linkFlags,
                      cmTarget&target);
  
  ///! put all the libraries for a target on into the given stream
  virtual void OutputLinkLibraries(std::ostream&, cmTarget&, bool relink);
287
  
288
289
  // Expand rule variables in CMake of the type found in language rules
  void ExpandRuleVariables(std::string& string,
290
                           const RuleVariables& replaceValues);
Bill Hoffman's avatar
Bill Hoffman committed
291
292
  // Expand rule variables in a single string
  std::string ExpandRuleVariable(std::string const& variable,
293
                                 const RuleVariables& replaceValues);
Bill Hoffman's avatar
Bill Hoffman committed
294
  
Bill Hoffman's avatar
Bill Hoffman committed
295
296
  /** Convert a target to a utility target for unsupported 
   *  languages of a generator */
297
298
299
300
301
302
303
304
305
306
307
308
  void AddBuildTargetRule(const char* llang, cmTarget& target);
  ///! add a custom command to build a .o file that is part of a target 
  void AddCustomCommandToCreateObject(const char* ofname, 
                                      const char* lang, 
                                      cmSourceFile& source,
                                      cmTarget& target);
  // Create Custom Targets and commands for unsupported languages
  // The set passed in should contain the languages supported by the
  // generator directly.  Any targets containing files that are not
  // of the types listed will be compiled as custom commands and added
  // to a custom target.
  void CreateCustomTargetsAndCommands(std::set<cmStdString> const&);
309
310
311
312
313

  // Handle old-style install rules stored in the targets.
  void GenerateTargetInstallRules(
    std::ostream& os, const char* config,
    std::vector<std::string> const& configurationTypes);
314

315
  // Compute object file names.
316
  std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
317
318
                                             std::string::size_type dir_len,
                                             bool* hasSourceExtension = 0);
319
320
  std::string& CreateSafeUniqueObjectFileName(const char* sin,
                                              std::string::size_type dir_len);
321

322
  void ConfigureRelativePaths();
323
324
  std::string FindRelativePathTopSource();
  std::string FindRelativePathTopBinary();
325
  void SetupPathConversions();
326

Ken Martin's avatar
Ken Martin committed
327
328
  cmMakefile *Makefile;
  cmGlobalGenerator *GlobalGenerator;
329
  // members used for relative path function ConvertToMakefilePath
Ken Martin's avatar
Ken Martin committed
330
331
332
333
334
335
336
  std::string RelativePathToSourceDir;
  std::string RelativePathToBinaryDir;
  std::vector<std::string> HomeDirectoryComponents;
  std::vector<std::string> StartDirectoryComponents;
  std::vector<std::string> HomeOutputDirectoryComponents;
  std::vector<std::string> StartOutputDirectoryComponents;
  cmLocalGenerator* Parent;
337
  std::vector<cmLocalGenerator*> Children;
Ken Martin's avatar
Ken Martin committed
338
  std::map<cmStdString, cmStdString> LanguageToIncludeFlags;
339
  std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
Ken Martin's avatar
Ken Martin committed
340
  bool WindowsShell;
341
  bool WindowsVSIDE;
342
  bool WatcomWMake;
343
  bool MinGWMake;
344
  bool NMake;
Ken Martin's avatar
Ken Martin committed
345
  bool ForceUnixPath;
346
  bool MSYSShell;
Ken Martin's avatar
Ken Martin committed
347
348
  bool UseRelativePaths;
  bool IgnoreLibPrefix;
349
  bool Configured;
350
  bool EmitUniversalBinaryFlags;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
351
352
  // A type flag is not nice. It's used only in TraceDependencies().
  bool IsMakefileGenerator;
353
354
  // Hack for ExpandRuleVariable until object-oriented version is
  // committed.
Ken Martin's avatar
Ken Martin committed
355
  std::string TargetImplib;
356
357
358
359
360
361
362
363
364

  // The top-most directories for relative path conversion.  Both the
  // source and destination location of a relative path conversion
  // must be underneath one of these directories (both under source or
  // both under binary) in order for the relative path to be evaluated
  // safely by the build tools.
  std::string RelativePathTopSource;
  std::string RelativePathTopBinary;
  bool RelativePathsConfigured;
365
  bool PathConversionsSetup;
366
367
368

  unsigned int BackwardsCompatibility;
  bool BackwardsCompatibilityFinal;
Ken Martin's avatar
Ken Martin committed
369
370
371
};

#endif