cmState.cxx 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmState.h"
  4. #include <algorithm>
  5. #include <array>
  6. #include <cassert>
  7. #include <cstdlib>
  8. #include <utility>
  9. #include <cm/memory>
  10. #include "cmsys/RegularExpression.hxx"
  11. #include "cmCacheManager.h"
  12. #include "cmCommand.h"
  13. #include "cmDefinitions.h"
  14. #include "cmExecutionStatus.h"
  15. #include "cmGlobVerificationManager.h"
  16. #include "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(
  208. bool recurse, bool listDirectories, bool followSymlinks,
  209. const std::string& relative, const std::string& expression,
  210. const std::vector<std::string>& files, const std::string& variable,
  211. cmListFileBacktrace const& backtrace, cmMessenger* messenger)
  212. {
  213. this->GlobVerificationManager->AddCacheEntry(
  214. recurse, listDirectories, followSymlinks, relative, expression, files,
  215. variable, backtrace, messenger);
  216. }
  217. void cmState::RemoveCacheEntry(std::string const& key)
  218. {
  219. this->CacheManager->RemoveCacheEntry(key);
  220. }
  221. void cmState::AppendCacheEntryProperty(const std::string& key,
  222. const std::string& property,
  223. const std::string& value, bool asString)
  224. {
  225. this->CacheManager->AppendCacheEntryProperty(key, property, value, asString);
  226. }
  227. void cmState::RemoveCacheEntryProperty(std::string const& key,
  228. std::string const& propertyName)
  229. {
  230. this->CacheManager->RemoveCacheEntryProperty(key, propertyName);
  231. }
  232. cmStateSnapshot cmState::Reset()
  233. {
  234. this->GlobalProperties.Clear();
  235. this->PropertyDefinitions = {};
  236. this->GlobVerificationManager->Reset();
  237. cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
  238. this->ExecutionListFiles.Truncate();
  239. {
  240. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator it =
  241. this->BuildsystemDirectory.Truncate();
  242. it->IncludeDirectories.clear();
  243. it->CompileDefinitions.clear();
  244. it->CompileOptions.clear();
  245. it->LinkOptions.clear();
  246. it->LinkDirectories.clear();
  247. it->CurrentScope = pos;
  248. it->NormalTargetNames.clear();
  249. it->ImportedTargetNames.clear();
  250. it->Properties.Clear();
  251. it->Children.clear();
  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(const std::string& name,
  284. cmProperty::ScopeType scope,
  285. const std::string& ShortDescription,
  286. const std::string& FullDescription, bool chained,
  287. const std::string& initializeFromVariable)
  288. {
  289. this->PropertyDefinitions.DefineProperty(name, scope, ShortDescription,
  290. FullDescription, chained,
  291. initializeFromVariable);
  292. }
  293. cmPropertyDefinition const* cmState::GetPropertyDefinition(
  294. const std::string& name, cmProperty::ScopeType scope) const
  295. {
  296. return this->PropertyDefinitions.GetPropertyDefinition(name, scope);
  297. }
  298. bool cmState::IsPropertyChained(const std::string& name,
  299. cmProperty::ScopeType scope) const
  300. {
  301. if (const auto* 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,
  340. std::unique_ptr<cmCommand> command)
  341. {
  342. this->AddBuiltinCommand(name, cmLegacyCommandWrapper(std::move(command)));
  343. }
  344. void cmState::AddBuiltinCommand(std::string const& name, Command command)
  345. {
  346. assert(name == cmSystemTools::LowerCase(name));
  347. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  348. this->BuiltinCommands.emplace(name, std::move(command));
  349. }
  350. static bool InvokeBuiltinCommand(cmState::BuiltinCommand command,
  351. std::vector<cmListFileArgument> const& args,
  352. cmExecutionStatus& status)
  353. {
  354. cmMakefile& mf = status.GetMakefile();
  355. std::vector<std::string> expandedArguments;
  356. if (!mf.ExpandArguments(args, expandedArguments)) {
  357. // There was an error expanding arguments. It was already
  358. // reported, so we can skip this command without error.
  359. return true;
  360. }
  361. return command(expandedArguments, status);
  362. }
  363. void cmState::AddBuiltinCommand(std::string const& name,
  364. BuiltinCommand command)
  365. {
  366. this->AddBuiltinCommand(
  367. name,
  368. [command](const std::vector<cmListFileArgument>& args,
  369. cmExecutionStatus& status) -> bool {
  370. return InvokeBuiltinCommand(command, args, status);
  371. });
  372. }
  373. void cmState::AddFlowControlCommand(std::string const& name, Command command)
  374. {
  375. this->FlowControlCommands.insert(name);
  376. this->AddBuiltinCommand(name, std::move(command));
  377. }
  378. void cmState::AddFlowControlCommand(std::string const& name,
  379. BuiltinCommand command)
  380. {
  381. this->FlowControlCommands.insert(name);
  382. this->AddBuiltinCommand(name, command);
  383. }
  384. void cmState::AddDisallowedCommand(std::string const& name,
  385. BuiltinCommand command,
  386. cmPolicies::PolicyID policy,
  387. const char* message,
  388. const char* additionalWarning)
  389. {
  390. this->AddBuiltinCommand(
  391. name,
  392. [command, policy, message,
  393. additionalWarning](const std::vector<cmListFileArgument>& args,
  394. cmExecutionStatus& status) -> bool {
  395. cmMakefile& mf = status.GetMakefile();
  396. switch (mf.GetPolicyStatus(policy)) {
  397. case cmPolicies::WARN: {
  398. std::string warning = cmPolicies::GetPolicyWarning(policy);
  399. if (additionalWarning) {
  400. warning = cmStrCat(warning, '\n', additionalWarning);
  401. }
  402. mf.IssueMessage(MessageType::AUTHOR_WARNING, warning);
  403. }
  404. CM_FALLTHROUGH;
  405. case cmPolicies::OLD:
  406. break;
  407. case cmPolicies::REQUIRED_IF_USED:
  408. case cmPolicies::REQUIRED_ALWAYS:
  409. case cmPolicies::NEW:
  410. mf.IssueMessage(MessageType::FATAL_ERROR, message);
  411. return true;
  412. }
  413. return InvokeBuiltinCommand(command, args, status);
  414. });
  415. }
  416. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  417. {
  418. this->AddBuiltinCommand(
  419. name,
  420. [name, error](std::vector<cmListFileArgument> const&,
  421. cmExecutionStatus& status) -> bool {
  422. cmValue versionValue =
  423. status.GetMakefile().GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  424. if (name == "endif" &&
  425. (!versionValue || atof(versionValue->c_str()) <= 1.4)) {
  426. return true;
  427. }
  428. status.SetError(error);
  429. return false;
  430. });
  431. }
  432. void cmState::AddUnexpectedFlowControlCommand(std::string const& name,
  433. const char* error)
  434. {
  435. this->FlowControlCommands.insert(name);
  436. this->AddUnexpectedCommand(name, error);
  437. }
  438. bool cmState::AddScriptedCommand(std::string const& name, BT<Command> command,
  439. cmMakefile& mf)
  440. {
  441. std::string sName = cmSystemTools::LowerCase(name);
  442. if (this->FlowControlCommands.count(sName)) {
  443. mf.GetCMakeInstance()->IssueMessage(
  444. MessageType::FATAL_ERROR,
  445. cmStrCat("Built-in flow control command \"", sName,
  446. "\" cannot be overridden."),
  447. command.Backtrace);
  448. cmSystemTools::SetFatalErrorOccurred();
  449. return false;
  450. }
  451. // if the command already exists, give a new name to the old command.
  452. if (Command oldCmd = this->GetCommandByExactName(sName)) {
  453. this->ScriptedCommands["_" + sName] = oldCmd;
  454. }
  455. this->ScriptedCommands[sName] = std::move(command.Value);
  456. return true;
  457. }
  458. cmState::Command cmState::GetCommand(std::string const& name) const
  459. {
  460. return this->GetCommandByExactName(cmSystemTools::LowerCase(name));
  461. }
  462. cmState::Command cmState::GetCommandByExactName(std::string const& name) const
  463. {
  464. auto pos = this->ScriptedCommands.find(name);
  465. if (pos != this->ScriptedCommands.end()) {
  466. return pos->second;
  467. }
  468. pos = this->BuiltinCommands.find(name);
  469. if (pos != this->BuiltinCommands.end()) {
  470. return pos->second;
  471. }
  472. return nullptr;
  473. }
  474. std::vector<std::string> cmState::GetCommandNames() const
  475. {
  476. std::vector<std::string> commandNames;
  477. commandNames.reserve(this->BuiltinCommands.size() +
  478. this->ScriptedCommands.size());
  479. for (auto const& bc : this->BuiltinCommands) {
  480. commandNames.push_back(bc.first);
  481. }
  482. for (auto const& sc : this->ScriptedCommands) {
  483. commandNames.push_back(sc.first);
  484. }
  485. std::sort(commandNames.begin(), commandNames.end());
  486. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  487. commandNames.end());
  488. return commandNames;
  489. }
  490. void cmState::RemoveBuiltinCommand(std::string const& name)
  491. {
  492. assert(name == cmSystemTools::LowerCase(name));
  493. this->BuiltinCommands.erase(name);
  494. }
  495. void cmState::RemoveUserDefinedCommands()
  496. {
  497. this->ScriptedCommands.clear();
  498. }
  499. void cmState::SetGlobalProperty(const std::string& prop,
  500. const std::string& value)
  501. {
  502. this->GlobalProperties.SetProperty(prop, value);
  503. }
  504. void cmState::SetGlobalProperty(const std::string& prop, cmValue value)
  505. {
  506. this->GlobalProperties.SetProperty(prop, value);
  507. }
  508. void cmState::AppendGlobalProperty(const std::string& prop,
  509. const std::string& value, bool asString)
  510. {
  511. this->GlobalProperties.AppendProperty(prop, value, asString);
  512. }
  513. cmValue cmState::GetGlobalProperty(const std::string& prop)
  514. {
  515. if (prop == "CACHE_VARIABLES") {
  516. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  517. this->SetGlobalProperty("CACHE_VARIABLES", cmList::to_string(cacheKeys));
  518. } else if (prop == "COMMANDS") {
  519. std::vector<std::string> commands = this->GetCommandNames();
  520. this->SetGlobalProperty("COMMANDS", cmList::to_string(commands));
  521. } else if (prop == "IN_TRY_COMPILE") {
  522. this->SetGlobalProperty(
  523. "IN_TRY_COMPILE",
  524. this->StateProjectKind == ProjectKind::TryCompile ? "1" : "0");
  525. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  526. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  527. this->IsGeneratorMultiConfig ? "1" : "0");
  528. } else if (prop == "ENABLED_LANGUAGES") {
  529. auto langs = cmList::to_string(this->EnabledLanguages);
  530. this->SetGlobalProperty("ENABLED_LANGUAGES", langs);
  531. } else if (prop == "CMAKE_ROLE") {
  532. std::string mode = this->GetModeString();
  533. this->SetGlobalProperty("CMAKE_ROLE", mode);
  534. }
  535. #define STRING_LIST_ELEMENT(F) ";" #F
  536. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  537. static const std::string s_out(
  538. &FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT)[1]);
  539. return cmValue(s_out);
  540. }
  541. if (prop == "CMAKE_C90_KNOWN_FEATURES") {
  542. static const std::string s_out(
  543. &FOR_EACH_C90_FEATURE(STRING_LIST_ELEMENT)[1]);
  544. return cmValue(s_out);
  545. }
  546. if (prop == "CMAKE_C99_KNOWN_FEATURES") {
  547. static const std::string s_out(
  548. &FOR_EACH_C99_FEATURE(STRING_LIST_ELEMENT)[1]);
  549. return cmValue(s_out);
  550. }
  551. if (prop == "CMAKE_C11_KNOWN_FEATURES") {
  552. static const std::string s_out(
  553. &FOR_EACH_C11_FEATURE(STRING_LIST_ELEMENT)[1]);
  554. return cmValue(s_out);
  555. }
  556. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  557. static const std::string s_out(
  558. &FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT)[1]);
  559. return cmValue(s_out);
  560. }
  561. if (prop == "CMAKE_CXX98_KNOWN_FEATURES") {
  562. static const std::string s_out(
  563. &FOR_EACH_CXX98_FEATURE(STRING_LIST_ELEMENT)[1]);
  564. return cmValue(s_out);
  565. }
  566. if (prop == "CMAKE_CXX11_KNOWN_FEATURES") {
  567. static const std::string s_out(
  568. &FOR_EACH_CXX11_FEATURE(STRING_LIST_ELEMENT)[1]);
  569. return cmValue(s_out);
  570. }
  571. if (prop == "CMAKE_CXX14_KNOWN_FEATURES") {
  572. static const std::string s_out(
  573. &FOR_EACH_CXX14_FEATURE(STRING_LIST_ELEMENT)[1]);
  574. return cmValue(s_out);
  575. }
  576. if (prop == "CMAKE_CUDA_KNOWN_FEATURES") {
  577. static const std::string s_out(
  578. &FOR_EACH_CUDA_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::SetWatcomWMake(bool watcomWMake)
  627. {
  628. this->WatcomWMake = watcomWMake;
  629. }
  630. bool cmState::UseWatcomWMake() const
  631. {
  632. return this->WatcomWMake;
  633. }
  634. void cmState::SetMinGWMake(bool minGWMake)
  635. {
  636. this->MinGWMake = minGWMake;
  637. }
  638. bool cmState::UseMinGWMake() const
  639. {
  640. return this->MinGWMake;
  641. }
  642. void cmState::SetNMake(bool nMake)
  643. {
  644. this->NMake = nMake;
  645. }
  646. bool cmState::UseNMake() const
  647. {
  648. return this->NMake;
  649. }
  650. void cmState::SetMSYSShell(bool mSYSShell)
  651. {
  652. this->MSYSShell = mSYSShell;
  653. }
  654. bool cmState::UseMSYSShell() const
  655. {
  656. return this->MSYSShell;
  657. }
  658. void cmState::SetNinja(bool ninja)
  659. {
  660. this->Ninja = ninja;
  661. }
  662. bool cmState::UseNinja() const
  663. {
  664. return this->Ninja;
  665. }
  666. void cmState::SetNinjaMulti(bool ninjaMulti)
  667. {
  668. this->NinjaMulti = ninjaMulti;
  669. }
  670. bool cmState::UseNinjaMulti() const
  671. {
  672. return this->NinjaMulti;
  673. }
  674. unsigned int cmState::GetCacheMajorVersion() const
  675. {
  676. return this->CacheManager->GetCacheMajorVersion();
  677. }
  678. unsigned int cmState::GetCacheMinorVersion() const
  679. {
  680. return this->CacheManager->GetCacheMinorVersion();
  681. }
  682. cmState::Mode cmState::GetMode() const
  683. {
  684. return this->StateMode;
  685. }
  686. std::string cmState::GetModeString() const
  687. {
  688. return ModeToString(this->StateMode);
  689. }
  690. std::string cmState::ModeToString(cmState::Mode mode)
  691. {
  692. switch (mode) {
  693. case Project:
  694. return "PROJECT";
  695. case Script:
  696. return "SCRIPT";
  697. case FindPackage:
  698. return "FIND_PACKAGE";
  699. case CTest:
  700. return "CTEST";
  701. case CPack:
  702. return "CPACK";
  703. case Help:
  704. return "HELP";
  705. case Unknown:
  706. return "UNKNOWN";
  707. }
  708. return "UNKNOWN";
  709. }
  710. cmState::ProjectKind cmState::GetProjectKind() const
  711. {
  712. return this->StateProjectKind;
  713. }
  714. std::string const& cmState::GetBinaryDirectory() const
  715. {
  716. return this->BinaryDirectory;
  717. }
  718. cmStateSnapshot cmState::CreateBaseSnapshot()
  719. {
  720. cmStateDetail::PositionType pos =
  721. this->SnapshotData.Push(this->SnapshotData.Root());
  722. pos->DirectoryParent = this->SnapshotData.Root();
  723. pos->ScopeParent = this->SnapshotData.Root();
  724. pos->SnapshotType = cmStateEnums::BaseType;
  725. pos->Keep = true;
  726. pos->BuildSystemDirectory =
  727. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  728. pos->ExecutionListFile =
  729. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  730. pos->IncludeDirectoryPosition = 0;
  731. pos->CompileDefinitionsPosition = 0;
  732. pos->CompileOptionsPosition = 0;
  733. pos->LinkOptionsPosition = 0;
  734. pos->LinkDirectoriesPosition = 0;
  735. pos->BuildSystemDirectory->CurrentScope = pos;
  736. pos->Policies = this->PolicyStack.Root();
  737. pos->PolicyRoot = this->PolicyStack.Root();
  738. pos->PolicyScope = this->PolicyStack.Root();
  739. assert(pos->Policies.IsValid());
  740. assert(pos->PolicyRoot.IsValid());
  741. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  742. assert(pos->Vars.IsValid());
  743. pos->Parent = this->VarTree.Root();
  744. pos->Root = this->VarTree.Root();
  745. return { this, pos };
  746. }
  747. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  748. cmStateSnapshot const& originSnapshot)
  749. {
  750. assert(originSnapshot.IsValid());
  751. cmStateDetail::PositionType pos =
  752. this->SnapshotData.Push(originSnapshot.Position);
  753. pos->DirectoryParent = originSnapshot.Position;
  754. pos->ScopeParent = originSnapshot.Position;
  755. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  756. pos->Keep = true;
  757. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  758. originSnapshot.Position->BuildSystemDirectory);
  759. pos->ExecutionListFile =
  760. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  761. pos->BuildSystemDirectory->CurrentScope = pos;
  762. pos->Policies = originSnapshot.Position->Policies;
  763. pos->PolicyRoot = originSnapshot.Position->Policies;
  764. pos->PolicyScope = originSnapshot.Position->Policies;
  765. assert(pos->Policies.IsValid());
  766. assert(pos->PolicyRoot.IsValid());
  767. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  768. pos->Parent = origin;
  769. pos->Root = origin;
  770. pos->Vars = this->VarTree.Push(origin);
  771. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  772. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  773. snapshot.SetDefaultDefinitions();
  774. snapshot.InitializeFromParent();
  775. snapshot.SetDirectoryDefinitions();
  776. return snapshot;
  777. }
  778. cmStateSnapshot cmState::CreateDeferCallSnapshot(
  779. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  780. {
  781. cmStateDetail::PositionType pos =
  782. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  783. pos->SnapshotType = cmStateEnums::DeferCallType;
  784. pos->Keep = false;
  785. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  786. originSnapshot.Position->ExecutionListFile, fileName);
  787. assert(originSnapshot.Position->Vars.IsValid());
  788. pos->BuildSystemDirectory->CurrentScope = pos;
  789. pos->PolicyScope = originSnapshot.Position->Policies;
  790. return { this, pos };
  791. }
  792. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  793. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  794. {
  795. cmStateDetail::PositionType pos =
  796. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  797. pos->ScopeParent = originSnapshot.Position;
  798. pos->SnapshotType = cmStateEnums::FunctionCallType;
  799. pos->Keep = false;
  800. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  801. originSnapshot.Position->ExecutionListFile, fileName);
  802. pos->BuildSystemDirectory->CurrentScope = pos;
  803. pos->PolicyScope = originSnapshot.Position->Policies;
  804. assert(originSnapshot.Position->Vars.IsValid());
  805. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  806. pos->Parent = origin;
  807. pos->Vars = this->VarTree.Push(origin);
  808. return { this, pos };
  809. }
  810. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  811. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  812. {
  813. cmStateDetail::PositionType pos =
  814. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  815. pos->SnapshotType = cmStateEnums::MacroCallType;
  816. pos->Keep = false;
  817. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  818. originSnapshot.Position->ExecutionListFile, fileName);
  819. assert(originSnapshot.Position->Vars.IsValid());
  820. pos->BuildSystemDirectory->CurrentScope = pos;
  821. pos->PolicyScope = originSnapshot.Position->Policies;
  822. return { this, pos };
  823. }
  824. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  825. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  826. {
  827. cmStateDetail::PositionType pos =
  828. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  829. pos->SnapshotType = cmStateEnums::IncludeFileType;
  830. pos->Keep = true;
  831. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  832. originSnapshot.Position->ExecutionListFile, fileName);
  833. assert(originSnapshot.Position->Vars.IsValid());
  834. pos->BuildSystemDirectory->CurrentScope = pos;
  835. pos->PolicyScope = originSnapshot.Position->Policies;
  836. return { this, pos };
  837. }
  838. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  839. cmStateSnapshot const& originSnapshot)
  840. {
  841. cmStateDetail::PositionType pos =
  842. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  843. pos->ScopeParent = originSnapshot.Position;
  844. pos->SnapshotType = cmStateEnums::VariableScopeType;
  845. pos->Keep = false;
  846. pos->BuildSystemDirectory->CurrentScope = pos;
  847. pos->PolicyScope = originSnapshot.Position->Policies;
  848. assert(originSnapshot.Position->Vars.IsValid());
  849. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  850. pos->Parent = origin;
  851. pos->Vars = this->VarTree.Push(origin);
  852. assert(pos->Vars.IsValid());
  853. return { this, pos };
  854. }
  855. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  856. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  857. {
  858. cmStateDetail::PositionType pos =
  859. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  860. pos->SnapshotType = cmStateEnums::InlineListFileType;
  861. pos->Keep = true;
  862. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  863. originSnapshot.Position->ExecutionListFile, fileName);
  864. pos->BuildSystemDirectory->CurrentScope = pos;
  865. pos->PolicyScope = originSnapshot.Position->Policies;
  866. return { this, pos };
  867. }
  868. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  869. cmStateSnapshot const& originSnapshot)
  870. {
  871. cmStateDetail::PositionType pos =
  872. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  873. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  874. pos->Keep = false;
  875. pos->BuildSystemDirectory->CurrentScope = pos;
  876. pos->PolicyScope = originSnapshot.Position->Policies;
  877. return { this, pos };
  878. }
  879. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  880. {
  881. cmStateDetail::PositionType pos = originSnapshot.Position;
  882. cmStateDetail::PositionType prevPos = pos;
  883. ++prevPos;
  884. prevPos->IncludeDirectoryPosition =
  885. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  886. prevPos->CompileDefinitionsPosition =
  887. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  888. prevPos->CompileOptionsPosition =
  889. prevPos->BuildSystemDirectory->CompileOptions.size();
  890. prevPos->LinkOptionsPosition =
  891. prevPos->BuildSystemDirectory->LinkOptions.size();
  892. prevPos->LinkDirectoriesPosition =
  893. prevPos->BuildSystemDirectory->LinkDirectories.size();
  894. prevPos->BuildSystemDirectory->CurrentScope = prevPos;
  895. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  896. if (pos->Vars != prevPos->Vars) {
  897. assert(this->VarTree.IsLast(pos->Vars));
  898. this->VarTree.Pop(pos->Vars);
  899. }
  900. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  901. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  902. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  903. }
  904. this->SnapshotData.Pop(pos);
  905. }
  906. return { this, prevPos };
  907. }
  908. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  909. std::string& value)
  910. {
  911. // input line is: key=value
  912. static cmsys::RegularExpression reg(
  913. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  914. // input line is: "key"=value
  915. static cmsys::RegularExpression regQuoted(
  916. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  917. bool flag = false;
  918. if (regQuoted.find(entry)) {
  919. var = regQuoted.match(1);
  920. value = regQuoted.match(2);
  921. flag = true;
  922. } else if (reg.find(entry)) {
  923. var = reg.match(1);
  924. value = reg.match(2);
  925. flag = true;
  926. }
  927. // if value is enclosed in single quotes ('foo') then remove them
  928. // it is used to enclose trailing space or tab
  929. if (flag && value.size() >= 2 && value.front() == '\'' &&
  930. value.back() == '\'') {
  931. value = value.substr(1, value.size() - 2);
  932. }
  933. return flag;
  934. }
  935. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  936. std::string& value,
  937. cmStateEnums::CacheEntryType& type)
  938. {
  939. // input line is: key:type=value
  940. static cmsys::RegularExpression reg(
  941. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  942. // input line is: "key":type=value
  943. static cmsys::RegularExpression regQuoted(
  944. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  945. bool flag = false;
  946. if (regQuoted.find(entry)) {
  947. var = regQuoted.match(1);
  948. type = cmState::StringToCacheEntryType(regQuoted.match(2));
  949. value = regQuoted.match(3);
  950. flag = true;
  951. } else if (reg.find(entry)) {
  952. var = reg.match(1);
  953. type = cmState::StringToCacheEntryType(reg.match(2));
  954. value = reg.match(3);
  955. flag = true;
  956. }
  957. // if value is enclosed in single quotes ('foo') then remove them
  958. // it is used to enclose trailing space or tab
  959. if (flag && value.size() >= 2 && value.front() == '\'' &&
  960. value.back() == '\'') {
  961. value = value.substr(1, value.size() - 2);
  962. }
  963. if (!flag) {
  964. return ParseEntryWithoutType(entry, var, value);
  965. }
  966. return flag;
  967. }
  968. cmState::Command cmState::GetDependencyProviderCommand(
  969. cmDependencyProvider::Method method) const
  970. {
  971. return (this->DependencyProvider &&
  972. this->DependencyProvider->SupportsMethod(method))
  973. ? this->GetCommand(this->DependencyProvider->GetCommand())
  974. : Command{};
  975. }