cmFindCommon.cxx 13 KB

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