cmFindBase.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 = false;
  106. const char* versionValue =
  107. this->Makefile->GetDefinition("CMAKE_BACKWARDS_COMPATIBILITY");
  108. int major = 0;
  109. int minor = 0;
  110. if(versionValue && sscanf(versionValue, "%d.%d", &major, &minor) != 2)
  111. {
  112. versionValue = 0;
  113. }
  114. if(versionValue && (major < 2 || major == 2 && minor < 3))
  115. {
  116. compatibility = true;
  117. }
  118. // copy argsIn into args so it can be modified,
  119. // in the process extract the DOC "documentation"
  120. size_t size = argsIn.size();
  121. std::vector<std::string> args;
  122. bool foundDoc = false;
  123. for(unsigned int j = 0; j < size; ++j)
  124. {
  125. if(foundDoc || argsIn[j] != "DOC" )
  126. {
  127. if(argsIn[j] == "ENV")
  128. {
  129. if(j+1 < size)
  130. {
  131. j++;
  132. cmSystemTools::GetPath(args, argsIn[j].c_str());
  133. }
  134. }
  135. else
  136. {
  137. args.push_back(argsIn[j]);
  138. }
  139. }
  140. else
  141. {
  142. if(j+1 < size)
  143. {
  144. foundDoc = true;
  145. this->VariableDocumentation = argsIn[j+1];
  146. j++;
  147. if(j >= size)
  148. {
  149. break;
  150. }
  151. }
  152. }
  153. }
  154. this->VariableName = args[0];
  155. if(this->CheckForVariableInCache())
  156. {
  157. this->AlreadyInCache = true;
  158. return true;
  159. }
  160. this->AlreadyInCache = false;
  161. // Find the current root path mode.
  162. this->SelectDefaultRootPathMode();
  163. // Find the current bundle/framework search policy.
  164. this->SelectDefaultMacMode();
  165. std::vector<std::string> userPaths;
  166. std::string doc;
  167. bool doingNames = true; // assume it starts with a name
  168. bool doingPaths = false;
  169. bool doingPathSuf = false;
  170. bool newStyle = false;
  171. for (unsigned int j = 1; j < args.size(); ++j)
  172. {
  173. if(args[j] == "NAMES")
  174. {
  175. doingNames = true;
  176. newStyle = true;
  177. doingPathSuf = false;
  178. doingPaths = false;
  179. }
  180. else if (args[j] == "PATHS")
  181. {
  182. doingPaths = true;
  183. newStyle = true;
  184. doingNames = false;
  185. doingPathSuf = false;
  186. }
  187. else if (args[j] == "PATH_SUFFIXES")
  188. {
  189. compatibility = false;
  190. doingPathSuf = true;
  191. newStyle = true;
  192. doingNames = false;
  193. doingPaths = false;
  194. }
  195. else if (args[j] == "NO_SYSTEM_PATH")
  196. {
  197. doingPaths = false;
  198. doingPathSuf = false;
  199. doingNames = false;
  200. this->NoDefaultPath = true;
  201. }
  202. else if (this->CheckCommonArgument(args[j]))
  203. {
  204. compatibility = false;
  205. doingPaths = false;
  206. doingPathSuf = false;
  207. doingNames = false;
  208. }
  209. else
  210. {
  211. if(doingNames)
  212. {
  213. this->Names.push_back(args[j]);
  214. }
  215. else if(doingPaths)
  216. {
  217. userPaths.push_back(args[j]);
  218. }
  219. else if(doingPathSuf)
  220. {
  221. this->AddPathSuffix(args[j]);
  222. }
  223. }
  224. }
  225. // Now that arguments have been parsed check the compatibility
  226. // setting. If we need to be compatible with CMake 2.2 and earlier
  227. // do not add the CMake system paths. It is safe to add the CMake
  228. // environment paths and system environment paths because that
  229. // existed in 2.2. It is safe to add the CMake user variable paths
  230. // because the user or project has explicitly set them.
  231. if(compatibility)
  232. {
  233. this->NoCMakeSystemPath = true;
  234. }
  235. if(this->VariableDocumentation.size() == 0)
  236. {
  237. this->VariableDocumentation = "Where can ";
  238. if(this->Names.size() == 0)
  239. {
  240. this->VariableDocumentation += "the (unknown) library be found";
  241. }
  242. else if(this->Names.size() == 1)
  243. {
  244. this->VariableDocumentation += "the "
  245. + this->Names[0] + " library be found";
  246. }
  247. else
  248. {
  249. this->VariableDocumentation += "one of the " + this->Names[0];
  250. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  251. {
  252. this->VariableDocumentation += ", " + this->Names[j];
  253. }
  254. this->VariableDocumentation += " or "
  255. + this->Names[this->Names.size() - 1] + " libraries be found";
  256. }
  257. }
  258. // look for old style
  259. // FIND_*(VAR name path1 path2 ...)
  260. if(!newStyle)
  261. {
  262. this->Names.clear(); // clear out any values in Names
  263. this->Names.push_back(args[1]);
  264. for(unsigned int j = 2; j < args.size(); ++j)
  265. {
  266. userPaths.push_back(args[j]);
  267. }
  268. }
  269. this->ExpandPaths(userPaths);
  270. // Handle search root stuff.
  271. this->RerootPaths(this->SearchPaths);
  272. return true;
  273. }
  274. void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
  275. {
  276. // if NO Default paths was not specified add the
  277. // standard search paths.
  278. if(!this->NoDefaultPath)
  279. {
  280. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  281. {
  282. this->AddFrameWorkPaths();
  283. }
  284. if(this->SearchAppBundleFirst || this->SearchAppBundleOnly)
  285. {
  286. this->AddAppBundlePaths();
  287. }
  288. if(!this->NoCMakeEnvironmentPath &&
  289. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  290. {
  291. // Add CMAKE_*_PATH environment variables
  292. this->AddEnvironmentVariables();
  293. }
  294. if(!this->NoCMakePath &&
  295. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  296. {
  297. // Add CMake varibles of the same name as the previous environment
  298. // varibles CMAKE_*_PATH to be used most of the time with -D
  299. // command line options
  300. this->AddCMakeVariables();
  301. }
  302. if(!this->NoSystemEnvironmentPath &&
  303. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  304. {
  305. // add System environment PATH and (LIB or INCLUDE)
  306. this->AddSystemEnvironmentVariables();
  307. }
  308. if(!this->NoCMakeSystemPath &&
  309. !(this->SearchFrameworkOnly || this->SearchAppBundleOnly))
  310. {
  311. // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
  312. this->AddCMakeSystemVariables();
  313. }
  314. if(this->SearchAppBundleLast)
  315. {
  316. this->AddAppBundlePaths();
  317. }
  318. if(this->SearchFrameworkLast)
  319. {
  320. this->AddFrameWorkPaths();
  321. }
  322. }
  323. std::vector<std::string> paths;
  324. // add the paths specified in the FIND_* call
  325. for(unsigned int i =0; i < userPaths.size(); ++i)
  326. {
  327. paths.push_back(userPaths[i]);
  328. }
  329. this->AddPaths(paths);
  330. }
  331. //----------------------------------------------------------------------------
  332. void cmFindBase::AddEnvironmentVariables()
  333. {
  334. std::vector<std::string> paths;
  335. std::vector<std::string> prefixPaths;
  336. cmSystemTools::GetPath(prefixPaths, "CMAKE_PREFIX_PATH");
  337. this->AddFindPrefix(paths, prefixPaths);
  338. std::string var = "CMAKE_";
  339. var += this->CMakePathName;
  340. var += "_PATH";
  341. cmSystemTools::GetPath(paths, var.c_str());
  342. this->AddPaths(paths);
  343. }
  344. void cmFindBase::AddFindPrefix(std::vector<std::string>& dest,
  345. const std::vector<std::string>& src)
  346. {
  347. // default for programs
  348. std::string subdir = "bin";
  349. if (this->CMakePathName == "INCLUDE")
  350. {
  351. subdir = "include";
  352. }
  353. else if (this->CMakePathName == "LIBRARY")
  354. {
  355. subdir = "lib";
  356. }
  357. else if (this->CMakePathName == "FRAMEWORK")
  358. {
  359. subdir = ""; // ? what to do for frameworks ?
  360. }
  361. for (std::vector<std::string>::const_iterator it = src.begin();
  362. it != src.end();
  363. ++it)
  364. {
  365. std::string dir = it->c_str();
  366. if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
  367. {
  368. dir += "/";
  369. }
  370. dest.push_back(dir + subdir);
  371. if (subdir == "bin")
  372. {
  373. dest.push_back(dir + "sbin");
  374. }
  375. if(!subdir.empty())
  376. {
  377. dest.push_back(*it);
  378. }
  379. }
  380. }
  381. void cmFindBase::AddFrameWorkPaths()
  382. {
  383. std::vector<std::string> paths;
  384. this->GetFrameworkPaths(paths);
  385. this->AddPaths(paths);
  386. }
  387. void cmFindBase::AddPaths(std::vector<std::string> & paths)
  388. {
  389. // add suffixes and clean up paths
  390. this->ExpandRegistryAndCleanPath(paths);
  391. // add the paths to the search paths
  392. this->SearchPaths.insert(this->SearchPaths.end(),
  393. paths.begin(),
  394. paths.end());
  395. }
  396. void cmFindBase::AddAppBundlePaths()
  397. {
  398. std::vector<std::string> paths;
  399. this->GetAppBundlePaths(paths);
  400. this->AddPaths(paths);
  401. }
  402. void cmFindBase::AddCMakeVariables()
  403. {
  404. std::string var = "CMAKE_";
  405. var += this->CMakePathName;
  406. var += "_PATH";
  407. std::vector<std::string> paths;
  408. if(const char* prefixPath =
  409. this->Makefile->GetDefinition("CMAKE_PREFIX_PATH"))
  410. {
  411. std::vector<std::string> prefixPaths;
  412. cmSystemTools::ExpandListArgument(prefixPath, prefixPaths);
  413. this->AddFindPrefix(paths, prefixPaths);
  414. }
  415. if(const char* path = this->Makefile->GetDefinition(var.c_str()))
  416. {
  417. cmSystemTools::ExpandListArgument(path, paths);
  418. }
  419. this->AddPaths(paths);
  420. }
  421. void cmFindBase::AddSystemEnvironmentVariables()
  422. {
  423. // Add LIB or INCLUDE
  424. std::vector<std::string> paths;
  425. if(this->EnvironmentPath.size())
  426. {
  427. cmSystemTools::GetPath(paths, this->EnvironmentPath.c_str());
  428. }
  429. // Add PATH
  430. cmSystemTools::GetPath(paths);
  431. this->AddPaths(paths);
  432. }
  433. void cmFindBase::AddCMakeSystemVariables()
  434. {
  435. std::string var = "CMAKE_SYSTEM_";
  436. var += this->CMakePathName;
  437. var += "_PATH";
  438. std::vector<std::string> paths;
  439. if(const char* prefixPath =
  440. this->Makefile->GetDefinition("CMAKE_SYSTEM_PREFIX_PATH"))
  441. {
  442. std::vector<std::string> prefixPaths;
  443. cmSystemTools::ExpandListArgument(prefixPath, prefixPaths);
  444. this->AddFindPrefix(paths, prefixPaths);
  445. }
  446. if(const char* path = this->Makefile->GetDefinition(var.c_str()))
  447. {
  448. cmSystemTools::ExpandListArgument(path, paths);
  449. }
  450. this->AddPaths(paths);
  451. }
  452. void cmFindBase::ExpandRegistryAndCleanPath(std::vector<std::string>& paths)
  453. {
  454. std::vector<std::string> finalPath;
  455. std::vector<std::string>::iterator i;
  456. // glob and expand registry stuff from paths and put
  457. // into finalPath
  458. for(i = paths.begin();
  459. i != paths.end(); ++i)
  460. {
  461. cmSystemTools::ExpandRegistryValues(*i);
  462. cmSystemTools::GlobDirs(i->c_str(), finalPath);
  463. }
  464. // clear the path
  465. paths.clear();
  466. // convert all paths to unix slashes and add search path suffixes
  467. // if there are any
  468. for(i = finalPath.begin();
  469. i != finalPath.end(); ++i)
  470. {
  471. cmSystemTools::ConvertToUnixSlashes(*i);
  472. // copy each finalPath combined with SearchPathSuffixes
  473. // to the SearchPaths ivar
  474. for(std::vector<std::string>::iterator j =
  475. this->SearchPathSuffixes.begin();
  476. j != this->SearchPathSuffixes.end(); ++j)
  477. {
  478. std::string p = *i + std::string("/") + *j;
  479. // add to all paths because the search path may be modified
  480. // later with lib being replaced for lib64 which may exist
  481. paths.push_back(p);
  482. }
  483. }
  484. // now put the path without the path suffixes in the SearchPaths
  485. for(i = finalPath.begin();
  486. i != finalPath.end(); ++i)
  487. {
  488. // put all search paths in because it may later be replaced
  489. // by lib64 stuff fixes bug 4009
  490. paths.push_back(*i);
  491. }
  492. }
  493. void cmFindBase::PrintFindStuff()
  494. {
  495. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  496. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  497. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  498. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  499. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  500. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  501. std::cerr << "VariableName " << this->VariableName << "\n";
  502. std::cerr << "VariableDocumentation "
  503. << this->VariableDocumentation << "\n";
  504. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  505. std::cerr << "NoCMakeEnvironmentPath "
  506. << this->NoCMakeEnvironmentPath << "\n";
  507. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  508. std::cerr << "NoSystemEnvironmentPath "
  509. << this->NoSystemEnvironmentPath << "\n";
  510. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  511. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  512. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  513. std::cerr << "Names ";
  514. for(unsigned int i =0; i < this->Names.size(); ++i)
  515. {
  516. std::cerr << this->Names[i] << " ";
  517. }
  518. std::cerr << "\n";
  519. std::cerr << "\n";
  520. std::cerr << "SearchPathSuffixes ";
  521. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  522. {
  523. std::cerr << this->SearchPathSuffixes[i] << "\n";
  524. }
  525. std::cerr << "\n";
  526. std::cerr << "SearchPaths\n";
  527. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  528. {
  529. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  530. }
  531. }
  532. bool cmFindBase::CheckForVariableInCache()
  533. {
  534. if(const char* cacheValue =
  535. this->Makefile->GetDefinition(this->VariableName.c_str()))
  536. {
  537. cmCacheManager::CacheIterator it =
  538. this->Makefile->GetCacheManager()->
  539. GetCacheIterator(this->VariableName.c_str());
  540. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  541. bool cached = !it.IsAtEnd();
  542. if(found)
  543. {
  544. // If the user specifies the entry on the command line without a
  545. // type we should add the type and docstring but keep the
  546. // original value. Tell the subclass implementations to do
  547. // this.
  548. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  549. {
  550. this->AlreadyInCacheWithoutMetaInfo = true;
  551. }
  552. return true;
  553. }
  554. else if(cached)
  555. {
  556. const char* hs = it.GetProperty("HELPSTRING");
  557. this->VariableDocumentation = hs?hs:"(none)";
  558. }
  559. }
  560. return false;
  561. }