cmState.cxx 33 KB

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