cmFindBase.cxx 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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. if(args.size() < 2 )
  160. {
  161. this->SetError("called with incorrect number of arguments");
  162. return false;
  163. }
  164. this->VariableName = args[0];
  165. if(this->CheckForVariableInCache())
  166. {
  167. this->AlreadyInCache = true;
  168. return true;
  169. }
  170. this->AlreadyInCache = false;
  171. // Find the current root path mode.
  172. this->SelectDefaultRootPathMode();
  173. // Find the current bundle/framework search policy.
  174. this->SelectDefaultMacMode();
  175. bool newStyle = false;
  176. enum Doing { DoingNone, DoingNames, DoingPaths, DoingPathSuffixes,
  177. DoingHints };
  178. Doing doing = DoingNames; // assume it starts with a name
  179. for (unsigned int j = 1; j < args.size(); ++j)
  180. {
  181. if(args[j] == "NAMES")
  182. {
  183. doing = DoingNames;
  184. newStyle = true;
  185. }
  186. else if (args[j] == "PATHS")
  187. {
  188. doing = DoingPaths;
  189. newStyle = true;
  190. }
  191. else if (args[j] == "HINTS")
  192. {
  193. doing = DoingHints;
  194. newStyle = true;
  195. }
  196. else if (args[j] == "PATH_SUFFIXES")
  197. {
  198. doing = DoingPathSuffixes;
  199. compatibility = false;
  200. newStyle = true;
  201. }
  202. else if (args[j] == "NO_SYSTEM_PATH")
  203. {
  204. doing = DoingNone;
  205. this->NoDefaultPath = true;
  206. }
  207. else if (this->CheckCommonArgument(args[j]))
  208. {
  209. doing = DoingNone;
  210. compatibility = false;
  211. // Some common arguments were accidentally supported by CMake
  212. // 2.4 and 2.6.0 in the short-hand form of the command, so we
  213. // must support it even though it is not documented.
  214. }
  215. else if(doing == DoingNames)
  216. {
  217. this->Names.push_back(args[j]);
  218. }
  219. else if(doing == DoingPaths)
  220. {
  221. this->AddUserPath(args[j], this->UserPaths);
  222. }
  223. else if(doing == DoingHints)
  224. {
  225. this->AddUserPath(args[j], this->UserHints);
  226. }
  227. else if(doing == DoingPathSuffixes)
  228. {
  229. this->AddPathSuffix(args[j]);
  230. }
  231. }
  232. // Now that arguments have been parsed check the compatibility
  233. // setting. If we need to be compatible with CMake 2.2 and earlier
  234. // do not add the CMake system paths. It is safe to add the CMake
  235. // environment paths and system environment paths because that
  236. // existed in 2.2. It is safe to add the CMake user variable paths
  237. // because the user or project has explicitly set them.
  238. if(compatibility)
  239. {
  240. this->NoCMakeSystemPath = true;
  241. }
  242. if(this->VariableDocumentation.size() == 0)
  243. {
  244. this->VariableDocumentation = "Where can ";
  245. if(this->Names.size() == 0)
  246. {
  247. this->VariableDocumentation += "the (unknown) library be found";
  248. }
  249. else if(this->Names.size() == 1)
  250. {
  251. this->VariableDocumentation += "the "
  252. + this->Names[0] + " library be found";
  253. }
  254. else
  255. {
  256. this->VariableDocumentation += "one of the " + this->Names[0];
  257. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  258. {
  259. this->VariableDocumentation += ", " + this->Names[j];
  260. }
  261. this->VariableDocumentation += " or "
  262. + this->Names[this->Names.size() - 1] + " libraries be found";
  263. }
  264. }
  265. // look for old style
  266. // FIND_*(VAR name path1 path2 ...)
  267. if(!newStyle)
  268. {
  269. // All the short-hand arguments have been recorded as names.
  270. std::vector<std::string> shortArgs = this->Names;
  271. this->Names.clear(); // clear out any values in Names
  272. this->Names.push_back(shortArgs[0]);
  273. for(unsigned int j = 1; j < shortArgs.size(); ++j)
  274. {
  275. this->AddUserPath(shortArgs[j], this->UserPaths);
  276. }
  277. }
  278. this->ExpandPaths();
  279. // Filter out ignored paths from the prefix list
  280. std::set<std::string> ignored;
  281. this->GetIgnoredPaths(ignored);
  282. this->FilterPaths(this->SearchPaths, ignored);
  283. // Handle search root stuff.
  284. this->RerootPaths(this->SearchPaths);
  285. // Add a trailing slash to all prefixes to aid the search process.
  286. this->AddTrailingSlashes(this->SearchPaths);
  287. return true;
  288. }
  289. void cmFindBase::ExpandPaths()
  290. {
  291. this->AddCMakeVariablePath();
  292. this->AddCMakeEnvironmentPath();
  293. this->AddUserHintsPath();
  294. this->AddSystemEnvironmentPath();
  295. this->AddCMakeSystemVariablePath();
  296. this->AddUserGuessPath();
  297. // Add suffixes and clean up paths.
  298. this->AddPathSuffixes();
  299. }
  300. //----------------------------------------------------------------------------
  301. void cmFindBase::AddPrefixPaths(std::vector<std::string> const& in_paths,
  302. PathType pathType)
  303. {
  304. // default for programs
  305. std::string subdir = "bin";
  306. if (this->CMakePathName == "INCLUDE")
  307. {
  308. subdir = "include";
  309. }
  310. else if (this->CMakePathName == "LIBRARY")
  311. {
  312. subdir = "lib";
  313. }
  314. else if (this->CMakePathName == "FRAMEWORK")
  315. {
  316. subdir = ""; // ? what to do for frameworks ?
  317. }
  318. for(std::vector<std::string>::const_iterator it = in_paths.begin();
  319. it != in_paths.end(); ++it)
  320. {
  321. std::string dir = it->c_str();
  322. if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
  323. {
  324. dir += "/";
  325. }
  326. std::string add = dir + subdir;
  327. if(add != "/")
  328. {
  329. this->AddPathInternal(add, pathType);
  330. }
  331. if (subdir == "bin")
  332. {
  333. this->AddPathInternal(dir+"sbin", pathType);
  334. }
  335. if(!subdir.empty() && *it != "/")
  336. {
  337. this->AddPathInternal(*it, pathType);
  338. }
  339. }
  340. }
  341. //----------------------------------------------------------------------------
  342. void cmFindBase::AddCMakePrefixPath(const char* variable)
  343. {
  344. // Get a path from a CMake variable.
  345. if(const char* varPath = this->Makefile->GetDefinition(variable))
  346. {
  347. std::vector<std::string> tmp;
  348. cmSystemTools::ExpandListArgument(varPath, tmp);
  349. this->AddPrefixPaths(tmp, CMakePath);
  350. }
  351. }
  352. //----------------------------------------------------------------------------
  353. void cmFindBase::AddEnvPrefixPath(const char* variable)
  354. {
  355. // Get a path from the environment.
  356. std::vector<std::string> tmp;
  357. cmSystemTools::GetPath(tmp, variable);
  358. this->AddPrefixPaths(tmp, EnvPath);
  359. }
  360. //----------------------------------------------------------------------------
  361. void cmFindBase::AddCMakeEnvironmentPath()
  362. {
  363. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  364. {
  365. // Add CMAKE_*_PATH environment variables
  366. std::string var = "CMAKE_";
  367. var += this->CMakePathName;
  368. var += "_PATH";
  369. this->AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  370. this->AddEnvPath(var.c_str());
  371. if(this->CMakePathName == "PROGRAM")
  372. {
  373. this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
  374. }
  375. else
  376. {
  377. this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
  378. }
  379. }
  380. }
  381. //----------------------------------------------------------------------------
  382. void cmFindBase::AddCMakeVariablePath()
  383. {
  384. if(!this->NoCMakePath && !this->NoDefaultPath)
  385. {
  386. // Add CMake varibles of the same name as the previous environment
  387. // varibles CMAKE_*_PATH to be used most of the time with -D
  388. // command line options
  389. std::string var = "CMAKE_";
  390. var += this->CMakePathName;
  391. var += "_PATH";
  392. this->AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  393. this->AddCMakePath(var.c_str());
  394. if(this->CMakePathName == "PROGRAM")
  395. {
  396. this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
  397. }
  398. else
  399. {
  400. this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
  401. }
  402. }
  403. }
  404. //----------------------------------------------------------------------------
  405. void cmFindBase::AddSystemEnvironmentPath()
  406. {
  407. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  408. {
  409. // Add LIB or INCLUDE
  410. if(!this->EnvironmentPath.empty())
  411. {
  412. this->AddEnvPath(this->EnvironmentPath.c_str());
  413. }
  414. // Add PATH
  415. this->AddEnvPath(0);
  416. }
  417. }
  418. //----------------------------------------------------------------------------
  419. void cmFindBase::AddCMakeSystemVariablePath()
  420. {
  421. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  422. {
  423. std::string var = "CMAKE_SYSTEM_";
  424. var += this->CMakePathName;
  425. var += "_PATH";
  426. this->AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  427. this->AddCMakePath(var.c_str());
  428. if(this->CMakePathName == "PROGRAM")
  429. {
  430. this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  431. }
  432. else
  433. {
  434. this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  435. }
  436. }
  437. }
  438. //----------------------------------------------------------------------------
  439. void cmFindBase::AddUserHintsPath()
  440. {
  441. this->AddPathsInternal(this->UserHints, CMakePath);
  442. }
  443. //----------------------------------------------------------------------------
  444. void cmFindBase::AddUserGuessPath()
  445. {
  446. this->AddPathsInternal(this->UserPaths, CMakePath);
  447. }
  448. //----------------------------------------------------------------------------
  449. void cmFindBase::AddPathSuffixes()
  450. {
  451. std::vector<std::string>& paths = this->SearchPaths;
  452. std::vector<std::string> finalPath = paths;
  453. std::vector<std::string>::iterator i;
  454. // clear the path
  455. paths.clear();
  456. // convert all paths to unix slashes and add search path suffixes
  457. // if there are any
  458. for(i = finalPath.begin();
  459. i != finalPath.end(); ++i)
  460. {
  461. cmSystemTools::ConvertToUnixSlashes(*i);
  462. // copy each finalPath combined with SearchPathSuffixes
  463. // to the SearchPaths ivar
  464. for(std::vector<std::string>::iterator j =
  465. this->SearchPathSuffixes.begin();
  466. j != this->SearchPathSuffixes.end(); ++j)
  467. {
  468. // if *i is only / then do not add a //
  469. // this will get incorrectly considered a network
  470. // path on windows and cause huge delays.
  471. std::string p = *i;
  472. if(p.size() && p[p.size()-1] != '/')
  473. {
  474. p += std::string("/");
  475. }
  476. p += *j;
  477. // add to all paths because the search path may be modified
  478. // later with lib being replaced for lib64 which may exist
  479. paths.push_back(p);
  480. }
  481. // now put the path without the path suffixes in the SearchPaths
  482. paths.push_back(*i);
  483. }
  484. }
  485. void cmFindBase::PrintFindStuff()
  486. {
  487. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  488. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  489. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  490. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  491. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  492. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  493. std::cerr << "VariableName " << this->VariableName << "\n";
  494. std::cerr << "VariableDocumentation "
  495. << this->VariableDocumentation << "\n";
  496. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  497. std::cerr << "NoCMakeEnvironmentPath "
  498. << this->NoCMakeEnvironmentPath << "\n";
  499. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  500. std::cerr << "NoSystemEnvironmentPath "
  501. << this->NoSystemEnvironmentPath << "\n";
  502. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  503. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  504. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  505. std::cerr << "Names ";
  506. for(unsigned int i =0; i < this->Names.size(); ++i)
  507. {
  508. std::cerr << this->Names[i] << " ";
  509. }
  510. std::cerr << "\n";
  511. std::cerr << "\n";
  512. std::cerr << "SearchPathSuffixes ";
  513. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  514. {
  515. std::cerr << this->SearchPathSuffixes[i] << "\n";
  516. }
  517. std::cerr << "\n";
  518. std::cerr << "SearchPaths\n";
  519. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  520. {
  521. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  522. }
  523. }
  524. bool cmFindBase::CheckForVariableInCache()
  525. {
  526. if(const char* cacheValue =
  527. this->Makefile->GetDefinition(this->VariableName.c_str()))
  528. {
  529. cmCacheManager::CacheIterator it =
  530. this->Makefile->GetCacheManager()->
  531. GetCacheIterator(this->VariableName.c_str());
  532. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  533. bool cached = !it.IsAtEnd();
  534. if(found)
  535. {
  536. // If the user specifies the entry on the command line without a
  537. // type we should add the type and docstring but keep the
  538. // original value. Tell the subclass implementations to do
  539. // this.
  540. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  541. {
  542. this->AlreadyInCacheWithoutMetaInfo = true;
  543. }
  544. return true;
  545. }
  546. else if(cached)
  547. {
  548. const char* hs = it.GetProperty("HELPSTRING");
  549. this->VariableDocumentation = hs?hs:"(none)";
  550. }
  551. }
  552. return false;
  553. }