cmMakefile.cxx 96.9 KB
Newer Older
1
2
/*=========================================================================

3
  Program:   CMake - Cross-Platform Makefile Generator
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.
11

Alexander Neundorf's avatar
   
Alexander Neundorf committed
12
13
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Will Schroeder's avatar
Will Schroeder committed
14
     PURPOSE.  See the above copyright notices for more information.
15
16

=========================================================================*/
17
#include "cmMakefile.h"
18
#include "cmVersion.h"
19
#include "cmCommand.h"
20
#include "cmSourceFile.h"
21
#include "cmSourceFileLocation.h"
22
#include "cmSystemTools.h"
Ken Martin's avatar
Ken Martin committed
23
24
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
25
#include "cmCommands.h"
26
#include "cmCacheManager.h"
27
#include "cmFunctionBlocker.h"
28
#include "cmListFileCache.h"
29
#include "cmCommandArgumentParserHelper.h"
30
#include "cmTest.h"
31
32
33
#ifdef CMAKE_BUILD_WITH_CMAKE
#  include "cmVariableWatch.h"
#endif
34
#include "cmInstallGenerator.h"
35
#include "cmake.h"
36
#include <stdlib.h> // required for atoi
37

38
39
#include "cmDocumentationFormatterText.h"

40
41
#include <cmsys/RegularExpression.hxx>

42
43
#include <cmsys/auto_ptr.hxx>

44
45
#include <ctype.h> // for isspace

46
47
48
// default is not to be building executables
cmMakefile::cmMakefile()
{
49
50
  this->DefinitionStack.push_back(DefinitionMap());

51
  // Setup the default include file regular expression (match everything).
Ken Martin's avatar
Ken Martin committed
52
  this->IncludeFileRegularExpression = "^.*$";
53
  // Setup the default include complaint regular expression (match nothing).
Ken Martin's avatar
Ken Martin committed
54
  this->ComplainFileRegularExpression = "^$";
55
  // Source and header file extensions that we can handle
56

57
  // Set up a list of source and header extensions
Alexander Neundorf's avatar
   
Alexander Neundorf committed
58
  // these are used to find files when the extension
59
  // is not given
60
  // The "c" extension MUST precede the "C" extension.
Ken Martin's avatar
Ken Martin committed
61
62
  this->SourceFileExtensions.push_back( "c" );
  this->SourceFileExtensions.push_back( "C" );
Alexander Neundorf's avatar
   
Alexander Neundorf committed
63

Ken Martin's avatar
Ken Martin committed
64
65
66
67
68
  this->SourceFileExtensions.push_back( "c++" );
  this->SourceFileExtensions.push_back( "cc" );
  this->SourceFileExtensions.push_back( "cpp" );
  this->SourceFileExtensions.push_back( "cxx" );
  this->SourceFileExtensions.push_back( "m" );
Alexander Neundorf's avatar
   
Alexander Neundorf committed
69
  this->SourceFileExtensions.push_back( "M" );
Ken Martin's avatar
Ken Martin committed
70
71
72
  this->SourceFileExtensions.push_back( "mm" );

  this->HeaderFileExtensions.push_back( "h" );
73
  this->HeaderFileExtensions.push_back( "hh" );
Ken Martin's avatar
Ken Martin committed
74
75
76
77
78
79
  this->HeaderFileExtensions.push_back( "h++" );
  this->HeaderFileExtensions.push_back( "hm" );
  this->HeaderFileExtensions.push_back( "hpp" );
  this->HeaderFileExtensions.push_back( "hxx" );
  this->HeaderFileExtensions.push_back( "in" );
  this->HeaderFileExtensions.push_back( "txx" );
Alexander Neundorf's avatar
   
Alexander Neundorf committed
80

Ken Martin's avatar
Ken Martin committed
81
82
  this->DefineFlags = " ";
  this->LocalGenerator = 0;
83
84

#if defined(CMAKE_BUILD_WITH_CMAKE)
85
  this->AddSourceGroup("", "^.*$");
Ken Martin's avatar
Ken Martin committed
86
87
88
  this->AddSourceGroup
    ("Source Files",
     "\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|rc|def|r|odl|idl|hpj|bat)$");
Alexander Neundorf's avatar
   
Alexander Neundorf committed
89
  this->AddSourceGroup("Header Files",
90
                       "\\.(h|hh|h\\+\\+|hm|hpp|hxx|in|txx|inl)$");
91
  this->AddSourceGroup("CMake Rules", "\\.rule$");
92
  this->AddSourceGroup("Resources", "\\.plist$");
93
#endif
94
  this->AddDefaultDefinitions();
95
  this->Initialize();
96
97
  this->PreOrder = false;
}
98

