cmFindBase.cxx 18 KB

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