cmFindBase.cxx 18 KB

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