99
100
101
102
cmMakefile::cmMakefile(const cmMakefile& mf)
{
  this->Prefix = mf.Prefix;
  this->AuxSourceDirectories = mf.AuxSourceDirectories;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
103
104
105
  this->cmStartDirectory = mf.cmStartDirectory;
  this->StartOutputDirectory = mf.StartOutputDirectory;
  this->cmHomeDirectory = mf.cmHomeDirectory;
106
107
108
109
110
111
112
113
  this->HomeOutputDirectory = mf.HomeOutputDirectory;
  this->cmCurrentListFile = mf.cmCurrentListFile;
  this->ProjectName = mf.ProjectName;
  this->Targets = mf.Targets;
  this->SourceFiles = mf.SourceFiles;
  this->Tests = mf.Tests;
  this->IncludeDirectories = mf.IncludeDirectories;
  this->LinkDirectories = mf.LinkDirectories;
114
  this->SystemIncludeDirectories = mf.SystemIncludeDirectories;
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  this->ListFiles = mf.ListFiles;
  this->OutputFiles = mf.OutputFiles;
  this->LinkLibraries = mf.LinkLibraries;
  this->InstallGenerators = mf.InstallGenerators;
  this->IncludeFileRegularExpression = mf.IncludeFileRegularExpression;
  this->ComplainFileRegularExpression = mf.ComplainFileRegularExpression;
  this->SourceFileExtensions = mf.SourceFileExtensions;
  this->HeaderFileExtensions = mf.HeaderFileExtensions;
  this->DefineFlags = mf.DefineFlags;

#if defined(CMAKE_BUILD_WITH_CMAKE)
  this->SourceGroups = mf.SourceGroups;
#endif

129
  this->DefinitionStack.push_back(mf.DefinitionStack.back());
130
131
132
133
134
135
136
137
138
  this->LocalGenerator = mf.LocalGenerator;
  this->FunctionBlockers = mf.FunctionBlockers;
  this->DataMap = mf.DataMap;
  this->MacrosMap = mf.MacrosMap;
  this->SubDirectoryOrder = mf.SubDirectoryOrder;
  this->TemporaryDefinitionKey = mf.TemporaryDefinitionKey;
  this->Properties = mf.Properties;
  this->PreOrder = mf.PreOrder;
  this->ListFileStack = mf.ListFileStack;
139
  this->Initialize();
140
141
}

142
143
144
145
146
147
//----------------------------------------------------------------------------
void cmMakefile::Initialize()
{
  this->cmDefineRegex.compile("#cmakedefine[ \t]+([A-Za-z_0-9]*)");
  this->cmDefine01Regex.compile("#cmakedefine01[ \t]+([A-Za-z_0-9]*)");
  this->cmAtVarRegex.compile("(@[A-Za-z_0-9/.+-]+@)");
148
149
150

  // Enter a policy level for this directory.
  this->PushPolicy();
151
}
152

153
154
unsigned int cmMakefile::GetCacheMajorVersion()
{
155
  return this->GetCacheManager()->GetCacheMajorVersion();
156
157
158
159
}

unsigned int cmMakefile::GetCacheMinorVersion()
{
160
  return this->GetCacheManager()->GetCacheMinorVersion();
161
162
}

163
164
165
166
bool cmMakefile::NeedCacheCompatibility(int major, int minor)
{
  return this->GetCacheManager()->NeedCacheCompatibility(major, minor);
}
167

168
169
cmMakefile::~cmMakefile()
{
170
  for(std::vector<cmInstallGenerator*>::iterator
Ken Martin's avatar
Ken Martin committed
171
172
        i = this->InstallGenerators.begin();
      i != this->InstallGenerators.end(); ++i)
173
174
175
    {
    delete *i;
    }
Ken Martin's avatar
Ken Martin committed
176
177
  for(std::vector<cmSourceFile*>::iterator i = this->SourceFiles.begin();
      i != this->SourceFiles.end(); ++i)
178
179
180
    {
    delete *i;
    }
Ken Martin's avatar
Ken Martin committed
181
182
  for(std::vector<cmTest*>::iterator i = this->Tests.begin();
      i != this->Tests.end(); ++i)
183
184
185
    {
    delete *i;
    }
186
187
188
189
190
191
  for(std::vector<cmTarget*>::iterator
        i = this->ImportedTargetsOwned.begin();
      i != this->ImportedTargetsOwned.end(); ++i)
    {
    delete *i;
    }
Ken Martin's avatar
Ken Martin committed
192
  for(unsigned int i=0; i < this->UsedCommands.size(); i++)
193
    {
Ken Martin's avatar
Ken Martin committed
194
    delete this->UsedCommands[i];
195
    }
Ken Martin's avatar
Ken Martin committed
196
197
  for(DataMapType::const_iterator d = this->DataMap.begin();
      d != this->DataMap.end(); ++d)
198
199
200
201
202
203
    {
    if(d->second)
      {
      delete d->second;
      }
    }
Bill Hoffman's avatar
Bill Hoffman committed
204
  std::list<cmFunctionBlocker *>::iterator pos;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
205
  for (pos = this->FunctionBlockers.begin();
Ken Martin's avatar
Ken Martin committed
206
       pos != this->FunctionBlockers.end(); ++pos)
Bill Hoffman's avatar
Bill Hoffman committed
207
208
209
210
    {
    cmFunctionBlocker* b = *pos;
    delete b;
    }
Ken Martin's avatar
Ken Martin committed
211
  this->FunctionBlockers.clear();
212
213
214
215
216
  if (this->PolicyStack.size() != 1)
  {
    cmSystemTools::Error("Internal CMake Error, Policy Stack has not been"
      " popped properly");
  }
217
218
}

