cmFindCommon.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 "cmFindCommon.h"
  4. #include <algorithm>
  5. #include <string.h>
  6. #include <utility>
  7. #include "cmMakefile.h"
  8. #include "cmSystemTools.h"
  9. cmFindCommon::PathGroup cmFindCommon::PathGroup::All("ALL");
  10. cmFindCommon::PathLabel cmFindCommon::PathLabel::PackageRoot(
  11. "PackageName_ROOT");
  12. cmFindCommon::PathLabel cmFindCommon::PathLabel::CMake("CMAKE");
  13. cmFindCommon::PathLabel cmFindCommon::PathLabel::CMakeEnvironment(
  14. "CMAKE_ENVIRONMENT");
  15. cmFindCommon::PathLabel cmFindCommon::PathLabel::Hints("HINTS");
  16. cmFindCommon::PathLabel cmFindCommon::PathLabel::SystemEnvironment(
  17. "SYSTM_ENVIRONMENT");
  18. cmFindCommon::PathLabel cmFindCommon::PathLabel::CMakeSystem("CMAKE_SYSTEM");
  19. cmFindCommon::PathLabel cmFindCommon::PathLabel::Guess("GUESS");
  20. cmFindCommon::cmFindCommon()
  21. {
  22. this->FindRootPathMode = RootPathModeBoth;
  23. this->NoDefaultPath = false;
  24. this->NoPackageRootPath = false;
  25. this->NoCMakePath = false;
  26. this->NoCMakeEnvironmentPath = false;
  27. this->NoSystemEnvironmentPath = false;
  28. this->NoCMakeSystemPath = false;
  29. // OS X Bundle and Framework search policy. The default is to
  30. // search frameworks first on apple.
  31. #if defined(__APPLE__)
  32. this->SearchFrameworkFirst = true;
  33. this->SearchAppBundleFirst = true;
  34. #else
  35. this->SearchFrameworkFirst = false;
  36. this->SearchAppBundleFirst = false;
  37. #endif
  38. this->SearchFrameworkOnly = false;
  39. this->SearchFrameworkLast = false;
  40. this->SearchAppBundleOnly = false;
  41. this->SearchAppBundleLast = false;
  42. this->InitializeSearchPathGroups();
  43. }
  44. cmFindCommon::~cmFindCommon() = default;
  45. void cmFindCommon::InitializeSearchPathGroups()
  46. {
  47. std::vector<PathLabel>* labels;
  48. // Define the various different groups of path types
  49. // All search paths
  50. labels = &this->PathGroupLabelMap[PathGroup::All];
  51. labels->push_back(PathLabel::PackageRoot);
  52. labels->push_back(PathLabel::CMake);
  53. labels->push_back(PathLabel::CMakeEnvironment);
  54. labels->push_back(PathLabel::Hints);
  55. labels->push_back(PathLabel::SystemEnvironment);
  56. labels->push_back(PathLabel::CMakeSystem);
  57. labels->push_back(PathLabel::Guess);
  58. // Define the search group order
  59. this->PathGroupOrder.push_back(PathGroup::All);
  60. // Create the individual labeled search paths
  61. this->LabeledPaths.insert(
  62. std::make_pair(PathLabel::PackageRoot, cmSearchPath(this)));
  63. this->LabeledPaths.insert(
  64. std::make_pair(PathLabel::CMake, cmSearchPath(this)));
  65. this->LabeledPaths.insert(
  66. std::make_pair(PathLabel::CMakeEnvironment, cmSearchPath(this)));
  67. this->LabeledPaths.insert(
  68. std::make_pair(PathLabel::Hints, cmSearchPath(this)));
  69. this->LabeledPaths.insert(
  70. std::make_pair(PathLabel::SystemEnvironment, cmSearchPath(this)));
  71. this->LabeledPaths.insert(
  72. std::make_pair(PathLabel::CMakeSystem, cmSearchPath(this)));
  73. this->LabeledPaths.insert(
  74. std::make_pair(PathLabel::Guess, cmSearchPath(this)));
  75. }
  76. void cmFindCommon::SelectDefaultRootPathMode()
  77. {
  78. // Check the policy variable for this find command type.
  79. std::string findRootPathVar = "CMAKE_FIND_ROOT_PATH_MODE_";
  80. findRootPathVar += this->CMakePathName;
  81. std::string rootPathMode =
  82. this->Makefile->GetSafeDefinition(findRootPathVar);
  83. if (rootPathMode == "NEVER") {
  84. this->FindRootPathMode = RootPathModeNever;
  85. } else if (rootPathMode == "ONLY") {
  86. this->FindRootPathMode = RootPathModeOnly;
  87. } else if (rootPathMode == "BOTH") {
  88. this->FindRootPathMode = RootPathModeBoth;
  89. }
  90. }
  91. void cmFindCommon::SelectDefaultMacMode()
  92. {
  93. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  94. if (ff == "NEVER") {
  95. this->SearchFrameworkLast = false;
  96. this->SearchFrameworkFirst = false;
  97. this->SearchFrameworkOnly = false;
  98. } else if (ff == "ONLY") {
  99. this->SearchFrameworkLast = false;
  100. this->SearchFrameworkFirst = false;
  101. this->SearchFrameworkOnly = true;
  102. } else if (ff == "FIRST") {
  103. this->SearchFrameworkLast = false;
  104. this->SearchFrameworkFirst = true;
  105. this->SearchFrameworkOnly = false;
  106. } else if (ff == "LAST") {
  107. this->SearchFrameworkLast = true;
  108. this->SearchFrameworkFirst = false;
  109. this->SearchFrameworkOnly = false;
  110. }
  111. std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
  112. if (fab == "NEVER") {
  113. this->SearchAppBundleLast = false;
  114. this->SearchAppBundleFirst = false;
  115. this->SearchAppBundleOnly = false;
  116. } else if (fab == "ONLY") {
  117. this->SearchAppBundleLast = false;
  118. this->SearchAppBundleFirst = false;
  119. this->SearchAppBundleOnly = true;
  120. } else if (fab == "FIRST") {
  121. this->SearchAppBundleLast = false;
  122. this->SearchAppBundleFirst = true;
  123. this->SearchAppBundleOnly = false;
  124. } else if (fab == "LAST") {
  125. this->SearchAppBundleLast = true;
  126. this->SearchAppBundleFirst = false;
  127. this->SearchAppBundleOnly = false;
  128. }
  129. }
  130. void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
  131. {
  132. #if 0
  133. for(std::string const& p : paths)
  134. {
  135. fprintf(stderr, "[%s]\n", p.c_str());
  136. }
  137. #endif
  138. // Short-circuit if there is nothing to do.
  139. if (this->FindRootPathMode == RootPathModeNever) {
  140. return;
  141. }
  142. const char* sysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  143. const char* sysrootCompile =
  144. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE");
  145. const char* sysrootLink =
  146. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK");
  147. const char* rootPath = this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  148. const bool noSysroot = !sysroot || !*sysroot;
  149. const bool noCompileSysroot = !sysrootCompile || !*sysrootCompile;
  150. const bool noLinkSysroot = !sysrootLink || !*sysrootLink;
  151. const bool noRootPath = !rootPath || !*rootPath;
  152. if (noSysroot && noCompileSysroot && noLinkSysroot && noRootPath) {
  153. return;
  154. }
  155. // Construct the list of path roots with no trailing slashes.
  156. std::vector<std::string> roots;
  157. if (rootPath) {
  158. cmSystemTools::ExpandListArgument(rootPath, roots);
  159. }
  160. if (sysrootCompile) {
  161. roots.emplace_back(sysrootCompile);
  162. }
  163. if (sysrootLink) {
  164. roots.emplace_back(sysrootLink);
  165. }
  166. if (sysroot) {
  167. roots.emplace_back(sysroot);
  168. }
  169. for (std::string& r : roots) {
  170. cmSystemTools::ConvertToUnixSlashes(r);
  171. }
  172. const char* stagePrefix =
  173. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  174. // Copy the original set of unrooted paths.
  175. std::vector<std::string> unrootedPaths = paths;
  176. paths.clear();
  177. for (std::string const& r : roots) {
  178. for (std::string const& up : unrootedPaths) {
  179. // Place the unrooted path under the current root if it is not
  180. // already inside. Skip the unrooted path if it is relative to
  181. // a user home directory or is empty.
  182. std::string rootedDir;
  183. if (cmSystemTools::IsSubDirectory(up, r) ||
  184. (stagePrefix && cmSystemTools::IsSubDirectory(up, stagePrefix))) {
  185. rootedDir = up;
  186. } else if (!up.empty() && up[0] != '~') {
  187. // Start with the new root.
  188. rootedDir = r;
  189. rootedDir += "/";
  190. // Append the original path with its old root removed.
  191. rootedDir += cmSystemTools::SplitPathRootComponent(up);
  192. }
  193. // Store the new path.
  194. paths.push_back(rootedDir);
  195. }
  196. }
  197. // If searching both rooted and unrooted paths add the original
  198. // paths again.
  199. if (this->FindRootPathMode == RootPathModeBoth) {
  200. paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
  201. }
  202. }
  203. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  204. {
  205. // null-terminated list of paths.
  206. static const char* paths[] = { "CMAKE_SYSTEM_IGNORE_PATH",
  207. "CMAKE_IGNORE_PATH", nullptr };
  208. // Construct the list of path roots with no trailing slashes.
  209. for (const char** pathName = paths; *pathName; ++pathName) {
  210. // Get the list of paths to ignore from the variable.
  211. const char* ignorePath = this->Makefile->GetDefinition(*pathName);
  212. if ((ignorePath == nullptr) || (strlen(ignorePath) == 0)) {
  213. continue;
  214. }
  215. cmSystemTools::ExpandListArgument(ignorePath, ignore);
  216. }
  217. for (std::string& i : ignore) {
  218. cmSystemTools::ConvertToUnixSlashes(i);
  219. }
  220. }
  221. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  222. {
  223. std::vector<std::string> ignoreVec;
  224. GetIgnoredPaths(ignoreVec);
  225. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  226. }
  227. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  228. {
  229. if (arg == "NO_DEFAULT_PATH") {
  230. this->NoDefaultPath = true;
  231. } else if (arg == "NO_PACKAGE_ROOT_PATH") {
  232. this->NoPackageRootPath = true;
  233. } else if (arg == "NO_CMAKE_PATH") {
  234. this->NoCMakePath = true;
  235. } else if (arg == "NO_CMAKE_ENVIRONMENT_PATH") {
  236. this->NoCMakeEnvironmentPath = true;
  237. } else if (arg == "NO_SYSTEM_ENVIRONMENT_PATH") {
  238. this->NoSystemEnvironmentPath = true;
  239. } else if (arg == "NO_CMAKE_SYSTEM_PATH") {
  240. this->NoCMakeSystemPath = true;
  241. } else if (arg == "NO_CMAKE_FIND_ROOT_PATH") {
  242. this->FindRootPathMode = RootPathModeNever;
  243. } else if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") {
  244. this->FindRootPathMode = RootPathModeOnly;
  245. } else if (arg == "CMAKE_FIND_ROOT_PATH_BOTH") {
  246. this->FindRootPathMode = RootPathModeBoth;
  247. } else {
  248. // The argument is not one of the above.
  249. return false;
  250. }
  251. // The argument is one of the above.
  252. return true;
  253. }
  254. void cmFindCommon::AddPathSuffix(std::string const& arg)
  255. {
  256. std::string suffix = arg;
  257. // Strip leading and trailing slashes.
  258. if (suffix.empty()) {
  259. return;
  260. }
  261. if (suffix.front() == '/') {
  262. suffix = suffix.substr(1);
  263. }
  264. if (suffix.empty()) {
  265. return;
  266. }
  267. if (suffix.back() == '/') {
  268. suffix = suffix.substr(0, suffix.size() - 1);
  269. }
  270. if (suffix.empty()) {
  271. return;
  272. }
  273. // Store the suffix.
  274. this->SearchPathSuffixes.push_back(std::move(suffix));
  275. }
  276. void AddTrailingSlash(std::string& s)
  277. {
  278. if (!s.empty() && *s.rbegin() != '/') {
  279. s += '/';
  280. }
  281. }
  282. void cmFindCommon::ComputeFinalPaths()
  283. {
  284. // Filter out ignored paths from the prefix list
  285. std::set<std::string> ignored;
  286. this->GetIgnoredPaths(ignored);
  287. // Combine the separate path types, filtering out ignores
  288. this->SearchPaths.clear();
  289. std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All];
  290. for (PathLabel const& l : allLabels) {
  291. this->LabeledPaths[l].ExtractWithout(ignored, this->SearchPaths);
  292. }
  293. // Expand list of paths inside all search roots.
  294. this->RerootPaths(this->SearchPaths);
  295. // Add a trailing slash to all paths to aid the search process.
  296. std::for_each(this->SearchPaths.begin(), this->SearchPaths.end(),
  297. &AddTrailingSlash);
  298. }