cmState.cxx 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  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 <cassert>
  6. #include <cstdlib>
  7. #include <cstring>
  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. 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. auto it = 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. auto it = this->PropertyDefinitions.find(scope);
  318. if (it == this->PropertyDefinitions.end()) {
  319. return false;
  320. }
  321. return it->second.IsPropertyChained(name);
  322. }
  323. void cmState::SetLanguageEnabled(std::string const& l)
  324. {
  325. auto it = std::lower_bound(this->EnabledLanguages.begin(),
  326. this->EnabledLanguages.end(), l);
  327. if (it == this->EnabledLanguages.end() || *it != l) {
  328. this->EnabledLanguages.insert(it, l);
  329. }
  330. }
  331. bool cmState::GetLanguageEnabled(std::string const& l) const
  332. {
  333. return std::binary_search(this->EnabledLanguages.begin(),
  334. this->EnabledLanguages.end(), l);
  335. }
  336. std::vector<std::string> cmState::GetEnabledLanguages() const
  337. {
  338. return this->EnabledLanguages;
  339. }
  340. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  341. {
  342. this->EnabledLanguages = langs;
  343. }
  344. void cmState::ClearEnabledLanguages()
  345. {
  346. this->EnabledLanguages.clear();
  347. }
  348. bool cmState::GetIsInTryCompile() const
  349. {
  350. return this->IsInTryCompile;
  351. }
  352. void cmState::SetIsInTryCompile(bool b)
  353. {
  354. this->IsInTryCompile = b;
  355. }
  356. bool cmState::GetIsGeneratorMultiConfig() const
  357. {
  358. return this->IsGeneratorMultiConfig;
  359. }
  360. void cmState::SetIsGeneratorMultiConfig(bool b)
  361. {
  362. this->IsGeneratorMultiConfig = b;
  363. }
  364. void cmState::AddBuiltinCommand(std::string const& name,
  365. std::unique_ptr<cmCommand> command)
  366. {
  367. this->AddBuiltinCommand(name, cmLegacyCommandWrapper(std::move(command)));
  368. }
  369. void cmState::AddBuiltinCommand(std::string const& name, Command command)
  370. {
  371. assert(name == cmSystemTools::LowerCase(name));
  372. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  373. this->BuiltinCommands.emplace(name, std::move(command));
  374. }
  375. static bool InvokeBuiltinCommand(cmState::BuiltinCommand command,
  376. std::vector<cmListFileArgument> const& args,
  377. cmExecutionStatus& status)
  378. {
  379. cmMakefile& mf = status.GetMakefile();
  380. std::vector<std::string> expandedArguments;
  381. if (!mf.ExpandArguments(args, expandedArguments)) {
  382. // There was an error expanding arguments. It was already
  383. // reported, so we can skip this command without error.
  384. return true;
  385. }
  386. return command(expandedArguments, status);
  387. }
  388. void cmState::AddBuiltinCommand(std::string const& name,
  389. BuiltinCommand command)
  390. {
  391. this->AddBuiltinCommand(
  392. name,
  393. [command](const std::vector<cmListFileArgument>& args,
  394. cmExecutionStatus& status) -> bool {
  395. return InvokeBuiltinCommand(command, args, status);
  396. });
  397. }
  398. void cmState::AddDisallowedCommand(std::string const& name,
  399. BuiltinCommand command,
  400. cmPolicies::PolicyID policy,
  401. const char* message)
  402. {
  403. this->AddBuiltinCommand(
  404. name,
  405. [command, policy, message](const std::vector<cmListFileArgument>& args,
  406. cmExecutionStatus& status) -> bool {
  407. cmMakefile& mf = status.GetMakefile();
  408. switch (mf.GetPolicyStatus(policy)) {
  409. case cmPolicies::WARN:
  410. mf.IssueMessage(MessageType::AUTHOR_WARNING,
  411. cmPolicies::GetPolicyWarning(policy));
  412. break;
  413. case cmPolicies::OLD:
  414. break;
  415. case cmPolicies::REQUIRED_IF_USED:
  416. case cmPolicies::REQUIRED_ALWAYS:
  417. case cmPolicies::NEW:
  418. mf.IssueMessage(MessageType::FATAL_ERROR, message);
  419. return true;
  420. }
  421. return InvokeBuiltinCommand(command, args, status);
  422. });
  423. }
  424. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  425. {
  426. this->AddBuiltinCommand(
  427. name,
  428. [name, error](std::vector<cmListFileArgument> const&,
  429. cmExecutionStatus& status) -> bool {
  430. const char* versionValue =
  431. status.GetMakefile().GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  432. if (name == "endif" && (!versionValue || atof(versionValue) <= 1.4)) {
  433. return true;
  434. }
  435. status.SetError(error);
  436. return false;
  437. });
  438. }
  439. void cmState::AddScriptedCommand(std::string const& name, Command command)
  440. {
  441. std::string sName = cmSystemTools::LowerCase(name);
  442. // if the command already exists, give a new name to the old command.
  443. if (Command oldCmd = this->GetCommandByExactName(sName)) {
  444. this->ScriptedCommands["_" + sName] = oldCmd;
  445. }
  446. this->ScriptedCommands[sName] = std::move(command);
  447. }
  448. cmState::Command cmState::GetCommand(std::string const& name) const
  449. {
  450. return GetCommandByExactName(cmSystemTools::LowerCase(name));
  451. }
  452. cmState::Command cmState::GetCommandByExactName(std::string const& name) const
  453. {
  454. auto pos = this->ScriptedCommands.find(name);
  455. if (pos != this->ScriptedCommands.end()) {
  456. return pos->second;
  457. }
  458. pos = this->BuiltinCommands.find(name);
  459. if (pos != this->BuiltinCommands.end()) {
  460. return pos->second;
  461. }
  462. return nullptr;
  463. }
  464. std::vector<std::string> cmState::GetCommandNames() const
  465. {
  466. std::vector<std::string> commandNames;
  467. commandNames.reserve(this->BuiltinCommands.size() +
  468. this->ScriptedCommands.size());
  469. for (auto const& bc : this->BuiltinCommands) {
  470. commandNames.push_back(bc.first);
  471. }
  472. for (auto const& sc : this->ScriptedCommands) {
  473. commandNames.push_back(sc.first);
  474. }
  475. std::sort(commandNames.begin(), commandNames.end());
  476. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  477. commandNames.end());
  478. return commandNames;
  479. }
  480. void cmState::RemoveBuiltinCommand(std::string const& name)
  481. {
  482. assert(name == cmSystemTools::LowerCase(name));
  483. this->BuiltinCommands.erase(name);
  484. }
  485. void cmState::RemoveUserDefinedCommands()
  486. {
  487. this->ScriptedCommands.clear();
  488. }
  489. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  490. {
  491. this->GlobalProperties.SetProperty(prop, value);
  492. }
  493. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  494. bool asString)
  495. {
  496. this->GlobalProperties.AppendProperty(prop, value, asString);
  497. }
  498. const char* cmState::GetGlobalProperty(const std::string& prop)
  499. {
  500. if (prop == "CACHE_VARIABLES") {
  501. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  502. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  503. } else if (prop == "COMMANDS") {
  504. std::vector<std::string> commands = this->GetCommandNames();
  505. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  506. } else if (prop == "IN_TRY_COMPILE") {
  507. this->SetGlobalProperty("IN_TRY_COMPILE",
  508. this->IsInTryCompile ? "1" : "0");
  509. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  510. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  511. this->IsGeneratorMultiConfig ? "1" : "0");
  512. } else if (prop == "ENABLED_LANGUAGES") {
  513. std::string langs;
  514. langs = cmJoin(this->EnabledLanguages, ";");
  515. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  516. } else if (prop == "CMAKE_ROLE") {
  517. std::string mode = this->GetModeString();
  518. this->SetGlobalProperty("CMAKE_ROLE", mode.c_str());
  519. }
  520. #define STRING_LIST_ELEMENT(F) ";" #F
  521. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  522. return &FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT)[1];
  523. }
  524. if (prop == "CMAKE_C90_KNOWN_FEATURES") {
  525. return &FOR_EACH_C90_FEATURE(STRING_LIST_ELEMENT)[1];
  526. }
  527. if (prop == "CMAKE_C99_KNOWN_FEATURES") {
  528. return &FOR_EACH_C99_FEATURE(STRING_LIST_ELEMENT)[1];
  529. }
  530. if (prop == "CMAKE_C11_KNOWN_FEATURES") {
  531. return &FOR_EACH_C11_FEATURE(STRING_LIST_ELEMENT)[1];
  532. }
  533. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  534. return &FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT)[1];
  535. }
  536. if (prop == "CMAKE_CXX98_KNOWN_FEATURES") {
  537. return &FOR_EACH_CXX98_FEATURE(STRING_LIST_ELEMENT)[1];
  538. }
  539. if (prop == "CMAKE_CXX11_KNOWN_FEATURES") {
  540. return &FOR_EACH_CXX11_FEATURE(STRING_LIST_ELEMENT)[1];
  541. }
  542. if (prop == "CMAKE_CXX14_KNOWN_FEATURES") {
  543. return &FOR_EACH_CXX14_FEATURE(STRING_LIST_ELEMENT)[1];
  544. }
  545. if (prop == "CMAKE_CUDA_KNOWN_FEATURES") {
  546. return &FOR_EACH_CUDA_FEATURE(STRING_LIST_ELEMENT)[1];
  547. }
  548. #undef STRING_LIST_ELEMENT
  549. return this->GlobalProperties.GetPropertyValue(prop);
  550. }
  551. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  552. {
  553. return cmIsOn(this->GetGlobalProperty(prop));
  554. }
  555. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  556. {
  557. this->SourceDirectory = sourceDirectory;
  558. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  559. }
  560. std::string const& cmState::GetSourceDirectory() const
  561. {
  562. return this->SourceDirectory;
  563. }
  564. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  565. {
  566. this->BinaryDirectory = binaryDirectory;
  567. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  568. }
  569. void cmState::SetWindowsShell(bool windowsShell)
  570. {
  571. this->WindowsShell = windowsShell;
  572. }
  573. bool cmState::UseWindowsShell() const
  574. {
  575. return this->WindowsShell;
  576. }
  577. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  578. {
  579. this->WindowsVSIDE = windowsVSIDE;
  580. }
  581. bool cmState::UseWindowsVSIDE() const
  582. {
  583. return this->WindowsVSIDE;
  584. }
  585. void cmState::SetGhsMultiIDE(bool ghsMultiIDE)
  586. {
  587. this->GhsMultiIDE = ghsMultiIDE;
  588. }
  589. bool cmState::UseGhsMultiIDE() const
  590. {
  591. return this->GhsMultiIDE;
  592. }
  593. void cmState::SetWatcomWMake(bool watcomWMake)
  594. {
  595. this->WatcomWMake = watcomWMake;
  596. }
  597. bool cmState::UseWatcomWMake() const
  598. {
  599. return this->WatcomWMake;
  600. }
  601. void cmState::SetMinGWMake(bool minGWMake)
  602. {
  603. this->MinGWMake = minGWMake;
  604. }
  605. bool cmState::UseMinGWMake() const
  606. {
  607. return this->MinGWMake;
  608. }
  609. void cmState::SetNMake(bool nMake)
  610. {
  611. this->NMake = nMake;
  612. }
  613. bool cmState::UseNMake() const
  614. {
  615. return this->NMake;
  616. }
  617. void cmState::SetMSYSShell(bool mSYSShell)
  618. {
  619. this->MSYSShell = mSYSShell;
  620. }
  621. bool cmState::UseMSYSShell() const
  622. {
  623. return this->MSYSShell;
  624. }
  625. void cmState::SetNinjaMulti(bool ninjaMulti)
  626. {
  627. this->NinjaMulti = ninjaMulti;
  628. }
  629. bool cmState::UseNinjaMulti() const
  630. {
  631. return this->NinjaMulti;
  632. }
  633. unsigned int cmState::GetCacheMajorVersion() const
  634. {
  635. return this->CacheManager->GetCacheMajorVersion();
  636. }
  637. unsigned int cmState::GetCacheMinorVersion() const
  638. {
  639. return this->CacheManager->GetCacheMinorVersion();
  640. }
  641. cmState::Mode cmState::GetMode() const
  642. {
  643. return this->CurrentMode;
  644. }
  645. std::string cmState::GetModeString() const
  646. {
  647. return ModeToString(this->CurrentMode);
  648. }
  649. void cmState::SetMode(cmState::Mode mode)
  650. {
  651. this->CurrentMode = mode;
  652. }
  653. std::string cmState::ModeToString(cmState::Mode mode)
  654. {
  655. switch (mode) {
  656. case Project:
  657. return "PROJECT";
  658. case Script:
  659. return "SCRIPT";
  660. case FindPackage:
  661. return "FIND_PACKAGE";
  662. case CTest:
  663. return "CTEST";
  664. case CPack:
  665. return "CPACK";
  666. case Unknown:
  667. return "UNKNOWN";
  668. }
  669. return "UNKNOWN";
  670. }
  671. std::string const& cmState::GetBinaryDirectory() const
  672. {
  673. return this->BinaryDirectory;
  674. }
  675. cmStateSnapshot cmState::CreateBaseSnapshot()
  676. {
  677. cmStateDetail::PositionType pos =
  678. this->SnapshotData.Push(this->SnapshotData.Root());
  679. pos->DirectoryParent = this->SnapshotData.Root();
  680. pos->ScopeParent = this->SnapshotData.Root();
  681. pos->SnapshotType = cmStateEnums::BaseType;
  682. pos->Keep = true;
  683. pos->BuildSystemDirectory =
  684. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  685. pos->ExecutionListFile =
  686. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  687. pos->IncludeDirectoryPosition = 0;
  688. pos->CompileDefinitionsPosition = 0;
  689. pos->CompileOptionsPosition = 0;
  690. pos->LinkOptionsPosition = 0;
  691. pos->LinkDirectoriesPosition = 0;
  692. pos->BuildSystemDirectory->DirectoryEnd = pos;
  693. pos->Policies = this->PolicyStack.Root();
  694. pos->PolicyRoot = this->PolicyStack.Root();
  695. pos->PolicyScope = this->PolicyStack.Root();
  696. assert(pos->Policies.IsValid());
  697. assert(pos->PolicyRoot.IsValid());
  698. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  699. assert(pos->Vars.IsValid());
  700. pos->Parent = this->VarTree.Root();
  701. pos->Root = this->VarTree.Root();
  702. return { this, pos };
  703. }
  704. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  705. cmStateSnapshot const& originSnapshot)
  706. {
  707. assert(originSnapshot.IsValid());
  708. cmStateDetail::PositionType pos =
  709. this->SnapshotData.Push(originSnapshot.Position);
  710. pos->DirectoryParent = originSnapshot.Position;
  711. pos->ScopeParent = originSnapshot.Position;
  712. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  713. pos->Keep = true;
  714. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  715. originSnapshot.Position->BuildSystemDirectory);
  716. pos->ExecutionListFile =
  717. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  718. pos->BuildSystemDirectory->DirectoryEnd = pos;
  719. pos->Policies = originSnapshot.Position->Policies;
  720. pos->PolicyRoot = originSnapshot.Position->Policies;
  721. pos->PolicyScope = originSnapshot.Position->Policies;
  722. assert(pos->Policies.IsValid());
  723. assert(pos->PolicyRoot.IsValid());
  724. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  725. pos->Parent = origin;
  726. pos->Root = origin;
  727. pos->Vars = this->VarTree.Push(origin);
  728. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  729. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  730. snapshot.SetDefaultDefinitions();
  731. snapshot.InitializeFromParent();
  732. snapshot.SetDirectoryDefinitions();
  733. return snapshot;
  734. }
  735. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  736. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  737. {
  738. cmStateDetail::PositionType pos =
  739. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  740. pos->ScopeParent = originSnapshot.Position;
  741. pos->SnapshotType = cmStateEnums::FunctionCallType;
  742. pos->Keep = false;
  743. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  744. originSnapshot.Position->ExecutionListFile, fileName);
  745. pos->BuildSystemDirectory->DirectoryEnd = pos;
  746. pos->PolicyScope = originSnapshot.Position->Policies;
  747. assert(originSnapshot.Position->Vars.IsValid());
  748. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  749. pos->Parent = origin;
  750. pos->Vars = this->VarTree.Push(origin);
  751. return { this, pos };
  752. }
  753. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  754. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  755. {
  756. cmStateDetail::PositionType pos =
  757. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  758. pos->SnapshotType = cmStateEnums::MacroCallType;
  759. pos->Keep = false;
  760. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  761. originSnapshot.Position->ExecutionListFile, fileName);
  762. assert(originSnapshot.Position->Vars.IsValid());
  763. pos->BuildSystemDirectory->DirectoryEnd = pos;
  764. pos->PolicyScope = originSnapshot.Position->Policies;
  765. return { this, pos };
  766. }
  767. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  768. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  769. {
  770. cmStateDetail::PositionType pos =
  771. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  772. pos->SnapshotType = cmStateEnums::IncludeFileType;
  773. pos->Keep = true;
  774. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  775. originSnapshot.Position->ExecutionListFile, fileName);
  776. assert(originSnapshot.Position->Vars.IsValid());
  777. pos->BuildSystemDirectory->DirectoryEnd = pos;
  778. pos->PolicyScope = originSnapshot.Position->Policies;
  779. return { this, pos };
  780. }
  781. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  782. cmStateSnapshot const& originSnapshot)
  783. {
  784. cmStateDetail::PositionType pos =
  785. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  786. pos->ScopeParent = originSnapshot.Position;
  787. pos->SnapshotType = cmStateEnums::VariableScopeType;
  788. pos->Keep = false;
  789. pos->PolicyScope = originSnapshot.Position->Policies;
  790. assert(originSnapshot.Position->Vars.IsValid());
  791. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  792. pos->Parent = origin;
  793. pos->Vars = this->VarTree.Push(origin);
  794. assert(pos->Vars.IsValid());
  795. return { this, pos };
  796. }
  797. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  798. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  799. {
  800. cmStateDetail::PositionType pos =
  801. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  802. pos->SnapshotType = cmStateEnums::InlineListFileType;
  803. pos->Keep = true;
  804. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  805. originSnapshot.Position->ExecutionListFile, fileName);
  806. pos->BuildSystemDirectory->DirectoryEnd = pos;
  807. pos->PolicyScope = originSnapshot.Position->Policies;
  808. return { this, pos };
  809. }
  810. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  811. cmStateSnapshot const& originSnapshot)
  812. {
  813. cmStateDetail::PositionType pos =
  814. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  815. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  816. pos->Keep = false;
  817. pos->BuildSystemDirectory->DirectoryEnd = pos;
  818. pos->PolicyScope = originSnapshot.Position->Policies;
  819. return { this, pos };
  820. }
  821. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  822. {
  823. cmStateDetail::PositionType pos = originSnapshot.Position;
  824. cmStateDetail::PositionType prevPos = pos;
  825. ++prevPos;
  826. prevPos->IncludeDirectoryPosition =
  827. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  828. prevPos->CompileDefinitionsPosition =
  829. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  830. prevPos->CompileOptionsPosition =
  831. prevPos->BuildSystemDirectory->CompileOptions.size();
  832. prevPos->LinkOptionsPosition =
  833. prevPos->BuildSystemDirectory->LinkOptions.size();
  834. prevPos->LinkDirectoriesPosition =
  835. prevPos->BuildSystemDirectory->LinkDirectories.size();
  836. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  837. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  838. if (pos->Vars != prevPos->Vars) {
  839. assert(this->VarTree.IsLast(pos->Vars));
  840. this->VarTree.Pop(pos->Vars);
  841. }
  842. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  843. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  844. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  845. }
  846. this->SnapshotData.Pop(pos);
  847. }
  848. return { this, prevPos };
  849. }
  850. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  851. std::string& value)
  852. {
  853. // input line is: key=value
  854. static cmsys::RegularExpression reg(
  855. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  856. // input line is: "key"=value
  857. static cmsys::RegularExpression regQuoted(
  858. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  859. bool flag = false;
  860. if (regQuoted.find(entry)) {
  861. var = regQuoted.match(1);
  862. value = regQuoted.match(2);
  863. flag = true;
  864. } else if (reg.find(entry)) {
  865. var = reg.match(1);
  866. value = reg.match(2);
  867. flag = true;
  868. }
  869. // if value is enclosed in single quotes ('foo') then remove them
  870. // it is used to enclose trailing space or tab
  871. if (flag && value.size() >= 2 && value.front() == '\'' &&
  872. value.back() == '\'') {
  873. value = value.substr(1, value.size() - 2);
  874. }
  875. return flag;
  876. }
  877. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  878. std::string& value,
  879. cmStateEnums::CacheEntryType& type)
  880. {
  881. // input line is: key:type=value
  882. static cmsys::RegularExpression reg(
  883. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  884. // input line is: "key":type=value
  885. static cmsys::RegularExpression regQuoted(
  886. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  887. bool flag = false;
  888. if (regQuoted.find(entry)) {
  889. var = regQuoted.match(1);
  890. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  891. value = regQuoted.match(3);
  892. flag = true;
  893. } else if (reg.find(entry)) {
  894. var = reg.match(1);
  895. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  896. value = reg.match(3);
  897. flag = true;
  898. }
  899. // if value is enclosed in single quotes ('foo') then remove them
  900. // it is used to enclose trailing space or tab
  901. if (flag && value.size() >= 2 && value.front() == '\'' &&
  902. value.back() == '\'') {
  903. value = value.substr(1, value.size() - 2);
  904. }
  905. if (!flag) {
  906. return ParseEntryWithoutType(entry, var, value);
  907. }
  908. return flag;
  909. }