cmMakefile.h 10.6 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

  /**
   * Add a link library to the build.
   */
93
  void AddLinkLibrary(const char*);
94
95
96
97

  /**
   * Add a link directory to the build.
   */
98
  void AddLinkDirectory(const char*);
99
100
101
102

  /**
   * Add a subdirectory to the build.
   */
103
  void AddSubDirectory(const char*);
104
105
106
107

  /**
   * Add an include directory to the build.
   */
108
  void AddIncludeDirectory(const char*);
109
110
111
112
113

  /**
   * Add a variable definition to the build. This variable
   * can be used in CMake to refer to lists, directories, etc.
   */
114
  void AddDefinition(const char* name, const char* value);
115
116
117
118

  /**
   * Specify the name of the project for this build.
   */
119
  void SetProjectName(const char*);
120
121
122
123
124
125
126
127
128
129
130
131

  /**
   * Get the name of the project for this build.
   */
  const char* GetProjectName()
    {
    return m_ProjectName.c_str();
    }
  
  /**
   * Set the name of the library.
   */
132
  void SetLibraryName(const char*);
133
134
135
136

  /**
   * Add a class/source file to the build.
   */
137
  void AddClass(cmClassFile& );
138
139
140
141

  /**
   * Add an auxiliary directory to the build.
   */
142
143
  void AddExtraDirectory(const char* dir);
  
Ken Martin's avatar
Ken Martin committed
144
145
146
147
148
149
150
151
152
153
154

  /**
   * Add an auxiliary directory to the build.
   */
  void MakeStartDirectoriesCurrent()
    {
      m_cmCurrentDirectory = m_cmStartDirectory;
      m_CurrentOutputDirectory = m_StartOutputDirectory;
    }
  
  //@{
155
  /**
Ken Martin's avatar
Ken Martin committed
156
157
158
159
160
   * 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.  
161
   */
162
163
  void SetHomeDirectory(const char* dir) 
    {
164
165
    m_cmHomeDirectory = dir;
    cmSystemTools::ConvertToUnixSlashes(m_cmHomeDirectory);
166
167
168
    }
  const char* GetHomeDirectory() 
    {
169
    return m_cmHomeDirectory.c_str();
170
    }
Ken Martin's avatar
Ken Martin committed
171
  void SetHomeOutputDirectory(const char* lib)
172
    {
Ken Martin's avatar
Ken Martin committed
173
174
    m_HomeOutputDirectory = lib;
    cmSystemTools::ConvertToUnixSlashes(m_HomeOutputDirectory);
175
    }
Ken Martin's avatar
Ken Martin committed
176
  const char* GetHomeOutputDirectory()
177
    {
Ken Martin's avatar
Ken Martin committed
178
    return m_HomeOutputDirectory.c_str();
179
    }
Ken Martin's avatar
Ken Martin committed
180
181
182
  //@}
  
  //@{
183
  /**
Ken Martin's avatar
Ken Martin committed
184
185
186
187
188
   * 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.  
189
   */
Ken Martin's avatar
Ken Martin committed
190
  void SetStartDirectory(const char* dir) 
191
    {
Ken Martin's avatar
Ken Martin committed
192
193
      m_cmStartDirectory = dir;
      cmSystemTools::ConvertToUnixSlashes(m_cmStartDirectory);
194
    }
Ken Martin's avatar
Ken Martin committed
195
  const char* GetStartDirectory() 
196
    {
Ken Martin's avatar
Ken Martin committed
197
      return m_cmStartDirectory.c_str();
198
    }
Ken Martin's avatar
Ken Martin committed
199
200
201
202
203
204
  void SetStartOutputDirectory(const char* lib)
    {
      m_StartOutputDirectory = lib;
      cmSystemTools::ConvertToUnixSlashes(m_StartOutputDirectory);
    }
  const char* GetStartOutputDirectory()
205
    {
Ken Martin's avatar
Ken Martin committed
206
      return m_StartOutputDirectory.c_str();
207
    }
Ken Martin's avatar
Ken Martin committed
208
  //@}
209

Ken Martin's avatar
Ken Martin committed
210
  //@{
211
  /**
Ken Martin's avatar
Ken Martin committed
212
213
214
215
216
   * 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.  
217
   */
Ken Martin's avatar
Ken Martin committed
218
  void SetCurrentDirectory(const char* dir) 
219
    {
Ken Martin's avatar
Ken Martin committed
220
221
      m_cmCurrentDirectory = dir;
      cmSystemTools::ConvertToUnixSlashes(m_cmCurrentDirectory);
222
    }
Ken Martin's avatar
Ken Martin committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
  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();
    }
  //@}
  
238
  /**
Ken Martin's avatar
Ken Martin committed
239
   * Specify the name of the library that is built by this makefile.
240
   */
Ken Martin's avatar
Ken Martin committed
241
  const char* GetLibraryName()
