cmFindCommon.cxx 11 KB

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