cmState.cxx 31 KB

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