cmState.cxx 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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 "cmsys/RegularExpression.hxx"
  5. #include <algorithm>
  6. #include <cassert>
  7. #include <cstdlib>
  8. #include <cstring>
  9. #include <utility>
  10. #include <cm/memory>
  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. #undef STRING_LIST_ELEMENT
  546. return this->GlobalProperties.GetPropertyValue(prop);
  547. }
  548. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  549. {
  550. return cmIsOn(this->GetGlobalProperty(prop));
  551. }
  552. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  553. {
  554. this->SourceDirectory = sourceDirectory;
  555. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  556. }
  557. std::string const& cmState::GetSourceDirectory() const
  558. {
  559. return this->SourceDirectory;
  560. }
  561. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  562. {
  563. this->BinaryDirectory = binaryDirectory;
  564. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  565. }
  566. void cmState::SetWindowsShell(bool windowsShell)
  567. {
  568. this->WindowsShell = windowsShell;
  569. }
  570. bool cmState::UseWindowsShell() const
  571. {
  572. return this->WindowsShell;
  573. }
  574. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  575. {
  576. this->WindowsVSIDE = windowsVSIDE;
  577. }
  578. bool cmState::UseWindowsVSIDE() const
  579. {
  580. return this->WindowsVSIDE;
  581. }
  582. void cmState::SetGhsMultiIDE(bool ghsMultiIDE)
  583. {
  584. this->GhsMultiIDE = ghsMultiIDE;
  585. }
  586. bool cmState::UseGhsMultiIDE() const
  587. {
  588. return this->GhsMultiIDE;
  589. }
  590. void cmState::SetWatcomWMake(bool watcomWMake)
  591. {
  592. this->WatcomWMake = watcomWMake;
  593. }
  594. bool cmState::UseWatcomWMake() const
  595. {
  596. return this->WatcomWMake;
  597. }
  598. void cmState::SetMinGWMake(bool minGWMake)
  599. {
  600. this->MinGWMake = minGWMake;
  601. }
  602. bool cmState::UseMinGWMake() const
  603. {
  604. return this->MinGWMake;
  605. }
  606. void cmState::SetNMake(bool nMake)
  607. {
  608. this->NMake = nMake;
  609. }
  610. bool cmState::UseNMake() const
  611. {
  612. return this->NMake;
  613. }
  614. void cmState::SetMSYSShell(bool mSYSShell)
  615. {
  616. this->MSYSShell = mSYSShell;
  617. }
  618. bool cmState::UseMSYSShell() const
  619. {
  620. return this->MSYSShell;
  621. }
  622. unsigned int cmState::GetCacheMajorVersion() const
  623. {
  624. return this->CacheManager->GetCacheMajorVersion();
  625. }
  626. unsigned int cmState::GetCacheMinorVersion() const
  627. {
  628. return this->CacheManager->GetCacheMinorVersion();
  629. }
  630. cmState::Mode cmState::GetMode() const
  631. {
  632. return this->CurrentMode;
  633. }
  634. std::string cmState::GetModeString() const
  635. {
  636. return ModeToString(this->CurrentMode);
  637. }
  638. void cmState::SetMode(cmState::Mode mode)
  639. {
  640. this->CurrentMode = mode;
  641. }
  642. std::string cmState::ModeToString(cmState::Mode mode)
  643. {
  644. switch (mode) {
  645. case Project:
  646. return "PROJECT";
  647. case Script:
  648. return "SCRIPT";
  649. case FindPackage:
  650. return "FIND_PACKAGE";
  651. case CTest:
  652. return "CTEST";
  653. case CPack:
  654. return "CPACK";
  655. case Unknown:
  656. return "UNKNOWN";
  657. }
  658. return "UNKNOWN";
  659. }
  660. std::string const& cmState::GetBinaryDirectory() const
  661. {
  662. return this->BinaryDirectory;
  663. }
  664. cmStateSnapshot cmState::CreateBaseSnapshot()
  665. {
  666. cmStateDetail::PositionType pos =
  667. this->SnapshotData.Push(this->SnapshotData.Root());
  668. pos->DirectoryParent = this->SnapshotData.Root();
  669. pos->ScopeParent = this->SnapshotData.Root();
  670. pos->SnapshotType = cmStateEnums::BaseType;
  671. pos->Keep = true;
  672. pos->BuildSystemDirectory =
  673. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  674. pos->ExecutionListFile =
  675. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  676. pos->IncludeDirectoryPosition = 0;
  677. pos->CompileDefinitionsPosition = 0;
  678. pos->CompileOptionsPosition = 0;
  679. pos->LinkOptionsPosition = 0;
  680. pos->LinkDirectoriesPosition = 0;
  681. pos->BuildSystemDirectory->DirectoryEnd = pos;
  682. pos->Policies = this->PolicyStack.Root();
  683. pos->PolicyRoot = this->PolicyStack.Root();
  684. pos->PolicyScope = this->PolicyStack.Root();
  685. assert(pos->Policies.IsValid());
  686. assert(pos->PolicyRoot.IsValid());
  687. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  688. assert(pos->Vars.IsValid());
  689. pos->Parent = this->VarTree.Root();
  690. pos->Root = this->VarTree.Root();
  691. return { this, pos };
  692. }
  693. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  694. cmStateSnapshot const& originSnapshot)
  695. {
  696. assert(originSnapshot.IsValid());
  697. cmStateDetail::PositionType pos =
  698. this->SnapshotData.Push(originSnapshot.Position);
  699. pos->DirectoryParent = originSnapshot.Position;
  700. pos->ScopeParent = originSnapshot.Position;
  701. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  702. pos->Keep = true;
  703. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  704. originSnapshot.Position->BuildSystemDirectory);
  705. pos->ExecutionListFile =
  706. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  707. pos->BuildSystemDirectory->DirectoryEnd = pos;
  708. pos->Policies = originSnapshot.Position->Policies;
  709. pos->PolicyRoot = originSnapshot.Position->Policies;
  710. pos->PolicyScope = originSnapshot.Position->Policies;
  711. assert(pos->Policies.IsValid());
  712. assert(pos->PolicyRoot.IsValid());
  713. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  714. pos->Parent = origin;
  715. pos->Root = origin;
  716. pos->Vars = this->VarTree.Push(origin);
  717. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  718. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  719. snapshot.SetDefaultDefinitions();
  720. snapshot.InitializeFromParent();
  721. snapshot.SetDirectoryDefinitions();
  722. return snapshot;
  723. }
  724. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  725. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  726. {
  727. cmStateDetail::PositionType pos =
  728. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  729. pos->ScopeParent = originSnapshot.Position;
  730. pos->SnapshotType = cmStateEnums::FunctionCallType;
  731. pos->Keep = false;
  732. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  733. originSnapshot.Position->ExecutionListFile, fileName);
  734. pos->BuildSystemDirectory->DirectoryEnd = pos;
  735. pos->PolicyScope = originSnapshot.Position->Policies;
  736. assert(originSnapshot.Position->Vars.IsValid());
  737. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  738. pos->Parent = origin;
  739. pos->Vars = this->VarTree.Push(origin);
  740. return { this, pos };
  741. }
  742. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  743. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  744. {
  745. cmStateDetail::PositionType pos =
  746. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  747. pos->SnapshotType = cmStateEnums::MacroCallType;
  748. pos->Keep = false;
  749. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  750. originSnapshot.Position->ExecutionListFile, fileName);
  751. assert(originSnapshot.Position->Vars.IsValid());
  752. pos->BuildSystemDirectory->DirectoryEnd = pos;
  753. pos->PolicyScope = originSnapshot.Position->Policies;
  754. return { this, pos };
  755. }
  756. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  757. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  758. {
  759. cmStateDetail::PositionType pos =
  760. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  761. pos->SnapshotType = cmStateEnums::IncludeFileType;
  762. pos->Keep = true;
  763. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  764. originSnapshot.Position->ExecutionListFile, fileName);
  765. assert(originSnapshot.Position->Vars.IsValid());
  766. pos->BuildSystemDirectory->DirectoryEnd = pos;
  767. pos->PolicyScope = originSnapshot.Position->Policies;
  768. return { this, pos };
  769. }
  770. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  771. cmStateSnapshot const& originSnapshot)
  772. {
  773. cmStateDetail::PositionType pos =
  774. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  775. pos->ScopeParent = originSnapshot.Position;
  776. pos->SnapshotType = cmStateEnums::VariableScopeType;
  777. pos->Keep = false;
  778. pos->PolicyScope = originSnapshot.Position->Policies;
  779. assert(originSnapshot.Position->Vars.IsValid());
  780. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  781. pos->Parent = origin;
  782. pos->Vars = this->VarTree.Push(origin);
  783. assert(pos->Vars.IsValid());
  784. return { this, pos };
  785. }
  786. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  787. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  788. {
  789. cmStateDetail::PositionType pos =
  790. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  791. pos->SnapshotType = cmStateEnums::InlineListFileType;
  792. pos->Keep = true;
  793. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  794. originSnapshot.Position->ExecutionListFile, fileName);
  795. pos->BuildSystemDirectory->DirectoryEnd = pos;
  796. pos->PolicyScope = originSnapshot.Position->Policies;
  797. return { this, pos };
  798. }
  799. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  800. cmStateSnapshot const& originSnapshot)
  801. {
  802. cmStateDetail::PositionType pos =
  803. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  804. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  805. pos->Keep = false;
  806. pos->BuildSystemDirectory->DirectoryEnd = pos;
  807. pos->PolicyScope = originSnapshot.Position->Policies;
  808. return { this, pos };
  809. }
  810. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  811. {
  812. cmStateDetail::PositionType pos = originSnapshot.Position;
  813. cmStateDetail::PositionType prevPos = pos;
  814. ++prevPos;
  815. prevPos->IncludeDirectoryPosition =
  816. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  817. prevPos->CompileDefinitionsPosition =
  818. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  819. prevPos->CompileOptionsPosition =
  820. prevPos->BuildSystemDirectory->CompileOptions.size();
  821. prevPos->LinkOptionsPosition =
  822. prevPos->BuildSystemDirectory->LinkOptions.size();
  823. prevPos->LinkDirectoriesPosition =
  824. prevPos->BuildSystemDirectory->LinkDirectories.size();
  825. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  826. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  827. if (pos->Vars != prevPos->Vars) {
  828. assert(this->VarTree.IsLast(pos->Vars));
  829. this->VarTree.Pop(pos->Vars);
  830. }
  831. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  832. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  833. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  834. }
  835. this->SnapshotData.Pop(pos);
  836. }
  837. return { this, prevPos };
  838. }
  839. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  840. std::string& value)
  841. {
  842. // input line is: key=value
  843. static cmsys::RegularExpression reg(
  844. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  845. // input line is: "key"=value
  846. static cmsys::RegularExpression regQuoted(
  847. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  848. bool flag = false;
  849. if (regQuoted.find(entry)) {
  850. var = regQuoted.match(1);
  851. value = regQuoted.match(2);
  852. flag = true;
  853. } else if (reg.find(entry)) {
  854. var = reg.match(1);
  855. value = reg.match(2);
  856. flag = true;
  857. }
  858. // if value is enclosed in single quotes ('foo') then remove them
  859. // it is used to enclose trailing space or tab
  860. if (flag && value.size() >= 2 && value.front() == '\'' &&
  861. value.back() == '\'') {
  862. value = value.substr(1, value.size() - 2);
  863. }
  864. return flag;
  865. }
  866. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  867. std::string& value,
  868. cmStateEnums::CacheEntryType& type)
  869. {
  870. // input line is: key:type=value
  871. static cmsys::RegularExpression reg(
  872. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  873. // input line is: "key":type=value
  874. static cmsys::RegularExpression regQuoted(
  875. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  876. bool flag = false;
  877. if (regQuoted.find(entry)) {
  878. var = regQuoted.match(1);
  879. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  880. value = regQuoted.match(3);
  881. flag = true;
  882. } else if (reg.find(entry)) {
  883. var = reg.match(1);
  884. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  885. value = reg.match(3);
  886. flag = true;
  887. }
  888. // if value is enclosed in single quotes ('foo') then remove them
  889. // it is used to enclose trailing space or tab
  890. if (flag && value.size() >= 2 && value.front() == '\'' &&
  891. value.back() == '\'') {
  892. value = value.substr(1, value.size() - 2);
  893. }
  894. if (!flag) {
  895. return ParseEntryWithoutType(entry, var, value);
  896. }
  897. return flag;
  898. }