cmState.cxx 34 KB

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