cmState.cxx 42.2 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 "cmCacheManager.h"
16
17
18
19
#include "cmCommand.h"
#include "cmAlgorithms.h"

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

21
22
23
struct cmState::SnapshotDataType
{
  cmState::PositionType DirectoryParent;
24
25
26
  cmLinkedTree<cmState::PolicyStackEntry>::iterator Policies;
  cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyRoot;
  cmLinkedTree<cmState::PolicyStackEntry>::iterator PolicyScope;
27
  cmState::SnapshotType SnapshotType;
28
  cmLinkedTree<std::string>::iterator ExecutionListFile;
29
30
  cmLinkedTree<cmState::BuildsystemDirectoryStateType>::iterator
                                                          BuildSystemDirectory;
31
32
  std::string EntryPointCommand;
  long EntryPointLine;
33
34
35
  std::vector<std::string>::size_type IncludeDirectoryPosition;
  std::vector<std::string>::size_type CompileDefinitionsPosition;
  std::vector<std::string>::size_type CompileOptionsPosition;
36
37
};

38
39
40
41
42
43
44
45
46
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;
};

47
48
struct cmState::BuildsystemDirectoryStateType
{
49
50
  cmState::PositionType DirectoryEnd;

51
52
53
54
55
56
57
58
59
60
61
62
  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;
63
64
65
66
67
68
69
70
71

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

Stephen Kelly's avatar
Stephen Kelly committed
74
cmState::cmState(cmake* cm)
75
  : CMakeInstance(cm),
76
77
78
79
80
81
82
    IsInTryCompile(false),
    WindowsShell(false),
    WindowsVSIDE(false),
    WatcomWMake(false),
    MinGWMake(false),
    NMake(false),
    MSYSShell(false)
Stephen Kelly's avatar
Stephen Kelly committed
83
84
{
}
85

86
87
88
89
90
cmState::~cmState()
{
  cmDeleteAll(this->Commands);
}

91
92
93
94
95
96
97
98
99
100
101
102
103
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
104
{
105
106
107
108
109
  if ( type > 6 )
    {
    return cmCacheEntryTypes[6];
    }
  return cmCacheEntryTypes[type];
Stephen Kelly's avatar
Stephen Kelly committed
110
111
}

112
113
cmState::CacheEntryType
cmState::StringToCacheEntryType(const char* s)
Stephen Kelly's avatar
Stephen Kelly committed
114
{
115
116
117
118
119
120
121
122
123
124
  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
125
126
127
128
}

bool cmState::IsCacheEntryType(std::string const& key)
{
129
130
131
132
133
134
135
136
  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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
}

std::vector<std::string> cmState::GetCacheEntryKeys() const
{
  std::vector<std::string> definitions;
  definitions.reserve(this->CMakeInstance->GetCacheManager()->GetSize());
  cmCacheManager::CacheIterator cit =
    this->CMakeInstance->GetCacheManager()->GetCacheIterator();
  for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
    {
    definitions.push_back(cit.GetName());
    }
  return definitions;
}

const char* cmState::GetCacheEntryValue(std::string const& key) const
{
  cmCacheManager::CacheEntry* e = this->CMakeInstance->GetCacheManager()
             ->GetCacheEntry(key);
  if (!e)
    {
    return 0;
    }
  return e->Value.c_str();
}

const char*
cmState::GetInitializedCacheValue(std::string const& key) const
{
  return this->CMakeInstance->GetCacheManager()->GetInitializedCacheValue(key);
}

169
cmState::CacheEntryType
Stephen Kelly's avatar
Stephen Kelly committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
cmState::GetCacheEntryType(std::string const& key) const
{
  cmCacheManager::CacheIterator it =
      this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  return it.GetType();
}

void cmState::SetCacheEntryValue(std::string const& key,
                                         std::string const& value)
{
  this->CMakeInstance->GetCacheManager()->SetCacheEntryValue(key, value);
}

void cmState::SetCacheEntryProperty(std::string const& key,
                            std::string const& propertyName,
                            std::string const& value)
{
  cmCacheManager::CacheIterator it =
      this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  it.SetProperty(propertyName, value.c_str());
}

void cmState::SetCacheEntryBoolProperty(std::string const& key,
                            std::string const& propertyName,
                            bool value)
{
  cmCacheManager::CacheIterator it =
      this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  it.SetProperty(propertyName, value);
}

const char* cmState::GetCacheEntryProperty(std::string const& key,
                                              std::string const& propertyName)
{
  cmCacheManager::CacheIterator it = this->CMakeInstance->GetCacheManager()
             ->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)
{
  return this->CMakeInstance->GetCacheManager()
             ->GetCacheIterator(key.c_str()).GetPropertyAsBool(propertyName);
}

void cmState::AddCacheEntry(const std::string& key, const char* value,
                                    const char* helpString,
222
                                    cmState::CacheEntryType type)
Stephen Kelly's avatar
Stephen Kelly committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
{
  this->CMakeInstance->GetCacheManager()->AddCacheEntry(key, value,
                                                        helpString, type);
}

void cmState::RemoveCacheEntry(std::string const& key)
{
  this->CMakeInstance->GetCacheManager()->RemoveCacheEntry(key);
}

void cmState::AppendCacheEntryProperty(const std::string& key,
                                               const std::string& property,
                                               const std::string& value,
                                               bool asString)
{
  this->CMakeInstance->GetCacheManager()
       ->GetCacheIterator(key.c_str()).AppendProperty(property,
                                                       value.c_str(),
                                                       asString);
}

void cmState::RemoveCacheEntryProperty(std::string const& key,
                                              std::string const& propertyName)
{
  this->CMakeInstance->GetCacheManager()
       ->GetCacheIterator(key.c_str()).SetProperty(propertyName, (void*)0);
}
250

251
cmState::Snapshot cmState::Reset()
252
{
253
  this->GlobalProperties.clear();
254
  this->PropertyDefinitions.clear();
255

256
257
258
  PositionType pos = this->SnapshotData.Truncate();
  this->ExecutionListFiles.Truncate();

259
260
261
262
263
264
265
266
267
  {
  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();
268
  it->DirectoryEnd = pos;
269
  }
270

271
272
273
274
275
276
277
  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());

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  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);
297
298

  return Snapshot(this, pos);
