cmFindCommon.cxx 13 KB

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