cmFindCommon.cxx 11 KB

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