cmFindCommon.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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::SelectDefaultNoPackageRootPath()
  79. {
  80. if (!this->Makefile->IsOn("__UNDOCUMENTED_CMAKE_FIND_PACKAGE_ROOT")) {
  81. this->NoPackageRootPath = true;
  82. }
  83. }
  84. void cmFindCommon::SelectDefaultRootPathMode()
  85. {
  86. // Check the policy variable for this find command type.
  87. std::string findRootPathVar = "CMAKE_FIND_ROOT_PATH_MODE_";
  88. findRootPathVar += this->CMakePathName;
  89. std::string rootPathMode =
  90. this->Makefile->GetSafeDefinition(findRootPathVar);
  91. if (rootPathMode == "NEVER") {
  92. this->FindRootPathMode = RootPathModeNever;
  93. } else if (rootPathMode == "ONLY") {
  94. this->FindRootPathMode = RootPathModeOnly;
  95. } else if (rootPathMode == "BOTH") {
  96. this->FindRootPathMode = RootPathModeBoth;
  97. }
  98. }
  99. void cmFindCommon::SelectDefaultMacMode()
  100. {
  101. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  102. if (ff == "NEVER") {
  103. this->SearchFrameworkLast = false;
  104. this->SearchFrameworkFirst = false;
  105. this->SearchFrameworkOnly = false;
  106. } else if (ff == "ONLY") {
  107. this->SearchFrameworkLast = false;
  108. this->SearchFrameworkFirst = false;
  109. this->SearchFrameworkOnly = true;
  110. } else if (ff == "FIRST") {
  111. this->SearchFrameworkLast = false;
  112. this->SearchFrameworkFirst = true;
  113. this->SearchFrameworkOnly = false;
  114. } else if (ff == "LAST") {
  115. this->SearchFrameworkLast = true;
  116. this->SearchFrameworkFirst = false;
  117. this->SearchFrameworkOnly = false;
  118. }
  119. std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
  120. if (fab == "NEVER") {
  121. this->SearchAppBundleLast = false;
  122. this->SearchAppBundleFirst = false;
  123. this->SearchAppBundleOnly = false;
  124. } else if (fab == "ONLY") {
  125. this->SearchAppBundleLast = false;
  126. this->SearchAppBundleFirst = false;
  127. this->SearchAppBundleOnly = true;
  128. } else if (fab == "FIRST") {
  129. this->SearchAppBundleLast = false;
  130. this->SearchAppBundleFirst = true;
  131. this->SearchAppBundleOnly = false;
  132. } else if (fab == "LAST") {
  133. this->SearchAppBundleLast = true;
  134. this->SearchAppBundleFirst = false;
  135. this->SearchAppBundleOnly = false;
  136. }
  137. }
  138. void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
  139. {
  140. #if 0
  141. for(std::string const& p : paths)
  142. {
  143. fprintf(stderr, "[%s]\n", p.c_str());
  144. }
  145. #endif
  146. // Short-circuit if there is nothing to do.
  147. if (this->FindRootPathMode == RootPathModeNever) {
  148. return;
  149. }
  150. const char* sysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  151. const char* sysrootCompile =
  152. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE");
  153. const char* sysrootLink =
  154. this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK");
  155. const char* rootPath = this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  156. const bool noSysroot = !sysroot || !*sysroot;
  157. const bool noCompileSysroot = !sysrootCompile || !*sysrootCompile;
  158. const bool noLinkSysroot = !sysrootLink || !*sysrootLink;
  159. const bool noRootPath = !rootPath || !*rootPath;
  160. if (noSysroot && noCompileSysroot && noLinkSysroot && noRootPath) {
  161. return;
  162. }
  163. // Construct the list of path roots with no trailing slashes.
  164. std::vector<std::string> roots;
  165. if (rootPath) {
  166. cmSystemTools::ExpandListArgument(rootPath, roots);
  167. }
  168. if (sysrootCompile) {
  169. roots.push_back(sysrootCompile);
  170. }
  171. if (sysrootLink) {
  172. roots.push_back(sysrootLink);
  173. }
  174. if (sysroot) {
  175. roots.push_back(sysroot);
  176. }
  177. for (std::string& r : roots) {
  178. cmSystemTools::ConvertToUnixSlashes(r);
  179. }
  180. const char* stagePrefix =
  181. this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  182. // Copy the original set of unrooted paths.
  183. std::vector<std::string> unrootedPaths = paths;
  184. paths.clear();
  185. for (std::string const& r : roots) {
  186. for (std::string const& up : unrootedPaths) {
  187. // Place the unrooted path under the current root if it is not
  188. // already inside. Skip the unrooted path if it is relative to
  189. // a user home directory or is empty.
  190. std::string rootedDir;
  191. if (cmSystemTools::IsSubDirectory(up, r) ||
  192. (stagePrefix && cmSystemTools::IsSubDirectory(up, stagePrefix))) {
  193. rootedDir = up;
  194. } else if (!up.empty() && up[0] != '~') {
  195. // Start with the new root.
  196. rootedDir = r;
  197. rootedDir += "/";
  198. // Append the original path with its old root removed.
  199. rootedDir += cmSystemTools::SplitPathRootComponent(up);
  200. }
  201. // Store the new path.
  202. paths.push_back(rootedDir);
  203. }
  204. }
  205. // If searching both rooted and unrooted paths add the original
  206. // paths again.
  207. if (this->FindRootPathMode == RootPathModeBoth) {
  208. paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
  209. }
  210. }
  211. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  212. {
  213. // null-terminated list of paths.
  214. static const char* paths[] = { "CMAKE_SYSTEM_IGNORE_PATH",
  215. "CMAKE_IGNORE_PATH", nullptr };
  216. // Construct the list of path roots with no trailing slashes.
  217. for (const char** pathName = paths; *pathName; ++pathName) {
  218. // Get the list of paths to ignore from the variable.
  219. const char* ignorePath = this->Makefile->GetDefinition(*pathName);
  220. if ((ignorePath == nullptr) || (strlen(ignorePath) == 0)) {
  221. continue;
  222. }
  223. cmSystemTools::ExpandListArgument(ignorePath, ignore);
  224. }
  225. for (std::string& i : ignore) {
  226. cmSystemTools::ConvertToUnixSlashes(i);
  227. }
  228. }
  229. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  230. {
  231. std::vector<std::string> ignoreVec;
  232. GetIgnoredPaths(ignoreVec);
  233. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  234. }
  235. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  236. {
  237. if (arg == "NO_DEFAULT_PATH") {
  238. this->NoDefaultPath = true;
  239. } else if (arg == "NO_PACKAGE_ROOT_PATH") {
  240. this->NoPackageRootPath = true;
  241. } else if (arg == "NO_CMAKE_PATH") {
  242. this->NoCMakePath = true;
  243. } else if (arg == "NO_CMAKE_ENVIRONMENT_PATH") {
  244. this->NoCMakeEnvironmentPath = true;
  245. } else if (arg == "NO_SYSTEM_ENVIRONMENT_PATH") {
  246. this->NoSystemEnvironmentPath = true;
  247. } else if (arg == "NO_CMAKE_SYSTEM_PATH") {
  248. this->NoCMakeSystemPath = true;
  249. } else if (arg == "NO_CMAKE_FIND_ROOT_PATH") {
  250. this->FindRootPathMode = RootPathModeNever;
  251. } else if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") {
  252. this->FindRootPathMode = RootPathModeOnly;
  253. } else if (arg == "CMAKE_FIND_ROOT_PATH_BOTH") {
  254. this->FindRootPathMode = RootPathModeBoth;
  255. } else {
  256. // The argument is not one of the above.
  257. return false;
  258. }
  259. // The argument is one of the above.
  260. return true;
  261. }
  262. void cmFindCommon::AddPathSuffix(std::string const& arg)
  263. {
  264. std::string suffix = arg;
  265. // Strip leading and trailing slashes.
  266. if (suffix.empty()) {
  267. return;
  268. }
  269. if (suffix[0] == '/') {
  270. suffix = suffix.substr(1);
  271. }
  272. if (suffix.empty()) {
  273. return;
  274. }
  275. if (suffix[suffix.size() - 1] == '/') {
  276. suffix = suffix.substr(0, suffix.size() - 1);
  277. }
  278. if (suffix.empty()) {
  279. return;
  280. }
  281. // Store the suffix.
  282. this->SearchPathSuffixes.push_back(std::move(suffix));
  283. }
  284. void AddTrailingSlash(std::string& s)
  285. {
  286. if (!s.empty() && *s.rbegin() != '/') {
  287. s += '/';
  288. }
  289. }
  290. void cmFindCommon::ComputeFinalPaths()
  291. {
  292. // Filter out ignored paths from the prefix list
  293. std::set<std::string> ignored;
  294. this->GetIgnoredPaths(ignored);
  295. // Combine the separate path types, filtering out ignores
  296. this->SearchPaths.clear();
  297. std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All];
  298. for (PathLabel const& l : allLabels) {
  299. this->LabeledPaths[l].ExtractWithout(ignored, this->SearchPaths);
  300. }
  301. // Expand list of paths inside all search roots.
  302. this->RerootPaths(this->SearchPaths);
  303. // Add a trailing slash to all paths to aid the search process.
  304. std::for_each(this->SearchPaths.begin(), this->SearchPaths.end(),
  305. &AddTrailingSlash);
  306. }