cmFindCommon.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <map>
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "cmPathLabel.h"
  11. #include "cmSearchPath.h"
  12. #include "cmWindowsRegistry.h"
  13. class cmFindCommonDebugState;
  14. class cmExecutionStatus;
  15. class cmMakefile;
  16. /** \class cmFindCommon
  17. * \brief Base class for FIND_XXX implementations.
  18. *
  19. * cmFindCommon is a parent class for cmFindBase,
  20. * cmFindProgramCommand, cmFindPathCommand, cmFindLibraryCommand,
  21. * cmFindFileCommand, and cmFindPackageCommand.
  22. */
  23. class cmFindCommon
  24. {
  25. public:
  26. cmFindCommon(cmExecutionStatus& status);
  27. void SetError(std::string const& e);
  28. bool DebugModeEnabled() const;
  29. protected:
  30. friend class cmSearchPath;
  31. friend class cmFindBaseDebugState;
  32. friend class cmFindCommonDebugState;
  33. /** Used to define groups of path labels */
  34. class PathGroup : public cmPathLabel
  35. {
  36. protected:
  37. PathGroup();
  38. public:
  39. PathGroup(std::string const& label)
  40. : cmPathLabel(label)
  41. {
  42. }
  43. static PathGroup All;
  44. };
  45. /* Individual path types */
  46. class PathLabel : public cmPathLabel
  47. {
  48. protected:
  49. PathLabel();
  50. public:
  51. PathLabel(std::string const& label)
  52. : cmPathLabel(label)
  53. {
  54. }
  55. static PathLabel PackageRoot;
  56. static PathLabel CMake;
  57. static PathLabel CMakeEnvironment;
  58. static PathLabel Hints;
  59. static PathLabel SystemEnvironment;
  60. static PathLabel CMakeSystem;
  61. static PathLabel Guess;
  62. };
  63. enum RootPathMode
  64. {
  65. RootPathModeNever,
  66. RootPathModeOnly,
  67. RootPathModeBoth
  68. };
  69. /** Construct the various path groups and labels */
  70. void InitializeSearchPathGroups();
  71. /** Place a set of search paths under the search roots. */
  72. void RerootPaths(std::vector<std::string>& paths,
  73. std::string* debugBuffer = nullptr);
  74. /** Get ignored paths from CMAKE_[SYSTEM_]IGNORE_PATH variables. */
  75. void GetIgnoredPaths(std::vector<std::string>& ignore);
  76. void GetIgnoredPaths(std::set<std::string>& ignore);
  77. /** Get ignored paths from CMAKE_[SYSTEM_]IGNORE_PREFIX_PATH variables. */
  78. void GetIgnoredPrefixPaths(std::vector<std::string>& ignore);
  79. void GetIgnoredPrefixPaths(std::set<std::string>& ignore);
  80. /** Compute final search path list (reroot + trailing slash). */
  81. enum class IgnorePaths
  82. {
  83. No,
  84. Yes,
  85. };
  86. void ComputeFinalPaths(IgnorePaths ignorePaths,
  87. std::string* debugBuffer = nullptr);
  88. /** Compute the current default root path mode. */
  89. void SelectDefaultRootPathMode();
  90. /** Compute the current default bundle/framework search policy. */
  91. void SelectDefaultMacMode();
  92. /** Compute the current default search modes based on global variables. */
  93. void SelectDefaultSearchModes();
  94. /** The `InitialPass` functions of the child classes should set
  95. this->DebugMode to the result of these. */
  96. bool ComputeIfDebugModeWanted();
  97. bool ComputeIfDebugModeWanted(std::string const& var);
  98. // Path arguments prior to path manipulation routines
  99. std::vector<std::string> UserHintsArgs;
  100. std::vector<std::string> UserGuessArgs;
  101. std::string CMakePathName;
  102. RootPathMode FindRootPathMode;
  103. bool CheckCommonArgument(std::string const& arg);
  104. void AddPathSuffix(std::string const& arg);
  105. void DebugMessage(std::string const& msg) const;
  106. std::unique_ptr<cmFindCommonDebugState> DebugState;
  107. bool NoDefaultPath;
  108. bool NoPackageRootPath;
  109. bool NoCMakePath;
  110. bool NoCMakeEnvironmentPath;
  111. bool NoSystemEnvironmentPath;
  112. bool NoCMakeSystemPath;
  113. bool NoCMakeInstallPath;
  114. cmWindowsRegistry::View RegistryView = cmWindowsRegistry::View::Target;
  115. std::vector<std::string> SearchPathSuffixes;
  116. std::map<PathGroup, std::vector<PathLabel>> PathGroupLabelMap;
  117. std::vector<PathGroup> PathGroupOrder;
  118. std::map<std::string, PathLabel> PathLabelStringMap;
  119. std::map<PathLabel, cmSearchPath> LabeledPaths;
  120. std::vector<std::string> SearchPaths;
  121. std::set<cmSearchPath::PathWithPrefix> SearchPathsEmitted;
  122. bool SearchFrameworkFirst;
  123. bool SearchFrameworkOnly;
  124. bool SearchFrameworkLast;
  125. bool SearchAppBundleFirst;
  126. bool SearchAppBundleOnly;
  127. bool SearchAppBundleLast;
  128. cmMakefile* Makefile;
  129. cmExecutionStatus& Status;
  130. };
  131. class cmFindCommonDebugState
  132. {
  133. public:
  134. cmFindCommonDebugState(std::string name, cmFindCommon const* findCommand);
  135. virtual ~cmFindCommonDebugState() = default;
  136. void FoundAt(std::string const& path, std::string regexName = std::string());
  137. void FailedAt(std::string const& path,
  138. std::string regexName = std::string());
  139. protected:
  140. virtual void FoundAtImpl(std::string const& path, std::string regexName) = 0;
  141. virtual void FailedAtImpl(std::string const& path,
  142. std::string regexName) = 0;
  143. cmFindCommon const* const FindCommand;
  144. std::string const CommandName;
  145. std::string const Mode;
  146. bool HasBeenFound() const { return this->IsFound; }
  147. private:
  148. bool TrackSearchProgress() const;
  149. bool IsFound{ false };
  150. };