cmake.cxx 95.4 KB
Newer Older
1
2
3
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
Ken Martin's avatar
Ken Martin committed
4

5
6
  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.
Ken Martin's avatar
Ken Martin committed
7

8
9
10
11
  This software is distributed WITHOUT ANY WARRANTY; without even the
  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  See the License for more information.
============================================================================*/
Ken Martin's avatar
Ken Martin committed
12
#include "cmake.h"
Bill Hoffman's avatar
Bill Hoffman committed
13
#include "cmDocumentVariables.h"
Ken Martin's avatar
Ken Martin committed
14
#include "cmCacheManager.h"
Ken Martin's avatar
Ken Martin committed
15
16
#include "cmMakefile.h"
#include "cmLocalGenerator.h"
Alexander Neundorf's avatar
 
Alexander Neundorf committed
17
#include "cmExternalMakefileProjectGenerator.h"
Ken Martin's avatar
Ken Martin committed
18
19
#include "cmCommands.h"
#include "cmCommand.h"
20
#include "cmFileTimeComparison.h"
21
22
#include "cmSourceFile.h"
#include "cmTest.h"
23
#include "cmDocumentationFormatterText.h"
Ken Martin's avatar
Ken Martin committed
24

25
#if defined(CMAKE_BUILD_WITH_CMAKE)
26
# include "cmGraphVizWriter.h"
27
28
# include "cmVariableWatch.h"
#endif
29

Alexander Neundorf's avatar
 
Alexander Neundorf committed
30
31
#include <cmsys/Glob.hxx>
#include <cmsys/RegularExpression.hxx>
Ken Martin's avatar
Ken Martin committed
32

33
34
35
36
37
38
39
40
// only build kdevelop generator on non-windows platforms
// when not bootstrapping cmake
#if !defined(_WIN32)
# if defined(CMAKE_BUILD_WITH_CMAKE)
#   define CMAKE_USE_KDEVELOP
# endif
#endif

Alexander Neundorf's avatar
 
Alexander Neundorf committed
41
42
43
44
#if defined(CMAKE_BUILD_WITH_CMAKE)
#  define CMAKE_USE_ECLIPSE
#endif

45
46
47
48
#if defined(__MINGW32__) && !defined(CMAKE_BUILD_WITH_CMAKE)
# define CMAKE_BOOT_MINGW
#endif

Ken Martin's avatar
Ken Martin committed
49
50
// include the generator
#if defined(_WIN32) && !defined(__CYGWIN__)
51
52
#  if !defined(CMAKE_BOOT_MINGW)
#    include "cmGlobalVisualStudio6Generator.h"
53
54
55
#    include "cmGlobalVisualStudio7Generator.h"
#    include "cmGlobalVisualStudio71Generator.h"
#    include "cmGlobalVisualStudio8Generator.h"
56
#    include "cmGlobalVisualStudio9Generator.h"
57
#    include "cmGlobalVisualStudio10Generator.h"
58
#    include "cmGlobalVisualStudio11Generator.h"
59
#    include "cmGlobalVisualStudio12Generator.h"
60
61
#    include "cmGlobalBorlandMakefileGenerator.h"
#    include "cmGlobalNMakeMakefileGenerator.h"
62
#    include "cmGlobalJOMMakefileGenerator.h"
63
#    include "cmGlobalWatcomWMakeGenerator.h"
64
#    define CMAKE_HAVE_VS_GENERATORS
65
#  endif
66
67
#  include "cmGlobalMSYSMakefileGenerator.h"
#  include "cmGlobalMinGWMakefileGenerator.h"
Ken Martin's avatar
Ken Martin committed
68
#else
Ken Martin's avatar
Ken Martin committed
69
#endif
Ken Martin's avatar
Ken Martin committed
70
#include "cmGlobalUnixMakefileGenerator3.h"
71
#include "cmGlobalNinjaGenerator.h"
72

73
#if !defined(CMAKE_BOOT_MINGW)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
74
75
# include "cmExtraCodeBlocksGenerator.h"
#endif
76
#include "cmExtraSublimeTextGenerator.h"
Alexander Neundorf's avatar
 
Alexander Neundorf committed
77

78
79
80
#ifdef CMAKE_USE_KDEVELOP
# include "cmGlobalKdevelopGenerator.h"
#endif
Ken Martin's avatar
Ken Martin committed
81

Alexander Neundorf's avatar
 
Alexander Neundorf committed
82
83
84
85
#ifdef CMAKE_USE_ECLIPSE
# include "cmExtraEclipseCDT4Generator.h"
#endif

86
#include <stdlib.h> // required for atoi
87

88
89
90
91
92
#if defined( __APPLE__ )
#  if defined(CMAKE_BUILD_WITH_CMAKE)
#    include "cmGlobalXCodeGenerator.h"
#    define CMAKE_USE_XCODE 1
#  endif
93
94
95
#  include <sys/types.h>
#  include <sys/time.h>
#  include <sys/resource.h>
Ken Martin's avatar
Ken Martin committed
96
#endif
97

98
99
#include <sys/stat.h> // struct stat

100
static bool cmakeCheckStampFile(const char* stampName);
101
static bool cmakeCheckStampList(const char* stampName);
102

Andy Cedilnik's avatar
Andy Cedilnik committed
103
void cmNeedBackwardsCompatibility(const std::string& variable,
104
  int access_type, void*, const char*, const cmMakefile*)
