cmFindCommon.cxx 12 KB

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