cmState.cxx 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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 <assert.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <utility>
  10. #include "cm_memory.hxx"
  11. #include "cmCacheManager.h"
  12. #include "cmCommand.h"
  13. #include "cmDefinitions.h"
  14. #include "cmDisallowedCommand.h"
  15. #include "cmExecutionStatus.h"
  16. #include "cmGlobVerificationManager.h"
  17. #include "cmListFileCache.h"
  18. #include "cmMakefile.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 cmStateSnapshot(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. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it =
  309. 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. std::map<cmProperty::ScopeType, cmPropertyDefinitionMap>::const_iterator it =
  319. this->PropertyDefinitions.find(scope);
  320. if (it == this->PropertyDefinitions.end()) {
  321. return false;
  322. }
  323. return it->second.IsPropertyChained(name);
  324. }
  325. void cmState::SetLanguageEnabled(std::string const& l)
  326. {
  327. std::vector<std::string>::iterator it = std::lower_bound(
  328. this->EnabledLanguages.begin(), this->EnabledLanguages.end(), l);
  329. if (it == this->EnabledLanguages.end() || *it != l) {
  330. this->EnabledLanguages.insert(it, l);
  331. }
  332. }
  333. bool cmState::GetLanguageEnabled(std::string const& l) const
  334. {
  335. return std::binary_search(this->EnabledLanguages.begin(),
  336. this->EnabledLanguages.end(), l);
  337. }
  338. std::vector<std::string> cmState::GetEnabledLanguages() const
  339. {
  340. return this->EnabledLanguages;
  341. }
  342. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  343. {
  344. this->EnabledLanguages = langs;
  345. }
  346. void cmState::ClearEnabledLanguages()
  347. {
  348. this->EnabledLanguages.clear();
  349. }
  350. bool cmState::GetIsInTryCompile() const
  351. {
  352. return this->IsInTryCompile;
  353. }
  354. void cmState::SetIsInTryCompile(bool b)
  355. {
  356. this->IsInTryCompile = b;
  357. }
  358. bool cmState::GetIsGeneratorMultiConfig() const
  359. {
  360. return this->IsGeneratorMultiConfig;
  361. }
  362. void cmState::SetIsGeneratorMultiConfig(bool b)
  363. {
  364. this->IsGeneratorMultiConfig = b;
  365. }
  366. void cmState::AddBuiltinCommand(std::string const& name,
  367. std::unique_ptr<cmCommand> command)
  368. {
  369. this->AddBuiltinCommand(name, cmLegacyCommandWrapper(std::move(command)));
  370. }
  371. void cmState::AddBuiltinCommand(std::string const& name, Command command)
  372. {
  373. assert(name == cmSystemTools::LowerCase(name));
  374. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  375. this->BuiltinCommands.emplace(name, std::move(command));
  376. }
  377. void cmState::AddBuiltinCommand(std::string const& name,
  378. BuiltinCommand command)
  379. {
  380. this->AddBuiltinCommand(
  381. name,
  382. [command](const std::vector<cmListFileArgument>& args,
  383. cmExecutionStatus& status) -> bool {
  384. std::vector<std::string> expandedArguments;
  385. if (!status.GetMakefile().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. }
  393. void cmState::AddDisallowedCommand(std::string const& name,
  394. std::unique_ptr<cmCommand> command,
  395. cmPolicies::PolicyID policy,
  396. const char* message)
  397. {
  398. this->AddBuiltinCommand(
  399. name,
  400. cm::make_unique<cmDisallowedCommand>(std::move(command), policy, message));
  401. }
  402. void cmState::AddUnexpectedCommand(std::string const& name, const char* error)
  403. {
  404. this->AddBuiltinCommand(
  405. name,
  406. [name, error](std::vector<cmListFileArgument> const&,
  407. cmExecutionStatus& status) -> bool {
  408. const char* versionValue =
  409. status.GetMakefile().GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  410. if (name == "endif" && (!versionValue || atof(versionValue) <= 1.4)) {
  411. return true;
  412. }
  413. status.SetError(error);
  414. return false;
  415. });
  416. }
  417. void cmState::AddScriptedCommand(std::string const& name, Command command)
  418. {
  419. std::string sName = cmSystemTools::LowerCase(name);
  420. // if the command already exists, give a new name to the old command.
  421. if (Command oldCmd = this->GetCommand(sName)) {
  422. this->ScriptedCommands["_" + sName] = oldCmd;
  423. }
  424. this->ScriptedCommands[sName] = std::move(command);
  425. }
  426. cmState::Command cmState::GetCommand(std::string const& name) const
  427. {
  428. return GetCommandByExactName(cmSystemTools::LowerCase(name));
  429. }
  430. cmState::Command cmState::GetCommandByExactName(std::string const& name) const
  431. {
  432. auto pos = this->ScriptedCommands.find(name);
  433. if (pos != this->ScriptedCommands.end()) {
  434. return pos->second;
  435. }
  436. pos = this->BuiltinCommands.find(name);
  437. if (pos != this->BuiltinCommands.end()) {
  438. return pos->second;
  439. }
  440. return nullptr;
  441. }
  442. std::vector<std::string> cmState::GetCommandNames() const
  443. {
  444. std::vector<std::string> commandNames;
  445. commandNames.reserve(this->BuiltinCommands.size() +
  446. this->ScriptedCommands.size());
  447. for (auto const& bc : this->BuiltinCommands) {
  448. commandNames.push_back(bc.first);
  449. }
  450. for (auto const& sc : this->ScriptedCommands) {
  451. commandNames.push_back(sc.first);
  452. }
  453. std::sort(commandNames.begin(), commandNames.end());
  454. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  455. commandNames.end());
  456. return commandNames;
  457. }
  458. void cmState::RemoveBuiltinCommand(std::string const& name)
  459. {
  460. assert(name == cmSystemTools::LowerCase(name));
  461. this->BuiltinCommands.erase(name);
  462. }
  463. void cmState::RemoveUserDefinedCommands()
  464. {
  465. this->ScriptedCommands.clear();
  466. }
  467. void cmState::SetGlobalProperty(const std::string& prop, const char* value)
  468. {
  469. this->GlobalProperties.SetProperty(prop, value);
  470. }
  471. void cmState::AppendGlobalProperty(const std::string& prop, const char* value,
  472. bool asString)
  473. {
  474. this->GlobalProperties.AppendProperty(prop, value, asString);
  475. }
  476. const char* cmState::GetGlobalProperty(const std::string& prop)
  477. {
  478. if (prop == "CACHE_VARIABLES") {
  479. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  480. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";").c_str());
  481. } else if (prop == "COMMANDS") {
  482. std::vector<std::string> commands = this->GetCommandNames();
  483. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";").c_str());
  484. } else if (prop == "IN_TRY_COMPILE") {
  485. this->SetGlobalProperty("IN_TRY_COMPILE",
  486. this->IsInTryCompile ? "1" : "0");
  487. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  488. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  489. this->IsGeneratorMultiConfig ? "1" : "0");
  490. } else if (prop == "ENABLED_LANGUAGES") {
  491. std::string langs;
  492. langs = cmJoin(this->EnabledLanguages, ";");
  493. this->SetGlobalProperty("ENABLED_LANGUAGES", langs.c_str());
  494. } else if (prop == "CMAKE_ROLE") {
  495. std::string mode = this->GetModeString();
  496. this->SetGlobalProperty("CMAKE_ROLE", mode.c_str());
  497. }
  498. #define STRING_LIST_ELEMENT(F) ";" #F
  499. if (prop == "CMAKE_C_KNOWN_FEATURES") {
  500. return &FOR_EACH_C_FEATURE(STRING_LIST_ELEMENT)[1];
  501. }
  502. if (prop == "CMAKE_C90_KNOWN_FEATURES") {
  503. return &FOR_EACH_C90_FEATURE(STRING_LIST_ELEMENT)[1];
  504. }
  505. if (prop == "CMAKE_C99_KNOWN_FEATURES") {
  506. return &FOR_EACH_C99_FEATURE(STRING_LIST_ELEMENT)[1];
  507. }
  508. if (prop == "CMAKE_C11_KNOWN_FEATURES") {
  509. return &FOR_EACH_C11_FEATURE(STRING_LIST_ELEMENT)[1];
  510. }
  511. if (prop == "CMAKE_CXX_KNOWN_FEATURES") {
  512. return &FOR_EACH_CXX_FEATURE(STRING_LIST_ELEMENT)[1];
  513. }
  514. if (prop == "CMAKE_CXX98_KNOWN_FEATURES") {
  515. return &FOR_EACH_CXX98_FEATURE(STRING_LIST_ELEMENT)[1];
  516. }
  517. if (prop == "CMAKE_CXX11_KNOWN_FEATURES") {
  518. return &FOR_EACH_CXX11_FEATURE(STRING_LIST_ELEMENT)[1];
  519. }
  520. if (prop == "CMAKE_CXX14_KNOWN_FEATURES") {
  521. return &FOR_EACH_CXX14_FEATURE(STRING_LIST_ELEMENT)[1];
  522. }
  523. #undef STRING_LIST_ELEMENT
  524. return this->GlobalProperties.GetPropertyValue(prop);
  525. }
  526. bool cmState::GetGlobalPropertyAsBool(const std::string& prop)
  527. {
  528. return cmSystemTools::IsOn(this->GetGlobalProperty(prop));
  529. }
  530. void cmState::SetSourceDirectory(std::string const& sourceDirectory)
  531. {
  532. this->SourceDirectory = sourceDirectory;
  533. cmSystemTools::ConvertToUnixSlashes(this->SourceDirectory);
  534. }
  535. std::string const& cmState::GetSourceDirectory() const
  536. {
  537. return this->SourceDirectory;
  538. }
  539. void cmState::SetBinaryDirectory(std::string const& binaryDirectory)
  540. {
  541. this->BinaryDirectory = binaryDirectory;
  542. cmSystemTools::ConvertToUnixSlashes(this->BinaryDirectory);
  543. }
  544. void cmState::SetWindowsShell(bool windowsShell)
  545. {
  546. this->WindowsShell = windowsShell;
  547. }
  548. bool cmState::UseWindowsShell() const
  549. {
  550. return this->WindowsShell;
  551. }
  552. void cmState::SetWindowsVSIDE(bool windowsVSIDE)
  553. {
  554. this->WindowsVSIDE = windowsVSIDE;
  555. }
  556. bool cmState::UseWindowsVSIDE() const
  557. {
  558. return this->WindowsVSIDE;
  559. }
  560. void cmState::SetGhsMultiIDE(bool ghsMultiIDE)
  561. {
  562. this->GhsMultiIDE = ghsMultiIDE;
  563. }
  564. bool cmState::UseGhsMultiIDE() const
  565. {
  566. return this->GhsMultiIDE;
  567. }
  568. void cmState::SetWatcomWMake(bool watcomWMake)
  569. {
  570. this->WatcomWMake = watcomWMake;
  571. }
  572. bool cmState::UseWatcomWMake() const
  573. {
  574. return this->WatcomWMake;
  575. }
  576. void cmState::SetMinGWMake(bool minGWMake)
  577. {
  578. this->MinGWMake = minGWMake;
  579. }
  580. bool cmState::UseMinGWMake() const
  581. {
  582. return this->MinGWMake;
  583. }
  584. void cmState::SetNMake(bool nMake)
  585. {
  586. this->NMake = nMake;
  587. }
  588. bool cmState::UseNMake() const
  589. {
  590. return this->NMake;
  591. }
  592. void cmState::SetMSYSShell(bool mSYSShell)
  593. {
  594. this->MSYSShell = mSYSShell;
  595. }
  596. bool cmState::UseMSYSShell() const
  597. {
  598. return this->MSYSShell;
  599. }
  600. unsigned int cmState::GetCacheMajorVersion() const
  601. {
  602. return this->CacheManager->GetCacheMajorVersion();
  603. }
  604. unsigned int cmState::GetCacheMinorVersion() const
  605. {
  606. return this->CacheManager->GetCacheMinorVersion();
  607. }
  608. cmState::Mode cmState::GetMode() const
  609. {
  610. return this->CurrentMode;
  611. }
  612. std::string cmState::GetModeString() const
  613. {
  614. return ModeToString(this->CurrentMode);
  615. }
  616. void cmState::SetMode(cmState::Mode mode)
  617. {
  618. this->CurrentMode = mode;
  619. }
  620. std::string cmState::ModeToString(cmState::Mode mode)
  621. {
  622. switch (mode) {
  623. case Project:
  624. return "PROJECT";
  625. case Script:
  626. return "SCRIPT";
  627. case FindPackage:
  628. return "FIND_PACKAGE";
  629. case CTest:
  630. return "CTEST";
  631. case CPack:
  632. return "CPACK";
  633. case Unknown:
  634. return "UNKNOWN";
  635. }
  636. return "UNKNOWN";
  637. }
  638. std::string const& cmState::GetBinaryDirectory() const
  639. {
  640. return this->BinaryDirectory;
  641. }
  642. cmStateSnapshot cmState::CreateBaseSnapshot()
  643. {
  644. cmStateDetail::PositionType pos =
  645. this->SnapshotData.Push(this->SnapshotData.Root());
  646. pos->DirectoryParent = this->SnapshotData.Root();
  647. pos->ScopeParent = this->SnapshotData.Root();
  648. pos->SnapshotType = cmStateEnums::BaseType;
  649. pos->Keep = true;
  650. pos->BuildSystemDirectory =
  651. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  652. pos->ExecutionListFile =
  653. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  654. pos->IncludeDirectoryPosition = 0;
  655. pos->CompileDefinitionsPosition = 0;
  656. pos->CompileOptionsPosition = 0;
  657. pos->LinkOptionsPosition = 0;
  658. pos->LinkDirectoriesPosition = 0;
  659. pos->BuildSystemDirectory->DirectoryEnd = pos;
  660. pos->Policies = this->PolicyStack.Root();
  661. pos->PolicyRoot = this->PolicyStack.Root();
  662. pos->PolicyScope = this->PolicyStack.Root();
  663. assert(pos->Policies.IsValid());
  664. assert(pos->PolicyRoot.IsValid());
  665. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  666. assert(pos->Vars.IsValid());
  667. pos->Parent = this->VarTree.Root();
  668. pos->Root = this->VarTree.Root();
  669. return cmStateSnapshot(this, pos);
  670. }
  671. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  672. cmStateSnapshot const& originSnapshot)
  673. {
  674. assert(originSnapshot.IsValid());
  675. cmStateDetail::PositionType pos =
  676. this->SnapshotData.Push(originSnapshot.Position);
  677. pos->DirectoryParent = originSnapshot.Position;
  678. pos->ScopeParent = originSnapshot.Position;
  679. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  680. pos->Keep = true;
  681. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  682. originSnapshot.Position->BuildSystemDirectory);
  683. pos->ExecutionListFile =
  684. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  685. pos->BuildSystemDirectory->DirectoryEnd = pos;
  686. pos->Policies = originSnapshot.Position->Policies;
  687. pos->PolicyRoot = originSnapshot.Position->Policies;
  688. pos->PolicyScope = originSnapshot.Position->Policies;
  689. assert(pos->Policies.IsValid());
  690. assert(pos->PolicyRoot.IsValid());
  691. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  692. pos->Parent = origin;
  693. pos->Root = origin;
  694. pos->Vars = this->VarTree.Push(origin);
  695. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  696. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  697. snapshot.SetDefaultDefinitions();
  698. snapshot.InitializeFromParent();
  699. snapshot.SetDirectoryDefinitions();
  700. return snapshot;
  701. }
  702. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  703. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  704. {
  705. cmStateDetail::PositionType pos =
  706. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  707. pos->ScopeParent = originSnapshot.Position;
  708. pos->SnapshotType = cmStateEnums::FunctionCallType;
  709. pos->Keep = false;
  710. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  711. originSnapshot.Position->ExecutionListFile, fileName);
  712. pos->BuildSystemDirectory->DirectoryEnd = pos;
  713. pos->PolicyScope = originSnapshot.Position->Policies;
  714. assert(originSnapshot.Position->Vars.IsValid());
  715. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  716. pos->Parent = origin;
  717. pos->Vars = this->VarTree.Push(origin);
  718. return cmStateSnapshot(this, pos);
  719. }
  720. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  721. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  722. {
  723. cmStateDetail::PositionType pos =
  724. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  725. pos->SnapshotType = cmStateEnums::MacroCallType;
  726. pos->Keep = false;
  727. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  728. originSnapshot.Position->ExecutionListFile, fileName);
  729. assert(originSnapshot.Position->Vars.IsValid());
  730. pos->BuildSystemDirectory->DirectoryEnd = pos;
  731. pos->PolicyScope = originSnapshot.Position->Policies;
  732. return cmStateSnapshot(this, pos);
  733. }
  734. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  735. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  736. {
  737. cmStateDetail::PositionType pos =
  738. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  739. pos->SnapshotType = cmStateEnums::IncludeFileType;
  740. pos->Keep = true;
  741. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  742. originSnapshot.Position->ExecutionListFile, fileName);
  743. assert(originSnapshot.Position->Vars.IsValid());
  744. pos->BuildSystemDirectory->DirectoryEnd = pos;
  745. pos->PolicyScope = originSnapshot.Position->Policies;
  746. return cmStateSnapshot(this, pos);
  747. }
  748. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  749. cmStateSnapshot const& originSnapshot)
  750. {
  751. cmStateDetail::PositionType pos =
  752. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  753. pos->ScopeParent = originSnapshot.Position;
  754. pos->SnapshotType = cmStateEnums::VariableScopeType;
  755. pos->Keep = false;
  756. pos->PolicyScope = originSnapshot.Position->Policies;
  757. assert(originSnapshot.Position->Vars.IsValid());
  758. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  759. pos->Parent = origin;
  760. pos->Vars = this->VarTree.Push(origin);
  761. assert(pos->Vars.IsValid());
  762. return cmStateSnapshot(this, pos);
  763. }
  764. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  765. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  766. {
  767. cmStateDetail::PositionType pos =
  768. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  769. pos->SnapshotType = cmStateEnums::InlineListFileType;
  770. pos->Keep = true;
  771. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  772. originSnapshot.Position->ExecutionListFile, fileName);
  773. pos->BuildSystemDirectory->DirectoryEnd = pos;
  774. pos->PolicyScope = originSnapshot.Position->Policies;
  775. return cmStateSnapshot(this, pos);
  776. }
  777. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  778. cmStateSnapshot const& originSnapshot)
  779. {
  780. cmStateDetail::PositionType pos =
  781. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  782. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  783. pos->Keep = false;
  784. pos->BuildSystemDirectory->DirectoryEnd = pos;
  785. pos->PolicyScope = originSnapshot.Position->Policies;
  786. return cmStateSnapshot(this, pos);
  787. }
  788. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  789. {
  790. cmStateDetail::PositionType pos = originSnapshot.Position;
  791. cmStateDetail::PositionType prevPos = pos;
  792. ++prevPos;
  793. prevPos->IncludeDirectoryPosition =
  794. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  795. prevPos->CompileDefinitionsPosition =
  796. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  797. prevPos->CompileOptionsPosition =
  798. prevPos->BuildSystemDirectory->CompileOptions.size();
  799. prevPos->LinkOptionsPosition =
  800. prevPos->BuildSystemDirectory->LinkOptions.size();
  801. prevPos->LinkDirectoriesPosition =
  802. prevPos->BuildSystemDirectory->LinkDirectories.size();
  803. prevPos->BuildSystemDirectory->DirectoryEnd = prevPos;
  804. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  805. if (pos->Vars != prevPos->Vars) {
  806. assert(this->VarTree.IsLast(pos->Vars));
  807. this->VarTree.Pop(pos->Vars);
  808. }
  809. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  810. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  811. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  812. }
  813. this->SnapshotData.Pop(pos);
  814. }
  815. return cmStateSnapshot(this, prevPos);
  816. }
  817. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  818. std::string& value)
  819. {
  820. // input line is: key=value
  821. static cmsys::RegularExpression reg(
  822. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  823. // input line is: "key"=value
  824. static cmsys::RegularExpression regQuoted(
  825. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  826. bool flag = false;
  827. if (regQuoted.find(entry)) {
  828. var = regQuoted.match(1);
  829. value = regQuoted.match(2);
  830. flag = true;
  831. } else if (reg.find(entry)) {
  832. var = reg.match(1);
  833. value = reg.match(2);
  834. flag = true;
  835. }
  836. // if value is enclosed in single quotes ('foo') then remove them
  837. // it is used to enclose trailing space or tab
  838. if (flag && value.size() >= 2 && value.front() == '\'' &&
  839. value.back() == '\'') {
  840. value = value.substr(1, value.size() - 2);
  841. }
  842. return flag;
  843. }
  844. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  845. std::string& value,
  846. cmStateEnums::CacheEntryType& type)
  847. {
  848. // input line is: key:type=value
  849. static cmsys::RegularExpression reg(
  850. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  851. // input line is: "key":type=value
  852. static cmsys::RegularExpression regQuoted(
  853. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  854. bool flag = false;
  855. if (regQuoted.find(entry)) {
  856. var = regQuoted.match(1);
  857. type = cmState::StringToCacheEntryType(regQuoted.match(2).c_str());
  858. value = regQuoted.match(3);
  859. flag = true;
  860. } else if (reg.find(entry)) {
  861. var = reg.match(1);
  862. type = cmState::StringToCacheEntryType(reg.match(2).c_str());
  863. value = reg.match(3);
  864. flag = true;
  865. }
  866. // if value is enclosed in single quotes ('foo') then remove them
  867. // it is used to enclose trailing space or tab
  868. if (flag && value.size() >= 2 && value.front() == '\'' &&
  869. value.back() == '\'') {
  870. value = value.substr(1, value.size() - 2);
  871. }
  872. if (!flag) {
  873. return ParseEntryWithoutType(entry, var, value);
  874. }
  875. return flag;
  876. }