cmMakefile.h 11.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile$
  Language:  C++
  Date:      $Date$
  Version:   $Revision$


  Copyright (c) 2000 National Library of Medicine
  All rights reserved.

  See COPYRIGHT.txt for copyright details.

=========================================================================*/
#ifndef cmMakefile_h
#define cmMakefile_h
18

19
#include "cmStandardIncludes.h"
20
#include "cmClassFile.h"
21
#include "cmSystemTools.h"
22

23
class cmCommand;
24
25
class cmMakefileGenerator;

26
27
28
29
/** \class cmMakefile
 * \brief Process the input CMakeLists.txt file.
 *
 * Process and store into memory the input CMakeLists.txt file.
30
 * Each CMakeLists.txt file is parsed and the commands found there
31
32
 * are added into the build process.
 */
33
34
35
class cmMakefile
{
public:
36
37
38
  /**
   * Construct an empty makefile.
   */
39
  cmMakefile();
40
41
42
43

  /**
   * Destructor.
   */
44
  ~cmMakefile();
45
46
47
48

  /**
   * Read and parse a CMakeLists.txt file.
   */
Ken Martin's avatar
Ken Martin committed
49
  bool ReadListFile(const char* listfile); 
50
51
52
53

  /**
   * Add a wrapper generator.
   */
54
  void AddCommand(cmCommand* );
55
56
57
58
59
60

  /**
   * Specify the makefile generator. This is platform/compiler
   * dependent, although the interface is through a generic
   * superclass.
   */
61
  void SetMakefileGenerator(cmMakefileGenerator*);
62
63
64
65

  /**
   * Produce the output makefile.
   */
66
67
  void GenerateMakefile();
  
68
69
70
  /**
   * Print the object state to std::cout.
   */
71
  void Print();
72
  
73
  /**
74
   * Add a custom command to the build.
75
   */
76
  void AddCustomCommand(const char* source,
77
78
79
                     const char* result,
                     const char* command,
                     std::vector<std::string>& depends);
80
81
82
  /**
   * Add a define flag to the build.
   */
83
  void AddDefineFlag(const char* definition);
84
85
86
87

  /**
   * Add an executable to the build.
   */
88
  void AddExecutable(cmClassFile&);
89

90
91
92
93
94
95
96
97
98
99
  /**
   * Add a utility on which this project depends.
   */
  void AddUtility(const char*);

  /**
   * Add a directory in which a utility may be built.
   */
  void AddUtilityDirectory(const char*);

100
101
102
  /**
   * Add a link library to the build.
   */
103
  void AddLinkLibrary(const char*);
104
105
106
107

  /**
   * Add a link directory to the build.
   */
108
  void AddLinkDirectory(const char*);
109
110
111
112

  /**
   * Add a subdirectory to the build.
   */
113
  void AddSubDirectory(const char*);
114
115
116
117

  /**
   * Add an include directory to the build.
   */
118
  void AddIncludeDirectory(const char*);
119
120
121
122
123

  /**
   * Add a variable definition to the build. This variable
   * can be used in CMake to refer to lists, directories, etc.
   */
124
  void AddDefinition(const char* name, const char* value);
125
126
127
128

  /**
   * Specify the name of the project for this build.
   */
129
  void SetProjectName(const char*);
130
131
132
133
134
135
136
137
138
139
140
141

  /**
   * Get the name of the project for this build.
   */
  const char* GetProjectName()
    {
    return m_ProjectName.c_str();
    }
  
  /**
   * Set the name of the library.
   */
142
  void SetLibraryName(const char*);
143
144
145
146

  /**
   * Add a class/source file to the build.
   */
147
  void AddClass(cmClassFile& );
148
149
150
151

  /**
   * Add an auxiliary directory to the build.
   */
152
153
  void AddExtraDirectory(const char* dir);
  
Ken Martin's avatar
Ken Martin committed
154
155
156
157
158
159
160
161
162
163
164