299
300
301
302
303
304
305
306
307
308
309
310
311
}

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

312
cmPropertyDefinition const* cmState
313
::GetPropertyDefinition(const std::string& name,
314
                        cmProperty::ScopeType scope) const
315
316
317
{
  if (this->IsPropertyDefined(name,scope))
    {
318
319
320
    cmPropertyDefinitionMap const& defs =
        this->PropertyDefinitions.find(scope)->second;
    return &defs.find(name)->second;
321
322
323
324
325
    }
  return 0;
}

bool cmState::IsPropertyDefined(const std::string& name,
326
                                cmProperty::ScopeType scope) const
327
{
328
329
330
331
332
333
334
  std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
      = this->PropertyDefinitions.find(scope);
  if (it == this->PropertyDefinitions.end())
    {
    return false;
    }
  return it->second.IsPropertyDefined(name);
335
336
337
}

bool cmState::IsPropertyChained(const std::string& name,
338
                                cmProperty::ScopeType scope) const
339
{
340
341
342
343
344
345
346
  std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
      = this->PropertyDefinitions.find(scope);
  if (it == this->PropertyDefinitions.end())
    {
    return false;
    }
  return it->second.IsPropertyChained(name);
347
}
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370

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

371
372
373
374
375
void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
{
  this->EnabledLanguages = langs;
}

376
377
378
379
void cmState::ClearEnabledLanguages()
{
  this->EnabledLanguages.clear();
}
380
381
382
383
384
385
386
387
388
389

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

void cmState::SetIsInTryCompile(bool b)
{
  this->IsInTryCompile = b;
}
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475

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()
{
476
  std::vector<cmCommand*> renamedCommands;
477
478
479
480
481
482
483
484
485
  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++);
      }
486
487
488
489
490
    else if (j->first != j->second->GetName())
      {
      renamedCommands.push_back(j->second);
      this->Commands.erase(j++);
      }
491
492
493
494
495
    else
      {
      ++j;
      }
    }
496
497
498
499
500
  for (std::vector<cmCommand*>::const_iterator it = renamedCommands.begin();
       it != renamedCommands.end(); ++it)
    {
    this->Commands[cmSystemTools::LowerCase((*it)->GetName())] = *it;
    }
501
}
502
503
504

void cmState::SetGlobalProperty(const std::string& prop, const char* value)
{
505
  this->GlobalProperties.SetProperty(prop, value);
506
507
508
509
510
}

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

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
547
  return this->GlobalProperties.GetPropertyValue(prop);
548
549
550
551
552
553
}

bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
{
  return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
}
554
555
556
557
558

