cmState.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmState.h"
  4. #include "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <utility>
  9. #include "cmAlgorithms.h"
  10. #include "cmCacheManager.h"
  11. #include "cmCommand.h"
  12. #include "cmDefinitions.h"
  13. #include "cmDisallowedCommand.h"
  14. #include "cmListFileCache.h"
  15. #include "cmStatePrivate.h"
  16. #include "cmStateSnapshot.h"
  17. #include "cmSystemTools.h"
  18. #include "cmUnexpectedCommand.h"
  19. #include "cmake.h"
  20. cmState::cmState()
  21. : IsInTryCompile(false)
  22. , IsGeneratorMultiConfig(false)
  23. , WindowsShell(false)
  24. , WindowsVSIDE(false)
  25. , WatcomWMake(false)
  26. , MinGWMake(false)
  27. , NMake(false)
  28. , MSYSShell(false)
  29. {
  30. this->CacheManager = new cmCacheManager;
  31. }
  32. cmState::~cmState()
  33. {
  34. delete this->CacheManager;
  35. cmDeleteAll(this->BuiltinCommands);
  36. cmDeleteAll(this->ScriptedCommands);
  37. }
  38. const char* cmState::GetTargetTypeName(cmStateEnums::TargetType targetType)
  39. {
  40. switch (targetType) {
  41. case cmStateEnums::STATIC_LIBRARY:
  42. return "STATIC_LIBRARY";
  43. case cmStateEnums::MODULE_LIBRARY:
  44. return "MODULE_LIBRARY";
  45. case cmStateEnums::SHARED_LIBRARY:
  46. return "SHARED_LIBRARY";
  47. case cmStateEnums::OBJECT_LIBRARY:
  48. return "OBJECT_LIBRARY";
  49. case cmStateEnums::EXECUTABLE:
  50. return "EXECUTABLE";
  51. case cmStateEnums::UTILITY:
  52. return "UTILITY";
  53. case cmStateEnums::GLOBAL_TARGET:
  54. return "GLOBAL_TARGET";
  55. case cmStateEnums::INTERFACE_LIBRARY:
  56. return "INTERFACE_LIBRARY";
  57. case cmStateEnums::UNKNOWN_LIBRARY:
  58. return "UNKNOWN_LIBRARY";
  59. }
  60. assert(false && "Unexpected target type");
  61. return nullptr;
  62. }
  63. const char* cmCacheEntryTypes[] = { "BOOL", "PATH", "FILEPATH",
  64. "STRING", "INTERNAL", "STATIC",
  65. "UNINITIALIZED", nullptr };
  66. const char* cmState::CacheEntryTypeToString(cmStateEnums::CacheEntryType type)
  67. {
  68. if (type > 6) {
  69. return cmCacheEntryTypes[6];
  70. }
  71. return cmCacheEntryTypes[type];
  72. }
  73. cmStateEnums::CacheEntryType cmState::StringToCacheEntryType(const char* s)
  74. {
  75. int i = 0;
  76. while (cmCacheEntryTypes[i]) {
  77. if (strcmp(s, cmCacheEntryTypes[i]) == 0) {
  78. return static_cast<cmStateEnums::CacheEntryType>(i);
  79. }
  80. ++i;
  81. }
  82. return cmStateEnums::STRING;
  83. }
  84. bool cmState::IsCacheEntryType(std::string const& key)
  85. {
  86. for (int i = 0; cmCacheEntryTypes[i]; ++i) {
  87. if (key == cmCacheEntryTypes[i]) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. bool cmState::LoadCache(const std::string& path, bool internal,
  94. std::set<std::string>& excludes,
  95. std::set<std::string>& includes)
  96. {
  97. return this->CacheManager->LoadCache(path, internal, excludes, includes);
  98. }
  99. bool cmState::SaveCache(const std::string& path, cmMessenger* messenger)
  100. {
  101. return this->CacheManager->SaveCache(path, messenger);
  102. }
  103. bool cmState::DeleteCache(const std::string& path)
  104. {
  105. return this->CacheManager->DeleteCache(path);
  106. }
  107. std::vector<std::string> cmState::GetCacheEntryKeys() const
  108. {
  109. std::vector<std::string> definitions;
  110. definitions.reserve(this->CacheManager->GetSize());
  111. cmCacheManager::CacheIterator cit = this->CacheManager->GetCacheIterator();
  112. for (cit.Begin(); !cit.IsAtEnd(); cit.Next()) {
  113. definitions.push_back(cit.GetName());
  114. }
  115. return definitions;
  116. }
  117. const char* cmState::GetCacheEntryValue(std::string const& key) const
  118. {
  119. cmCacheManager::CacheEntry* e = this->CacheManager->GetCacheEntry(key);
  120. if (!e) {
  121. return nullptr;
  122. }
  123. return e->Value.c_str();
  124. }
  125. const char* cmState::GetInitializedCacheValue(std::string const& key) const
  126. {
  127. return this->CacheManager->GetInitializedCacheValue(key);
  128. }
  129. cmStateEnums::CacheEntryType cmState::GetCacheEntryType(
  130. std::string const& key) const
  131. {
  132. cmCacheManager::CacheIterator it =
  133. this->CacheManager->GetCacheIterator(key.c_str());
  134. return it.GetType();
  135. }
  136. void cmState::SetCacheEntryValue(std::string const& key,
  137. std::string const& value)
  138. {
  139. this->CacheManager->SetCacheEntryValue(key, value);
  140. }
  141. void cmState::SetCacheEntryProperty(std::string const& key,
  142. std::string const& propertyName,
  143. std::string const& value)
  144. {
  145. cmCacheManager::CacheIterator it =
  146. this->CacheManager->GetCacheIterator(key.c_str());
  147. it.SetProperty(propertyName, value.c_str());
  148. }
  149. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  150. std::string const& propertyName,
  151. bool value)
  152. {
  153. cmCacheManager::CacheIterator it =
  154. this->CacheManager->GetCacheIterator(key.c_str());
  155. it.SetProperty(propertyName, value);
  156. }
  157. std::vector<std::string> cmState::GetCacheEntryPropertyList(
  158. const std::string& key)
  159. {
  160. cmCacheManager::CacheIterator it =
  161. this->CacheManager->GetCacheIterator(key.c_str());
  162. return it.GetPropertyList();
  163. }
  164. const char* cmState::GetCacheEntryProperty(std::string const& key,
  165. std::string const& propertyName)
  166. {
  167. cmCacheManager::CacheIterator it =
  168. this->CacheManager->GetCacheIterator(key.c_str());
  169. if (!it.PropertyExists(propertyName)) {
  170. return nullptr;
  171. }
  172. return it.GetProperty(propertyName);
  173. }
  174. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  175. std::string const& propertyName)
  176. {
  177. return this->CacheManager->GetCacheIterator(key.c_str())
  178. .GetPropertyAsBool(propertyName);
  179. }
  180. void cmState::AddCacheEntry(const std::string& key, const char* value,
  181. const char* helpString,
  182. cmStateEnums::CacheEntryType type)
  183. {
  184. this->CacheManager->AddCacheEntry(key, value, helpString, type);
  185. }
  186. void cmState::RemoveCacheEntry(std::string const& key)
  187. {
  188. this->CacheManager->RemoveCacheEntry(key);
  189. }
  190. void cmState::AppendCacheEntryProperty(const std::string& key,
  191. const std::string& property,
  192. const std::string& value, bool asString)
  193. {
  194. this->CacheManager->GetCacheIterator(key.c_str())
  195. .AppendProperty(property, value.c_str(), asString);
  196. }
  197. void cmState::RemoveCacheEntryProperty(std::string const& key,
  198. std::string const& propertyName)
  199. {
  200. this->CacheManager->GetCacheIterator(key.c_str())
  201. .SetProperty(propertyName, nullptr);
  202. }
  203. cmStateSnapshot cmState::Reset()
  204. {
  205. this->GlobalProperties.clear();
  206. this->PropertyDefinitions.clear();
  207. cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
  208. this->ExecutionListFiles.Truncate();
  209. {
  210. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator it =
  211. this->BuildsystemDirectory.Truncate();
  212. it->IncludeDirectories.clear();
  213. it->IncludeDirectoryBacktraces.clear();
  214. it->CompileDefinitions.clear();
  215. it->CompileDefinitionsBacktraces.clear();
  216. it->CompileOptions.clear();
  217. it->CompileOptionsBacktraces.clear();
  218. it->DirectoryEnd = pos;
  219. it->NormalTargetNames.clear();
  220. it->Properties.clear();
  221. it->Children.clear();
  222. }
  223. this->PolicyStack.Clear();
  224. pos->Policies = this->PolicyStack.Root();
  225. pos->PolicyRoot = this->PolicyStack.Root();
  226. pos->PolicyScope = this->PolicyStack.Root();
  227. assert(pos->Policies.IsValid());
  228. assert(pos->PolicyRoot.IsValid());
  229. {
  230. std::string srcDir =
  231. cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root);
  232. std::string binDir =
  233. cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root);
  234. this->VarTree.Clear();
  235. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  236. pos->Parent = this->VarTree.Root();
  237. pos->Root = this->VarTree.Root();
  238. pos->Vars->Set("CMAKE_SOURCE_DIR", srcDir.c_str());
  239. pos->Vars->Set("CMAKE_BINARY_DIR", binDir.c_str());
  240. }
  241. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY, "", "",
  242. true);
  243. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::DIRECTORY, "", "",
  244. true);
  245. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY, "", "",
  246. true);
  247. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::TARGET, "", "",
  248. true);
  249. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::TARGET, "", "", true);
  250. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::TARGET, "", "", true);
  251. return cmStateSnapshot(this, pos);
  252. }
  253. void cmState::DefineProperty(const std::string& name,
  254. cmProperty::ScopeType scope,
  255. const char* ShortDescription,
  256. const char* FullDescription, bool chained)
  257. {
  258. this->PropertyDefinitions[scope].DefineProperty(
  259. name, scope, ShortDescription, FullDescription, chained);
  260. }
  261. cmPropertyDefinition const* cmState::GetPropertyDefinition(
  262. const std::string& name, cmProperty::ScopeType scope) const
  263. {
  264. if (this->IsPropertyDefined(name, scope)) {
  265. cmPropertyDefinitionMap const& defs =
  266. this->PropertyDefinitions.find(scope)->second;
  267. return &defs.find(name)->second;
  268. }
  269. return nullptr;
  270. }
  271. bool cmState::IsPropertyDefined(const std::string& name,
  272. cmProperty::ScopeType scope) const
  273. {
  274. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it =
  275. this->PropertyDefinitions.find(scope);
  276. if (it == this->PropertyDefinitions.end()) {
  277. return false;
  278. }
  279. return it->second.IsPropertyDefined(name);
  280. }
  281. bool cmState::IsPropertyChained(const std::string& name,
  282. cmProperty::ScopeType scope) const
  283. {
  284. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it =
  285. this->PropertyDefinitions.find(scope);
  286. if (it == this->PropertyDefinitions.end()) {
  287. return false;
  288. }
  289. return it->second.IsPropertyChained(name);
  290. }
  291. void cmState::SetLanguageEnabled(std::string const& l)
  292. {
  293. std::vector<std::string>::iterator it = std::lower_bound(
  294. this->EnabledLanguages.begin(), this->EnabledLanguages.end(), l);
  295. if (it == this->EnabledLanguages.end() || *it != l) {
  296. this->EnabledLanguages.insert(it, l);
  297. }
  298. }
  299. bool cmState::GetLanguageEnabled(std::string const& l) const
  300. {
  301. return std::binary_search(this->EnabledLanguages.begin(),
  302. this->EnabledLanguages.end(), l);
  303. }
  304. std::vector<std::string> cmState::GetEnabledLanguages() const
  305. {
  306. return this->EnabledLanguages;
  307. }
  308. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  309. {
  310. this->EnabledLanguages = langs;
  311. }
  312. void cmState::ClearEnabledLanguages()
  313. {
  314. this->EnabledLanguages.clear();
  315. }
  316. bool cmState::GetIsInTryCompile() const
  317. {
  318. return this->IsInTryCompile;
  319. }
  320. void cmState::SetIsInTryCompile(bool b)
  321. {
  322. this->IsInTryCompile = b;
  323. }
  324. bool cmState::GetIsGeneratorMultiConfig() const
  325. {
  326. return this->IsGeneratorMultiConfig;
  327. }
  328. void cmState::SetIsGeneratorMultiConfig(bool b)
  329. {
  330. this->IsGeneratorMultiConfig = b;
  331. }
  332. void cmState::AddBuiltinCommand(std::string const& name, cmCommand* command)
  333. {
  334. assert(name == cmSystemTools::LowerCase(name));
  335. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  336. this->BuiltinCommands.insert(std::make_pair(name, command));
  337. }
  338. void cmState::AddDisallowedCommand(std::string const& name, cmCommand* command,
  339. cmPolicies::PolicyID policy,
  340. const char* message)
  341. {
  342. this->AddBuiltinCommand(name,
  343. new cmDisallowedCommand(command, policy, message));
  344. }
  345. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  346. {
  347. this->AddBuiltinCommand(name, new cmUnexpectedCommand(name, error));
  348. }
  349. void cmState::AddScriptedCommand(std::string const& name, cmCommand* command)
  350. {
  351. std::string sName = cmSystemTools::LowerCase(name);
  352. // if the command already exists, give a new name to the old command.
  353. if (cmCommand* oldCmd = this->GetCommand(sName)) {
  354. std::string const newName = "_" + sName;
  355. std::map<std::string, cmCommand*>::iterator pos =
  356. this->ScriptedCommands.find(newName);
  357. if (pos != this->ScriptedCommands.end()) {
  358. delete pos->second;
  359. this->ScriptedCommands.erase(pos);
  360. }
  361. this->ScriptedCommands.insert(std::make_pair(newName, oldCmd->Clone()));
  362. }
  363. // if the command already exists, free the old one
  364. std::map<std::string, cmCommand*>::iterator pos =
  365. this->ScriptedCommands.find(sName);
  366. if (pos != this->ScriptedCommands.end()) {
  367. delete pos->second;
  368. this->ScriptedCommands.erase(pos);
  369. }
  370. this->ScriptedCommands.insert(std::make_pair(sName, command));
  371. }
  372. cmCommand* cmState::GetCommand(std::string const& name) const
  373. {
  374. std::string sName = cmSystemTools::LowerCase(name);
  375. std::map<std::string, cmCommand*>::const_iterator pos;
  376. pos = this->ScriptedCommands.find(sName);
  377. if (pos != this->ScriptedCommands.end()) {
  378. return pos->second;
  379. }
  380. pos = this->BuiltinCommands.find(sName);
  381. if (pos != this->BuiltinCommands.end()) {
  382. return pos->second;
  383. }
  384. return nullptr;
  385. }
  386. std::vector<std::string> cmState::GetCommandNames() const
  387. {
  388. std::vector<std::string> commandNames;
  389. commandNames.reserve(this->BuiltinCommands.size() +
  390. this->ScriptedCommands.size());
  391. for (auto const& bc : this->BuiltinCommands) {
  392. commandNames.push_back(bc.first);
  393. }
  394. for (auto const& sc : this->ScriptedCommands) {
  395. commandNames.push_back(sc.first);
  396. }
  397. std::sort(commandNames.begin(), commandNames.end());
  398. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  399. commandNames.end());
  400. return commandNames;
  401. }
  402. void cmState::RemoveUserDefinedCommands()
  403. {
  404. cmDeleteAll(this->ScriptedCommands);
  405. this->ScriptedCommands.clear();
  406. }
  407. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  408. {
  409. this->GlobalProperties.SetProperty(prop, value);
  410. }
  411. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  412. bool asString)
  413. {
  414. this->GlobalProperties.AppendProperty(prop, value, asString);
  415. }
  416. const char* cmState::GetGlobalProperty(const std::string& prop)
  417. {
  418. if (prop == "CACHE_VARIABLES") {
  419. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  420. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  421. } else if (prop == "COMMANDS") {
  422. std::vector<std::string> commands = this->GetCommandNames();
  423. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  424. } else if (prop == "IN_TRY_COMPILE") {
  425. this->SetGlobalProperty("IN_TRY_COMPILE",
  426. this->IsInTryCompile ? "1" : "0");
  427. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  428. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  429. this->IsGeneratorMultiConfig ? "1" : "0");
  430. } else if (prop == "ENABLED_LANGUAGES") {
  431. std::string langs;
  432. langs = cmJoin(this->EnabledLanguages, ";");
  433. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  434. }
  435. #define STRING_LIST_ELEMENT(F) ";" #F
  436. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  437. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  438. }
  439. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  440. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  441. }
  442. #undef STRING_LIST_ELEMENT
  443. return this->GlobalProperties.GetPropertyValue(prop);
  444. }
  445. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  446. {
  447. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  448. }
  449. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  450. {
  451. this->SourceDirectory = sourceDirectory;
  452. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  453. }
  454. std::string const& cmState::GetSourceDirectory() const
  455. {
  456. return this->SourceDirectory;
  457. }
  458. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  459. {
  460. this->BinaryDirectory = binaryDirectory;
  461. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  462. }
  463. void cmState::SetWindowsShell(bool windowsShell)
  464. {
  465. this->WindowsShell = windowsShell;
  466. }
  467. bool cmState::UseWindowsShell() const
  468. {
  469. return this->WindowsShell;
  470. }
  471. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  472. {
  473. this->WindowsVSIDE = windowsVSIDE;
  474. }
  475. bool cmState::UseWindowsVSIDE() const
  476. {
  477. return this->WindowsVSIDE;
  478. }
  479. void cmState::SetWatcomWMake(bool watcomWMake)
  480. {
  481. this->WatcomWMake = watcomWMake;
  482. }
  483. bool cmState::UseWatcomWMake() const
  484. {
  485. return this->WatcomWMake;
  486. }
  487. void cmState::SetMinGWMake(bool minGWMake)
  488. {
  489. this->MinGWMake = minGWMake;
  490. }
  491. bool cmState::UseMinGWMake() const
  492. {
  493. return this->MinGWMake;
  494. }
  495. void cmState::SetNMake(bool nMake)
  496. {
  497. this->NMake = nMake;
  498. }
  499. bool cmState::UseNMake() const
  500. {
  501. return this->NMake;
  502. }
  503. void cmState::SetMSYSShell(bool mSYSShell)
  504. {
  505. this->MSYSShell = mSYSShell;
  506. }
  507. bool cmState::UseMSYSShell() const
  508. {
  509. return this->MSYSShell;
  510. }
  511. unsigned int cmState::GetCacheMajorVersion() const
  512. {
  513. return this->CacheManager->GetCacheMajorVersion();
  514. }
  515. unsigned int cmState::GetCacheMinorVersion() const
  516. {
  517. return this->CacheManager->GetCacheMinorVersion();
  518. }
  519. std::string const& cmState::GetBinaryDirectory() const
  520. {
  521. return this->BinaryDirectory;
  522. }
  523. cmStateSnapshot cmState::CreateBaseSnapshot()
  524. {
  525. cmStateDetail::PositionType pos =
  526. this->SnapshotData.Push(this->SnapshotData.Root());
  527. pos->DirectoryParent = this->SnapshotData.Root();
  528. pos->ScopeParent = this->SnapshotData.Root();
  529. pos->SnapshotType = cmStateEnums::BaseType;
  530. pos->Keep = true;
  531. pos->BuildSystemDirectory =
  532. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  533. pos->ExecutionListFile =
  534. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  535. pos->IncludeDirectoryPosition = 0;
  536. pos->CompileDefinitionsPosition = 0;
  537. pos->CompileOptionsPosition = 0;
  538. pos->BuildSystemDirectory->DirectoryEnd = pos;
  539. pos->Policies = this->PolicyStack.Root();
  540. pos->PolicyRoot = this->PolicyStack.Root();
  541. pos->PolicyScope = this->PolicyStack.Root();
  542. assert(pos->Policies.IsValid());
  543. assert(pos->PolicyRoot.IsValid());
  544. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  545. assert(pos->Vars.IsValid());
  546. pos->Parent = this->VarTree.Root();
  547. pos->Root = this->VarTree.Root();
  548. return cmStateSnapshot(this, pos);
  549. }
  550. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  551. cmStateSnapshot const& originSnapshot)
  552. {
  553. assert(originSnapshot.IsValid());
  554. cmStateDetail::PositionType pos =
  555. this->SnapshotData.Push(originSnapshot.Position);
  556. pos->DirectoryParent = originSnapshot.Position;
  557. pos->ScopeParent = originSnapshot.Position;
  558. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  559. pos->Keep = true;
  560. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  561. originSnapshot.Position->BuildSystemDirectory);
  562. pos->ExecutionListFile =
  563. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  564. pos->BuildSystemDirectory->DirectoryEnd = pos;
  565. pos->Policies = originSnapshot.Position->Policies;
  566. pos->PolicyRoot = originSnapshot.Position->Policies;
  567. pos->PolicyScope = originSnapshot.Position->Policies;
  568. assert(pos->Policies.IsValid());
  569. assert(pos->PolicyRoot.IsValid());
  570. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  571. pos->Parent = origin;
  572. pos->Root = origin;
  573. pos->Vars = this->VarTree.Push(origin);
  574. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  575. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  576. snapshot.SetDefaultDefinitions();
  577. snapshot.InitializeFromParent();
  578. snapshot.SetDirectoryDefinitions();
  579. return snapshot;
  580. }
  581. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  582. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  583. {
  584. cmStateDetail::PositionType pos =
  585. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  586. pos->ScopeParent = originSnapshot.Position;
  587. pos->SnapshotType = cmStateEnums::FunctionCallType;
  588. pos->Keep = false;
  589. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  590. originSnapshot.Position->ExecutionListFile, fileName);
  591. pos->BuildSystemDirectory->DirectoryEnd = pos;
  592. pos->PolicyScope = originSnapshot.Position->Policies;
  593. assert(originSnapshot.Position->Vars.IsValid());
  594. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  595. pos->Parent = origin;
  596. pos->Vars = this->VarTree.Push(origin);
  597. return cmStateSnapshot(this, pos);
  598. }
  599. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  600. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  601. {
  602. cmStateDetail::PositionType pos =
  603. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  604. pos->SnapshotType = cmStateEnums::MacroCallType;
  605. pos->Keep = false;
  606. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  607. originSnapshot.Position->ExecutionListFile, fileName);
  608. assert(originSnapshot.Position->Vars.IsValid());
  609. pos->BuildSystemDirectory->DirectoryEnd = pos;
  610. pos->PolicyScope = originSnapshot.Position->Policies;
  611. return cmStateSnapshot(this, pos);
  612. }
  613. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  614. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  615. {
  616. cmStateDetail::PositionType pos =
  617. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  618. pos->SnapshotType = cmStateEnums::IncludeFileType;
  619. pos->Keep = true;
  620. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  621. originSnapshot.Position->ExecutionListFile, fileName);
  622. assert(originSnapshot.Position->Vars.IsValid());
  623. pos->BuildSystemDirectory->DirectoryEnd = pos;
  624. pos->PolicyScope = originSnapshot.Position->Policies;
  625. return cmStateSnapshot(this, pos);
  626. }
  627. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  628. cmStateSnapshot const& originSnapshot)
  629. {
  630. cmStateDetail::PositionType pos =
  631. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  632. pos->ScopeParent = originSnapshot.Position;
  633. pos->SnapshotType = cmStateEnums::VariableScopeType;
  634. pos->Keep = false;
  635. pos->PolicyScope = originSnapshot.Position->Policies;
  636. assert(originSnapshot.Position->Vars.IsValid());
  637. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  638. pos->Parent = origin;
  639. pos->Vars = this->VarTree.Push(origin);
  640. assert(pos->Vars.IsValid());
  641. return cmStateSnapshot(this, pos);
  642. }
  643. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  644. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  645. {
  646. cmStateDetail::PositionType pos =
  647. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  648. pos->SnapshotType = cmStateEnums::InlineListFileType;
  649. pos->Keep = true;
  650. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  651. originSnapshot.Position->ExecutionListFile, fileName);
  652. pos->BuildSystemDirectory->DirectoryEnd = pos;
  653. pos->PolicyScope = originSnapshot.Position->Policies;
  654. return cmStateSnapshot(this, pos);
  655. }
  656. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  657. cmStateSnapshot const& originSnapshot)
  658. {
  659. cmStateDetail::PositionType pos =
  660. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  661. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  662. pos->Keep = false;
  663. pos->BuildSystemDirectory->DirectoryEnd = pos;
  664. pos->PolicyScope = originSnapshot.Position->Policies;
  665. return cmStateSnapshot(this, pos);
  666. }
  667. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  668. {
  669. cmStateDetail::PositionType pos = originSnapshot.Position;
  670. cmStateDetail::PositionType prevPos = pos;
  671. ++prevPos;
  672. prevPos->IncludeDirectoryPosition =
  673. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  674. prevPos->CompileDefinitionsPosition =
  675. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  676. prevPos->CompileOptionsPosition =
  677. prevPos->BuildSystemDirectory->CompileOptions.size();
  678. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  679. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  680. if (pos->Vars != prevPos->Vars) {
  681. assert(this->VarTree.IsLast(pos->Vars));
  682. this->VarTree.Pop(pos->Vars);
  683. }
  684. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  685. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  686. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  687. }
  688. this->SnapshotData.Pop(pos);
  689. }
  690. return cmStateSnapshot(this, prevPos);
  691. }
  692. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  693. std::string& value)
  694. {
  695. // input line is: key=value
  696. static cmsys::RegularExpression reg(
  697. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  698. // input line is: "key"=value
  699. static cmsys::RegularExpression regQuoted(
  700. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  701. bool flag = false;
  702. if (regQuoted.find(entry)) {
  703. var = regQuoted.match(1);
  704. value = regQuoted.match(2);
  705. flag = true;
  706. } else if (reg.find(entry)) {
  707. var = reg.match(1);
  708. value = reg.match(2);
  709. flag = true;
  710. }
  711. // if value is enclosed in single quotes ('foo') then remove them
  712. // it is used to enclose trailing space or tab
  713. if (flag && value.size() >= 2 && value[0] == '\'' &&
  714. value[value.size() - 1] == '\'') {
  715. value = value.substr(1, value.size() - 2);
  716. }
  717. return flag;
  718. }
  719. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  720. std::string& value,
  721. cmStateEnums::CacheEntryType& type)
  722. {
  723. // input line is: key:type=value
  724. static cmsys::RegularExpression reg(
  725. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  726. // input line is: "key":type=value
  727. static cmsys::RegularExpression regQuoted(
  728. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  729. bool flag = false;
  730. if (regQuoted.find(entry)) {
  731. var = regQuoted.match(1);
  732. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  733. value = regQuoted.match(3);
  734. flag = true;
  735. } else if (reg.find(entry)) {
  736. var = reg.match(1);
  737. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  738. value = reg.match(3);
  739. flag = true;
  740. }
  741. // if value is enclosed in single quotes ('foo') then remove them
  742. // it is used to enclose trailing space or tab
  743. if (flag && value.size() >= 2 && value[0] == '\'' &&
  744. value[value.size() - 1] == '\'') {
  745. value = value.substr(1, value.size() - 2);
  746. }
  747. if (!flag) {
  748. return ParseEntryWithoutType(entry, var, value);
  749. }
  750. return flag;
  751. }