cmState.cxx 34 KB

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