cmState.cxx 31 KB

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