  /**
   * Add an auxiliary directory to the build.
   */
  void MakeStartDirectoriesCurrent()
    {
      m_cmCurrentDirectory = m_cmStartDirectory;
      m_CurrentOutputDirectory = m_StartOutputDirectory;
    }
  
  //@{
165
  /**
Ken Martin's avatar
Ken Martin committed
166
167
168
169
170
   * Set/Get the home directory (or output directory) in the project. The
   * home directory is the top directory of the project. It is where
   * CMakeSetup or configure was run. Remember that CMake processes
   * CMakeLists files by recursing up the tree starting at the StartDirectory
   * and going up until it reaches the HomeDirectory.  
171
   */
172
173
  void SetHomeDirectory(const char* dir) 
    {
174
175
    m_cmHomeDirectory = dir;
    cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
176
    this->AddDefinition("CMAKE_SOURCE_DIR", this->GetHomeDirectory());
177
178
179
    }
  const char* GetHomeDirectory() 
    {
180
    return m_cmHomeDirectory.c_str();
181
    }
Ken Martin's avatar
Ken Martin committed
182
  void SetHomeOutputDirectory(const char* lib)
183
    {
Ken Martin's avatar
Ken Martin committed
184
185
    m_HomeOutputDirectory = lib;
    cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
186
    this->AddDefinition("CMAKE_BINARY_DIR", this->GetHomeOutputDirectory());
187
    }
Ken Martin's avatar
Ken Martin committed
188
  const char* GetHomeOutputDirectory()
189
    {
Ken Martin's avatar
Ken Martin committed
190
    return m_HomeOutputDirectory.c_str();
191
    }
Ken Martin's avatar
Ken Martin committed
192
193
194
  //@}
  
  //@{
195
  /**
Ken Martin's avatar
Ken Martin committed
196
197
198
199
200
   * Set/Get the start directory (or output directory). The start directory
   * is the directory of the CMakeLists.txt file that started the current
   * round of processing. Remember that CMake processes CMakeLists files by
   * recursing up the tree starting at the StartDirectory and going up until
   * it reaches the HomeDirectory.  
201
   */
Ken Martin's avatar
Ken Martin committed
202
  void SetStartDirectory(const char* dir) 
203
    {
Ken Martin's avatar
Ken Martin committed
204
205
      m_cmStartDirectory = dir;
      cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
206
    }
Ken Martin's avatar
Ken Martin committed
207
  const char* GetStartDirectory() 
208
    {
Ken Martin's avatar
Ken Martin committed
209
      return m_cmStartDirectory.c_str();
210
    }
Ken Martin's avatar
Ken Martin committed
211
212
213
214
215
216
  void SetStartOutputDirectory(const char* lib)
    {
      m_StartOutputDirectory = lib;
      cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
    }
  const char* GetStartOutputDirectory()
217
    {
Ken Martin's avatar
Ken Martin committed
218
      return m_StartOutputDirectory.c_str();
219
    }
Ken Martin's avatar
Ken Martin committed
220
  //@}
221

Ken Martin's avatar
Ken Martin committed
222
  //@{
223
  /**
Ken Martin's avatar
Ken Martin committed
224
225
226
227
228
   * Set/Get the current directory (or output directory) in the project. The
   * current directory is the directory of the CMakeLists.txt file that is
   * currently being processed. Remember that CMake processes CMakeLists
   * files by recursing up the tree starting at the StartDirectory and going
   * up until it reaches the HomeDirectory.  
229
   */
Ken Martin's avatar
Ken Martin committed
230
  void SetCurrentDirectory(const char* dir) 
231
    {
Ken Martin's avatar
Ken Martin committed
232
233
      m_cmCurrentDirectory = dir;
      cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
234
    }
Ken Martin's avatar
Ken Martin committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  const char* GetCurrentDirectory() 
    {
      return m_cmCurrentDirectory.c_str();
    }
  void SetCurrentOutputDirectory(const char* lib)
    {
      m_CurrentOutputDirectory = lib;
      cmSystemTools::ConvertToUnixSlashes(m_CurrentOutputDirectory);
    }
  const char* GetCurrentOutputDirectory()
    {
      return m_CurrentOutputDirectory.c_str();
    }
  //@}
249
250
251
252
253
254
255
256
257
258

