cmStateSnapshot.cxx 15 KB

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