cmState.cxx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2015 Stephen Kelly <[email protected]>
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmState.h"
  11. #include "cmake.h"
  12. #include "cmCacheManager.h"
  13. #include "cmCommand.h"
  14. #include "cmAlgorithms.h"
  15. #include <assert.h>
  16. struct cmState::SnapshotDataType
  17. {
  18. cmState::PositionType DirectoryParent;
  19. cmState::SnapshotType SnapshotType;
  20. cmLinkedTree<cmState::BuildsystemDirectoryStateType>::iterator
  21. BuildSystemDirectory;
  22. };
  23. struct cmState::BuildsystemDirectoryStateType
  24. {
  25. std::string Location;
  26. std::string OutputLocation;
  27. std::vector<std::string> CurrentSourceDirectoryComponents;
  28. std::vector<std::string> CurrentBinaryDirectoryComponents;
  29. // The top-most directories for relative path conversion. Both the
  30. // source and destination location of a relative path conversion
  31. // must be underneath one of these directories (both under source or
  32. // both under binary) in order for the relative path to be evaluated
  33. // safely by the build tools.
  34. std::string RelativePathTopSource;
  35. std::string RelativePathTopBinary;
  36. };
  37. cmState::cmState(cmake* cm)
  38. : CMakeInstance(cm),
  39. IsInTryCompile(false),
  40. WindowsShell(false),
  41. WindowsVSIDE(false),
  42. WatcomWMake(false),
  43. MinGWMake(false),
  44. NMake(false),
  45. MSYSShell(false)
  46. {
  47. }
  48. cmState::~cmState()
  49. {
  50. cmDeleteAll(this->Commands);
  51. }
  52. const char* cmCacheEntryTypes[] =
  53. { "BOOL",
  54. "PATH",
  55. "FILEPATH",
  56. "STRING",
  57. "INTERNAL",
  58. "STATIC",
  59. "UNINITIALIZED",
  60. 0
  61. };
  62. const char*
  63. cmState::CacheEntryTypeToString(cmState::CacheEntryType type)
  64. {
  65. if ( type > 6 )
  66. {
  67. return cmCacheEntryTypes[6];
  68. }
  69. return cmCacheEntryTypes[type];
  70. }
  71. cmState::CacheEntryType
  72. cmState::StringToCacheEntryType(const char* s)
  73. {
  74. int i = 0;
  75. while(cmCacheEntryTypes[i])
  76. {
  77. if(strcmp(s, cmCacheEntryTypes[i]) == 0)
  78. {
  79. return static_cast<cmState::CacheEntryType>(i);
  80. }
  81. ++i;
  82. }
  83. return STRING;
  84. }
  85. bool cmState::IsCacheEntryType(std::string const& key)
  86. {
  87. for(int i=0; cmCacheEntryTypes[i]; ++i)
  88. {
  89. if(strcmp(key.c_str(), cmCacheEntryTypes[i]) == 0)
  90. {
  91. return true;
  92. }
  93. }
  94. return false;
  95. }
  96. std::vector<std::string> cmState::GetCacheEntryKeys() const
  97. {
  98. std::vector<std::string> definitions;
  99. definitions.reserve(this->CMakeInstance->GetCacheManager()->GetSize());
  100. cmCacheManager::CacheIterator cit =
  101. this->CMakeInstance->GetCacheManager()->GetCacheIterator();
  102. for ( cit.Begin(); !cit.IsAtEnd(); cit.Next() )
  103. {
  104. definitions.push_back(cit.GetName());
  105. }
  106. return definitions;
  107. }
  108. const char* cmState::GetCacheEntryValue(std::string const& key) const
  109. {
  110. cmCacheManager::CacheEntry* e = this->CMakeInstance->GetCacheManager()
  111. ->GetCacheEntry(key);
  112. if (!e)
  113. {
  114. return 0;
  115. }
  116. return e->Value.c_str();
  117. }
  118. const char*
  119. cmState::GetInitializedCacheValue(std::string const& key) const
  120. {
  121. return this->CMakeInstance->GetCacheManager()->GetInitializedCacheValue(key);
  122. }
  123. cmState::CacheEntryType
  124. cmState::GetCacheEntryType(std::string const& key) const
  125. {
  126. cmCacheManager::CacheIterator it =
  127. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  128. return it.GetType();
  129. }
  130. void cmState::SetCacheEntryValue(std::string const& key,
  131. std::string const& value)
  132. {
  133. this->CMakeInstance->GetCacheManager()->SetCacheEntryValue(key, value);
  134. }
  135. void cmState::SetCacheEntryProperty(std::string const& key,
  136. std::string const& propertyName,
  137. std::string const& value)
  138. {
  139. cmCacheManager::CacheIterator it =
  140. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  141. it.SetProperty(propertyName, value.c_str());
  142. }
  143. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  144. std::string const& propertyName,
  145. bool value)
  146. {
  147. cmCacheManager::CacheIterator it =
  148. this->CMakeInstance->GetCacheManager()->GetCacheIterator(key.c_str());
  149. it.SetProperty(propertyName, value);
  150. }
  151. const char* cmState::GetCacheEntryProperty(std::string const& key,
  152. std::string const& propertyName)
  153. {
  154. cmCacheManager::CacheIterator it = this->CMakeInstance->GetCacheManager()
  155. ->GetCacheIterator(key.c_str());
  156. if (!it.PropertyExists(propertyName))
  157. {
  158. return 0;
  159. }
  160. return it.GetProperty(propertyName);
  161. }
  162. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  163. std::string const& propertyName)
  164. {
  165. return this->CMakeInstance->GetCacheManager()
  166. ->GetCacheIterator(key.c_str()).GetPropertyAsBool(propertyName);
  167. }
  168. void cmState::AddCacheEntry(const std::string& key, const char* value,
  169. const char* helpString,
  170. cmState::CacheEntryType type)
  171. {
  172. this->CMakeInstance->GetCacheManager()->AddCacheEntry(key, value,
  173. helpString, type);
  174. }
  175. void cmState::RemoveCacheEntry(std::string const& key)
  176. {
  177. this->CMakeInstance->GetCacheManager()->RemoveCacheEntry(key);
  178. }
  179. void cmState::AppendCacheEntryProperty(const std::string& key,
  180. const std::string& property,
  181. const std::string& value,
  182. bool asString)
  183. {
  184. this->CMakeInstance->GetCacheManager()
  185. ->GetCacheIterator(key.c_str()).AppendProperty(property,
  186. value.c_str(),
  187. asString);
  188. }
  189. void cmState::RemoveCacheEntryProperty(std::string const& key,
  190. std::string const& propertyName)
  191. {
  192. this->CMakeInstance->GetCacheManager()
  193. ->GetCacheIterator(key.c_str()).SetProperty(propertyName, (void*)0);
  194. }
  195. void cmState::Reset()
  196. {
  197. this->GlobalProperties.clear();
  198. this->PropertyDefinitions.clear();
  199. this->BuildsystemDirectory.Truncate();
  200. this->SnapshotData.Truncate();
  201. this->DefineProperty
  202. ("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY,
  203. "", "", true);
  204. this->DefineProperty
  205. ("RULE_LAUNCH_LINK", cmProperty::DIRECTORY,
  206. "", "", true);
  207. this->DefineProperty
  208. ("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY,
  209. "", "", true);
  210. this->DefineProperty
  211. ("RULE_LAUNCH_COMPILE", cmProperty::TARGET,
  212. "", "", true);
  213. this->DefineProperty
  214. ("RULE_LAUNCH_LINK", cmProperty::TARGET,
  215. "", "", true);
  216. this->DefineProperty
  217. ("RULE_LAUNCH_CUSTOM", cmProperty::TARGET,
  218. "", "", true);
  219. }
  220. void cmState::DefineProperty(const std::string& name,
  221. cmProperty::ScopeType scope,
  222. const char *ShortDescription,
  223. const char *FullDescription,
  224. bool chained)
  225. {
  226. this->PropertyDefinitions[scope].DefineProperty(name,scope,ShortDescription,
  227. FullDescription,
  228. chained);
  229. }
  230. cmPropertyDefinition const* cmState
  231. ::GetPropertyDefinition(const std::string& name,
  232. cmProperty::ScopeType scope) const
  233. {
  234. if (this->IsPropertyDefined(name,scope))
  235. {
  236. cmPropertyDefinitionMap const& defs =
  237. this->PropertyDefinitions.find(scope)->second;
  238. return &defs.find(name)->second;
  239. }
  240. return 0;
  241. }
  242. bool cmState::IsPropertyDefined(const std::string& name,
  243. cmProperty::ScopeType scope) const
  244. {
  245. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
  246. = this->PropertyDefinitions.find(scope);
  247. if (it == this->PropertyDefinitions.end())
  248. {
  249. return false;
  250. }
  251. return it->second.IsPropertyDefined(name);
  252. }
  253. bool cmState::IsPropertyChained(const std::string& name,
  254. cmProperty::ScopeType scope) const
  255. {
  256. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it
  257. = this->PropertyDefinitions.find(scope);
  258. if (it == this->PropertyDefinitions.end())
  259. {
  260. return false;
  261. }
  262. return it->second.IsPropertyChained(name);
  263. }
  264. void cmState::SetLanguageEnabled(std::string const& l)
  265. {
  266. std::vector<std::string>::iterator it =
  267. std::lower_bound(this->EnabledLanguages.begin(),
  268. this->EnabledLanguages.end(), l);
  269. if (it == this->EnabledLanguages.end() || *it != l)
  270. {
  271. this->EnabledLanguages.insert(it, l);
  272. }
  273. }
  274. bool cmState::GetLanguageEnabled(std::string const& l) const
  275. {
  276. return std::binary_search(this->EnabledLanguages.begin(),
  277. this->EnabledLanguages.end(), l);
  278. }
  279. std::vector<std::string> cmState::GetEnabledLanguages() const
  280. {
  281. return this->EnabledLanguages;
  282. }
  283. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  284. {
  285. this->EnabledLanguages = langs;
  286. }
  287. void cmState::ClearEnabledLanguages()
  288. {
  289. this->EnabledLanguages.clear();
  290. }
  291. bool cmState::GetIsInTryCompile() const
  292. {
  293. return this->IsInTryCompile;
  294. }
  295. void cmState::SetIsInTryCompile(bool b)
  296. {
  297. this->IsInTryCompile = b;
  298. }
  299. void cmState::RenameCommand(std::string const& oldName,
  300. std::string const& newName)
  301. {
  302. // if the command already exists, free the old one
  303. std::string sOldName = cmSystemTools::LowerCase(oldName);
  304. std::string sNewName = cmSystemTools::LowerCase(newName);
  305. std::map<std::string, cmCommand*>::iterator pos =
  306. this->Commands.find(sOldName);
  307. if ( pos == this->Commands.end() )
  308. {
  309. return;
  310. }
  311. cmCommand* cmd = pos->second;
  312. pos = this->Commands.find(sNewName);
  313. if (pos != this->Commands.end())
  314. {
  315. delete pos->second;
  316. this->Commands.erase(pos);
  317. }
  318. this->Commands.insert(std::make_pair(sNewName, cmd));
  319. pos = this->Commands.find(sOldName);
  320. this->Commands.erase(pos);
  321. }
  322. void cmState::AddCommand(cmCommand* command)
  323. {
  324. std::string name = cmSystemTools::LowerCase(command->GetName());
  325. // if the command already exists, free the old one
  326. std::map<std::string, cmCommand*>::iterator pos = this->Commands.find(name);
  327. if (pos != this->Commands.end())
  328. {
  329. delete pos->second;
  330. this->Commands.erase(pos);
  331. }
  332. this->Commands.insert(std::make_pair(name, command));
  333. }
  334. void cmState::RemoveUnscriptableCommands()
  335. {
  336. std::vector<std::string> unscriptableCommands;
  337. for (std::map<std::string, cmCommand*>::iterator
  338. pos = this->Commands.begin();
  339. pos != this->Commands.end(); )
  340. {
  341. if (!pos->second->IsScriptable())
  342. {
  343. delete pos->second;
  344. this->Commands.erase(pos++);
  345. }
  346. else
  347. {
  348. ++pos;
  349. }
  350. }
  351. }
  352. cmCommand* cmState::GetCommand(std::string const& name) const
  353. {
  354. cmCommand* command = 0;
  355. std::string sName = cmSystemTools::LowerCase(name);
  356. std::map<std::string, cmCommand*>::const_iterator pos =
  357. this->Commands.find(sName);
  358. if (pos != this->Commands.end())
  359. {
  360. command = (*pos).second;
  361. }
  362. return command;
  363. }
  364. std::vector<std::string> cmState::GetCommandNames() const
  365. {
  366. std::vector<std::string> commandNames;
  367. commandNames.reserve(this->Commands.size());
  368. std::map<std::string, cmCommand*>::const_iterator cmds
  369. = this->Commands.begin();
  370. for ( ; cmds != this->Commands.end(); ++ cmds )
  371. {
  372. commandNames.push_back(cmds->first);
  373. }
  374. return commandNames;
  375. }
  376. void cmState::RemoveUserDefinedCommands()
  377. {
  378. for(std::map<std::string, cmCommand*>::iterator j = this->Commands.begin();
  379. j != this->Commands.end(); )
  380. {
  381. if (j->second->IsA("cmMacroHelperCommand") ||
  382. j->second->IsA("cmFunctionHelperCommand"))
  383. {
  384. delete j->second;
  385. this->Commands.erase(j++);
  386. }
  387. else
  388. {
  389. ++j;
  390. }
  391. }
  392. }
  393. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  394. {
  395. this->GlobalProperties.SetProperty(prop, value);
  396. }
  397. void cmState::AppendGlobalProperty(const std::string& prop,
  398. const char* value, bool asString)
  399. {
  400. this->GlobalProperties.AppendProperty(prop, value, asString);
  401. }
  402. const char *cmState::GetGlobalProperty(const std::string& prop)
  403. {
  404. // watch for special properties
  405. std::string output = "";
  406. if ( prop == "CACHE_VARIABLES" )
  407. {
  408. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  409. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  410. }
  411. else if ( prop == "COMMANDS" )
  412. {
  413. std::vector<std::string> commands = this->GetCommandNames();
  414. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  415. }
  416. else if ( prop == "IN_TRY_COMPILE" )
  417. {
  418. this->SetGlobalProperty("IN_TRY_COMPILE",
  419. this->IsInTryCompile ? "1" : "0");
  420. }
  421. else if ( prop == "ENABLED_LANGUAGES" )
  422. {
  423. std::string langs;
  424. langs = cmJoin(this->EnabledLanguages, ";");
  425. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  426. }
  427. #define STRING_LIST_ELEMENT(F) ";" #F
  428. if (prop == "CMAKE_C_KNOWN_FEATURES")
  429. {
  430. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  431. }
  432. if (prop == "CMAKE_CXX_KNOWN_FEATURES")
  433. {
  434. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  435. }
  436. #undef STRING_LIST_ELEMENT
  437. return this->GlobalProperties.GetPropertyValue(prop);
  438. }
  439. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  440. {
  441. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  442. }
  443. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  444. {
  445. this->SourceDirectory = sourceDirectory;
  446. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  447. cmSystemTools::SplitPath(
  448. cmSystemTools::CollapseFullPath(this->SourceDirectory),
  449. this->SourceDirectoryComponents);
  450. }
  451. const char* cmState::GetSourceDirectory() const
  452. {
  453. return this->SourceDirectory.c_str();
  454. }
  455. std::vector<std::string> const& cmState::GetSourceDirectoryComponents() const
  456. {
  457. return this->SourceDirectoryComponents;
  458. }
  459. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  460. {
  461. this->BinaryDirectory = binaryDirectory;
  462. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  463. cmSystemTools::SplitPath(
  464. cmSystemTools::CollapseFullPath(this->BinaryDirectory),
  465. this->BinaryDirectoryComponents);
  466. }
  467. void cmState::SetWindowsShell(bool windowsShell)
  468. {
  469. this->WindowsShell = windowsShell;
  470. }
  471. bool cmState::UseWindowsShell() const
  472. {
  473. return this->WindowsShell;
  474. }
  475. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  476. {
  477. this->WindowsVSIDE = windowsVSIDE;
  478. }
  479. bool cmState::UseWindowsVSIDE() const
  480. {
  481. return this->WindowsVSIDE;
  482. }
  483. void cmState::SetWatcomWMake(bool watcomWMake)
  484. {
  485. this->WatcomWMake = watcomWMake;
  486. }
  487. bool cmState::UseWatcomWMake() const
  488. {
  489. return this->WatcomWMake;
  490. }
  491. void cmState::SetMinGWMake(bool minGWMake)
  492. {
  493. this->MinGWMake = minGWMake;
  494. }
  495. bool cmState::UseMinGWMake() const
  496. {
  497. return this->MinGWMake;
  498. }
  499. void cmState::SetNMake(bool nMake)
  500. {
  501. this->NMake = nMake;
  502. }
  503. bool cmState::UseNMake() const
  504. {
  505. return this->NMake;
  506. }
  507. void cmState::SetMSYSShell(bool mSYSShell)
  508. {
  509. this->MSYSShell = mSYSShell;
  510. }
  511. bool cmState::UseMSYSShell() const
  512. {
  513. return this->MSYSShell;
  514. }
  515. const char* cmState::GetBinaryDirectory() const
  516. {
  517. return this->BinaryDirectory.c_str();
  518. }
  519. std::vector<std::string> const& cmState::GetBinaryDirectoryComponents() const
  520. {
  521. return this->BinaryDirectoryComponents;
  522. }
  523. void cmState::Snapshot::ComputeRelativePathTopSource()
  524. {
  525. // Relative path conversion inside the source tree is not used to
  526. // construct relative paths passed to build tools so it is safe to use
  527. // even when the source is a network path.
  528. cmState::Snapshot snapshot = *this;
  529. std::vector<cmState::Snapshot> snapshots;
  530. snapshots.push_back(snapshot);
  531. while (true)
  532. {
  533. snapshot = snapshot.GetBuildsystemDirectoryParent();
  534. if (snapshot.IsValid())
  535. {
  536. snapshots.push_back(snapshot);
  537. }
  538. else
  539. {
  540. break;
  541. }
  542. }
  543. std::string result = snapshots.front().GetCurrentSourceDirectory();
  544. for (std::vector<cmState::Snapshot>::const_iterator it =
  545. snapshots.begin() + 1; it != snapshots.end(); ++it)
  546. {
  547. std::string currentSource = it->GetCurrentSourceDirectory();
  548. if(cmSystemTools::IsSubDirectory(result, currentSource))
  549. {
  550. result = currentSource;
  551. }
  552. }
  553. this->Position->BuildSystemDirectory->RelativePathTopSource = result;
  554. }
  555. void cmState::Snapshot::ComputeRelativePathTopBinary()
  556. {
  557. cmState::Snapshot snapshot = *this;
  558. std::vector<cmState::Snapshot> snapshots;
  559. snapshots.push_back(snapshot);
  560. while (true)
  561. {
  562. snapshot = snapshot.GetBuildsystemDirectoryParent();
  563. if (snapshot.IsValid())
  564. {
  565. snapshots.push_back(snapshot);
  566. }
  567. else
  568. {
  569. break;
  570. }
  571. }
  572. std::string result =
  573. snapshots.front().GetCurrentBinaryDirectory();
  574. for (std::vector<cmState::Snapshot>::const_iterator it =
  575. snapshots.begin() + 1; it != snapshots.end(); ++it)
  576. {
  577. std::string currentBinary = it->GetCurrentBinaryDirectory();
  578. if(cmSystemTools::IsSubDirectory(result, currentBinary))
  579. {
  580. result = currentBinary;
  581. }
  582. }
  583. // The current working directory on Windows cannot be a network
  584. // path. Therefore relative paths cannot work when the binary tree
  585. // is a network path.
  586. if(result.size() < 2 || result.substr(0, 2) != "//")
  587. {
  588. this->Position->BuildSystemDirectory->RelativePathTopBinary = result;
  589. }
  590. else
  591. {
  592. this->Position->BuildSystemDirectory->RelativePathTopBinary = "";
  593. }
  594. }
  595. cmState::Snapshot cmState::CreateBaseSnapshot()
  596. {
  597. PositionType pos = this->SnapshotData.Extend(this->SnapshotData.Root());
  598. pos->DirectoryParent = this->SnapshotData.Root();
  599. pos->SnapshotType = BuildsystemDirectoryType;
  600. pos->BuildSystemDirectory =
  601. this->BuildsystemDirectory.Extend(this->BuildsystemDirectory.Root());
  602. return cmState::Snapshot(this, pos);
  603. }
  604. cmState::Snapshot
  605. cmState::CreateBuildsystemDirectorySnapshot(Snapshot originSnapshot)
  606. {
  607. assert(originSnapshot.IsValid());
  608. PositionType pos = this->SnapshotData.Extend(originSnapshot.Position);
  609. pos->DirectoryParent = originSnapshot.Position;
  610. pos->SnapshotType = BuildsystemDirectoryType;
  611. pos->BuildSystemDirectory =
  612. this->BuildsystemDirectory.Extend(
  613. originSnapshot.Position->BuildSystemDirectory);
  614. return cmState::Snapshot(this, pos);
  615. }
  616. cmState::Snapshot::Snapshot(cmState* state, PositionType position)
  617. : State(state),
  618. Position(position)
  619. {
  620. }
  621. const char* cmState::Snapshot::GetCurrentSourceDirectory() const
  622. {
  623. return this->Position->BuildSystemDirectory->Location.c_str();
  624. }
  625. void cmState::Snapshot::SetCurrentSourceDirectory(std::string const& dir)
  626. {
  627. assert(this->State);
  628. std::string& loc = this->Position->BuildSystemDirectory->Location;
  629. loc = dir;
  630. cmSystemTools::ConvertToUnixSlashes(loc);
  631. loc = cmSystemTools::CollapseFullPath(loc);
  632. cmSystemTools::SplitPath(
  633. loc,
  634. this->Position->BuildSystemDirectory->CurrentSourceDirectoryComponents);
  635. this->ComputeRelativePathTopSource();
  636. }
  637. const char* cmState::Snapshot::GetCurrentBinaryDirectory() const
  638. {
  639. return this->Position->BuildSystemDirectory->OutputLocation.c_str();
  640. }
  641. void cmState::Snapshot::SetCurrentBinaryDirectory(std::string const& dir)
  642. {
  643. std::string& loc = this->Position->BuildSystemDirectory->OutputLocation;
  644. loc = dir;
  645. cmSystemTools::ConvertToUnixSlashes(loc);
  646. loc = cmSystemTools::CollapseFullPath(loc);
  647. cmSystemTools::SplitPath(
  648. loc,
  649. this->Position->BuildSystemDirectory->CurrentBinaryDirectoryComponents);
  650. this->ComputeRelativePathTopBinary();
  651. }
  652. std::vector<std::string> const&
  653. cmState::Snapshot::GetCurrentSourceDirectoryComponents()
  654. {
  655. return this->Position->BuildSystemDirectory
  656. ->CurrentSourceDirectoryComponents;
  657. }
  658. std::vector<std::string> const&
  659. cmState::Snapshot::GetCurrentBinaryDirectoryComponents()
  660. {
  661. return this->Position->BuildSystemDirectory
  662. ->CurrentBinaryDirectoryComponents;
  663. }
  664. const char* cmState::Snapshot::GetRelativePathTopSource() const
  665. {
  666. return this->Position->BuildSystemDirectory->RelativePathTopSource.c_str();
  667. }
  668. const char* cmState::Snapshot::GetRelativePathTopBinary() const
  669. {
  670. return this->Position->BuildSystemDirectory->RelativePathTopBinary.c_str();
  671. }
  672. void cmState::Snapshot::SetRelativePathTopSource(const char* dir)
  673. {
  674. this->Position->BuildSystemDirectory->RelativePathTopSource = dir;
  675. }
  676. void cmState::Snapshot::SetRelativePathTopBinary(const char* dir)
  677. {
  678. this->Position->BuildSystemDirectory->RelativePathTopBinary = dir;
  679. }
  680. bool cmState::Snapshot::IsValid() const
  681. {
  682. return this->State && this->Position.IsValid()
  683. ? this->Position != this->State->SnapshotData.Root()
  684. : false;
  685. }
  686. cmState::Snapshot cmState::Snapshot::GetBuildsystemDirectoryParent() const
  687. {
  688. Snapshot snapshot;
  689. if (!this->State || this->Position == this->State->SnapshotData.Root())
  690. {
  691. return snapshot;
  692. }
  693. PositionType parentPos = this->Position->DirectoryParent;
  694. if (parentPos != this->State->SnapshotData.Root())
  695. {
  696. snapshot = Snapshot(this->State, parentPos);
  697. }
  698. return snapshot;
  699. }
  700. cmState* cmState::Snapshot::GetState() const
  701. {
  702. return this->State;
  703. }