cmFindBase.cxx 18 KB

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