void cmState::SetSourceDirectory(std::string const& sourceDirectory)
{
  this->SourceDirectory = sourceDirectory;
  cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
559
560
561
562

  cmSystemTools::SplitPath(
      cmSystemTools::CollapseFullPath(this->SourceDirectory),
        this->SourceDirectoryComponents);
563
564
565
566
567
568
569
}

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

570
571
572
573
574
std::vector<std::string> const& cmState::GetSourceDirectoryComponents() const
{
  return this->SourceDirectoryComponents;
}

575
576
577
578
void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
{
  this->BinaryDirectory = binaryDirectory;
  cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
579
580
581
582

  cmSystemTools::SplitPath(
      cmSystemTools::CollapseFullPath(this->BinaryDirectory),
        this->BinaryDirectoryComponents);
583
584
}

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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
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;
}

645
646
647
648
const char* cmState::GetBinaryDirectory() const
{
  return this->BinaryDirectory.c_str();
}
Stephen Kelly's avatar
Stephen Kelly committed
649

650
651
652
653
654
std::vector<std::string> const& cmState::GetBinaryDirectoryComponents() const
{
  return this->BinaryDirectoryComponents;
}

655
void cmState::Directory::ComputeRelativePathTopSource()
656
657
658
659
660
{
  // 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.

661
  cmState::Snapshot snapshot = this->Snapshot_;
662
663
664
665
  std::vector<cmState::Snapshot> snapshots;
  snapshots.push_back(snapshot);
  while (true)
    {
666
    snapshot = snapshot.GetBuildsystemDirectoryParent();
667
668
669
670
671
672
673
674
675
676
    if (snapshot.IsValid())
      {
      snapshots.push_back(snapshot);
      }
    else
      {
      break;
      }
    }

677
  std::string result = snapshots.front().GetDirectory().GetCurrentSource();
678
679
680
681

  for (std::vector<cmState::Snapshot>::const_iterator it =
       snapshots.begin() + 1; it != snapshots.end(); ++it)
    {
682
    std::string currentSource = it->GetDirectory().GetCurrentSource();
683
684
685
686
687
    if(cmSystemTools::IsSubDirectory(result, currentSource))
      {
      result = currentSource;
      }
    }
688
  this->DirectoryState->RelativePathTopSource = result;
689
690
}

691
void cmState::Directory::ComputeRelativePathTopBinary()
692
{
693
  cmState::Snapshot snapshot = this->Snapshot_;
694
695
696
697
  std::vector<cmState::Snapshot> snapshots;
  snapshots.push_back(snapshot);
  while (true)
    {
698
    snapshot = snapshot.GetBuildsystemDirectoryParent();
699
700
701
702
703
704
705
706
707
708
709
    if (snapshot.IsValid())
      {
      snapshots.push_back(snapshot);
      }
    else
      {
      break;
      }
    }

  std::string result =
710
      snapshots.front().GetDirectory().GetCurrentBinary();
711
712
713
714

  for (std::vector<cmState::Snapshot>::const_iterator it =
       snapshots.begin() + 1; it != snapshots.end(); ++it)
    {
715
    std::string currentBinary = it->GetDirectory().GetCurrentBinary();
716
717
718
719
720
721
722
723
724
725
726
    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) != "//")
    {
727
    this->DirectoryState->RelativePathTopBinary = result;
728
729
730
    }
  else
    {
731
    this->DirectoryState->RelativePathTopBinary = "";
732
733
734
    }
}

735
736
cmState::Snapshot cmState::CreateBaseSnapshot()
{
737
738
739
740
741
  PositionType pos = this->SnapshotData.Extend(this->SnapshotData.Root());
  pos->DirectoryParent = this->SnapshotData.Root();
  pos->SnapshotType = BuildsystemDirectoryType;
  pos->BuildSystemDirectory =
      this->BuildsystemDirectory.Extend(this->BuildsystemDirectory.Root());
742
743
  pos->ExecutionListFile =
      this->ExecutionListFiles.Extend(this->ExecutionListFiles.Root());
744
745
746
  pos->IncludeDirectoryPosition = 0;
  pos->CompileDefinitionsPosition = 0;
  pos->CompileOptionsPosition = 0;
747
  pos->BuildSystemDirectory->DirectoryEnd = pos;
748
749
750
751
752
  pos->Policies = this->PolicyStack.Root();
  pos->PolicyRoot = this->PolicyStack.Root();
  pos->PolicyScope = this->PolicyStack.Root();
  assert(pos->Policies.IsValid());
  assert(pos->PolicyRoot.IsValid());
753
754
755
  return cmState::Snapshot(this, pos);
}

