cmFindCommon.cxx 13 KB

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