cmState.cxx 32 KB

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