cmState.cxx 59.4 KB
Newer Older
Stephen Kelly's avatar
Stephen Kelly committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*============================================================================
  CMake - Cross Platform Makefile Generator
  Copyright 2015 Stephen Kelly <steveire@gmail.com>

  Distributed under the OSI-approved BSD License (the "License");
  see accompanying file Copyright.txt for details.

  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.
============================================================================*/
#include "cmState.h"

#include "cmake.h"
15
#include "cmVersion.h"
16
#include "cmCacheManager.h"
17
18
#include "cmCommand.h"
#include "cmAlgorithms.h"
19
#include "cmDefinitions.h"
20
21

#include <assert.h>
Stephen Kelly's avatar
Stephen Kelly committed
22

23
24
struct cmState::SnapshotDataType
{
25
  cmState::PositionType ScopeParent;
26
  cmState::PositionType DirectoryParent;
27
28
29
  cmLinkedTree<cmState::PolicyStackEntry>::iterator Policies;
  cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyRoot;
  cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyScope;
30
  cmState::SnapshotType SnapshotType;
31
  bool Keep;
32
  cmLinkedTree<std::string>::iterator ExecutionListFile;
33
34
  cmLinkedTree<cmState::BuildsystemDirectoryStateType>::iterator
                                                          BuildSystemDirectory;
35
36
37
  cmLinkedTree<cmDefinitions>::iterator Vars;
  cmLinkedTree<cmDefinitions>::iterator Root;
  cmLinkedTree<cmDefinitions>::iterator Parent;
38
39
  std::string EntryPointCommand;
  long EntryPointLine;
40
41
42
  std::vector<std::string>::size_type IncludeDirectoryPosition;
  std::vector<std::string>::size_type CompileDefinitionsPosition;
  std::vector<std::string>::size_type CompileOptionsPosition;
43
44
};

45
46
47
48
49
50
51
52
53
struct cmState::PolicyStackEntry: public cmPolicies::PolicyMap
{
  typedef cmPolicies::PolicyMap derived;
  PolicyStackEntry(bool w = false): derived(), Weak(w) {}
  PolicyStackEntry(derived const& d, bool w): derived(d), Weak(w) {}
  PolicyStackEntry(PolicyStackEntry const& r): derived(r), Weak(r.Weak) {}
  bool Weak;
};

54
55
struct cmState::BuildsystemDirectoryStateType
{
56
57
  cmState::PositionType DirectoryEnd;

58
59
60
61
62
63
64
65
66
67
68
69
  std::string Location;
  std::string OutputLocation;

  std::vector<std::string> CurrentSourceDirectoryComponents;
  std::vector<std::string> CurrentBinaryDirectoryComponents;
  // The top-most directories for relative path conversion.  Both the
  // source and destination location of a relative path conversion
  // must be underneath one of these directories (both under source or
  // both under binary) in order for the relative path to be evaluated
  // safely by the build tools.
  std::string RelativePathTopSource;
  std::string RelativePathTopBinary;
70
71
72
73
74
75
76
77
78

  std::vector<std::string> IncludeDirectories;
  std::vector<cmListFileBacktrace> IncludeDirectoryBacktraces;

  std::vector<std::string> CompileDefinitions;
  std::vector<cmListFileBacktrace> CompileDefinitionsBacktraces;

  std::vector<std::string> CompileOptions;
  std::vector<cmListFileBacktrace> CompileOptionsBacktraces;
79

80
81
  std::string ProjectName;

82
  cmPropertyMap Properties;
83
84

  std::vector<cmState::Snapshot> Children;
85
86
};

87
88
cmState::cmState()
  : IsInTryCompile(false),
89
90
91
92
93
94
    WindowsShell(false),
    WindowsVSIDE(false),
    WatcomWMake(false),
    MinGWMake(false),
    NMake(false),
    MSYSShell(false)
Stephen Kelly's avatar
Stephen Kelly committed
95
{
96
  this->CacheManager = new cmCacheManager;
Stephen Kelly's avatar
Stephen Kelly committed
97
}
98

99
100
cmState::~cmState()
{
101
  delete this->CacheManager;
102
103
104
  cmDeleteAll(this->Commands);
}

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const char* cmState::GetTargetTypeName(cmState::TargetType targetType)
{
  switch( targetType )
    {
      case cmState::STATIC_LIBRARY:
        return "STATIC_LIBRARY";
      case cmState::MODULE_LIBRARY:
        return "MODULE_LIBRARY";
      case cmState::SHARED_LIBRARY:
        return "SHARED_LIBRARY";
      case cmState::OBJECT_LIBRARY:
        return "OBJECT_LIBRARY";
      case cmState::EXECUTABLE:
        return "EXECUTABLE";
      case cmState::UTILITY:
        return "UTILITY";
      case cmState::GLOBAL_TARGET:
        return "GLOBAL_TARGET";
      case cmState::INTERFACE_LIBRARY:
        return "INTERFACE_LIBRARY";
      case cmState::UNKNOWN_LIBRARY:
        return "UNKNOWN_LIBRARY";
    }
  assert(0 && "Unexpected target type");
  return 0;
}

