cmState.cxx 58.1 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
40
  std::vector<std::string>::size_type IncludeDirectoryPosition;
  std::vector<std::string>::size_type CompileDefinitionsPosition;
  std::vector<std::string>::size_type CompileOptionsPosition;
41
42
};

43
44
45
46
47
48
49
50
51
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;
};

52
53
struct cmState::BuildsystemDirectoryStateType
{
54
55
  cmState::PositionType DirectoryEnd;

56
57
58
59
60
61
62
63
64
65
66
67
  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;
68
69
70
71
72
73
74
75
76

  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;
77

78
79
  std::string ProjectName;

80
  cmPropertyMap Properties;
81
82

  std::vector<cmState::Snapshot> Children;
83
84
};

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

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

103
104
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
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;
}

130
131
132
133
134
135
136
137
138
139
140
141
142
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
143
{
144
145
146
147
148
  if ( type > 6 )
    {
    return cmCacheEntryTypes[6];
    }
  return cmCacheEntryTypes[type];
Stephen Kelly's avatar
Stephen Kelly committed
149
150
}

151
152
cmState::CacheEntryType
cmState::StringToCacheEntryType(const char* s)
Stephen Kelly's avatar
Stephen Kelly committed
153
{
154
155
156
157
158
159
160
161
162
163
  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
164
165
166
167
}

bool cmState::IsCacheEntryType(std::string const& key)
{
168
169
170
171
172
173
174
175
  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
176
177
}

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

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

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

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

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

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

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

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

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

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

const char* cmState::GetCacheEntryProperty(std::string const& key,
                                              std::string const& propertyName)
{
261
  cmCacheManager::CacheIterator it = this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
262
263
264
265
266
267
268
269
270
271
272
             ->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)
{
273
  return this->CacheManager
Stephen Kelly's avatar
Stephen Kelly committed
274
275
276
277
278
             ->GetCacheIterator(key.c_str()).GetPropertyAsBool(propertyName);
}

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

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

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

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

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

313
314
315
  PositionType pos = this->SnapshotData.Truncate();
  this->ExecutionListFiles.Truncate();

316
317
318
319
320
321
322
323
324
  {
  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();
325
  it->DirectoryEnd = pos;
326
  it->Properties.clear();
327
  it->Children.clear();
328
  }
329

330
331
332
333
334
335
  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());
336
337
338
339
340
341

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

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

351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
  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);
370
371

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

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);
}

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

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

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

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;
}

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

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

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

void cmState::SetIsInTryCompile(bool b)
{
  this->IsInTryCompile = b;
}
463
464
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

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()
{
549
  std::vector<cmCommand*> renamedCommands;
550
551
552
553
554
555
556
557
558
  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++);
      }
559
560
561
562
563
    else if (j->first != j->second->GetName())
      {
      renamedCommands.push_back(j->second);
      this->Commands.erase(j++);
      }
564
565
566
567
568
    else
      {
      ++j;
      }
    }
569
570
571
572
573
  for (std::vector<cmCommand*>::const_iterator it = renamedCommands.begin();
       it != renamedCommands.end(); ++it)
    {
    this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
    }
574
}
575
576
577

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

void cmState::AppendGlobalProperty(const std::string& prop,
                                   const char* value, bool asString)
{
584
  this->GlobalProperties.AppendProperty(prop, value, asString);
585
586
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
}

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
620
  return this->GlobalProperties.GetPropertyValue(prop);
621
622
623
624
625
626
}

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

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

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

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

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

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

  cmSystemTools::SplitPath(
      cmSystemTools::CollapseFullPath(this->BinaryDirectory),
        this->BinaryDirectoryComponents);
656
657
}

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
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;
}

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

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

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

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

738
void cmState::Directory::ComputeRelativePathTopSource()
739
740
741
742
743
{
  // 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.

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

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

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

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

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

  for (std::vector<cmState::Snapshot>::const_iterator it =
       snapshots.begin() + 1; it != snapshots.end(); ++it)
    {
798
    std::string currentBinary = it->GetDirectory().GetCurrentBinary();
799
800
801
802
803
804
805
806
807
808
809
    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) != "//")
    {
810
    this->DirectoryState->RelativePathTopBinary = result;
811
812
813
    }
  else
    {
814
    this->DirectoryState->RelativePathTopBinary = "";
815
816
817
    }
}

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