105
{
106
#ifdef CMAKE_BUILD_WITH_CMAKE
107
108
  if (access_type == cmVariableWatch::UNKNOWN_VARIABLE_READ_ACCESS)
    {
Brad King's avatar
Brad King committed
109
    std::string message = "An attempt was made to access a variable: ";
110
    message += variable;
Andy Cedilnik's avatar
Andy Cedilnik committed
111
112
113
114
    message +=
      " that has not been defined. Some variables were always defined "
      "by CMake in versions prior to 1.6. To fix this you might need to set "
      "the cache value of CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less. If "
115
      "you are writing a CMakeLists file, (or have already set "
116
      "CMAKE_BACKWARDS_COMPATIBILITY to 1.4 or less) then you probably need "
Andy Cedilnik's avatar
Andy Cedilnik committed
117
118
      "to include a CMake module to test for the feature this variable "
      "defines.";
119
120
    cmSystemTools::Error(message.c_str());
    }
121
122
123
124
#else
  (void)variable;
  (void)access_type;
#endif
125
126
}

127
128
129
130
131
132
133
void cmWarnUnusedCliWarning(const std::string& variable,
  int, void* ctx, const char*, const cmMakefile*)
{
  cmake* cm = reinterpret_cast<cmake*>(ctx);
  cm->MarkCliAsUsed(variable);
}

Bill Hoffman's avatar
Bill Hoffman committed
134
135
cmake::cmake()
{
Bill Hoffman's avatar
Bill Hoffman committed
136
  this->Trace = false;
137
  this->WarnUninitialized = false;
138
  this->WarnUnused = false;
139
  this->WarnUnusedCli = true;
140
  this->CheckSystemVars = false;
141
  this->SuppressDevWarnings = false;
142
  this->DoSuppressDevWarnings = false;
143
  this->DebugOutput = false;
Ken Martin's avatar
Ken Martin committed
144
145
146
  this->DebugTryCompile = false;
  this->ClearBuildSystem = false;
  this->FileComparison = new cmFileTimeComparison;
Andy Cedilnik's avatar
Andy Cedilnik committed
147

148
  this->Policies = new cmPolicies();
149
  this->InitializeProperties();
150

151
152
153
154
#ifdef __APPLE__
  struct rlimit rlp;
  if(!getrlimit(RLIMIT_STACK, &rlp))
    {
155
    if(rlp.rlim_cur != rlp.rlim_max)
156
      {
157
        rlp.rlim_cur = rlp.rlim_max;
158
159
160
161
162
         setrlimit(RLIMIT_STACK, &rlp);
      }
    }
#endif

Ken Martin's avatar
Ken Martin committed
163
164
  this->Verbose = false;
  this->InTryCompile = false;
165
  this->CacheManager = new cmCacheManager(this);
Ken Martin's avatar
Ken Martin committed
166
167
168
  this->GlobalGenerator = 0;
  this->ProgressCallback = 0;
  this->ProgressCallbackClientData = 0;
169
  this->CurrentWorkingMode = NORMAL_MODE;
170

171
#ifdef CMAKE_BUILD_WITH_CMAKE
Ken Martin's avatar
Ken Martin committed
172
173
  this->VariableWatch = new cmVariableWatch;
  this->VariableWatch->AddWatch("CMAKE_WORDS_BIGENDIAN",
174
                            cmNeedBackwardsCompatibility);
Ken Martin's avatar
Ken Martin committed
175
  this->VariableWatch->AddWatch("CMAKE_SIZEOF_INT",
176
                            cmNeedBackwardsCompatibility);
Ken Martin's avatar
Ken Martin committed
177
  this->VariableWatch->AddWatch("CMAKE_X_LIBS",
178
                            cmNeedBackwardsCompatibility);
179
180
181
#endif

  this->AddDefaultGenerators();
Alexander Neundorf's avatar
 
Alexander Neundorf committed
182
  this->AddDefaultExtraGenerators();
183
184
  this->AddDefaultCommands();

185
186
  // Make sure we can capture the build tool output.
  cmSystemTools::EnableVSConsoleOutput();
Bill Hoffman's avatar
Bill Hoffman committed
187
188
}

189
190
cmake::~cmake()
{
Ken Martin's avatar
Ken Martin committed
191
  delete this->CacheManager;
192
  delete this->Policies;
Ken Martin's avatar
Ken Martin committed
193
  if (this->GlobalGenerator)
194
    {
Ken Martin's avatar
Ken Martin committed
195
196
    delete this->GlobalGenerator;
    this->GlobalGenerator = 0;
197
    }
Ken Martin's avatar
Ken Martin committed
198
199
  for(RegisteredCommandsMap::iterator j = this->Commands.begin();
      j != this->Commands.end(); ++j)
Ken Martin's avatar
Ken Martin committed
200
201
202
    {
    delete (*j).second;
    }
203
  for(RegisteredGeneratorsVector::iterator j = this->Generators.begin();
204
205
      j != this->Generators.end(); ++j)
    {
206
    delete *j;
207
    }
208
#ifdef CMAKE_BUILD_WITH_CMAKE
Ken Martin's avatar
Ken Martin committed
209
  delete this->VariableWatch;
210
#endif
Ken Martin's avatar
Ken Martin committed
211
  delete this->FileComparison;
Ken Martin's avatar
Ken Martin committed
212
213
}