Alexander Neundorf's avatar
   
Alexander Neundorf committed
219
void cmMakefile::PrintStringVector(const char* s,
Ken Martin's avatar
Ken Martin committed
220
                                   const std::vector<std::string>& v) const
221
222
{
  std::cout << s << ": ( \n";
223
  for(std::vector<std::string>::const_iterator i = v.begin();
224
225
226
227
228
      i != v.end(); ++i)
    {
    std::cout << (*i).c_str() << " ";
    }
  std::cout << " )\n";
229
230
}

Ken Martin's avatar
Ken Martin committed
231
void cmMakefile
Alexander Neundorf's avatar
   
Alexander Neundorf committed
232
::PrintStringVector(const char* s,
Ken Martin's avatar
Ken Martin committed
233
                    const std::vector<std::pair<cmStdString, bool> >& v) const
234
235
{
  std::cout << s << ": ( \n";
Alexander Neundorf's avatar
   
Alexander Neundorf committed
236
  for(std::vector<std::pair<cmStdString, bool> >::const_iterator i
Ken Martin's avatar
Ken Martin committed
237
        = v.begin(); i != v.end(); ++i)
238
239
240
241
242
243
    {
    std::cout << i->first.c_str() << " " << i->second;
    }
  std::cout << " )\n";
}

244
245

// call print on all the classes in the makefile
246
void cmMakefile::Print()
247
{
248
  // print the class lists
249
  std::cout << "classes:\n";
250

Ken Martin's avatar
Ken Martin committed
251
252
253
  std::cout << " this->Targets: ";
  for (cmTargets::iterator l = this->Targets.begin();
       l != this->Targets.end(); l++)
254
255
256
257
    {
    std::cout << l->first << std::endl;
    }

Alexander Neundorf's avatar
   
Alexander Neundorf committed
258
  std::cout << " this->StartOutputDirectory; " <<
Ken Martin's avatar
Ken Martin committed
259
    this->StartOutputDirectory.c_str() << std::endl;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
260
  std::cout << " this->HomeOutputDirectory; " <<
Ken Martin's avatar
Ken Martin committed
261
    this->HomeOutputDirectory.c_str() << std::endl;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
262
  std::cout << " this->cmStartDirectory; " <<
Ken Martin's avatar
Ken Martin committed
263
    this->cmStartDirectory.c_str() << std::endl;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
264
  std::cout << " this->cmHomeDirectory; " <<
Ken Martin's avatar
Ken Martin committed
265
    this->cmHomeDirectory.c_str() << std::endl;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
266
  std::cout << " this->ProjectName; "
Ken Martin's avatar
Ken Martin committed
267
            <<  this->ProjectName.c_str() << std::endl;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
268
  this->PrintStringVector("this->IncludeDirectories;",
Ken Martin's avatar
Ken Martin committed
269
                          this->IncludeDirectories);
Ken Martin's avatar
Ken Martin committed
270
  this->PrintStringVector("this->LinkDirectories", this->LinkDirectories);
271
#if defined(CMAKE_BUILD_WITH_CMAKE)
Alexander Neundorf's avatar
   
Alexander Neundorf committed
272
  for( std::vector<cmSourceGroup>::const_iterator i =
Ken Martin's avatar
Ken Martin committed
273
         this->SourceGroups.begin(); i != this->SourceGroups.end(); ++i)
274
    {
275
    std::cout << "Source Group: " << i->GetName() << std::endl;
276
    }
277
#endif
278
279
}

280
281
bool cmMakefile::CommandExists(const char* name) const
{
282
  return this->GetCMakeInstance()->CommandExists(name);
283
}
284

285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//----------------------------------------------------------------------------
void cmMakefile::IssueError(std::string const& msg) const
{
  this->IssueMessage(msg, true);
}

//----------------------------------------------------------------------------
void cmMakefile::IssueWarning(std::string const& msg) const
{
  this->IssueMessage(msg, false);
}

