cmStateSnapshot.cxx 15 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 <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 { 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. std::vector<std::string>::const_iterator parentBegin = parentContent.begin();
  240. std::vector<std::string>::const_iterator parentEnd = parentContent.end();
  241. std::vector<std::string>::const_reverse_iterator parentRbegin =
  242. cmMakeReverseIterator(parentEnd);
  243. std::vector<std::string>::const_reverse_iterator parentRend =
  244. parentContent.rend();
  245. parentRbegin = std::find(parentRbegin, parentRend, cmPropertySentinal);
  246. std::vector<std::string>::const_iterator parentIt = parentRbegin.base();
  247. thisContent = std::vector<std::string>(parentIt, parentEnd);
  248. std::vector<cmListFileBacktrace>::const_iterator btIt =
  249. parentBacktraces.begin() + std::distance(parentBegin, parentIt);
  250. std::vector<cmListFileBacktrace>::const_iterator btEnd =
  251. parentBacktraces.end();
  252. thisBacktraces = std::vector<cmListFileBacktrace>(btIt, btEnd);
  253. contentEndPosition = thisContent.size();
  254. }
  255. void cmStateSnapshot::SetDefaultDefinitions()
  256. {
  257. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  258. With CMake must separate between target and host platform. In most cases
  259. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  260. additional set of variables for the host system is required ->
  261. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  262. WIN32, UNIX and APPLE are now set in the platform files in
  263. Modules/Platforms/.
  264. To keep cmake scripts (-P) and custom language and compiler modules
  265. working, these variables are still also set here in this place, but they
  266. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  267. files are executed. */
  268. #if defined(_WIN32)
  269. this->SetDefinition("WIN32", "1");
  270. this->SetDefinition("CMAKE_HOST_WIN32", "1");
  271. this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", "Windows");
  272. #else
  273. this->SetDefinition("UNIX", "1");
  274. this->SetDefinition("CMAKE_HOST_UNIX", "1");
  275. struct utsname uts_name;
  276. if (uname(&uts_name) >= 0) {
  277. this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", uts_name.sysname);
  278. }
  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. const char* include_regex =
  356. parent->BuildSystemDirectory->Properties.GetPropertyValue(
  357. "INCLUDE_REGULAR_EXPRESSION");
  358. this->Position->BuildSystemDirectory->Properties.SetProperty(
  359. "INCLUDE_REGULAR_EXPRESSION", include_regex);
  360. }
  361. cmState* cmStateSnapshot::GetState() const
  362. {
  363. return this->State;
  364. }
  365. cmStateDirectory cmStateSnapshot::GetDirectory() const
  366. {
  367. return { this->Position->BuildSystemDirectory, *this };
  368. }
  369. void cmStateSnapshot::SetProjectName(const std::string& name)
  370. {
  371. this->Position->BuildSystemDirectory->ProjectName = name;
  372. }
  373. std::string cmStateSnapshot::GetProjectName() const
  374. {
  375. return this->Position->BuildSystemDirectory->ProjectName;
  376. }
  377. void cmStateSnapshot::InitializeFromParent_ForSubdirsCommand()
  378. {
  379. std::string currentSrcDir = *this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR");
  380. std::string currentBinDir = *this->GetDefinition("CMAKE_CURRENT_BINARY_DIR");
  381. this->InitializeFromParent();
  382. this->SetDefinition("CMAKE_SOURCE_DIR", this->State->GetSourceDirectory());
  383. this->SetDefinition("CMAKE_BINARY_DIR", this->State->GetBinaryDirectory());
  384. this->SetDefinition("CMAKE_CURRENT_SOURCE_DIR", currentSrcDir);
  385. this->SetDefinition("CMAKE_CURRENT_BINARY_DIR", currentBinDir);
  386. }
  387. bool cmStateSnapshot::StrictWeakOrder::operator()(
  388. const cmStateSnapshot& lhs, const cmStateSnapshot& rhs) const
  389. {
  390. return lhs.Position.StrictWeakOrdered(rhs.Position);
  391. }
  392. bool operator==(const cmStateSnapshot& lhs, const cmStateSnapshot& rhs)
  393. {
  394. return lhs.Position == rhs.Position;
  395. }
  396. bool operator!=(const cmStateSnapshot& lhs, const cmStateSnapshot& rhs)
  397. {
  398. return lhs.Position != rhs.Position;
  399. }