845
cmState::Snapshot
846
cmState::CreateBuildsystemDirectorySnapshot(Snapshot originSnapshot)
Stephen Kelly's avatar
Stephen Kelly committed
847
{
848
  assert(originSnapshot.IsValid());
849
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position);
850
  pos->DirectoryParent = originSnapshot.Position;
851
  pos->ScopeParent = originSnapshot.Position;
852
  pos->SnapshotType = BuildsystemDirectoryType;
853
  pos->Keep = true;
854
  pos->BuildSystemDirectory =
855
      this->BuildsystemDirectory.Push(
856
        originSnapshot.Position->BuildSystemDirectory);
857
  pos->ExecutionListFile =
858
      this->ExecutionListFiles.Push(
859
        originSnapshot.Position->ExecutionListFile);
860
  pos->BuildSystemDirectory->DirectoryEnd = pos;
861
862
863
864
865
  pos->Policies = originSnapshot.Position->Policies;
  pos->PolicyRoot = originSnapshot.Position->Policies;
  pos->PolicyScope = originSnapshot.Position->Policies;
  assert(pos->Policies.IsValid());
  assert(pos->PolicyRoot.IsValid());
866
867
868
869
870

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

873
874
  cmState::Snapshot snapshot = cmState::Snapshot(this, pos);
  originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
875
  snapshot.SetDefaultDefinitions();
876
  snapshot.InitializeFromParent();
877
  snapshot.SetDirectoryDefinitions();
878
  return snapshot;
Stephen Kelly's avatar
Stephen Kelly committed
879
880
}

881
cmState::Snapshot
882
883
cmState::CreateFunctionCallSnapshot(cmState::Snapshot originSnapshot,
                                    std::string const& fileName)
884
{
885
886
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
887
  pos->ScopeParent = originSnapshot.Position;
888
  pos->SnapshotType = FunctionCallType;
889
  pos->Keep = false;
890
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
891
        originSnapshot.Position->ExecutionListFile, fileName);
892
  pos->BuildSystemDirectory->DirectoryEnd = pos;
893
  pos->PolicyScope = originSnapshot.Position->Policies;
894
895
896
897
  assert(originSnapshot.Position->Vars.IsValid());
  cmLinkedTree<cmDefinitions>::iterator origin =
      originSnapshot.Position->Vars;
  pos->Parent = origin;
898
  pos->Vars = this->VarTree.Push(origin);
899
900
901
902
903
  return cmState::Snapshot(this, pos);
}


cmState::Snapshot
904
905
cmState::CreateMacroCallSnapshot(cmState::Snapshot originSnapshot,
                                    std::string const& fileName)
906
{
907
908
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
909
  pos->SnapshotType = MacroCallType;
910
  pos->Keep = false;
911
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
912
        originSnapshot.Position->ExecutionListFile, fileName);
913
  assert(originSnapshot.Position->Vars.IsValid());
914
  pos->BuildSystemDirectory->DirectoryEnd = pos;
915
  pos->PolicyScope = originSnapshot.Position->Policies;
916
917
918
919
  return cmState::Snapshot(this, pos);
}

cmState::Snapshot
920
921
cmState::CreateIncludeFileSnapshot(cmState::Snapshot originSnapshot,
                                   const std::string& fileName)
922
{
923
924
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
925
  pos->SnapshotType = IncludeFileType;
926
  pos->Keep = true;
927
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
928
        originSnapshot.Position->ExecutionListFile, fileName);
929
  assert(originSnapshot.Position->Vars.IsValid());
930
  pos->BuildSystemDirectory->DirectoryEnd = pos;
931
  pos->PolicyScope = originSnapshot.Position->Policies;
932
933
934
  return cmState::Snapshot(this, pos);
}