//----------------------------------------------------------------------------
void cmMakefile::IssueMessage(std::string const& text, bool isError) const
{
  cmOStringStream msg;

  // Construct the message header.
  if(isError)
    {
    msg << "CMake Error:";
    }
  else
    {
    msg << "CMake Warning:";
    }

  // Add the immediate context.
  CallStackType::const_reverse_iterator i = this->CallStack.rbegin();
  if(i != this->CallStack.rend())
    {
    if(isError)
      {
318
      (*i).Status->SetNestedError(true);
319
      }
320
    cmListFileContext const& lfc = *(*i).Context;
321
322
323
324
325
326
327
328
329
    msg
      << " at "
      << this->LocalGenerator->Convert(lfc.FilePath.c_str(),
                                       cmLocalGenerator::HOME)
      << ":" << lfc.Line << " " << lfc.Name;
    ++i;
    }

  // Add the message text.
330
  {
331
  msg << " {\n";
332
333
334
  cmDocumentationFormatterText formatter;
  formatter.SetIndent("  ");
  formatter.PrintFormatted(msg, text.c_str());
335
  msg << "}";
336
  }
337
338
339
340
341
342
343

  // Add the rest of the context.
  if(i != this->CallStack.rend())
    {
    msg << " with call stack {\n";
    while(i != this->CallStack.rend())
      {
344
      cmListFileContext const& lfc = *(*i).Context;
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
      msg << "  "
          << this->LocalGenerator->Convert(lfc.FilePath.c_str(),
                                           cmLocalGenerator::HOME)
          << ":" << lfc.Line << " " << lfc.Name << "\n";
      ++i;
      }
    msg << "}\n";
    }
  else
    {
    msg << "\n";
    }

  // Output the message.
  if(isError)
    {
    cmSystemTools::SetErrorOccured();
    cmSystemTools::Message(msg.str().c_str(), "Error");
    }
  else
    {
    cmSystemTools::Message(msg.str().c_str(), "Warning");
    }
}

//----------------------------------------------------------------------------
// Helper class to make sure the call stack is valid.
class cmMakefileCall
{
public:
  cmMakefileCall(cmMakefile* mf,
                 cmListFileContext const& lfc,
                 cmExecutionStatus& status): Makefile(mf)
    {
    cmMakefile::CallStackEntry entry = {&lfc, &status};
    this->Makefile->CallStack.push_back(entry);
    }
  ~cmMakefileCall()
    {
    this->Makefile->CallStack.pop_back();
    }
private:
  cmMakefile* Makefile;
};

//----------------------------------------------------------------------------
391
392
bool cmMakefile::ExecuteCommand(const cmListFileFunction& lff,
                                cmExecutionStatus &status)
Ken Martin's avatar
Ken Martin committed
393
{
394
  bool result = true;
395

396
  // quick return if blocked
397
  if(this->IsFunctionBlocked(lff,status))
398
    {
399
400
    // No error.
    return result;
401
    }
402
  
Ken Martin's avatar
Ken Martin committed
403
  std::string name = lff.Name;
404

405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  // Place this call on the call stack.
  cmMakefileCall stack_manager(this, lff, status);
  static_cast<void>(stack_manager);

  // Lookup the command prototype.
  if(cmCommand* proto = this->GetCMakeInstance()->GetCommand(name.c_str()))
    {
    // Clone the prototype.
    cmsys::auto_ptr<cmCommand> pcmd(proto->Clone());
    pcmd->SetMakefile(this);

    // Decide whether to invoke the command.
    if(pcmd->GetEnabled() && !cmSystemTools::GetFatalErrorOccured()  &&
       (!this->GetCMakeInstance()->GetScriptMode() || pcmd->IsScriptable()))
      {
      // Try invoking the command.
      if(!pcmd->InvokeInitialPass(lff.Arguments,status) ||
         status.GetNestedError())
Ken Martin's avatar
Ken Martin committed
423
        {
424
425
426
427
428
        if(!status.GetNestedError())
          {
          // The command invocation requested that we report an error.
          this->IssueError(pcmd->GetError());
          }
429
430
        result = false;
        if ( this->GetCMakeInstance()->GetScriptMode() )
Ken Martin's avatar
Ken Martin committed
431
          {
432
          cmSystemTools::SetFatalErrorOccured();
Ken Martin's avatar
Ken Martin committed
433
434
          }
        }
435
436
437
      else
        {
        // use the command
438
        this->UsedCommands.push_back(pcmd.release());
439
        }
Ken Martin's avatar
Ken Martin committed
440
      }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
441
    else if ( this->GetCMakeInstance()->GetScriptMode()
442
              && !pcmd->IsScriptable() )
443
      {
444
445
446
447
      std::string error = "Command ";
      error += pcmd->GetName();
      error += "() is not scriptable";
      this->IssueError(error);
448
449
450
      result = false;
      cmSystemTools::SetFatalErrorOccured();
      }
Ken Martin's avatar
Ken Martin committed
451
452
453
    }
  else
    {
454
455
    if(!cmSystemTools::GetFatalErrorOccured())
      {
456
457
458
459
      std::string error = "Unknown CMake command \"";
      error += lff.Name;
      error += "\".";
      this->IssueError(error);
460
      result = false;
461
      cmSystemTools::SetFatalErrorOccured();
462
      }
Ken Martin's avatar
Ken Martin committed
463
    }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
464

465
  return result;
Ken Martin's avatar
Ken Martin committed
466
467
}

468
// Parse the given CMakeLists.txt file executing all commands
Anonymous's avatar
Anonymous committed
469
//
Alexander Neundorf's avatar
   
Alexander Neundorf committed
470
bool cmMakefile::ReadListFile(const char* filename_in,
Alexander Neundorf's avatar
   
Alexander Neundorf committed
471
472
                              const char *external_in,
                              std::string* fullPath)
