cmFindCommon.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 =
  83. cmStrCat("CMAKE_FIND_ROOT_PATH_MODE_", 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 = cmStrCat(r, '/');
  210. // Append the original path with its old root removed.
  211. rootedDir += cmSystemTools::SplitPathRootComponent(up);
  212. }
  213. // Store the new path.
  214. paths.push_back(rootedDir);
  215. }
  216. }
  217. // If searching both rooted and unrooted paths add the original
  218. // paths again.
  219. if (this->FindRootPathMode == RootPathModeBoth) {
  220. cmAppend(paths, unrootedPaths);
  221. }
  222. }
  223. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  224. {
  225. // null-terminated list of paths.
  226. static const char* paths[] = { "CMAKE_SYSTEM_IGNORE_PATH",
  227. "CMAKE_IGNORE_PATH", nullptr };
  228. // Construct the list of path roots with no trailing slashes.
  229. for (const char** pathName = paths; *pathName; ++pathName) {
  230. // Get the list of paths to ignore from the variable.
  231. const char* ignorePath = this->Makefile->GetDefinition(*pathName);
  232. if ((ignorePath == nullptr) || (strlen(ignorePath) == 0)) {
  233. continue;
  234. }
  235. cmExpandList(ignorePath, ignore);
  236. }
  237. for (std::string& i : ignore) {
  238. cmSystemTools::ConvertToUnixSlashes(i);
  239. }
  240. }
  241. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  242. {
  243. std::vector<std::string> ignoreVec;
  244. GetIgnoredPaths(ignoreVec);
  245. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  246. }
  247. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  248. {
  249. if (arg == "NO_DEFAULT_PATH") {
  250. this->NoDefaultPath = true;
  251. } else if (arg == "NO_PACKAGE_ROOT_PATH") {
  252. this->NoPackageRootPath = true;
  253. } else if (arg == "NO_CMAKE_PATH") {
  254. this->NoCMakePath = true;
  255. } else if (arg == "NO_CMAKE_ENVIRONMENT_PATH") {
  256. this->NoCMakeEnvironmentPath = true;
  257. } else if (arg == "NO_SYSTEM_ENVIRONMENT_PATH") {
  258. this->NoSystemEnvironmentPath = true;
  259. } else if (arg == "NO_CMAKE_SYSTEM_PATH") {
  260. this->NoCMakeSystemPath = true;
  261. } else if (arg == "NO_CMAKE_FIND_ROOT_PATH") {
  262. this->FindRootPathMode = RootPathModeNever;
  263. } else if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") {
  264. this->FindRootPathMode = RootPathModeOnly;
  265. } else if (arg == "CMAKE_FIND_ROOT_PATH_BOTH") {
  266. this->FindRootPathMode = RootPathModeBoth;
  267. } else {
  268. // The argument is not one of the above.
  269. return false;
  270. }
  271. // The argument is one of the above.
  272. return true;
  273. }
  274. void cmFindCommon::AddPathSuffix(std::string const& arg)
  275. {
  276. std::string suffix = arg;
  277. // Strip leading and trailing slashes.
  278. if (suffix.empty()) {
  279. return;
  280. }
  281. if (suffix.front() == '/') {
  282. suffix = suffix.substr(1);
  283. }
  284. if (suffix.empty()) {
  285. return;
  286. }
  287. if (suffix.back() == '/') {
  288. suffix = suffix.substr(0, suffix.size() - 1);
  289. }
  290. if (suffix.empty()) {
  291. return;
  292. }
  293. // Store the suffix.
  294. this->SearchPathSuffixes.push_back(std::move(suffix));
  295. }
  296. void AddTrailingSlash(std::string& s)
  297. {
  298. if (!s.empty() && s.back() != '/') {
  299. s += '/';
  300. }
  301. }
  302. void cmFindCommon::ComputeFinalPaths()
  303. {
  304. // Filter out ignored paths from the prefix list
  305. std::set<std::string> ignored;
  306. this->GetIgnoredPaths(ignored);
  307. // Combine the separate path types, filtering out ignores
  308. this->SearchPaths.clear();
  309. std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All];
  310. for (PathLabel const& l : allLabels) {
  311. this->LabeledPaths[l].ExtractWithout(ignored, this->SearchPaths);
  312. }
  313. // Expand list of paths inside all search roots.
  314. this->RerootPaths(this->SearchPaths);
  315. // Add a trailing slash to all paths to aid the search process.
  316. std::for_each(this->SearchPaths.begin(), this->SearchPaths.end(),
  317. &AddTrailingSlash);
  318. }