cmMakefile.cxx 41 KB
Newer Older
1
2
3
4
5
6
7
8
/*=========================================================================

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

Will Schroeder's avatar
Will Schroeder committed
9
10
  Copyright (c) 2002 Insight Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11

Will Schroeder's avatar
Will Schroeder committed
12
13
14
     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.
15
16

=========================================================================*/
17
#include "cmMakefile.h"
18
#include "cmCommand.h"
19
#include "cmStandardIncludes.h"
20
#include "cmSourceFile.h"
21
#include "cmDirectory.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 "cmake.h"
30
#include <stdio.h>  // required for sprintf
31
32
33
34

// default is not to be building executables
cmMakefile::cmMakefile()
{
35
36
37
38
  // Setup the default include file regular expression (match everything).
  m_IncludeFileRegularExpression = "^.*$";
  // Setup the default include complaint regular expression (match nothing).
  m_ComplainFileRegularExpression = "^$";
39
40
41
42
43
44
  // Source and header file extensions that we can handle
  m_SourceFileExtensions.push_back( "cxx" );
  m_SourceFileExtensions.push_back( "cpp" );
  m_SourceFileExtensions.push_back( "c" );
  m_SourceFileExtensions.push_back( "M" );
  m_SourceFileExtensions.push_back( "m" );
Yves Starreveld's avatar
   
Yves Starreveld committed
45
  m_SourceFileExtensions.push_back( "mm" );
46
47

  m_HeaderFileExtensions.push_back( "h" );
48
  m_HeaderFileExtensions.push_back( "txx" );
49
  m_HeaderFileExtensions.push_back( "in" );
50
  
51
  m_DefineFlags = " ";
Ken Martin's avatar
Ken Martin committed
52
  m_LocalGenerator = 0;
53
54
55
  this->AddSourceGroup("", "^.*$");
  this->AddSourceGroup("Source Files", "\\.(cpp|C|c|cxx|rc|def|r|odl|idl|hpj|bat)$");
  this->AddSourceGroup("Header Files", "\\.(h|hh|hpp|hxx|hm|inl)$");
56
  this->AddDefaultDefinitions();
57
58
}