473
{
474
475
476
477
  std::string currentParentFile
    = this->GetSafeDefinition("CMAKE_PARENT_LIST_FILE");
  std::string currentFile
    = this->GetSafeDefinition("CMAKE_CURRENT_LIST_FILE");
478
479
  this->AddDefinition("CMAKE_PARENT_LIST_FILE", filename_in);

480
481
482
483
  // used to watch for blockers going out of scope
  // e.g. mismatched IF statement
  std::set<cmFunctionBlocker *> originalBlockers;

Brad King's avatar
Brad King committed
484
485
486
487
488
489
490
491
492
493
  const char* external = 0;
  std::string external_abs;

  const char* filename = filename_in;
  std::string filename_abs;

  if (external_in)
    {
    external_abs =
      cmSystemTools::CollapseFullPath(external_in,
Ken Martin's avatar
Ken Martin committed
494
                                      this->cmStartDirectory.c_str());
Brad King's avatar
Brad King committed
495
496
497
498
499
    external = external_abs.c_str();
    if (filename_in)
      {
      filename_abs =
        cmSystemTools::CollapseFullPath(filename_in,
Ken Martin's avatar
Ken Martin committed
500
                                        this->cmStartDirectory.c_str());
Brad King's avatar
Brad King committed
501
502
503
      filename = filename_abs.c_str();
      }
    }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
504

505
  // keep track of the current file being read
506
507
  if (filename)
    {
Ken Martin's avatar
Ken Martin committed
508
    if(this->cmCurrentListFile != filename)
Bill Hoffman's avatar
Bill Hoffman committed
509
      {
Ken Martin's avatar
Ken Martin committed
510
      this->cmCurrentListFile = filename;
Bill Hoffman's avatar
Bill Hoffman committed
511
      }
512
    // loop over current function blockers and record them
Bill Hoffman's avatar
Bill Hoffman committed
513
    std::list<cmFunctionBlocker *>::iterator pos;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
514
    for (pos = this->FunctionBlockers.begin();
Ken Martin's avatar
Ken Martin committed
515
         pos != this->FunctionBlockers.end(); ++pos)
516
517
518
      {
      originalBlockers.insert(*pos);
      }
519
    }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
520

521
  // Now read the input file
Anonymous's avatar
Anonymous committed
522
523
524
  const char *filenametoread= filename;

  if( external)
525
    {
Anonymous's avatar
Anonymous committed
526
    filenametoread= external;
527
    }
528
529
530

  this->AddDefinition("CMAKE_CURRENT_LIST_FILE", filenametoread);

531
532
533
  // try to see if the list file is the top most
  // list file for a project, and if it is, then it
  // must have a project command.   If there is not
Alexander Neundorf's avatar
   
Alexander Neundorf committed
534
  // one, then cmake will provide one via the
535
536
  // cmListFileCache class.
  bool requireProjectCommand = false;
Ken Martin's avatar
Ken Martin committed
537
  if(!external && this->cmStartDirectory == this->cmHomeDirectory)
538
539
    {
    if(cmSystemTools::LowerCase(
Bill Hoffman's avatar
Bill Hoffman committed
540
      cmSystemTools::GetFilenameName(filename)) == "cmakelists.txt")
541
542
543
544
      {
      requireProjectCommand = true;
      }
    }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
545

Ken Martin's avatar
Ken Martin committed
546
547
  // push the listfile onto the stack
  this->ListFileStack.push_back(filenametoread);
Alexander Neundorf's avatar
   
Alexander Neundorf committed
548
549
550
551
  if(fullPath!=0)
    {
    *fullPath=filenametoread;
    }
552
  cmListFile cacheFile;
553
  if( !cacheFile.ParseFile(filenametoread, requireProjectCommand, this) )
554
    {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
555
556
    // pop the listfile off the stack
    this->ListFileStack.pop_back();
Alexander Neundorf's avatar
   
Alexander Neundorf committed
557
558
    if(fullPath!=0)
      {
559
      *fullPath = "";
Alexander Neundorf's avatar
   
Alexander Neundorf committed
560
      }
561
562
    this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
    this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
563
564
    return false;
    }
565
  // add this list file to the list of dependencies
Ken Martin's avatar
Ken Martin committed
566
567
  this->ListFiles.push_back( filenametoread);
  const size_t numberFunctions = cacheFile.Functions.size();
568
  for(size_t i =0; i < numberFunctions; ++i)
569
    {
570
571
572
573
    cmExecutionStatus status;
    this->ExecuteCommand(cacheFile.Functions[i],status);
    if (status.GetReturnInvoked() ||
        cmSystemTools::GetFatalErrorOccured() )
574
      {
Ken Martin's avatar
Ken Martin committed
575
576
      // pop the listfile off the stack
      this->ListFileStack.pop_back();
Brad King's avatar
Brad King committed
577
578
      this->AddDefinition("CMAKE_PARENT_LIST_FILE",
                          currentParentFile.c_str());
579
      this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
580
      return true;
581
      }
582
    }
Ken Martin's avatar
Ken Martin committed
583

584
  // send scope ended to and function blockers
Ken Martin's avatar
Ken Martin committed
585
586
587
  if (filename)
    {
    // loop over all function blockers to see if any block this command
Bill Hoffman's avatar
Bill Hoffman committed
588
    std::list<cmFunctionBlocker *>::iterator pos;
Alexander Neundorf's avatar
   
Alexander Neundorf committed
589
    for (pos = this->FunctionBlockers.begin();
Ken Martin's avatar
Ken Martin committed
590
         pos != this->FunctionBlockers.end(); ++pos)
Ken Martin's avatar
Ken Martin committed
591
      {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
592
      // if this blocker was not in the original then send a
593
594
595
596
597
      // scope ended message
      if (originalBlockers.find(*pos) == originalBlockers.end())
        {
        (*pos)->ScopeEnded(*this);
        }
Ken Martin's avatar
Ken Martin committed
598
599
      }
    }
Alexander Neundorf's avatar
   
Alexander Neundorf committed
600

601
602
  this->AddDefinition("CMAKE_PARENT_LIST_FILE", currentParentFile.c_str());
  this->AddDefinition("CMAKE_CURRENT_LIST_FILE", currentFile.c_str());
Ken Martin's avatar
Ken Martin committed
603
604
605
606

  // pop the listfile off the stack
  this->ListFileStack.pop_back();

607
608
  return true;
}
609

