cmFindBase.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFindBase.h"
  14. cmFindBase::cmFindBase()
  15. {
  16. cmSystemTools::ReplaceString(this->GenericDocumentationPathsOrder,
  17. "FIND_ARGS_XXX", "<VAR> NAMES name");
  18. this->AlreadyInCache = false;
  19. this->AlreadyInCacheWithoutMetaInfo = false;
  20. this->GenericDocumentation =
  21. " FIND_XXX(<VAR> name1 [path1 path2 ...])\n"
  22. "This is the short-hand signature for the command that "
  23. "is sufficient in many cases. It is the same "
  24. "as FIND_XXX(<VAR> name1 [PATHS path1 path2 ...])\n"
  25. " FIND_XXX(\n"
  26. " <VAR>\n"
  27. " name | NAMES name1 [name2 ...]\n"
  28. " [HINTS path1 [path2 ... ENV var]]\n"
  29. " [PATHS path1 [path2 ... ENV var]]\n"
  30. " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
  31. " [DOC \"cache documentation string\"]\n"
  32. " [NO_DEFAULT_PATH]\n"
  33. " [NO_CMAKE_ENVIRONMENT_PATH]\n"
  34. " [NO_CMAKE_PATH]\n"
  35. " [NO_SYSTEM_ENVIRONMENT_PATH]\n"
  36. " [NO_CMAKE_SYSTEM_PATH]\n"
  37. " [CMAKE_FIND_ROOT_PATH_BOTH |\n"
  38. " ONLY_CMAKE_FIND_ROOT_PATH |\n"
  39. " NO_CMAKE_FIND_ROOT_PATH]\n"
  40. " )\n"
  41. ""
  42. "This command is used to find a SEARCH_XXX_DESC. "
  43. "A cache entry named by <VAR> is created to store the result "
  44. "of this command. "
  45. "If the SEARCH_XXX is found the result is stored in the variable "
  46. "and the search will not be repeated unless the variable is cleared. "
  47. "If nothing is found, the result will be "
  48. "<VAR>-NOTFOUND, and the search will be attempted again the "
  49. "next time FIND_XXX is invoked with the same variable. "
  50. "The name of the SEARCH_XXX that "
  51. "is searched for is specified by the names listed "
  52. "after the NAMES argument. Additional search locations "
  53. "can be specified after the PATHS argument. If ENV var is "
  54. "found in the HINTS or PATHS section the environment variable var "
  55. "will be read and converted from a system environment variable to "
  56. "a cmake style list of paths. For example ENV PATH would be a way "
  57. "to list the system path variable. The argument "
  58. "after DOC will be used for the documentation string in "
  59. "the cache. PATH_SUFFIXES can be used to give sub directories "
  60. "that will be appended to the search paths.\n"
  61. "If NO_DEFAULT_PATH is specified, then no additional paths are "
  62. "added to the search. "
  63. "If NO_DEFAULT_PATH is not specified, the search process is as follows:\n"
  64. "1. Search paths specified in cmake-specific cache variables. "
  65. "These are intended to be used on the command line with a -DVAR=value. "
  66. "This can be skipped if NO_CMAKE_PATH is passed.\n"
  67. " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
  68. " CMAKE_XXX_PATH\n"
  69. " CMAKE_XXX_MAC_PATH\n"
  70. "2. Search paths specified in cmake-specific environment variables. "
  71. "These are intended to be set in the user's shell configuration. "
  72. "This can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
  73. " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_PREFIX_PATH\n"
  74. " CMAKE_XXX_PATH\n"
  75. " CMAKE_XXX_MAC_PATH\n"
  76. "3. Search the paths specified by the HINTS option. "
  77. "These should be paths computed by system introspection, such as a "
  78. "hint provided by the location of another item already found. "
  79. "Hard-coded guesses should be specified with the PATHS option.\n"
  80. "4. Search the standard system environment variables. "
  81. "This can be skipped if NO_SYSTEM_ENVIRONMENT_PATH is an argument.\n"
  82. " PATH\n"
  83. " XXX_SYSTEM\n" // replace with "", LIB, or INCLUDE
  84. "5. Search cmake variables defined in the Platform files "
  85. "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
  86. "is passed.\n"
  87. " <prefix>/XXX_SUBDIR for each <prefix> in CMAKE_SYSTEM_PREFIX_PATH\n"
  88. " CMAKE_SYSTEM_XXX_PATH\n"
  89. " CMAKE_SYSTEM_XXX_MAC_PATH\n"
  90. "6. Search the paths specified by the PATHS option "
  91. "or in the short-hand version of the command. "
  92. "These are typically hard-coded guesses.\n"
  93. ;
  94. this->GenericDocumentation += this->GenericDocumentationMacPolicy;
  95. this->GenericDocumentation += this->GenericDocumentationRootPath;
  96. this->GenericDocumentation += this->GenericDocumentationPathsOrder;
  97. }
  98. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  99. {
  100. if(argsIn.size() < 2 )
  101. {
  102. this->SetError("called with incorrect number of arguments");
  103. return false;
  104. }
  105. // CMake versions below 2.3 did not search all these extra
  106. // locations. Preserve compatibility unless a modern argument is
  107. // passed.
  108. bool compatibility = this->Makefile->NeedBackwardsCompatibility(2,3);
  109. // copy argsIn into args so it can be modified,
  110. // in the process extract the DOC "documentation"
  111. size_t size = argsIn.size();
  112. std::vector<std::string> args;
  113. bool foundDoc = false;
  114. for(unsigned int j = 0; j < size; ++j)
  115. {
  116. if(foundDoc || argsIn[j] != "DOC" )
  117. {
  118. if(argsIn[j] == "ENV")
  119. {
  120. if(j+1 < size)
  121. {
  122. j++;
  123. cmSystemTools::GetPath(args, argsIn[j].c_str());
  124. }
  125. }
  126. else
  127. {
  128. args.push_back(argsIn[j]);
  129. }
  130. }
  131. else
  132. {
  133. if(j+1 < size)
  134. {
  135. foundDoc = true;
  136. this->VariableDocumentation = argsIn[j+1];
  137. j++;
  138. if(j >= size)
  139. {
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. this->VariableName = args[0];
  146. if(this->CheckForVariableInCache())
  147. {
  148. this->AlreadyInCache = true;
  149. return true;
  150. }
  151. this->AlreadyInCache = false;
  152. // Find the current root path mode.
  153. this->SelectDefaultRootPathMode();
  154. // Find the current bundle/framework search policy.
  155. this->SelectDefaultMacMode();
  156. bool newStyle = false;
  157. enum Doing { DoingNone, DoingNames, DoingPaths, DoingPathSuffixes,
  158. DoingHints };
  159. Doing doing = DoingNames; // assume it starts with a name
  160. for (unsigned int j = 1; j < args.size(); ++j)
  161. {
  162. if(args[j] == "NAMES")
  163. {
  164. doing = DoingNames;
  165. newStyle = true;
  166. }
  167. else if (args[j] == "PATHS")
  168. {
  169. doing = DoingPaths;
  170. newStyle = true;
  171. }
  172. else if (args[j] == "HINTS")
  173. {
  174. doing = DoingHints;
  175. newStyle = true;
  176. }
  177. else if (args[j] == "PATH_SUFFIXES")
  178. {
  179. doing = DoingPathSuffixes;
  180. compatibility = false;
  181. newStyle = true;
  182. }
  183. else if (args[j] == "NO_SYSTEM_PATH")
  184. {
  185. doing = DoingNone;
  186. this->NoDefaultPath = true;
  187. }
  188. else if (this->CheckCommonArgument(args[j]))
  189. {
  190. doing = DoingNone;
  191. compatibility = false;
  192. // Some common arguments were accidentally supported by CMake
  193. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  194. // must support it even though it is not documented.
  195. }
  196. else if(doing == DoingNames)
  197. {
  198. this->Names.push_back(args[j]);
  199. }
  200. else if(doing == DoingPaths)
  201. {
  202. this->AddUserPath(args[j], this->UserPaths);
  203. }
  204. else if(doing == DoingHints)
  205. {
  206. this->AddUserPath(args[j], this->UserHints);
  207. }
  208. else if(doing == DoingPathSuffixes)
  209. {
  210. this->AddPathSuffix(args[j]);
  211. }
  212. }
  213. // Now that arguments have been parsed check the compatibility
  214. // setting. If we need to be compatible with CMake 2.2 and earlier
  215. // do not add the CMake system paths. It is safe to add the CMake
  216. // environment paths and system environment paths because that
  217. // existed in 2.2. It is safe to add the CMake user variable paths
  218. // because the user or project has explicitly set them.
  219. if(compatibility)
  220. {
  221. this->NoCMakeSystemPath = true;
  222. }
  223. if(this->VariableDocumentation.size() == 0)
  224. {
  225. this->VariableDocumentation = "Where can ";
  226. if(this->Names.size() == 0)
  227. {
  228. this->VariableDocumentation += "the (unknown) library be found";
  229. }
  230. else if(this->Names.size() == 1)
  231. {
  232. this->VariableDocumentation += "the "
  233. + this->Names[0] + " library be found";
  234. }
  235. else
  236. {
  237. this->VariableDocumentation += "one of the " + this->Names[0];
  238. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  239. {
  240. this->VariableDocumentation += ", " + this->Names[j];
  241. }
  242. this->VariableDocumentation += " or "
  243. + this->Names[this->Names.size() - 1] + " libraries be found";
  244. }
  245. }
  246. // look for old style
  247. // FIND_*(VAR name path1 path2 ...)
  248. if(!newStyle)
  249. {
  250. // All the short-hand arguments have been recorded as names.
  251. std::vector<std::string> shortArgs = this->Names;
  252. this->Names.clear(); // clear out any values in Names
  253. this->Names.push_back(shortArgs[0]);
  254. for(unsigned int j = 1; j < shortArgs.size(); ++j)
  255. {
  256. this->AddUserPath(shortArgs[j], this->UserPaths);
  257. }
  258. }
  259. this->ExpandPaths();
  260. // Handle search root stuff.
  261. this->RerootPaths(this->SearchPaths);
  262. // Add a trailing slash to all prefixes to aid the search process.
  263. this->AddTrailingSlashes(this->SearchPaths);
  264. return true;
  265. }
  266. void cmFindBase::ExpandPaths()
  267. {
  268. this->AddCMakeVariablePath();
  269. this->AddCMakeEnvironmentPath();
  270. this->AddUserHintsPath();
  271. this->AddSystemEnvironmentPath();
  272. this->AddCMakeSystemVariablePath();
  273. this->AddUserGuessPath();
  274. // Add suffixes and clean up paths.
  275. this->AddPathSuffixes();
  276. }
  277. //----------------------------------------------------------------------------
  278. void cmFindBase::AddPrefixPaths(std::vector<std::string> const& in_paths,
  279. PathType pathType)
  280. {
  281. // default for programs
  282. std::string subdir = "bin";
  283. if (this->CMakePathName == "INCLUDE")
  284. {
  285. subdir = "include";
  286. }
  287. else if (this->CMakePathName == "LIBRARY")
  288. {
  289. subdir = "lib";
  290. }
  291. else if (this->CMakePathName == "FRAMEWORK")
  292. {
  293. subdir = ""; // ? what to do for frameworks ?
  294. }
  295. for(std::vector<std::string>::const_iterator it = in_paths.begin();
  296. it != in_paths.end(); ++it)
  297. {
  298. std::string dir = it->c_str();
  299. if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
  300. {
  301. dir += "/";
  302. }
  303. std::string add = dir + subdir;
  304. if(add != "/")
  305. {
  306. this->AddPathInternal(add, pathType);
  307. }
  308. if (subdir == "bin")
  309. {
  310. this->AddPathInternal(dir+"sbin", pathType);
  311. }
  312. if(!subdir.empty() && *it != "/")
  313. {
  314. this->AddPathInternal(*it, pathType);
  315. }
  316. }
  317. }
  318. //----------------------------------------------------------------------------
  319. void cmFindBase::AddCMakePrefixPath(const char* variable)
  320. {
  321. // Get a path from a CMake variable.
  322. if(const char* varPath = this->Makefile->GetDefinition(variable))
  323. {
  324. std::vector<std::string> tmp;
  325. cmSystemTools::ExpandListArgument(varPath, tmp);
  326. this->AddPrefixPaths(tmp, CMakePath);
  327. }
  328. }
  329. //----------------------------------------------------------------------------
  330. void cmFindBase::AddEnvPrefixPath(const char* variable)
  331. {
  332. // Get a path from the environment.
  333. std::vector<std::string> tmp;
  334. cmSystemTools::GetPath(tmp, variable);
  335. this->AddPrefixPaths(tmp, EnvPath);
  336. }
  337. //----------------------------------------------------------------------------
  338. void cmFindBase::AddCMakeEnvironmentPath()
  339. {
  340. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  341. {
  342. // Add CMAKE_*_PATH environment variables
  343. std::string var = "CMAKE_";
  344. var += this->CMakePathName;
  345. var += "_PATH";
  346. this->AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  347. this->AddEnvPath(var.c_str());
  348. if(this->CMakePathName == "PROGRAM")
  349. {
  350. this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
  351. }
  352. else
  353. {
  354. this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
  355. }
  356. }
  357. }
  358. //----------------------------------------------------------------------------
  359. void cmFindBase::AddCMakeVariablePath()
  360. {
  361. if(!this->NoCMakePath && !this->NoDefaultPath)
  362. {
  363. // Add CMake varibles of the same name as the previous environment
  364. // varibles CMAKE_*_PATH to be used most of the time with -D
  365. // command line options
  366. std::string var = "CMAKE_";
  367. var += this->CMakePathName;
  368. var += "_PATH";
  369. this->AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  370. this->AddCMakePath(var.c_str());
  371. if(this->CMakePathName == "PROGRAM")
  372. {
  373. this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
  374. }
  375. else
  376. {
  377. this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
  378. }
  379. }
  380. }
  381. //----------------------------------------------------------------------------
  382. void cmFindBase::AddSystemEnvironmentPath()
  383. {
  384. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  385. {
  386. // Add LIB or INCLUDE
  387. if(!this->EnvironmentPath.empty())
  388. {
  389. this->AddEnvPath(this->EnvironmentPath.c_str());
  390. }
  391. // Add PATH
  392. this->AddEnvPath(0);
  393. }
  394. }
  395. //----------------------------------------------------------------------------
  396. void cmFindBase::AddCMakeSystemVariablePath()
  397. {
  398. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  399. {
  400. std::string var = "CMAKE_SYSTEM_";
  401. var += this->CMakePathName;
  402. var += "_PATH";
  403. this->AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  404. this->AddCMakePath(var.c_str());
  405. if(this->CMakePathName == "PROGRAM")
  406. {
  407. this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  408. }
  409. else
  410. {
  411. this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  412. }
  413. }
  414. }
  415. //----------------------------------------------------------------------------
  416. void cmFindBase::AddUserHintsPath()
  417. {
  418. this->AddPathsInternal(this->UserHints, CMakePath);
  419. }
  420. //----------------------------------------------------------------------------
  421. void cmFindBase::AddUserGuessPath()
  422. {
  423. this->AddPathsInternal(this->UserPaths, CMakePath);
  424. }
  425. //----------------------------------------------------------------------------
  426. void cmFindBase::AddPathSuffixes()
  427. {
  428. std::vector<std::string>& paths = this->SearchPaths;
  429. std::vector<std::string> finalPath = paths;
  430. std::vector<std::string>::iterator i;
  431. // clear the path
  432. paths.clear();
  433. // convert all paths to unix slashes and add search path suffixes
  434. // if there are any
  435. for(i = finalPath.begin();
  436. i != finalPath.end(); ++i)
  437. {
  438. cmSystemTools::ConvertToUnixSlashes(*i);
  439. // copy each finalPath combined with SearchPathSuffixes
  440. // to the SearchPaths ivar
  441. for(std::vector<std::string>::iterator j =
  442. this->SearchPathSuffixes.begin();
  443. j != this->SearchPathSuffixes.end(); ++j)
  444. {
  445. // if *i is only / then do not add a //
  446. // this will get incorrectly considered a network
  447. // path on windows and cause huge delays.
  448. std::string p = *i;
  449. if(p.size() && p[p.size()-1] != '/')
  450. {
  451. p += std::string("/");
  452. }
  453. p += *j;
  454. // add to all paths because the search path may be modified
  455. // later with lib being replaced for lib64 which may exist
  456. paths.push_back(p);
  457. }
  458. }
  459. // now put the path without the path suffixes in the SearchPaths
  460. for(i = finalPath.begin();
  461. i != finalPath.end(); ++i)
  462. {
  463. // put all search paths in because it may later be replaced
  464. // by lib64 stuff fixes bug 4009
  465. paths.push_back(*i);
  466. }
  467. }
  468. void cmFindBase::PrintFindStuff()
  469. {
  470. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  471. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  472. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  473. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  474. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  475. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  476. std::cerr << "VariableName " << this->VariableName << "\n";
  477. std::cerr << "VariableDocumentation "
  478. << this->VariableDocumentation << "\n";
  479. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  480. std::cerr << "NoCMakeEnvironmentPath "
  481. << this->NoCMakeEnvironmentPath << "\n";
  482. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  483. std::cerr << "NoSystemEnvironmentPath "
  484. << this->NoSystemEnvironmentPath << "\n";
  485. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  486. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  487. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  488. std::cerr << "Names ";
  489. for(unsigned int i =0; i < this->Names.size(); ++i)
  490. {
  491. std::cerr << this->Names[i] << " ";
  492. }
  493. std::cerr << "\n";
  494. std::cerr << "\n";
  495. std::cerr << "SearchPathSuffixes ";
  496. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  497. {
  498. std::cerr << this->SearchPathSuffixes[i] << "\n";
  499. }
  500. std::cerr << "\n";
  501. std::cerr << "SearchPaths\n";
  502. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  503. {
  504. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  505. }
  506. }
  507. bool cmFindBase::CheckForVariableInCache()
  508. {
  509. if(const char* cacheValue =
  510. this->Makefile->GetDefinition(this->VariableName.c_str()))
  511. {
  512. cmCacheManager::CacheIterator it =
  513. this->Makefile->GetCacheManager()->
  514. GetCacheIterator(this->VariableName.c_str());
  515. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  516. bool cached = !it.IsAtEnd();
  517. if(found)
  518. {
  519. // If the user specifies the entry on the command line without a
  520. // type we should add the type and docstring but keep the
  521. // original value. Tell the subclass implementations to do
  522. // this.
  523. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  524. {
  525. this->AlreadyInCacheWithoutMetaInfo = true;
  526. }
  527. return true;
  528. }
  529. else if(cached)
  530. {
  531. const char* hs = it.GetProperty("HELPSTRING");
  532. this->VariableDocumentation = hs?hs:"(none)";
  533. }
  534. }
  535. return false;
  536. }