cmState.cxx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  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 (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 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 (std::map<std::string, cmCommand*>::const_iterator cmds =
  392. this->BuiltinCommands.begin();
  393. cmds != this->BuiltinCommands.end(); ++cmds) {
  394. commandNames.push_back(cmds->first);
  395. }
  396. for (std::map<std::string, cmCommand*>::const_iterator cmds =
  397. this->ScriptedCommands.begin();
  398. cmds != this->ScriptedCommands.end(); ++cmds) {
  399. commandNames.push_back(cmds->first);
  400. }
  401. std::sort(commandNames.begin(), commandNames.end());
  402. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  403. commandNames.end());
  404. return commandNames;
  405. }
  406. void cmState::RemoveUserDefinedCommands()
  407. {
  408. cmDeleteAll(this->ScriptedCommands);
  409. this->ScriptedCommands.clear();
  410. }
  411. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  412. {
  413. this->GlobalProperties.SetProperty(prop, value);
  414. }
  415. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  416. bool asString)
  417. {
  418. this->GlobalProperties.AppendProperty(prop, value, asString);
  419. }
  420. const char* cmState::GetGlobalProperty(const std::string& prop)
  421. {
  422. if (prop == "CACHE_VARIABLES") {
  423. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  424. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  425. } else if (prop == "COMMANDS") {
  426. std::vector<std::string> commands = this->GetCommandNames();
  427. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  428. } else if (prop == "IN_TRY_COMPILE") {
  429. this->SetGlobalProperty("IN_TRY_COMPILE",
  430. this->IsInTryCompile ? "1" : "0");
  431. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  432. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  433. this->IsGeneratorMultiConfig ? "1" : "0");
  434. } else if (prop == "ENABLED_LANGUAGES") {
  435. std::string langs;
  436. langs = cmJoin(this->EnabledLanguages, ";");
  437. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  438. }
  439. #define STRING_LIST_ELEMENT(F) ";" #F
  440. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  441. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  442. }
  443. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  444. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  445. }
  446. #undef STRING_LIST_ELEMENT
  447. return this->GlobalProperties.GetPropertyValue(prop);
  448. }
  449. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  450. {
  451. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  452. }
  453. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  454. {
  455. this->SourceDirectory = sourceDirectory;
  456. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  457. }
  458. const char* cmState::GetSourceDirectory() const
  459. {
  460. return this->SourceDirectory.c_str();
  461. }
  462. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  463. {
  464. this->BinaryDirectory = binaryDirectory;
  465. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  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. unsigned int cmState::GetCacheMajorVersion() const
  516. {
  517. return this->CacheManager->GetCacheMajorVersion();
  518. }
  519. unsigned int cmState::GetCacheMinorVersion() const
  520. {
  521. return this->CacheManager->GetCacheMinorVersion();
  522. }
  523. const char* cmState::GetBinaryDirectory() const
  524. {
  525. return this->BinaryDirectory.c_str();
  526. }
  527. cmStateSnapshot cmState::CreateBaseSnapshot()
  528. {
  529. cmStateDetail::PositionType pos =
  530. this->SnapshotData.Push(this->SnapshotData.Root());
  531. pos->DirectoryParent = this->SnapshotData.Root();
  532. pos->ScopeParent = this->SnapshotData.Root();
  533. pos->SnapshotType = cmStateEnums::BaseType;
  534. pos->Keep = true;
  535. pos->BuildSystemDirectory =
  536. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  537. pos->ExecutionListFile =
  538. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  539. pos->IncludeDirectoryPosition = 0;
  540. pos->CompileDefinitionsPosition = 0;
  541. pos->CompileOptionsPosition = 0;
  542. pos->BuildSystemDirectory->DirectoryEnd = pos;
  543. pos->Policies = this->PolicyStack.Root();
  544. pos->PolicyRoot = this->PolicyStack.Root();
  545. pos->PolicyScope = this->PolicyStack.Root();
  546. assert(pos->Policies.IsValid());
  547. assert(pos->PolicyRoot.IsValid());
  548. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  549. assert(pos->Vars.IsValid());
  550. pos->Parent = this->VarTree.Root();
  551. pos->Root = this->VarTree.Root();
  552. return cmStateSnapshot(this, pos);
  553. }
  554. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  555. cmStateSnapshot const& originSnapshot)
  556. {
  557. assert(originSnapshot.IsValid());
  558. cmStateDetail::PositionType pos =
  559. this->SnapshotData.Push(originSnapshot.Position);
  560. pos->DirectoryParent = originSnapshot.Position;
  561. pos->ScopeParent = originSnapshot.Position;
  562. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  563. pos->Keep = true;
  564. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  565. originSnapshot.Position->BuildSystemDirectory);
  566. pos->ExecutionListFile =
  567. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  568. pos->BuildSystemDirectory->DirectoryEnd = pos;
  569. pos->Policies = originSnapshot.Position->Policies;
  570. pos->PolicyRoot = originSnapshot.Position->Policies;
  571. pos->PolicyScope = originSnapshot.Position->Policies;
  572. assert(pos->Policies.IsValid());
  573. assert(pos->PolicyRoot.IsValid());
  574. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  575. pos->Parent = origin;
  576. pos->Root = origin;
  577. pos->Vars = this->VarTree.Push(origin);
  578. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  579. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  580. snapshot.SetDefaultDefinitions();
  581. snapshot.InitializeFromParent();
  582. snapshot.SetDirectoryDefinitions();
  583. return snapshot;
  584. }
  585. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  586. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  587. {
  588. cmStateDetail::PositionType pos =
  589. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  590. pos->ScopeParent = originSnapshot.Position;
  591. pos->SnapshotType = cmStateEnums::FunctionCallType;
  592. pos->Keep = false;
  593. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  594. originSnapshot.Position->ExecutionListFile, fileName);
  595. pos->BuildSystemDirectory->DirectoryEnd = pos;
  596. pos->PolicyScope = originSnapshot.Position->Policies;
  597. assert(originSnapshot.Position->Vars.IsValid());
  598. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  599. pos->Parent = origin;
  600. pos->Vars = this->VarTree.Push(origin);
  601. return cmStateSnapshot(this, pos);
  602. }
  603. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  604. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  605. {
  606. cmStateDetail::PositionType pos =
  607. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  608. pos->SnapshotType = cmStateEnums::MacroCallType;
  609. pos->Keep = false;
  610. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  611. originSnapshot.Position->ExecutionListFile, fileName);
  612. assert(originSnapshot.Position->Vars.IsValid());
  613. pos->BuildSystemDirectory->DirectoryEnd = pos;
  614. pos->PolicyScope = originSnapshot.Position->Policies;
  615. return cmStateSnapshot(this, pos);
  616. }
  617. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  618. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  619. {
  620. cmStateDetail::PositionType pos =
  621. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  622. pos->SnapshotType = cmStateEnums::IncludeFileType;
  623. pos->Keep = true;
  624. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  625. originSnapshot.Position->ExecutionListFile, fileName);
  626. assert(originSnapshot.Position->Vars.IsValid());
  627. pos->BuildSystemDirectory->DirectoryEnd = pos;
  628. pos->PolicyScope = originSnapshot.Position->Policies;
  629. return cmStateSnapshot(this, pos);
  630. }
  631. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  632. cmStateSnapshot const& originSnapshot)
  633. {
  634. cmStateDetail::PositionType pos =
  635. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  636. pos->ScopeParent = originSnapshot.Position;
  637. pos->SnapshotType = cmStateEnums::VariableScopeType;
  638. pos->Keep = false;
  639. pos->PolicyScope = originSnapshot.Position->Policies;
  640. assert(originSnapshot.Position->Vars.IsValid());
  641. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  642. pos->Parent = origin;
  643. pos->Vars = this->VarTree.Push(origin);
  644. assert(pos->Vars.IsValid());
  645. return cmStateSnapshot(this, pos);
  646. }
  647. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  648. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  649. {
  650. cmStateDetail::PositionType pos =
  651. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  652. pos->SnapshotType = cmStateEnums::InlineListFileType;
  653. pos->Keep = true;
  654. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  655. originSnapshot.Position->ExecutionListFile, fileName);
  656. pos->BuildSystemDirectory->DirectoryEnd = pos;
  657. pos->PolicyScope = originSnapshot.Position->Policies;
  658. return cmStateSnapshot(this, pos);
  659. }
  660. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  661. cmStateSnapshot const& originSnapshot)
  662. {
  663. cmStateDetail::PositionType pos =
  664. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  665. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  666. pos->Keep = false;
  667. pos->BuildSystemDirectory->DirectoryEnd = pos;
  668. pos->PolicyScope = originSnapshot.Position->Policies;
  669. return cmStateSnapshot(this, pos);
  670. }
  671. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  672. {
  673. cmStateDetail::PositionType pos = originSnapshot.Position;
  674. cmStateDetail::PositionType prevPos = pos;
  675. ++prevPos;
  676. prevPos->IncludeDirectoryPosition =
  677. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  678. prevPos->CompileDefinitionsPosition =
  679. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  680. prevPos->CompileOptionsPosition =
  681. prevPos->BuildSystemDirectory->CompileOptions.size();
  682. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  683. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  684. if (pos->Vars != prevPos->Vars) {
  685. assert(this->VarTree.IsLast(pos->Vars));
  686. this->VarTree.Pop(pos->Vars);
  687. }
  688. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  689. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  690. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  691. }
  692. this->SnapshotData.Pop(pos);
  693. }
  694. return cmStateSnapshot(this, prevPos);
  695. }
  696. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  697. std::string& value)
  698. {
  699. // input line is: key=value
  700. static cmsys::RegularExpression reg(
  701. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  702. // input line is: "key"=value
  703. static cmsys::RegularExpression regQuoted(
  704. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  705. bool flag = false;
  706. if (regQuoted.find(entry)) {
  707. var = regQuoted.match(1);
  708. value = regQuoted.match(2);
  709. flag = true;
  710. } else if (reg.find(entry)) {
  711. var = reg.match(1);
  712. value = reg.match(2);
  713. flag = true;
  714. }
  715. // if value is enclosed in single quotes ('foo') then remove them
  716. // it is used to enclose trailing space or tab
  717. if (flag && value.size() >= 2 && value[0] == '\'' &&
  718. value[value.size() - 1] == '\'') {
  719. value = value.substr(1, value.size() - 2);
  720. }
  721. return flag;
  722. }
  723. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  724. std::string& value,
  725. cmStateEnums::CacheEntryType& type)
  726. {
  727. // input line is: key:type=value
  728. static cmsys::RegularExpression reg(
  729. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  730. // input line is: "key":type=value
  731. static cmsys::RegularExpression regQuoted(
  732. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  733. bool flag = false;
  734. if (regQuoted.find(entry)) {
  735. var = regQuoted.match(1);
  736. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  737. value = regQuoted.match(3);
  738. flag = true;
  739. } else if (reg.find(entry)) {
  740. var = reg.match(1);
  741. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  742. value = reg.match(3);
  743. flag = true;
  744. }
  745. // if value is enclosed in single quotes ('foo') then remove them
  746. // it is used to enclose trailing space or tab
  747. if (flag && value.size() >= 2 && value[0] == '\'' &&
  748. value[value.size() - 1] == '\'') {
  749. value = value.substr(1, value.size() - 2);
  750. }
  751. if (!flag) {
  752. return ParseEntryWithoutType(entry, var, value);
  753. }
  754. return flag;
  755. }