1
0

cmFindCommon.cxx 11 KB

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