59
60
unsigned int cmMakefile::GetCacheMajorVersion()
{
Ken Martin's avatar
Ken Martin committed
61
  if(!this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"))
62
63
64
    {
    return 0;
    }
Ken Martin's avatar
Ken Martin committed
65
  return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MAJOR_VERSION"));
66
67
68
69
}

unsigned int cmMakefile::GetCacheMinorVersion()
{
Ken Martin's avatar
Ken Martin committed
70
  if(!this->GetCacheManager()->GetCacheValue("Cmake_Cache_MINOR_VERSION"))
71
72
73
    {
    return 0;
    }
Ken Martin's avatar
Ken Martin committed
74
  return atoi(this->GetCacheManager()->GetCacheValue("CMAKE_CACHE_MINOR_VERSION"));
75
76
77
}


78
79
cmMakefile::~cmMakefile()
{
80
81
82
83
84
  for(std::vector<cmSourceFile*>::iterator i = m_SourceFiles.begin();
      i != m_SourceFiles.end(); ++i)
    {
    delete *i;
    }
85
  for(unsigned int i=0; i < m_UsedCommands.size(); i++)
86
    {
87
    delete m_UsedCommands[i];
88
    }
89
90
91
92
93
94
95
96
  for(DataMap::const_iterator d = m_DataMap.begin();
      d != m_DataMap.end(); ++d)
    {
    if(d->second)
      {
      delete d->second;
      }
    }
Bill Hoffman's avatar
Bill Hoffman committed
97
  std::list<cmFunctionBlocker *>::iterator pos;
Bill Hoffman's avatar
Bill Hoffman committed
98
  for (pos = m_FunctionBlockers.begin(); 
Ken Martin's avatar
Ken Martin committed
99
       pos != m_FunctionBlockers.end(); ++pos)
Bill Hoffman's avatar
Bill Hoffman committed
100
101
102
103
    {
    cmFunctionBlocker* b = *pos;
    delete b;
    }
Ken Martin's avatar
Ken Martin committed
104
  m_FunctionBlockers.clear();
105
106
}

107
void cmMakefile::PrintStringVector(const char* s, const std::vector<std::string>& v) const
108
109
{
  std::cout << s << ": ( \n";
110
  for(std::vector<std::string>::const_iterator i = v.begin();
111
112
113
114
115
      i != v.end(); ++i)
    {
    std::cout << (*i).c_str() << " ";
    }
  std::cout << " )\n";
116
117
118
119
}


// call print on all the classes in the makefile
120
void cmMakefile::Print() const
121
{
122
  // print the class lists
123
  std::cout << "classes:\n";
124

125
126
127
128
129
130
131
  std::cout << " m_Targets: ";
  for (cmTargets::const_iterator l = m_Targets.begin();
       l != m_Targets.end(); l++)
    {
    std::cout << l->first << std::endl;
    }

Ken Martin's avatar
Ken Martin committed
132
133
134
135
136
137
  std::cout << " m_CurrentOutputDirectory; " << 
    m_CurrentOutputDirectory.c_str() << std::endl;
  std::cout << " m_StartOutputDirectory; " << 
    m_StartOutputDirectory.c_str() << std::endl;
  std::cout << " m_HomeOutputDirectory; " << 
    m_HomeOutputDirectory.c_str() << std::endl;
138
139
  std::cout << " m_cmCurrentDirectory; " << 
    m_cmCurrentDirectory.c_str() << std::endl;
Ken Martin's avatar
Ken Martin committed
140
141
142
143
  std::cout << " m_cmStartDirectory; " << 
    m_cmStartDirectory.c_str() << std::endl;
  std::cout << " m_cmHomeDirectory; " << 
    m_cmHomeDirectory.c_str() << std::endl;
144
145
146
147
  std::cout << " m_ProjectName;	" <<  m_ProjectName.c_str() << std::endl;
  this->PrintStringVector("m_SubDirectories ", m_SubDirectories); 
  this->PrintStringVector("m_IncludeDirectories;", m_IncludeDirectories);
  this->PrintStringVector("m_LinkDirectories", m_LinkDirectories);
148
149
150
151
152
  for( std::vector<cmSourceGroup>::const_iterator i = m_SourceGroups.begin();
       i != m_SourceGroups.end(); ++i)
    {
    i->Print();
    }
153
154
}

155
156
bool cmMakefile::CommandExists(const char* name) const
{
157
  return m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->CommandExists(name);
158
}
Ken Martin's avatar
Ken Martin committed
159
      
Ken Martin's avatar
Ken Martin committed
160
void cmMakefile::ExecuteCommand(std::string const &name,
161
                                std::vector<std::string> const& arguments)
Ken Martin's avatar
Ken Martin committed
162
{
163
164
165
166
167
168
  // quick return if blocked
  if(this->IsFunctionBlocked(name.c_str(), arguments))
    {
    return;
    }
  // execute the command
169
170
171
  cmCommand *rm = 
    m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->GetCommand(name.c_str());
  if(rm)
Ken Martin's avatar
Ken Martin committed
172
173
174
175
176
177
178
179
180
181
    {
    cmCommand* usedCommand = rm->Clone();
    usedCommand->SetMakefile(this);
    bool keepCommand = false;
    if(usedCommand->GetEnabled())
      {
      // if not running in inherit mode or
      // if the command is inherited then InitialPass it.
      if(!m_Inheriting || usedCommand->IsInherited())
        {
182
183
184
        std::vector<std::string> expandedArguments;
        for(std::vector<std::string>::const_iterator i = arguments.begin();
            i != arguments.end(); ++i)
185
          {
186
187
188
189
190
191
192
          std::string tmps = *i;
          this->ExpandVariablesInString(tmps);
          if (tmps.find_first_not_of(" ") != std::string::npos)
            {
            // we found something in the args
            expandedArguments.push_back(tmps);
            }
193
194
          }
        if(!usedCommand->InitialPass(expandedArguments))
Ken Martin's avatar
Ken Martin committed
195
          {
196
197
198
199
200
201
202
	  std::string error;
	  error = usedCommand->GetName();
	  error += ": Error : \n";
	  error += usedCommand->GetError();
	  error += " from CMakeLists.txt file in directory: ";
	  error += m_cmCurrentDirectory;
          cmSystemTools::Error(error.c_str());
Ken Martin's avatar
Ken Martin committed
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
          }
        else
          {
          // use the command
          keepCommand = true;
          m_UsedCommands.push_back(usedCommand);
          }
        }
      }
    // if the Cloned command was not used 
    // then delete it
    if(!keepCommand)
      {
      delete usedCommand;
      }
    }
219
220
221
222
223
224
225
  else if((name == "CABLE_WRAP_TCL") || (name == "CABLE_CLASS_SET") ||
          (name == "CONFIGURE_GCCXML"))
    {
    cmSystemTools::Error("The command ", name.c_str(),
                         " is not implemented in this version of CMake.\n"
                         "Contact cable@public.kitware.com for more information.");
    }
Ken Martin's avatar
Ken Martin committed
226
227
  else
    {
228
229
230
    cmSystemTools::Error("unknown CMake command:", name.c_str(), 
                         "\nReading cmake file in directory:" , 
                         m_cmCurrentDirectory.c_str());
Ken Martin's avatar
Ken Martin committed
231
232
233
    }
}

234
// Parse the given CMakeLists.txt file into a list of classes.
Ken Martin's avatar
Ken Martin committed
235
236
// Reads in current CMakeLists file and all parent CMakeLists files
// executing all inherited commands in the parents
Anonymous's avatar
Anonymous committed
237
238
239
240
241
242
243
//
//   if external is non-zero, this means that we have branched to grab some
//   commands from a remote list-file (that is, the equivalent of a
//   #include has been called).  We DO NOT look at the parents of this
//   list-file, and for all other purposes, the name of this list-file
//   is "filename" and not "external".
bool cmMakefile::ReadListFile(const char* filename, const char* external)
244
{
245
246
247
248
249
  // used to watch for blockers going out of scope
  // e.g. mismatched IF statement
  std::set<cmFunctionBlocker *> originalBlockers;

      // keep track of the current file being read
250
251
  if (filename)
    {
Bill Hoffman's avatar
Bill Hoffman committed
252
253
254
255
    if(m_cmCurrentListFile != filename)
      {
      m_cmCurrentListFile = filename;
      }
256
    // loop over current function blockers and record them
Bill Hoffman's avatar
Bill Hoffman committed
257
    std::list<cmFunctionBlocker *>::iterator pos;
258
259
260
261
262
    for (pos = m_FunctionBlockers.begin(); 
         pos != m_FunctionBlockers.end(); ++pos)
      {
      originalBlockers.insert(*pos);
      }
263
    }
264
  
Anonymous's avatar
Anonymous committed
265
266
267
268
  // if this is not a remote makefile
  //  (if it were, this would be called from the "filename" call,
  //   rather than the "external" call)
  if (!external)
269
    {
Anonymous's avatar
Anonymous committed
270
271
272
273
274
275
      // is there a parent CMakeLists file that does not go beyond the
      // Home directory? if so recurse and read in that List file 
      std::string parentList = this->GetParentListFileName(filename);
      if (parentList != "")
	{
	  std::string srcdir = m_cmCurrentDirectory;
276
277
278
279
280
281
282
283
284
          std::string bindir = m_CurrentOutputDirectory;

	  std::string::size_type pos = parentList.rfind('/');

          m_cmCurrentDirectory = parentList.substr(0, pos);
	  m_CurrentOutputDirectory = m_HomeOutputDirectory + parentList.substr(m_cmHomeDirectory.size(), pos - m_cmHomeDirectory.size());

	  // if not found, oops
	  if(pos == std::string::npos)
Anonymous's avatar
Anonymous committed
285
	    {
286
            cmSystemTools::Error("Trailing slash not found");
Anonymous's avatar
Anonymous committed
287
	    }
288

Anonymous's avatar
Anonymous committed
289
	  this->ReadListFile(parentList.c_str());
290

Anonymous's avatar
Anonymous committed
291
292
293
294
	  // restore the current directory
	  m_cmCurrentDirectory = srcdir;
	  m_CurrentOutputDirectory = bindir;    
	}
295
    }
Ken Martin's avatar
Ken Martin committed
296
297
298

  // are we at the start CMakeLists file or are we processing a parent 
  // lists file
Anonymous's avatar
Anonymous committed
299
300
301
  //
  //   this might, or might not be true, irrespective if we are
  //   off looking at an external makefile.
Ken Martin's avatar
Ken Martin committed
302
  m_Inheriting = (m_cmCurrentDirectory != m_cmStartDirectory);
Ken Martin's avatar
Ken Martin committed
303
                    
304
  // Now read the input file
Anonymous's avatar
Anonymous committed
305
306
307
  const char *filenametoread= filename;

  if( external)
308
    {
Anonymous's avatar
Anonymous committed
309
    filenametoread= external;
310
    }
311
312
313
314

  cmListFile* lf = 
    cmListFileCache::GetInstance()->GetFileCache(filenametoread);
  if(!lf)
315
316
317
    {
    return false;
    }
318
319
  // add this list file to the list of dependencies
  m_ListFiles.push_back( filenametoread);
320
321
  const size_t numberFunctions = lf->m_Functions.size();
  for(size_t i =0; i < numberFunctions; ++i)
322
    {
323
    cmListFileFunction& curFunction = lf->m_Functions[i];
324
325
    this->ExecuteCommand(curFunction.m_Name,
                         curFunction.m_Arguments);
326
    }
Ken Martin's avatar
Ken Martin committed
327
328
329
330
331

  // send scope ended to and funciton blockers
  if (filename)
    {
    // loop over all function blockers to see if any block this command
Bill Hoffman's avatar
Bill Hoffman committed
332
    std::list<cmFunctionBlocker *>::iterator pos;
Ken Martin's avatar
Ken Martin committed
333
334
335
    for (pos = m_FunctionBlockers.begin(); 
         pos != m_FunctionBlockers.end(); ++pos)
      {
336
337
338
339
340
341
      // if this blocker was not in the original then send a 
      // scope ended message
      if (originalBlockers.find(*pos) == originalBlockers.end())
        {
        (*pos)->ScopeEnded(*this);
        }
Ken Martin's avatar
Ken Martin committed
342
343
344
      }
    }
  
345
346
  return true;
}
347

348

349
void cmMakefile::AddCommand(cmCommand* wg)
350
{
351
  m_LocalGenerator->GetGlobalGenerator()->GetCMakeInstance()->AddCommand(wg);
352
353
354
}

  // Set the make file 
Ken Martin's avatar
Ken Martin committed
355
void cmMakefile::SetLocalGenerator(cmLocalGenerator* lg)
356
{
Ken Martin's avatar
Ken Martin committed
357
  m_LocalGenerator = lg;
358
359
}

360
void cmMakefile::FinalPass()
361
362
{
  // do all the variable expansions here
363
  this->ExpandVariables();
364

365
  // give all the commands a chance to do something
366
  // after the file has been parsed before generation
367
368
  for(std::vector<cmCommand*>::iterator i = m_UsedCommands.begin();
      i != m_UsedCommands.end(); ++i)
369
370
    {
    (*i)->FinalPass();
371
    }
372
373
374
375

}

  // Generate the output file
376
void cmMakefile::ConfigureFinalPass()
377
378
{
  this->FinalPass();
379
380
381
  const char* versionValue
    = this->GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  bool oldVersion = (!versionValue || atof(versionValue) < 1.4);
382
  // merge libraries
383
  
384
385
386
  for (cmTargets::iterator l = m_Targets.begin();
       l != m_Targets.end(); l++)
    {
Bill Hoffman's avatar
Bill Hoffman committed
387
    l->second.GenerateSourceFilesFromSourceLists(*this);
388
389
390
391
392
    // pick up any LINK_LIBRARIES that were added after the target
    if(oldVersion)
      {
      this->AddGlobalLinkInformation(l->first.c_str(), l->second);
      }
393
    l->second.AnalyzeLibDependencies(*this);
394
    }
395
396
}

397

398
399
void cmMakefile::AddCustomCommand(const char* source,
                                  const char* command,
Ken Martin's avatar
Ken Martin committed
400
                                  const std::vector<std::string>& commandArgs,
401
                                  const std::vector<std::string>& depends,
402
403
                                  const std::vector<std::string>& outputs,
                                  const char *target) 
404
{
405
406
407
  // find the target, 
  if (m_Targets.find(target) != m_Targets.end())
    {
408
409
410
    std::string expandC = command;
    this->ExpandVariablesInString(expandC);
    std::string c = cmSystemTools::EscapeSpaces(expandC.c_str());
Ken Martin's avatar
Ken Martin committed
411
412

    std::string combinedArgs;
Bill Hoffman's avatar
Bill Hoffman committed
413
    unsigned int i;
Ken Martin's avatar
Ken Martin committed
414
415
416
417
418
419
420
421
    
    for (i = 0; i < commandArgs.size(); ++i)
      {
      combinedArgs += cmSystemTools::EscapeSpaces(commandArgs[i].c_str());
      combinedArgs += " ";
      }
    
    cmCustomCommand cc(source,c.c_str(),combinedArgs.c_str(),depends,outputs);
422
    m_Targets[target].GetCustomCommands().push_back(cc);
423
424
    std::string cacheCommand = command;
    this->ExpandVariablesInString(cacheCommand);
Ken Martin's avatar
Ken Martin committed
425
    if(this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()))
426
427
      {
      m_Targets[target].AddUtility(
Ken Martin's avatar
Ken Martin committed
428
        this->GetCacheManager()->GetCacheValue(cacheCommand.c_str()));
429
      }
430
    }
431
}
432

