cmState.cxx 27 KB

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