cmState.cxx 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. cmProp 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. static const std::string s_out(
  527. &FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT)[1]);
  528. return &s_out;
  529. }
  530. if (prop == "CMAKE_C90_KNOWN_FEATURES") {
  531. static const std::string s_out(
  532. &FOR_EACH_C90_FEATURE(STRING_LIST_ELEMENT)[1]);
  533. return &s_out;
  534. }
  535. if (prop == "CMAKE_C99_KNOWN_FEATURES") {
  536. static const std::string s_out(
  537. &FOR_EACH_C99_FEATURE(STRING_LIST_ELEMENT)[1]);
  538. return &s_out;
  539. }
  540. if (prop == "CMAKE_C11_KNOWN_FEATURES") {
  541. static const std::string s_out(
  542. &FOR_EACH_C11_FEATURE(STRING_LIST_ELEMENT)[1]);
  543. return &s_out;
  544. }
  545. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  546. static const std::string s_out(
  547. &FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT)[1]);
  548. return &s_out;
  549. }
  550. if (prop == "CMAKE_CXX98_KNOWN_FEATURES") {
  551. static const std::string s_out(
  552. &FOR_EACH_CXX98_FEATURE(STRING_LIST_ELEMENT)[1]);
  553. return &s_out;
  554. }
  555. if (prop == "CMAKE_CXX11_KNOWN_FEATURES") {
  556. static const std::string s_out(
  557. &FOR_EACH_CXX11_FEATURE(STRING_LIST_ELEMENT)[1]);
  558. return &s_out;
  559. }
  560. if (prop == "CMAKE_CXX14_KNOWN_FEATURES") {
  561. static const std::string s_out(
  562. &FOR_EACH_CXX14_FEATURE(STRING_LIST_ELEMENT)[1]);
  563. return &s_out;
  564. }
  565. if (prop == "CMAKE_CUDA_KNOWN_FEATURES") {
  566. static const std::string s_out(
  567. &FOR_EACH_CUDA_FEATURE(STRING_LIST_ELEMENT)[1]);
  568. return &s_out;
  569. }
  570. #undef STRING_LIST_ELEMENT
  571. return this->GlobalProperties.GetPropertyValue(prop);
  572. }
  573. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  574. {
  575. cmProp p = this->GetGlobalProperty(prop);
  576. return p && cmIsOn(*p);
  577. }
  578. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  579. {
  580. this->SourceDirectory = sourceDirectory;
  581. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  582. }
  583. std::string const& cmState::GetSourceDirectory() const
  584. {
  585. return this->SourceDirectory;
  586. }
  587. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  588. {
  589. this->BinaryDirectory = binaryDirectory;
  590. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  591. }
  592. void cmState::SetWindowsShell(bool windowsShell)
  593. {
  594. this->WindowsShell = windowsShell;
  595. }
  596. bool cmState::UseWindowsShell() const
  597. {
  598. return this->WindowsShell;
  599. }
  600. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  601. {
  602. this->WindowsVSIDE = windowsVSIDE;
  603. }
  604. bool cmState::UseWindowsVSIDE() const
  605. {
  606. return this->WindowsVSIDE;
  607. }
  608. void cmState::SetGhsMultiIDE(bool ghsMultiIDE)
  609. {
  610. this->GhsMultiIDE = ghsMultiIDE;
  611. }
  612. bool cmState::UseGhsMultiIDE() const
  613. {
  614. return this->GhsMultiIDE;
  615. }
  616. void cmState::SetWatcomWMake(bool watcomWMake)
  617. {
  618. this->WatcomWMake = watcomWMake;
  619. }
  620. bool cmState::UseWatcomWMake() const
  621. {
  622. return this->WatcomWMake;
  623. }
  624. void cmState::SetMinGWMake(bool minGWMake)
  625. {
  626. this->MinGWMake = minGWMake;
  627. }
  628. bool cmState::UseMinGWMake() const
  629. {
  630. return this->MinGWMake;
  631. }
  632. void cmState::SetNMake(bool nMake)
  633. {
  634. this->NMake = nMake;
  635. }
  636. bool cmState::UseNMake() const
  637. {
  638. return this->NMake;
  639. }
  640. void cmState::SetMSYSShell(bool mSYSShell)
  641. {
  642. this->MSYSShell = mSYSShell;
  643. }
  644. bool cmState::UseMSYSShell() const
  645. {
  646. return this->MSYSShell;
  647. }
  648. void cmState::SetNinjaMulti(bool ninjaMulti)
  649. {
  650. this->NinjaMulti = ninjaMulti;
  651. }
  652. bool cmState::UseNinjaMulti() const
  653. {
  654. return this->NinjaMulti;
  655. }
  656. unsigned int cmState::GetCacheMajorVersion() const
  657. {
  658. return this->CacheManager->GetCacheMajorVersion();
  659. }
  660. unsigned int cmState::GetCacheMinorVersion() const
  661. {
  662. return this->CacheManager->GetCacheMinorVersion();
  663. }
  664. cmState::Mode cmState::GetMode() const
  665. {
  666. return this->CurrentMode;
  667. }
  668. std::string cmState::GetModeString() const
  669. {
  670. return ModeToString(this->CurrentMode);
  671. }
  672. void cmState::SetMode(cmState::Mode mode)
  673. {
  674. this->CurrentMode = mode;
  675. }
  676. std::string cmState::ModeToString(cmState::Mode mode)
  677. {
  678. switch (mode) {
  679. case Project:
  680. return "PROJECT";
  681. case Script:
  682. return "SCRIPT";
  683. case FindPackage:
  684. return "FIND_PACKAGE";
  685. case CTest:
  686. return "CTEST";
  687. case CPack:
  688. return "CPACK";
  689. case Unknown:
  690. return "UNKNOWN";
  691. }
  692. return "UNKNOWN";
  693. }
  694. std::string const& cmState::GetBinaryDirectory() const
  695. {
  696. return this->BinaryDirectory;
  697. }
  698. cmStateSnapshot cmState::CreateBaseSnapshot()
  699. {
  700. cmStateDetail::PositionType pos =
  701. this->SnapshotData.Push(this->SnapshotData.Root());
  702. pos->DirectoryParent = this->SnapshotData.Root();
  703. pos->ScopeParent = this->SnapshotData.Root();
  704. pos->SnapshotType = cmStateEnums::BaseType;
  705. pos->Keep = true;
  706. pos->BuildSystemDirectory =
  707. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  708. pos->ExecutionListFile =
  709. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  710. pos->IncludeDirectoryPosition = 0;
  711. pos->CompileDefinitionsPosition = 0;
  712. pos->CompileOptionsPosition = 0;
  713. pos->LinkOptionsPosition = 0;
  714. pos->LinkDirectoriesPosition = 0;
  715. pos->BuildSystemDirectory->DirectoryEnd = pos;
  716. pos->Policies = this->PolicyStack.Root();
  717. pos->PolicyRoot = this->PolicyStack.Root();
  718. pos->PolicyScope = this->PolicyStack.Root();
  719. assert(pos->Policies.IsValid());
  720. assert(pos->PolicyRoot.IsValid());
  721. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  722. assert(pos->Vars.IsValid());
  723. pos->Parent = this->VarTree.Root();
  724. pos->Root = this->VarTree.Root();
  725. return { this, pos };
  726. }
  727. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  728. cmStateSnapshot const& originSnapshot)
  729. {
  730. assert(originSnapshot.IsValid());
  731. cmStateDetail::PositionType pos =
  732. this->SnapshotData.Push(originSnapshot.Position);
  733. pos->DirectoryParent = originSnapshot.Position;
  734. pos->ScopeParent = originSnapshot.Position;
  735. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  736. pos->Keep = true;
  737. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  738. originSnapshot.Position->BuildSystemDirectory);
  739. pos->ExecutionListFile =
  740. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  741. pos->BuildSystemDirectory->DirectoryEnd = pos;
  742. pos->Policies = originSnapshot.Position->Policies;
  743. pos->PolicyRoot = originSnapshot.Position->Policies;
  744. pos->PolicyScope = originSnapshot.Position->Policies;
  745. assert(pos->Policies.IsValid());
  746. assert(pos->PolicyRoot.IsValid());
  747. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  748. pos->Parent = origin;
  749. pos->Root = origin;
  750. pos->Vars = this->VarTree.Push(origin);
  751. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  752. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  753. snapshot.SetDefaultDefinitions();
  754. snapshot.InitializeFromParent();
  755. snapshot.SetDirectoryDefinitions();
  756. return snapshot;
  757. }
  758. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  759. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  760. {
  761. cmStateDetail::PositionType pos =
  762. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  763. pos->ScopeParent = originSnapshot.Position;
  764. pos->SnapshotType = cmStateEnums::FunctionCallType;
  765. pos->Keep = false;
  766. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  767. originSnapshot.Position->ExecutionListFile, fileName);
  768. pos->BuildSystemDirectory->DirectoryEnd = pos;
  769. pos->PolicyScope = originSnapshot.Position->Policies;
  770. assert(originSnapshot.Position->Vars.IsValid());
  771. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  772. pos->Parent = origin;
  773. pos->Vars = this->VarTree.Push(origin);
  774. return { this, pos };
  775. }
  776. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  777. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  778. {
  779. cmStateDetail::PositionType pos =
  780. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  781. pos->SnapshotType = cmStateEnums::MacroCallType;
  782. pos->Keep = false;
  783. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  784. originSnapshot.Position->ExecutionListFile, fileName);
  785. assert(originSnapshot.Position->Vars.IsValid());
  786. pos->BuildSystemDirectory->DirectoryEnd = pos;
  787. pos->PolicyScope = originSnapshot.Position->Policies;
  788. return { this, pos };
  789. }
  790. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  791. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  792. {
  793. cmStateDetail::PositionType pos =
  794. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  795. pos->SnapshotType = cmStateEnums::IncludeFileType;
  796. pos->Keep = true;
  797. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  798. originSnapshot.Position->ExecutionListFile, fileName);
  799. assert(originSnapshot.Position->Vars.IsValid());
  800. pos->BuildSystemDirectory->DirectoryEnd = pos;
  801. pos->PolicyScope = originSnapshot.Position->Policies;
  802. return { this, pos };
  803. }
  804. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  805. cmStateSnapshot const& originSnapshot)
  806. {
  807. cmStateDetail::PositionType pos =
  808. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  809. pos->ScopeParent = originSnapshot.Position;
  810. pos->SnapshotType = cmStateEnums::VariableScopeType;
  811. pos->Keep = false;
  812. pos->PolicyScope = originSnapshot.Position->Policies;
  813. assert(originSnapshot.Position->Vars.IsValid());
  814. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  815. pos->Parent = origin;
  816. pos->Vars = this->VarTree.Push(origin);
  817. assert(pos->Vars.IsValid());
  818. return { this, pos };
  819. }
  820. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  821. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  822. {
  823. cmStateDetail::PositionType pos =
  824. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  825. pos->SnapshotType = cmStateEnums::InlineListFileType;
  826. pos->Keep = true;
  827. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  828. originSnapshot.Position->ExecutionListFile, fileName);
  829. pos->BuildSystemDirectory->DirectoryEnd = pos;
  830. pos->PolicyScope = originSnapshot.Position->Policies;
  831. return { this, pos };
  832. }
  833. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  834. cmStateSnapshot const& originSnapshot)
  835. {
  836. cmStateDetail::PositionType pos =
  837. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  838. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  839. pos->Keep = false;
  840. pos->BuildSystemDirectory->DirectoryEnd = pos;
  841. pos->PolicyScope = originSnapshot.Position->Policies;
  842. return { this, pos };
  843. }
  844. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  845. {
  846. cmStateDetail::PositionType pos = originSnapshot.Position;
  847. cmStateDetail::PositionType prevPos = pos;
  848. ++prevPos;
  849. prevPos->IncludeDirectoryPosition =
  850. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  851. prevPos->CompileDefinitionsPosition =
  852. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  853. prevPos->CompileOptionsPosition =
  854. prevPos->BuildSystemDirectory->CompileOptions.size();
  855. prevPos->LinkOptionsPosition =
  856. prevPos->BuildSystemDirectory->LinkOptions.size();
  857. prevPos->LinkDirectoriesPosition =
  858. prevPos->BuildSystemDirectory->LinkDirectories.size();
  859. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  860. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  861. if (pos->Vars != prevPos->Vars) {
  862. assert(this->VarTree.IsLast(pos->Vars));
  863. this->VarTree.Pop(pos->Vars);
  864. }
  865. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  866. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  867. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  868. }
  869. this->SnapshotData.Pop(pos);
  870. }
  871. return { this, prevPos };
  872. }
  873. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  874. std::string& value)
  875. {
  876. // input line is: key=value
  877. static cmsys::RegularExpression reg(
  878. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  879. // input line is: "key"=value
  880. static cmsys::RegularExpression regQuoted(
  881. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  882. bool flag = false;
  883. if (regQuoted.find(entry)) {
  884. var = regQuoted.match(1);
  885. value = regQuoted.match(2);
  886. flag = true;
  887. } else if (reg.find(entry)) {
  888. var = reg.match(1);
  889. value = reg.match(2);
  890. flag = true;
  891. }
  892. // if value is enclosed in single quotes ('foo') then remove them
  893. // it is used to enclose trailing space or tab
  894. if (flag && value.size() >= 2 && value.front() == '\'' &&
  895. value.back() == '\'') {
  896. value = value.substr(1, value.size() - 2);
  897. }
  898. return flag;
  899. }
  900. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  901. std::string& value,
  902. cmStateEnums::CacheEntryType& type)
  903. {
  904. // input line is: key:type=value
  905. static cmsys::RegularExpression reg(
  906. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  907. // input line is: "key":type=value
  908. static cmsys::RegularExpression regQuoted(
  909. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  910. bool flag = false;
  911. if (regQuoted.find(entry)) {
  912. var = regQuoted.match(1);
  913. type = cmState::StringToCacheEntryType(regQuoted.match(2));
  914. value = regQuoted.match(3);
  915. flag = true;
  916. } else if (reg.find(entry)) {
  917. var = reg.match(1);
  918. type = cmState::StringToCacheEntryType(reg.match(2));
  919. value = reg.match(3);
  920. flag = true;
  921. }
  922. // if value is enclosed in single quotes ('foo') then remove them
  923. // it is used to enclose trailing space or tab
  924. if (flag && value.size() >= 2 && value.front() == '\'' &&
  925. value.back() == '\'') {
  926. value = value.substr(1, value.size() - 2);
  927. }
  928. if (!flag) {
  929. return ParseEntryWithoutType(entry, var, value);
  930. }
  931. return flag;
  932. }