cmFindCommon.cxx 11 KB

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