cmFindCommon.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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* rootPath =
  131. this->Makefile->GetDefinition("CMAKE_FIND_ROOT_PATH");
  132. if((rootPath == 0) || (strlen(rootPath) == 0))
  133. {
  134. return;
  135. }
  136. // Construct the list of path roots with no trailing slashes.
  137. std::vector<std::string> roots;
  138. cmSystemTools::ExpandListArgument(rootPath, roots);
  139. for(std::vector<std::string>::iterator ri = roots.begin();
  140. ri != roots.end(); ++ri)
  141. {
  142. cmSystemTools::ConvertToUnixSlashes(*ri);
  143. }
  144. // Copy the original set of unrooted paths.
  145. std::vector<std::string> unrootedPaths = paths;
  146. paths.clear();
  147. for(std::vector<std::string>::const_iterator ri = roots.begin();
  148. ri != roots.end(); ++ri)
  149. {
  150. for(std::vector<std::string>::const_iterator ui = unrootedPaths.begin();
  151. ui != unrootedPaths.end(); ++ui)
  152. {
  153. // Place the unrooted path under the current root if it is not
  154. // already inside. Skip the unrooted path if it is relative to
  155. // a user home directory or is empty.
  156. std::string rootedDir;
  157. if(cmSystemTools::IsSubDirectory(ui->c_str(), ri->c_str()))
  158. {
  159. rootedDir = *ui;
  160. }
  161. else if(!ui->empty() && (*ui)[0] != '~')
  162. {
  163. // Start with the new root.
  164. rootedDir = *ri;
  165. rootedDir += "/";
  166. // Append the original path with its old root removed.
  167. rootedDir += cmSystemTools::SplitPathRootComponent(ui->c_str());
  168. }
  169. // Store the new path.
  170. paths.push_back(rootedDir);
  171. }
  172. }
  173. // If searching both rooted and unrooted paths add the original
  174. // paths again.
  175. if(this->FindRootPathMode == RootPathModeBoth)
  176. {
  177. paths.insert(paths.end(), unrootedPaths.begin(), unrootedPaths.end());
  178. }
  179. }
  180. //----------------------------------------------------------------------------
  181. void cmFindCommon::FilterPaths(std::vector<std::string>& paths,
  182. const std::set<std::string>& ignore)
  183. {
  184. // Now filter out anything that's in the ignore set.
  185. std::vector<std::string> unfiltered;
  186. unfiltered.swap(paths);
  187. for(std::vector<std::string>::iterator pi = unfiltered.begin();
  188. pi != unfiltered.end(); ++pi)
  189. {
  190. if (ignore.count(*pi) == 0)
  191. {
  192. paths.push_back(*pi);
  193. }
  194. }
  195. }
  196. //----------------------------------------------------------------------------
  197. void cmFindCommon::GetIgnoredPaths(std::vector<std::string>& ignore)
  198. {
  199. // null-terminated list of paths.
  200. static const char *paths[] =
  201. { "CMAKE_SYSTEM_IGNORE_PATH", "CMAKE_IGNORE_PATH", 0 };
  202. // Construct the list of path roots with no trailing slashes.
  203. for(const char **pathName = paths; *pathName; ++pathName)
  204. {
  205. // Get the list of paths to ignore from the variable.
  206. const char* ignorePath = this->Makefile->GetDefinition(*pathName);
  207. if((ignorePath == 0) || (strlen(ignorePath) == 0))
  208. {
  209. continue;
  210. }
  211. cmSystemTools::ExpandListArgument(ignorePath, ignore);
  212. }
  213. for(std::vector<std::string>::iterator i = ignore.begin();
  214. i != ignore.end(); ++i)
  215. {
  216. cmSystemTools::ConvertToUnixSlashes(*i);
  217. }
  218. }
  219. //----------------------------------------------------------------------------
  220. void cmFindCommon::GetIgnoredPaths(std::set<std::string>& ignore)
  221. {
  222. std::vector<std::string> ignoreVec;
  223. GetIgnoredPaths(ignoreVec);
  224. ignore.insert(ignoreVec.begin(), ignoreVec.end());
  225. }
  226. //----------------------------------------------------------------------------
  227. bool cmFindCommon::CheckCommonArgument(std::string const& arg)
  228. {
  229. if(arg == "NO_DEFAULT_PATH")
  230. {
  231. this->NoDefaultPath = true;
  232. }
  233. else if(arg == "NO_CMAKE_ENVIRONMENT_PATH")
  234. {
  235. this->NoCMakeEnvironmentPath = true;
  236. }
  237. else if(arg == "NO_CMAKE_PATH")
  238. {
  239. this->NoCMakePath = true;
  240. }
  241. else if(arg == "NO_SYSTEM_ENVIRONMENT_PATH")
  242. {
  243. this->NoSystemEnvironmentPath = true;
  244. }
  245. else if(arg == "NO_CMAKE_SYSTEM_PATH")
  246. {
  247. this->NoCMakeSystemPath = true;
  248. }
  249. else if(arg == "NO_CMAKE_FIND_ROOT_PATH")
  250. {
  251. this->FindRootPathMode = RootPathModeNoRootPath;
  252. }
  253. else if(arg == "ONLY_CMAKE_FIND_ROOT_PATH")
  254. {
  255. this->FindRootPathMode = RootPathModeOnlyRootPath;
  256. }
  257. else if(arg == "CMAKE_FIND_ROOT_PATH_BOTH")
  258. {
  259. this->FindRootPathMode = RootPathModeBoth;
  260. }
  261. else
  262. {
  263. // The argument is not one of the above.
  264. return false;
  265. }
  266. // The argument is one of the above.
  267. return true;
  268. }
  269. //----------------------------------------------------------------------------
  270. void cmFindCommon::AddPathSuffix(std::string const& arg)
  271. {
  272. std::string suffix = arg;
  273. // Strip leading and trailing slashes.
  274. if(suffix.empty())
  275. {
  276. return;
  277. }
  278. if(suffix[0] == '/')
  279. {
  280. suffix = suffix.substr(1, suffix.npos);
  281. }
  282. if(suffix.empty())
  283. {
  284. return;
  285. }
  286. if(suffix[suffix.size()-1] == '/')
  287. {
  288. suffix = suffix.substr(0, suffix.size()-1);
  289. }
  290. if(suffix.empty())
  291. {
  292. return;
  293. }
  294. // Store the suffix.
  295. this->SearchPathSuffixes.push_back(suffix);
  296. }
  297. //----------------------------------------------------------------------------
  298. void cmFindCommon::AddUserPath(std::string const& p,
  299. std::vector<std::string>& paths)
  300. {
  301. // We should view the registry as the target application would view
  302. // it.
  303. cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
  304. cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
  305. if(this->Makefile->PlatformIs64Bit())
  306. {
  307. view = cmSystemTools::KeyWOW64_64;
  308. other_view = cmSystemTools::KeyWOW64_32;
  309. }
  310. // Expand using the view of the target application.
  311. std::string expanded = p;
  312. cmSystemTools::ExpandRegistryValues(expanded, view);
  313. cmSystemTools::GlobDirs(expanded.c_str(), paths);
  314. // Executables can be either 32-bit or 64-bit, so expand using the
  315. // alternative view.
  316. if(expanded != p && this->CMakePathName == "PROGRAM")
  317. {
  318. expanded = p;
  319. cmSystemTools::ExpandRegistryValues(expanded, other_view);
  320. cmSystemTools::GlobDirs(expanded.c_str(), paths);
  321. }
  322. }
  323. //----------------------------------------------------------------------------
  324. void cmFindCommon::AddCMakePath(const char* variable)
  325. {
  326. // Get a path from a CMake variable.
  327. if(const char* varPath = this->Makefile->GetDefinition(variable))
  328. {
  329. std::vector<std::string> tmp;
  330. cmSystemTools::ExpandListArgument(varPath, tmp);
  331. // Relative paths are interpreted with respect to the current
  332. // source directory.
  333. this->AddPathsInternal(tmp, CMakePath);
  334. }
  335. }
  336. //----------------------------------------------------------------------------
  337. void cmFindCommon::AddEnvPath(const char* variable)
  338. {
  339. // Get a path from the environment.
  340. std::vector<std::string> tmp;
  341. cmSystemTools::GetPath(tmp, variable);
  342. // Relative paths are interpreted with respect to the current
  343. // working directory.
  344. this->AddPathsInternal(tmp, EnvPath);
  345. }
  346. //----------------------------------------------------------------------------
  347. void cmFindCommon::AddPathsInternal(std::vector<std::string> const& in_paths,
  348. PathType pathType)
  349. {
  350. for(std::vector<std::string>::const_iterator i = in_paths.begin();
  351. i != in_paths.end(); ++i)
  352. {
  353. this->AddPathInternal(*i, pathType);
  354. }
  355. }
  356. //----------------------------------------------------------------------------
  357. void cmFindCommon::AddPathInternal(std::string const& in_path,
  358. PathType pathType)
  359. {
  360. if(in_path.empty())
  361. {
  362. return;
  363. }
  364. // Select the base path with which to interpret relative paths.
  365. const char* relbase = 0;
  366. if(pathType == CMakePath)
  367. {
  368. relbase = this->Makefile->GetCurrentDirectory();
  369. }
  370. // Convert to clean full path.
  371. std::string fullPath =
  372. cmSystemTools::CollapseFullPath(in_path.c_str(), relbase);
  373. // Insert the path if has not already been emitted.
  374. if(this->SearchPathsEmitted.insert(fullPath).second)
  375. {
  376. this->SearchPaths.push_back(fullPath.c_str());
  377. }
  378. }
  379. //----------------------------------------------------------------------------
  380. void cmFindCommon::ComputeFinalPaths()
  381. {
  382. std::vector<std::string>& paths = this->SearchPaths;
  383. // Expand list of paths inside all search roots.
  384. this->RerootPaths(paths);
  385. // Add a trailing slash to all paths to aid the search process.
  386. for(std::vector<std::string>::iterator i = paths.begin();
  387. i != paths.end(); ++i)
  388. {
  389. std::string& p = *i;
  390. if(!p.empty() && p[p.size()-1] != '/')
  391. {
  392. p += "/";
  393. }
  394. }
  395. }
  396. //----------------------------------------------------------------------------
  397. void cmFindCommon::SetMakefile(cmMakefile* makefile)
  398. {
  399. cmCommand::SetMakefile(makefile);
  400. // If we are building for Apple (OSX or also iphone), make sure
  401. // that frameworks and bundles are searched first.
  402. if(this->Makefile->IsOn("APPLE"))
  403. {
  404. this->SearchFrameworkFirst = true;
  405. this->SearchAppBundleFirst = true;
  406. }
  407. }