756
cmState::Snapshot
757
758
759
cmState::CreateBuildsystemDirectorySnapshot(Snapshot originSnapshot,
                                    std::string const& entryPointCommand,
                                    long entryPointLine)
Stephen Kelly's avatar
Stephen Kelly committed
760
{
761
  assert(originSnapshot.IsValid());
762
  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position);
763
764
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
765
766
767
768
769
  pos->DirectoryParent = originSnapshot.Position;
  pos->SnapshotType = BuildsystemDirectoryType;
  pos->BuildSystemDirectory =
      this->BuildsystemDirectory.Extend(
        originSnapshot.Position->BuildSystemDirectory);
770
771
772
  pos->ExecutionListFile =
      this->ExecutionListFiles.Extend(
        originSnapshot.Position->ExecutionListFile);
773
  pos->BuildSystemDirectory->DirectoryEnd = pos;
774
775
776
777
778
  pos->Policies = originSnapshot.Position->Policies;
  pos->PolicyRoot = originSnapshot.Position->Policies;
  pos->PolicyScope = originSnapshot.Position->Policies;
  assert(pos->Policies.IsValid());
  assert(pos->PolicyRoot.IsValid());
Stephen Kelly's avatar
Stephen Kelly committed
779
780
781
  return cmState::Snapshot(this, pos);
}

782
cmState::Snapshot
783
784
785
786
cmState::CreateFunctionCallSnapshot(cmState::Snapshot originSnapshot,
                                    std::string const& entryPointCommand,
                                    long entryPointLine,
                                    std::string const& fileName)
787
788
789
{
  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
                                               *originSnapshot.Position);
790
791
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
792
  pos->SnapshotType = FunctionCallType;
793
794
  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
        originSnapshot.Position->ExecutionListFile, fileName);
795
  pos->BuildSystemDirectory->DirectoryEnd = pos;
796
  pos->PolicyScope = originSnapshot.Position->Policies;
797
798
799
800
801
  return cmState::Snapshot(this, pos);
}


cmState::Snapshot
802
803
804
805
cmState::CreateMacroCallSnapshot(cmState::Snapshot originSnapshot,
                                    std::string const& entryPointCommand,
                                    long entryPointLine,
                                    std::string const& fileName)
806
807
808
{
  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
                                               *originSnapshot.Position);
809
810
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
811
  pos->SnapshotType = MacroCallType;
812
813
  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
        originSnapshot.Position->ExecutionListFile, fileName);
814
  pos->BuildSystemDirectory->DirectoryEnd = pos;
815
  pos->PolicyScope = originSnapshot.Position->Policies;
816
817
818
819
  return cmState::Snapshot(this, pos);
}

cmState::Snapshot
820
821
822
823
cmState::CreateCallStackSnapshot(cmState::Snapshot originSnapshot,
                                 const std::string& entryPointCommand,
                                 long entryPointLine,
                                 const std::string& fileName)
824
825
826
{
  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
                                               *originSnapshot.Position);
827
828
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
829
  pos->SnapshotType = CallStackType;
830
831
  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
        originSnapshot.Position->ExecutionListFile, fileName);
832
  pos->BuildSystemDirectory->DirectoryEnd = pos;
833
  pos->PolicyScope = originSnapshot.Position->Policies;
834
835
836
837
  return cmState::Snapshot(this, pos);
}

cmState::Snapshot
838
839
840
841
cmState::CreateInlineListFileSnapshot(cmState::Snapshot originSnapshot,
                                      const std::string& entryPointCommand,
                                      long entryPointLine,
                                      const std::string& fileName)
842
843
844
{
  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
                                               *originSnapshot.Position);
845
846
  pos->EntryPointLine = entryPointLine;
  pos->EntryPointCommand = entryPointCommand;
847
  pos->SnapshotType = InlineListFileType;
848
849
  pos->ExecutionListFile = this->ExecutionListFiles.Extend(
        originSnapshot.Position->ExecutionListFile, fileName);
850
  pos->BuildSystemDirectory->DirectoryEnd = pos;
851
  pos->PolicyScope = originSnapshot.Position->Policies;
852
853
854
  return cmState::Snapshot(this, pos);
}

