cmState.cxx 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 <stdlib.h>
  8. #include <string.h>
  9. #include <utility>
  10. #include "cm_memory.hxx"
  11. #include "cmCacheManager.h"
  12. #include "cmCommand.h"
  13. #include "cmDefinitions.h"
  14. #include "cmExecutionStatus.h"
  15. #include "cmGlobVerificationManager.h"
  16. #include "cmListFileCache.h"
  17. #include "cmMakefile.h"
  18. #include "cmMessageType.h"
  19. #include "cmStatePrivate.h"
  20. #include "cmStateSnapshot.h"
  21. #include "cmStringAlgorithms.h"
  22. #include "cmSystemTools.h"
  23. #include "cmake.h"
  24. cmState::cmState()
  25. {
  26. this->CacheManager = cm::make_unique<cmCacheManager>();
  27. this->GlobVerificationManager = cm::make_unique<cmGlobVerificationManager>();
  28. }
  29. cmState::~cmState() = default;
  30. const char* cmState::GetTargetTypeName(cmStateEnums::TargetType targetType)
  31. {
  32. switch (targetType) {
  33. case cmStateEnums::STATIC_LIBRARY:
  34. return "STATIC_LIBRARY";
  35. case cmStateEnums::MODULE_LIBRARY:
  36. return "MODULE_LIBRARY";
  37. case cmStateEnums::SHARED_LIBRARY:
  38. return "SHARED_LIBRARY";
  39. case cmStateEnums::OBJECT_LIBRARY:
  40. return "OBJECT_LIBRARY";
  41. case cmStateEnums::EXECUTABLE:
  42. return "EXECUTABLE";
  43. case cmStateEnums::UTILITY:
  44. return "UTILITY";
  45. case cmStateEnums::GLOBAL_TARGET:
  46. return "GLOBAL_TARGET";
  47. case cmStateEnums::INTERFACE_LIBRARY:
  48. return "INTERFACE_LIBRARY";
  49. case cmStateEnums::UNKNOWN_LIBRARY:
  50. return "UNKNOWN_LIBRARY";
  51. }
  52. assert(false && "Unexpected target type");
  53. return nullptr;
  54. }
  55. const char* cmCacheEntryTypes[] = { "BOOL", "PATH", "FILEPATH",
  56. "STRING", "INTERNAL", "STATIC",
  57. "UNINITIALIZED", nullptr };
  58. const char* cmState::CacheEntryTypeToString(cmStateEnums::CacheEntryType type)
  59. {
  60. if (type > 6) {
  61. return cmCacheEntryTypes[6];
  62. }
  63. return cmCacheEntryTypes[type];
  64. }
  65. cmStateEnums::CacheEntryType cmState::StringToCacheEntryType(const char* s)
  66. {
  67. cmStateEnums::CacheEntryType type = cmStateEnums::STRING;
  68. StringToCacheEntryType(s, type);
  69. return type;
  70. }
  71. bool cmState::StringToCacheEntryType(const char* s,
  72. cmStateEnums::CacheEntryType& type)
  73. {
  74. int i = 0;
  75. while (cmCacheEntryTypes[i]) {
  76. if (strcmp(s, cmCacheEntryTypes[i]) == 0) {
  77. type = static_cast<cmStateEnums::CacheEntryType>(i);
  78. return true;
  79. }
  80. ++i;
  81. }
  82. return false;
  83. }
  84. bool cmState::IsCacheEntryType(std::string const& key)
  85. {
  86. for (int i = 0; cmCacheEntryTypes[i]; ++i) {
  87. if (key == cmCacheEntryTypes[i]) {
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. bool cmState::LoadCache(const std::string& path, bool internal,
  94. std::set<std::string>& excludes,
  95. std::set<std::string>& includes)
  96. {
  97. return this->CacheManager->LoadCache(path, internal, excludes, includes);
  98. }
  99. bool cmState::SaveCache(const std::string& path, cmMessenger* messenger)
  100. {
  101. return this->CacheManager->SaveCache(path, messenger);
  102. }
  103. bool cmState::DeleteCache(const std::string& path)
  104. {
  105. return this->CacheManager->DeleteCache(path);
  106. }
  107. std::vector<std::string> cmState::GetCacheEntryKeys() const
  108. {
  109. std::vector<std::string> definitions;
  110. definitions.reserve(this->CacheManager->GetSize());
  111. cmCacheManager::CacheIterator cit = this->CacheManager->GetCacheIterator();
  112. for (cit.Begin(); !cit.IsAtEnd(); cit.Next()) {
  113. definitions.push_back(cit.GetName());
  114. }
  115. return definitions;
  116. }
  117. const char* cmState::GetCacheEntryValue(std::string const& key) const
  118. {
  119. cmCacheManager::CacheEntry* e = this->CacheManager->GetCacheEntry(key);
  120. if (!e) {
  121. return nullptr;
  122. }
  123. return e->Value.c_str();
  124. }
  125. const std::string* cmState::GetInitializedCacheValue(
  126. std::string const& key) const
  127. {
  128. return this->CacheManager->GetInitializedCacheValue(key);
  129. }
  130. cmStateEnums::CacheEntryType cmState::GetCacheEntryType(
  131. std::string const& key) const
  132. {
  133. cmCacheManager::CacheIterator it =
  134. this->CacheManager->GetCacheIterator(key.c_str());
  135. return it.GetType();
  136. }
  137. void cmState::SetCacheEntryValue(std::string const& key,
  138. std::string const& value)
  139. {
  140. this->CacheManager->SetCacheEntryValue(key, value);
  141. }
  142. void cmState::SetCacheEntryProperty(std::string const& key,
  143. std::string const& propertyName,
  144. std::string const& value)
  145. {
  146. cmCacheManager::CacheIterator it =
  147. this->CacheManager->GetCacheIterator(key.c_str());
  148. it.SetProperty(propertyName, value.c_str());
  149. }
  150. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  151. std::string const& propertyName,
  152. bool value)
  153. {
  154. cmCacheManager::CacheIterator it =
  155. this->CacheManager->GetCacheIterator(key.c_str());
  156. it.SetProperty(propertyName, value);
  157. }
  158. std::vector<std::string> cmState::GetCacheEntryPropertyList(
  159. const std::string& key)
  160. {
  161. cmCacheManager::CacheIterator it =
  162. this->CacheManager->GetCacheIterator(key.c_str());
  163. return it.GetPropertyList();
  164. }
  165. const char* cmState::GetCacheEntryProperty(std::string const& key,
  166. std::string const& propertyName)
  167. {
  168. cmCacheManager::CacheIterator it =
  169. this->CacheManager->GetCacheIterator(key.c_str());
  170. if (!it.PropertyExists(propertyName)) {
  171. return nullptr;
  172. }
  173. return it.GetProperty(propertyName);
  174. }
  175. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  176. std::string const& propertyName)
  177. {
  178. return this->CacheManager->GetCacheIterator(key.c_str())
  179. .GetPropertyAsBool(propertyName);
  180. }
  181. void cmState::AddCacheEntry(const std::string& key, const char* value,
  182. const char* helpString,
  183. cmStateEnums::CacheEntryType type)
  184. {
  185. this->CacheManager->AddCacheEntry(key, value, helpString, type);
  186. }
  187. bool cmState::DoWriteGlobVerifyTarget() const
  188. {
  189. return this->GlobVerificationManager->DoWriteVerifyTarget();
  190. }
  191. std::string const& cmState::GetGlobVerifyScript() const
  192. {
  193. return this->GlobVerificationManager->GetVerifyScript();
  194. }
  195. std::string const& cmState::GetGlobVerifyStamp() const
  196. {
  197. return this->GlobVerificationManager->GetVerifyStamp();
  198. }
  199. bool cmState::SaveVerificationScript(const std::string& path)
  200. {
  201. return this->GlobVerificationManager->SaveVerificationScript(path);
  202. }
  203. void cmState::AddGlobCacheEntry(bool recurse, bool listDirectories,
  204. bool followSymlinks,
  205. const std::string& relative,
  206. const std::string& expression,
  207. const std::vector<std::string>& files,
  208. const std::string& variable,
  209. cmListFileBacktrace const& backtrace)
  210. {
  211. this->GlobVerificationManager->AddCacheEntry(
  212. recurse, listDirectories, followSymlinks, relative, expression, files,
  213. variable, backtrace);
  214. }
  215. void cmState::RemoveCacheEntry(std::string const& key)
  216. {
  217. this->CacheManager->RemoveCacheEntry(key);
  218. }
  219. void cmState::AppendCacheEntryProperty(const std::string& key,
  220. const std::string& property,
  221. const std::string& value, bool asString)
  222. {
  223. this->CacheManager->GetCacheIterator(key.c_str())
  224. .AppendProperty(property, value.c_str(), asString);
  225. }
  226. void cmState::RemoveCacheEntryProperty(std::string const& key,
  227. std::string const& propertyName)
  228. {
  229. this->CacheManager->GetCacheIterator(key.c_str())
  230. .SetProperty(propertyName, nullptr);
  231. }
  232. cmStateSnapshot cmState::Reset()
  233. {
  234. this->GlobalProperties.Clear();
  235. this->PropertyDefinitions.clear();
  236. this->GlobVerificationManager->Reset();
  237. cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
  238. this->ExecutionListFiles.Truncate();
  239. {
  240. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator it =
  241. this->BuildsystemDirectory.Truncate();
  242. it->IncludeDirectories.clear();
  243. it->IncludeDirectoryBacktraces.clear();
  244. it->CompileDefinitions.clear();
  245. it->CompileDefinitionsBacktraces.clear();
  246. it->CompileOptions.clear();
  247. it->CompileOptionsBacktraces.clear();
  248. it->LinkOptions.clear();
  249. it->LinkOptionsBacktraces.clear();
  250. it->LinkDirectories.clear();
  251. it->LinkDirectoriesBacktraces.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);
  273. pos->Vars->Set("CMAKE_BINARY_DIR", binDir);
  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 { 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,
  367. std::unique_ptr<cmCommand> command)
  368. {
  369. this->AddBuiltinCommand(name, cmLegacyCommandWrapper(std::move(command)));
  370. }
  371. void cmState::AddBuiltinCommand(std::string const& name, Command command)
  372. {
  373. assert(name == cmSystemTools::LowerCase(name));
  374. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  375. this->BuiltinCommands.emplace(name, std::move(command));
  376. }
  377. static bool InvokeBuiltinCommand(cmState::BuiltinCommand command,
  378. std::vector<cmListFileArgument> const& args,
  379. cmExecutionStatus& status)
  380. {
  381. cmMakefile& mf = status.GetMakefile();
  382. std::vector<std::string> expandedArguments;
  383. if (!mf.ExpandArguments(args, expandedArguments)) {
  384. // There was an error expanding arguments. It was already
  385. // reported, so we can skip this command without error.
  386. return true;
  387. }
  388. return command(expandedArguments, status);
  389. }
  390. void cmState::AddBuiltinCommand(std::string const& name,
  391. BuiltinCommand command)
  392. {
  393. this->AddBuiltinCommand(
  394. name,
  395. [command](const std::vector<cmListFileArgument>& args,
  396. cmExecutionStatus& status) -> bool {
  397. return InvokeBuiltinCommand(command, args, status);
  398. });
  399. }
  400. void cmState::AddDisallowedCommand(std::string const& name,
  401. BuiltinCommand command,
  402. cmPolicies::PolicyID policy,
  403. const char* message)
  404. {
  405. this->AddBuiltinCommand(
  406. name,
  407. [command, policy, message](const std::vector<cmListFileArgument>& args,
  408. cmExecutionStatus& status) -> bool {
  409. cmMakefile& mf = status.GetMakefile();
  410. switch (mf.GetPolicyStatus(policy)) {
  411. case cmPolicies::WARN:
  412. mf.IssueMessage(MessageType::AUTHOR_WARNING,
  413. cmPolicies::GetPolicyWarning(policy));
  414. break;
  415. case cmPolicies::OLD:
  416. break;
  417. case cmPolicies::REQUIRED_IF_USED:
  418. case cmPolicies::REQUIRED_ALWAYS:
  419. case cmPolicies::NEW:
  420. mf.IssueMessage(MessageType::FATAL_ERROR, message);
  421. return true;
  422. }
  423. return InvokeBuiltinCommand(command, args, status);
  424. });
  425. }
  426. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  427. {
  428. this->AddBuiltinCommand(
  429. name,
  430. [name, error](std::vector<cmListFileArgument> const&,
  431. cmExecutionStatus& status) -> bool {
  432. const char* versionValue =
  433. status.GetMakefile().GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  434. if (name == "endif" && (!versionValue || atof(versionValue) <= 1.4)) {
  435. return true;
  436. }
  437. status.SetError(error);
  438. return false;
  439. });
  440. }
  441. void cmState::AddScriptedCommand(std::string const& name, Command command)
  442. {
  443. std::string sName = cmSystemTools::LowerCase(name);
  444. // if the command already exists, give a new name to the old command.
  445. if (Command oldCmd = this->GetCommand(sName)) {
  446. this->ScriptedCommands["_" + sName] = oldCmd;
  447. }
  448. this->ScriptedCommands[sName] = std::move(command);
  449. }
  450. cmState::Command cmState::GetCommand(std::string const& name) const
  451. {
  452. return GetCommandByExactName(cmSystemTools::LowerCase(name));
  453. }
  454. cmState::Command cmState::GetCommandByExactName(std::string const& name) const
  455. {
  456. auto pos = this->ScriptedCommands.find(name);
  457. if (pos != this->ScriptedCommands.end()) {
  458. return pos->second;
  459. }
  460. pos = this->BuiltinCommands.find(name);
  461. if (pos != this->BuiltinCommands.end()) {
  462. return pos->second;
  463. }
  464. return nullptr;
  465. }
  466. std::vector<std::string> cmState::GetCommandNames() const
  467. {
  468. std::vector<std::string> commandNames;
  469. commandNames.reserve(this->BuiltinCommands.size() +
  470. this->ScriptedCommands.size());
  471. for (auto const& bc : this->BuiltinCommands) {
  472. commandNames.push_back(bc.first);
  473. }
  474. for (auto const& sc : this->ScriptedCommands) {
  475. commandNames.push_back(sc.first);
  476. }
  477. std::sort(commandNames.begin(), commandNames.end());
  478. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  479. commandNames.end());
  480. return commandNames;
  481. }
  482. void cmState::RemoveBuiltinCommand(std::string const& name)
  483. {
  484. assert(name == cmSystemTools::LowerCase(name));
  485. this->BuiltinCommands.erase(name);
  486. }
  487. void cmState::RemoveUserDefinedCommands()
  488. {
  489. this->ScriptedCommands.clear();
  490. }
  491. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  492. {
  493. this->GlobalProperties.SetProperty(prop, value);
  494. }
  495. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  496. bool asString)
  497. {
  498. this->GlobalProperties.AppendProperty(prop, value, asString);
  499. }
  500. const char* cmState::GetGlobalProperty(const std::string& prop)
  501. {
  502. if (prop == "CACHE_VARIABLES") {
  503. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  504. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  505. } else if (prop == "COMMANDS") {
  506. std::vector<std::string> commands = this->GetCommandNames();
  507. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  508. } else if (prop == "IN_TRY_COMPILE") {
  509. this->SetGlobalProperty("IN_TRY_COMPILE",
  510. this->IsInTryCompile ? "1" : "0");
  511. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  512. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  513. this->IsGeneratorMultiConfig ? "1" : "0");
  514. } else if (prop == "ENABLED_LANGUAGES") {
  515. std::string langs;
  516. langs = cmJoin(this->EnabledLanguages, ";");
  517. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  518. } else if (prop == "CMAKE_ROLE") {
  519. std::string mode = this->GetModeString();
  520. this->SetGlobalProperty("CMAKE_ROLE", mode.c_str());
  521. }
  522. #define STRING_LIST_ELEMENT(F) ";" #F
  523. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  524. return &FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT)[1];
  525. }
  526. if (prop == "CMAKE_C90_KNOWN_FEATURES") {
  527. return &FOR_EACH_C90_FEATURE(STRING_LIST_ELEMENT)[1];
  528. }
  529. if (prop == "CMAKE_C99_KNOWN_FEATURES") {
  530. return &FOR_EACH_C99_FEATURE(STRING_LIST_ELEMENT)[1];
  531. }
  532. if (prop == "CMAKE_C11_KNOWN_FEATURES") {
  533. return &FOR_EACH_C11_FEATURE(STRING_LIST_ELEMENT)[1];
  534. }
  535. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  536. return &FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT)[1];
  537. }
  538. if (prop == "CMAKE_CXX98_KNOWN_FEATURES") {
  539. return &FOR_EACH_CXX98_FEATURE(STRING_LIST_ELEMENT)[1];
  540. }
  541. if (prop == "CMAKE_CXX11_KNOWN_FEATURES") {
  542. return &FOR_EACH_CXX11_FEATURE(STRING_LIST_ELEMENT)[1];
  543. }
  544. if (prop == "CMAKE_CXX14_KNOWN_FEATURES") {
  545. return &FOR_EACH_CXX14_FEATURE(STRING_LIST_ELEMENT)[1];
  546. }
  547. #undef STRING_LIST_ELEMENT
  548. return this->GlobalProperties.GetPropertyValue(prop);
  549. }
  550. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  551. {
  552. return cmIsOn(this->GetGlobalProperty(prop));
  553. }
  554. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  555. {
  556. this->SourceDirectory = sourceDirectory;
  557. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  558. }
  559. std::string const& cmState::GetSourceDirectory() const
  560. {
  561. return this->SourceDirectory;
  562. }
  563. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  564. {
  565. this->BinaryDirectory = binaryDirectory;
  566. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  567. }
  568. void cmState::SetWindowsShell(bool windowsShell)
  569. {
  570. this->WindowsShell = windowsShell;
  571. }
  572. bool cmState::UseWindowsShell() const
  573. {
  574. return this->WindowsShell;
  575. }
  576. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  577. {
  578. this->WindowsVSIDE = windowsVSIDE;
  579. }
  580. bool cmState::UseWindowsVSIDE() const
  581. {
  582. return this->WindowsVSIDE;
  583. }
  584. void cmState::SetGhsMultiIDE(bool ghsMultiIDE)
  585. {
  586. this->GhsMultiIDE = ghsMultiIDE;
  587. }
  588. bool cmState::UseGhsMultiIDE() const
  589. {
  590. return this->GhsMultiIDE;
  591. }
  592. void cmState::SetWatcomWMake(bool watcomWMake)
  593. {
  594. this->WatcomWMake = watcomWMake;
  595. }
  596. bool cmState::UseWatcomWMake() const
  597. {
  598. return this->WatcomWMake;
  599. }
  600. void cmState::SetMinGWMake(bool minGWMake)
  601. {
  602. this->MinGWMake = minGWMake;
  603. }
  604. bool cmState::UseMinGWMake() const
  605. {
  606. return this->MinGWMake;
  607. }
  608. void cmState::SetNMake(bool nMake)
  609. {
  610. this->NMake = nMake;
  611. }
  612. bool cmState::UseNMake() const
  613. {
  614. return this->NMake;
  615. }
  616. void cmState::SetMSYSShell(bool mSYSShell)
  617. {
  618. this->MSYSShell = mSYSShell;
  619. }
  620. bool cmState::UseMSYSShell() const
  621. {
  622. return this->MSYSShell;
  623. }
  624. unsigned int cmState::GetCacheMajorVersion() const
  625. {
  626. return this->CacheManager->GetCacheMajorVersion();
  627. }
  628. unsigned int cmState::GetCacheMinorVersion() const
  629. {
  630. return this->CacheManager->GetCacheMinorVersion();
  631. }
  632. cmState::Mode cmState::GetMode() const
  633. {
  634. return this->CurrentMode;
  635. }
  636. std::string cmState::GetModeString() const
  637. {
  638. return ModeToString(this->CurrentMode);
  639. }
  640. void cmState::SetMode(cmState::Mode mode)
  641. {
  642. this->CurrentMode = mode;
  643. }
  644. std::string cmState::ModeToString(cmState::Mode mode)
  645. {
  646. switch (mode) {
  647. case Project:
  648. return "PROJECT";
  649. case Script:
  650. return "SCRIPT";
  651. case FindPackage:
  652. return "FIND_PACKAGE";
  653. case CTest:
  654. return "CTEST";
  655. case CPack:
  656. return "CPACK";
  657. case Unknown:
  658. return "UNKNOWN";
  659. }
  660. return "UNKNOWN";
  661. }
  662. std::string const& cmState::GetBinaryDirectory() const
  663. {
  664. return this->BinaryDirectory;
  665. }
  666. cmStateSnapshot cmState::CreateBaseSnapshot()
  667. {
  668. cmStateDetail::PositionType pos =
  669. this->SnapshotData.Push(this->SnapshotData.Root());
  670. pos->DirectoryParent = this->SnapshotData.Root();
  671. pos->ScopeParent = this->SnapshotData.Root();
  672. pos->SnapshotType = cmStateEnums::BaseType;
  673. pos->Keep = true;
  674. pos->BuildSystemDirectory =
  675. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  676. pos->ExecutionListFile =
  677. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  678. pos->IncludeDirectoryPosition = 0;
  679. pos->CompileDefinitionsPosition = 0;
  680. pos->CompileOptionsPosition = 0;
  681. pos->LinkOptionsPosition = 0;
  682. pos->LinkDirectoriesPosition = 0;
  683. pos->BuildSystemDirectory->DirectoryEnd = pos;
  684. pos->Policies = this->PolicyStack.Root();
  685. pos->PolicyRoot = this->PolicyStack.Root();
  686. pos->PolicyScope = this->PolicyStack.Root();
  687. assert(pos->Policies.IsValid());
  688. assert(pos->PolicyRoot.IsValid());
  689. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  690. assert(pos->Vars.IsValid());
  691. pos->Parent = this->VarTree.Root();
  692. pos->Root = this->VarTree.Root();
  693. return { this, pos };
  694. }
  695. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  696. cmStateSnapshot const& originSnapshot)
  697. {
  698. assert(originSnapshot.IsValid());
  699. cmStateDetail::PositionType pos =
  700. this->SnapshotData.Push(originSnapshot.Position);
  701. pos->DirectoryParent = originSnapshot.Position;
  702. pos->ScopeParent = originSnapshot.Position;
  703. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  704. pos->Keep = true;
  705. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  706. originSnapshot.Position->BuildSystemDirectory);
  707. pos->ExecutionListFile =
  708. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  709. pos->BuildSystemDirectory->DirectoryEnd = pos;
  710. pos->Policies = originSnapshot.Position->Policies;
  711. pos->PolicyRoot = originSnapshot.Position->Policies;
  712. pos->PolicyScope = originSnapshot.Position->Policies;
  713. assert(pos->Policies.IsValid());
  714. assert(pos->PolicyRoot.IsValid());
  715. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  716. pos->Parent = origin;
  717. pos->Root = origin;
  718. pos->Vars = this->VarTree.Push(origin);
  719. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  720. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  721. snapshot.SetDefaultDefinitions();
  722. snapshot.InitializeFromParent();
  723. snapshot.SetDirectoryDefinitions();
  724. return snapshot;
  725. }
  726. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  727. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  728. {
  729. cmStateDetail::PositionType pos =
  730. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  731. pos->ScopeParent = originSnapshot.Position;
  732. pos->SnapshotType = cmStateEnums::FunctionCallType;
  733. pos->Keep = false;
  734. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  735. originSnapshot.Position->ExecutionListFile, fileName);
  736. pos->BuildSystemDirectory->DirectoryEnd = pos;
  737. pos->PolicyScope = originSnapshot.Position->Policies;
  738. assert(originSnapshot.Position->Vars.IsValid());
  739. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  740. pos->Parent = origin;
  741. pos->Vars = this->VarTree.Push(origin);
  742. return { this, pos };
  743. }
  744. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  745. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  746. {
  747. cmStateDetail::PositionType pos =
  748. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  749. pos->SnapshotType = cmStateEnums::MacroCallType;
  750. pos->Keep = false;
  751. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  752. originSnapshot.Position->ExecutionListFile, fileName);
  753. assert(originSnapshot.Position->Vars.IsValid());
  754. pos->BuildSystemDirectory->DirectoryEnd = pos;
  755. pos->PolicyScope = originSnapshot.Position->Policies;
  756. return { this, pos };
  757. }
  758. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  759. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  760. {
  761. cmStateDetail::PositionType pos =
  762. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  763. pos->SnapshotType = cmStateEnums::IncludeFileType;
  764. pos->Keep = true;
  765. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  766. originSnapshot.Position->ExecutionListFile, fileName);
  767. assert(originSnapshot.Position->Vars.IsValid());
  768. pos->BuildSystemDirectory->DirectoryEnd = pos;
  769. pos->PolicyScope = originSnapshot.Position->Policies;
  770. return { this, pos };
  771. }
  772. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  773. cmStateSnapshot const& originSnapshot)
  774. {
  775. cmStateDetail::PositionType pos =
  776. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  777. pos->ScopeParent = originSnapshot.Position;
  778. pos->SnapshotType = cmStateEnums::VariableScopeType;
  779. pos->Keep = false;
  780. pos->PolicyScope = originSnapshot.Position->Policies;
  781. assert(originSnapshot.Position->Vars.IsValid());
  782. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  783. pos->Parent = origin;
  784. pos->Vars = this->VarTree.Push(origin);
  785. assert(pos->Vars.IsValid());
  786. return { this, pos };
  787. }
  788. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  789. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  790. {
  791. cmStateDetail::PositionType pos =
  792. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  793. pos->SnapshotType = cmStateEnums::InlineListFileType;
  794. pos->Keep = true;
  795. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  796. originSnapshot.Position->ExecutionListFile, fileName);
  797. pos->BuildSystemDirectory->DirectoryEnd = pos;
  798. pos->PolicyScope = originSnapshot.Position->Policies;
  799. return { this, pos };
  800. }
  801. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  802. cmStateSnapshot const& originSnapshot)
  803. {
  804. cmStateDetail::PositionType pos =
  805. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  806. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  807. pos->Keep = false;
  808. pos->BuildSystemDirectory->DirectoryEnd = pos;
  809. pos->PolicyScope = originSnapshot.Position->Policies;
  810. return { this, pos };
  811. }
  812. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  813. {
  814. cmStateDetail::PositionType pos = originSnapshot.Position;
  815. cmStateDetail::PositionType prevPos = pos;
  816. ++prevPos;
  817. prevPos->IncludeDirectoryPosition =
  818. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  819. prevPos->CompileDefinitionsPosition =
  820. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  821. prevPos->CompileOptionsPosition =
  822. prevPos->BuildSystemDirectory->CompileOptions.size();
  823. prevPos->LinkOptionsPosition =
  824. prevPos->BuildSystemDirectory->LinkOptions.size();
  825. prevPos->LinkDirectoriesPosition =
  826. prevPos->BuildSystemDirectory->LinkDirectories.size();
  827. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  828. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  829. if (pos->Vars != prevPos->Vars) {
  830. assert(this->VarTree.IsLast(pos->Vars));
  831. this->VarTree.Pop(pos->Vars);
  832. }
  833. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  834. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  835. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  836. }
  837. this->SnapshotData.Pop(pos);
  838. }
  839. return { this, prevPos };
  840. }
  841. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  842. std::string& value)
  843. {
  844. // input line is: key=value
  845. static cmsys::RegularExpression reg(
  846. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  847. // input line is: "key"=value
  848. static cmsys::RegularExpression regQuoted(
  849. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  850. bool flag = false;
  851. if (regQuoted.find(entry)) {
  852. var = regQuoted.match(1);
  853. value = regQuoted.match(2);
  854. flag = true;
  855. } else if (reg.find(entry)) {
  856. var = reg.match(1);
  857. value = reg.match(2);
  858. flag = true;
  859. }
  860. // if value is enclosed in single quotes ('foo') then remove them
  861. // it is used to enclose trailing space or tab
  862. if (flag && value.size() >= 2 && value.front() == '\'' &&
  863. value.back() == '\'') {
  864. value = value.substr(1, value.size() - 2);
  865. }
  866. return flag;
  867. }
  868. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  869. std::string& value,
  870. cmStateEnums::CacheEntryType& type)
  871. {
  872. // input line is: key:type=value
  873. static cmsys::RegularExpression reg(
  874. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  875. // input line is: "key":type=value
  876. static cmsys::RegularExpression regQuoted(
  877. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  878. bool flag = false;
  879. if (regQuoted.find(entry)) {
  880. var = regQuoted.match(1);
  881. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  882. value = regQuoted.match(3);
  883. flag = true;
  884. } else if (reg.find(entry)) {
  885. var = reg.match(1);
  886. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  887. value = reg.match(3);
  888. flag = true;
  889. }
  890. // if value is enclosed in single quotes ('foo') then remove them
  891. // it is used to enclose trailing space or tab
  892. if (flag && value.size() >= 2 && value.front() == '\'' &&
  893. value.back() == '\'') {
  894. value = value.substr(1, value.size() - 2);
  895. }
  896. if (!flag) {
  897. return ParseEntryWithoutType(entry, var, value);
  898. }
  899. return flag;
  900. }