cmState.cxx 32 KB

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