Updates will be applied on October 27th between 12pm - 12:45pm EDT (UTC-0400). Gitlab may be slow during the maintenance window.

cmLocalGenerator.h 15.4 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

  /**
   * Generate a manifest of target files that will be built.
   */
78
  virtual void GenerateTargetManifest();
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
  /** 
   * 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 };
111
112
  std::string ConvertToOutputFormat(const char* source, OutputFormat output);
  std::string Convert(const char* remote, RelativeRoot local,
113
114
                      OutputFormat output = UNCHANGED,
                      bool optional = false);
115
116
117
118
119
120
121
122
  std::string Convert(RelativeRoot remote, const char* local,
                      OutputFormat output = UNCHANGED,
                      bool optional = false);

  /**
    * Get path for the specified relative root.
    */
  const char* GetRelativeRootPath(RelativeRoot relroot);
123
  
124
125
126
127
128
129
130
131
  /**
   * 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);

132
  ///! set/get the parent generator 
Ken Martin's avatar
Ken Martin committed
133
134
  cmLocalGenerator* GetParent(){return this->Parent;}
  void SetParent(cmLocalGenerator* g) { this->Parent = g; g->AddChild(this); }
135
136
137
138
139

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

141
142
  void AddLanguageFlags(std::string& flags, const char* lang,
                        const char* config);
143
  void AddSharedFlags(std::string& flags, const char* lang, bool shared);
144
145
  void AddConfigVariableFlags(std::string& flags, const char* var,
                              const char* config);
146
  virtual void AppendFlags(std::string& flags, const char* newFlags);
147
148
  ///! Get the include flags for the current makefile and language
  const char* GetIncludeFlags(const char* lang); 
149

150
151
152
153
  /**
   * Encode a list of preprocessor definitions for the compiler
   * command line.
   */
154
155
  void AppendDefines(std::string& defines, const char* defines_list,
                     const char* lang);
156

157
158
159
  /** 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
160
161
162
163
164
165
      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
166
167
168
169
170
  
  /** 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);
171

Bill Hoffman's avatar
Bill Hoffman committed
172
  ///! for existing files convert to output path and short path if spaces
173
174
175
176
177
178
179
  std::string ConvertToOutputForExisting(const char* remote,
                                         RelativeRoot local = START_OUTPUT);

  /** For existing path identified by RelativeRoot convert to output
      path and short path if spaces.  */
  std::string ConvertToOutputForExisting(RelativeRoot remote,
                                         const char* local = 0);
Bill Hoffman's avatar
Bill Hoffman committed
180
  
181
182
183
  /** Called from command-line hook to clear dependencies.  */
  virtual void ClearDependencies(cmMakefile* /* mf */, 
                                 bool /* verbose */) {}
184
  
185
186
  /** Called from command-line hook to update dependencies.  */
  virtual bool UpdateDependencies(const char* /* tgtInfo */,
187
188
                                  bool /*verbose*/,
                                  bool /*color*/)
189
    { return true; }
Ken Martin's avatar
Ken Martin committed
190

191
  /** Get the include flags for the current makefile and language.  */
192
193
  void GetIncludeDirectories(std::vector<std::string>& dirs,
                             const char* lang = "C");
194

195
196
197
  /** Compute the language used to compile the given source file.  */
  const char* GetSourceFileLanguage(const cmSourceFile& source);

198
199
200
201
202
203
  // Create a struct to hold the varibles passed into
  // ExpandRuleVariables
  struct RuleVariables
  {
    RuleVariables()
      {
204
        memset(this, 0,  sizeof(*this));
205
      }
206
    cmTarget* CMTarget;
207
    const char* TargetPDB;
208
209
    const char* TargetVersionMajor;
    const char* TargetVersionMinor;
210
211
212
213
214
    const char* Language;
    const char* Objects;
    const char* Target;
    const char* LinkLibraries;
    const char* Source;
215
216
    const char* AssemblySource;
    const char* PreprocessedSource;
217
    const char* Output;
218
    const char* Object;
219
    const char* ObjectDir;
220
221
222
    const char* Flags;
    const char* ObjectsQuoted;
    const char* TargetSOName;
223
    const char* TargetInstallNameDir;
224
    const char* LinkFlags;
225
    const char* LanguageCompileFlags;
226
    const char* Defines;
227
    const char* RuleLauncher;
228
  };
Bill Hoffman's avatar
Bill Hoffman committed
229

230
231
232
  /** Set whether to treat conversions to SHELL as a link script shell.  */
  void SetLinkScriptShell(bool b) { this->LinkScriptShell = b; }

233
  /** Escape the given string to be used as a command line argument in
234
      the native build system shell.  Optionally allow the build
235
236
237
238
239
      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);
240
241
242

  /** Backwards-compatibility version of EscapeForShell.  */
  std::string EscapeForShellOldStyle(const char* str);
243
244
245
246

  /** Escape the given string as an argument in a CMake script.  */
  std::string EscapeForCMake(const char* str);

