cmStateSnapshot.cxx 14 KB

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