cmStateSnapshot.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 "cmStateSnapshot.h"
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <string>
  7. #include <cm/iterator>
  8. #include "cmDefinitions.h"
  9. #include "cmListFileCache.h"
  10. #include "cmProperty.h"
  11. #include "cmPropertyMap.h"
  12. #include "cmState.h"
  13. #include "cmStateDirectory.h"
  14. #include "cmStatePrivate.h"
  15. #include "cmSystemTools.h"
  16. #include "cmVersion.h"
  17. cmStateSnapshot::cmStateSnapshot(cmState* state)
  18. : State(state)
  19. {
  20. }
  21. std::vector<cmStateSnapshot> cmStateSnapshot::GetChildren()
  22. {
  23. return this->Position->BuildSystemDirectory->Children;
  24. }
  25. cmStateSnapshot::cmStateSnapshot(cmState* state,
  26. cmStateDetail::PositionType position)
  27. : State(state)
  28. , Position(position)
  29. {
  30. }
  31. cmStateEnums::SnapshotType cmStateSnapshot::GetType() const
  32. {
  33. return this->Position->SnapshotType;
  34. }
  35. void cmStateSnapshot::SetListFile(const std::string& listfile)
  36. {
  37. *this->Position->ExecutionListFile = listfile;
  38. }
  39. std::string const& cmStateSnapshot::GetExecutionListFile() const
  40. {
  41. return *this->Position->ExecutionListFile;
  42. }
  43. bool cmStateSnapshot::IsValid() const
  44. {
  45. return this->State && this->Position.IsValid()
  46. ? this->Position != this->State->SnapshotData.Root()
  47. : false;
  48. }
  49. cmStateSnapshot cmStateSnapshot::GetBuildsystemDirectory() const
  50. {
  51. return { this->State, this->Position->BuildSystemDirectory->DirectoryEnd };
  52. }
  53. cmStateSnapshot cmStateSnapshot::GetBuildsystemDirectoryParent() const
  54. {
  55. cmStateSnapshot snapshot;
  56. if (!this->State || this->Position == this->State->SnapshotData.Root()) {
  57. return snapshot;
  58. }
  59. cmStateDetail::PositionType parentPos = this->Position->DirectoryParent;
  60. if (parentPos != this->State->SnapshotData.Root()) {
  61. snapshot = cmStateSnapshot(this->State,
  62. parentPos->BuildSystemDirectory->DirectoryEnd);
  63. }
  64. return snapshot;
  65. }
  66. cmStateSnapshot cmStateSnapshot::GetCallStackParent() const
  67. {
  68. assert(this->State);
  69. assert(this->Position != this->State->SnapshotData.Root());
  70. cmStateSnapshot snapshot;
  71. cmStateDetail::PositionType parentPos = this->Position;
  72. while (parentPos->SnapshotType == cmStateEnums::PolicyScopeType ||
  73. parentPos->SnapshotType == cmStateEnums::VariableScopeType) {
  74. ++parentPos;
  75. }
  76. if (parentPos->SnapshotType == cmStateEnums::BuildsystemDirectoryType ||
  77. parentPos->SnapshotType == cmStateEnums::BaseType) {
  78. return snapshot;
  79. }
  80. ++parentPos;
  81. while (parentPos->SnapshotType == cmStateEnums::PolicyScopeType ||
  82. parentPos->SnapshotType == cmStateEnums::VariableScopeType) {
  83. ++parentPos;
  84. }
  85. if (parentPos == this->State->SnapshotData.Root()) {
  86. return snapshot;
  87. }
  88. snapshot = cmStateSnapshot(this->State, parentPos);
  89. return snapshot;
  90. }
  91. cmStateSnapshot cmStateSnapshot::GetCallStackBottom() const
  92. {
  93. assert(this->State);
  94. assert(this->Position != this->State->SnapshotData.Root());
  95. cmStateDetail::PositionType pos = this->Position;
  96. while (pos->SnapshotType != cmStateEnums::BaseType &&
  97. pos->SnapshotType != cmStateEnums::BuildsystemDirectoryType &&
  98. pos != this->State->SnapshotData.Root()) {
  99. ++pos;
  100. }
  101. return { this->State, pos };
  102. }
  103. void cmStateSnapshot::PushPolicy(cmPolicies::PolicyMap const& entry, bool weak)
  104. {
  105. cmStateDetail::PositionType pos = this->Position;
  106. pos->Policies = this->State->PolicyStack.Push(
  107. pos->Policies, cmStateDetail::PolicyStackEntry(entry, weak));
  108. }
  109. bool cmStateSnapshot::PopPolicy()
  110. {
  111. cmStateDetail::PositionType pos = this->Position;
  112. if (pos->Policies == pos->PolicyScope) {
  113. return false;
  114. }
  115. pos->Policies = this->State->PolicyStack.Pop(pos->Policies);
  116. return true;
  117. }
  118. bool cmStateSnapshot::CanPopPolicyScope()
  119. {
  120. return this->Position->Policies != this->Position->PolicyScope;
  121. }
  122. void cmStateSnapshot::SetPolicy(cmPolicies::PolicyID id,
  123. cmPolicies::PolicyStatus status)
  124. {
  125. // Update the policy stack from the top to the top-most strong entry.
  126. bool previous_was_weak = true;
  127. for (cmLinkedTree<cmStateDetail::PolicyStackEntry>::iterator psi =
  128. this->Position->Policies;
  129. previous_was_weak && psi != this->Position->PolicyRoot; ++psi) {
  130. psi->Set(id, status);
  131. previous_was_weak = psi->Weak;
  132. }
  133. }
  134. cmPolicies::PolicyStatus cmStateSnapshot::GetPolicy(cmPolicies::PolicyID id,
  135. bool parent_scope) const
  136. {
  137. cmPolicies::PolicyStatus status = cmPolicies::GetPolicyStatus(id);
  138. if (status == cmPolicies::REQUIRED_ALWAYS ||
  139. status == cmPolicies::REQUIRED_IF_USED) {
  140. return status;
  141. }
  142. cmLinkedTree<cmStateDetail::BuildsystemDirectoryStateType>::iterator dir =
  143. this->Position->BuildSystemDirectory;
  144. while (true) {
  145. assert(dir.IsValid());
  146. cmLinkedTree<cmStateDetail::PolicyStackEntry>::iterator leaf =
  147. dir->DirectoryEnd->Policies;
  148. cmLinkedTree<cmStateDetail::PolicyStackEntry>::iterator root =
  149. dir->DirectoryEnd->PolicyRoot;
  150. for (; leaf != root; ++leaf) {
  151. if (parent_scope) {
  152. parent_scope = false;
  153. continue;
  154. }
  155. if (leaf->IsDefined(id)) {
  156. status = leaf->Get(id);
  157. return status;
  158. }
  159. }
  160. cmStateDetail::PositionType e = dir->DirectoryEnd;
  161. cmStateDetail::PositionType p = e->DirectoryParent;
  162. if (p == this->State->SnapshotData.Root()) {
  163. break;
  164. }
  165. dir = p->BuildSystemDirectory;
  166. }
  167. return status;
  168. }
  169. bool cmStateSnapshot::HasDefinedPolicyCMP0011()
  170. {
  171. return !this->Position->Policies->IsEmpty();
  172. }
  173. cmProp cmStateSnapshot::GetDefinition(std::string const& name) const
  174. {
  175. assert(this->Position->Vars.IsValid());
  176. return cmDefinitions::Get(name, this->Position->Vars, this->Position->Root);
  177. }
  178. bool cmStateSnapshot::IsInitialized(std::string const& name) const
  179. {
  180. return cmDefinitions::HasKey(name, this->Position->Vars,
  181. this->Position->Root);
  182. }
  183. void cmStateSnapshot::SetDefinition(std::string const& name,
  184. cm::string_view value)
  185. {
  186. this->Position->Vars->Set(name, value);
  187. }
  188. void cmStateSnapshot::RemoveDefinition(std::string const& name)
  189. {
  190. this->Position->Vars->Unset(name);
  191. }
  192. std::vector<std::string> cmStateSnapshot::ClosureKeys() const
  193. {
  194. return cmDefinitions::ClosureKeys(this->Position->Vars,
  195. this->Position->Root);
  196. }
  197. bool cmStateSnapshot::RaiseScope(std::string const& var, const char* varDef)
  198. {
  199. if (this->Position->ScopeParent == this->Position->DirectoryParent) {
  200. cmStateSnapshot parentDir = this->GetBuildsystemDirectoryParent();
  201. if (!parentDir.IsValid()) {
  202. return false;
  203. }
  204. // Update the definition in the parent directory top scope. This
  205. // directory's scope was initialized by the closure of the parent
  206. // scope, so we do not need to localize the definition first.
  207. if (varDef) {
  208. parentDir.SetDefinition(var, varDef);
  209. } else {
  210. parentDir.RemoveDefinition(var);
  211. }
  212. return true;
  213. }
  214. // First localize the definition in the current scope.
  215. cmDefinitions::Raise(var, this->Position->Vars, this->Position->Root);
  216. // Now update the definition in the parent scope.
  217. if (varDef) {
  218. this->Position->Parent->Set(var, varDef);
  219. } else {
  220. this->Position->Parent->Unset(var);
  221. }
  222. return true;
  223. }
  224. template <typename T, typename U, typename V>
  225. void InitializeContentFromParent(T& parentContent, T& thisContent,
  226. U& parentBacktraces, U& thisBacktraces,
  227. V& contentEndPosition)
  228. {
  229. auto parentBegin = parentContent.begin();
  230. auto parentEnd = parentContent.end();
  231. auto parentRbegin = cm::make_reverse_iterator(parentEnd);
  232. auto parentRend = parentContent.rend();
  233. parentRbegin = std::find(parentRbegin, parentRend, cmPropertySentinal);
  234. auto parentIt = parentRbegin.base();
  235. thisContent = std::vector<std::string>(parentIt, parentEnd);
  236. auto btIt = parentBacktraces.begin() + std::distance(parentBegin, parentIt);
  237. auto btEnd = parentBacktraces.end();
  238. thisBacktraces = std::vector<cmListFileBacktrace>(btIt, btEnd);
  239. contentEndPosition = thisContent.size();
  240. }
  241. void cmStateSnapshot::SetDefaultDefinitions()
  242. {
  243. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  244. With CMake must separate between target and host platform. In most cases
  245. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  246. additional set of variables for the host system is required ->
  247. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  248. WIN32, UNIX and APPLE are now set in the platform files in
  249. Modules/Platforms/.
  250. To keep cmake scripts (-P) and custom language and compiler modules
  251. working, these variables are still also set here in this place, but they
  252. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  253. files are executed. */
  254. cm::string_view hostSystemName = cmSystemTools::GetSystemName();
  255. this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", hostSystemName);
  256. if (hostSystemName == "Windows") {
  257. this->SetDefinition("WIN32", "1");
  258. this->SetDefinition("CMAKE_HOST_WIN32", "1");
  259. } else {
  260. this->SetDefinition("UNIX", "1");
  261. this->SetDefinition("CMAKE_HOST_UNIX", "1");
  262. }
  263. #if defined(__CYGWIN__)
  264. std::string legacy;
  265. if (cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32", legacy) &&
  266. cmIsOn(legacy)) {
  267. this->SetDefinition("WIN32", "1");
  268. this->SetDefinition("CMAKE_HOST_WIN32", "1");
  269. }
  270. #endif
  271. #if defined(__APPLE__)
  272. this->SetDefinition("APPLE", "1");
  273. this->SetDefinition("CMAKE_HOST_APPLE", "1");
  274. #endif
  275. #if defined(__sun__)
  276. this->SetDefinition("CMAKE_HOST_SOLARIS", "1");
  277. #endif
  278. this->SetDefinition("CMAKE_MAJOR_VERSION",
  279. std::to_string(cmVersion::GetMajorVersion()));
  280. this->SetDefinition("CMAKE_MINOR_VERSION",
  281. std::to_string(cmVersion::GetMinorVersion()));
  282. this->SetDefinition("CMAKE_PATCH_VERSION",
  283. std::to_string(cmVersion::GetPatchVersion()));
  284. this->SetDefinition("CMAKE_TWEAK_VERSION",
  285. std::to_string(cmVersion::GetTweakVersion()));
  286. this->SetDefinition("CMAKE_VERSION", cmVersion::GetCMakeVersion());
  287. this->SetDefinition("CMAKE_FILES_DIRECTORY", "/CMakeFiles");
  288. // Setup the default include file regular expression (match everything).
  289. this->Position->BuildSystemDirectory->Properties.SetProperty(
  290. "INCLUDE_REGULAR_EXPRESSION", "^.*$");
  291. }
  292. void cmStateSnapshot::SetDirectoryDefinitions()
  293. {
  294. this->SetDefinition("CMAKE_SOURCE_DIR", this->State->GetSourceDirectory());
  295. this->SetDefinition("CMAKE_CURRENT_SOURCE_DIR",
  296. this->State->GetSourceDirectory());
  297. this->SetDefinition("CMAKE_BINARY_DIR", this->State->GetBinaryDirectory());
  298. this->SetDefinition("CMAKE_CURRENT_BINARY_DIR",
  299. this->State->GetBinaryDirectory());
  300. }
  301. void cmStateSnapshot::InitializeFromParent()
  302. {
  303. cmStateDetail::PositionType parent = this->Position->DirectoryParent;
  304. assert(this->Position->Vars.IsValid());
  305. assert(parent->Vars.IsValid());
  306. *this->Position->Vars =
  307. cmDefinitions::MakeClosure(parent->Vars, parent->Root);
  308. InitializeContentFromParent(
  309. parent->BuildSystemDirectory->IncludeDirectories,
  310. this->Position->BuildSystemDirectory->IncludeDirectories,
  311. parent->BuildSystemDirectory->IncludeDirectoryBacktraces,
  312. this->Position->BuildSystemDirectory->IncludeDirectoryBacktraces,
  313. this->Position->IncludeDirectoryPosition);
  314. InitializeContentFromParent(
  315. parent->BuildSystemDirectory->CompileDefinitions,
  316. this->Position->BuildSystemDirectory->CompileDefinitions,
  317. parent->BuildSystemDirectory->CompileDefinitionsBacktraces,
  318. this->Position->BuildSystemDirectory->CompileDefinitionsBacktraces,
  319. this->Position->CompileDefinitionsPosition);
  320. InitializeContentFromParent(
  321. parent->BuildSystemDirectory->CompileOptions,
  322. this->Position->BuildSystemDirectory->CompileOptions,
  323. parent->BuildSystemDirectory->CompileOptionsBacktraces,
  324. this->Position->BuildSystemDirectory->CompileOptionsBacktraces,
  325. this->Position->CompileOptionsPosition);
  326. InitializeContentFromParent(
  327. parent->BuildSystemDirectory->LinkOptions,
  328. this->Position->BuildSystemDirectory->LinkOptions,
  329. parent->BuildSystemDirectory->LinkOptionsBacktraces,
  330. this->Position->BuildSystemDirectory->LinkOptionsBacktraces,
  331. this->Position->LinkOptionsPosition);
  332. InitializeContentFromParent(
  333. parent->BuildSystemDirectory->LinkDirectories,
  334. this->Position->BuildSystemDirectory->LinkDirectories,
  335. parent->BuildSystemDirectory->LinkDirectoriesBacktraces,
  336. this->Position->BuildSystemDirectory->LinkDirectoriesBacktraces,
  337. this->Position->LinkDirectoriesPosition);
  338. cmProp include_regex =
  339. parent->BuildSystemDirectory->Properties.GetPropertyValue(
  340. "INCLUDE_REGULAR_EXPRESSION");
  341. this->Position->BuildSystemDirectory->Properties.SetProperty(
  342. "INCLUDE_REGULAR_EXPRESSION", include_regex);
  343. }
  344. cmState* cmStateSnapshot::GetState() const
  345. {
  346. return this->State;
  347. }
  348. cmStateDirectory cmStateSnapshot::GetDirectory() const
  349. {
  350. return { this->Position->BuildSystemDirectory, *this };
  351. }
  352. void cmStateSnapshot::SetProjectName(const std::string& name)
  353. {
  354. this->Position->BuildSystemDirectory->ProjectName = name;
  355. }
  356. std::string cmStateSnapshot::GetProjectName() const
  357. {
  358. return this->Position->BuildSystemDirectory->ProjectName;
  359. }
  360. void cmStateSnapshot::InitializeFromParent_ForSubdirsCommand()
  361. {
  362. std::string currentSrcDir = *this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR");
  363. std::string currentBinDir = *this->GetDefinition("CMAKE_CURRENT_BINARY_DIR");
  364. this->InitializeFromParent();
  365. this->SetDefinition("CMAKE_SOURCE_DIR", this->State->GetSourceDirectory());
  366. this->SetDefinition("CMAKE_BINARY_DIR", this->State->GetBinaryDirectory());
  367. this->SetDefinition("CMAKE_CURRENT_SOURCE_DIR", currentSrcDir);
  368. this->SetDefinition("CMAKE_CURRENT_BINARY_DIR", currentBinDir);
  369. }
  370. bool cmStateSnapshot::StrictWeakOrder::operator()(
  371. const cmStateSnapshot& lhs, const cmStateSnapshot& rhs) const
  372. {
  373. return lhs.Position.StrictWeakOrdered(rhs.Position);
  374. }
  375. bool operator==(const cmStateSnapshot& lhs, const cmStateSnapshot& rhs)
  376. {
  377. return lhs.Position == rhs.Position;
  378. }
  379. bool operator!=(const cmStateSnapshot& lhs, const cmStateSnapshot& rhs)
  380. {
  381. return lhs.Position != rhs.Position;
  382. }