cmFindCommon.cxx 13 KB

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