cmState.cxx 31 KB

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