cmState.cxx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  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 "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. cmState::cmState(Mode mode, ProjectKind projectKind)
  26. : StateMode(mode)
  27. , StateProjectKind(projectKind)
  28. {
  29. this->CacheManager = cm::make_unique<cmCacheManager>();
  30. this->GlobVerificationManager = cm::make_unique<cmGlobVerificationManager>();
  31. }
  32. cmState::~cmState() = default;
  33. const std::string& cmState::GetTargetTypeName(
  34. cmStateEnums::TargetType targetType)
  35. {
  36. #define MAKE_STATIC_PROP(PROP) static const std::string prop##PROP = #PROP
  37. MAKE_STATIC_PROP(STATIC_LIBRARY);
  38. MAKE_STATIC_PROP(MODULE_LIBRARY);
  39. MAKE_STATIC_PROP(SHARED_LIBRARY);
  40. MAKE_STATIC_PROP(OBJECT_LIBRARY);
  41. MAKE_STATIC_PROP(EXECUTABLE);
  42. MAKE_STATIC_PROP(UTILITY);
  43. MAKE_STATIC_PROP(GLOBAL_TARGET);
  44. MAKE_STATIC_PROP(INTERFACE_LIBRARY);
  45. MAKE_STATIC_PROP(UNKNOWN_LIBRARY);
  46. static const std::string propEmpty;
  47. #undef MAKE_STATIC_PROP
  48. switch (targetType) {
  49. case cmStateEnums::STATIC_LIBRARY:
  50. return propSTATIC_LIBRARY;
  51. case cmStateEnums::MODULE_LIBRARY:
  52. return propMODULE_LIBRARY;
  53. case cmStateEnums::SHARED_LIBRARY:
  54. return propSHARED_LIBRARY;
  55. case cmStateEnums::OBJECT_LIBRARY:
  56. return propOBJECT_LIBRARY;
  57. case cmStateEnums::EXECUTABLE:
  58. return propEXECUTABLE;
  59. case cmStateEnums::UTILITY:
  60. return propUTILITY;
  61. case cmStateEnums::GLOBAL_TARGET:
  62. return propGLOBAL_TARGET;
  63. case cmStateEnums::INTERFACE_LIBRARY:
  64. return propINTERFACE_LIBRARY;
  65. case cmStateEnums::UNKNOWN_LIBRARY:
  66. return propUNKNOWN_LIBRARY;
  67. }
  68. assert(false && "Unexpected target type");
  69. return propEmpty;
  70. }
  71. static const std::array<std::string, 7> cmCacheEntryTypes = {
  72. { "BOOL", "PATH", "FILEPATH", "STRING", "INTERNAL", "STATIC",
  73. "UNINITIALIZED" }
  74. };
  75. const std::string& cmState::CacheEntryTypeToString(
  76. cmStateEnums::CacheEntryType type)
  77. {
  78. if (type < cmStateEnums::BOOL || type > cmStateEnums::UNINITIALIZED) {
  79. type = cmStateEnums::UNINITIALIZED;
  80. }
  81. return cmCacheEntryTypes[type];
  82. }
  83. cmStateEnums::CacheEntryType cmState::StringToCacheEntryType(
  84. const std::string& s)
  85. {
  86. cmStateEnums::CacheEntryType type = cmStateEnums::STRING;
  87. StringToCacheEntryType(s, type);
  88. return type;
  89. }
  90. bool cmState::StringToCacheEntryType(const std::string& s,
  91. cmStateEnums::CacheEntryType& type)
  92. {
  93. // NOLINTNEXTLINE(readability-qualified-auto)
  94. auto const entry =
  95. std::find(cmCacheEntryTypes.begin(), cmCacheEntryTypes.end(), s);
  96. if (entry != cmCacheEntryTypes.end()) {
  97. type = static_cast<cmStateEnums::CacheEntryType>(
  98. entry - cmCacheEntryTypes.begin());
  99. return true;
  100. }
  101. return false;
  102. }
  103. bool cmState::IsCacheEntryType(std::string const& key)
  104. {
  105. return std::any_of(
  106. cmCacheEntryTypes.begin(), cmCacheEntryTypes.end(),
  107. [&key](std::string const& i) -> bool { return key == i; });
  108. }
  109. bool cmState::LoadCache(const std::string& path, bool internal,
  110. std::set<std::string>& excludes,
  111. std::set<std::string>& includes)
  112. {
  113. return this->CacheManager->LoadCache(path, internal, excludes, includes);
  114. }
  115. bool cmState::SaveCache(const std::string& path, cmMessenger* messenger)
  116. {
  117. return this->CacheManager->SaveCache(path, messenger);
  118. }
  119. bool cmState::DeleteCache(const std::string& path)
  120. {
  121. return this->CacheManager->DeleteCache(path);
  122. }
  123. bool cmState::IsCacheLoaded() const
  124. {
  125. return this->CacheManager->IsCacheLoaded();
  126. }
  127. std::vector<std::string> cmState::GetCacheEntryKeys() const
  128. {
  129. return this->CacheManager->GetCacheEntryKeys();
  130. }
  131. cmValue cmState::GetCacheEntryValue(std::string const& key) const
  132. {
  133. return this->CacheManager->GetCacheEntryValue(key);
  134. }
  135. std::string cmState::GetSafeCacheEntryValue(std::string const& key) const
  136. {
  137. if (cmValue val = this->GetCacheEntryValue(key)) {
  138. return *val;
  139. }
  140. return std::string();
  141. }
  142. cmValue cmState::GetInitializedCacheValue(std::string const& key) const
  143. {
  144. return this->CacheManager->GetInitializedCacheValue(key);
  145. }
  146. cmStateEnums::CacheEntryType cmState::GetCacheEntryType(
  147. std::string const& key) const
  148. {
  149. return this->CacheManager->GetCacheEntryType(key);
  150. }
  151. void cmState::SetCacheEntryValue(std::string const& key,
  152. std::string const& value)
  153. {
  154. this->CacheManager->SetCacheEntryValue(key, value);
  155. }
  156. void cmState::SetCacheEntryProperty(std::string const& key,
  157. std::string const& propertyName,
  158. std::string const& value)
  159. {
  160. this->CacheManager->SetCacheEntryProperty(key, propertyName, value);
  161. }
  162. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  163. std::string const& propertyName,
  164. bool value)
  165. {
  166. this->CacheManager->SetCacheEntryBoolProperty(key, propertyName, value);
  167. }
  168. std::vector<std::string> cmState::GetCacheEntryPropertyList(
  169. const std::string& key)
  170. {
  171. return this->CacheManager->GetCacheEntryPropertyList(key);
  172. }
  173. cmValue cmState::GetCacheEntryProperty(std::string const& key,
  174. std::string const& propertyName)
  175. {
  176. return this->CacheManager->GetCacheEntryProperty(key, propertyName);
  177. }
  178. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  179. std::string const& propertyName)
  180. {
  181. return this->CacheManager->GetCacheEntryPropertyAsBool(key, propertyName);
  182. }
  183. void cmState::AddCacheEntry(const std::string& key, cmValue value,
  184. const std::string& helpString,
  185. cmStateEnums::CacheEntryType type)
  186. {
  187. this->CacheManager->AddCacheEntry(key, value, helpString, type);
  188. }
  189. bool cmState::DoWriteGlobVerifyTarget() const
  190. {
  191. return this->GlobVerificationManager->DoWriteVerifyTarget();
  192. }
  193. std::string const& cmState::GetGlobVerifyScript() const
  194. {
  195. return this->GlobVerificationManager->GetVerifyScript();
  196. }
  197. std::string const& cmState::GetGlobVerifyStamp() const
  198. {
  199. return this->GlobVerificationManager->GetVerifyStamp();
  200. }
  201. bool cmState::SaveVerificationScript(const std::string& path,
  202. cmMessenger* messenger)
  203. {
  204. return this->GlobVerificationManager->SaveVerificationScript(path,
  205. messenger);
  206. }
  207. void cmState::AddGlobCacheEntry(
  208. bool recurse, bool listDirectories, bool followSymlinks,
  209. const std::string& relative, const std::string& expression,
  210. const std::vector<std::string>& files, const std::string& variable,
  211. cmListFileBacktrace const& backtrace, cmMessenger* messenger)
  212. {
  213. this->GlobVerificationManager->AddCacheEntry(
  214. recurse, listDirectories, followSymlinks, relative, expression, files,
  215. variable, backtrace, messenger);
  216. }
  217. void cmState::RemoveCacheEntry(std::string const& key)
  218. {
  219. this->CacheManager->RemoveCacheEntry(key);
  220. }
  221. void cmState::AppendCacheEntryProperty(const std::string& key,
  222. const std::string& property,
  223. const std::string& value, bool asString)
  224. {
  225. this->CacheManager->AppendCacheEntryProperty(key, property, value, asString);
  226. }
  227. void cmState::RemoveCacheEntryProperty(std::string const& key,
  228. std::string const& propertyName)
  229. {
  230. this->CacheManager->RemoveCacheEntryProperty(key, propertyName);
  231. }
  232. cmStateSnapshot cmState::Reset()
  233. {
  234. this->GlobalProperties.Clear();
  235. this->PropertyDefinitions = {};
  236. this->GlobVerificationManager->Reset();
  237. cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
  238. this->ExecutionListFiles.Truncate();
  239. {
  240. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator it =
  241. this->BuildsystemDirectory.Truncate();
  242. it->IncludeDirectories.clear();
  243. it->CompileDefinitions.clear();
  244. it->CompileOptions.clear();
  245. it->LinkOptions.clear();
  246. it->LinkDirectories.clear();
  247. it->CurrentScope = pos;
  248. it->NormalTargetNames.clear();
  249. it->ImportedTargetNames.clear();
  250. it->Properties.Clear();
  251. it->Children.clear();
  252. }
  253. this->PolicyStack.Clear();
  254. pos->Policies = this->PolicyStack.Root();
  255. pos->PolicyRoot = this->PolicyStack.Root();
  256. pos->PolicyScope = this->PolicyStack.Root();
  257. assert(pos->Policies.IsValid());
  258. assert(pos->PolicyRoot.IsValid());
  259. {
  260. std::string srcDir =
  261. *cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root);
  262. std::string binDir =
  263. *cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root);
  264. this->VarTree.Clear();
  265. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  266. pos->Parent = this->VarTree.Root();
  267. pos->Root = this->VarTree.Root();
  268. pos->Vars->Set("CMAKE_SOURCE_DIR", srcDir);
  269. pos->Vars->Set("CMAKE_BINARY_DIR", binDir);
  270. }
  271. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY, "", "",
  272. true);
  273. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::DIRECTORY, "", "",
  274. true);
  275. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY, "", "",
  276. true);
  277. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::TARGET, "", "",
  278. true);
  279. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::TARGET, "", "", true);
  280. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::TARGET, "", "", true);
  281. return { this, pos };
  282. }
  283. void cmState::DefineProperty(const std::string& name,
  284. cmProperty::ScopeType scope,
  285. const std::string& ShortDescription,
  286. const std::string& FullDescription, bool chained,
  287. const std::string& initializeFromVariable)
  288. {
  289. this->PropertyDefinitions.DefineProperty(name, scope, ShortDescription,
  290. FullDescription, chained,
  291. initializeFromVariable);
  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. CM_FALLTHROUGH;
  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. cmValue 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::SetFatalErrorOccurred();
  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,
  494. const std::string& value)
  495. {
  496. this->GlobalProperties.SetProperty(prop, value);
  497. }
  498. void cmState::SetGlobalProperty(const std::string& prop, cmValue 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. cmValue 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", cmList::to_string(cacheKeys));
  512. } else if (prop == "COMMANDS") {
  513. std::vector<std::string> commands = this->GetCommandNames();
  514. this->SetGlobalProperty("COMMANDS", cmList::to_string(commands));
  515. } else if (prop == "IN_TRY_COMPILE") {
  516. this->SetGlobalProperty(
  517. "IN_TRY_COMPILE",
  518. this->StateProjectKind == ProjectKind::TryCompile ? "1" : "0");
  519. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  520. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  521. this->IsGeneratorMultiConfig ? "1" : "0");
  522. } else if (prop == "ENABLED_LANGUAGES") {
  523. auto langs = cmList::to_string(this->EnabledLanguages);
  524. this->SetGlobalProperty("ENABLED_LANGUAGES", langs);
  525. } else if (prop == "CMAKE_ROLE") {
  526. std::string mode = this->GetModeString();
  527. this->SetGlobalProperty("CMAKE_ROLE", mode);
  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 cmValue(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 cmValue(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 cmValue(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 cmValue(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 cmValue(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 cmValue(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 cmValue(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 cmValue(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 cmValue(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->StateMode;
  671. }
  672. std::string cmState::GetModeString() const
  673. {
  674. return ModeToString(this->StateMode);
  675. }
  676. std::string cmState::ModeToString(cmState::Mode mode)
  677. {
  678. switch (mode) {
  679. case Project:
  680. return "PROJECT";
  681. case Script:
  682. return "SCRIPT";
  683. case FindPackage:
  684. return "FIND_PACKAGE";
  685. case CTest:
  686. return "CTEST";
  687. case CPack:
  688. return "CPACK";
  689. case Help:
  690. return "HELP";
  691. case Unknown:
  692. return "UNKNOWN";
  693. }
  694. return "UNKNOWN";
  695. }
  696. cmState::ProjectKind cmState::GetProjectKind() const
  697. {
  698. return this->StateProjectKind;
  699. }
  700. std::string const& cmState::GetBinaryDirectory() const
  701. {
  702. return this->BinaryDirectory;
  703. }
  704. cmStateSnapshot cmState::CreateBaseSnapshot()
  705. {
  706. cmStateDetail::PositionType pos =
  707. this->SnapshotData.Push(this->SnapshotData.Root());
  708. pos->DirectoryParent = this->SnapshotData.Root();
  709. pos->ScopeParent = this->SnapshotData.Root();
  710. pos->SnapshotType = cmStateEnums::BaseType;
  711. pos->Keep = true;
  712. pos->BuildSystemDirectory =
  713. this->BuildsystemDirectory.Push(this->BuildsystemDirectory.Root());
  714. pos->ExecutionListFile =
  715. this->ExecutionListFiles.Push(this->ExecutionListFiles.Root());
  716. pos->IncludeDirectoryPosition = 0;
  717. pos->CompileDefinitionsPosition = 0;
  718. pos->CompileOptionsPosition = 0;
  719. pos->LinkOptionsPosition = 0;
  720. pos->LinkDirectoriesPosition = 0;
  721. pos->BuildSystemDirectory->CurrentScope = pos;
  722. pos->Policies = this->PolicyStack.Root();
  723. pos->PolicyRoot = this->PolicyStack.Root();
  724. pos->PolicyScope = this->PolicyStack.Root();
  725. assert(pos->Policies.IsValid());
  726. assert(pos->PolicyRoot.IsValid());
  727. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  728. assert(pos->Vars.IsValid());
  729. pos->Parent = this->VarTree.Root();
  730. pos->Root = this->VarTree.Root();
  731. return { this, pos };
  732. }
  733. cmStateSnapshot cmState::CreateBuildsystemDirectorySnapshot(
  734. cmStateSnapshot const& originSnapshot)
  735. {
  736. assert(originSnapshot.IsValid());
  737. cmStateDetail::PositionType pos =
  738. this->SnapshotData.Push(originSnapshot.Position);
  739. pos->DirectoryParent = originSnapshot.Position;
  740. pos->ScopeParent = originSnapshot.Position;
  741. pos->SnapshotType = cmStateEnums::BuildsystemDirectoryType;
  742. pos->Keep = true;
  743. pos->BuildSystemDirectory = this->BuildsystemDirectory.Push(
  744. originSnapshot.Position->BuildSystemDirectory);
  745. pos->ExecutionListFile =
  746. this->ExecutionListFiles.Push(originSnapshot.Position->ExecutionListFile);
  747. pos->BuildSystemDirectory->CurrentScope = pos;
  748. pos->Policies = originSnapshot.Position->Policies;
  749. pos->PolicyRoot = originSnapshot.Position->Policies;
  750. pos->PolicyScope = originSnapshot.Position->Policies;
  751. assert(pos->Policies.IsValid());
  752. assert(pos->PolicyRoot.IsValid());
  753. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  754. pos->Parent = origin;
  755. pos->Root = origin;
  756. pos->Vars = this->VarTree.Push(origin);
  757. cmStateSnapshot snapshot = cmStateSnapshot(this, pos);
  758. originSnapshot.Position->BuildSystemDirectory->Children.push_back(snapshot);
  759. snapshot.SetDefaultDefinitions();
  760. snapshot.InitializeFromParent();
  761. snapshot.SetDirectoryDefinitions();
  762. return snapshot;
  763. }
  764. cmStateSnapshot cmState::CreateDeferCallSnapshot(
  765. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  766. {
  767. cmStateDetail::PositionType pos =
  768. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  769. pos->SnapshotType = cmStateEnums::DeferCallType;
  770. pos->Keep = false;
  771. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  772. originSnapshot.Position->ExecutionListFile, fileName);
  773. assert(originSnapshot.Position->Vars.IsValid());
  774. pos->BuildSystemDirectory->CurrentScope = pos;
  775. pos->PolicyScope = originSnapshot.Position->Policies;
  776. return { this, pos };
  777. }
  778. cmStateSnapshot cmState::CreateFunctionCallSnapshot(
  779. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  780. {
  781. cmStateDetail::PositionType pos =
  782. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  783. pos->ScopeParent = originSnapshot.Position;
  784. pos->SnapshotType = cmStateEnums::FunctionCallType;
  785. pos->Keep = false;
  786. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  787. originSnapshot.Position->ExecutionListFile, fileName);
  788. pos->BuildSystemDirectory->CurrentScope = pos;
  789. pos->PolicyScope = originSnapshot.Position->Policies;
  790. assert(originSnapshot.Position->Vars.IsValid());
  791. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  792. pos->Parent = origin;
  793. pos->Vars = this->VarTree.Push(origin);
  794. return { this, pos };
  795. }
  796. cmStateSnapshot cmState::CreateMacroCallSnapshot(
  797. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  798. {
  799. cmStateDetail::PositionType pos =
  800. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  801. pos->SnapshotType = cmStateEnums::MacroCallType;
  802. pos->Keep = false;
  803. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  804. originSnapshot.Position->ExecutionListFile, fileName);
  805. assert(originSnapshot.Position->Vars.IsValid());
  806. pos->BuildSystemDirectory->CurrentScope = pos;
  807. pos->PolicyScope = originSnapshot.Position->Policies;
  808. return { this, pos };
  809. }
  810. cmStateSnapshot cmState::CreateIncludeFileSnapshot(
  811. cmStateSnapshot const& originSnapshot, std::string const& fileName)
  812. {
  813. cmStateDetail::PositionType pos =
  814. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  815. pos->SnapshotType = cmStateEnums::IncludeFileType;
  816. pos->Keep = true;
  817. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  818. originSnapshot.Position->ExecutionListFile, fileName);
  819. assert(originSnapshot.Position->Vars.IsValid());
  820. pos->BuildSystemDirectory->CurrentScope = pos;
  821. pos->PolicyScope = originSnapshot.Position->Policies;
  822. return { this, pos };
  823. }
  824. cmStateSnapshot cmState::CreateVariableScopeSnapshot(
  825. cmStateSnapshot const& originSnapshot)
  826. {
  827. cmStateDetail::PositionType pos =
  828. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  829. pos->ScopeParent = originSnapshot.Position;
  830. pos->SnapshotType = cmStateEnums::VariableScopeType;
  831. pos->Keep = false;
  832. pos->BuildSystemDirectory->CurrentScope = pos;
  833. pos->PolicyScope = originSnapshot.Position->Policies;
  834. assert(originSnapshot.Position->Vars.IsValid());
  835. cmLinkedTree<cmDefinitions>::iterator origin = originSnapshot.Position->Vars;
  836. pos->Parent = origin;
  837. pos->Vars = this->VarTree.Push(origin);
  838. assert(pos->Vars.IsValid());
  839. return { this, pos };
  840. }
  841. cmStateSnapshot cmState::CreateInlineListFileSnapshot(
  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::InlineListFileType;
  847. pos->Keep = true;
  848. pos->ExecutionListFile = this->ExecutionListFiles.Push(
  849. originSnapshot.Position->ExecutionListFile, fileName);
  850. pos->BuildSystemDirectory->CurrentScope = pos;
  851. pos->PolicyScope = originSnapshot.Position->Policies;
  852. return { this, pos };
  853. }
  854. cmStateSnapshot cmState::CreatePolicyScopeSnapshot(
  855. cmStateSnapshot const& originSnapshot)
  856. {
  857. cmStateDetail::PositionType pos =
  858. this->SnapshotData.Push(originSnapshot.Position, *originSnapshot.Position);
  859. pos->SnapshotType = cmStateEnums::PolicyScopeType;
  860. pos->Keep = false;
  861. pos->BuildSystemDirectory->CurrentScope = pos;
  862. pos->PolicyScope = originSnapshot.Position->Policies;
  863. return { this, pos };
  864. }
  865. cmStateSnapshot cmState::Pop(cmStateSnapshot const& originSnapshot)
  866. {
  867. cmStateDetail::PositionType pos = originSnapshot.Position;
  868. cmStateDetail::PositionType prevPos = pos;
  869. ++prevPos;
  870. prevPos->IncludeDirectoryPosition =
  871. prevPos->BuildSystemDirectory->IncludeDirectories.size();
  872. prevPos->CompileDefinitionsPosition =
  873. prevPos->BuildSystemDirectory->CompileDefinitions.size();
  874. prevPos->CompileOptionsPosition =
  875. prevPos->BuildSystemDirectory->CompileOptions.size();
  876. prevPos->LinkOptionsPosition =
  877. prevPos->BuildSystemDirectory->LinkOptions.size();
  878. prevPos->LinkDirectoriesPosition =
  879. prevPos->BuildSystemDirectory->LinkDirectories.size();
  880. prevPos->BuildSystemDirectory->CurrentScope = prevPos;
  881. if (!pos->Keep && this->SnapshotData.IsLast(pos)) {
  882. if (pos->Vars != prevPos->Vars) {
  883. assert(this->VarTree.IsLast(pos->Vars));
  884. this->VarTree.Pop(pos->Vars);
  885. }
  886. if (pos->ExecutionListFile != prevPos->ExecutionListFile) {
  887. assert(this->ExecutionListFiles.IsLast(pos->ExecutionListFile));
  888. this->ExecutionListFiles.Pop(pos->ExecutionListFile);
  889. }
  890. this->SnapshotData.Pop(pos);
  891. }
  892. return { this, prevPos };
  893. }
  894. static bool ParseEntryWithoutType(const std::string& entry, std::string& var,
  895. std::string& value)
  896. {
  897. // input line is: key=value
  898. static cmsys::RegularExpression reg(
  899. "^([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  900. // input line is: "key"=value
  901. static cmsys::RegularExpression regQuoted(
  902. "^\"([^\"]*)\"=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  903. bool flag = false;
  904. if (regQuoted.find(entry)) {
  905. var = regQuoted.match(1);
  906. value = regQuoted.match(2);
  907. flag = true;
  908. } else if (reg.find(entry)) {
  909. var = reg.match(1);
  910. value = reg.match(2);
  911. flag = true;
  912. }
  913. // if value is enclosed in single quotes ('foo') then remove them
  914. // it is used to enclose trailing space or tab
  915. if (flag && value.size() >= 2 && value.front() == '\'' &&
  916. value.back() == '\'') {
  917. value = value.substr(1, value.size() - 2);
  918. }
  919. return flag;
  920. }
  921. bool cmState::ParseCacheEntry(const std::string& entry, std::string& var,
  922. std::string& value,
  923. cmStateEnums::CacheEntryType& type)
  924. {
  925. // input line is: key:type=value
  926. static cmsys::RegularExpression reg(
  927. "^([^=:]*):([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  928. // input line is: "key":type=value
  929. static cmsys::RegularExpression regQuoted(
  930. "^\"([^\"]*)\":([^=]*)=(.*[^\r\t ]|[\r\t ]*)[\r\t ]*$");
  931. bool flag = false;
  932. if (regQuoted.find(entry)) {
  933. var = regQuoted.match(1);
  934. type = cmState::StringToCacheEntryType(regQuoted.match(2));
  935. value = regQuoted.match(3);
  936. flag = true;
  937. } else if (reg.find(entry)) {
  938. var = reg.match(1);
  939. type = cmState::StringToCacheEntryType(reg.match(2));
  940. value = reg.match(3);
  941. flag = true;
  942. }
  943. // if value is enclosed in single quotes ('foo') then remove them
  944. // it is used to enclose trailing space or tab
  945. if (flag && value.size() >= 2 && value.front() == '\'' &&
  946. value.back() == '\'') {
  947. value = value.substr(1, value.size() - 2);
  948. }
  949. if (!flag) {
  950. return ParseEntryWithoutType(entry, var, value);
  951. }
  952. return flag;
  953. }
  954. cmState::Command cmState::GetDependencyProviderCommand(
  955. cmDependencyProvider::Method method) const
  956. {
  957. return (this->DependencyProvider &&
  958. this->DependencyProvider->SupportsMethod(method))
  959. ? this->GetCommand(this->DependencyProvider->GetCommand())
  960. : Command{};
  961. }