cmState.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  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. this->GlobVerificationManager->Reset();
  239. cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
  240. this->ExecutionListFiles.Truncate();
  241. {
  242. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator it =
  243. this->BuildsystemDirectory.Truncate();
  244. it->IncludeDirectories.clear();
  245. it->IncludeDirectoryBacktraces.clear();
  246. it->CompileDefinitions.clear();
  247. it->CompileDefinitionsBacktraces.clear();
  248. it->CompileOptions.clear();
  249. it->CompileOptionsBacktraces.clear();
  250. it->LinkOptions.clear();
  251. it->LinkOptionsBacktraces.clear();
  252. it->DirectoryEnd = pos;
  253. it->NormalTargetNames.clear();
  254. it->Properties.clear();
  255. it->Children.clear();
  256. }
  257. this->PolicyStack.Clear();
  258. pos->Policies = this->PolicyStack.Root();
  259. pos->PolicyRoot = this->PolicyStack.Root();
  260. pos->PolicyScope = this->PolicyStack.Root();
  261. assert(pos->Policies.IsValid());
  262. assert(pos->PolicyRoot.IsValid());
  263. {
  264. std::string srcDir =
  265. *cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root);
  266. std::string binDir =
  267. *cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root);
  268. this->VarTree.Clear();
  269. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  270. pos->Parent = this->VarTree.Root();
  271. pos->Root = this->VarTree.Root();
  272. pos->Vars->Set("CMAKE_SOURCE_DIR", srcDir.c_str());
  273. pos->Vars->Set("CMAKE_BINARY_DIR", binDir.c_str());
  274. }
  275. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY, "", "",
  276. true);
  277. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::DIRECTORY, "", "",
  278. true);
  279. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY, "", "",
  280. true);
  281. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::TARGET, "", "",
  282. true);
  283. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::TARGET, "", "", true);
  284. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::TARGET, "", "", true);
  285. return cmStateSnapshot(this, pos);
  286. }
  287. void cmState::DefineProperty(const std::string& name,
  288. cmProperty::ScopeType scope,
  289. const char* ShortDescription,
  290. const char* FullDescription, bool chained)
  291. {
  292. this->PropertyDefinitions[scope].DefineProperty(
  293. name, scope, ShortDescription, FullDescription, chained);
  294. }
  295. cmPropertyDefinition const* cmState::GetPropertyDefinition(
  296. const std::string& name, cmProperty::ScopeType scope) const
  297. {
  298. if (this->IsPropertyDefined(name, scope)) {
  299. cmPropertyDefinitionMap const& defs =
  300. this->PropertyDefinitions.find(scope)->second;
  301. return &defs.find(name)->second;
  302. }
  303. return nullptr;
  304. }
  305. bool cmState::IsPropertyDefined(const std::string& name,
  306. cmProperty::ScopeType scope) const
  307. {
  308. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it =
  309. this->PropertyDefinitions.find(scope);
  310. if (it == this->PropertyDefinitions.end()) {
  311. return false;
  312. }
  313. return it->second.IsPropertyDefined(name);
  314. }
  315. bool cmState::IsPropertyChained(const std::string& name,
  316. cmProperty::ScopeType scope) const
  317. {
  318. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it =
  319. this->PropertyDefinitions.find(scope);
  320. if (it == this->PropertyDefinitions.end()) {
  321. return false;
  322. }
  323. return it->second.IsPropertyChained(name);
  324. }
  325. void cmState::SetLanguageEnabled(std::string const& l)
  326. {
  327. std::vector<std::string>::iterator it = std::lower_bound(
  328. this->EnabledLanguages.begin(), this->EnabledLanguages.end(), l);
  329. if (it == this->EnabledLanguages.end() || *it != l) {
  330. this->EnabledLanguages.insert(it, l);
  331. }
  332. }
  333. bool cmState::GetLanguageEnabled(std::string const& l) const
  334. {
  335. return std::binary_search(this->EnabledLanguages.begin(),
  336. this->EnabledLanguages.end(), l);
  337. }
  338. std::vector<std::string> cmState::GetEnabledLanguages() const
  339. {
  340. return this->EnabledLanguages;
  341. }
  342. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  343. {
  344. this->EnabledLanguages = langs;
  345. }
  346. void cmState::ClearEnabledLanguages()
  347. {
  348. this->EnabledLanguages.clear();
  349. }
  350. bool cmState::GetIsInTryCompile() const
  351. {
  352. return this->IsInTryCompile;
  353. }
  354. void cmState::SetIsInTryCompile(bool b)
  355. {
  356. this->IsInTryCompile = b;
  357. }
  358. bool cmState::GetIsGeneratorMultiConfig() const
  359. {
  360. return this->IsGeneratorMultiConfig;
  361. }
  362. void cmState::SetIsGeneratorMultiConfig(bool b)
  363. {
  364. this->IsGeneratorMultiConfig = b;
  365. }
  366. void cmState::AddBuiltinCommand(std::string const& name, cmCommand* command)
  367. {
  368. assert(name == cmSystemTools::LowerCase(name));
  369. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  370. this->BuiltinCommands.insert(std::make_pair(name, command));
  371. }
  372. void cmState::AddDisallowedCommand(std::string const& name, cmCommand* command,
  373. cmPolicies::PolicyID policy,
  374. const char* message)
  375. {
  376. this->AddBuiltinCommand(name,
  377. new cmDisallowedCommand(command, policy, message));
  378. }
  379. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  380. {
  381. this->AddBuiltinCommand(name, new cmUnexpectedCommand(name, error));
  382. }
  383. void cmState::AddScriptedCommand(std::string const& name, cmCommand* command)
  384. {
  385. std::string sName = cmSystemTools::LowerCase(name);
  386. // if the command already exists, give a new name to the old command.
  387. if (cmCommand* oldCmd = this->GetCommand(sName)) {
  388. std::string const newName = "_" + sName;
  389. std::map<std::string, cmCommand*>::iterator pos =
  390. this->ScriptedCommands.find(newName);
  391. if (pos != this->ScriptedCommands.end()) {
  392. delete pos->second;
  393. this->ScriptedCommands.erase(pos);
  394. }
  395. this->ScriptedCommands.insert(std::make_pair(newName, oldCmd->Clone()));
  396. }
  397. // if the command already exists, free the old one
  398. std::map<std::string, cmCommand*>::iterator pos =
  399. this->ScriptedCommands.find(sName);
  400. if (pos != this->ScriptedCommands.end()) {
  401. delete pos->second;
  402. this->ScriptedCommands.erase(pos);
  403. }
  404. this->ScriptedCommands.insert(std::make_pair(sName, command));
  405. }
  406. cmCommand* cmState::GetCommand(std::string const& name) const
  407. {
  408. return GetCommandByExactName(cmSystemTools::LowerCase(name));
  409. }
  410. cmCommand* cmState::GetCommandByExactName(std::string const& name) const
  411. {
  412. std::map<std::string, cmCommand*>::const_iterator pos;
  413. pos = this->ScriptedCommands.find(name);
  414. if (pos != this->ScriptedCommands.end()) {
  415. return pos->second;
  416. }
  417. pos = this->BuiltinCommands.find(name);
  418. if (pos != this->BuiltinCommands.end()) {
  419. return pos->second;
  420. }
  421. return nullptr;
  422. }
  423. std::vector<std::string> cmState::GetCommandNames() const
  424. {
  425. std::vector<std::string> commandNames;
  426. commandNames.reserve(this->BuiltinCommands.size() +
  427. this->ScriptedCommands.size());
  428. for (auto const& bc : this->BuiltinCommands) {
  429. commandNames.push_back(bc.first);
  430. }
  431. for (auto const& sc : this->ScriptedCommands) {
  432. commandNames.push_back(sc.first);
  433. }
  434. std::sort(commandNames.begin(), commandNames.end());
  435. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  436. commandNames.end());
  437. return commandNames;
  438. }
  439. void cmState::RemoveUserDefinedCommands()
  440. {
  441. cmDeleteAll(this->ScriptedCommands);
  442. this->ScriptedCommands.clear();
  443. }
  444. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  445. {
  446. this->GlobalProperties.SetProperty(prop, value);
  447. }
  448. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  449. bool asString)
  450. {
  451. this->GlobalProperties.AppendProperty(prop, value, asString);
  452. }
  453. const char* cmState::GetGlobalProperty(const std::string& prop)
  454. {
  455. if (prop == "CACHE_VARIABLES") {
  456. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  457. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  458. } else if (prop == "COMMANDS") {
  459. std::vector<std::string> commands = this->GetCommandNames();
  460. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  461. } else if (prop == "IN_TRY_COMPILE") {
  462. this->SetGlobalProperty("IN_TRY_COMPILE",
  463. this->IsInTryCompile ? "1" : "0");
  464. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  465. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  466. this->IsGeneratorMultiConfig ? "1" : "0");
  467. } else if (prop == "ENABLED_LANGUAGES") {
  468. std::string langs;
  469. langs = cmJoin(this->EnabledLanguages, ";");
  470. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  471. }
  472. #define STRING_LIST_ELEMENT(F) ";" #F
  473. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  474. return FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT) + 1;
  475. }
  476. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  477. return FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT) + 1;
  478. }
  479. #undef STRING_LIST_ELEMENT
  480. return this->GlobalProperties.GetPropertyValue(prop);
  481. }
  482. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  483. {
  484. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  485. }
  486. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  487. {
  488. this->SourceDirectory = sourceDirectory;
  489. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  490. }
  491. std::string const& cmState::GetSourceDirectory() const
  492. {
  493. return this->SourceDirectory;
  494. }
  495. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  496. {
  497. this->BinaryDirectory = binaryDirectory;
  498. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  499. }
  500. void cmState::SetWindowsShell(bool windowsShell)
  501. {
  502. this->WindowsShell = windowsShell;
  503. }
  504. bool cmState::UseWindowsShell() const
  505. {
  506. return this->WindowsShell;
  507. }
  508. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  509. {
  510. this->WindowsVSIDE = windowsVSIDE;
  511. }
  512. bool cmState::UseWindowsVSIDE() const
  513. {
  514. return this->WindowsVSIDE;
  515. }
  516. void cmState::SetWatcomWMake(bool watcomWMake)
  517. {
  518. this->WatcomWMake = watcomWMake;
  519. }
  520. bool cmState::UseWatcomWMake() const
  521. {
  522. return this->WatcomWMake;
  523. }
  524. void cmState::SetMinGWMake(bool minGWMake)
  525. {
  526. this->MinGWMake = minGWMake;
  527. }
  528. bool cmState::UseMinGWMake() const
  529. {
  530. return this->MinGWMake;
  531. }
  532. void cmState::SetNMake(bool nMake)
  533. {
  534. this->NMake = nMake;
  535. }
  536. bool cmState::UseNMake() const
  537. {
  538. return this->NMake;
  539. }
  540. void cmState::SetMSYSShell(bool mSYSShell)
  541. {
  542. this->MSYSShell = mSYSShell;
  543. }
  544. bool cmState::UseMSYSShell() const
  545. {
  546. return this->MSYSShell;
  547. }
  548. unsigned int cmState::GetCacheMajorVersion() const
  549. {
  550. return this->CacheManager->GetCacheMajorVersion();
  551. }
  552. unsigned int cmState::GetCacheMinorVersion() const
  553. {
  554. return this->CacheManager->GetCacheMinorVersion();
  555. }
  556. std::string const& cmState::GetBinaryDirectory() const
  557. {
  558. return this->BinaryDirectory;
  559. }
  560. cmStateSnapshot cmState::CreateBaseSnapshot()
  561. {
  562. cmStateDetail::PositionType pos =
  563. this->SnapshotData.Push(this->SnapshotData.Root());
  564. pos->DirectoryParent = this->SnapshotData.Root();
  565. pos->ScopeParent = this->SnapshotData.Root();
  566. pos->SnapshotType = cmStateEnums::BaseType;
  567. pos->Keep = true;
  568. pos->BuildSystemDirectory =
  569. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  570. pos->ExecutionListFile =
  571. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  572. pos->IncludeDirectoryPosition = 0;
  573. pos->CompileDefinitionsPosition = 0;
  574. pos->CompileOptionsPosition = 0;
  575. pos->BuildSystemDirectory->DirectoryEnd = pos;
  576. pos->Policies = this->PolicyStack.Root();
  577. pos->PolicyRoot = this->PolicyStack.Root();
  578. pos->PolicyScope = this->PolicyStack.Root();
  579. assert(pos->Policies.IsValid());
  580. assert(pos->PolicyRoot.IsValid());
  581. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  582. assert(pos->Vars.IsValid());
  583. pos->Parent = this->VarTree.Root();
  584. pos->Root = this->VarTree.Root();
  585. return cmStateSnapshot(this, pos);
  586. }
  587. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  588. cmStateSnapshot const& originSnapshot)
  589. {
  590. assert(originSnapshot.IsValid());
  591. cmStateDetail::PositionType pos =
  592. this->SnapshotData.Push(originSnapshot.Position);
  593. pos->DirectoryParent = originSnapshot.Position;
  594. pos->ScopeParent = originSnapshot.Position;
  595. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  596. pos->Keep = true;
  597. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  598. originSnapshot.Position->BuildSystemDirectory);
  599. pos->ExecutionListFile =
  600. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  601. pos->BuildSystemDirectory->DirectoryEnd = pos;
  602. pos->Policies = originSnapshot.Position->Policies;
  603. pos->PolicyRoot = originSnapshot.Position->Policies;
  604. pos->PolicyScope = originSnapshot.Position->Policies;
  605. assert(pos->Policies.IsValid());
  606. assert(pos->PolicyRoot.IsValid());
  607. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  608. pos->Parent = origin;
  609. pos->Root = origin;
  610. pos->Vars = this->VarTree.Push(origin);
  611. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  612. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  613. snapshot.SetDefaultDefinitions();
  614. snapshot.InitializeFromParent();
  615. snapshot.SetDirectoryDefinitions();
  616. return snapshot;
  617. }
  618. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  619. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  620. {
  621. cmStateDetail::PositionType pos =
  622. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  623. pos->ScopeParent = originSnapshot.Position;
  624. pos->SnapshotType = cmStateEnums::FunctionCallType;
  625. pos->Keep = false;
  626. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  627. originSnapshot.Position->ExecutionListFile, fileName);
  628. pos->BuildSystemDirectory->DirectoryEnd = pos;
  629. pos->PolicyScope = originSnapshot.Position->Policies;
  630. assert(originSnapshot.Position->Vars.IsValid());
  631. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  632. pos->Parent = origin;
  633. pos->Vars = this->VarTree.Push(origin);
  634. return cmStateSnapshot(this, pos);
  635. }
  636. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  637. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  638. {
  639. cmStateDetail::PositionType pos =
  640. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  641. pos->SnapshotType = cmStateEnums::MacroCallType;
  642. pos->Keep = false;
  643. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  644. originSnapshot.Position->ExecutionListFile, fileName);
  645. assert(originSnapshot.Position->Vars.IsValid());
  646. pos->BuildSystemDirectory->DirectoryEnd = pos;
  647. pos->PolicyScope = originSnapshot.Position->Policies;
  648. return cmStateSnapshot(this, pos);
  649. }
  650. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  651. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  652. {
  653. cmStateDetail::PositionType pos =
  654. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  655. pos->SnapshotType = cmStateEnums::IncludeFileType;
  656. pos->Keep = true;
  657. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  658. originSnapshot.Position->ExecutionListFile, fileName);
  659. assert(originSnapshot.Position->Vars.IsValid());
  660. pos->BuildSystemDirectory->DirectoryEnd = pos;
  661. pos->PolicyScope = originSnapshot.Position->Policies;
  662. return cmStateSnapshot(this, pos);
  663. }
  664. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  665. cmStateSnapshot const& originSnapshot)
  666. {
  667. cmStateDetail::PositionType pos =
  668. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  669. pos->ScopeParent = originSnapshot.Position;
  670. pos->SnapshotType = cmStateEnums::VariableScopeType;
  671. pos->Keep = false;
  672. pos->PolicyScope = originSnapshot.Position->Policies;
  673. assert(originSnapshot.Position->Vars.IsValid());
  674. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  675. pos->Parent = origin;
  676. pos->Vars = this->VarTree.Push(origin);
  677. assert(pos->Vars.IsValid());
  678. return cmStateSnapshot(this, pos);
  679. }
  680. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  681. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  682. {
  683. cmStateDetail::PositionType pos =
  684. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  685. pos->SnapshotType = cmStateEnums::InlineListFileType;
  686. pos->Keep = true;
  687. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  688. originSnapshot.Position->ExecutionListFile, fileName);
  689. pos->BuildSystemDirectory->DirectoryEnd = pos;
  690. pos->PolicyScope = originSnapshot.Position->Policies;
  691. return cmStateSnapshot(this, pos);
  692. }
  693. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  694. cmStateSnapshot const& originSnapshot)
  695. {
  696. cmStateDetail::PositionType pos =
  697. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  698. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  699. pos->Keep = false;
  700. pos->BuildSystemDirectory->DirectoryEnd = pos;
  701. pos->PolicyScope = originSnapshot.Position->Policies;
  702. return cmStateSnapshot(this, pos);
  703. }
  704. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  705. {
  706. cmStateDetail::PositionType pos = originSnapshot.Position;
  707. cmStateDetail::PositionType prevPos = pos;
  708. ++prevPos;
  709. prevPos->IncludeDirectoryPosition =
  710. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  711. prevPos->CompileDefinitionsPosition =
  712. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  713. prevPos->CompileOptionsPosition =
  714. prevPos->BuildSystemDirectory->CompileOptions.size();
  715. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  716. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  717. if (pos->Vars != prevPos->Vars) {
  718. assert(this->VarTree.IsLast(pos->Vars));
  719. this->VarTree.Pop(pos->Vars);
  720. }
  721. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  722. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  723. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  724. }
  725. this->SnapshotData.Pop(pos);
  726. }
  727. return cmStateSnapshot(this, prevPos);
  728. }
  729. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  730. std::string& value)
  731. {
  732. // input line is: key=value
  733. static cmsys::RegularExpression reg(
  734. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  735. // input line is: "key"=value
  736. static cmsys::RegularExpression regQuoted(
  737. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  738. bool flag = false;
  739. if (regQuoted.find(entry)) {
  740. var = regQuoted.match(1);
  741. value = regQuoted.match(2);
  742. flag = true;
  743. } else if (reg.find(entry)) {
  744. var = reg.match(1);
  745. value = reg.match(2);
  746. flag = true;
  747. }
  748. // if value is enclosed in single quotes ('foo') then remove them
  749. // it is used to enclose trailing space or tab
  750. if (flag && value.size() >= 2 && value[0] == '\'' &&
  751. value[value.size() - 1] == '\'') {
  752. value = value.substr(1, value.size() - 2);
  753. }
  754. return flag;
  755. }
  756. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  757. std::string& value,
  758. cmStateEnums::CacheEntryType& type)
  759. {
  760. // input line is: key:type=value
  761. static cmsys::RegularExpression reg(
  762. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  763. // input line is: "key":type=value
  764. static cmsys::RegularExpression regQuoted(
  765. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  766. bool flag = false;
  767. if (regQuoted.find(entry)) {
  768. var = regQuoted.match(1);
  769. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  770. value = regQuoted.match(3);
  771. flag = true;
  772. } else if (reg.find(entry)) {
  773. var = reg.match(1);
  774. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  775. value = reg.match(3);
  776. flag = true;
  777. }
  778. // if value is enclosed in single quotes ('foo') then remove them
  779. // it is used to enclose trailing space or tab
  780. if (flag && value.size() >= 2 && value[0] == '\'' &&
  781. value[value.size() - 1] == '\'') {
  782. value = value.substr(1, value.size() - 2);
  783. }
  784. if (!flag) {
  785. return ParseEntryWithoutType(entry, var, value);
  786. }
  787. return flag;
  788. }