cmState.cxx 27 KB

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