  /** 
   * Set a regular expression that include files must match
   * in order to be considered as part of the depend information.
   */
  void SetIncludeRegularExpression(const char* regex)
    {
      m_IncludeFileRegularExpression = regex;
    }

259
  /**
Ken Martin's avatar
Ken Martin committed
260
   * Specify the name of the library that is built by this makefile.
261
   */
Ken Martin's avatar
Ken Martin committed
262
  const char* GetLibraryName()
263
    {
Ken Martin's avatar
Ken Martin committed
264
    return m_LibraryName.c_str();
265
    }
266
267
268
269

  /**
   * Get a list of the build subdirectories.
   */
270
271
  const std::vector<std::string>& GetSubDirectories()
    { 
272
    return m_SubDirectories;
273
    }
274
275
276
277
278

  /**
   * Return a boolean flag indicating whether the build generates
   * any executables.
   */
Ken Martin's avatar
Ken Martin committed
279
  bool HasExecutables();
280
281
282
283

  /**
   * Get a list of include directories in the build.
   */
284
285
  std::vector<std::string>& GetIncludeDirectories()
    { 
286
    return m_IncludeDirectories;
287
    }
288
289
290
291

  /**
   * Get a list of link directories in the build.
   */
292
293
  std::vector<std::string>& GetLinkDirectories()
    { 
294
    return m_LinkDirectories;
295
296
    }
  
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  /**
   * Get a list of utilities on which the project depends.
   */
  std::vector<std::string>& GetUtilities()
    { 
    return m_Utilities;
    }

  /**
   * Get a list of directories that may contain the Utilities.
   */
  std::vector<std::string>& GetUtilityDirectories()
    { 
    return m_UtilityDirectories;
    }

313
314
315
  /**
   * Get a list of link libraries in the build.
   */
316
317
  std::vector<std::string>& GetLinkLibraries()
    { 
318
    return m_LinkLibraries;
319
    }
320
321
322
323

  /**
   * Get a list of Win32 link libraries in the build.
   */
324
325
  std::vector<std::string>& GetLinkLibrariesWin32()
    { 
326
    return m_LinkLibrariesWin32;
327
328
    }
  
329
330
331
  /**
   * Get a list of Unix link libraries in the build.
   */
332
333
  std::vector<std::string>& GetLinkLibrariesUnix()
    { 
334
    return m_LinkLibrariesUnix;
335
    }
336
337
338
339
340
341
342
343
344
345

  /**
   * Return a list of source files in this makefile.
   */
  std::vector<cmClassFile>& GetClasses()
    {return  m_Classes;}

  /**
   * Obtain a list of auxiliary source directories.
   */
346
  std::vector<std::string>& GetAuxSourceDirectories()
347
    {return m_AuxSourceDirectories;}
348

349
350
351
352
353
  /**
   * Do not use this.
   */
  std::vector<std::string>& GetMakeVerbatim() 
    {return m_MakeVerbatim;}
354

355
356
  /**
   * Given a variable name, return its value (as a string).
357
   */
358
359
  const char* GetDefinition(const char*);

360
  /**
361
   * Get a list of preprocessor define flags.
362
   */
363
364
  const char* GetDefineFlags()
    {return m_DefineFlags.c_str();}
365
366
367
368
369
370
371
  
