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