433
void cmMakefile::AddCustomCommand(const char* source,
434
                                  const char* command,
Ken Martin's avatar
Ken Martin committed
435
                                  const std::vector<std::string>& commandArgs,
436
                                  const std::vector<std::string>& depends,
437
438
                                  const char* output, 
                                  const char *target) 
439
{
440
441
  std::vector<std::string> outputs;
  outputs.push_back(output);
442
  this->AddCustomCommand(source, command, commandArgs, depends, outputs, target);
443
444
445
446
447
448
449
450
}

void cmMakefile::AddDefineFlag(const char* flag)
{
  m_DefineFlags += " ";
  m_DefineFlags += flag;
}

451

452
void cmMakefile::AddLinkLibrary(const char* lib, cmTarget::LinkLibraryType llt)
453
454
{
  m_LinkLibraries.push_back(
455
456
457
458
459
460
	  std::pair<std::string, cmTarget::LinkLibraryType>(lib,llt));
}

void cmMakefile::AddLinkLibraryForTarget(const char *target,
                                         const char* lib, 
                                         cmTarget::LinkLibraryType llt)
461
462
463
{ 
  cmTargets::iterator i = m_Targets.find(target);
  if ( i != m_Targets.end())
464
    {
465
    i->second.AddLinkLibrary( *this, target, lib, llt );
466
    }
Berk Geveci's avatar
Berk Geveci committed
467
468
469
470
  else
    {
    cmSystemTools::Error("Attempt to add link libraries to non-existant target: ",  target, " for lib ", lib);
    }
471
472
}

