cmState.cxx 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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. #undef STRING_LIST_ELEMENT
  585. return this->GlobalProperties.GetPropertyValue(prop);
  586. }
  587. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  588. {
  589. return this->GetGlobalProperty(prop).IsOn();
  590. }
  591. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  592. {
  593. this->SourceDirectory = sourceDirectory;
  594. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  595. }
  596. std::string const& cmState::GetSourceDirectory() const
  597. {
  598. return this->SourceDirectory;
  599. }
  600. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  601. {
  602. this->BinaryDirectory = binaryDirectory;
  603. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  604. }
  605. void cmState::SetWindowsShell(bool windowsShell)
  606. {
  607. this->WindowsShell = windowsShell;
  608. }
  609. bool cmState::UseWindowsShell() const
  610. {
  611. return this->WindowsShell;
  612. }
  613. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  614. {
  615. this->WindowsVSIDE = windowsVSIDE;
  616. }
  617. bool cmState::UseWindowsVSIDE() const
  618. {
  619. return this->WindowsVSIDE;
  620. }
  621. void cmState::SetGhsMultiIDE(bool ghsMultiIDE)
  622. {
  623. this->GhsMultiIDE = ghsMultiIDE;
  624. }
  625. bool cmState::UseGhsMultiIDE() const
  626. {
  627. return this->GhsMultiIDE;
  628. }
  629. void cmState::SetWatcomWMake(bool watcomWMake)
  630. {
  631. this->WatcomWMake = watcomWMake;
  632. }
  633. bool cmState::UseWatcomWMake() const
  634. {
  635. return this->WatcomWMake;
  636. }
  637. void cmState::SetMinGWMake(bool minGWMake)
  638. {
  639. this->MinGWMake = minGWMake;
  640. }
  641. bool cmState::UseMinGWMake() const
  642. {
  643. return this->MinGWMake;
  644. }
  645. void cmState::SetNMake(bool nMake)
  646. {
  647. this->NMake = nMake;
  648. }
  649. bool cmState::UseNMake() const
  650. {
  651. return this->NMake;
  652. }
  653. void cmState::SetMSYSShell(bool mSYSShell)
  654. {
  655. this->MSYSShell = mSYSShell;
  656. }
  657. bool cmState::UseMSYSShell() const
  658. {
  659. return this->MSYSShell;
  660. }
  661. void cmState::SetNinja(bool ninja)
  662. {
  663. this->Ninja = ninja;
  664. }
  665. bool cmState::UseNinja() const
  666. {
  667. return this->Ninja;
  668. }
  669. void cmState::SetNinjaMulti(bool ninjaMulti)
  670. {
  671. this->NinjaMulti = ninjaMulti;
  672. }
  673. bool cmState::UseNinjaMulti() const
  674. {
  675. return this->NinjaMulti;
  676. }
  677. unsigned int cmState::GetCacheMajorVersion() const
  678. {
  679. return this->CacheManager->GetCacheMajorVersion();
  680. }
  681. unsigned int cmState::GetCacheMinorVersion() const
  682. {
  683. return this->CacheManager->GetCacheMinorVersion();
  684. }
  685. cmState::Mode cmState::GetMode() const
  686. {
  687. return this->StateMode;
  688. }
  689. std::string cmState::GetModeString() const
  690. {
  691. return ModeToString(this->StateMode);
  692. }
  693. std::string cmState::ModeToString(cmState::Mode mode)
  694. {
  695. switch (mode) {
  696. case Project:
  697. return "PROJECT";
  698. case Script:
  699. return "SCRIPT";
  700. case FindPackage:
  701. return "FIND_PACKAGE";
  702. case CTest:
  703. return "CTEST";
  704. case CPack:
  705. return "CPACK";
  706. case Help:
  707. return "HELP";
  708. case Unknown:
  709. return "UNKNOWN";
  710. }
  711. return "UNKNOWN";
  712. }
  713. cmState::ProjectKind cmState::GetProjectKind() const
  714. {
  715. return this->StateProjectKind;
  716. }
  717. std::string const& cmState::GetBinaryDirectory() const
  718. {
  719. return this->BinaryDirectory;
  720. }
  721. cmStateSnapshot cmState::CreateBaseSnapshot()
  722. {
  723. cmStateDetail::PositionType pos =
  724. this->SnapshotData.Push(this->SnapshotData.Root());
  725. pos->DirectoryParent = this->SnapshotData.Root();
  726. pos->ScopeParent = this->SnapshotData.Root();
  727. pos->SnapshotType = cmStateEnums::BaseType;
  728. pos->Keep = true;
  729. pos->BuildSystemDirectory =
  730. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  731. pos->ExecutionListFile =
  732. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  733. pos->IncludeDirectoryPosition = 0;
  734. pos->CompileDefinitionsPosition = 0;
  735. pos->CompileOptionsPosition = 0;
  736. pos->LinkOptionsPosition = 0;
  737. pos->LinkDirectoriesPosition = 0;
  738. pos->BuildSystemDirectory->CurrentScope = pos;
  739. pos->Policies = this->PolicyStack.Root();
  740. pos->PolicyRoot = this->PolicyStack.Root();
  741. pos->PolicyScope = this->PolicyStack.Root();
  742. assert(pos->Policies.IsValid());
  743. assert(pos->PolicyRoot.IsValid());
  744. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  745. assert(pos->Vars.IsValid());
  746. pos->Parent = this->VarTree.Root();
  747. pos->Root = this->VarTree.Root();
  748. return { this, pos };
  749. }
  750. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  751. cmStateSnapshot const& originSnapshot)
  752. {
  753. assert(originSnapshot.IsValid());
  754. cmStateDetail::PositionType pos =
  755. this->SnapshotData.Push(originSnapshot.Position);
  756. pos->DirectoryParent = originSnapshot.Position;
  757. pos->ScopeParent = originSnapshot.Position;
  758. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  759. pos->Keep = true;
  760. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  761. originSnapshot.Position->BuildSystemDirectory);
  762. pos->ExecutionListFile =
  763. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  764. pos->BuildSystemDirectory->CurrentScope = pos;
  765. pos->Policies = originSnapshot.Position->Policies;
  766. pos->PolicyRoot = originSnapshot.Position->Policies;
  767. pos->PolicyScope = originSnapshot.Position->Policies;
  768. assert(pos->Policies.IsValid());
  769. assert(pos->PolicyRoot.IsValid());
  770. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  771. pos->Parent = origin;
  772. pos->Root = origin;
  773. pos->Vars = this->VarTree.Push(origin);
  774. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  775. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  776. snapshot.SetDefaultDefinitions();
  777. snapshot.InitializeFromParent();
  778. snapshot.SetDirectoryDefinitions();
  779. return snapshot;
  780. }
  781. cmStateSnapshot cmState::CreateDeferCallSnapshot(
  782. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  783. {
  784. cmStateDetail::PositionType pos =
  785. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  786. pos->SnapshotType = cmStateEnums::DeferCallType;
  787. pos->Keep = false;
  788. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  789. originSnapshot.Position->ExecutionListFile, fileName);
  790. assert(originSnapshot.Position->Vars.IsValid());
  791. pos->BuildSystemDirectory->CurrentScope = pos;
  792. pos->PolicyScope = originSnapshot.Position->Policies;
  793. return { this, pos };
  794. }
  795. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  796. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  797. {
  798. cmStateDetail::PositionType pos =
  799. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  800. pos->ScopeParent = originSnapshot.Position;
  801. pos->SnapshotType = cmStateEnums::FunctionCallType;
  802. pos->Keep = false;
  803. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  804. originSnapshot.Position->ExecutionListFile, fileName);
  805. pos->BuildSystemDirectory->CurrentScope = pos;
  806. pos->PolicyScope = originSnapshot.Position->Policies;
  807. assert(originSnapshot.Position->Vars.IsValid());
  808. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  809. pos->Parent = origin;
  810. pos->Vars = this->VarTree.Push(origin);
  811. return { this, pos };
  812. }
  813. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  814. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  815. {
  816. cmStateDetail::PositionType pos =
  817. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  818. pos->SnapshotType = cmStateEnums::MacroCallType;
  819. pos->Keep = false;
  820. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  821. originSnapshot.Position->ExecutionListFile, fileName);
  822. assert(originSnapshot.Position->Vars.IsValid());
  823. pos->BuildSystemDirectory->CurrentScope = pos;
  824. pos->PolicyScope = originSnapshot.Position->Policies;
  825. return { this, pos };
  826. }
  827. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  828. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  829. {
  830. cmStateDetail::PositionType pos =
  831. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  832. pos->SnapshotType = cmStateEnums::IncludeFileType;
  833. pos->Keep = true;
  834. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  835. originSnapshot.Position->ExecutionListFile, fileName);
  836. assert(originSnapshot.Position->Vars.IsValid());
  837. pos->BuildSystemDirectory->CurrentScope = pos;
  838. pos->PolicyScope = originSnapshot.Position->Policies;
  839. return { this, pos };
  840. }
  841. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  842. cmStateSnapshot const& originSnapshot)
  843. {
  844. cmStateDetail::PositionType pos =
  845. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  846. pos->ScopeParent = originSnapshot.Position;
  847. pos->SnapshotType = cmStateEnums::VariableScopeType;
  848. pos->Keep = false;
  849. pos->BuildSystemDirectory->CurrentScope = pos;
  850. pos->PolicyScope = originSnapshot.Position->Policies;
  851. assert(originSnapshot.Position->Vars.IsValid());
  852. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  853. pos->Parent = origin;
  854. pos->Vars = this->VarTree.Push(origin);
  855. assert(pos->Vars.IsValid());
  856. return { this, pos };
  857. }
  858. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  859. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  860. {
  861. cmStateDetail::PositionType pos =
  862. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  863. pos->SnapshotType = cmStateEnums::InlineListFileType;
  864. pos->Keep = true;
  865. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  866. originSnapshot.Position->ExecutionListFile, fileName);
  867. pos->BuildSystemDirectory->CurrentScope = pos;
  868. pos->PolicyScope = originSnapshot.Position->Policies;
  869. return { this, pos };
  870. }
  871. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  872. cmStateSnapshot const& originSnapshot)
  873. {
  874. cmStateDetail::PositionType pos =
  875. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  876. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  877. pos->Keep = false;
  878. pos->BuildSystemDirectory->CurrentScope = pos;
  879. pos->PolicyScope = originSnapshot.Position->Policies;
  880. return { this, pos };
  881. }
  882. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  883. {
  884. cmStateDetail::PositionType pos = originSnapshot.Position;
  885. cmStateDetail::PositionType prevPos = pos;
  886. ++prevPos;
  887. prevPos->IncludeDirectoryPosition =
  888. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  889. prevPos->CompileDefinitionsPosition =
  890. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  891. prevPos->CompileOptionsPosition =
  892. prevPos->BuildSystemDirectory->CompileOptions.size();
  893. prevPos->LinkOptionsPosition =
  894. prevPos->BuildSystemDirectory->LinkOptions.size();
  895. prevPos->LinkDirectoriesPosition =
  896. prevPos->BuildSystemDirectory->LinkDirectories.size();
  897. prevPos->BuildSystemDirectory->CurrentScope = prevPos;
  898. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  899. if (pos->Vars != prevPos->Vars) {
  900. assert(this->VarTree.IsLast(pos->Vars));
  901. this->VarTree.Pop(pos->Vars);
  902. }
  903. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  904. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  905. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  906. }
  907. this->SnapshotData.Pop(pos);
  908. }
  909. return { this, prevPos };
  910. }
  911. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  912. std::string& value)
  913. {
  914. // input line is: key=value
  915. static cmsys::RegularExpression reg(
  916. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  917. // input line is: "key"=value
  918. static cmsys::RegularExpression regQuoted(
  919. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  920. bool flag = false;
  921. if (regQuoted.find(entry)) {
  922. var = regQuoted.match(1);
  923. value = regQuoted.match(2);
  924. flag = true;
  925. } else if (reg.find(entry)) {
  926. var = reg.match(1);
  927. value = reg.match(2);
  928. flag = true;
  929. }
  930. // if value is enclosed in single quotes ('foo') then remove them
  931. // it is used to enclose trailing space or tab
  932. if (flag && value.size() >= 2 && value.front() == '\'' &&
  933. value.back() == '\'') {
  934. value = value.substr(1, value.size() - 2);
  935. }
  936. return flag;
  937. }
  938. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  939. std::string& value,
  940. cmStateEnums::CacheEntryType& type)
  941. {
  942. // input line is: key:type=value
  943. static cmsys::RegularExpression reg(
  944. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  945. // input line is: "key":type=value
  946. static cmsys::RegularExpression regQuoted(
  947. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  948. bool flag = false;
  949. if (regQuoted.find(entry)) {
  950. var = regQuoted.match(1);
  951. type = cmState::StringToCacheEntryType(regQuoted.match(2));
  952. value = regQuoted.match(3);
  953. flag = true;
  954. } else if (reg.find(entry)) {
  955. var = reg.match(1);
  956. type = cmState::StringToCacheEntryType(reg.match(2));
  957. value = reg.match(3);
  958. flag = true;
  959. }
  960. // if value is enclosed in single quotes ('foo') then remove them
  961. // it is used to enclose trailing space or tab
  962. if (flag && value.size() >= 2 && value.front() == '\'' &&
  963. value.back() == '\'') {
  964. value = value.substr(1, value.size() - 2);
  965. }
  966. if (!flag) {
  967. return ParseEntryWithoutType(entry, var, value);
  968. }
  969. return flag;
  970. }
  971. cmState::Command cmState::GetDependencyProviderCommand(
  972. cmDependencyProvider::Method method) const
  973. {
  974. return (this->DependencyProvider &&
  975. this->DependencyProvider->SupportsMethod(method))
  976. ? this->GetCommand(this->DependencyProvider->GetCommand())
  977. : Command{};
  978. }