cmFindBase.cxx 19 KB

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