610

611
void cmMakefile::AddCommand(cmCommand* wg)
612
{
613
  this->GetCMakeInstance()->AddCommand(wg);
614
615
}

Alexander Neundorf's avatar
   
Alexander Neundorf committed
616
// Set the make file
Ken Martin's avatar
Ken Martin committed
617
void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
618
{
Ken Martin's avatar
Ken Martin committed
619
  this->LocalGenerator = lg;
620
621
}

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
bool cmMakefile::NeedBackwardsCompatibility(unsigned int major,
                                            unsigned int minor,
                                            unsigned int patch)
{
  if(this->LocalGenerator)
    {
    return
      this->LocalGenerator->NeedBackwardsCompatibility(major, minor, patch);
    }
  else
    {
    return false;
    }
}

637
void cmMakefile::FinalPass()
638
639
{
  // do all the variable expansions here
640
  this->ExpandVariables();
641

642
  // give all the commands a chance to do something
643
  // after the file has been parsed before generation
Ken Martin's avatar
Ken Martin committed
644
645
  for(std::vector<cmCommand*>::iterator i = this->UsedCommands.begin();
      i != this->UsedCommands.end(); ++i)
646
647
    {
    (*i)->FinalPass();
648
    }
649
650
651

}

Bill Hoffman's avatar
Bill Hoffman committed
652
// Generate the output file
653
void cmMakefile::ConfigureFinalPass()
654
655
{
  this->FinalPass();
656
657
658
659
  const char* oldValue
    = this->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  if (oldValue && atof(oldValue) <= 1.2)
    {
Ken Martin's avatar
Ken Martin committed
660
661
662
663
664
    cmSystemTools::Error("You have requested backwards compatibility "
                         "with CMake version 1.2 or earlier. This version "
                         "of CMake only supports backwards compatibility "
                         "with CMake 1.4 or later. For compatibility with "
                         "1.2 or earlier please use CMake 2.0");
665
    }
Ken Martin's avatar
Ken Martin committed
666
667
  for (cmTargets::iterator l = this->Targets.begin();
       l != this->Targets.end(); l++)
668
    {
669
    l->second.AnalyzeLibDependencies(*this);
670
    }
671
672
}

673
674
675
676
677
678
//----------------------------------------------------------------------------
void
cmMakefile::AddCustomCommandToTarget(const char* target,
                                     const std::vector<std::string>& depends,
                                     const cmCustomCommandLines& commandLines,
                                     cmTarget::CustomCommandType type,
679
                                     const char* comment,
680
681
                                     const char* workingDir,
                                     bool escapeOldStyle)
682
{
683
  // Find the target to which to add the custom command.
Ken Martin's avatar
Ken Martin committed
684
685
  cmTargets::iterator ti = this->Targets.find(target);
  if(ti != this->Targets.end())
686
687
    {
    // Add the command to the appropriate build step for the target.
688
    std::vector<std::string> no_output;
689
    cmCustomCommand cc(no_output, depends, commandLines, comment, workingDir);
690
    cc.SetEscapeOldStyle(escapeOldStyle);
691
    switch(type)
692
      {
693
694
695
696
697
698
699
700
701
702
      case cmTarget::PRE_BUILD:
        ti->second.GetPreBuildCommands().push_back(cc);
        break;
      case cmTarget::PRE_LINK:
        ti->second.GetPreLinkCommands().push_back(cc);
        break;
      case cmTarget::POST_BUILD:
        ti->second.GetPostBuildCommands().push_back(cc);
        break;
      }
703
704
705
    }
}

706
707
//----------------------------------------------------------------------------
void
708
cmMakefile::AddCustomCommandToOutput(const std::vector<std::string>& outputs,
709
710
711
712
                                     const std::vector<std::string>& depends,
                                     const char* main_dependency,
                                     const cmCustomCommandLines& commandLines,
                                     const char* comment,
713
                                     const char* workingDir,
714
715
                                     bool replace,
                                     bool escapeOldStyle)