247
248
249
250
251
252
253
254
  /** 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);
  
255
256
257
258
259
260
261
262
263
264
  /**
   * 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);

265
266
267
268
269
270
  /**
   * Get the relative path from the generator output directory to a
   * per-target support directory.
   */
  virtual std::string GetTargetDirectory(cmTarget const& target) const;

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  /**
   * 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);
289
290
291
292
293
294

  /**
   * Generate a Mac OS X application bundle Info.plist file.
   */
  void GenerateAppleInfoPList(cmTarget* target, const char* targetName,
                              const char* fname);
295
296
297
298
299
300
301

  /**
   * Generate a Mac OS X framework Info.plist file.
   */
  void GenerateFrameworkInfoPList(cmTarget* target,
                                  const char* targetName,
                                  const char* fname);
302
303
304
  /** Construct a comment for a custom command.  */
  std::string ConstructComment(const cmCustomCommand& cc,
                               const char* default_comment = "");
305
306
307
308
  // Compute object file names.
  std::string GetObjectFileNameWithoutTarget(const cmSourceFile& source,
                                             std::string const& dir_max,
                                             bool* hasSourceExtension = 0);
309

310
protected:
Bill Hoffman's avatar
Bill Hoffman committed
311
312
  /** Fill out these strings for the given target.  Libraries to link,
   *  flags, and linkflags. */
Bill Hoffman's avatar
Bill Hoffman committed
313
314
315
316
317
318
319
  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);
320
  
321
322
  // Expand rule variables in CMake of the type found in language rules
  void ExpandRuleVariables(std::string& string,
323
                           const RuleVariables& replaceValues);
Bill Hoffman's avatar
Bill Hoffman committed
324
325
  // Expand rule variables in a single string
  std::string ExpandRuleVariable(std::string const& variable,
326
                                 const RuleVariables& replaceValues);
327
328
329
330
331

  const char* GetRuleLauncher(cmTarget* target, const char* prop);
  void InsertRuleLauncher(std::string& s, cmTarget* target,
                          const char* prop);

Bill Hoffman's avatar
Bill Hoffman committed
332
  
Bill Hoffman's avatar
Bill Hoffman committed
333
334
  /** Convert a target to a utility target for unsupported 
   *  languages of a generator */
335
336
337
338
339
340
341
342
343
344
345
346
  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&);
347
348
349
350
351

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

353
  std::string& CreateSafeUniqueObjectFileName(const char* sin,
354
                                              std::string const& dir_max);
355
  void ComputeObjectMaxPath();
356

357
  void ConfigureRelativePaths();
358
359
  std::string FindRelativePathTopSource();
  std::string FindRelativePathTopBinary();
360
  void SetupPathConversions();
361

362
363
  std::string ConvertToLinkReference(std::string const& lib);

364
365
366
367
  /** Check whether the native build system supports the given
      definition.  Issues a warning.  */
  virtual bool CheckDefinition(std::string const& define) const;

368
369
370
  /** Read the input CMakeLists.txt file.  */
  void ReadInputFile();

Ken Martin's avatar
Ken Martin committed
371
372
  cmMakefile *Makefile;
  cmGlobalGenerator *GlobalGenerator;
373
  // members used for relative path function ConvertToMakefilePath
Ken Martin's avatar
Ken Martin committed
374
375
376
377
378
379
380
  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;
381
  std::vector<cmLocalGenerator*> Children;
Ken Martin's avatar
Ken Martin committed
382
  std::map<cmStdString, cmStdString> LanguageToIncludeFlags;
383
  std::map<cmStdString, cmStdString> UniqueObjectNamesMap;
384
  std::string::size_type ObjectPathMax;
385
  std::set<cmStdString> ObjectMaxPathViolations;
Ken Martin's avatar
Ken Martin committed
386
  bool WindowsShell;
387
  bool WindowsVSIDE;
388
  bool WatcomWMake;
389
  bool MinGWMake;
390
  bool NMake;
Ken Martin's avatar
Ken Martin committed
391
  bool ForceUnixPath;
392
  bool MSYSShell;
393
  bool LinkScriptShell;
Ken Martin's avatar
Ken Martin committed
394
395
  bool UseRelativePaths;
  bool IgnoreLibPrefix;
396
  bool Configured;
397
  bool EmitUniversalBinaryFlags;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
398
399
  // A type flag is not nice. It's used only in TraceDependencies().
  bool IsMakefileGenerator;
400
401
  // Hack for ExpandRuleVariable until object-oriented version is
  // committed.
Ken Martin's avatar
Ken Martin committed
402
  std::string TargetImplib;
403
404
405
406
407
408
409
410
411

  // 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;
412
  bool PathConversionsSetup;
413
414
415

  unsigned int BackwardsCompatibility;
  bool BackwardsCompatibilityFinal;
416
417
418
private:
  std::string ConvertToOutputForExistingCommon(const char* remote,
                                               std::string const& result);
Ken Martin's avatar
Ken Martin committed
419
420
421
};

#endif