132
133
134
135
136
137
138
139
140
141
142
143
144
const char* cmCacheEntryTypes[] =
{ "BOOL",
  "PATH",
  "FILEPATH",
  "STRING",
  "INTERNAL",
  "STATIC",
  "UNINITIALIZED",
  0
};

const char*
cmState::CacheEntryTypeToString(cmState::CacheEntryType type)
Stephen Kelly's avatar
Stephen Kelly committed
145
{
146
147
148
149
150
  if ( type > 6 )
    {
    return cmCacheEntryTypes[6];
    }
  return cmCacheEntryTypes[type];
Stephen Kelly's avatar
Stephen Kelly committed
151
152
}

153
154
cmState::CacheEntryType
cmState::StringToCacheEntryType(const char* s)
Stephen Kelly's avatar
Stephen Kelly committed
155
{
156
157
158
159
160
161
162
163
164
165
  int i = 0;
  while(cmCacheEntryTypes[i])
    {
    if(strcmp(s, cmCacheEntryTypes[i]) == 0)
      {
      return static_cast<cmState::CacheEntryType>(i);
      }
    ++i;
    }
  return STRING;
Stephen Kelly's avatar
Stephen Kelly committed
166
167
168
169
}

bool cmState::IsCacheEntryType(std::string const& key)
{
170
171
172
173
174
175
176
177
  for(int i=0; cmCacheEntryTypes[i]; ++i)
    {
    if(strcmp(key.c_str(), cmCacheEntryTypes[i]) == 0)
      {
      return true;
      }
    }
  return false;
Stephen Kelly's avatar
Stephen Kelly committed
178
179
}

180
181
182
183
bool cmState::LoadCache(const std::string& path, bool internal,
                        std::set<std::string>& excludes,
                        std::set<std::string>& includes)
{
184
185
  return this->CacheManager->LoadCache(path, internal,
                                       excludes, includes);
186
187
188
189
}

bool cmState::SaveCache(const std::string& path)
{
190
  return this->CacheManager->SaveCache(path);
191
192
193
194
}

bool cmState::DeleteCache(const std::string& path)
{
195
  return this->CacheManager->DeleteCache(path);
196
197
}

Stephen Kelly's avatar
Stephen Kelly committed
198
199
200
std::vector<std::string> cmState::GetCacheEntryKeys() const
{
  std::vector<std::string> definitions;
201
  definitions.reserve(this->CacheManager->GetSize());
Stephen Kelly's avatar
Stephen Kelly committed
202
  cmCacheManager::CacheIterator cit =
203
    this->CacheManager->GetCacheIterator();
Stephen Kelly's avatar
Stephen Kelly committed
204
205
206
207
208
209
210
211
212
  for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
    {
    definitions.push_back(cit.GetName());
    }
  return definitions;
}

const char* cmState::GetCacheEntryValue(std::string const& key) const
{
213
  cmCacheManager::CacheEntry* e = this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
214
215
216
217
218
219
220
221
222
223
224
             ->GetCacheEntry(key);
  if (!e)
    {
    return 0;
    }
  return e->Value.c_str();
}

const char*
cmState::GetInitializedCacheValue(std::string const& key) const
{
225
  return this->CacheManager->GetInitializedCacheValue(key);
Stephen Kelly's avatar
Stephen Kelly committed
226
227
}

228
cmState::CacheEntryType
Stephen Kelly's avatar
Stephen Kelly committed
229
230
231
cmState::GetCacheEntryType(std::string const& key) const
{
  cmCacheManager::CacheIterator it =
232
      this->CacheManager->GetCacheIterator(key.c_str());
Stephen Kelly's avatar
Stephen Kelly committed
233
234
235
236
237
238
  return it.GetType();
}

void cmState::SetCacheEntryValue(std::string const& key,
                                         std::string const& value)
{
239
  this->CacheManager->SetCacheEntryValue(key, value);
Stephen Kelly's avatar
Stephen Kelly committed
240
241
242
243
244
245
246
}

void cmState::SetCacheEntryProperty(std::string const& key,
                            std::string const& propertyName,
                            std::string const& value)
{
  cmCacheManager::CacheIterator it =
247
      this->CacheManager->GetCacheIterator(key.c_str());
Stephen Kelly's avatar
Stephen Kelly committed
248
249
250
251
252
253
254
255
  it.SetProperty(propertyName, value.c_str());
}

void cmState::SetCacheEntryBoolProperty(std::string const& key,
                            std::string const& propertyName,
                            bool value)
{
  cmCacheManager::CacheIterator it =
256
      this->CacheManager->GetCacheIterator(key.c_str());
Stephen Kelly's avatar
Stephen Kelly committed
257
258
259
260
261
262
  it.SetProperty(propertyName, value);
}

const char* cmState::GetCacheEntryProperty(std::string const& key,
                                              std::string const& propertyName)
{
263
  cmCacheManager::CacheIterator it = this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
264
265
266
267
268
269
270
271
272
273
274
             ->GetCacheIterator(key.c_str());
  if (!it.PropertyExists(propertyName))
    {
    return 0;
    }
  return it.GetProperty(propertyName);
}

bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
                                              std::string const& propertyName)
{
275
  return this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
276
277
278
279
280
             ->GetCacheIterator(key.c_str()).GetPropertyAsBool(propertyName);
}

void cmState::AddCacheEntry(const std::string& key, const char* value,
                                    const char* helpString,
281
                                    cmState::CacheEntryType type)
Stephen Kelly's avatar
Stephen Kelly committed
282
{
283
  this->CacheManager->AddCacheEntry(key, value,
Stephen Kelly's avatar
Stephen Kelly committed
284
285
286
287
288
                                                        helpString, type);
}

void cmState::RemoveCacheEntry(std::string const& key)
{
289
  this->CacheManager->RemoveCacheEntry(key);
Stephen Kelly's avatar
Stephen Kelly committed
290
291
292
293
294
295
296
}

void cmState::AppendCacheEntryProperty(const std::string& key,
                                               const std::string& property,
                                               const std::string& value,
                                               bool asString)
{
297
  this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
298
299
300
301
302
303
304
305
       ->GetCacheIterator(key.c_str()).AppendProperty(property,
                                                       value.c_str(),
                                                       asString);
}

void cmState::RemoveCacheEntryProperty(std::string const& key,
                                              std::string const& propertyName)
{
306
  this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
307
308
       ->GetCacheIterator(key.c_str()).SetProperty(propertyName, (void*)0);
}
309

310
cmState::Snapshot cmState::Reset()
311
{
312
  this->GlobalProperties.clear();
313
  this->PropertyDefinitions.clear();
314

315
316
317
  PositionType pos = this->SnapshotData.Truncate();
  this->ExecutionListFiles.Truncate();

318
319
320
321
322
323
324
325
326
  {
  cmLinkedTree<BuildsystemDirectoryStateType>::iterator it =
      this->BuildsystemDirectory.Truncate();
  it->IncludeDirectories.clear();
  it->IncludeDirectoryBacktraces.clear();
  it->CompileDefinitions.clear();
  it->CompileDefinitionsBacktraces.clear();
  it->CompileOptions.clear();
  it->CompileOptionsBacktraces.clear();
327
  it->DirectoryEnd = pos;
328
  it->Properties.clear();
329
  it->Children.clear();
330
  }
331

332
333
334
335
336
337
  this->PolicyStack.Clear();
  pos->Policies = this->PolicyStack.Root();
  pos->PolicyRoot = this->PolicyStack.Root();
  pos->PolicyScope = this->PolicyStack.Root();
  assert(pos->Policies.IsValid());
  assert(pos->PolicyRoot.IsValid());
338
339
340
341
342
343

  {
  std::string srcDir =
      cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root);
  std::string binDir =
      cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root);
344
  this->VarTree.Clear();
345
  pos->Vars = this->VarTree.Push(this->VarTree.Root());
346
347
  pos->Parent = this->VarTree.Root();
  pos->Root = this->VarTree.Root();
348

349
350
351
352
  pos->Vars->Set("CMAKE_SOURCE_DIR", srcDir.c_str());
  pos->Vars->Set("CMAKE_BINARY_DIR", binDir.c_str());
  }

353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  this->DefineProperty
    ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
     "", "", true);
  this->DefineProperty
    ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
     "", "", true);
  this->DefineProperty
    ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
     "", "", true);

  this->DefineProperty
    ("RULE_LAUNCH_COMPILE", cmProperty::TARGET,
     "", "", true);
  this->DefineProperty
    ("RULE_LAUNCH_LINK", cmProperty::TARGET,
     "", "", true);
  this->DefineProperty
    ("RULE_LAUNCH_CUSTOM", cmProperty::TARGET,
     "", "", true);
372
373

  return Snapshot(this, pos);
374
375
376
377
378
379
380
381
382
383
384
385
386
}

void cmState::DefineProperty(const std::string& name,
                           cmProperty::ScopeType scope,
                           const char *ShortDescription,
                           const char *FullDescription,
                           bool chained)
{
  this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
                                                  FullDescription,
                                                  chained);
}

387
cmPropertyDefinition const* cmState
388
::GetPropertyDefinition(const std::string& name,
389
                        cmProperty::ScopeType scope) const
390
391
392
{
  if (this->IsPropertyDefined(name,scope))
    {
393
394
395
    cmPropertyDefinitionMap const& defs =
        this->PropertyDefinitions.find(scope)->second;
    return &defs.find(name)->second;
396
397
398
399
400
    }
  return 0;
}

bool cmState::IsPropertyDefined(const std::string& name,
401
                                cmProperty::ScopeType scope) const
402
{
403
404
405
406
407
408
409
  std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
      = this->PropertyDefinitions.find(scope);
  if (it == this->PropertyDefinitions.end())
    {
    return false;
    }
  return it->second.IsPropertyDefined(name);
410
411
412
}

bool cmState::IsPropertyChained(const std::string& name,
413
                                cmProperty::ScopeType scope) const
414
{
415
416
417
418
419
420
421
  std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
      = this->PropertyDefinitions.find(scope);
  if (it == this->PropertyDefinitions.end())
    {
    return false;
    }
  return it->second.IsPropertyChained(name);
422
}
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

