cmState.cxx 33 KB

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