cmStateSnapshot.cxx 15 KB

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