cmFindCommon.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. "SYSTEM_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.emplace(PathLabel::PackageRoot, cmSearchPath(this));
  104. this->LabeledPaths.emplace(PathLabel::CMake, cmSearchPath(this));
  105. this->LabeledPaths.emplace(PathLabel::CMakeEnvironment, cmSearchPath(this));
  106. this->LabeledPaths.emplace(PathLabel::Hints, cmSearchPath(this));
  107. this->LabeledPaths.emplace(PathLabel::SystemEnvironment, cmSearchPath(this));
  108. this->LabeledPaths.emplace(PathLabel::CMakeSystem, cmSearchPath(this));
  109. this->LabeledPaths.emplace(PathLabel::Guess, cmSearchPath(this));
  110. }
  111. void cmFindCommon::SelectDefaultRootPathMode()
  112. {
  113. // Check the policy variable for this find command type.
  114. std::string findRootPathVar =
  115. cmStrCat("CMAKE_FIND_ROOT_PATH_MODE_", this->CMakePathName);
  116. std::string rootPathMode =
  117. this->Makefile->GetSafeDefinition(findRootPathVar);
  118. if (rootPathMode == "NEVER") {
  119. this->FindRootPathMode = RootPathModeNever;
  120. } else if (rootPathMode == "ONLY") {
  121. this->FindRootPathMode = RootPathModeOnly;
  122. } else if (rootPathMode == "BOTH") {
  123. this->FindRootPathMode = RootPathModeBoth;
  124. }
  125. }
  126. void cmFindCommon::SelectDefaultMacMode()
  127. {
  128. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  129. if (ff == "NEVER") {
  130. this->SearchFrameworkLast = false;
  131. this->SearchFrameworkFirst = false;
  132. this->SearchFrameworkOnly = false;
  133. } else if (ff == "ONLY") {
  134. this->SearchFrameworkLast = false;
  135. this->SearchFrameworkFirst = false;
  136. this->SearchFrameworkOnly = true;
  137. } else if (ff == "FIRST") {
  138. this->SearchFrameworkLast = false;
  139. this->SearchFrameworkFirst = true;
  140. this->SearchFrameworkOnly = false;
  141. } else if (ff == "LAST") {
  142. this->SearchFrameworkLast = true;
  143. this->SearchFrameworkFirst = false;
  144. this->SearchFrameworkOnly = false;
  145. }
  146. std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
  147. if (fab == "NEVER") {
  148. this->SearchAppBundleLast = false;
  149. this->SearchAppBundleFirst = false;
  150. this->SearchAppBundleOnly = false;
  151. } else if (fab == "ONLY") {
  152. this->SearchAppBundleLast = false;
  153. this->SearchAppBundleFirst = false;
  154. this->SearchAppBundleOnly = true;
  155. } else if (fab == "FIRST") {
  156. this->SearchAppBundleLast = false;
  157. this->SearchAppBundleFirst = true;
  158. this->SearchAppBundleOnly = false;
  159. } else if (fab == "LAST") {
  160. this->SearchAppBundleLast = true;
  161. this->SearchAppBundleFirst = false;
  162. this->SearchAppBundleOnly = false;
  163. }
  164. }
  165. void cmFindCommon::SelectDefaultSearchModes()
  166. {
  167. const std::array<std::pair<bool&, std::string>, 6> search_paths = {
  168. { { this->NoPackageRootPath, "CMAKE_FIND_USE_PACKAGE_ROOT_PATH" },
  169. { this->NoCMakePath, "CMAKE_FIND_USE_CMAKE_PATH" },
  170. { this->NoCMakeEnvironmentPath,
  171. "CMAKE_FIND_USE_CMAKE_ENVIRONMENT_PATH" },
  172. { this->NoSystemEnvironmentPath,
  173. "CMAKE_FIND_USE_SYSTEM_ENVIRONMENT_PATH" },
  174. { this->NoCMakeSystemPath, "CMAKE_FIND_USE_CMAKE_SYSTEM_PATH" },
  175. { this->NoCMakeInstallPath, "CMAKE_FIND_USE_INSTALL_PREFIX" } }
  176. };
  177. for (auto const& path : search_paths) {
  178. cmValue def = this->Makefile->GetDefinition(path.second);
  179. if (def) {
  180. path.first = !def.IsOn();
  181. }
  182. }
  183. }
  184. void cmFindCommon::RerootPaths(std::vector<std::string>& paths,
  185. std::string* debugBuffer)
  186. {
  187. // Short-circuit if there is nothing to do.
  188. if (this->FindRootPathMode == RootPathModeNever) {
  189. return;
  190. }
  191. cmValue sysroot = this->Makefile->GetDefinition("CMAKE_SYSROOT");
  192. cmValue sysrootCompile =
  193. this->Makefile->GetDefinition("CMAKE_SYSROOT_COMPILE");
  194. cmValue sysrootLink = this->Makefile->GetDefinition("CMAKE_SYSROOT_LINK");
  195. cmValue rootPath = this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  196. const bool noSysroot = !cmNonempty(sysroot);
  197. const bool noCompileSysroot = !cmNonempty(sysrootCompile);
  198. const bool noLinkSysroot = !cmNonempty(sysrootLink);
  199. const bool noRootPath = !cmNonempty(rootPath);
  200. if (noSysroot && noCompileSysroot && noLinkSysroot && noRootPath) {
  201. return;
  202. }
  203. if (this->DebugMode && debugBuffer) {
  204. *debugBuffer = cmStrCat(
  205. *debugBuffer, "Prepending the following roots to each prefix:\n");
  206. }
  207. auto debugRoot = [this, debugBuffer](const std::string& name,
  208. cmValue value) {
  209. if (this->DebugMode && debugBuffer) {
  210. *debugBuffer = cmStrCat(*debugBuffer, name, '\n');
  211. cmList roots{ value };
  212. if (roots.empty()) {
  213. *debugBuffer = cmStrCat(*debugBuffer, " none\n");
  214. }
  215. for (auto const& root : roots) {
  216. *debugBuffer = cmStrCat(*debugBuffer, " ", root, '\n');
  217. }
  218. }
  219. };
  220. // Construct the list of path roots with no trailing slashes.
  221. cmList roots;
  222. debugRoot("CMAKE_FIND_ROOT_PATH", rootPath);
  223. if (cmNonempty(rootPath)) {
  224. roots.assign(*rootPath);
  225. }
  226. debugRoot("CMAKE_SYSROOT_COMPILE", sysrootCompile);
  227. if (cmNonempty(sysrootCompile)) {
  228. roots.emplace_back(*sysrootCompile);
  229. }
  230. debugRoot("CMAKE_SYSROOT_LINK", sysrootLink);
  231. if (cmNonempty(sysrootLink)) {
  232. roots.emplace_back(*sysrootLink);
  233. }
  234. debugRoot("CMAKE_SYSROOT", sysroot);
  235. if (cmNonempty(sysroot)) {
  236. roots.emplace_back(*sysroot);
  237. }
  238. for (auto& r : roots) {
  239. cmSystemTools::ConvertToUnixSlashes(r);
  240. }
  241. cmValue stagePrefix = this->Makefile->GetDefinition("CMAKE_STAGING_PREFIX");
  242. // Copy the original set of unrooted paths.
  243. auto unrootedPaths = paths;
  244. paths.clear();
  245. auto isSameDirectoryOrSubDirectory = [](std::string const& l,
  246. std::string const& r) {
  247. return (cmSystemTools::GetRealPath(l) == cmSystemTools::GetRealPath(r)) ||
  248. cmSystemTools::IsSubDirectory(l, r);
  249. };
  250. for (auto const& r : roots) {
  251. for (auto const& up : unrootedPaths) {
  252. // Place the unrooted path under the current root if it is not
  253. // already inside. Skip the unrooted path if it is relative to
  254. // a user home directory or is empty.
  255. std::string rootedDir;
  256. if (isSameDirectoryOrSubDirectory(up, r) ||
  257. (stagePrefix && isSameDirectoryOrSubDirectory(up, *stagePrefix))) {
  258. rootedDir = up;
  259. } else if (!up.empty() && up[0] != '~') {
  260. auto const* split = cmSystemTools::SplitPathRootComponent(up);
  261. if (split && *split) {
  262. // Start with the new root.
  263. rootedDir = cmStrCat(r, '/', split);
  264. } else {
  265. rootedDir = r;
  266. }
  267. }
  268. // Store the new path.
  269. paths.push_back(rootedDir);
  270. }
  271. }
  272. // If searching both rooted and unrooted paths add the original
  273. // paths again.
  274. if (this->FindRootPathMode == RootPathModeBoth) {
  275. cm::append(paths, unrootedPaths);
  276. }
  277. }
  278. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  279. {
  280. const std::array<const char*, 2> paths = { { "CMAKE_SYSTEM_IGNORE_PATH",
  281. "CMAKE_IGNORE_PATH" } };
  282. // Construct the list of path roots with no trailing slashes.
  283. for (const char* pathName : paths) {
  284. // Get the list of paths to ignore from the variable.
  285. cmList::append(ignore, this->Makefile->GetDefinition(pathName));
  286. }
  287. for (std::string& i : ignore) {
  288. cmSystemTools::ConvertToUnixSlashes(i);
  289. }
  290. }
  291. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  292. {
  293. std::vector<std::string> ignoreVec;
  294. this->GetIgnoredPaths(ignoreVec);
  295. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  296. }
  297. void cmFindCommon::GetIgnoredPrefixPaths(std::vector<std::string>& ignore)
  298. {
  299. const std::array<const char*, 2> paths = {
  300. { "CMAKE_SYSTEM_IGNORE_PREFIX_PATH", "CMAKE_IGNORE_PREFIX_PATH" }
  301. };
  302. // Construct the list of path roots with no trailing slashes.
  303. for (const char* pathName : paths) {
  304. // Get the list of paths to ignore from the variable.
  305. cmList::append(ignore, this->Makefile->GetDefinition(pathName));
  306. }
  307. for (std::string& i : ignore) {
  308. cmSystemTools::ConvertToUnixSlashes(i);
  309. }
  310. }
  311. void cmFindCommon::GetIgnoredPrefixPaths(std::set<std::string>& ignore)
  312. {
  313. std::vector<std::string> ignoreVec;
  314. this->GetIgnoredPrefixPaths(ignoreVec);
  315. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  316. }
  317. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  318. {
  319. if (arg == "NO_DEFAULT_PATH") {
  320. this->NoDefaultPath = true;
  321. return true;
  322. }
  323. if (arg == "NO_PACKAGE_ROOT_PATH") {
  324. this->NoPackageRootPath = true;
  325. return true;
  326. }
  327. if (arg == "NO_CMAKE_PATH") {
  328. this->NoCMakePath = true;
  329. return true;
  330. }
  331. if (arg == "NO_CMAKE_ENVIRONMENT_PATH") {
  332. this->NoCMakeEnvironmentPath = true;
  333. return true;
  334. }
  335. if (arg == "NO_SYSTEM_ENVIRONMENT_PATH") {
  336. this->NoSystemEnvironmentPath = true;
  337. return true;
  338. }
  339. if (arg == "NO_CMAKE_SYSTEM_PATH") {
  340. this->NoCMakeSystemPath = true;
  341. return true;
  342. }
  343. if (arg == "NO_CMAKE_INSTALL_PREFIX") {
  344. this->NoCMakeInstallPath = true;
  345. return true;
  346. }
  347. if (arg == "NO_CMAKE_FIND_ROOT_PATH") {
  348. this->FindRootPathMode = RootPathModeNever;
  349. return true;
  350. }
  351. if (arg == "ONLY_CMAKE_FIND_ROOT_PATH") {
  352. this->FindRootPathMode = RootPathModeOnly;
  353. return true;
  354. }
  355. if (arg == "CMAKE_FIND_ROOT_PATH_BOTH") {
  356. this->FindRootPathMode = RootPathModeBoth;
  357. return true;
  358. }
  359. // The argument is not one of the above.
  360. return false;
  361. }
  362. void cmFindCommon::AddPathSuffix(std::string const& arg)
  363. {
  364. std::string suffix = arg;
  365. // Strip leading and trailing slashes.
  366. if (suffix.empty()) {
  367. return;
  368. }
  369. if (suffix.front() == '/') {
  370. suffix = suffix.substr(1);
  371. }
  372. if (suffix.empty()) {
  373. return;
  374. }
  375. if (suffix.back() == '/') {
  376. suffix = suffix.substr(0, suffix.size() - 1);
  377. }
  378. if (suffix.empty()) {
  379. return;
  380. }
  381. // Store the suffix.
  382. this->SearchPathSuffixes.push_back(std::move(suffix));
  383. }
  384. void cmFindCommon::ComputeFinalPaths(IgnorePaths ignorePaths,
  385. std::string* debugBuffer)
  386. {
  387. // Filter out ignored paths from the prefix list
  388. std::set<std::string> ignoredPaths;
  389. std::set<std::string> ignoredPrefixes;
  390. if (ignorePaths == IgnorePaths::Yes) {
  391. this->GetIgnoredPaths(ignoredPaths);
  392. this->GetIgnoredPrefixPaths(ignoredPrefixes);
  393. }
  394. // Combine the separate path types, filtering out ignores
  395. this->SearchPaths.clear();
  396. std::vector<PathLabel>& allLabels = this->PathGroupLabelMap[PathGroup::All];
  397. for (PathLabel const& l : allLabels) {
  398. this->LabeledPaths[l].ExtractWithout(ignoredPaths, ignoredPrefixes,
  399. this->SearchPaths);
  400. }
  401. // Expand list of paths inside all search roots.
  402. this->RerootPaths(this->SearchPaths, debugBuffer);
  403. // Add a trailing slash to all paths to aid the search process.
  404. std::for_each(this->SearchPaths.begin(), this->SearchPaths.end(),
  405. [](std::string& s) {
  406. if (!s.empty() && s.back() != '/') {
  407. s += '/';
  408. }
  409. });
  410. }