void cmState::SetLanguageEnabled(std::string const& l)
{
  std::vector<std::string>::iterator it =
      std::lower_bound(this->EnabledLanguages.begin(),
                       this->EnabledLanguages.end(), l);
  if (it == this->EnabledLanguages.end() || *it != l)
    {
    this->EnabledLanguages.insert(it, l);
    }
}

bool cmState::GetLanguageEnabled(std::string const& l) const
{
  return std::binary_search(this->EnabledLanguages.begin(),
                            this->EnabledLanguages.end(), l);
}

std::vector<std::string> cmState::GetEnabledLanguages() const
{
  return this->EnabledLanguages;
}

446
447
448
449
450
void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
{
  this->EnabledLanguages = langs;
}

451
452
453
454
void cmState::ClearEnabledLanguages()
{
  this->EnabledLanguages.clear();
}
455
456
457
458
459
460
461
462
463
464

bool cmState::GetIsInTryCompile() const
{
  return this->IsInTryCompile;
}

void cmState::SetIsInTryCompile(bool b)
{
  this->IsInTryCompile = b;
}
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550

void cmState::RenameCommand(std::string const& oldName,
                            std::string const& newName)
{
  // if the command already exists, free the old one
  std::string sOldName = cmSystemTools::LowerCase(oldName);
  std::string sNewName = cmSystemTools::LowerCase(newName);
  std::map<std::string, cmCommand*>::iterator pos =
      this->Commands.find(sOldName);
  if ( pos == this->Commands.end() )
    {
    return;
    }
  cmCommand* cmd = pos->second;

  pos = this->Commands.find(sNewName);
  if (pos != this->Commands.end())
    {
    delete pos->second;
    this->Commands.erase(pos);
    }
  this->Commands.insert(std::make_pair(sNewName, cmd));
  pos = this->Commands.find(sOldName);
  this->Commands.erase(pos);
}

void cmState::AddCommand(cmCommand* command)
{
  std::string name = cmSystemTools::LowerCase(command->GetName());
  // if the command already exists, free the old one
  std::map<std::string, cmCommand*>::iterator pos = this->Commands.find(name);
  if (pos != this->Commands.end())
    {
    delete pos->second;
    this->Commands.erase(pos);
    }
  this->Commands.insert(std::make_pair(name, command));
}

void cmState::RemoveUnscriptableCommands()
{
  std::vector<std::string> unscriptableCommands;
  for (std::map<std::string, cmCommand*>::iterator
       pos = this->Commands.begin();
       pos != this->Commands.end(); )
    {
    if (!pos->second->IsScriptable())
      {
      delete pos->second;
      this->Commands.erase(pos++);
      }
    else
      {
      ++pos;
      }
    }
}

cmCommand* cmState::GetCommand(std::string const& name) const
{
  cmCommand* command = 0;
  std::string sName = cmSystemTools::LowerCase(name);
  std::map<std::string, cmCommand*>::const_iterator pos =
      this->Commands.find(sName);
  if (pos != this->Commands.end())
    {
    command = (*pos).second;
    }
  return command;
}

std::vector<std::string> cmState::GetCommandNames() const
{
  std::vector<std::string> commandNames;
  commandNames.reserve(this->Commands.size());
  std::map<std::string, cmCommand*>::const_iterator cmds
      = this->Commands.begin();
  for ( ; cmds != this->Commands.end(); ++ cmds )
    {
    commandNames.push_back(cmds->first);
    }
  return commandNames;
}

void cmState::RemoveUserDefinedCommands()
{
551
  std::vector<cmCommand*> renamedCommands;
552
553
554
555
556
557
558
559
560
  for(std::map<std::string, cmCommand*>::iterator j = this->Commands.begin();
      j != this->Commands.end(); )
    {
    if (j->second->IsA("cmMacroHelperCommand") ||
        j->second->IsA("cmFunctionHelperCommand"))
      {
      delete j->second;
      this->Commands.erase(j++);
      }
561
562
563
564
565
    else if (j->first != j->second->GetName())
      {
      renamedCommands.push_back(j->second);
      this->Commands.erase(j++);
      }
566
567
568
569
570
    else
      {
      ++j;
      }
    }
571
572
573
574
575
  for (std::vector<cmCommand*>::const_iterator it = renamedCommands.begin();
       it != renamedCommands.end(); ++it)
    {
    this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
    }
576
}
577
578
579

void cmState::SetGlobalProperty(const std::string& prop, const char* value)
{
580
  this->GlobalProperties.SetProperty(prop, value);
581
582
583
584
585
}

void cmState::AppendGlobalProperty(const std::string& prop,
                                   const char* value, bool asString)
{
586
  this->GlobalProperties.AppendProperty(prop, value, asString);
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
}

