cmState.cxx 34 KB

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