214
215
216
217
void cmake::InitializeProperties()
{
  this->Properties.clear();
  this->Properties.SetCMakeInstance(this);
218
219
  this->AccessedProperties.clear();
  this->PropertyDefinitions.clear();
220
221

  // initialize properties
222
  cmCacheManager::DefineProperties(this);
223
224
225
226
227
228
229
  cmSourceFile::DefineProperties(this);
  cmTarget::DefineProperties(this);
  cmMakefile::DefineProperties(this);
  cmTest::DefineProperties(this);
  cmake::DefineProperties(this);
}

230
231
void cmake::CleanupCommandsAndMacros()
{
232
  this->InitializeProperties();
233
  std::vector<cmCommand*> commands;
Ken Martin's avatar
Ken Martin committed
234
235
  for(RegisteredCommandsMap::iterator j = this->Commands.begin();
      j != this->Commands.end(); ++j)
236
    {
237
    if ( !j->second->IsA("cmMacroHelperCommand") &&
238
         !j->second->IsA("cmFunctionHelperCommand"))
239
240
241
242
243
244
245
246
      {
      commands.push_back(j->second);
      }
    else
      {
      delete j->second;
      }
    }
Ken Martin's avatar
Ken Martin committed
247
  this->Commands.erase(this->Commands.begin(), this->Commands.end());
248
249
250
251
  std::vector<cmCommand*>::iterator it;
  for ( it = commands.begin(); it != commands.end();
    ++ it )
    {
Ken Martin's avatar
Ken Martin committed
252
    this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
253
254
255
    }
}

Ken Martin's avatar
Ken Martin committed
256
257
bool cmake::CommandExists(const char* name) const
{
258
  std::string sName = cmSystemTools::LowerCase(name);
Ken Martin's avatar
Ken Martin committed
259
  return (this->Commands.find(sName) != this->Commands.end());
260
261
}

Andy Cedilnik's avatar
Andy Cedilnik committed
262
cmCommand *cmake::GetCommand(const char *name)
Ken Martin's avatar
Ken Martin committed
263
264
{
  cmCommand* rm = 0;
265
  std::string sName = cmSystemTools::LowerCase(name);
Ken Martin's avatar
Ken Martin committed
266
267
  RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  if (pos != this->Commands.end())
Ken Martin's avatar
Ken Martin committed
268
269
270
271
272
273
    {
    rm = (*pos).second;
    }
  return rm;
}

274
275
276
void cmake::RenameCommand(const char*oldName, const char* newName)
{
  // if the command already exists, free the old one
277
278
  std::string sOldName = cmSystemTools::LowerCase(oldName);
  std::string sNewName = cmSystemTools::LowerCase(newName);
Ken Martin's avatar
Ken Martin committed
279
280
  RegisteredCommandsMap::iterator pos = this->Commands.find(sOldName);
  if ( pos == this->Commands.end() )
281
282
283
    {
    return;
    }
284
  cmCommand* cmd = pos->second;
285

Ken Martin's avatar
Ken Martin committed
286
287
  pos = this->Commands.find(sNewName);
  if (pos != this->Commands.end())
288
289
    {
    delete pos->second;
Ken Martin's avatar
Ken Martin committed
290
    this->Commands.erase(pos);
291
    }
Ken Martin's avatar
Ken Martin committed
292
293
294
  this->Commands.insert(RegisteredCommandsMap::value_type(sNewName, cmd));
  pos = this->Commands.find(sOldName);
  this->Commands.erase(pos);
295
296
}

Alexander Neundorf's avatar
 
Alexander Neundorf committed
297
298
299
300
301
302
303
304
305
306
307
void cmake::RemoveCommand(const char* name)
{
  std::string sName = cmSystemTools::LowerCase(name);
  RegisteredCommandsMap::iterator pos = this->Commands.find(sName);
  if ( pos != this->Commands.end() )
    {
    delete pos->second;
    this->Commands.erase(pos);
    }
}

Ken Martin's avatar
Ken Martin committed
308
309
void cmake::AddCommand(cmCommand* wg)
{
310
  std::string name = cmSystemTools::LowerCase(wg->GetName());
Ken Martin's avatar
Ken Martin committed
311
  // if the command already exists, free the old one
Ken Martin's avatar
Ken Martin committed
312
313
  RegisteredCommandsMap::iterator pos = this->Commands.find(name);
  if (pos != this->Commands.end())
Ken Martin's avatar
Ken Martin committed
314
315
    {
    delete pos->second;
Ken Martin's avatar
Ken Martin committed
316
    this->Commands.erase(pos);
Ken Martin's avatar
Ken Martin committed
317
    }
Ken Martin's avatar
Ken Martin committed
318
  this->Commands.insert( RegisteredCommandsMap::value_type(name, wg));
Ken Martin's avatar
Ken Martin committed
319
}
320

Alexander Neundorf's avatar
 
Alexander Neundorf committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343

void cmake::RemoveUnscriptableCommands()
{
  std::vector<std::string> unscriptableCommands;
  cmake::RegisteredCommandsMap* commands = this->GetCommands();
  for (cmake::RegisteredCommandsMap::const_iterator pos = commands->begin();
       pos != commands->end();
       ++pos)
    {
    if (!pos->second->IsScriptable())
      {
      unscriptableCommands.push_back(pos->first);
      }
    }

  for(std::vector<std::string>::const_iterator it=unscriptableCommands.begin();
      it != unscriptableCommands.end();
      ++it)
    {
    this->RemoveCommand(it->c_str());
    }
}

