cmState.cxx 31 KB

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