473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
void cmMakefile::AddLinkDirectoryForTarget(const char *target,
                                           const char* d)
{
  cmTargets::iterator i = m_Targets.find(target);
  if ( i != m_Targets.end())
    {
    i->second.AddLinkDirectory( d );
    }
  else
    {
    cmSystemTools::Error("Attempt to add link directories to non-existant target: ", 
                         target, " for directory ", d);
    }
}

488
489
void cmMakefile::AddLinkLibrary(const char* lib)
{
490
  this->AddLinkLibrary(lib,cmTarget::GENERAL);
491
492
493
494
}

void cmMakefile::AddLinkDirectory(const char* dir)
{
495
496
497
498
  // Don't add a link directory that is already present.  Yes, this
  // linear search results in n^2 behavior, but n won't be getting
  // much bigger than 20.  We cannot use a set because of order
  // dependency of the link search path.
499
500
501
502
503
504
505
506
507
508
509
510
511
  
  // remove trailing slashes
  if(dir && dir[strlen(dir)-1] == '/')
    {
    std::string newdir = dir;
    newdir = newdir.substr(0, newdir.size()-1);
    if(std::find(m_LinkDirectories.begin(),
                 m_LinkDirectories.end(), newdir.c_str()) == m_LinkDirectories.end())
      {
      m_LinkDirectories.push_back(newdir);
      }
    }
  else
512
    {
513
514
515
516
517
    if(std::find(m_LinkDirectories.begin(),
                 m_LinkDirectories.end(), dir) == m_LinkDirectories.end())
      {
      m_LinkDirectories.push_back(dir);
      }
518
    }
519
520
521
522
523
524
525
}