344
// Parse the args
345
bool cmake::SetCacheArgs(const std::vector<std::string>& args)
Andy Cedilnik's avatar
Andy Cedilnik committed
346
{
347
  bool findPackageMode = false;
348
349
350
351
352
353
  for(unsigned int i=1; i < args.size(); ++i)
    {
    std::string arg = args[i];
    if(arg.find("-D",0) == 0)
      {
      std::string entry = arg.substr(2);
354
355
      if(entry.size() == 0)
        {
356
357
358
359
360
361
362
        ++i;
        if(i < args.size())
          {
          entry = args[i];
          }
        else
          {
Alexander Neundorf's avatar
 
Alexander Neundorf committed
363
          cmSystemTools::Error("-D must be followed with VAR=VALUE.");
364
365
          return false;
          }
366
        }
367
      std::string var, value;
368
      cmCacheManager::CacheEntryType type = cmCacheManager::UNINITIALIZED;
369
      if(cmCacheManager::ParseEntry(entry.c_str(), var, value, type))
370
        {
371
372
373
374
375
376
        // The value is transformed if it is a filepath for example, so
        // we can't compare whether the value is already in the cache until
        // after we call AddCacheEntry.
        const char *cachedValue =
                              this->CacheManager->GetCacheValue(var.c_str());

Ken Martin's avatar
Ken Martin committed
377
        this->CacheManager->AddCacheEntry(var.c_str(), value.c_str(),
Alexander Neundorf's avatar
 
Alexander Neundorf committed
378
          "No help, variable specified on the command line.", type);
379
380
        if(this->WarnUnusedCli)
          {
381
382
383
384
385
386
          if (!cachedValue
              || strcmp(this->CacheManager->GetCacheValue(var.c_str()),
                        cachedValue) != 0)
            {
            this->WatchUnusedCli(var.c_str());
            }
387
          }
388
389
390
391
392
        }
      else
        {
        std::cerr << "Parse error in command line argument: " << arg << "\n"
                  << "Should be: VAR:type=value\n";
Alexander Neundorf's avatar
Alexander Neundorf committed
393
        cmSystemTools::Error("No cmake script provided.");
394
        return false;
Andy Cedilnik's avatar
Andy Cedilnik committed
395
        }
396
      }
397
398
    else if(arg.find("-Wno-dev",0) == 0)
      {
399
      this->SuppressDevWarnings = true;
400
      this->DoSuppressDevWarnings = true;
401
402
      }
    else if(arg.find("-Wdev",0) == 0)
403
      {
404
      this->SuppressDevWarnings = false;
405
      this->DoSuppressDevWarnings = true;
406
      }
Alexander Neundorf's avatar
 
Alexander Neundorf committed
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    else if(arg.find("-U",0) == 0)
      {
      std::string entryPattern = arg.substr(2);
      if(entryPattern.size() == 0)
        {
        ++i;
        if(i < args.size())
          {
          entryPattern = args[i];
          }
        else
          {
          cmSystemTools::Error("-U must be followed with VAR.");
          return false;
          }
        }
Alexander Neundorf's avatar
 
Alexander Neundorf committed
423
      cmsys::RegularExpression regex(
424
        cmsys::Glob::PatternToRegex(entryPattern.c_str(), true, true).c_str());
Alexander Neundorf's avatar
 
Alexander Neundorf committed
425
426
      //go through all cache entries and collect the vars which will be removed
      std::vector<std::string> entriesToDelete;
427
      cmCacheManager::CacheIterator it =
Alexander Neundorf's avatar
 
Alexander Neundorf committed
428
429
430
431
                                    this->CacheManager->GetCacheIterator();
      for ( it.Begin(); !it.IsAtEnd(); it.Next() )
        {
        cmCacheManager::CacheEntryType t = it.GetType();
Alexander Neundorf's avatar
 
Alexander Neundorf committed
432
        if(t != cmCacheManager::STATIC)
Alexander Neundorf's avatar
 
Alexander Neundorf committed
433
434
          {
          std::string entryName = it.GetName();
Alexander Neundorf's avatar
 
Alexander Neundorf committed
435
          if (regex.find(entryName.c_str()))
Alexander Neundorf's avatar
 
Alexander Neundorf committed
436
437
438
439
440
441
442
            {
            entriesToDelete.push_back(entryName);
            }
          }
        }

      // now remove them from the cache
443
444
      for(std::vector<std::string>::const_iterator currentEntry =
          entriesToDelete.begin();
Alexander Neundorf's avatar
 
Alexander Neundorf committed
445
446
447
448
449
450
          currentEntry != entriesToDelete.end();
          ++currentEntry)
        {
        this->CacheManager->RemoveCacheEntry(currentEntry->c_str());
        }
      }
451
452
453
    else if(arg.find("-C",0) == 0)
      {
      std::string path = arg.substr(2);
454
455
      if ( path.size() == 0 )
        {
456
457
458
459
460
461
462
463
464
465
        ++i;
        if(i < args.size())
          {
          path = args[i];
          }
        else
          {
          cmSystemTools::Error("-C must be followed by a file name.");
          return false;
          }
466
        }
467
      std::cerr << "loading initial cache file " << path.c_str() << "\n";
468
      this->ReadListFile(args, path.c_str());
Ken Martin's avatar
Ken Martin committed
469
      }
470
    else if(arg.find("-P",0) == 0)
471
      {
472
      i++;
473
474
475
476
477
      if(i >= args.size())
        {
        cmSystemTools::Error("-P must be followed by a file name.");
        return false;
        }
478
      std::string path = args[i];
479
480
      if ( path.size() == 0 )
        {
481
        cmSystemTools::Error("No cmake script provided.");
482
483
        return false;
        }
484
      this->ReadListFile(args, path.c_str());
485
      }
486
487
488
489
    else if (arg.find("--find-package",0) == 0)
      {
      findPackageMode = true;
      }
Ken Martin's avatar
Ken Martin committed
490
    }
491
492
493
494

  if (findPackageMode)
    {
    return this->FindPackage(args);
Ken Martin's avatar
Ken Martin committed
495
    }
496

497
  return true;
Ken Martin's avatar
Ken Martin committed
498
499
}

