cmState.cxx 31 KB

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