cmFindBase.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. // Handle search root stuff.
  260. this->RerootPaths(this->SearchPaths);
  261. // Add a trailing slash to all prefixes to aid the search process.
  262. this->AddTrailingSlashes(this->SearchPaths);
  263. return true;
  264. }
  265. void cmFindBase::ExpandPaths()
  266. {
  267. this->AddCMakeVariablePath();
  268. this->AddCMakeEnvironmentPath();
  269. this->AddUserHintsPath();
  270. this->AddSystemEnvironmentPath();
  271. this->AddCMakeSystemVariablePath();
  272. this->AddUserGuessPath();
  273. // Add suffixes and clean up paths.
  274. this->AddPathSuffixes();
  275. }
  276. //----------------------------------------------------------------------------
  277. void cmFindBase::AddPrefixPaths(std::vector<std::string> const& in_paths,
  278. PathType pathType)
  279. {
  280. // default for programs
  281. std::string subdir = "bin";
  282. if (this->CMakePathName == "INCLUDE")
  283. {
  284. subdir = "include";
  285. }
  286. else if (this->CMakePathName == "LIBRARY")
  287. {
  288. subdir = "lib";
  289. }
  290. else if (this->CMakePathName == "FRAMEWORK")
  291. {
  292. subdir = ""; // ? what to do for frameworks ?
  293. }
  294. for(std::vector<std::string>::const_iterator it = in_paths.begin();
  295. it != in_paths.end(); ++it)
  296. {
  297. std::string dir = it->c_str();
  298. if(!subdir.empty() && !dir.empty() && dir[dir.size()-1] != '/')
  299. {
  300. dir += "/";
  301. }
  302. std::string add = dir + subdir;
  303. if(add != "/")
  304. {
  305. this->AddPathInternal(add, pathType);
  306. }
  307. if (subdir == "bin")
  308. {
  309. this->AddPathInternal(dir+"sbin", pathType);
  310. }
  311. if(!subdir.empty() && *it != "/")
  312. {
  313. this->AddPathInternal(*it, pathType);
  314. }
  315. }
  316. }
  317. //----------------------------------------------------------------------------
  318. void cmFindBase::AddCMakePrefixPath(const char* variable)
  319. {
  320. // Get a path from a CMake variable.
  321. if(const char* varPath = this->Makefile->GetDefinition(variable))
  322. {
  323. std::vector<std::string> tmp;
  324. cmSystemTools::ExpandListArgument(varPath, tmp);
  325. this->AddPrefixPaths(tmp, CMakePath);
  326. }
  327. }
  328. //----------------------------------------------------------------------------
  329. void cmFindBase::AddEnvPrefixPath(const char* variable)
  330. {
  331. // Get a path from the environment.
  332. std::vector<std::string> tmp;
  333. cmSystemTools::GetPath(tmp, variable);
  334. this->AddPrefixPaths(tmp, EnvPath);
  335. }
  336. //----------------------------------------------------------------------------
  337. void cmFindBase::AddCMakeEnvironmentPath()
  338. {
  339. if(!this->NoCMakeEnvironmentPath && !this->NoDefaultPath)
  340. {
  341. // Add CMAKE_*_PATH environment variables
  342. std::string var = "CMAKE_";
  343. var += this->CMakePathName;
  344. var += "_PATH";
  345. this->AddEnvPrefixPath("CMAKE_PREFIX_PATH");
  346. this->AddEnvPath(var.c_str());
  347. if(this->CMakePathName == "PROGRAM")
  348. {
  349. this->AddEnvPath("CMAKE_APPBUNDLE_PATH");
  350. }
  351. else
  352. {
  353. this->AddEnvPath("CMAKE_FRAMEWORK_PATH");
  354. }
  355. }
  356. }
  357. //----------------------------------------------------------------------------
  358. void cmFindBase::AddCMakeVariablePath()
  359. {
  360. if(!this->NoCMakePath && !this->NoDefaultPath)
  361. {
  362. // Add CMake varibles of the same name as the previous environment
  363. // varibles CMAKE_*_PATH to be used most of the time with -D
  364. // command line options
  365. std::string var = "CMAKE_";
  366. var += this->CMakePathName;
  367. var += "_PATH";
  368. this->AddCMakePrefixPath("CMAKE_PREFIX_PATH");
  369. this->AddCMakePath(var.c_str());
  370. if(this->CMakePathName == "PROGRAM")
  371. {
  372. this->AddCMakePath("CMAKE_APPBUNDLE_PATH");
  373. }
  374. else
  375. {
  376. this->AddCMakePath("CMAKE_FRAMEWORK_PATH");
  377. }
  378. }
  379. }
  380. //----------------------------------------------------------------------------
  381. void cmFindBase::AddSystemEnvironmentPath()
  382. {
  383. if(!this->NoSystemEnvironmentPath && !this->NoDefaultPath)
  384. {
  385. // Add LIB or INCLUDE
  386. if(!this->EnvironmentPath.empty())
  387. {
  388. this->AddEnvPath(this->EnvironmentPath.c_str());
  389. }
  390. // Add PATH
  391. this->AddEnvPath(0);
  392. }
  393. }
  394. //----------------------------------------------------------------------------
  395. void cmFindBase::AddCMakeSystemVariablePath()
  396. {
  397. if(!this->NoCMakeSystemPath && !this->NoDefaultPath)
  398. {
  399. std::string var = "CMAKE_SYSTEM_";
  400. var += this->CMakePathName;
  401. var += "_PATH";
  402. this->AddCMakePrefixPath("CMAKE_SYSTEM_PREFIX_PATH");
  403. this->AddCMakePath(var.c_str());
  404. if(this->CMakePathName == "PROGRAM")
  405. {
  406. this->AddCMakePath("CMAKE_SYSTEM_APPBUNDLE_PATH");
  407. }
  408. else
  409. {
  410. this->AddCMakePath("CMAKE_SYSTEM_FRAMEWORK_PATH");
  411. }
  412. }
  413. }
  414. //----------------------------------------------------------------------------
  415. void cmFindBase::AddUserHintsPath()
  416. {
  417. this->AddPathsInternal(this->UserHints, CMakePath);
  418. }
  419. //----------------------------------------------------------------------------
  420. void cmFindBase::AddUserGuessPath()
  421. {
  422. this->AddPathsInternal(this->UserPaths, CMakePath);
  423. }
  424. //----------------------------------------------------------------------------
  425. void cmFindBase::AddPathSuffixes()
  426. {
  427. std::vector<std::string>& paths = this->SearchPaths;
  428. std::vector<std::string> finalPath = paths;
  429. std::vector<std::string>::iterator i;
  430. // clear the path
  431. paths.clear();
  432. // convert all paths to unix slashes and add search path suffixes
  433. // if there are any
  434. for(i = finalPath.begin();
  435. i != finalPath.end(); ++i)
  436. {
  437. cmSystemTools::ConvertToUnixSlashes(*i);
  438. // copy each finalPath combined with SearchPathSuffixes
  439. // to the SearchPaths ivar
  440. for(std::vector<std::string>::iterator j =
  441. this->SearchPathSuffixes.begin();
  442. j != this->SearchPathSuffixes.end(); ++j)
  443. {
  444. // if *i is only / then do not add a //
  445. // this will get incorrectly considered a network
  446. // path on windows and cause huge delays.
  447. std::string p = *i;
  448. if(p.size() && p[p.size()-1] != '/')
  449. {
  450. p += std::string("/");
  451. }
  452. p += *j;
  453. // add to all paths because the search path may be modified
  454. // later with lib being replaced for lib64 which may exist
  455. paths.push_back(p);
  456. }
  457. // now put the path without the path suffixes in the SearchPaths
  458. paths.push_back(*i);
  459. }
  460. }
  461. void cmFindBase::PrintFindStuff()
  462. {
  463. std::cerr << "SearchFrameworkLast: " << this->SearchFrameworkLast << "\n";
  464. std::cerr << "SearchFrameworkOnly: " << this->SearchFrameworkOnly << "\n";
  465. std::cerr << "SearchFrameworkFirst: " << this->SearchFrameworkFirst << "\n";
  466. std::cerr << "SearchAppBundleLast: " << this->SearchAppBundleLast << "\n";
  467. std::cerr << "SearchAppBundleOnly: " << this->SearchAppBundleOnly << "\n";
  468. std::cerr << "SearchAppBundleFirst: " << this->SearchAppBundleFirst << "\n";
  469. std::cerr << "VariableName " << this->VariableName << "\n";
  470. std::cerr << "VariableDocumentation "
  471. << this->VariableDocumentation << "\n";
  472. std::cerr << "NoDefaultPath " << this->NoDefaultPath << "\n";
  473. std::cerr << "NoCMakeEnvironmentPath "
  474. << this->NoCMakeEnvironmentPath << "\n";
  475. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  476. std::cerr << "NoSystemEnvironmentPath "
  477. << this->NoSystemEnvironmentPath << "\n";
  478. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  479. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  480. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  481. std::cerr << "Names ";
  482. for(unsigned int i =0; i < this->Names.size(); ++i)
  483. {
  484. std::cerr << this->Names[i] << " ";
  485. }
  486. std::cerr << "\n";
  487. std::cerr << "\n";
  488. std::cerr << "SearchPathSuffixes ";
  489. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  490. {
  491. std::cerr << this->SearchPathSuffixes[i] << "\n";
  492. }
  493. std::cerr << "\n";
  494. std::cerr << "SearchPaths\n";
  495. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  496. {
  497. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  498. }
  499. }
  500. bool cmFindBase::CheckForVariableInCache()
  501. {
  502. if(const char* cacheValue =
  503. this->Makefile->GetDefinition(this->VariableName.c_str()))
  504. {
  505. cmCacheManager::CacheIterator it =
  506. this->Makefile->GetCacheManager()->
  507. GetCacheIterator(this->VariableName.c_str());
  508. bool found = !cmSystemTools::IsNOTFOUND(cacheValue);
  509. bool cached = !it.IsAtEnd();
  510. if(found)
  511. {
  512. // If the user specifies the entry on the command line without a
  513. // type we should add the type and docstring but keep the
  514. // original value. Tell the subclass implementations to do
  515. // this.
  516. if(cached && it.GetType() == cmCacheManager::UNINITIALIZED)
  517. {
  518. this->AlreadyInCacheWithoutMetaInfo = true;
  519. }
  520. return true;
  521. }
  522. else if(cached)
  523. {
  524. const char* hs = it.GetProperty("HELPSTRING");
  525. this->VariableDocumentation = hs?hs:"(none)";
  526. }
  527. }
  528. return false;
  529. }