const char *cmState::GetGlobalProperty(const std::string& prop)
{
  if ( prop == "CACHE_VARIABLES" )
    {
    std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
    this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
    }
  else if ( prop == "COMMANDS" )
    {
    std::vector<std::string> commands = this->GetCommandNames();
    this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
    }
  else if ( prop == "IN_TRY_COMPILE" )
    {
    this->SetGlobalProperty("IN_TRY_COMPILE",
                      this->IsInTryCompile ? "1" : "0");
    }
  else if ( prop == "ENABLED_LANGUAGES" )
    {
    std::string langs;
    langs = cmJoin(this->EnabledLanguages, ";");
    this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
    }
#define STRING_LIST_ELEMENT(F) ";" #F
  if (prop == "CMAKE_C_KNOWN_FEATURES")
    {
    return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
    }
  if (prop == "CMAKE_CXX_KNOWN_FEATURES")
    {
    return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
    }
#undef STRING_LIST_ELEMENT
622
  return this->GlobalProperties.GetPropertyValue(prop);
623
624
625
626
627
628
}

bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
{
  return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
}
629
630
631
632
633

void cmState::SetSourceDirectory(std::string const& sourceDirectory)
{
  this->SourceDirectory = sourceDirectory;
  cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
634
635
636
637

  cmSystemTools::SplitPath(
      cmSystemTools::CollapseFullPath(this->SourceDirectory),
        this->SourceDirectoryComponents);
638
639
640
641
642
643
644
}

const char* cmState::GetSourceDirectory() const
{
  return this->SourceDirectory.c_str();
}

645
646
647
648
649
std::vector<std::string> const& cmState::GetSourceDirectoryComponents() const
{
  return this->SourceDirectoryComponents;
}

650
651
652
653
void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
{
  this->BinaryDirectory = binaryDirectory;
  cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
654
655
656
657

  cmSystemTools::SplitPath(
      cmSystemTools::CollapseFullPath(this->BinaryDirectory),
        this->BinaryDirectoryComponents);
658
659
}

660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
void cmState::SetWindowsShell(bool windowsShell)
{
  this->WindowsShell = windowsShell;
}

bool cmState::UseWindowsShell() const
{
  return this->WindowsShell;
}

void cmState::SetWindowsVSIDE(bool windowsVSIDE)
{
  this->WindowsVSIDE = windowsVSIDE;
}

bool cmState::UseWindowsVSIDE() const
{
  return this->WindowsVSIDE;
}

void cmState::SetWatcomWMake(bool watcomWMake)
{
  this->WatcomWMake = watcomWMake;
}

bool cmState::UseWatcomWMake() const
{
  return this->WatcomWMake;
}

void cmState::SetMinGWMake(bool minGWMake)
{
  this->MinGWMake = minGWMake;
}

bool cmState::UseMinGWMake() const
{
  return this->MinGWMake;
}

void cmState::SetNMake(bool nMake)
{
  this->NMake = nMake;
}

bool cmState::UseNMake() const
{
  return this->NMake;
}

void cmState::SetMSYSShell(bool mSYSShell)
{
  this->MSYSShell = mSYSShell;
}

bool cmState::UseMSYSShell() const
{
  return this->MSYSShell;
}

720
721
unsigned int cmState::GetCacheMajorVersion() const
{
722
  return this->CacheManager->GetCacheMajorVersion();
723
724
725
726
}

unsigned int cmState::GetCacheMinorVersion() const
{
727
  return this->CacheManager->GetCacheMinorVersion();
728
729
}

730
731
732
733
const char* cmState::GetBinaryDirectory() const
{
  return this->BinaryDirectory.c_str();
}
Stephen Kelly's avatar
Stephen Kelly committed
734

735
736
737
738
739
std::vector<std::string> const& cmState::GetBinaryDirectoryComponents() const
{
  return this->BinaryDirectoryComponents;
}

740
void cmState::Directory::ComputeRelativePathTopSource()
741
742
743
744
745
{
  // Relative path conversion inside the source tree is not used to
  // construct relative paths passed to build tools so it is safe to use
  // even when the source is a network path.

746
  cmState::Snapshot snapshot = this->Snapshot_;
747
748
749
750
  std::vector<cmState::Snapshot> snapshots;
  snapshots.push_back(snapshot);
  while (true)
    {
751
    snapshot = snapshot.GetBuildsystemDirectoryParent();
752
753
754
755
756
757
758
759
760
761
    if (snapshot.IsValid())
      {
      snapshots.push_back(snapshot);
      }
    else
      {
      break;
      }
    }

762
  std::string result = snapshots.front().GetDirectory().GetCurrentSource();
763
764
765
766

  for (std::vector<cmState::Snapshot>::const_iterator it =
       snapshots.begin() + 1; it != snapshots.end(); ++it)
    {
767
    std::string currentSource = it->GetDirectory().GetCurrentSource();
768
769
770
771
772
    if(cmSystemTools::IsSubDirectory(result, currentSource))
      {
      result = currentSource;
      }
    }
773
  this->DirectoryState->RelativePathTopSource = result;
774
775
}

