cmFindCommon.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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()
  45. {
  46. }
  47. void cmFindCommon::InitializeSearchPathGroups()
  48. {
  49. std::vector<PathLabel>* labels;
  50. // Define the varoius 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 idividual labeld 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::RerootPaths(std::vector<std::string>& paths)
  133. {
  134. #if 0
  135. for(std::string const& p : paths)
  136. {
  137. fprintf(stderr, "[%s]\n", p.c_str());
  138. }
  139. #endif
  140. // Short-circuit if there is nothing to do.
  141. if (this->FindRootPathMode == RootPathModeNever) {
  142. return;
  143. }
  144. const char* sysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  145. const char* sysrootCompile =
  146. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE");
  147. const char* sysrootLink =
  148. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK");
  149. const char* rootPath = this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  150. const bool noSysroot = !sysroot || !*sysroot;
  151. const bool noCompileSysroot = !sysrootCompile || !*sysrootCompile;
  152. const bool noLinkSysroot = !sysrootLink || !*sysrootLink;
  153. const bool noRootPath = !rootPath || !*rootPath;
  154. if (noSysroot && noCompileSysroot && noLinkSysroot && noRootPath) {
  155. return;
  156. }
  157. // Construct the list of path roots with no trailing slashes.
  158. std::vector<std::string> roots;
  159. if (rootPath) {
  160. cmSystemTools::ExpandListArgument(rootPath, roots);
  161. }
  162. if (sysrootCompile) {
  163. roots.push_back(sysrootCompile);
  164. }
  165. if (sysrootLink) {
  166. roots.push_back(sysrootLink);
  167. }
  168. if (sysroot) {
  169. roots.push_back(sysroot);
  170. }
  171. for (std::string& r : roots) {
  172. cmSystemTools::ConvertToUnixSlashes(r);
  173. }
  174. const char* stagePrefix =
  175. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  176. // Copy the original set of unrooted paths.
  177. std::vector<std::string> unrootedPaths = paths;
  178. paths.clear();
  179. for (std::string const& r : roots) {
  180. for (std::string const& up : unrootedPaths) {
  181. // Place the unrooted path under the current root if it is not
  182. // already inside. Skip the unrooted path if it is relative to
  183. // a user home directory or is empty.
  184. std::string rootedDir;
  185. if (cmSystemTools::IsSubDirectory(up, r) ||
  186. (stagePrefix && cmSystemTools::IsSubDirectory(up, stagePrefix))) {
  187. rootedDir = up;
  188. } else if (!up.empty() && up[0] != '~') {
  189. // Start with the new root.
  190. rootedDir = r;
  191. rootedDir += "/";
  192. // Append the original path with its old root removed.
  193. rootedDir += cmSystemTools::SplitPathRootComponent(up);
  194. }
  195. // Store the new path.
  196. paths.push_back(rootedDir);
  197. }
  198. }
  199. // If searching both rooted and unrooted paths add the original
  200. // paths again.
  201. if (this->FindRootPathMode == RootPathModeBoth) {
  202. paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
  203. }
  204. }
  205. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  206. {
  207. // null-terminated list of paths.
  208. static const char* paths[] = { "CMAKE_SYSTEM_IGNORE_PATH",
  209. "CMAKE_IGNORE_PATH", nullptr };
  210. // Construct the list of path roots with no trailing slashes.
  211. for (const char** pathName = paths; *pathName; ++pathName) {
  212. // Get the list of paths to ignore from the variable.
  213. const char* ignorePath = this->Makefile->GetDefinition(*pathName);
  214. if ((ignorePath == nullptr) || (strlen(ignorePath) == 0)) {
  215. continue;
  216. }
  217. cmSystemTools::ExpandListArgument(ignorePath, ignore);
  218. }
  219. for (std::string& i : ignore) {
  220. cmSystemTools::ConvertToUnixSlashes(i);
  221. }
  222. }
  223. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  224. {
  225. std::vector<std::string> ignoreVec;
  226. GetIgnoredPaths(ignoreVec);
  227. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  228. }
  229. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  230. {
  231. if (arg == "NO_DEFAULT_PATH") {
  232. this->NoDefaultPath = true;
  233. } else if (arg == "NO_PACKAGE_ROOT_PATH") {
  234. this->NoPackageRootPath = true;
  235. } else if (arg == "NO_CMAKE_PATH") {
  236. this->NoCMakePath = true;
  237. } else if (arg == "NO_CMAKE_ENVIRONMENT_PATH") {
  238. this->NoCMakeEnvironmentPath = true;
  239. } else if (arg == "NO_SYSTEM_ENVIRONMENT_PATH") {
  240. this->NoSystemEnvironmentPath = true;
  241. } else if (arg == "NO_CMAKE_SYSTEM_PATH") {
  242. this->NoCMakeSystemPath = true;
  243. } else if (arg == "NO_CMAKE_FIND_ROOT_PATH") {
  244. this->FindRootPathMode = RootPathModeNever;
  245. } else if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") {
  246. this->FindRootPathMode = RootPathModeOnly;
  247. } else if (arg == "CMAKE_FIND_ROOT_PATH_BOTH") {
  248. this->FindRootPathMode = RootPathModeBoth;
  249. } else {
  250. // The argument is not one of the above.
  251. return false;
  252. }
  253. // The argument is one of the above.
  254. return true;
  255. }
  256. void cmFindCommon::AddPathSuffix(std::string const& arg)
  257. {
  258. std::string suffix = arg;
  259. // Strip leading and trailing slashes.
  260. if (suffix.empty()) {
  261. return;
  262. }
  263. if (suffix[0] == '/') {
  264. suffix = suffix.substr(1);
  265. }
  266. if (suffix.empty()) {
  267. return;
  268. }
  269. if (suffix[suffix.size() - 1] == '/') {
  270. suffix = suffix.substr(0, suffix.size() - 1);
  271. }
  272. if (suffix.empty()) {
  273. return;
  274. }
  275. // Store the suffix.
  276. this->SearchPathSuffixes.push_back(std::move(suffix));
  277. }
  278. void AddTrailingSlash(std::string& s)
  279. {
  280. if (!s.empty() && *s.rbegin() != '/') {
  281. s += '/';
  282. }
  283. }
  284. void cmFindCommon::ComputeFinalPaths()
  285. {
  286. // Filter out ignored paths from the prefix list
  287. std::set<std::string> ignored;
  288. this->GetIgnoredPaths(ignored);
  289. // Combine the separate path types, filtering out ignores
  290. this->SearchPaths.clear();
  291. std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All];
  292. for (PathLabel const& l : allLabels) {
  293. this->LabeledPaths[l].ExtractWithout(ignored, this->SearchPaths);
  294. }
  295. // Expand list of paths inside all search roots.
  296. this->RerootPaths(this->SearchPaths);
  297. // Add a trailing slash to all paths to aid the search process.
  298. std::for_each(this->SearchPaths.begin(), this->SearchPaths.end(),
  299. &AddTrailingSlash);
  300. }