void cmMakefile::AddSubDirectory(const char* sub)
{
  m_SubDirectories.push_back(sub);
}

526
void cmMakefile::AddIncludeDirectory(const char* inc, bool before)
527
{
528
529
530
531
532
533
534
  // Don't add an include directory that is already present.  Yes,
  // this linear search results in n^2 behavior, but n won't be
  // getting much bigger than 20.  We cannot use a set because of
  // order dependency of the include path.
  if(std::find(m_IncludeDirectories.begin(),
               m_IncludeDirectories.end(), inc) == m_IncludeDirectories.end())
    {
535
536
537
538
539
540
541
542
543
    if (before)
      {
      // WARNING: this *is* expensive (linear time) since it's a vector
      m_IncludeDirectories.insert(m_IncludeDirectories.begin(), inc);
      }
    else
      {
      m_IncludeDirectories.push_back(inc);
      }
544
    }
545
546
}

547

548
549
void cmMakefile::AddDefinition(const char* name, const char* value)
{
550
551
552
553
  if (!value )
    {
    return;
    }
554
  m_Definitions.erase( DefinitionMap::key_type(name));
555
556
  m_Definitions.insert(DefinitionMap::value_type(name, value));
}
557

558
559
560
561
562

void cmMakefile::AddCacheDefinition(const char* name, const char* value, 
                                    const char* doc,
                                    cmCacheManager::CacheEntryType type)
{
Ken Martin's avatar
Ken Martin committed
563
  this->GetCacheManager()->AddCacheEntry(name, value, doc, type);
564
565
566
567
  this->AddDefinition(name, value);
}


568
569
570
571
void cmMakefile::AddDefinition(const char* name, bool value)
{
  if(value)
    {
572
    m_Definitions.erase( DefinitionMap::key_type(name));
573
574
575
576
    m_Definitions.insert(DefinitionMap::value_type(name, "ON"));
    }
  else
    {
577
    m_Definitions.erase( DefinitionMap::key_type(name));
578
579
580
    m_Definitions.insert(DefinitionMap::value_type(name, "OFF"));
    }
}
581

582
583
584

void cmMakefile::AddCacheDefinition(const char* name, bool value, const char* doc)
{
Ken Martin's avatar
Ken Martin committed
585
  this->GetCacheManager()->AddCacheEntry(name, value, doc);
586
587
588
  this->AddDefinition(name, value);
}

589
590
591
592
593
void cmMakefile::RemoveDefinition(const char* name)
{
  m_Definitions.erase(DefinitionMap::key_type(name));
}

594
595
596
597
598
void cmMakefile::SetProjectName(const char* p)
{
  m_ProjectName = p;
}

599
600
601

void cmMakefile::AddGlobalLinkInformation(const char* name, cmTarget& target)
{
602
603
604
  // for these targets do not add anything
  switch(target.GetType())
    {
Brad King's avatar
Brad King committed
605
606
607
608
    case cmTarget::UTILITY: return;
    case cmTarget::INSTALL_FILES: return;
    case cmTarget::INSTALL_PROGRAMS: return;
    default:;
609
    }
610
611
612
613
614
615
  std::vector<std::string>::iterator j;
  for(j = m_LinkDirectories.begin();
      j != m_LinkDirectories.end(); ++j)
    {
    target.AddLinkDirectory(j->c_str());
    }
616
  target.MergeLinkLibraries( *this, name, m_LinkLibraries );
617
618
619
}


Yves Starreveld's avatar
   
