cmFindCommon.cxx 11 KB

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