cmFindCommon.cxx 12 KB

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