716
{
717
718
719
720
721
722
723
  // Make sure there is at least one output.
  if(outputs.empty())
    {
    cmSystemTools::Error("Attempt to add a custom rule with no output!");
    return;
    }

724
725
726
727
728
729
730
731
  // Choose a source file on which to store the custom command.
  cmSourceFile* file = 0;
  if(main_dependency && main_dependency[0])
    {
    // The main dependency was specified.  Use it unless a different
    // custom command already used it.
    file = this->GetSource(main_dependency);
    if(file && file->GetCustomCommand() && !replace)
732
      {
733
734
      // The main dependency already has a custom command.
      if(commandLines == file->GetCustomCommand()->GetCommandLines())
735
        {
736
737
738
        // The existing custom command is identical.  Silently ignore
        // the duplicate.
        return;
739
740
741
        }
      else
        {
742
743
744
        // The existing custom command is different.  We need to
        // generate a rule file for this new command.
        file = 0;
745
        }
746
747
748
      }
    else
      {
749
750
751
      // The main dependency does not have a custom command or we are
      // allowed to replace it.  Use it to store the command.
      file = this->GetOrCreateSource(main_dependency);
752
753
754
      }
    }

755
756
  // Generate a rule file if the main dependency is not available.
  if(!file)
757
    {
758
759
    // Construct a rule file associated with the first output produced.
    std::string outName = outputs[0];
760
761
762
    outName += ".rule";

    // Check if the rule file already exists.
763
    file = this->GetSource(outName.c_str());
764
    if(file && file->GetCustomCommand() && !replace)
765
      {
766
767
      // The rule file already exists.
      if(commandLines != file->GetCustomCommand()->GetCommandLines())
768
        {
769
        cmSystemTools::Error("Attempt to add a custom rule to output \"",
770
771
                             outName.c_str(),
                             "\" which already has a custom rule.");
772
        }
773
774
      return;
      }
775
776

    // Create a cmSourceFile for the rule file.
777
778
    file = this->GetOrCreateSource(outName.c_str(), true);
    }
779

780
781
782
  // Always create the output sources and mark them generated.
  for(std::vector<std::string>::const_iterator o = outputs.begin();
      o != outputs.end(); ++o)
783
    {
784
785
786
787
    if(cmSourceFile* out = this->GetOrCreateSource(o->c_str(), true))
      {
      out->SetProperty("GENERATED", "1");
      }
788
    }
789
790

  // Construct a complete list of dependencies.
791
  std::vector<std::string> depends2(depends);
792
  if(main_dependency && main_dependency[0])
793
    {
794
    depends2.push_back(main_dependency);
795
    }
796
797

  // Attach the custom command to the file.
798
  if(file)
799
    {
800
    cmCustomCommand* cc =
801
802
      new cmCustomCommand(outputs, depends2, commandLines,
                          comment, workingDir);
803
    cc->SetEscapeOldStyle(escapeOldStyle);
804
    file->SetCustomCommand(cc);
805
806
807
    }
}

808
809
810
811
812
813
814
815
//----------------------------------------------------------------------------
void
cmMakefile::AddCustomCommandToOutput(const char* output,
                                     const std::vector<std::string>& depends,
                                     const char* main_dependency,
                                     const cmCustomCommandLines& commandLines,
                                     const char* comment,
                                     const char* workingDir,
816
817
                                     bool replace,
                                     bool escapeOldStyle)
818
819
820
821
822
{
  std::vector<std::string> outputs;
  outputs.push_back(output);
  this->AddCustomCommandToOutput(outputs, depends, main_dependency,
                                 commandLines, comment, workingDir,
823
                                 replace, escapeOldStyle);
824
825
}

826
827
828
829
830
831
832
833
//----------------------------------------------------------------------------
void
cmMakefile::AddCustomCommandOldStyle(const char* target,
                                     const std::vector<std::string>& outputs,
                                     const std::vector<std::string>& depends,
                                     const char* source,
                                     const cmCustomCommandLines& commandLines,
                                     const char* comment)