935
cmState::Snapshot
936
cmState::CreateVariableScopeSnapshot(cmState::Snapshot originSnapshot)
937
{
938
939
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
940
941
  pos->ScopeParent = originSnapshot.Position;
  pos->SnapshotType = VariableScopeType;
942
  pos->Keep = false;
943
  pos->PolicyScope = originSnapshot.Position->Policies;
944
  assert(originSnapshot.Position->Vars.IsValid());
945

946
947
948
  cmLinkedTree<cmDefinitions>::iterator origin =
      originSnapshot.Position->Vars;
  pos->Parent = origin;
949
  pos->Vars = this->VarTree.Push(origin);
950
  assert(pos->Vars.IsValid());
951
952
953
  return cmState::Snapshot(this, pos);
}

954
cmState::Snapshot
955
956
cmState::CreateInlineListFileSnapshot(cmState::Snapshot originSnapshot,
                                      const std::string& fileName)
957
{
958
959
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
960
  pos->SnapshotType = InlineListFileType;
961
  pos->Keep = true;
962
  pos->ExecutionListFile = this->ExecutionListFiles.Push(
963
        originSnapshot.Position->ExecutionListFile, fileName);
964
  pos->BuildSystemDirectory->DirectoryEnd = pos;
965
  pos->PolicyScope = originSnapshot.Position->Policies;
966
967
968
  return cmState::Snapshot(this, pos);
}

969
970
971
cmState::Snapshot
cmState::CreatePolicyScopeSnapshot(cmState::Snapshot originSnapshot)
{
972
973
  PositionType pos = this->SnapshotData.Push(originSnapshot.Position,
                                             *originSnapshot.Position);
974
  pos->SnapshotType = PolicyScopeType;
975
  pos->Keep = false;
976
  pos->BuildSystemDirectory->DirectoryEnd = pos;
977
  pos->PolicyScope = originSnapshot.Position->Policies;
978
979
980
  return cmState::Snapshot(this, pos);
}

981
982
983
984
985
cmState::Snapshot cmState::Pop(cmState::Snapshot originSnapshot)
{
  PositionType pos = originSnapshot.Position;
  PositionType prevPos = pos;
  ++prevPos;
986
987
988
989
990
991
  prevPos->IncludeDirectoryPosition =
      prevPos->BuildSystemDirectory->IncludeDirectories.size();
  prevPos->CompileDefinitionsPosition =
      prevPos->BuildSystemDirectory->CompileDefinitions.size();
  prevPos->CompileOptionsPosition =
      prevPos->BuildSystemDirectory->CompileOptions.size();
992
  prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
993

994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
  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);
    }

1009
  return Snapshot(this, prevPos);
1010
1011
}

1012
1013
1014
1015
1016
1017
cmState::Snapshot::Snapshot(cmState* state)
  : State(state)
  , Position()
{
}

1018
1019
1020
1021
1022
std::vector<cmState::Snapshot> cmState::Snapshot::GetChildren()
{
  return this->Position->BuildSystemDirectory->Children;
}

Stephen Kelly's avatar
Stephen Kelly committed
1023
1024
1025
1026
1027
1028
cmState::Snapshot::Snapshot(cmState* state, PositionType position)
  : State(state),
  Position(position)
{

}
1029

1030
1031
1032
1033
1034
cmState::SnapshotType cmState::Snapshot::GetType() const
{
  return this->Position->SnapshotType;
}

1035
const char* cmState::Directory::GetCurrentSource() const
1036
{
1037
  return this->DirectoryState->Location.c_str();
1038
1039
}

1040
void cmState::Directory::SetCurrentSource(std::string const& dir)
1041
{
1042
  std::string& loc = this->DirectoryState->Location;
1043
1044
1045
  loc = dir;
  cmSystemTools::ConvertToUnixSlashes(loc);
  loc = cmSystemTools::CollapseFullPath(loc);
1046
1047

  cmSystemTools::SplitPath(
1048
      loc,
1049
      this->DirectoryState->CurrentSourceDirectoryComponents);
1050
  this->ComputeRelativePathTopSource();
1051
1052

  this->Snapshot_.SetDefinition("CMAKE_CURRENT_SOURCE_DIR", loc.c_str());
1053
1054
}

1055
const char* cmState::Directory::GetCurrentBinary() const
1056
{
1057
  return this->DirectoryState->OutputLocation.c_str();
1058
1059
}

