cmFindCommon.cxx 13 KB

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