cmState.cxx 31 KB

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