Ken Martin's avatar
Ken Martin committed
834
{
835
836
837
838
839
840
841
842
  // Translate the old-style signature to one of the new-style
  // signatures.
  if(strcmp(source, target) == 0)
    {
    // In the old-style signature if the source and target were the
    // same then it added a post-build rule to the target.  Preserve
    // this behavior.
    this->AddCustomCommandToTarget(target, depends, commandLines,
843
                                   cmTarget::POST_BUILD, comment, 0);
844
845
    return;
    }
Ken Martin's avatar
Ken Martin committed
846

847
848
849
850
851
852
  // Each output must get its own copy of this rule.
  cmsys::RegularExpression sourceFiles("\\.(C|M|c|c\\+\\+|cc|cpp|cxx|m|mm|"
                                       "rc|def|r|odl|idl|hpj|bat|h|h\\+\\+|"
                                       "hm|hpp|hxx|in|txx|inl)$");
  for(std::vector<std::string>::const_iterator oi = outputs.begin();
      oi != outputs.end(); ++oi)
853
    {
854
855
856
857
858
    // Get the name of this output.
    const char* output = oi->c_str();

    // Choose whether to use a main dependency.
    if(sourceFiles.find(source))
859
      {
860
861
      // The source looks like a real file.  Use it as the main dependency.
      this->AddCustomCommandToOutput(output, depends, source,
862
                                     commandLines, comment, 0);
863
      }
864
    else
865
      {
866
867
868
869
870
      // The source may not be a real file.  Do not use a main dependency.
      const char* no_main_dependency = 0;
      std::vector<std::string> depends2 = depends;
      depends2.push_back(source);
      this->AddCustomCommandToOutput(output, depends2, no_main_dependency,
871
                                     commandLines, comment, 0);
872
      }
873
874
875
876
877
878
879

    // If the rule was added to the source (and not a .rule file),
    // then add the source to the target to make sure the rule is
    // included.
    std::string sname = output;
    sname += ".rule";
    if(!this->GetSource(sname.c_str()))
880
      {
Ken Martin's avatar
Ken Martin committed
881
      if (this->Targets.find(target) != this->Targets.end())
882
        {
883
        this->Targets[target].AddSource(source);
884
885
886
887
888
889
890
        }
      else
        {
        cmSystemTools::Error("Attempt to add a custom rule to a target "
                             "that does not exist yet for target ", target);
        return;
        }
891
      }
892
    }
893
}
894

895
//----------------------------------------------------------------------------
Alexander Neundorf's avatar
   
Alexander Neundorf committed
896
void cmMakefile::AddUtilityCommand(const char* utilityName,
Ken Martin's avatar
Ken Martin committed
897
                                   bool excludeFromAll,
898
                                   const std::vector<std::string>& depends,
899
                                   const char* workingDirectory,
900
901
902
                                   const char* command,
                                   const char* arg1,
                                   const char* arg2,
Andy Cedilnik's avatar
Andy Cedilnik committed
903
904
                                   const char* arg3,
                                   const char* arg4)
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
{
  // Construct the command line for the custom command.
  cmCustomCommandLine commandLine;
  commandLine.push_back(command);
  if(arg1)
    {
    commandLine.push_back(arg1);
    }
  if(arg2)
    {
    commandLine.push_back(arg2);
    }
  if(arg3)
    {
    commandLine.push_back(arg3);
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
921
922
923
924
  if(arg4)
    {
    commandLine.push_back(arg4);
    }
925
926
927
928
  cmCustomCommandLines commandLines;
  commandLines.push_back(commandLine);

  // Call the real signature of this method.
Alexander Neundorf's avatar
   
Alexander Neundorf committed
929
  this->AddUtilityCommand(utilityName, excludeFromAll, workingDirectory,
930
                          depends, commandLines);
931
932
933
}

//----------------------------------------------------------------------------
Alexander Neundorf's avatar
   
Alexander Neundorf committed
934
void cmMakefile::AddUtilityCommand(const char* utilityName,
Ken Martin's avatar
Ken Martin committed
935
                                   bool excludeFromAll,
936
                                   const char* workingDirectory,
937
                                   const std::vector<std::string>& depends,
938
                                   const cmCustomCommandLines& commandLines,
939
                                   bool escapeOldStyle, const char* comment)
940
941
{
  // Create a target instance for this utility.
942
  cmTarget* target = this->AddNewTarget(cmTarget::UTILITY, utilityName);
Ken Martin's avatar
Ken Martin committed
943
944
  if (excludeFromAll)
    {
Alexander Neundorf's avatar
   
Alexander Neundorf committed
945
    target->SetProperty("EXCLUDE_FROM_ALL", "TRUE");
Ken Martin's avatar
Ken Martin committed
946
    }
947
948
949
950
951
952
  if(!comment)
    {
    // Use an empty comment to avoid generation of default comment.
    comment = "";
    }

953
  // Store the custom command in the target.
954
955
956
957
958
959
960
961
  std::string force = this->GetStartOutputDirectory();
  force += cmake::GetCMakeFilesDirectory();
  force += "/";
  force += utilityName;
  const char* no_main_dependency = 0;
  bool no_replace = false;
  this->AddCustomCommandToOutput(force.c_str(), depends,
                                 no_main_dependency,
962
                                 commandLines, comment,
963
964
                                 workingDirectory, no_replace,
                                 escapeOldStyle);
965
  cmSourceFile* sf = target->AddSource(force.c_str());
966

967
  // The output is not actually created so mark it symbolic.
968
  if(sf)
969
970
971
972
973
974
975
976
    {
    sf->SetProperty("SYMBOLIC", "1");
    }
  else
    {
    cmSystemTools::Error("Could not get source file entry for ",
                         force.c_str());
    }
977
978
}

979
980
void cmMakefile::AddDefineFlag(const char* flag)
{
981
982
983
984
985
  if (!flag)
    {
    return;
    }

986
987
988
989
990
991
  // If this is really a definition, update COMPILE_DEFINITIONS.
  if(this->ParseDefineFlag(flag, false))
    {
    return;
    }

992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  // remove any \n\r
  std::string ret = flag;
  std::string::size_type pos = 0;
  while((pos = ret.find('\n', pos