cmFindCommon.cxx 11 KB

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