cmState.cxx 32 KB

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