242
    {
Ken Martin's avatar
Ken Martin committed
243
    return m_LibraryName.c_str();
244
    }
245
246
247
248

  /**
   * Get a list of the build subdirectories.
   */
249
250
  const std::vector<std::string>& GetSubDirectories()
    { 
251
    return m_SubDirectories;
252
    }
253
254
255
256
257

  /**
   * Return a boolean flag indicating whether the build generates
   * any executables.
   */
Ken Martin's avatar
Ken Martin committed
258
  bool HasExecutables();
259
260
261
262

  /**
   * Get a list of include directories in the build.
   */
263
264
  std::vector<std::string>& GetIncludeDirectories()
    { 
265
    return m_IncludeDirectories;
266
    }
267
268
269
270

  /**
   * Get a list of link directories in the build.
   */
271
272
  std::vector<std::string>& GetLinkDirectories()
    { 
273
    return m_LinkDirectories;
274
275
    }
  
276
277
278
  /**
   * Get a list of link libraries in the build.
   */
279
280
  std::vector<std::string>& GetLinkLibraries()
    { 
281
    return m_LinkLibraries;
282
    }
283
284
285
286

  /**
   * Get a list of Win32 link libraries in the build.
   */
287
288
  std::vector<std::string>& GetLinkLibrariesWin32()
    { 
289
    return m_LinkLibrariesWin32;
290
291
    }
  
292
293
294
  /**
   * Get a list of Unix link libraries in the build.
   */
295
296
  std::vector<std::string>& GetLinkLibrariesUnix()
    { 
297
    return m_LinkLibrariesUnix;
298
    }
299
300
301
302
303
304
305
306
307
308

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

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

312
313
314
315
316
  /**
   * Do not use this.
   */
  std::vector<std::string>& GetMakeVerbatim() 
    {return m_MakeVerbatim;}
317

318
319
  /**
   * Given a variable name, return its value (as a string).
320
   */
321
322
  const char* GetDefinition(const char*);

323
  /**
324
   * Get a list of preprocessor define flags.
325
   */
326
327
  const char* GetDefineFlags()
    {return m_DefineFlags.c_str();}
328
329
330
331
332
333
334
  
  /**
   * Get the vector of used command instances.
   */
  const std::vector<cmCommand*>& GetUsedCommands() const
    {return m_UsedCommands;}
  
335
336
337
338
339
340
  /**
   * Dump documentation to a file. If 0 is returned, the
   * operation failed.
   */
  int DumpDocumentationToFile(const char *fileName);

341
342
343
344
  /**
   * Expand all defined varibles in the string.  
   * Defined varibles come from the m_Definitions map.
   * They are expanded with ${var} where var is the
345
346
   * entry in the m_Definitions map.  Also @var@ is
   * expanded to match autoconf style expansions.
347
   */
348
349
350
351
352
353
354
  void ExpandVariablesInString(std::string& source);

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

355
356
357
358
359
360
361
362
363
364
365
366
  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
367
368
369
370
371
372
373
374
375
376
377
378
379
  /** 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();
380

381
382
protected:
  std::string m_Prefix;
383
  std::vector<std::string> m_AuxSourceDirectories; // 
Ken Martin's avatar
Ken Martin committed
384
385
386
387
388
389
390
391

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

392
  std::string m_LibraryName;	// library name
393
  std::string m_ProjectName;	// project name
394
395
  std::vector<cmClassFile> m_Classes; // list of classes in makefile
  std::vector<std::string> m_SubDirectories; // list of sub directories
396
  std::vector<std::string> m_MakeVerbatim; // lines copied from input file
397
398
399
400
401
402
  std::vector<std::string> m_IncludeDirectories;
  std::vector<std::string> m_LinkDirectories;
  std::vector<std::string> m_LinkLibraries;
  std::vector<std::string> m_LinkLibrariesWin32;
  std::vector<std::string> m_LinkLibrariesUnix;
  std::string m_DefineFlags;
403
404
  std::vector<customCommand> m_CustomCommands;
  typedef std::map<std::string, cmCommand*> RegisteredCommandsMap;
405
406
  typedef std::map<std::string, std::string> DefinitionMap;
  DefinitionMap m_Definitions;
407
408
  RegisteredCommandsMap m_Commands;
  std::vector<cmCommand*> m_UsedCommands;
409
  cmMakefileGenerator* m_MakefileGenerator;
410
411
  
private:
Ken Martin's avatar
Ken Martin committed
412
413
414
  /**
   * Get the name of the parent directories CMakeLists file
   * given a current CMakeLists file name
415
   */
Ken Martin's avatar
Ken Martin committed
416
  std::string GetParentListFileName(const char *listFileName);
417
418
419
420
421

  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);
422
  void AddDefaultCommands();
423
  
424
425
426
427
};


#endif