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 "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. // NOLINTNEXTLINE(readability-qualified-auto)
  93. auto const entry =
  94. std::find(cmCacheEntryTypes.begin(), cmCacheEntryTypes.end(), s);
  95. if (entry != cmCacheEntryTypes.end()) {
  96. type = static_cast<cmStateEnums::CacheEntryType>(
  97. entry - cmCacheEntryTypes.begin());
  98. return true;
  99. }
  100. return false;
  101. }
  102. bool cmState::IsCacheEntryType(std::string const& key)
  103. {
  104. return std::any_of(
  105. cmCacheEntryTypes.begin(), cmCacheEntryTypes.end(),
  106. [&key](std::string const& i) -> bool { return key == i; });
  107. }
  108. bool cmState::LoadCache(const std::string& path, bool internal,
  109. std::set<std::string>& excludes,
  110. std::set<std::string>& includes)
  111. {
  112. return this->CacheManager->LoadCache(path, internal, excludes, includes);
  113. }
  114. bool cmState::SaveCache(const std::string& path, cmMessenger* messenger)
  115. {
  116. return this->CacheManager->SaveCache(path, messenger);
  117. }
  118. bool cmState::DeleteCache(const std::string& path)
  119. {
  120. return this->CacheManager->DeleteCache(path);
  121. }
  122. bool cmState::IsCacheLoaded() const
  123. {
  124. return this->CacheManager->IsCacheLoaded();
  125. }
  126. std::vector<std::string> cmState::GetCacheEntryKeys() const
  127. {
  128. return this->CacheManager->GetCacheEntryKeys();
  129. }
  130. cmValue cmState::GetCacheEntryValue(std::string const& key) const
  131. {
  132. return this->CacheManager->GetCacheEntryValue(key);
  133. }
  134. std::string cmState::GetSafeCacheEntryValue(std::string const& key) const
  135. {
  136. if (cmValue val = this->GetCacheEntryValue(key)) {
  137. return *val;
  138. }
  139. return std::string();
  140. }
  141. cmValue cmState::GetInitializedCacheValue(std::string const& key) const
  142. {
  143. return this->CacheManager->GetInitializedCacheValue(key);
  144. }
  145. cmStateEnums::CacheEntryType cmState::GetCacheEntryType(
  146. std::string const& key) const
  147. {
  148. return this->CacheManager->GetCacheEntryType(key);
  149. }
  150. void cmState::SetCacheEntryValue(std::string const& key,
  151. std::string const& value)
  152. {
  153. this->CacheManager->SetCacheEntryValue(key, value);
  154. }
  155. void cmState::SetCacheEntryProperty(std::string const& key,
  156. std::string const& propertyName,
  157. std::string const& value)
  158. {
  159. this->CacheManager->SetCacheEntryProperty(key, propertyName, value);
  160. }
  161. void cmState::SetCacheEntryBoolProperty(std::string const& key,
  162. std::string const& propertyName,
  163. bool value)
  164. {
  165. this->CacheManager->SetCacheEntryBoolProperty(key, propertyName, value);
  166. }
  167. std::vector<std::string> cmState::GetCacheEntryPropertyList(
  168. const std::string& key)
  169. {
  170. return this->CacheManager->GetCacheEntryPropertyList(key);
  171. }
  172. cmValue cmState::GetCacheEntryProperty(std::string const& key,
  173. std::string const& propertyName)
  174. {
  175. return this->CacheManager->GetCacheEntryProperty(key, propertyName);
  176. }
  177. bool cmState::GetCacheEntryPropertyAsBool(std::string const& key,
  178. std::string const& propertyName)
  179. {
  180. return this->CacheManager->GetCacheEntryPropertyAsBool(key, propertyName);
  181. }
  182. void cmState::AddCacheEntry(const std::string& key, cmValue value,
  183. const std::string& helpString,
  184. cmStateEnums::CacheEntryType type)
  185. {
  186. this->CacheManager->AddCacheEntry(key, value, helpString, type);
  187. }
  188. bool cmState::DoWriteGlobVerifyTarget() const
  189. {
  190. return this->GlobVerificationManager->DoWriteVerifyTarget();
  191. }
  192. std::string const& cmState::GetGlobVerifyScript() const
  193. {
  194. return this->GlobVerificationManager->GetVerifyScript();
  195. }
  196. std::string const& cmState::GetGlobVerifyStamp() const
  197. {
  198. return this->GlobVerificationManager->GetVerifyStamp();
  199. }
  200. bool cmState::SaveVerificationScript(const std::string& path,
  201. cmMessenger* messenger)
  202. {
  203. return this->GlobVerificationManager->SaveVerificationScript(path,
  204. messenger);
  205. }
  206. void cmState::AddGlobCacheEntry(
  207. bool recurse, bool listDirectories, bool followSymlinks,
  208. const std::string& relative, const std::string& expression,
  209. const std::vector<std::string>& files, const std::string& variable,
  210. cmListFileBacktrace const& backtrace, cmMessenger* messenger)
  211. {
  212. this->GlobVerificationManager->AddCacheEntry(
  213. recurse, listDirectories, followSymlinks, relative, expression, files,
  214. variable, backtrace, messenger);
  215. }
  216. void cmState::RemoveCacheEntry(std::string const& key)
  217. {
  218. this->CacheManager->RemoveCacheEntry(key);
  219. }
  220. void cmState::AppendCacheEntryProperty(const std::string& key,
  221. const std::string& property,
  222. const std::string& value, bool asString)
  223. {
  224. this->CacheManager->AppendCacheEntryProperty(key, property, value, asString);
  225. }
  226. void cmState::RemoveCacheEntryProperty(std::string const& key,
  227. std::string const& propertyName)
  228. {
  229. this->CacheManager->RemoveCacheEntryProperty(key, propertyName);
  230. }
  231. cmStateSnapshot cmState::Reset()
  232. {
  233. this->GlobalProperties.Clear();
  234. this->PropertyDefinitions = {};
  235. this->GlobVerificationManager->Reset();
  236. cmStateDetail::PositionType pos = this->SnapshotData.Truncate();
  237. this->ExecutionListFiles.Truncate();
  238. {
  239. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator it =
  240. this->BuildsystemDirectory.Truncate();
  241. it->IncludeDirectories.clear();
  242. it->CompileDefinitions.clear();
  243. it->CompileOptions.clear();
  244. it->LinkOptions.clear();
  245. it->LinkDirectories.clear();
  246. it->CurrentScope = pos;
  247. it->NormalTargetNames.clear();
  248. it->ImportedTargetNames.clear();
  249. it->Properties.Clear();
  250. it->Children.clear();
  251. }
  252. this->PolicyStack.Clear();
  253. pos->Policies = this->PolicyStack.Root();
  254. pos->PolicyRoot = this->PolicyStack.Root();
  255. pos->PolicyScope = this->PolicyStack.Root();
  256. assert(pos->Policies.IsValid());
  257. assert(pos->PolicyRoot.IsValid());
  258. {
  259. std::string srcDir =
  260. *cmDefinitions::Get("CMAKE_SOURCE_DIR", pos->Vars, pos->Root);
  261. std::string binDir =
  262. *cmDefinitions::Get("CMAKE_BINARY_DIR", pos->Vars, pos->Root);
  263. this->VarTree.Clear();
  264. pos->Vars = this->VarTree.Push(this->VarTree.Root());
  265. pos->Parent = this->VarTree.Root();
  266. pos->Root = this->VarTree.Root();
  267. pos->Vars->Set("CMAKE_SOURCE_DIR", srcDir);
  268. pos->Vars->Set("CMAKE_BINARY_DIR", binDir);
  269. }
  270. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::DIRECTORY, "", "",
  271. true);
  272. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::DIRECTORY, "", "",
  273. true);
  274. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::DIRECTORY, "", "",
  275. true);
  276. this->DefineProperty("RULE_LAUNCH_COMPILE", cmProperty::TARGET, "", "",
  277. true);
  278. this->DefineProperty("RULE_LAUNCH_LINK", cmProperty::TARGET, "", "", true);
  279. this->DefineProperty("RULE_LAUNCH_CUSTOM", cmProperty::TARGET, "", "", true);
  280. return { this, pos };
  281. }
  282. void cmState::DefineProperty(const std::string& name,
  283. cmProperty::ScopeType scope,
  284. const std::string& ShortDescription,
  285. const std::string& FullDescription, bool chained,
  286. const std::string& initializeFromVariable)
  287. {
  288. this->PropertyDefinitions.DefineProperty(name, scope, ShortDescription,
  289. FullDescription, chained,
  290. initializeFromVariable);
  291. }
  292. cmPropertyDefinition const* cmState::GetPropertyDefinition(
  293. const std::string& name, cmProperty::ScopeType scope) const
  294. {
  295. return this->PropertyDefinitions.GetPropertyDefinition(name, scope);
  296. }
  297. bool cmState::IsPropertyChained(const std::string& name,
  298. cmProperty::ScopeType scope) const
  299. {
  300. if (const auto* def = this->GetPropertyDefinition(name, scope)) {
  301. return def->IsChained();
  302. }
  303. return false;
  304. }
  305. void cmState::SetLanguageEnabled(std::string const& l)
  306. {
  307. auto it = std::lower_bound(this->EnabledLanguages.begin(),
  308. this->EnabledLanguages.end(), l);
  309. if (it == this->EnabledLanguages.end() || *it != l) {
  310. this->EnabledLanguages.insert(it, l);
  311. }
  312. }
  313. bool cmState::GetLanguageEnabled(std::string const& l) const
  314. {
  315. return std::binary_search(this->EnabledLanguages.begin(),
  316. this->EnabledLanguages.end(), l);
  317. }
  318. std::vector<std::string> cmState::GetEnabledLanguages() const
  319. {
  320. return this->EnabledLanguages;
  321. }
  322. void cmState::SetEnabledLanguages(std::vector<std::string> const& langs)
  323. {
  324. this->EnabledLanguages = langs;
  325. }
  326. void cmState::ClearEnabledLanguages()
  327. {
  328. this->EnabledLanguages.clear();
  329. }
  330. bool cmState::GetIsGeneratorMultiConfig() const
  331. {
  332. return this->IsGeneratorMultiConfig;
  333. }
  334. void cmState::SetIsGeneratorMultiConfig(bool b)
  335. {
  336. this->IsGeneratorMultiConfig = b;
  337. }
  338. void cmState::AddBuiltinCommand(std::string const& name,
  339. std::unique_ptr<cmCommand> command)
  340. {
  341. this->AddBuiltinCommand(name, cmLegacyCommandWrapper(std::move(command)));
  342. }
  343. void cmState::AddBuiltinCommand(std::string const& name, Command command)
  344. {
  345. assert(name == cmSystemTools::LowerCase(name));
  346. assert(this->BuiltinCommands.find(name) == this->BuiltinCommands.end());
  347. this->BuiltinCommands.emplace(name, std::move(command));
  348. }
  349. static bool InvokeBuiltinCommand(cmState::BuiltinCommand command,
  350. std::vector<cmListFileArgument> const& args,
  351. cmExecutionStatus& status)
  352. {
  353. cmMakefile& mf = status.GetMakefile();
  354. std::vector<std::string> expandedArguments;
  355. if (!mf.ExpandArguments(args, expandedArguments)) {
  356. // There was an error expanding arguments. It was already
  357. // reported, so we can skip this command without error.
  358. return true;
  359. }
  360. return command(expandedArguments, status);
  361. }
  362. void cmState::AddBuiltinCommand(std::string const& name,
  363. BuiltinCommand command)
  364. {
  365. this->AddBuiltinCommand(
  366. name,
  367. [command](const std::vector<cmListFileArgument>& args,
  368. cmExecutionStatus& status) -> bool {
  369. return InvokeBuiltinCommand(command, args, status);
  370. });
  371. }
  372. void cmState::AddFlowControlCommand(std::string const& name, Command command)
  373. {
  374. this->FlowControlCommands.insert(name);
  375. this->AddBuiltinCommand(name, std::move(command));
  376. }
  377. void cmState::AddFlowControlCommand(std::string const& name,
  378. BuiltinCommand command)
  379. {
  380. this->FlowControlCommands.insert(name);
  381. this->AddBuiltinCommand(name, command);
  382. }
  383. void cmState::AddDisallowedCommand(std::string const& name,
  384. BuiltinCommand command,
  385. cmPolicies::PolicyID policy,
  386. const char* message)
  387. {
  388. this->AddBuiltinCommand(
  389. name,
  390. [command, policy, message](const std::vector<cmListFileArgument>& args,
  391. cmExecutionStatus& status) -> bool {
  392. cmMakefile& mf = status.GetMakefile();
  393. switch (mf.GetPolicyStatus(policy)) {
  394. case cmPolicies::WARN:
  395. mf.IssueMessage(MessageType::AUTHOR_WARNING,
  396. cmPolicies::GetPolicyWarning(policy));
  397. CM_FALLTHROUGH;
  398. case cmPolicies::OLD:
  399. break;
  400. case cmPolicies::REQUIRED_IF_USED:
  401. case cmPolicies::REQUIRED_ALWAYS:
  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::AddUnexpectedCommand(std::string const& name, const char* error)
  410. {
  411. this->AddBuiltinCommand(
  412. name,
  413. [name, error](std::vector<cmListFileArgument> const&,
  414. cmExecutionStatus& status) -> bool {
  415. cmValue versionValue =
  416. status.GetMakefile().GetDefinition("CMAKE_MINIMUM_REQUIRED_VERSION");
  417. if (name == "endif" &&
  418. (!versionValue || atof(versionValue->c_str()) <= 1.4)) {
  419. return true;
  420. }
  421. status.SetError(error);
  422. return false;
  423. });
  424. }
  425. void cmState::AddUnexpectedFlowControlCommand(std::string const& name,
  426. const char* error)
  427. {
  428. this->FlowControlCommands.insert(name);
  429. this->AddUnexpectedCommand(name, error);
  430. }
  431. bool cmState::AddScriptedCommand(std::string const& name, BT<Command> command,
  432. cmMakefile& mf)
  433. {
  434. std::string sName = cmSystemTools::LowerCase(name);
  435. if (this->FlowControlCommands.count(sName)) {
  436. mf.GetCMakeInstance()->IssueMessage(
  437. MessageType::FATAL_ERROR,
  438. cmStrCat("Built-in flow control command \"", sName,
  439. "\" cannot be overridden."),
  440. command.Backtrace);
  441. cmSystemTools::SetFatalErrorOccurred();
  442. return false;
  443. }
  444. // if the command already exists, give a new name to the old command.
  445. if (Command oldCmd = this->GetCommandByExactName(sName)) {
  446. this->ScriptedCommands["_" + sName] = oldCmd;
  447. }
  448. this->ScriptedCommands[sName] = std::move(command.Value);
  449. return true;
  450. }
  451. cmState::Command cmState::GetCommand(std::string const& name) const
  452. {
  453. return this->GetCommandByExactName(cmSystemTools::LowerCase(name));
  454. }
  455. cmState::Command cmState::GetCommandByExactName(std::string const& name) const
  456. {
  457. auto pos = this->ScriptedCommands.find(name);
  458. if (pos != this->ScriptedCommands.end()) {
  459. return pos->second;
  460. }
  461. pos = this->BuiltinCommands.find(name);
  462. if (pos != this->BuiltinCommands.end()) {
  463. return pos->second;
  464. }
  465. return nullptr;
  466. }
  467. std::vector<std::string> cmState::GetCommandNames() const
  468. {
  469. std::vector<std::string> commandNames;
  470. commandNames.reserve(this->BuiltinCommands.size() +
  471. this->ScriptedCommands.size());
  472. for (auto const& bc : this->BuiltinCommands) {
  473. commandNames.push_back(bc.first);
  474. }
  475. for (auto const& sc : this->ScriptedCommands) {
  476. commandNames.push_back(sc.first);
  477. }
  478. std::sort(commandNames.begin(), commandNames.end());
  479. commandNames.erase(std::unique(commandNames.begin(), commandNames.end()),
  480. commandNames.end());
  481. return commandNames;
  482. }
  483. void cmState::RemoveBuiltinCommand(std::string const& name)
  484. {
  485. assert(name == cmSystemTools::LowerCase(name));
  486. this->BuiltinCommands.erase(name);
  487. }
  488. void cmState::RemoveUserDefinedCommands()
  489. {
  490. this->ScriptedCommands.clear();
  491. }
  492. void cmState::SetGlobalProperty(const std::string& prop,
  493. const std::string& value)
  494. {
  495. this->GlobalProperties.SetProperty(prop, value);
  496. }
  497. void cmState::SetGlobalProperty(const std::string& prop, cmValue value)
  498. {
  499. this->GlobalProperties.SetProperty(prop, value);
  500. }
  501. void cmState::AppendGlobalProperty(const std::string& prop,
  502. const std::string& value, bool asString)
  503. {
  504. this->GlobalProperties.AppendProperty(prop, value, asString);
  505. }
  506. cmValue cmState::GetGlobalProperty(const std::string& prop)
  507. {
  508. if (prop == "CACHE_VARIABLES") {
  509. std::vector<std::string> cacheKeys = this->GetCacheEntryKeys();
  510. this->SetGlobalProperty("CACHE_VARIABLES", cmJoin(cacheKeys, ";"));
  511. } else if (prop == "COMMANDS") {
  512. std::vector<std::string> commands = this->GetCommandNames();
  513. this->SetGlobalProperty("COMMANDS", cmJoin(commands, ";"));
  514. } else if (prop == "IN_TRY_COMPILE") {
  515. this->SetGlobalProperty(
  516. "IN_TRY_COMPILE",
  517. this->StateProjectKind == ProjectKind::TryCompile ? "1" : "0");
  518. } else if (prop == "GENERATOR_IS_MULTI_CONFIG") {
  519. this->SetGlobalProperty("GENERATOR_IS_MULTI_CONFIG",
  520. this->IsGeneratorMultiConfig ? "1" : "0");
  521. } else if (prop == "ENABLED_LANGUAGES") {
  522. std::string langs;
  523. langs = cmJoin(this->EnabledLanguages, ";");
  524. this->SetGlobalProperty("ENABLED_LANGUAGES", langs);
  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. }