cmStateSnapshot.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #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(cmPolicies::PolicyID id,
  138. bool parent_scope) 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 (parent_scope) {
  155. parent_scope = false;
  156. continue;
  157. }
  158. if (leaf->IsDefined(id)) {
  159. status = leaf->Get(id);
  160. return status;
  161. }
  162. }
  163. cmStateDetail::PositionType e = dir->DirectoryEnd;
  164. cmStateDetail::PositionType p = e->DirectoryParent;
  165. if (p == this->State->SnapshotData.Root()) {
  166. break;
  167. }
  168. dir = p->BuildSystemDirectory;
  169. }
  170. return status;
  171. }
  172. bool cmStateSnapshot::HasDefinedPolicyCMP0011()
  173. {
  174. return !this->Position->Policies->IsEmpty();
  175. }
  176. const char* cmStateSnapshot::GetDefinition(std::string const& name) const
  177. {
  178. assert(this->Position->Vars.IsValid());
  179. return cmDefinitions::Get(name, this->Position->Vars, this->Position->Root);
  180. }
  181. bool cmStateSnapshot::IsInitialized(std::string const& name) const
  182. {
  183. return cmDefinitions::HasKey(name, this->Position->Vars,
  184. this->Position->Root);
  185. }
  186. void cmStateSnapshot::SetDefinition(std::string const& name,
  187. std::string const& value)
  188. {
  189. this->Position->Vars->Set(name, value.c_str());
  190. }
  191. void cmStateSnapshot::RemoveDefinition(std::string const& name)
  192. {
  193. this->Position->Vars->Set(name, nullptr);
  194. }
  195. std::vector<std::string> cmStateSnapshot::UnusedKeys() const
  196. {
  197. return this->Position->Vars->UnusedKeys();
  198. }
  199. std::vector<std::string> cmStateSnapshot::ClosureKeys() const
  200. {
  201. return cmDefinitions::ClosureKeys(this->Position->Vars,
  202. this->Position->Root);
  203. }
  204. bool cmStateSnapshot::RaiseScope(std::string const& var, const char* varDef)
  205. {
  206. if (this->Position->ScopeParent == this->Position->DirectoryParent) {
  207. cmStateSnapshot parentDir = this->GetBuildsystemDirectoryParent();
  208. if (!parentDir.IsValid()) {
  209. return false;
  210. }
  211. // Update the definition in the parent directory top scope. This
  212. // directory's scope was initialized by the closure of the parent
  213. // scope, so we do not need to localize the definition first.
  214. if (varDef) {
  215. parentDir.SetDefinition(var, varDef);
  216. } else {
  217. parentDir.RemoveDefinition(var);
  218. }
  219. return true;
  220. }
  221. // First localize the definition in the current scope.
  222. cmDefinitions::Raise(var, this->Position->Vars, this->Position->Root);
  223. // Now update the definition in the parent scope.
  224. this->Position->Parent->Set(var, varDef);
  225. return true;
  226. }
  227. template <typename T, typename U, typename V>
  228. void InitializeContentFromParent(T& parentContent, T& thisContent,
  229. U& parentBacktraces, U& thisBacktraces,
  230. V& contentEndPosition)
  231. {
  232. std::vector<std::string>::const_iterator parentBegin = parentContent.begin();
  233. std::vector<std::string>::const_iterator parentEnd = parentContent.end();
  234. std::vector<std::string>::const_reverse_iterator parentRbegin =
  235. cmMakeReverseIterator(parentEnd);
  236. std::vector<std::string>::const_reverse_iterator parentRend =
  237. parentContent.rend();
  238. parentRbegin = std::find(parentRbegin, parentRend, cmPropertySentinal);
  239. std::vector<std::string>::const_iterator parentIt = parentRbegin.base();
  240. thisContent = std::vector<std::string>(parentIt, parentEnd);
  241. std::vector<cmListFileBacktrace>::const_iterator btIt =
  242. parentBacktraces.begin() + std::distance(parentBegin, parentIt);
  243. std::vector<cmListFileBacktrace>::const_iterator btEnd =
  244. parentBacktraces.end();
  245. thisBacktraces = std::vector<cmListFileBacktrace>(btIt, btEnd);
  246. contentEndPosition = thisContent.size();
  247. }
  248. void cmStateSnapshot::SetDefaultDefinitions()
  249. {
  250. /* Up to CMake 2.4 here only WIN32, UNIX and APPLE were set.
  251. With CMake must separate between target and host platform. In most cases
  252. the tests for WIN32, UNIX and APPLE will be for the target system, so an
  253. additional set of variables for the host system is required ->
  254. CMAKE_HOST_WIN32, CMAKE_HOST_UNIX, CMAKE_HOST_APPLE.
  255. WIN32, UNIX and APPLE are now set in the platform files in
  256. Modules/Platforms/.
  257. To keep cmake scripts (-P) and custom language and compiler modules
  258. working, these variables are still also set here in this place, but they
  259. will be reset in CMakeSystemSpecificInformation.cmake before the platform
  260. files are executed. */
  261. #if defined(_WIN32)
  262. this->SetDefinition("WIN32", "1");
  263. this->SetDefinition("CMAKE_HOST_WIN32", "1");
  264. this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", "Windows");
  265. #else
  266. this->SetDefinition("UNIX", "1");
  267. this->SetDefinition("CMAKE_HOST_UNIX", "1");
  268. struct utsname uts_name;
  269. if (uname(&uts_name) >= 0) {
  270. this->SetDefinition("CMAKE_HOST_SYSTEM_NAME", uts_name.sysname);
  271. }
  272. #endif
  273. #if defined(__CYGWIN__)
  274. std::string legacy;
  275. if (cmSystemTools::GetEnv("CMAKE_LEGACY_CYGWIN_WIN32", legacy) &&
  276. cmSystemTools::IsOn(legacy.c_str())) {
  277. this->SetDefinition("WIN32", "1");
  278. this->SetDefinition("CMAKE_HOST_WIN32", "1");
  279. }
  280. #endif
  281. #if defined(__APPLE__)
  282. this->SetDefinition("APPLE", "1");
  283. this->SetDefinition("CMAKE_HOST_APPLE", "1");
  284. #endif
  285. #if defined(__sun__)
  286. this->SetDefinition("CMAKE_HOST_SOLARIS", "1");
  287. #endif
  288. this->SetDefinition("CMAKE_MAJOR_VERSION",
  289. std::to_string(cmVersion::GetMajorVersion()));
  290. this->SetDefinition("CMAKE_MINOR_VERSION",
  291. std::to_string(cmVersion::GetMinorVersion()));
  292. this->SetDefinition("CMAKE_PATCH_VERSION",
  293. std::to_string(cmVersion::GetPatchVersion()));
  294. this->SetDefinition("CMAKE_TWEAK_VERSION",
  295. std::to_string(cmVersion::GetTweakVersion()));
  296. this->SetDefinition("CMAKE_VERSION", cmVersion::GetCMakeVersion());
  297. this->SetDefinition("CMAKE_FILES_DIRECTORY",
  298. cmake::GetCMakeFilesDirectory());
  299. // Setup the default include file regular expression (match everything).
  300. this->Position->BuildSystemDirectory->Properties.SetProperty(
  301. "INCLUDE_REGULAR_EXPRESSION", "^.*$");
  302. }
  303. void cmStateSnapshot::SetDirectoryDefinitions()
  304. {
  305. this->SetDefinition("CMAKE_SOURCE_DIR", this->State->GetSourceDirectory());
  306. this->SetDefinition("CMAKE_CURRENT_SOURCE_DIR",
  307. this->State->GetSourceDirectory());
  308. this->SetDefinition("CMAKE_BINARY_DIR", this->State->GetBinaryDirectory());
  309. this->SetDefinition("CMAKE_CURRENT_BINARY_DIR",
  310. this->State->GetBinaryDirectory());
  311. }
  312. void cmStateSnapshot::InitializeFromParent()
  313. {
  314. cmStateDetail::PositionType parent = this->Position->DirectoryParent;
  315. assert(this->Position->Vars.IsValid());
  316. assert(parent->Vars.IsValid());
  317. *this->Position->Vars =
  318. cmDefinitions::MakeClosure(parent->Vars, parent->Root);
  319. InitializeContentFromParent(
  320. parent->BuildSystemDirectory->IncludeDirectories,
  321. this->Position->BuildSystemDirectory->IncludeDirectories,
  322. parent->BuildSystemDirectory->IncludeDirectoryBacktraces,
  323. this->Position->BuildSystemDirectory->IncludeDirectoryBacktraces,
  324. this->Position->IncludeDirectoryPosition);
  325. InitializeContentFromParent(
  326. parent->BuildSystemDirectory->CompileDefinitions,
  327. this->Position->BuildSystemDirectory->CompileDefinitions,
  328. parent->BuildSystemDirectory->CompileDefinitionsBacktraces,
  329. this->Position->BuildSystemDirectory->CompileDefinitionsBacktraces,
  330. this->Position->CompileDefinitionsPosition);
  331. InitializeContentFromParent(
  332. parent->BuildSystemDirectory->CompileOptions,
  333. this->Position->BuildSystemDirectory->CompileOptions,
  334. parent->BuildSystemDirectory->CompileOptionsBacktraces,
  335. this->Position->BuildSystemDirectory->CompileOptionsBacktraces,
  336. this->Position->CompileOptionsPosition);
  337. InitializeContentFromParent(
  338. parent->BuildSystemDirectory->LinkOptions,
  339. this->Position->BuildSystemDirectory->LinkOptions,
  340. parent->BuildSystemDirectory->LinkOptionsBacktraces,
  341. this->Position->BuildSystemDirectory->LinkOptionsBacktraces,
  342. this->Position->LinkOptionsPosition);
  343. const char* include_regex =
  344. parent->BuildSystemDirectory->Properties.GetPropertyValue(
  345. "INCLUDE_REGULAR_EXPRESSION");
  346. this->Position->BuildSystemDirectory->Properties.SetProperty(
  347. "INCLUDE_REGULAR_EXPRESSION", include_regex);
  348. }
  349. cmState* cmStateSnapshot::GetState() const
  350. {
  351. return this->State;
  352. }
  353. cmStateDirectory cmStateSnapshot::GetDirectory() const
  354. {
  355. return cmStateDirectory(this->Position->BuildSystemDirectory, *this);
  356. }
  357. void cmStateSnapshot::SetProjectName(const std::string& name)
  358. {
  359. this->Position->BuildSystemDirectory->ProjectName = name;
  360. }
  361. std::string cmStateSnapshot::GetProjectName() const
  362. {
  363. return this->Position->BuildSystemDirectory->ProjectName;
  364. }
  365. void cmStateSnapshot::InitializeFromParent_ForSubdirsCommand()
  366. {
  367. std::string currentSrcDir = this->GetDefinition("CMAKE_CURRENT_SOURCE_DIR");
  368. std::string currentBinDir = this->GetDefinition("CMAKE_CURRENT_BINARY_DIR");
  369. this->InitializeFromParent();
  370. this->SetDefinition("CMAKE_SOURCE_DIR", this->State->GetSourceDirectory());
  371. this->SetDefinition("CMAKE_BINARY_DIR", this->State->GetBinaryDirectory());
  372. this->SetDefinition("CMAKE_CURRENT_SOURCE_DIR", currentSrcDir);
  373. this->SetDefinition("CMAKE_CURRENT_BINARY_DIR", currentBinDir);
  374. }
  375. bool cmStateSnapshot::StrictWeakOrder::operator()(
  376. const cmStateSnapshot& lhs, const cmStateSnapshot& rhs) const
  377. {
  378. return lhs.Position.StrictWeakOrdered(rhs.Position);
  379. }
  380. bool operator==(const cmStateSnapshot& lhs, const cmStateSnapshot& rhs)
  381. {
  382. return lhs.Position == rhs.Position;
  383. }
  384. bool operator!=(const cmStateSnapshot& lhs, const cmStateSnapshot& rhs)
  385. {
  386. return lhs.Position != rhs.Position;
  387. }