cmState.cxx 31 KB

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