cmState.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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 CM_NULLPTR;
  62. }
  63. const char* cmCacheEntryTypes[] = { "BOOL", "PATH", "FILEPATH",
  64. "STRING", "INTERNAL", "STATIC",
  65. "UNINITIALIZED", CM_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 (strcmp(key.c_str(), cmCacheEntryTypes[i]) == 0) {
  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)
  100. {
  101. return this->CacheManager->SaveCache(path);
  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 CM_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 CM_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, (void*)CM_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 CM_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(name == cmSystemTools::LowerCase(command->GetName()));
  336. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  337. this->BuiltinCommands.insert(std::make_pair(name, command));
  338. }
  339. void cmState::AddDisallowedCommand(std::string const& name, cmCommand* command,
  340. cmPolicies::PolicyID policy,
  341. const char* message)
  342. {
  343. this->AddBuiltinCommand(name,
  344. new cmDisallowedCommand(command, policy, message));
  345. }
  346. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  347. {
  348. this->AddBuiltinCommand(name, new cmUnexpectedCommand(name, error));
  349. }
  350. void cmState::AddScriptedCommand(std::string const& name, cmCommand* command)
  351. {
  352. std::string sName = cmSystemTools::LowerCase(name);
  353. // if the command already exists, give a new name to the old command.
  354. if (cmCommand* oldCmd = this->GetCommand(sName)) {
  355. std::string const newName = "_" + sName;
  356. std::map<std::string, cmCommand*>::iterator pos =
  357. this->ScriptedCommands.find(newName);
  358. if (pos != this->ScriptedCommands.end()) {
  359. delete pos->second;
  360. this->ScriptedCommands.erase(pos);
  361. }
  362. this->ScriptedCommands.insert(std::make_pair(newName, oldCmd->Clone()));
  363. }
  364. // if the command already exists, free the old one
  365. std::map<std::string, cmCommand*>::iterator pos =
  366. this->ScriptedCommands.find(sName);
  367. if (pos != this->ScriptedCommands.end()) {
  368. delete pos->second;
  369. this->ScriptedCommands.erase(pos);
  370. }
  371. this->ScriptedCommands.insert(std::make_pair(sName, command));
  372. }
  373. cmCommand* cmState::GetCommand(std::string const& name) const
  374. {
  375. std::string sName = cmSystemTools::LowerCase(name);
  376. std::map<std::string, cmCommand*>::const_iterator pos;
  377. pos = this->ScriptedCommands.find(sName);
  378. if (pos != this->ScriptedCommands.end()) {
  379. return pos->second;
  380. }
  381. pos = this->BuiltinCommands.find(sName);
  382. if (pos != this->BuiltinCommands.end()) {
  383. return pos->second;
  384. }
  385. return CM_NULLPTR;
  386. }
  387. std::vector<std::string> cmState::GetCommandNames() const
  388. {
  389. std::vector<std::string> commandNames;
  390. commandNames.reserve(this->BuiltinCommands.size() +
  391. this->ScriptedCommands.size());
  392. for (std::map<std::string, cmCommand*>::const_iterator cmds =
  393. this->BuiltinCommands.begin();
  394. cmds != this->BuiltinCommands.end(); ++cmds) {
  395. commandNames.push_back(cmds->first);
  396. }
  397. for (std::map<std::string, cmCommand*>::const_iterator cmds =
  398. this->ScriptedCommands.begin();
  399. cmds != this->ScriptedCommands.end(); ++cmds) {
  400. commandNames.push_back(cmds->first);
  401. }
  402. std::sort(commandNames.begin(), commandNames.end());
  403. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  404. commandNames.end());
  405. return commandNames;
  406. }
  407. void cmState::RemoveUserDefinedCommands()
  408. {
  409. cmDeleteAll(this->ScriptedCommands);
  410. this->ScriptedCommands.clear();
  411. }
  412. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  413. {
  414. this->GlobalProperties.SetProperty(prop, value);
  415. }
  416. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  417. bool asString)
  418. {
  419. this->GlobalProperties.AppendProperty(prop, value, asString);
  420. }
  421. const char* cmState::GetGlobalProperty(const std::string& prop)
  422. {
  423. if (prop == "CACHE_VARIABLES") {
  424. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  425. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  426. } else if (prop == "COMMANDS") {
  427. std::vector<std::string> commands = this->GetCommandNames();
  428. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  429. } else if (prop == "IN_TRY_COMPILE") {
  430. this->SetGlobalProperty("IN_TRY_COMPILE",
  431. this->IsInTryCompile ? "1" : "0");
  432. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  433. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  434. this->IsGeneratorMultiConfig ? "1" : "0");
  435. } else if (prop == "ENABLED_LANGUAGES") {
  436. std::string langs;
  437. langs = cmJoin(this->EnabledLanguages, ";");
  438. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  439. }
  440. #define STRING_LIST_ELEMENT(F) ";" #F
  441. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  442. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  443. }
  444. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  445. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  446. }
  447. #undef STRING_LIST_ELEMENT
  448. return this->GlobalProperties.GetPropertyValue(prop);
  449. }
  450. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  451. {
  452. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  453. }
  454. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  455. {
  456. this->SourceDirectory = sourceDirectory;
  457. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  458. }
  459. const char* cmState::GetSourceDirectory() const
  460. {
  461. return this->SourceDirectory.c_str();
  462. }
  463. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  464. {
  465. this->BinaryDirectory = binaryDirectory;
  466. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  467. }
  468. void cmState::SetWindowsShell(bool windowsShell)
  469. {
  470. this->WindowsShell = windowsShell;
  471. }
  472. bool cmState::UseWindowsShell() const
  473. {
  474. return this->WindowsShell;
  475. }
  476. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  477. {
  478. this->WindowsVSIDE = windowsVSIDE;
  479. }
  480. bool cmState::UseWindowsVSIDE() const
  481. {
  482. return this->WindowsVSIDE;
  483. }
  484. void cmState::SetWatcomWMake(bool watcomWMake)
  485. {
  486. this->WatcomWMake = watcomWMake;
  487. }
  488. bool cmState::UseWatcomWMake() const
  489. {
  490. return this->WatcomWMake;
  491. }
  492. void cmState::SetMinGWMake(bool minGWMake)
  493. {
  494. this->MinGWMake = minGWMake;
  495. }
  496. bool cmState::UseMinGWMake() const
  497. {
  498. return this->MinGWMake;
  499. }
  500. void cmState::SetNMake(bool nMake)
  501. {
  502. this->NMake = nMake;
  503. }
  504. bool cmState::UseNMake() const
  505. {
  506. return this->NMake;
  507. }
  508. void cmState::SetMSYSShell(bool mSYSShell)
  509. {
  510. this->MSYSShell = mSYSShell;
  511. }
  512. bool cmState::UseMSYSShell() const
  513. {
  514. return this->MSYSShell;
  515. }
  516. unsigned int cmState::GetCacheMajorVersion() const
  517. {
  518. return this->CacheManager->GetCacheMajorVersion();
  519. }
  520. unsigned int cmState::GetCacheMinorVersion() const
  521. {
  522. return this->CacheManager->GetCacheMinorVersion();
  523. }
  524. const char* cmState::GetBinaryDirectory() const
  525. {
  526. return this->BinaryDirectory.c_str();
  527. }
  528. cmStateSnapshot cmState::CreateBaseSnapshot()
  529. {
  530. cmStateDetail::PositionType pos =
  531. this->SnapshotData.Push(this->SnapshotData.Root());
  532. pos->DirectoryParent = this->SnapshotData.Root();
  533. pos->ScopeParent = this->SnapshotData.Root();
  534. pos->SnapshotType = cmStateEnums::BaseType;
  535. pos->Keep = true;
  536. pos->BuildSystemDirectory =
  537. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  538. pos->ExecutionListFile =
  539. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  540. pos->IncludeDirectoryPosition = 0;
  541. pos->CompileDefinitionsPosition = 0;
  542. pos->CompileOptionsPosition = 0;
  543. pos->BuildSystemDirectory->DirectoryEnd = pos;
  544. pos->Policies = this->PolicyStack.Root();
  545. pos->PolicyRoot = this->PolicyStack.Root();
  546. pos->PolicyScope = this->PolicyStack.Root();
  547. assert(pos->Policies.IsValid());
  548. assert(pos->PolicyRoot.IsValid());
  549. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  550. assert(pos->Vars.IsValid());
  551. pos->Parent = this->VarTree.Root();
  552. pos->Root = this->VarTree.Root();
  553. return cmStateSnapshot(this, pos);
  554. }
  555. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  556. cmStateSnapshot originSnapshot)
  557. {
  558. assert(originSnapshot.IsValid());
  559. cmStateDetail::PositionType pos =
  560. this->SnapshotData.Push(originSnapshot.Position);
  561. pos->DirectoryParent = originSnapshot.Position;
  562. pos->ScopeParent = originSnapshot.Position;
  563. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  564. pos->Keep = true;
  565. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  566. originSnapshot.Position->BuildSystemDirectory);
  567. pos->ExecutionListFile =
  568. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  569. pos->BuildSystemDirectory->DirectoryEnd = pos;
  570. pos->Policies = originSnapshot.Position->Policies;
  571. pos->PolicyRoot = originSnapshot.Position->Policies;
  572. pos->PolicyScope = originSnapshot.Position->Policies;
  573. assert(pos->Policies.IsValid());
  574. assert(pos->PolicyRoot.IsValid());
  575. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  576. pos->Parent = origin;
  577. pos->Root = origin;
  578. pos->Vars = this->VarTree.Push(origin);
  579. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  580. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  581. snapshot.SetDefaultDefinitions();
  582. snapshot.InitializeFromParent();
  583. snapshot.SetDirectoryDefinitions();
  584. return snapshot;
  585. }
  586. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  587. cmStateSnapshot originSnapshot, std::string const& fileName)
  588. {
  589. cmStateDetail::PositionType pos =
  590. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  591. pos->ScopeParent = originSnapshot.Position;
  592. pos->SnapshotType = cmStateEnums::FunctionCallType;
  593. pos->Keep = false;
  594. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  595. originSnapshot.Position->ExecutionListFile, fileName);
  596. pos->BuildSystemDirectory->DirectoryEnd = pos;
  597. pos->PolicyScope = originSnapshot.Position->Policies;
  598. assert(originSnapshot.Position->Vars.IsValid());
  599. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  600. pos->Parent = origin;
  601. pos->Vars = this->VarTree.Push(origin);
  602. return cmStateSnapshot(this, pos);
  603. }
  604. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  605. cmStateSnapshot originSnapshot, std::string const& fileName)
  606. {
  607. cmStateDetail::PositionType pos =
  608. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  609. pos->SnapshotType = cmStateEnums::MacroCallType;
  610. pos->Keep = false;
  611. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  612. originSnapshot.Position->ExecutionListFile, fileName);
  613. assert(originSnapshot.Position->Vars.IsValid());
  614. pos->BuildSystemDirectory->DirectoryEnd = pos;
  615. pos->PolicyScope = originSnapshot.Position->Policies;
  616. return cmStateSnapshot(this, pos);
  617. }
  618. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  619. cmStateSnapshot originSnapshot, const std::string& fileName)
  620. {
  621. cmStateDetail::PositionType pos =
  622. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  623. pos->SnapshotType = cmStateEnums::IncludeFileType;
  624. pos->Keep = true;
  625. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  626. originSnapshot.Position->ExecutionListFile, fileName);
  627. assert(originSnapshot.Position->Vars.IsValid());
  628. pos->BuildSystemDirectory->DirectoryEnd = pos;
  629. pos->PolicyScope = originSnapshot.Position->Policies;
  630. return cmStateSnapshot(this, pos);
  631. }
  632. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  633. cmStateSnapshot originSnapshot)
  634. {
  635. cmStateDetail::PositionType pos =
  636. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  637. pos->ScopeParent = originSnapshot.Position;
  638. pos->SnapshotType = cmStateEnums::VariableScopeType;
  639. pos->Keep = false;
  640. pos->PolicyScope = originSnapshot.Position->Policies;
  641. assert(originSnapshot.Position->Vars.IsValid());
  642. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  643. pos->Parent = origin;
  644. pos->Vars = this->VarTree.Push(origin);
  645. assert(pos->Vars.IsValid());
  646. return cmStateSnapshot(this, pos);
  647. }
  648. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  649. cmStateSnapshot originSnapshot, const std::string& fileName)
  650. {
  651. cmStateDetail::PositionType pos =
  652. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  653. pos->SnapshotType = cmStateEnums::InlineListFileType;
  654. pos->Keep = true;
  655. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  656. originSnapshot.Position->ExecutionListFile, fileName);
  657. pos->BuildSystemDirectory->DirectoryEnd = pos;
  658. pos->PolicyScope = originSnapshot.Position->Policies;
  659. return cmStateSnapshot(this, pos);
  660. }
  661. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  662. cmStateSnapshot originSnapshot)
  663. {
  664. cmStateDetail::PositionType pos =
  665. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  666. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  667. pos->Keep = false;
  668. pos->BuildSystemDirectory->DirectoryEnd = pos;
  669. pos->PolicyScope = originSnapshot.Position->Policies;
  670. return cmStateSnapshot(this, pos);
  671. }
  672. cmStateSnapshot cmState::Pop(cmStateSnapshot originSnapshot)
  673. {
  674. cmStateDetail::PositionType pos = originSnapshot.Position;
  675. cmStateDetail::PositionType prevPos = pos;
  676. ++prevPos;
  677. prevPos->IncludeDirectoryPosition =
  678. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  679. prevPos->CompileDefinitionsPosition =
  680. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  681. prevPos->CompileOptionsPosition =
  682. prevPos->BuildSystemDirectory->CompileOptions.size();
  683. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  684. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  685. if (pos->Vars != prevPos->Vars) {
  686. assert(this->VarTree.IsLast(pos->Vars));
  687. this->VarTree.Pop(pos->Vars);
  688. }
  689. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  690. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  691. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  692. }
  693. this->SnapshotData.Pop(pos);
  694. }
  695. return cmStateSnapshot(this, prevPos);
  696. }
  697. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  698. std::string& value)
  699. {
  700. // input line is: key=value
  701. static cmsys::RegularExpression reg(
  702. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  703. // input line is: "key"=value
  704. static cmsys::RegularExpression regQuoted(
  705. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  706. bool flag = false;
  707. if (regQuoted.find(entry)) {
  708. var = regQuoted.match(1);
  709. value = regQuoted.match(2);
  710. flag = true;
  711. } else if (reg.find(entry)) {
  712. var = reg.match(1);
  713. value = reg.match(2);
  714. flag = true;
  715. }
  716. // if value is enclosed in single quotes ('foo') then remove them
  717. // it is used to enclose trailing space or tab
  718. if (flag && value.size() >= 2 && value[0] == '\'' &&
  719. value[value.size() - 1] == '\'') {
  720. value = value.substr(1, value.size() - 2);
  721. }
  722. return flag;
  723. }
  724. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  725. std::string& value,
  726. cmStateEnums::CacheEntryType& type)
  727. {
  728. // input line is: key:type=value
  729. static cmsys::RegularExpression reg(
  730. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  731. // input line is: "key":type=value
  732. static cmsys::RegularExpression regQuoted(
  733. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  734. bool flag = false;
  735. if (regQuoted.find(entry)) {
  736. var = regQuoted.match(1);
  737. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  738. value = regQuoted.match(3);
  739. flag = true;
  740. } else if (reg.find(entry)) {
  741. var = reg.match(1);
  742. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  743. value = reg.match(3);
  744. flag = true;
  745. }
  746. // if value is enclosed in single quotes ('foo') then remove them
  747. // it is used to enclose trailing space or tab
  748. if (flag && value.size() >= 2 && value[0] == '\'' &&
  749. value[value.size() - 1] == '\'') {
  750. value = value.substr(1, value.size() - 2);
  751. }
  752. if (!flag) {
  753. return ParseEntryWithoutType(entry, var, value);
  754. }
  755. return flag;
  756. }