cmFindBase.cxx 18 KB

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