cmState.cxx 32 KB

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