cmStateSnapshot.cxx 14 KB

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