500
501
void cmake::ReadListFile(const std::vector<std::string>& args,
                         const char *path)
Ken Martin's avatar
Ken Martin committed
502
503
504
{
  // if a generator was not yet created, temporarily create one
  cmGlobalGenerator *gg = this->GetGlobalGenerator();
Ken Martin's avatar
Ken Martin committed
505
  bool created = false;
Andy Cedilnik's avatar
Andy Cedilnik committed
506

Ken Martin's avatar
Ken Martin committed
507
508
509
510
511
512
513
514
515
516
517
  // if a generator was not specified use a generic one
  if (!gg)
    {
    gg = new cmGlobalGenerator;
    gg->SetCMakeInstance(this);
    created = true;
    }

  // read in the list file to fill the cache
  if(path)
    {
518
    cmsys::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
519
520
521
522
523
524
525
526
    lg->GetMakefile()->SetHomeOutputDirectory
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
    lg->GetMakefile()->SetStartOutputDirectory
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
    lg->GetMakefile()->SetHomeDirectory
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
    lg->GetMakefile()->SetStartDirectory
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
527
    if (this->GetWorkingMode() != NORMAL_MODE)
528
529
530
531
      {
      std::string file(cmSystemTools::CollapseFullPath(path));
      cmSystemTools::ConvertToUnixSlashes(file);
      lg->GetMakefile()->SetScriptModeFile(file.c_str());
532
533

      lg->GetMakefile()->SetArgcArgv(args);
534
      }
535
    if (!lg->GetMakefile()->ReadListFile(0, path))
Ken Martin's avatar
Ken Martin committed
536
      {
537
      cmSystemTools::Error("Error processing file: ", path);
538
      }
539
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
540

Ken Martin's avatar
Ken Martin committed
541
542
543
544
545
  // free generic one if generated
  if (created)
    {
    delete gg;
    }
546
547
}

548
549
550

bool cmake::FindPackage(const std::vector<std::string>& args)
{
551
552
553
554
555
556
  // if a generator was not yet created, temporarily create one
  cmGlobalGenerator *gg = new cmGlobalGenerator;
  gg->SetCMakeInstance(this);
  this->SetGlobalGenerator(gg);

  // read in the list file to fill the cache
557
  cmsys::auto_ptr<cmLocalGenerator> lg(gg->CreateLocalGenerator());
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
  cmMakefile* mf = lg->GetMakefile();
  mf->SetHomeOutputDirectory
    (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  mf->SetStartOutputDirectory
    (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  mf->SetHomeDirectory
    (cmSystemTools::GetCurrentWorkingDirectory().c_str());
  mf->SetStartDirectory
    (cmSystemTools::GetCurrentWorkingDirectory().c_str());

  mf->SetArgcArgv(args);

  std::string systemFile = mf->GetModulesFile("CMakeFindPackageMode.cmake");
  mf->ReadListFile(0, systemFile.c_str());

  std::string language = mf->GetSafeDefinition("LANGUAGE");
  std::string mode = mf->GetSafeDefinition("MODE");
  std::string packageName = mf->GetSafeDefinition("NAME");
  bool packageFound = mf->IsOn("PACKAGE_FOUND");
  bool quiet = mf->IsOn("PACKAGE_QUIET");

  if (!packageFound)
    {
    if (!quiet)
      {
      printf("%s not found.\n", packageName.c_str());
      }
    }
  else if (mode == "EXIST")
    {
    if (!quiet)
      {
      printf("%s found.\n", packageName.c_str());
      }
    }
  else if (mode == "COMPILE")
    {
    std::string includes = mf->GetSafeDefinition("PACKAGE_INCLUDE_DIRS");
    std::vector<std::string> includeDirs;
    cmSystemTools::ExpandListArgument(includes, includeDirs);

599
    std::string includeFlags = lg->GetIncludeFlags(includeDirs, 0,
600
601
                                                   language.c_str(), false);

602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
    std::string definitions = mf->GetSafeDefinition("PACKAGE_DEFINITIONS");
    printf("%s %s\n", includeFlags.c_str(), definitions.c_str());
    }
  else if (mode == "LINK")
    {
    const char* targetName = "dummy";
    std::vector<std::string> srcs;
    cmTarget* tgt = mf->AddExecutable(targetName, srcs, true);
    tgt->SetProperty("LINKER_LANGUAGE", language.c_str());

    std::string libs = mf->GetSafeDefinition("PACKAGE_LIBRARIES");
    std::vector<std::string> libList;
    cmSystemTools::ExpandListArgument(libs, libList);
    for(std::vector<std::string>::const_iterator libIt=libList.begin();
            libIt != libList.end();
            ++libIt)
      {
Alexander Neundorf's avatar
Alexander Neundorf committed
619
620
      mf->AddLinkLibraryForTarget(targetName, libIt->c_str(),
                                  cmTarget::GENERAL);
621
622
623
624
      }


    std::string linkLibs;
625
626
    std::string frameworkPath;
    std::string linkPath;
627
628
    std::string flags;
    std::string linkFlags;
629
    cmGeneratorTarget gtgt(tgt);
630
631
632
    lg->GetTargetFlags(linkLibs, frameworkPath, linkPath, flags, linkFlags,
                       &gtgt);
    linkLibs = frameworkPath + linkPath + linkLibs;
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650

    printf("%s\n", linkLibs.c_str() );

/*    if ( use_win32 )
      {
      tgt->SetProperty("WIN32_EXECUTABLE", "ON");
      }
    if ( use_macbundle)
      {
      tgt->SetProperty("MACOSX_BUNDLE", "ON");
      }*/
    }

  // free generic one if generated
//  this->SetGlobalGenerator(0); // setting 0-pointer is not possible
//  delete gg; // this crashes inside the cmake instance

  return packageFound;
651
652
653
}


Ken Martin's avatar
Ken Martin committed
654
// Parse the args
Ben Boeckel's avatar
Ben Boeckel committed
655
656
void cmake::SetArgs(const std::vector<std::string>& args,
                    bool directoriesSetBefore)
Ken Martin's avatar
Ken Martin committed
657
{
658
  bool directoriesSet = directoriesSetBefore;
659
  bool haveToolset = false;
Brad King's avatar
Brad King committed
660
  for(unsigned int i=1; i < args.size(); ++i)
Ken Martin's avatar
Ken Martin committed
661
    {
662
    std::string arg = args[i];
Ken Martin's avatar
Ken Martin committed
663
    if(arg.find("-H",0) == 0)
Ken Martin's avatar
Ken Martin committed
664
      {
Bill Hoffman's avatar
Bill Hoffman committed
665
      directoriesSet = true;
Ken Martin's avatar
Ken Martin committed
666
      std::string path = arg.substr(2);
667
668
      path = cmSystemTools::CollapseFullPath(path.c_str());
      cmSystemTools::ConvertToUnixSlashes(path);
Ken Martin's avatar
Ken Martin committed
669
      this->SetHomeDirectory(path.c_str());
Ken Martin's avatar
Ken Martin committed
670
      }
Bill Hoffman's avatar
Bill Hoffman committed
671
    else if(arg.find("-S",0) == 0)
Ken Martin's avatar
Ken Martin committed
672
      {
673
      // There is no local generate anymore.  Ignore -S option.
Ken Martin's avatar
Ken Martin committed
674
      }
Bill Hoffman's avatar
Bill Hoffman committed
675
    else if(arg.find("-O",0) == 0)
Ken Martin's avatar
Ken Martin committed
676
      {
677
      // There is no local generate anymore.  Ignore -O option.
Ken Martin's avatar
Ken Martin committed
678
      }
Bill Hoffman's avatar
Bill Hoffman committed
679
    else if(arg.find("-B",0) == 0)
Ken Martin's avatar
Ken Martin committed
680
      {
Bill Hoffman's avatar
Bill Hoffman committed
681
      directoriesSet = true;
Ken Martin's avatar
Ken Martin committed
682
      std::string path = arg.substr(2);
683
684
      path = cmSystemTools::CollapseFullPath(path.c_str());
      cmSystemTools::ConvertToUnixSlashes(path);
Ken Martin's avatar
Ken Martin committed
685
      this->SetHomeOutputDirectory(path.c_str());
Ken Martin's avatar
Ken Martin committed
686
      }
687
    else if((i < args.size()-1) && (arg.find("--check-build-system",0) == 0))
688
      {
Ken Martin's avatar
Ken Martin committed
689
690
      this->CheckBuildSystemArgument = args[++i];
      this->ClearBuildSystem = (atoi(args[++i].c_str()) > 0);
691
      }
692
693
694
695
    else if((i < args.size()-1) && (arg.find("--check-stamp-file",0) == 0))
      {
      this->CheckStampFile = args[++i];
      }
696
697
698
699
    else if((i < args.size()-1) && (arg.find("--check-stamp-list",0) == 0))
      {
      this->CheckStampList = args[++i];
      }
700
#if defined(CMAKE_HAVE_VS_GENERATORS)
701
702
703
704
    else if((i < args.size()-1) && (arg.find("--vs-solution-file",0) == 0))
      {
      this->VSSolutionFile = args[++i];
      }
705
#endif
Bill Hoffman's avatar
Bill Hoffman committed
706
    else if(arg.find("-V",0) == 0)
Ken Martin's avatar
Ken Martin committed
707
      {
Ken Martin's avatar
Ken Martin committed
708
        this->Verbose = true;
Ken Martin's avatar
Ken Martin committed
709
      }
710
711
712
713
    else if(arg.find("-D",0) == 0)
      {
      // skip for now
      }
Alexander Neundorf's avatar
 
Alexander Neundorf committed
714
715
716
717
    else if(arg.find("-U",0) == 0)
      {
      // skip for now
      }
718
719
720
721
    else if(arg.find("-C",0) == 0)
      {
      // skip for now
      }
722
    else if(arg.find("-P",0) == 0)
723
724
      {
      // skip for now
725
      i++;
726
      }
727
728
729
730
731
    else if(arg.find("--find-package",0) == 0)
      {
      // skip for now
      i++;
      }
732
733
734
735
736
737
738
739
    else if(arg.find("-Wno-dev",0) == 0)
      {
      // skip for now
      }
    else if(arg.find("-Wdev",0) == 0)
      {
      // skip for now
      }
740
741
742
743
744
    else if(arg.find("--graphviz=",0) == 0)
      {
      std::string path = arg.substr(strlen("--graphviz="));
      path = cmSystemTools::CollapseFullPath(path.c_str());
      cmSystemTools::ConvertToUnixSlashes(path);
Ken Martin's avatar
Ken Martin committed
745
746
      this->GraphVizFile = path;
      if ( this->GraphVizFile.empty() )
747
748
749
750
        {
        cmSystemTools::Error("No file specified for --graphviz");
        }
      }
751
752
753
754
755
    else if(arg.find("--debug-trycompile",0) == 0)
      {
      std::cout << "debug trycompile on\n";
      this->DebugTryCompileOn();
      }
756
757
758
    else if(arg.find("--debug-output",0) == 0)
      {
      std::cout << "Running with debug output on.\n";
759
      this->SetDebugOutputOn(true);
760
      }
Bill Hoffman's avatar
Bill Hoffman committed
761
762
763
764
765
    else if(arg.find("--trace",0) == 0)
      {
      std::cout << "Running with trace output on.\n";
      this->SetTrace(true);
      }
766
    else if(arg.find("--warn-uninitialized",0) == 0)
767
      {
768
769
      std::cout << "Warn about uninitialized values.\n";
      this->SetWarnUninitialized(true);
770
      }
771
    else if(arg.find("--warn-unused-vars",0) == 0)
772
      {
773
      std::cout << "Finding unused variables.\n";
774
      this->SetWarnUnused(true);
775
      }
776
    else if(arg.find("--no-warn-unused-cli",0) == 0)
777
      {
778
779
      std::cout << "Not searching for unused variables given on the " <<
                   "command line.\n";
780
      this->SetWarnUnusedCli(false);
781
      }
782
783
    else if(arg.find("--check-system-vars",0) == 0)
      {
784
785
      std::cout << "Also check system files when warning about unused and " <<
                   "uninitialized variables.\n";
786
787
      this->SetCheckSystemVars(true);
      }
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
    else if(arg.find("-T",0) == 0)
      {
      std::string value = arg.substr(2);
      if(value.size() == 0)
        {
        ++i;
        if(i >= args.size())
          {
          cmSystemTools::Error("No toolset specified for -T");
          return;
          }
        value = args[i];
        }
      if(haveToolset)
        {
        cmSystemTools::Error("Multiple -T options not allowed");
        return;
        }
      this->GeneratorToolset = value;
      haveToolset = true;
      }
Bill Hoffman's avatar
Bill Hoffman committed
809
810
811
    else if(arg.find("-G",0) == 0)
      {
      std::string value = arg.substr(2);
812
813
      if(value.size() == 0)
        {
814
815
816
817
818
819
820
        ++i;
        if(i >= args.size())
          {
          cmSystemTools::Error("No generator specified for -G");
          return;
          }
        value = args[i];
821
        }
Andy Cedilnik's avatar
Andy Cedilnik committed
822
      cmGlobalGenerator* gen =
Ken Martin's avatar
Ken Martin committed
823
        this->CreateGlobalGenerator(value.c_str());
Bill Hoffman's avatar
Bill Hoffman committed
824
825
826
827
828
829
830
      if(!gen)
        {
        cmSystemTools::Error("Could not create named generator ",
                             value.c_str());
        }
      else
        {
Ken Martin's avatar
Ken Martin committed
831
        this->SetGlobalGenerator(gen);
Bill Hoffman's avatar
Bill Hoffman committed
832
833
        }
      }
834
    // no option assume it is the path to the source
Bill Hoffman's avatar
Bill Hoffman committed
835
836
    else
      {
837
      directoriesSet = true;
838
      this->SetDirectoriesFromFile(arg.c_str());
Bill Hoffman's avatar
Bill Hoffman committed
839
840
841
842
      }
    }
  if(!directoriesSet)
    {
Ken Martin's avatar
Ken Martin committed
843
    this->SetHomeOutputDirectory
844
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
Ken Martin's avatar
Ken Martin committed
845
    this->SetStartOutputDirectory
846
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
Ken Martin's avatar
Ken Martin committed
847
    this->SetHomeDirectory
848
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
Ken Martin's avatar
Ken Martin committed
849
    this->SetStartDirectory
850
      (cmSystemTools::GetCurrentWorkingDirectory().c_str());
Ken Martin's avatar
Ken Martin committed
851
    }
852
853
854

  this->SetStartDirectory(this->GetHomeDirectory());
  this->SetStartOutputDirectory(this->GetHomeOutputDirectory());
Ken Martin's avatar
Ken Martin committed
855
856
}

857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
//----------------------------------------------------------------------------
void cmake::SetDirectoriesFromFile(const char* arg)
{
  // Check if the argument refers to a CMakeCache.txt or
  // CMakeLists.txt file.
  std::string listPath;
  std::string cachePath;
  bool argIsFile = false;
  if(cmSystemTools::FileIsDirectory(arg))
    {
    std::string path = cmSystemTools::CollapseFullPath(arg);
    cmSystemTools::ConvertToUnixSlashes(path);
    std::string cacheFile = path;
    cacheFile += "/CMakeCache.txt";
    std::string listFile = path;
    listFile += "/CMakeLists.txt";
    if(cmSystemTools::FileExists(cacheFile.c_str()))
      {
      cachePath = path;
      }
    if(cmSystemTools::FileExists(listFile.c_str()))
      {
      listPath = path;
      }
    }
  else if(cmSystemTools::FileExists(arg))
    {
    argIsFile = true;
    std::string fullPath = cmSystemTools::CollapseFullPath(arg);
    std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
    name = cmSystemTools::LowerCase(name);
    if(name == "cmakecache.txt")
      {
      cachePath = cmSystemTools::GetFilenamePath(fullPath.c_str());
      }
    else if(name == "cmakelists.txt")
      {
      listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
      }
    }
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
  else
    {
    // Specified file or directory does not exist.  Try to set things
    // up to produce a meaningful error message.
    std::string fullPath = cmSystemTools::CollapseFullPath(arg);
    std::string name = cmSystemTools::GetFilenameName(fullPath.c_str());
    name = cmSystemTools::LowerCase(name);
    if(name == "cmakecache.txt" || name == "cmakelists.txt")
      {
      argIsFile = true;
      listPath = cmSystemTools::GetFilenamePath(fullPath.c_str());
      }
    else
      {
      listPath = fullPath;
      }
    }

915
916
917
918
919
  // If there is a CMakeCache.txt file, use its settings.
  if(cachePath.length() > 0)
    {
    cmCacheManager* cachem = this->GetCacheManager();
    cmCacheManager::CacheIterator it = cachem->NewIterator();
Andy Cedilnik's avatar
Andy Cedilnik committed
920
921
    if(cachem->LoadCache(cachePath.c_str()) &&
      it.Find("CMAKE_HOME_DIRECTORY"))
922
      {
Andy Cedilnik's avatar
Andy Cedilnik committed
923
924
      this->SetHomeOutputDirectory(cachePath.c_str());
      this->SetStartOutputDirectory(cachePath.c_str());
925
926
927
928
929
      this->SetHomeDirectory(it.GetValue());
      this->SetStartDirectory(it.GetValue());
      return;
      }
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
930

931
932
933
934
935
  // If there is a CMakeLists.txt file, use it as the source tree.
  if(listPath.length() > 0)
    {
    this->SetHomeDirectory(listPath.c_str());
    this->SetStartDirectory(listPath.c_str());
Andy Cedilnik's avatar
Andy Cedilnik committed
936

937
938
939
940
    if(argIsFile)
      {
      // Source CMakeLists.txt file given.  It was probably dropped
      // onto the executable in a GUI.  Default to an in-source build.
Andy Cedilnik's avatar
Andy Cedilnik committed
941
942
      this->SetHomeOutputDirectory(listPath.c_str());
      this->SetStartOutputDirectory(listPath.c_str());
943
944
945
946
947
948
949
      }
    else
      {
      // Source directory given on command line.  Use current working
      // directory as build tree.
      std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
      this->SetHomeOutputDirectory(cwd.c_str());
Andy Cedilnik's avatar
Andy Cedilnik committed
950
      this->SetStartOutputDirectory(cwd.c_str());
951
952
953
      }
    return;
    }
Andy Cedilnik's avatar
Andy Cedilnik committed
954

955
956
957
958
959
960
961
962
  // We didn't find a CMakeLists.txt or CMakeCache.txt file from the
  // argument.  Assume it is the path to the source tree, and use the
  // current working directory as the build tree.
  std::string full = cmSystemTools::CollapseFullPath(arg);
  std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  this->SetHomeDirectory(full.c_str());
  this->SetStartDirectory(full.c_str());
  this->SetHomeOutputDirectory(cwd.c_str());
Andy Cedilnik's avatar
Andy Cedilnik committed
963
  this->SetStartOutputDirectory(cwd.c_str());
964
965
}

Andy Cedilnik's avatar
Andy Cedilnik committed
966
967
// at the end of this CMAKE_ROOT and CMAKE_COMMAND should be added to the
// cache
968
int cmake::AddCMakePaths()
Ken Martin's avatar
Ken Martin committed
969
{
Alexander Neundorf's avatar
 
Alexander Neundorf committed
970
  // Find the cmake executable
971
  std::string cMakeSelf = cmSystemTools::GetExecutableDirectory();
972
  cMakeSelf = cmSystemTools::GetRealPath(cMakeSelf.c_str());
Ken Martin's avatar
Ken Martin committed
973
974
  cMakeSelf += "/cmake";
  cMakeSelf += cmSystemTools::GetExecutableExtension();
Stephen Kelly's avatar
Stephen Kelly committed
975
#ifdef __APPLE__
Bill Hoffman's avatar