cmFindBase.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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::vector<std::string> userPaths;
  154. std::string doc;
  155. bool doingNames = true; // assume it starts with a name
  156. bool doingPaths = false;
  157. bool doingPathSuf = false;
  158. bool newStyle = false;
  159. for (unsigned int j = 1; j < args.size(); ++j)
  160. {
  161. if(args[j] == "NAMES")
  162. {
  163. doingNames = true;
  164. newStyle = true;
  165. doingPathSuf = false;
  166. doingPaths = false;
  167. }
  168. else if (args[j] == "PATHS")
  169. {
  170. doingPaths = true;
  171. newStyle = true;
  172. doingNames = false;
  173. doingPathSuf = false;
  174. }
  175. else if (args[j] == "PATH_SUFFIXES")
  176. {
  177. compatibility = false;
  178. doingPathSuf = true;
  179. newStyle = true;
  180. doingNames = false;
  181. doingPaths = false;
  182. }
  183. else if (args[j] == "NO_SYSTEM_PATH")
  184. {
  185. doingPaths = false;
  186. doingPathSuf = false;
  187. doingNames = false;
  188. this->NoDefaultPath = true;
  189. }
  190. else if (this->CheckCommonArgument(args[j]))
  191. {
  192. compatibility = false;
  193. doingPaths = false;
  194. doingPathSuf = false;
  195. doingNames = false;
  196. }
  197. else
  198. {
  199. if(doingNames)
  200. {
  201. this->Names.push_back(args[j]);
  202. }
  203. else if(doingPaths)
  204. {
  205. userPaths.push_back(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. userPaths.push_back(args[j]);
  255. }
  256. }
  257. this->ExpandPaths(userPaths);
  258. // Handle search root stuff.
  259. this->RerootPaths(this->SearchPaths);
  260. return true;
  261. }
  262. void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
  263. {
  264. // if NO Default paths was not specified add the
  265. // standard search paths.
  266. if(!this->NoDefaultPath)
  267. {
  268. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  269. {
  270. this->AddFrameWorkPaths();
  271. }
  272. if(this->SearchAppBundleFirst || this->SearchAppBundleOnly)
  273. {
  274. this->AddAppBundlePaths();
  275. }
  276. if(!this->NoCMakeEnvironmentPath &&
  277. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  278. {
  279. // Add CMAKE_*_PATH environment variables
  280. this->AddEnvironmentVariables();
  281. }
  282. if(!this->NoCMakePath &&
  283. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  284. {
  285. // Add CMake varibles of the same name as the previous environment
  286. // varibles CMAKE_*_PATH to be used most of the time with -D
  287. // command line options
  288. this->AddCMakeVariables();
  289. }
  290. if(!this->NoSystemEnvironmentPath &&
  291. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  292. {
  293. // add System environment PATH and (LIB or INCLUDE)
  294. this->AddSystemEnvironmentVariables();
  295. }
  296. if(!this->NoCMakeSystemPath &&
  297. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  298. {
  299. // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
  300. this->AddCMakeSystemVariables();
  301. }
  302. if(this->SearchAppBundleLast)
  303. {
  304. this->AddAppBundlePaths();
  305. }
  306. if(this->SearchFrameworkLast)
  307. {
  308. this->AddFrameWorkPaths();
  309. }
  310. }
  311. std::vector<std::string> paths;
  312. // add the paths specified in the FIND_* call
  313. for(unsigned int i =0; i < userPaths.size(); ++i)
  314. {
  315. paths.push_back(userPaths[i]);
  316. }
  317. this->AddPaths(paths);
  318. }
  319. //----------------------------------------------------------------------------
  320. void cmFindBase::AddEnvironmentVariables()
  321. {
  322. std::vector<std::string> paths;
  323. std::vector<std::string> prefixPaths;
  324. cmSystemTools::GetPath(prefixPaths, "CMAKE_PREFIX_PATH");
  325. this->AddFindPrefix(paths, prefixPaths);
  326. std::string var = "CMAKE_";
  327. var += this->CMakePathName;
  328. var += "_PATH";
  329. cmSystemTools::GetPath(paths, var.c_str());
  330. this->AddPaths(paths);
  331. }
  332. void cmFindBase::AddFindPrefix(std::vector<std::string>& dest,
  333. const std::vector<std::string>& src)
  334. {
  335. // default for programs
  336. std::string subdir = "bin";
  337. if (this->CMakePathName == "INCLUDE")
  338. {
  339. subdir = "include";
  340. }
  341. else if (this->CMakePathName == "LIBRARY")
  342. {
  343. subdir = "lib";
  344. }
  345. else if (this->CMakePathName == "FRAMEWORK")
  346. {
  347. subdir = ""; // ? what to do for frameworks ?
  348. }
  349. for (std::vector<std::string>::const_iterator it = src.begin();
  350. it != src.end();
  351. ++it)
  352. {
  353. std::string dir = it->c_str();
  354. if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
  355. {
  356. dir += "/";
  357. }
  358. dest.push_back(dir + subdir);
  359. if (subdir == "bin")
  360. {
  361. dest.push_back(dir + "sbin");
  362. }
  363. if(!subdir.empty())
  364. {
  365. dest.push_back(*it);
  366. }
  367. }
  368. }
  369. void cmFindBase::AddFrameWorkPaths()
  370. {
  371. std::vector<std::string> paths;
  372. this->GetFrameworkPaths(paths);
  373. this->AddPaths(paths);
  374. }
  375. void cmFindBase::AddPaths(std::vector<std::string> & paths)
  376. {
  377. // add suffixes and clean up paths
  378. this->ExpandRegistryAndCleanPath(paths);
  379. // add the paths to the search paths
  380. this->SearchPaths.insert(this->SearchPaths.end(),
  381. paths.begin(),
  382. paths.end());
  383. }
  384. void cmFindBase::AddAppBundlePaths()
  385. {
  386. std::vector<std::string> paths;
  387. this->GetAppBundlePaths(paths);
  388. this->AddPaths(paths);
  389. }
  390. void cmFindBase::AddCMakeVariables()
  391. {
  392. std::string var = "CMAKE_";
  393. var += this->CMakePathName;
  394. var += "_PATH";
  395. std::vector<std::string> paths;
  396. if(const char* prefixPath =
  397. this->Makefile->GetDefinition("CMAKE_PREFIX_PATH"))
  398. {
  399. std::vector<std::string> prefixPaths;
  400. cmSystemTools::ExpandListArgument(prefixPath, prefixPaths);
  401. this->AddFindPrefix(paths, prefixPaths);
  402. }
  403. if(const char* path = this->Makefile->GetDefinition(var.c_str()))
  404. {
  405. cmSystemTools::ExpandListArgument(path, paths);
  406. }
  407. this->AddPaths(paths);
  408. }
  409. void cmFindBase::AddSystemEnvironmentVariables()
  410. {
  411. // Add LIB or INCLUDE
  412. std::vector<std::string> paths;
  413. if(this->EnvironmentPath.size())
  414. {
  415. cmSystemTools::GetPath(paths, this->EnvironmentPath.c_str());
  416. }
  417. // Add PATH
  418. cmSystemTools::GetPath(paths);
  419. this->AddPaths(paths);
  420. }
  421. void cmFindBase::AddCMakeSystemVariables()
  422. {
  423. std::string var = "CMAKE_SYSTEM_";
  424. var += this->CMakePathName;
  425. var += "_PATH";
  426. std::vector<std::string> paths;
  427. if(const char* prefixPath =
  428. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH"))
  429. {
  430. std::vector<std::string> prefixPaths;
  431. cmSystemTools::ExpandListArgument(prefixPath, prefixPaths);
  432. this->AddFindPrefix(paths, prefixPaths);
  433. }
  434. if(const char* path = this->Makefile->GetDefinition(var.c_str()))
  435. {
  436. cmSystemTools::ExpandListArgument(path, paths);
  437. }
  438. this->AddPaths(paths);
  439. }
  440. void cmFindBase::ExpandRegistryAndCleanPath(std::vector<std::string>& paths)
  441. {
  442. std::vector<std::string> finalPath;
  443. std::vector<std::string>::iterator i;
  444. // glob and expand registry stuff from paths and put
  445. // into finalPath
  446. for(i = paths.begin();
  447. i != paths.end(); ++i)
  448. {
  449. cmSystemTools::ExpandRegistryValues(*i);
  450. cmSystemTools::GlobDirs(i->c_str(), finalPath);
  451. }
  452. // clear the path
  453. paths.clear();
  454. // convert all paths to unix slashes and add search path suffixes
  455. // if there are any
  456. for(i = finalPath.begin();
  457. i != finalPath.end(); ++i)
  458. {
  459. cmSystemTools::ConvertToUnixSlashes(*i);
  460. // copy each finalPath combined with SearchPathSuffixes
  461. // to the SearchPaths ivar
  462. for(std::vector<std::string>::iterator j =
  463. this->SearchPathSuffixes.begin();
  464. j != this->SearchPathSuffixes.end(); ++j)
  465. {
  466. std::string p = *i + std::string("/") + *j;
  467. // add to all paths because the search path may be modified
  468. // later with lib being replaced for lib64 which may exist
  469. paths.push_back(p);
  470. }
  471. }
  472. // now put the path without the path suffixes in the SearchPaths
  473. for(i = finalPath.begin();
  474. i != finalPath.end(); ++i)
  475. {
  476. // put all search paths in because it may later be replaced
  477. // by lib64 stuff fixes bug 4009
  478. paths.push_back(*i);
  479. }
  480. }
  481. void cmFindBase::PrintFindStuff()
  482. {
  483. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  484. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  485. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  486. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  487. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  488. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  489. std::cerr << "VariableName " << this->VariableName << "\n";
  490. std::cerr << "VariableDocumentation "
  491. << this->VariableDocumentation << "\n";
  492. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  493. std::cerr << "NoCMakeEnvironmentPath "
  494. << this->NoCMakeEnvironmentPath << "\n";
  495. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  496. std::cerr << "NoSystemEnvironmentPath "
  497. << this->NoSystemEnvironmentPath << "\n";
  498. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  499. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  500. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  501. std::cerr << "Names ";
  502. for(unsigned int i =0; i < this->Names.size(); ++i)
  503. {
  504. std::cerr << this->Names[i] << " ";
  505. }
  506. std::cerr << "\n";
  507. std::cerr << "\n";
  508. std::cerr << "SearchPathSuffixes ";
  509. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  510. {
  511. std::cerr << this->SearchPathSuffixes[i] << "\n";
  512. }
  513. std::cerr << "\n";
  514. std::cerr << "SearchPaths\n";
  515. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  516. {
  517. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  518. }
  519. }
  520. bool cmFindBase::CheckForVariableInCache()
  521. {
  522. if(const char* cacheValue =
  523. this->Makefile->GetDefinition(this->VariableName.c_str()))
  524. {
  525. cmCacheManager::CacheIterator it =
  526. this->Makefile->GetCacheManager()->
  527. GetCacheIterator(this->VariableName.c_str());
  528. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  529. bool cached = !it.IsAtEnd();
  530. if(found)
  531. {
  532. // If the user specifies the entry on the command line without a
  533. // type we should add the type and docstring but keep the
  534. // original value. Tell the subclass implementations to do
  535. // this.
  536. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  537. {
  538. this->AlreadyInCacheWithoutMetaInfo = true;
  539. }
  540. return true;
  541. }
  542. else if(cached)
  543. {
  544. const char* hs = it.GetProperty("HELPSTRING");
  545. this->VariableDocumentation = hs?hs:"(none)";
  546. }
  547. }
  548. return false;
  549. }