cmFindCommon.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFindCommon.h"
  11. //----------------------------------------------------------------------------
  12. cmFindCommon::cmFindCommon()
  13. {
  14. this->FindRootPathMode = RootPathModeBoth;
  15. this->NoDefaultPath = false;
  16. this->NoCMakePath = false;
  17. this->NoCMakeEnvironmentPath = false;
  18. this->NoSystemEnvironmentPath = false;
  19. this->NoCMakeSystemPath = false;
  20. // OS X Bundle and Framework search policy. The default is to
  21. // search frameworks first on apple.
  22. #if defined(__APPLE__)
  23. this->SearchFrameworkFirst = true;
  24. this->SearchAppBundleFirst = true;
  25. #else
  26. this->SearchFrameworkFirst = false;
  27. this->SearchAppBundleFirst = false;
  28. #endif
  29. this->SearchFrameworkOnly = false;
  30. this->SearchFrameworkLast = false;
  31. this->SearchAppBundleOnly = false;
  32. this->SearchAppBundleLast = false;
  33. // Documentation components.
  34. this->GenericDocumentationMacPolicy =
  35. "On Darwin or systems supporting OS X Frameworks, the cmake variable"
  36. " CMAKE_FIND_FRAMEWORK can be set to empty or one of the following:\n"
  37. " \"FIRST\" - Try to find frameworks before standard\n"
  38. " libraries or headers. This is the default on Darwin.\n"
  39. " \"LAST\" - Try to find frameworks after standard\n"
  40. " libraries or headers.\n"
  41. " \"ONLY\" - Only try to find frameworks.\n"
  42. " \"NEVER\" - Never try to find frameworks.\n"
  43. "On Darwin or systems supporting OS X Application Bundles, the cmake "
  44. "variable CMAKE_FIND_APPBUNDLE can be set to empty or one of the "
  45. "following:\n"
  46. " \"FIRST\" - Try to find application bundles before standard\n"
  47. " programs. This is the default on Darwin.\n"
  48. " \"LAST\" - Try to find application bundles after standard\n"
  49. " programs.\n"
  50. " \"ONLY\" - Only try to find application bundles.\n"
  51. " \"NEVER\" - Never try to find application bundles.\n";
  52. this->GenericDocumentationRootPath =
  53. "The CMake variable CMAKE_FIND_ROOT_PATH specifies one or more "
  54. "directories to be prepended to all other search directories. "
  55. "This effectively \"re-roots\" the entire search under given locations. "
  56. "By default it is empty. It is especially useful when "
  57. "cross-compiling to point to the root directory of the "
  58. "target environment and CMake will search there too. By default at first "
  59. "the directories listed in CMAKE_FIND_ROOT_PATH and then the non-rooted "
  60. "directories will be searched. "
  61. "The default behavior can be adjusted by setting "
  62. "CMAKE_FIND_ROOT_PATH_MODE_XXX. This behavior can be manually "
  63. "overridden on a per-call basis. "
  64. "By using CMAKE_FIND_ROOT_PATH_BOTH the search order will "
  65. "be as described above. If NO_CMAKE_FIND_ROOT_PATH is used "
  66. "then CMAKE_FIND_ROOT_PATH will not be used. If ONLY_CMAKE_FIND_ROOT_PATH "
  67. "is used then only the re-rooted directories will be searched.\n";
  68. this->GenericDocumentationPathsOrder =
  69. "The default search order is designed to be most-specific to "
  70. "least-specific for common use cases. "
  71. "Projects may override the order by simply calling the command "
  72. "multiple times and using the NO_* options:\n"
  73. " FIND_XXX(FIND_ARGS_XXX PATHS paths... NO_DEFAULT_PATH)\n"
  74. " FIND_XXX(FIND_ARGS_XXX)\n"
  75. "Once one of the calls succeeds the result variable will be set "
  76. "and stored in the cache so that no call will search again.";
  77. }
  78. //----------------------------------------------------------------------------
  79. cmFindCommon::~cmFindCommon()
  80. {
  81. }
  82. //----------------------------------------------------------------------------
  83. void cmFindCommon::SelectDefaultRootPathMode()
  84. {
  85. // Use both by default.
  86. this->FindRootPathMode = RootPathModeBoth;
  87. // Check the policy variable for this find command type.
  88. std::string findRootPathVar = "CMAKE_FIND_ROOT_PATH_MODE_";
  89. findRootPathVar += this->CMakePathName;
  90. std::string rootPathMode =
  91. this->Makefile->GetSafeDefinition(findRootPathVar.c_str());
  92. if (rootPathMode=="NEVER")
  93. {
  94. this->FindRootPathMode = RootPathModeNoRootPath;
  95. }
  96. else if (rootPathMode=="ONLY")
  97. {
  98. this->FindRootPathMode = RootPathModeOnlyRootPath;
  99. }
  100. else if (rootPathMode=="BOTH")
  101. {
  102. this->FindRootPathMode = RootPathModeBoth;
  103. }
  104. }
  105. //----------------------------------------------------------------------------
  106. void cmFindCommon::SelectDefaultMacMode()
  107. {
  108. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  109. if(ff == "NEVER")
  110. {
  111. this->SearchFrameworkLast = false;
  112. this->SearchFrameworkFirst = false;
  113. this->SearchFrameworkOnly = false;
  114. }
  115. else if(ff == "ONLY")
  116. {
  117. this->SearchFrameworkLast = false;
  118. this->SearchFrameworkFirst = false;
  119. this->SearchFrameworkOnly = true;
  120. }
  121. else if(ff == "FIRST")
  122. {
  123. this->SearchFrameworkLast = false;
  124. this->SearchFrameworkFirst = true;
  125. this->SearchFrameworkOnly = false;
  126. }
  127. else if(ff == "LAST")
  128. {
  129. this->SearchFrameworkLast = true;
  130. this->SearchFrameworkFirst = false;
  131. this->SearchFrameworkOnly = false;
  132. }
  133. std::string fab = this->Makefile->GetSafeDefinition("CMAKE_FIND_APPBUNDLE");
  134. if(fab == "NEVER")
  135. {
  136. this->SearchAppBundleLast = false;
  137. this->SearchAppBundleFirst = false;
  138. this->SearchAppBundleOnly = false;
  139. }
  140. else if(fab == "ONLY")
  141. {
  142. this->SearchAppBundleLast = false;
  143. this->SearchAppBundleFirst = false;
  144. this->SearchAppBundleOnly = true;
  145. }
  146. else if(fab == "FIRST")
  147. {
  148. this->SearchAppBundleLast = false;
  149. this->SearchAppBundleFirst = true;
  150. this->SearchAppBundleOnly = false;
  151. }
  152. else if(fab == "LAST")
  153. {
  154. this->SearchAppBundleLast = true;
  155. this->SearchAppBundleFirst = false;
  156. this->SearchAppBundleOnly = false;
  157. }
  158. }
  159. //----------------------------------------------------------------------------
  160. void cmFindCommon::RerootPaths(std::vector<std::string>& paths)
  161. {
  162. #if 0
  163. for(std::vector<std::string>::const_iterator i = paths.begin();
  164. i != paths.end(); ++i)
  165. {
  166. fprintf(stderr, "[%s]\n", i->c_str());
  167. }
  168. #endif
  169. // Short-circuit if there is nothing to do.
  170. if(this->FindRootPathMode == RootPathModeNoRootPath)
  171. {
  172. return;
  173. }
  174. const char* rootPath =
  175. this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  176. if((rootPath == 0) || (strlen(rootPath) == 0))
  177. {
  178. return;
  179. }
  180. // Construct the list of path roots with no trailing slashes.
  181. std::vector<std::string> roots;
  182. cmSystemTools::ExpandListArgument(rootPath, roots);
  183. for(std::vector<std::string>::iterator ri = roots.begin();
  184. ri != roots.end(); ++ri)
  185. {
  186. cmSystemTools::ConvertToUnixSlashes(*ri);
  187. }
  188. // Copy the original set of unrooted paths.
  189. std::vector<std::string> unrootedPaths = paths;
  190. paths.clear();
  191. for(std::vector<std::string>::const_iterator ri = roots.begin();
  192. ri != roots.end(); ++ri)
  193. {
  194. for(std::vector<std::string>::const_iterator ui = unrootedPaths.begin();
  195. ui != unrootedPaths.end(); ++ui)
  196. {
  197. // Place the unrooted path under the current root if it is not
  198. // already inside. Skip the unrooted path if it is relative to
  199. // a user home directory or is empty.
  200. std::string rootedDir;
  201. if(cmSystemTools::IsSubDirectory(ui->c_str(), ri->c_str()))
  202. {
  203. rootedDir = *ui;
  204. }
  205. else if(!ui->empty() && (*ui)[0] != '~')
  206. {
  207. // Start with the new root.
  208. rootedDir = *ri;
  209. rootedDir += "/";
  210. // Append the original path with its old root removed.
  211. rootedDir += cmSystemTools::SplitPathRootComponent(ui->c_str());
  212. }
  213. // Store the new path.
  214. paths.push_back(rootedDir);
  215. }
  216. }
  217. // If searching both rooted and unrooted paths add the original
  218. // paths again.
  219. if(this->FindRootPathMode == RootPathModeBoth)
  220. {
  221. paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
  222. }
  223. }
  224. //----------------------------------------------------------------------------
  225. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  226. {
  227. if(arg == "NO_DEFAULT_PATH")
  228. {
  229. this->NoDefaultPath = true;
  230. }
  231. else if(arg == "NO_CMAKE_ENVIRONMENT_PATH")
  232. {
  233. this->NoCMakeEnvironmentPath = true;
  234. }
  235. else if(arg == "NO_CMAKE_PATH")
  236. {
  237. this->NoCMakePath = true;
  238. }
  239. else if(arg == "NO_SYSTEM_ENVIRONMENT_PATH")
  240. {
  241. this->NoSystemEnvironmentPath = true;
  242. }
  243. else if(arg == "NO_CMAKE_SYSTEM_PATH")
  244. {
  245. this->NoCMakeSystemPath = true;
  246. }
  247. else if(arg == "NO_CMAKE_FIND_ROOT_PATH")
  248. {
  249. this->FindRootPathMode = RootPathModeNoRootPath;
  250. }
  251. else if(arg == "ONLY_CMAKE_FIND_ROOT_PATH")
  252. {
  253. this->FindRootPathMode = RootPathModeOnlyRootPath;
  254. }
  255. else if(arg == "CMAKE_FIND_ROOT_PATH_BOTH")
  256. {
  257. this->FindRootPathMode = RootPathModeBoth;
  258. }
  259. else
  260. {
  261. // The argument is not one of the above.
  262. return false;
  263. }
  264. // The argument is one of the above.
  265. return true;
  266. }
  267. //----------------------------------------------------------------------------
  268. void cmFindCommon::AddPathSuffix(std::string const& arg)
  269. {
  270. std::string suffix = arg;
  271. // Strip leading and trailing slashes.
  272. if(suffix.empty())
  273. {
  274. return;
  275. }
  276. if(suffix[0] == '/')
  277. {
  278. suffix = suffix.substr(1, suffix.npos);
  279. }
  280. if(suffix.empty())
  281. {
  282. return;
  283. }
  284. if(suffix[suffix.size()-1] == '/')
  285. {
  286. suffix = suffix.substr(0, suffix.size()-1);
  287. }
  288. if(suffix.empty())
  289. {
  290. return;
  291. }
  292. // Store the suffix.
  293. this->SearchPathSuffixes.push_back(suffix);
  294. }
  295. //----------------------------------------------------------------------------
  296. void cmFindCommon::AddUserPath(std::string const& p,
  297. std::vector<std::string>& paths)
  298. {
  299. // We should view the registry as the target application would view
  300. // it.
  301. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  302. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  303. if(this->Makefile->PlatformIs64Bit())
  304. {
  305. view = cmSystemTools::KeyWOW64_64;
  306. other_view = cmSystemTools::KeyWOW64_32;
  307. }
  308. // Expand using the view of the target application.
  309. std::string expanded = p;
  310. cmSystemTools::ExpandRegistryValues(expanded, view);
  311. cmSystemTools::GlobDirs(expanded.c_str(), paths);
  312. // Executables can be either 32-bit or 64-bit, so expand using the
  313. // alternative view.
  314. if(expanded != p && this->CMakePathName == "PROGRAM")
  315. {
  316. expanded = p;
  317. cmSystemTools::ExpandRegistryValues(expanded, other_view);
  318. cmSystemTools::GlobDirs(expanded.c_str(), paths);
  319. }
  320. }
  321. //----------------------------------------------------------------------------
  322. void cmFindCommon::AddCMakePath(const char* variable)
  323. {
  324. // Get a path from a CMake variable.
  325. if(const char* varPath = this->Makefile->GetDefinition(variable))
  326. {
  327. std::vector<std::string> tmp;
  328. cmSystemTools::ExpandListArgument(varPath, tmp);
  329. // Relative paths are interpreted with respect to the current
  330. // source directory.
  331. this->AddPathsInternal(tmp, CMakePath);
  332. }
  333. }
  334. //----------------------------------------------------------------------------
  335. void cmFindCommon::AddEnvPath(const char* variable)
  336. {
  337. // Get a path from the environment.
  338. std::vector<std::string> tmp;
  339. cmSystemTools::GetPath(tmp, variable);
  340. // Relative paths are interpreted with respect to the current
  341. // working directory.
  342. this->AddPathsInternal(tmp, EnvPath);
  343. }
  344. //----------------------------------------------------------------------------
  345. void cmFindCommon::AddPathsInternal(std::vector<std::string> const& in_paths,
  346. PathType pathType)
  347. {
  348. for(std::vector<std::string>::const_iterator i = in_paths.begin();
  349. i != in_paths.end(); ++i)
  350. {
  351. this->AddPathInternal(*i, pathType);
  352. }
  353. }
  354. //----------------------------------------------------------------------------
  355. void cmFindCommon::AddPathInternal(std::string const& in_path,
  356. PathType pathType)
  357. {
  358. if(in_path.empty())
  359. {
  360. return;
  361. }
  362. // Select the base path with which to interpret relative paths.
  363. const char* relbase = 0;
  364. if(pathType == CMakePath)
  365. {
  366. relbase = this->Makefile->GetCurrentDirectory();
  367. }
  368. // Convert to clean full path.
  369. std::string fullPath =
  370. cmSystemTools::CollapseFullPath(in_path.c_str(), relbase);
  371. // Insert the path if has not already been emitted.
  372. if(this->SearchPathsEmitted.insert(fullPath).second)
  373. {
  374. this->SearchPaths.push_back(fullPath.c_str());
  375. }
  376. }
  377. //----------------------------------------------------------------------------
  378. void cmFindCommon::AddTrailingSlashes(std::vector<std::string>& paths)
  379. {
  380. // Add a trailing slash to all paths to aid the search process.
  381. for(std::vector<std::string>::iterator i = paths.begin();
  382. i != paths.end(); ++i)
  383. {
  384. std::string& p = *i;
  385. if(!p.empty() && p[p.size()-1] != '/')
  386. {
  387. p += "/";
  388. }
  389. }
  390. }