  /**
   * Get the vector of used command instances.
   */
  const std::vector<cmCommand*>& GetUsedCommands() const
    {return m_UsedCommands;}
  
372
373
374
375
376
377
  /**
   * Dump documentation to a file. If 0 is returned, the
   * operation failed.
   */
  int DumpDocumentationToFile(const char *fileName);

378
379
380
381
  /**
   * Expand all defined varibles in the string.  
   * Defined varibles come from the m_Definitions map.
   * They are expanded with ${var} where var is the
382
383
   * entry in the m_Definitions map.  Also @var@ is
   * expanded to match autoconf style expansions.
384
   */
385
386
387
388
389
390
391
  void ExpandVariablesInString(std::string& source);

  /**
   * Expand variables in the makefiles ivars such as link directories etc
   */
  void ExpandVariables();

392
393
394
395
396
397
398
399
400
401
402
403
  struct customCommand
  {
    std::string m_Source;
    std::string m_Result;
    std::string m_Command;
    std::vector<std::string> m_Depends;
  };

  std::vector<customCommand>& GetCustomCommands() {
    return m_CustomCommands; };
  
      
Bill Hoffman's avatar
Bill Hoffman committed
404
405
406
407
408
409
410
411
412
413
414
415
416
  /** Recursivly read and create a cmMakefile object for
   *  all CMakeLists.txt files in the GetSubDirectories list.
   *  Once the file is found, it ReadListFile is called on
   *  the cmMakefile created for it.
   */
  void FindSubDirectoryCMakeListsFiles(std::vector<cmMakefile*>& makefiles);
  
  /** Generate the cache file only.  This is done
   *  by calling FindSubDirectoryCMakeListsFiles which
   *  will cause all the rules to fire, and the cache to
   *  be filled.
   */
  void GenerateCacheOnly();
417

418
419
protected:
  std::string m_Prefix;
420
  std::vector<std::string> m_AuxSourceDirectories; // 
Ken Martin's avatar
Ken Martin committed
421
422
423
424
425
426
427
428

  std::string m_cmCurrentDirectory; 
  std::string m_CurrentOutputDirectory; 
  std::string m_cmStartDirectory; 
  std::string m_StartOutputDirectory; 
  std::string m_cmHomeDirectory; 
  std::string m_HomeOutputDirectory;

429
  std::string m_LibraryName;	// library name
430
  std::string m_ProjectName;	// project name
431
432
  std::vector<cmClassFile> m_Classes; // list of classes in makefile
  std::vector<std::string> m_SubDirectories; // list of sub directories
433
  std::vector<std::string> m_MakeVerbatim; // lines copied from input file
434
435
  std::vector<std::string> m_IncludeDirectories;
  std::vector<std::string> m_LinkDirectories;
436
437
  std::vector<std::string> m_Utilities;
  std::vector<std::string> m_UtilityDirectories;
438
439
440
  std::vector<std::string> m_LinkLibraries;
  std::vector<std::string> m_LinkLibrariesWin32;
  std::vector<std::string> m_LinkLibrariesUnix;
441
  std::string m_IncludeFileRegularExpression;
442
  std::string m_DefineFlags;
443
444
  std::vector<customCommand> m_CustomCommands;
  typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
445
446
  typedef std::map<std::string, std::string> DefinitionMap;
  DefinitionMap m_Definitions;
447
448
  RegisteredCommandsMap m_Commands;
  std::vector<cmCommand*> m_UsedCommands;
449
  cmMakefileGenerator* m_MakefileGenerator;
450
451
  
private:
Ken Martin's avatar
Ken Martin committed
452
453
454
  /**
   * Get the name of the parent directories CMakeLists file
   * given a current CMakeLists file name
455
   */
Ken Martin's avatar
Ken Martin committed
456
  std::string GetParentListFileName(const char *listFileName);
457
458
459
460
461

  void ReadClasses(std::ifstream& fin, bool t);
  friend class cmMakeDepend;	// make depend needs direct access 
				// to the m_Classes array 
  void PrintStringVector(const char* s, std::vector<std::string>& v);
462
  void AddDefaultCommands();
463
  void AddDefaultDefinitions();
464
465
466
467
};


#endif