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 "cmSystemTools.h"
  11. cmFindCommon::PathGroup cmFindCommon::PathGroup::All("ALL");
  12. cmFindCommon::PathLabel cmFindCommon::PathLabel::PackageRoot(
  13. "PackageName_ROOT");
  14. cmFindCommon::PathLabel cmFindCommon::PathLabel::CMake("CMAKE");
  15. cmFindCommon::PathLabel cmFindCommon::PathLabel::CMakeEnvironment(
  16. "CMAKE_ENVIRONMENT");
  17. cmFindCommon::PathLabel cmFindCommon::PathLabel::Hints("HINTS");
  18. cmFindCommon::PathLabel cmFindCommon::PathLabel::SystemEnvironment(
  19. "SYSTM_ENVIRONMENT");
  20. cmFindCommon::PathLabel cmFindCommon::PathLabel::CMakeSystem("CMAKE_SYSTEM");
  21. cmFindCommon::PathLabel cmFindCommon::PathLabel::Guess("GUESS");
  22. cmFindCommon::cmFindCommon()
  23. {
  24. this->FindRootPathMode = RootPathModeBoth;
  25. this->NoDefaultPath = false;
  26. this->NoPackageRootPath = false;
  27. this->NoCMakePath = false;
  28. this->NoCMakeEnvironmentPath = false;
  29. this->NoSystemEnvironmentPath = false;
  30. this->NoCMakeSystemPath = false;
  31. // OS X Bundle and Framework search policy. The default is to
  32. // search frameworks first on apple.
  33. #if defined(__APPLE__)
  34. this->SearchFrameworkFirst = true;
  35. this->SearchAppBundleFirst = true;
  36. #else
  37. this->SearchFrameworkFirst = false;
  38. this->SearchAppBundleFirst = false;
  39. #endif
  40. this->SearchFrameworkOnly = false;
  41. this->SearchFrameworkLast = false;
  42. this->SearchAppBundleOnly = false;
  43. this->SearchAppBundleLast = false;
  44. this->InitializeSearchPathGroups();
  45. }
  46. cmFindCommon::~cmFindCommon() = default;
  47. void cmFindCommon::InitializeSearchPathGroups()
  48. {
  49. std::vector<PathLabel>* labels;
  50. // Define the various different groups of path types
  51. // All search paths
  52. labels = &this->PathGroupLabelMap[PathGroup::All];
  53. labels->push_back(PathLabel::PackageRoot);
  54. labels->push_back(PathLabel::CMake);
  55. labels->push_back(PathLabel::CMakeEnvironment);
  56. labels->push_back(PathLabel::Hints);
  57. labels->push_back(PathLabel::SystemEnvironment);
  58. labels->push_back(PathLabel::CMakeSystem);
  59. labels->push_back(PathLabel::Guess);
  60. // Define the search group order
  61. this->PathGroupOrder.push_back(PathGroup::All);
  62. // Create the individual labeled search paths
  63. this->LabeledPaths.insert(
  64. std::make_pair(PathLabel::PackageRoot, cmSearchPath(this)));
  65. this->LabeledPaths.insert(
  66. std::make_pair(PathLabel::CMake, cmSearchPath(this)));
  67. this->LabeledPaths.insert(
  68. std::make_pair(PathLabel::CMakeEnvironment, cmSearchPath(this)));
  69. this->LabeledPaths.insert(
  70. std::make_pair(PathLabel::Hints, cmSearchPath(this)));
  71. this->LabeledPaths.insert(
  72. std::make_pair(PathLabel::SystemEnvironment, cmSearchPath(this)));
  73. this->LabeledPaths.insert(
  74. std::make_pair(PathLabel::CMakeSystem, cmSearchPath(this)));
  75. this->LabeledPaths.insert(
  76. std::make_pair(PathLabel::Guess, cmSearchPath(this)));
  77. }
  78. void cmFindCommon::SelectDefaultRootPathMode()
  79. {
  80. // Check the policy variable for this find command type.
  81. std::string findRootPathVar = "CMAKE_FIND_ROOT_PATH_MODE_";
  82. findRootPathVar += this->CMakePathName;
  83. std::string rootPathMode =
  84. this->Makefile->GetSafeDefinition(findRootPathVar);
  85. if (rootPathMode == "NEVER") {
  86. this->FindRootPathMode = RootPathModeNever;
  87. } else if (rootPathMode == "ONLY") {
  88. this->FindRootPathMode = RootPathModeOnly;
  89. } else if (rootPathMode == "BOTH") {
  90. this->FindRootPathMode = RootPathModeBoth;
  91. }
  92. }
  93. void cmFindCommon::SelectDefaultMacMode()
  94. {
  95. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  96. if (ff == "NEVER") {
  97. this->SearchFrameworkLast = false;
  98. this->SearchFrameworkFirst = false;
  99. this->SearchFrameworkOnly = false;
  100. } else if (ff == "ONLY") {
  101. this->SearchFrameworkLast = false;
  102. this->SearchFrameworkFirst = false;
  103. this->SearchFrameworkOnly = true;
  104. } else if (ff == "FIRST") {
  105. this->SearchFrameworkLast = false;
  106. this->SearchFrameworkFirst = true;
  107. this->SearchFrameworkOnly = false;
  108. } else if (ff == "LAST") {
  109. this->SearchFrameworkLast = true;
  110. this->SearchFrameworkFirst = false;
  111. this->SearchFrameworkOnly = false;
  112. }
  113. std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
  114. if (fab == "NEVER") {
  115. this->SearchAppBundleLast = false;
  116. this->SearchAppBundleFirst = false;
  117. this->SearchAppBundleOnly = false;
  118. } else if (fab == "ONLY") {
  119. this->SearchAppBundleLast = false;
  120. this->SearchAppBundleFirst = false;
  121. this->SearchAppBundleOnly = true;
  122. } else if (fab == "FIRST") {
  123. this->SearchAppBundleLast = false;
  124. this->SearchAppBundleFirst = true;
  125. this->SearchAppBundleOnly = false;
  126. } else if (fab == "LAST") {
  127. this->SearchAppBundleLast = true;
  128. this->SearchAppBundleFirst = false;
  129. this->SearchAppBundleOnly = false;
  130. }
  131. }
  132. void cmFindCommon::SelectDefaultSearchModes()
  133. {
  134. const std::array<std::pair<bool&, std::string>, 5> search_paths = {
  135. { { this->NoPackageRootPath, "CMAKE_FIND_USE_PACAKGE_ROOT_PATH" },
  136. { this->NoCMakePath, "CMAKE_FIND_USE_CMAKE_PATH" },
  137. { this->NoCMakeEnvironmentPath,
  138. "CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH" },
  139. { this->NoSystemEnvironmentPath,
  140. "CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH" },
  141. { this->NoCMakeSystemPath, "CMAKE_FIND_USE_CMAKE_SYSTEM_PATH" } }
  142. };
  143. for (auto& path : search_paths) {
  144. const char* def = this->Makefile->GetDefinition(path.second);
  145. if (def) {
  146. path.first = !cmSystemTools::IsOn(def);
  147. }
  148. }
  149. }
  150. void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
  151. {
  152. #if 0
  153. for(std::string const& p : paths)
  154. {
  155. fprintf(stderr, "[%s]\n", p.c_str());
  156. }
  157. #endif
  158. // Short-circuit if there is nothing to do.
  159. if (this->FindRootPathMode == RootPathModeNever) {
  160. return;
  161. }
  162. const char* sysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  163. const char* sysrootCompile =
  164. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE");
  165. const char* sysrootLink =
  166. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK");
  167. const char* rootPath = this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  168. const bool noSysroot = !sysroot || !*sysroot;
  169. const bool noCompileSysroot = !sysrootCompile || !*sysrootCompile;
  170. const bool noLinkSysroot = !sysrootLink || !*sysrootLink;
  171. const bool noRootPath = !rootPath || !*rootPath;
  172. if (noSysroot && noCompileSysroot && noLinkSysroot && noRootPath) {
  173. return;
  174. }
  175. // Construct the list of path roots with no trailing slashes.
  176. std::vector<std::string> roots;
  177. if (rootPath) {
  178. cmSystemTools::ExpandListArgument(rootPath, roots);
  179. }
  180. if (sysrootCompile) {
  181. roots.emplace_back(sysrootCompile);
  182. }
  183. if (sysrootLink) {
  184. roots.emplace_back(sysrootLink);
  185. }
  186. if (sysroot) {
  187. roots.emplace_back(sysroot);
  188. }
  189. for (std::string& r : roots) {
  190. cmSystemTools::ConvertToUnixSlashes(r);
  191. }
  192. const char* stagePrefix =
  193. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  194. // Copy the original set of unrooted paths.
  195. std::vector<std::string> unrootedPaths = paths;
  196. paths.clear();
  197. for (std::string const& r : roots) {
  198. for (std::string const& up : unrootedPaths) {
  199. // Place the unrooted path under the current root if it is not
  200. // already inside. Skip the unrooted path if it is relative to
  201. // a user home directory or is empty.
  202. std::string rootedDir;
  203. if (cmSystemTools::IsSubDirectory(up, r) ||
  204. (stagePrefix && cmSystemTools::IsSubDirectory(up, stagePrefix))) {
  205. rootedDir = up;
  206. } else if (!up.empty() && up[0] != '~') {
  207. // Start with the new root.
  208. rootedDir = r;
  209. rootedDir += "/";
  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. cmSystemTools::ExpandListArgument(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. }