776
void cmState::Directory::ComputeRelativePathTopBinary()
777
{
778
  cmState::Snapshot snapshot = this->Snapshot_;
779
780
781
782
  std::vector<cmState::Snapshot> snapshots;
  snapshots.push_back(snapshot);
  while (true)
    {
783
    snapshot = snapshot.GetBuildsystemDirectoryParent();
784
785
786
787
788
789
790
791
792
793
794
    if (snapshot.IsValid())
      {
      snapshots.push_back(snapshot);
      }
    else
      {
      break;
      }
    }

  std::string result =
795
      snapshots.front().GetDirectory().GetCurrentBinary();
796
797
798
799

  for (std::vector<cmState::Snapshot>::const_iterator it =
       snapshots.begin() + 1; it != snapshots.end(); ++it)
    {
800
    std::string currentBinary = it->GetDirectory().GetCurrentBinary();
801
802
803
804
805
806
807
808
809
810
811
    if(cmSystemTools::IsSubDirectory(result, currentBinary))
      {
      result = currentBinary;
      }
    }

  // The current working directory on Windows cannot be a network
  // path.  Therefore relative paths cannot work when the binary tree
  // is a network path.
  if(result.size() < 2 || result.substr(0, 2) != "//")
    {
812
    this->DirectoryState->RelativePathTopBinary = result;
813
814
815
    }
  else
    {
816
    this->DirectoryState->RelativePathTopBinary = "";
817
818
819
    }
}

820
821
cmState::Snapshot cmState::CreateBaseSnapshot()
{
822
  PositionType pos = this->SnapshotData.Push(this->SnapshotData.Root());
823
  pos->DirectoryParent = this->SnapshotData.Root();
824
  pos->ScopeParent = this->SnapshotData.Root();
825
  pos->SnapshotType = BaseType;
826
  pos->Keep = true;
827
  pos->BuildSystemDirectory =
828
      this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
829
  pos->ExecutionListFile =
830
      this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
831
832
833
  pos->IncludeDirectoryPosition = 0;
  pos->CompileDefinitionsPosition = 0;
  pos->CompileOptionsPosition = 0;
834
  pos->BuildSystemDirectory->DirectoryEnd = pos;
835
836
837
838
839
  pos->Policies = this->PolicyStack.Root();
  pos->PolicyRoot = this->PolicyStack.Root();
  pos->PolicyScope = this->PolicyStack.Root();
  assert(pos->Policies.IsValid());
  assert(pos->PolicyRoot.IsValid());
840
  pos->Vars = this->VarTree.Push(this->VarTree.Root());
841
842
843
  assert(pos->Vars.IsValid());
  pos->Parent = this->VarTree.Root();
  pos->Root = this->VarTree.Root();
844
845
846
  return cmState::Snapshot(this, pos);
}

847
cmState::Snapshot
848
849
850
cmState::CreateBuildsystemDirectorySnapshot(Snapshot originSnapshot,
                                    std::string const& entryPointCommand,
                                    long entryPointLine)
Stephen Kelly's avatar
Stephen Kelly committed
851
{
852
  assert(originSnapshot.IsValid());
853
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position);
854
855
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
856
  pos->DirectoryParent = originSnapshot.Position;
857
  pos->ScopeParent = originSnapshot.Position;
858
  pos->SnapshotType = BuildsystemDirectoryType;
859
  pos->Keep = true;
860
  pos->BuildSystemDirectory =
861
      this->BuildsystemDirectory.Push(
862
        originSnapshot.Position->BuildSystemDirectory);
863
  pos->ExecutionListFile =
864
      this->ExecutionListFiles.Push(
865
        originSnapshot.Position->ExecutionListFile);
866
  pos->BuildSystemDirectory->DirectoryEnd = pos;
867
868
869
870
871
  pos->Policies = originSnapshot.Position->Policies;
  pos->PolicyRoot = originSnapshot.Position->Policies;
  pos->PolicyScope = originSnapshot.Position->Policies;
  assert(pos->Policies.IsValid());
  assert(pos->PolicyRoot.IsValid());
872
873
874
875
876

  cmLinkedTree<cmDefinitions>::iterator origin =
      originSnapshot.Position->Vars;
  pos->Parent = origin;
  pos->Root = origin;
877
  pos->Vars = this->VarTree.Push(origin);
878

879
880
  cmState::Snapshot snapshot = cmState::Snapshot(this, pos);
  originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
881
  snapshot.SetDefaultDefinitions();
882
  snapshot.InitializeFromParent();
883
  snapshot.SetDirectoryDefinitions();
884
  return snapshot;
Stephen Kelly's avatar
Stephen Kelly committed
885
886
}

887
cmState::Snapshot
888
889
890
891
cmState::CreateFunctionCallSnapshot(cmState::Snapshot originSnapshot,
                                    std::string const& entryPointCommand,
                                    long entryPointLine,
                                    std::string const& fileName)
892
{
893
894
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
895
  pos->ScopeParent = originSnapshot.Position;
896
897
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
898
  pos->SnapshotType = FunctionCallType;
899
  pos->Keep = false;
900
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
901
        originSnapshot.Position->ExecutionListFile, fileName);
902
  pos->BuildSystemDirectory->DirectoryEnd = pos;
903
  pos->PolicyScope = originSnapshot.Position->Policies;
