cmState.cxx 33 KB

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