855
856
857
858
859
860
cmState::Snapshot
cmState::CreatePolicyScopeSnapshot(cmState::Snapshot originSnapshot)
{
  PositionType pos = this->SnapshotData.Extend(originSnapshot.Position,
                                               *originSnapshot.Position);
  pos->SnapshotType = PolicyScopeType;
861
  pos->BuildSystemDirectory->DirectoryEnd = pos;
862
  pos->PolicyScope = originSnapshot.Position->Policies;
863
864
865
  return cmState::Snapshot(this, pos);
}

866
867
868
869
870
cmState::Snapshot cmState::Pop(cmState::Snapshot originSnapshot)
{
  PositionType pos = originSnapshot.Position;
  PositionType prevPos = pos;
  ++prevPos;
871
872
873
874
875
876
  prevPos->IncludeDirectoryPosition =
      prevPos->BuildSystemDirectory->IncludeDirectories.size();
  prevPos->CompileDefinitionsPosition =
      prevPos->BuildSystemDirectory->CompileDefinitions.size();
  prevPos->CompileOptionsPosition =
      prevPos->BuildSystemDirectory->CompileOptions.size();
877
  prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
878

879
  return Snapshot(this, prevPos);
880
881
}

882
883
884
885
886
887
cmState::Snapshot::Snapshot(cmState* state)
  : State(state)
  , Position()
{
}

Stephen Kelly's avatar
Stephen Kelly committed
888
889
890
891
892
893
cmState::Snapshot::Snapshot(cmState* state, PositionType position)
  : State(state),
  Position(position)
{

}
894

895
896
897
898
899
cmState::SnapshotType cmState::Snapshot::GetType() const
{
  return this->Position->SnapshotType;
}

900
const char* cmState::Directory::GetCurrentSource() const
901
{
902
  return this->DirectoryState->Location.c_str();
903
904
}

905
void cmState::Directory::SetCurrentSource(std::string const& dir)
906
{
907
  std::string& loc = this->DirectoryState->Location;
908
909
910
  loc = dir;
  cmSystemTools::ConvertToUnixSlashes(loc);
  loc = cmSystemTools::CollapseFullPath(loc);
911
912

  cmSystemTools::SplitPath(
913
      loc,
914
      this->DirectoryState->CurrentSourceDirectoryComponents);
915
  this->ComputeRelativePathTopSource();
916
917
}

918
const char* cmState::Directory::GetCurrentBinary() const
919
{
920
  return this->DirectoryState->OutputLocation.c_str();
921
922
}

923
void cmState::Directory::SetCurrentBinary(std::string const& dir)
924
{
925
  std::string& loc = this->DirectoryState->OutputLocation;
926
927
928
  loc = dir;
  cmSystemTools::ConvertToUnixSlashes(loc);
  loc = cmSystemTools::CollapseFullPath(loc);
929
930

  cmSystemTools::SplitPath(
931
      loc,
932
      this->DirectoryState->CurrentBinaryDirectoryComponents);
933
  this->ComputeRelativePathTopBinary();
934
935
}

936
937
938
939
940
void cmState::Snapshot::SetListFile(const std::string& listfile)
{
  *this->Position->ExecutionListFile = listfile;
}

941
std::vector<std::string> const&
942
cmState::Directory::GetCurrentSourceComponents() const
943
{
944
  return this->DirectoryState->CurrentSourceDirectoryComponents;
945
946
947
}

std::vector<std::string> const&
948
cmState::Directory::GetCurrentBinaryComponents() const
949
{
950
  return this->DirectoryState->CurrentBinaryDirectoryComponents;
951
}
952

953
const char* cmState::Directory::GetRelativePathTopSource() const
954
{
955
  return this->DirectoryState->RelativePathTopSource.c_str();
956
957
}

958
const char* cmState::Directory::GetRelativePathTopBinary() const
959
{
960
  return this->DirectoryState->RelativePathTopBinary.c_str();
961
962
}

963
void cmState::Directory::SetRelativePathTopSource(const char* dir)
964
{
965
  this->DirectoryState->RelativePathTopSource = dir;
966
967
}

968
void cmState::Directory::SetRelativePathTopBinary(const char* dir)
969
{
970
  this->DirectoryState->RelativePathTopBinary = dir;
971
972
}

973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
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;
}

988
989
bool cmState::Snapshot::IsValid() const
{
990
991
992
  return this->State && this->Position.IsValid()
      ? this->Position != this->State->SnapshotData.Root()
      : false;
993
994
}

995
cmState::Snapshot cmState::Snapshot::GetBuildsystemDirectoryParent() const
996
997
{
  Snapshot snapshot;
998
  if (!this->State || this->Position == this->State->SnapshotData.Root())
999
1000
    {
    return snapshot;