904
905
906
907
  assert(originSnapshot.Position->Vars.IsValid());
  cmLinkedTree<cmDefinitions>::iterator origin =
      originSnapshot.Position->Vars;
  pos->Parent = origin;
908
  pos->Vars = this->VarTree.Push(origin);
909
910
911
912
913
  return cmState::Snapshot(this, pos);
}


cmState::Snapshot
914
915
916
917
cmState::CreateMacroCallSnapshot(cmState::Snapshot originSnapshot,
                                    std::string const& entryPointCommand,
                                    long entryPointLine,
                                    std::string const& fileName)
918
{
919
920
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
921
922
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
923
  pos->SnapshotType = MacroCallType;
924
  pos->Keep = false;
925
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
926
        originSnapshot.Position->ExecutionListFile, fileName);
927
  assert(originSnapshot.Position->Vars.IsValid());
928
  pos->BuildSystemDirectory->DirectoryEnd = pos;
929
  pos->PolicyScope = originSnapshot.Position->Policies;
930
931
932
933
  return cmState::Snapshot(this, pos);
}

cmState::Snapshot
934
935
936
937
cmState::CreateIncludeFileSnapshot(cmState::Snapshot originSnapshot,
                                   const std::string& entryPointCommand,
                                   long entryPointLine,
                                   const std::string& fileName)
938
{
939
940
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
941
942
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
943
  pos->SnapshotType = IncludeFileType;
944
  pos->Keep = true;
945
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
946
        originSnapshot.Position->ExecutionListFile, fileName);
947
  assert(originSnapshot.Position->Vars.IsValid());
948
  pos->BuildSystemDirectory->DirectoryEnd = pos;
949
  pos->PolicyScope = originSnapshot.Position->Policies;
950
951
952
  return cmState::Snapshot(this, pos);
}

953
954
955
956
957
cmState::Snapshot
cmState::CreateVariableScopeSnapshot(cmState::Snapshot originSnapshot,
                                     std::string const& entryPointCommand,
                                     long entryPointLine)
{
958
959
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
960
961
962
963
  pos->ScopeParent = originSnapshot.Position;
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
  pos->SnapshotType = VariableScopeType;
964
  pos->Keep = false;
965
  pos->PolicyScope = originSnapshot.Position->Policies;
966
  assert(originSnapshot.Position->Vars.IsValid());
967

968
969
970
  cmLinkedTree<cmDefinitions>::iterator origin =
      originSnapshot.Position->Vars;
  pos->Parent = origin;
971
  pos->Vars = this->VarTree.Push(origin);
972
  assert(pos->Vars.IsValid());
973
974
975
  return cmState::Snapshot(this, pos);
}

976
cmState::Snapshot
977
978
979
980
cmState::CreateInlineListFileSnapshot(cmState::Snapshot originSnapshot,
                                      const std::string& entryPointCommand,
                                      long entryPointLine,
                                      const std::string& fileName)
981
{
982
983
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
984
985
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
986
  pos->SnapshotType = InlineListFileType;
987
  pos->Keep = true;
988
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
989
        originSnapshot.Position->ExecutionListFile, fileName);
990
  pos->BuildSystemDirectory->DirectoryEnd = pos;
991
  pos->PolicyScope = originSnapshot.Position->Policies;
992
993
994
  return cmState::Snapshot(this, pos);
}

995
996
997
cmState::Snapshot
cmState::CreatePolicyScopeSnapshot(cmState::Snapshot originSnapshot)
{
998
999
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
1000
  pos->SnapshotType = PolicyScopeType;
1001
  pos->Keep = false;
1002
  pos->BuildSystemDirectory->DirectoryEnd = pos;
1003
  pos->PolicyScope = originSnapshot.Position->Policies;
1004
1005
1006
  return cmState::Snapshot(this, pos);
}

1007
1008
1009
1010
1011
cmState::Snapshot cmState::Pop(cmState::Snapshot originSnapshot)
{
  PositionType pos = originSnapshot.Position;
  PositionType prevPos = pos;
  ++prevPos;
1012
1013
1014
1015
1016
1017
  prevPos->IncludeDirectoryPosition =
      prevPos->BuildSystemDirectory->IncludeDirectories.size();
  prevPos->CompileDefinitionsPosition =
      prevPos->BuildSystemDirectory->CompileDefinitions.size();
  prevPos->CompileOptionsPosition =
      prevPos->BuildSystemDirectory->CompileOptions.size();
1018
  prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
1019

1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
  if (!pos->Keep && this->SnapshotData.IsLast(pos))
    {
    if (pos->Vars != prevPos->Vars)
      {
      assert(this->VarTree.IsLast(pos->Vars));
      this->VarTree.Pop(pos->Vars);
      }
    if (pos->ExecutionListFile != prevPos->ExecutionListFile)
      {
      assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
      this->ExecutionListFiles.Pop(pos->ExecutionListFile);
      }
    this->SnapshotData.Pop(pos);
    }

1035
  return Snapshot(this, prevPos);
1036
1037
}

1038
1039
1040
1041
1042
1043
cmState::Snapshot::Snapshot(cmState* state)
  : State(state)
  , Position()
{
}

