cmFindCommon.h 5.6 KB

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