Yves Starreveld committed
620
void cmMakefile::AddLibrary(const char* lname, int shared,
621
                            const std::vector<std::string> &srcs)
622
623
{
  cmTarget target;
Yves Starreveld's avatar
   
Yves Starreveld committed
624
625
626
627
628
629
630
631
632
633
634
635
636
637
  switch (shared)
    {
    case 0:
      target.SetType(cmTarget::STATIC_LIBRARY);
      break;
    case 1:
      target.SetType(cmTarget::SHARED_LIBRARY);
      break;
    case 2:
      target.SetType(cmTarget::MODULE_LIBRARY);
      break;
    default:
      target.SetType(cmTarget::STATIC_LIBRARY);
    }
Bill Hoffman's avatar
Bill Hoffman committed
638
639
640
641
642
  // Clear its dependencies. Otherwise, dependencies might persist
  // over changes in CMakeLists.txt, making the information stale and
  // hence useless.
  std::string depname = lname;
  depname += "_LIB_DEPENDS";
Ken Martin's avatar
Ken Martin committed
643
  this->GetCacheManager()->
Bill Hoffman's avatar
Bill Hoffman committed
644
    AddCacheEntry(depname.c_str(), "",
645
                  "Dependencies for target", cmCacheManager::STATIC);
Yves Starreveld's avatar
   
Yves Starreveld committed
646

Bill Hoffman's avatar
Bill Hoffman committed
647
  
Ken Martin's avatar
Ken Martin committed
648
  target.SetInAll(true);
649
  target.GetSourceLists() = srcs;
650
  this->AddGlobalLinkInformation(lname, target);
651
  m_Targets.insert(cmTargets::value_type(lname,target));
652
653

  // Add an entry into the cache 
654
655
  std::string libPath = lname;
  libPath += "_CMAKE_PATH";
Ken Martin's avatar
Ken Martin committed
656
  this->GetCacheManager()->
657
    AddCacheEntry(libPath.c_str(),
658
659
                  this->GetCurrentOutputDirectory(),
                  "Path to a library", cmCacheManager::INTERNAL);
660
661
662
663

  // Add an entry into the cache
  std::string ltname = lname;
  ltname += "_LIBRARY_TYPE";
Yves Starreveld's avatar
   
Yves Starreveld committed
664
665
666
  switch (shared)
    {
    case 0:
Ken Martin's avatar
Ken Martin committed
667
      this->GetCacheManager()->AddCacheEntry(ltname.c_str(),"STATIC",
Yves Starreveld's avatar
   
Yves Starreveld committed
668
669
670
671
		      "Whether a library is static, shared or module.",
		      cmCacheManager::INTERNAL);
      break;
    case 1:
Ken Martin's avatar
Ken Martin committed
672
      this->GetCacheManager()->
Yves Starreveld's avatar
   
Yves Starreveld committed
673
674
675
676
677
678
	AddCacheEntry(ltname.c_str(),
		      "SHARED",
		      "Whether a library is static, shared or module.",
		      cmCacheManager::INTERNAL);
      break;
    case 2:
Ken Martin's avatar
Ken Martin committed
679
      this->GetCacheManager()->
Yves Starreveld's avatar
   
Yves Starreveld committed
680
681
682
683
684
685
	AddCacheEntry(ltname.c_str(),
		      "MODULE",
		      "Whether a library is static, shared or module.",
		      cmCacheManager::INTERNAL);
      break;
    default:
Ken Martin's avatar
Ken Martin committed
686
      this->GetCacheManager()->
Yves Starreveld's avatar
   
Yves Starreveld committed
687
688
689
690
691
	AddCacheEntry(ltname.c_str(),
		      "STATIC",
		      "Whether a library is static, shared or module.",
		      cmCacheManager::INTERNAL);
    }
692

693
694
695
696
}

void cmMakefile::AddExecutable(const char *exeName, 
                               const std::vector<std::string> &srcs)
697
698
699
700
701
702
703
{
  this->AddExecutable(exeName,srcs,false);
}

void cmMakefile::AddExecutable(const char *exeName, 
                               const std::vector<std::string> &srcs,
                               bool win32)
704
{
705
  cmTarget target;
706
707
708
709
710
711
712
713
  if (win32)
    {
    target.SetType(cmTarget::WIN32_EXECUTABLE);
    }
  else
    {
    target.SetType(cmTarget::EXECUTABLE);
    }
Ken Martin's avatar
Ken Martin committed
714
  target.SetInAll(true);
715
  target.GetSourceLists() = srcs;
716
  this->AddGlobalLinkInformation(exeName, target);
717
718
  m_Targets.insert(cmTargets::value_type(exeName,target));
  
719
720
  
  // Add an entry into the cache 
721
722
  std::string exePath = exeName;
  exePath += "_CMAKE_PATH";
Ken Martin's avatar
Ken Martin committed
723
  this->GetCacheManager()->
724
    AddCacheEntry(exePath.c_str(),
725
726
                  this->GetCurrentOutputDirectory(),
                  "Path to an executable", cmCacheManager::INTERNAL);
727
728
}