1060
void cmState::Directory::SetCurrentBinary(std::string const& dir)
1061
{
1062
  std::string& loc = this->DirectoryState->OutputLocation;
1063
1064
1065
  loc = dir;
  cmSystemTools::ConvertToUnixSlashes(loc);
  loc = cmSystemTools::CollapseFullPath(loc);
1066
1067

  cmSystemTools::SplitPath(
1068
      loc,
1069
      this->DirectoryState->CurrentBinaryDirectoryComponents);
1070
  this->ComputeRelativePathTopBinary();
1071
1072

  this->Snapshot_.SetDefinition("CMAKE_CURRENT_BINARY_DIR", loc.c_str());
1073
1074
}

1075
1076
1077
1078
1079
void cmState::Snapshot::SetListFile(const std::string& listfile)
{
  *this->Position->ExecutionListFile = listfile;
}

1080
std::vector<std::string> const&
1081
cmState::Directory::GetCurrentSourceComponents() const
1082
{
1083
  return this->DirectoryState->CurrentSourceDirectoryComponents;
1084
1085
1086
}

std::vector<std::string> const&
1087
cmState::Directory::GetCurrentBinaryComponents() const
1088
{
1089
  return this->DirectoryState->CurrentBinaryDirectoryComponents;
1090
}
1091

1092
const char* cmState::Directory::GetRelativePathTopSource() const
1093
{
1094
  return this->DirectoryState->RelativePathTopSource.c_str();
1095
1096
}

1097
const char* cmState::Directory::GetRelativePathTopBinary() const
1098
{
1099
  return this->DirectoryState->RelativePathTopBinary.c_str();
1100
1101
}

1102
void cmState::Directory::SetRelativePathTopSource(const char* dir)
1103
{
1104
  this->DirectoryState->RelativePathTopSource = dir;
1105
1106
}

1107
void cmState::Directory::SetRelativePathTopBinary(const char* dir)
1108
{
1109
  this->DirectoryState->RelativePathTopBinary = dir;
1110
1111
}

1112
1113
1114
1115
1116
std::string cmState::Snapshot::GetExecutionListFile() const
{
  return *this->Position->ExecutionListFile;
}

1117
1118
bool cmState::Snapshot::IsValid() const
{
1119
1120
1121
  return this->State && this->Position.IsValid()
      ? this->Position != this->State->SnapshotData.Root()
      : false;
1122
1123
}

1124
cmState::Snapshot cmState::Snapshot::GetBuildsystemDirectoryParent() const
1125
1126
{
  Snapshot snapshot;
1127
  if (!this->State || this->Position == this->State->SnapshotData.Root())
1128
1129
1130
    {
    return snapshot;
    }
1131
1132
1133
  PositionType parentPos = this->Position->DirectoryParent;
  if (parentPos != this->State->SnapshotData.Root())
    {
1134
1135
    snapshot = Snapshot(this->State,
                        parentPos->BuildSystemDirectory->DirectoryEnd);
1136
    }
1137
1138
1139

  return snapshot;
}
1140

1141
1142
1143
1144
1145
1146
cmState::Snapshot cmState::Snapshot::GetCallStackParent() const
{
  assert(this->State);
  assert(this->Position != this->State->SnapshotData.Root());

  Snapshot snapshot;
1147
  PositionType parentPos = this->Position;
1148
1149
  while (parentPos->SnapshotType == cmState::PolicyScopeType ||
         parentPos->SnapshotType == cmState::VariableScopeType)
1150
1151
1152
    {
    ++parentPos;
    }
1153
1154
  if (parentPos->SnapshotType == cmState::BuildsystemDirectoryType
      || parentPos->SnapshotType == cmState::BaseType)
1155
1156
1157
1158
1159
    {
    return snapshot;
    }

  ++parentPos;
1160
1161
  while (parentPos->SnapshotType == cmState::PolicyScopeType ||
         parentPos->SnapshotType == cmState::VariableScopeType)
1162
1163
1164
1165
    {
    ++parentPos;
    }

1166
1167
1168
1169
1170
1171
1172
1173
1174
  if (parentPos == this->State->SnapshotData.Root())
    {
    return snapshot;
    }

  snapshot = Snapshot(this->State, parentPos);
  return snapshot;
}