1044
1045
1046
1047
1048
std::vector<cmState::Snapshot> cmState::Snapshot::GetChildren()
{
  return this->Position->BuildSystemDirectory->Children;
}

Stephen Kelly's avatar
Stephen Kelly committed
1049
1050
1051
1052
1053
1054
cmState::Snapshot::Snapshot(cmState* state, PositionType position)
  : State(state),
  Position(position)
{

}
1055

1056
1057
1058
1059
1060
cmState::SnapshotType cmState::Snapshot::GetType() const
{
  return this->Position->SnapshotType;
}

1061
const char* cmState::Directory::GetCurrentSource() const
1062
{
1063
  return this->DirectoryState->Location.c_str();
1064
1065
}

1066
void cmState::Directory::SetCurrentSource(std::string const& dir)
1067
{
1068
  std::string& loc = this->DirectoryState->Location;
1069
1070
1071
  loc = dir;
  cmSystemTools::ConvertToUnixSlashes(loc);
  loc = cmSystemTools::CollapseFullPath(loc);
1072
1073

  cmSystemTools::SplitPath(
1074
      loc,
1075
      this->DirectoryState->CurrentSourceDirectoryComponents);
1076
  this->ComputeRelativePathTopSource();
1077
1078

  this->Snapshot_.SetDefinition("CMAKE_CURRENT_SOURCE_DIR", loc.c_str());
1079
1080
}

1081
const char* cmState::Directory::GetCurrentBinary() const
1082
{
1083
  return this->DirectoryState->OutputLocation.c_str();
1084
1085
}

1086
void cmState::Directory::SetCurrentBinary(std::string const& dir)
1087
{
1088
  std::string& loc = this->DirectoryState->OutputLocation;
1089
1090
1091
  loc = dir;
  cmSystemTools::ConvertToUnixSlashes(loc);
  loc = cmSystemTools::CollapseFullPath(loc);
1092
1093

  cmSystemTools::SplitPath(
1094
      loc,
1095
      this->DirectoryState->CurrentBinaryDirectoryComponents);
1096
  this->ComputeRelativePathTopBinary();
1097
1098

  this->Snapshot_.SetDefinition("CMAKE_CURRENT_BINARY_DIR", loc.c_str());
1099
1100
}

1101
1102
1103
1104
1105
void cmState::Snapshot::Keep()
{
  this->Position->Keep = true;
}

1106
1107
1108
1109
1110
void cmState::Snapshot::SetListFile(const std::string& listfile)
{
  *this->Position->ExecutionListFile = listfile;
}

1111
std::vector<std::string> const&
1112
cmState::Directory::GetCurrentSourceComponents() const
1113
{
1114
  return this->DirectoryState->CurrentSourceDirectoryComponents;
1115
1116
1117
}

std::vector<std::string> const&
1118
cmState::Directory::GetCurrentBinaryComponents() const
1119
{
1120
  return this->DirectoryState->CurrentBinaryDirectoryComponents;
1121
}
1122

1123
const char* cmState::Directory::GetRelativePathTopSource() const
1124
{
1125
  return this->DirectoryState->RelativePathTopSource.c_str();
1126
1127
}

1128
const char* cmState::Directory::GetRelativePathTopBinary() const
1129
{
1130
  return this->DirectoryState->RelativePathTopBinary.c_str();
1131
1132
}

1133
void cmState::Directory::SetRelativePathTopSource(const char* dir)
1134
{
1135
  this->DirectoryState->RelativePathTopSource = dir;
1136
1137
}

1138
void cmState::Directory::SetRelativePathTopBinary(const char* dir)
1139
{
1140
  this->DirectoryState->RelativePathTopBinary = dir;
1141
1142
}

1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
std::string cmState::Snapshot::GetExecutionListFile() const
{
  return *this->Position->ExecutionListFile;
}

std::string cmState::Snapshot::GetEntryPointCommand() const
{
  return this->Position->EntryPointCommand;
}

long cmState::Snapshot::GetEntryPointLine() const
{
  return this->Position->EntryPointLine;
}

1158
1159
bool cmState::Snapshot::IsValid() const
{
1160
1161
1162
  return this->State && this->Position.IsValid()
      ? this->Position != this->State->SnapshotData.Root()
      : false;
1163
1164
}

1165
cmState::Snapshot cmState::Snapshot::GetBuildsystemDirectoryParent() const
1166
1167
{
  Snapshot snapshot;
1168
  if (!this->State || this->Position == this->State->SnapshotData.Root())
1169
1170
1171
    {
    return snapshot;
    }
1172
1173
1174
  PositionType parentPos = this->Position->DirectoryParent;
  if (parentPos != this->State->SnapshotData.Root())
    {
1175
1176
    snapshot = Snapshot(this->State,
                        parentPos->BuildSystemDirectory->DirectoryEnd);
1177
    }
1178
1179
1180

  return snapshot;
}
1181

1182
1183
1184
1185
1186
1187
cmState::Snapshot cmState::Snapshot::GetCallStackParent() const
{
  assert(this->State);
  assert(this->Position != this->State->SnapshotData.Root());

  Snapshot snapshot;