729

730
void cmMakefile::AddUtilityCommand(const char* utilityName,
731
                                   const char* command,
732
                                   const char* arguments,
733
734
735
                                   bool all)
{
  std::vector<std::string> empty;
736
  this->AddUtilityCommand(utilityName,command,arguments,all,
737
738
739
740
741
                          empty,empty);
}

void cmMakefile::AddUtilityCommand(const char* utilityName,
                                   const char* command,
742
                                   const char* arguments,
743
744
745
                                   bool all,
                                   const std::vector<std::string> &dep,
                                   const std::vector<std::string> &out)
746
747
748
{
  cmTarget target;
  target.SetType(cmTarget::UTILITY);
749
  target.SetInAll(all);
750
  cmCustomCommand cc(utilityName, command, arguments, dep, out);
751
752
753
754
755
  target.GetCustomCommands().push_back(cc);
  m_Targets.insert(cmTargets::value_type(utilityName,target));
}


756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
void cmMakefile::AddSourceGroup(const char* name, const char* regex)
{
  // First see if the group exists.  If so, replace its regular expression.
  for(std::vector<cmSourceGroup>::iterator sg = m_SourceGroups.begin();
      sg != m_SourceGroups.end(); ++sg)
    {
    std::string sgName = sg->GetName();
    if(sgName == name)
      {
      // We only want to set the regular expression.  If there are already
      // source files in the group, we don't want to remove them.
      sg->SetGroupRegex(regex);
      return;
      }
    }
  
  // The group doesn't exist.  Add it.
  m_SourceGroups.push_back(cmSourceGroup(name, regex));
}
775
776
777
778
779

void cmMakefile::AddExtraDirectory(const char* dir)
{
  m_AuxSourceDirectories.push_back(dir);
}
780

781

Ken Martin's avatar
Ken Martin committed
782
783
784
785
786
// return the file name for the parent CMakeLists file to the 
// one passed in. Zero is returned if the CMakeLists file is the
// one in the home directory or if for some reason a parent cmake lists 
// file cannot be found.
std::string cmMakefile::GetParentListFileName(const char *currentFileName)
787
{
Ken Martin's avatar
Ken Martin committed
788
789
790
791
792
793
  // extract the directory name
  std::string parentFile;
  std::string listsDir = currentFileName;
  std::string::size_type pos = listsDir.rfind('/');
  // if we could not find the directory return 0
  if(pos == std::string::npos)
794
    {
Ken Martin's avatar
Ken Martin committed
795
    return parentFile;
796
    }
Ken Martin's avatar
Ken Martin committed
797
798
799
800
  listsDir = listsDir.substr(0, pos);
  
  // if we are in the home directory then stop, return 0
  if(m_cmHomeDirectory == listsDir)
801
    {
Ken Martin's avatar
Ken Martin committed
802
    return parentFile;
803
    }
Ken Martin's avatar
Ken Martin committed
804
805
806
807
808
809
810
811
812
813
814
815
816
  
  // is there a parent directory we can check
  pos = listsDir.rfind('/');
  // if we could not find the directory return 0
  if(pos == std::string::npos)
    {
    return parentFile;
    }
  listsDir = listsDir.substr(0, pos);
  
  // is there a CMakeLists.txt file in the parent directory ?
  parentFile = listsDir;
  parentFile += "/CMakeLists.txt";
817
  while(!cmSystemTools::FileExists(parentFile.c_str()))
818
    {
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
    // There is no CMakeLists.txt file in the parent directory.  This
    // can occur when coming out of a subdirectory resulting from a
    // SUBDIRS(Foo/Bar) command (coming out of Bar into Foo).  Try
    // walking up until a CMakeLists.txt is found or the home
    // directory is hit.
    
    // if we are in the home directory then stop, return 0
    if(m_cmHomeDirectory == listsDir) { return ""; }
    
    // is there a parent directory we can check
    pos = listsDir.rfind('/');
    // if we could not find the directory return 0
    if(pos == std::string::npos) { return ""; }
    listsDir = listsDir.substr(0, pos);
    parentFile = listsDir;
    parentFile += "/CMakeLists.txt";
835
    }
836

Ken Martin's avatar
Ken Martin committed
837
  return parentFile;
838
839
}

840
841
842
// expance CMAKE_BINARY_DIR and CMAKE_SOURCE_DIR in the
// include and library directories.

843
void cmMakefile::ExpandVariables()
844
{
845
  // Now expand varibles in the include and link strings
846
847
  for(std::vector<std::string>::iterator d = m_IncludeDirectories.begin();
        d != m_IncludeDirectories.end(); ++d)
848
    {
849
    this->ExpandVariablesInString(*d);
850
    }
851
852
  for(std::vector<std::string>::iterator d = m_LinkDirectories.begin();
        d != m_LinkDirectories.end(); ++d)
853
    {
854
    this->ExpandVariablesInString(*d);
855
    }
856
857
  for(cmTarget::LinkLibraries::iterator l = m_LinkLibraries.begin();
      l != m_LinkLibraries.end(); ++l)
858
    {
859
    this->ExpandVariablesInString(l->first);
860
861
862
    }
}

