cmFindCommon.cxx 15 KB

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