cmState.cxx 29 KB

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