863
bool cmMakefile::IsOn(const char* name) const
864
865
866
867
868
{
  const char* value = this->GetDefinition(name);
  return cmSystemTools::IsOn(value);
}

869
const char* cmMakefile::GetDefinition(const char* name) const
870
{
871
  DefinitionMap::const_iterator pos = m_Definitions.find(name);
872
873
874
875
  if(pos != m_Definitions.end())
    {
    return (*pos).second.c_str();
    }
Ken Martin's avatar
Ken Martin committed
876
  return this->GetCacheManager()->GetCacheValue(name);
877
}
878
879


880

881
const char *cmMakefile::ExpandVariablesInString(std::string& source) const
882
{
883
  return this->ExpandVariablesInString(source, false);
884
885
}

886
const char *cmMakefile::ExpandVariablesInString(std::string& source,
887
888
                                                bool escapeQuotes,
                                                bool atOnly) const
889
{
890
  // This method replaces ${VAR} and @VAR@ where VAR is looked up
891
  // with GetDefinition(), if not found in the map, nothing is expanded.
892
893
  // It also supports the $ENV{VAR} syntax where VAR is looked up in
  // the current environment variables.
894

895
  // start by look for $ or @ in the string
896
897
898
899
900
901
902
903
904
  std::string::size_type markerPos;
  if(atOnly)
    {
    markerPos = source.find_first_of("@");
    }
  else
    {
    markerPos = source.find_first_of("$@");
    }
905
906
907
  // if not found, or found as the last character, then leave quickly as
  // nothing needs to be expanded
  if((markerPos == std::string::npos) || (markerPos >= source.size()-1))
908
    {
909
    return source.c_str();
910
911
    }
  // current position
912
  std::string::size_type currentPos =0; // start at 0
913
914
  std::string result; // string with replacements
  // go until the the end of the string
915
  while((markerPos != std::string::npos) && (markerPos < source.size()-1))
916
917
918
919
920
    {
    // grab string from currentPos to the start of the variable
    // and add it to the result
    result += source.substr(currentPos, markerPos - currentPos);
    char endVariableMarker;     // what is the end of the variable @ or }
921
    int markerStartSize = 1;    // size of the start marker 1 or 2 or 5
922
    if(!atOnly && source[markerPos] == '$')
923
      {
924
925
926
927
928
929
      // ${var} case
      if(source[markerPos+1] == '{')
        {
        endVariableMarker = '}';
        markerStartSize = 2;
        }
930
      // $ENV{var} case
931
      else if(markerPos+4 < source.size() &&
932
              source[markerPos+4] == '{' &&
933
              !source.substr(markerPos+1, 3).compare("ENV"))
934
935
936
937
        {
        endVariableMarker = '}';
        markerStartSize = 5;
        }
938
939
940
941
942
943
944
      else
        {
        // bogus $ with no { so add $ to result and move on
        result += '$'; // add bogus $ back into string
        currentPos = markerPos+1; // move on
        endVariableMarker = ' '; // set end var to space so we can tell bogus
        }
945
      }
946
947
948
949
950
    else
      {
      // @VAR case
      endVariableMarker = '@';
      }
951
    // if it was a valid variable (started with @ or ${ or $ENV{ )
952
953
954
955
    if(endVariableMarker != ' ')
      {
      markerPos += markerStartSize; // move past marker
      // find the end variable marker starting at the markerPos
956
      std::string::size_type endVariablePos =
957
958
959
960
961
962
963
964
965
966
        source.find(endVariableMarker, markerPos);
      if(endVariablePos == std::string::npos)
        {
        // no end marker found so add the bogus start
        if(endVariableMarker == '@')
          {
          result += '@';
          }
        else
          {
967
          result += (markerStartSize == 5 ? "$ENV{" : "${");
968
969
970
971
972
973
974
          }
        currentPos = markerPos;
        }
      else
        {
        // good variable remove it
        std::string var = source.substr(markerPos, endVariablePos - markerPos);
975
976
        bool found = false;
        if (markerStartSize == 5) // $ENV{
977
          {
978
          char *ptr = getenv(var.c_str());
979
          if (ptr)
980
            {
981
982
983
984
985
986
987
988
989
            if (escapeQuotes)
              {
              result += cmSystemTools::EscapeQuotes(ptr);
              }
            else
              {
              result += ptr;
              }
            found = true;
990
            }
991
992
993
          }
        else
          {
994
995
          const char* lookup = this->GetDefinition(var.c_str());
          if(lookup)
996
            {
997
998
            if (escapeQuotes)
              {
999
              result += cmSystemTools::EscapeQuotes(lookup);
1000
              }
For faster browsing, not all history is shown. View entire blame