cmFindCommon.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. void cmFindCommon::FilterPaths(std::vector<std::string>& paths,
  226. const std::set<std::string>& ignore)
  227. {
  228. // Now filter out anything that's in the ignore set.
  229. std::vector<std::string> unfiltered;
  230. unfiltered.swap(paths);
  231. for(std::vector<std::string>::iterator pi = unfiltered.begin();
  232. pi != unfiltered.end(); ++pi)
  233. {
  234. if (ignore.count(*pi) == 0)
  235. {
  236. paths.push_back(*pi);
  237. }
  238. }
  239. }
  240. //----------------------------------------------------------------------------
  241. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  242. {
  243. // null-terminated list of paths.
  244. static const char *paths[] =
  245. { "CMAKE_SYSTEM_IGNORE_PATH", "CMAKE_IGNORE_PATH", 0 };
  246. // Construct the list of path roots with no trailing slashes.
  247. for(const char **pathName = paths; *pathName; ++pathName)
  248. {
  249. // Get the list of paths to ignore from the variable.
  250. const char* ignorePath = this->Makefile->GetDefinition(*pathName);
  251. if((ignorePath == 0) || (strlen(ignorePath) == 0))
  252. {
  253. continue;
  254. }
  255. cmSystemTools::ExpandListArgument(ignorePath, ignore);
  256. }
  257. for(std::vector<std::string>::iterator i = ignore.begin();
  258. i != ignore.end(); ++i)
  259. {
  260. cmSystemTools::ConvertToUnixSlashes(*i);
  261. }
  262. }
  263. //----------------------------------------------------------------------------
  264. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  265. {
  266. std::vector<std::string> ignoreVec;
  267. GetIgnoredPaths(ignoreVec);
  268. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  269. }
  270. //----------------------------------------------------------------------------
  271. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  272. {
  273. if(arg == "NO_DEFAULT_PATH")
  274. {
  275. this->NoDefaultPath = true;
  276. }
  277. else if(arg == "NO_CMAKE_ENVIRONMENT_PATH")
  278. {
  279. this->NoCMakeEnvironmentPath = true;
  280. }
  281. else if(arg == "NO_CMAKE_PATH")
  282. {
  283. this->NoCMakePath = true;
  284. }
  285. else if(arg == "NO_SYSTEM_ENVIRONMENT_PATH")
  286. {
  287. this->NoSystemEnvironmentPath = true;
  288. }
  289. else if(arg == "NO_CMAKE_SYSTEM_PATH")
  290. {
  291. this->NoCMakeSystemPath = true;
  292. }
  293. else if(arg == "NO_CMAKE_FIND_ROOT_PATH")
  294. {
  295. this->FindRootPathMode = RootPathModeNoRootPath;
  296. }
  297. else if(arg == "ONLY_CMAKE_FIND_ROOT_PATH")
  298. {
  299. this->FindRootPathMode = RootPathModeOnlyRootPath;
  300. }
  301. else if(arg == "CMAKE_FIND_ROOT_PATH_BOTH")
  302. {
  303. this->FindRootPathMode = RootPathModeBoth;
  304. }
  305. else
  306. {
  307. // The argument is not one of the above.
  308. return false;
  309. }
  310. // The argument is one of the above.
  311. return true;
  312. }
  313. //----------------------------------------------------------------------------
  314. void cmFindCommon::AddPathSuffix(std::string const& arg)
  315. {
  316. std::string suffix = arg;
  317. // Strip leading and trailing slashes.
  318. if(suffix.empty())
  319. {
  320. return;
  321. }
  322. if(suffix[0] == '/')
  323. {
  324. suffix = suffix.substr(1, suffix.npos);
  325. }
  326. if(suffix.empty())
  327. {
  328. return;
  329. }
  330. if(suffix[suffix.size()-1] == '/')
  331. {
  332. suffix = suffix.substr(0, suffix.size()-1);
  333. }
  334. if(suffix.empty())
  335. {
  336. return;
  337. }
  338. // Store the suffix.
  339. this->SearchPathSuffixes.push_back(suffix);
  340. }
  341. //----------------------------------------------------------------------------
  342. void cmFindCommon::AddUserPath(std::string const& p,
  343. std::vector<std::string>& paths)
  344. {
  345. // We should view the registry as the target application would view
  346. // it.
  347. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  348. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  349. if(this->Makefile->PlatformIs64Bit())
  350. {
  351. view = cmSystemTools::KeyWOW64_64;
  352. other_view = cmSystemTools::KeyWOW64_32;
  353. }
  354. // Expand using the view of the target application.
  355. std::string expanded = p;
  356. cmSystemTools::ExpandRegistryValues(expanded, view);
  357. cmSystemTools::GlobDirs(expanded.c_str(), paths);
  358. // Executables can be either 32-bit or 64-bit, so expand using the
  359. // alternative view.
  360. if(expanded != p && this->CMakePathName == "PROGRAM")
  361. {
  362. expanded = p;
  363. cmSystemTools::ExpandRegistryValues(expanded, other_view);
  364. cmSystemTools::GlobDirs(expanded.c_str(), paths);
  365. }
  366. }
  367. //----------------------------------------------------------------------------
  368. void cmFindCommon::AddCMakePath(const char* variable)
  369. {
  370. // Get a path from a CMake variable.
  371. if(const char* varPath = this->Makefile->GetDefinition(variable))
  372. {
  373. std::vector<std::string> tmp;
  374. cmSystemTools::ExpandListArgument(varPath, tmp);
  375. // Relative paths are interpreted with respect to the current
  376. // source directory.
  377. this->AddPathsInternal(tmp, CMakePath);
  378. }
  379. }
  380. //----------------------------------------------------------------------------
  381. void cmFindCommon::AddEnvPath(const char* variable)
  382. {
  383. // Get a path from the environment.
  384. std::vector<std::string> tmp;
  385. cmSystemTools::GetPath(tmp, variable);
  386. // Relative paths are interpreted with respect to the current
  387. // working directory.
  388. this->AddPathsInternal(tmp, EnvPath);
  389. }
  390. //----------------------------------------------------------------------------
  391. void cmFindCommon::AddPathsInternal(std::vector<std::string> const& in_paths,
  392. PathType pathType)
  393. {
  394. for(std::vector<std::string>::const_iterator i = in_paths.begin();
  395. i != in_paths.end(); ++i)
  396. {
  397. this->AddPathInternal(*i, pathType);
  398. }
  399. }
  400. //----------------------------------------------------------------------------
  401. void cmFindCommon::AddPathInternal(std::string const& in_path,
  402. PathType pathType)
  403. {
  404. if(in_path.empty())
  405. {
  406. return;
  407. }
  408. // Select the base path with which to interpret relative paths.
  409. const char* relbase = 0;
  410. if(pathType == CMakePath)
  411. {
  412. relbase = this->Makefile->GetCurrentDirectory();
  413. }
  414. // Convert to clean full path.
  415. std::string fullPath =
  416. cmSystemTools::CollapseFullPath(in_path.c_str(), relbase);
  417. // Insert the path if has not already been emitted.
  418. if(this->SearchPathsEmitted.insert(fullPath).second)
  419. {
  420. this->SearchPaths.push_back(fullPath.c_str());
  421. }
  422. }
  423. //----------------------------------------------------------------------------
  424. void cmFindCommon::AddTrailingSlashes(std::vector<std::string>& paths)
  425. {
  426. // Add a trailing slash to all paths to aid the search process.
  427. for(std::vector<std::string>::iterator i = paths.begin();
  428. i != paths.end(); ++i)
  429. {
  430. std::string& p = *i;
  431. if(!p.empty() && p[p.size()-1] != '/')
  432. {
  433. p += "/";
  434. }
  435. }
  436. }
  437. //----------------------------------------------------------------------------
  438. void cmFindCommon::SetMakefile(cmMakefile* makefile)
  439. {
  440. cmCommand::SetMakefile(makefile);
  441. // If we are building for Apple (OSX or also iphone), make sure
  442. // that frameworks and bundles are searched first.
  443. if(this->Makefile->IsOn("APPLE"))
  444. {
  445. this->SearchFrameworkFirst = true;
  446. this->SearchAppBundleFirst = true;
  447. }
  448. }