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