cmFindBase.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. this->AlreadyInCache = false;
  17. this->NoSystemPath = false;
  18. this->NoCMakePath = false;
  19. this->NoCMakeEnvironmentPath = false;
  20. this->NoCMakeSystemPath = false;
  21. // default is to search frameworks first on apple
  22. #if defined(__APPLE__)
  23. this->SearchFrameworkFirst = true;
  24. #else
  25. this->SearchFrameworkFirst = false;
  26. #endif
  27. this->SearchFrameworkOnly = false;
  28. this->SearchFrameworkLast = false;
  29. this->GenericDocumentation =
  30. " FIND_XXX(<VAR> name1 path1 path2 ...)\n"
  31. "This is the short-hand signature for the command that "
  32. "is sufficient in many cases. It is the same "
  33. "as FIND_XXX(<VAR> NAMES name1 PATHS path2 path2 ...)\n"
  34. " FIND_XXX(\n"
  35. " <VAR> \n"
  36. " NAMES name1 [name2 ...]\n"
  37. " PATHS path1 [path2 ...]\n"
  38. " [PATH_SUFFIXES suffix1 [suffix2 ...]]\n"
  39. " [DOC \"cache documentation string\"]\n"
  40. " [NO_CMAKE_ENVIRONMENT_PATH]\n"
  41. " [NO_CMAKE_PATH]\n"
  42. " [NO_SYSTEM_PATH]\n"
  43. " [NO_CMAKE_SYSTEM_PATH]\n"
  44. " )\n"
  45. ""
  46. "This command is used to find a SEARCH_XXX_DESC. "
  47. "A cache entry named by <VAR> is created to store the result "
  48. "of this command. If nothing is found, the result will be "
  49. "<VAR>-NOTFOUND. 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. The argument "
  53. "after DOC will be used for the documentation string in "
  54. "the cache. PATH_SUFFIXES can be used to give sub directories "
  55. "that will be appended to the search paths.\n"
  56. "The search process is as follows:\n"
  57. "1. Search cmake specific environment variables. This "
  58. "can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.\n"
  59. ""
  60. " CMAKE_FRAMEWORK_PATH\n"
  61. " CMAKE_XXX_PATH\n"
  62. "2. Search cmake variables with the same names as "
  63. "the cmake specific environment variables. These "
  64. "are intended to be used on the command line with a "
  65. "-DVAR=value. This can be skipped if NO_CMAKE_PATH "
  66. "is passed.\n"
  67. ""
  68. " CMAKE_FRAMEWORK_PATH\n"
  69. " CMAKE_XXX_PATH\n"
  70. "3. Search the standard system environment variables. "
  71. "This can be skipped if NO_SYSTEM_PATH is an argument.\n"
  72. " PATH\n"
  73. " XXX_SYSTEM\n" // replace with "", LIB, or INCLUDE
  74. "4. Search cmake variables defined in the Platform files "
  75. "for the current system. This can be skipped if NO_CMAKE_SYSTEM_PATH "
  76. "is passed.\n"
  77. " CMAKE_SYSTEM_FRAMEWORK_PATH\n"
  78. " CMAKE_SYSTEM_XXX_PATH\n"
  79. "5. Search the paths specified after PATHS or in the short-hand version "
  80. "of the command.\n"
  81. "On Darwin or systems supporting OSX Frameworks, the cmake variable"
  82. " CMAKE_FIND_FRAMEWORK can be set to empty or one of the following:\n"
  83. " \"FIRST\" - Try to find frameworks before standard\n"
  84. " libraries or headers. This is the default on Darwin.\n"
  85. " \"LAST\" - Try to find frameworks after standard\n"
  86. " libraries or headers.\n"
  87. " \"ONLY\" - Only try to find frameworks.\n"
  88. " \"NEVER\". - Never try to find frameworks.\n";
  89. }
  90. bool cmFindBase::ParseArguments(std::vector<std::string> const& argsIn)
  91. {
  92. if(argsIn.size() < 2 )
  93. {
  94. this->SetError("called with incorrect number of arguments");
  95. return false;
  96. }
  97. // copy argsIn into args so it can be modified,
  98. // in the process extract the DOC "documentation"
  99. size_t size = argsIn.size();
  100. std::vector<std::string> args;
  101. bool foundDoc = false;
  102. for(unsigned int j = 0; j < size; ++j)
  103. {
  104. if(foundDoc || argsIn[j] != "DOC" )
  105. {
  106. args.push_back(argsIn[j]);
  107. }
  108. else
  109. {
  110. if(j+1 < size)
  111. {
  112. foundDoc = true;
  113. this->VariableDocumentation = argsIn[j+1];
  114. j++;
  115. if(j >= size)
  116. {
  117. break;
  118. }
  119. }
  120. }
  121. }
  122. this->VariableName = args[0];
  123. if(this->CheckForVariableInCache())
  124. {
  125. this->AlreadyInCache = true;
  126. return true;
  127. }
  128. this->AlreadyInCache = false;
  129. std::vector<std::string> userPaths;
  130. std::string doc;
  131. bool doingNames = false;
  132. bool doingPaths = false;
  133. bool doingPathSuf = false;
  134. bool newStyle = false;
  135. for (unsigned int j = 1; j < args.size(); ++j)
  136. {
  137. if(args[j] == "NAMES")
  138. {
  139. doingNames = true;
  140. newStyle = true;
  141. doingPathSuf = false;
  142. doingPaths = false;
  143. }
  144. else if (args[j] == "PATHS")
  145. {
  146. doingPaths = true;
  147. newStyle = true;
  148. doingNames = false;
  149. doingPathSuf = false;
  150. }
  151. else if (args[j] == "PATH_SUFFIXES")
  152. {
  153. doingPathSuf = true;
  154. newStyle = true;
  155. doingNames = false;
  156. doingPaths = false;
  157. }
  158. else if (args[j] == "NO_SYSTEM_PATH")
  159. {
  160. this->NoSystemPath = true;
  161. }
  162. else if (args[j] == "NO_CMAKE_PATH")
  163. {
  164. this->NoCMakePath = true;
  165. }
  166. else if (args[j] == "NO_CMAKE_ENVIRONMENT_PATH")
  167. {
  168. this->NoCMakeEnvironmentPath = true;
  169. }
  170. else if (args[j] == "NO_CMAKE_SYSTEM_PATH")
  171. {
  172. this->NoCMakeSystemPath = true;
  173. }
  174. else
  175. {
  176. if(doingNames)
  177. {
  178. this->Names.push_back(args[j]);
  179. }
  180. else if(doingPaths)
  181. {
  182. userPaths.push_back(args[j]);
  183. }
  184. else if(doingPathSuf)
  185. {
  186. this->SearchPathSuffixes.push_back(args[j]);
  187. }
  188. }
  189. }
  190. if(this->VariableDocumentation.size() == 0)
  191. {
  192. this->VariableDocumentation = "Whare can ";
  193. if(this->Names.size() == 0)
  194. {
  195. this->VariableDocumentation += "the (unknown) library be found";
  196. }
  197. else if(this->Names.size() == 1)
  198. {
  199. this->VariableDocumentation += "the " + this->Names[0] + " library be found";
  200. }
  201. else
  202. {
  203. this->VariableDocumentation += "one of the " + this->Names[0];
  204. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  205. {
  206. this->VariableDocumentation += ", " + this->Names[j];
  207. }
  208. this->VariableDocumentation += " or " + this->Names[this->Names.size() - 1] + " libraries be found";
  209. }
  210. }
  211. // look for old style
  212. // FIND_*(VAR name path1 path2 ...)
  213. if(!newStyle)
  214. {
  215. this->Names.push_back(args[1]);
  216. for(unsigned int j = 2; j < args.size(); ++j)
  217. {
  218. userPaths.push_back(args[j]);
  219. }
  220. }
  221. this->ExpandPaths(userPaths);
  222. return true;
  223. }
  224. void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
  225. {
  226. // Add CMAKE_*_PATH environment variables
  227. if(!this->NoCMakeEnvironmentPath)
  228. {
  229. this->AddEnvironmentVairables();
  230. }
  231. // Add CMake varibles of the same name as the previous environment
  232. // varibles CMAKE_*_PATH to be used most of the time with -D
  233. // command line options
  234. if(!this->NoCMakePath)
  235. {
  236. this->AddCMakeVairables();
  237. }
  238. // add System environment PATH and (LIB or INCLUDE)
  239. if(!this->NoSystemPath)
  240. {
  241. this->AddSystemEnvironmentVairables();
  242. }
  243. // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
  244. if(!this->NoCMakeSystemPath)
  245. {
  246. this->AddCMakeSystemVariables();
  247. }
  248. // add the paths specified in the FIND_* call
  249. for(unsigned int i =0; i < userPaths.size(); ++i)
  250. {
  251. this->SearchPaths.push_back(userPaths[i]);
  252. }
  253. // clean things up
  254. this->ExpandRegistryAndCleanPath();
  255. }
  256. void cmFindBase::AddEnvironmentVairables()
  257. {
  258. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  259. {
  260. cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
  261. }
  262. std::string var = "CMAKE_";
  263. var += this->CMakePathName;
  264. var += "_PATH";
  265. cmSystemTools::GetPath(this->SearchPaths, var.c_str());
  266. if(this->SearchFrameworkLast)
  267. {
  268. cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
  269. }
  270. }
  271. void cmFindBase::AddCMakeVairables()
  272. {
  273. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  274. {
  275. if(const char* path = m_Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
  276. {
  277. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  278. }
  279. }
  280. std::string var = "CMAKE_";
  281. var += this->CMakePathName;
  282. var += "_PATH";
  283. if(const char* path = m_Makefile->GetDefinition(var.c_str()))
  284. {
  285. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  286. }
  287. if(this->SearchFrameworkLast)
  288. {
  289. if(const char* path = m_Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
  290. {
  291. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  292. }
  293. }
  294. }
  295. void cmFindBase::AddSystemEnvironmentVairables()
  296. {
  297. // Add LIB or INCLUDE
  298. if(this->EnvironmentPath.size())
  299. {
  300. cmSystemTools::GetPath(this->SearchPaths, this->EnvironmentPath.c_str());
  301. }
  302. // Add PATH
  303. cmSystemTools::GetPath(this->SearchPaths);
  304. }
  305. void cmFindBase::AddCMakeSystemVariables()
  306. {
  307. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  308. {
  309. if(const char* path = m_Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
  310. {
  311. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  312. }
  313. }
  314. std::string var = "CMAKE_SYSTEM_";
  315. var += this->CMakePathName;
  316. var += "_PATH";
  317. if(const char* path = m_Makefile->GetDefinition(var.c_str()))
  318. {
  319. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  320. }
  321. if(this->SearchFrameworkLast)
  322. {
  323. if(const char* path = m_Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
  324. {
  325. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  326. }
  327. }
  328. }
  329. void cmFindBase::ExpandRegistryAndCleanPath()
  330. {
  331. std::vector<std::string> finalPath;
  332. std::vector<std::string>::iterator i;
  333. for(i = this->SearchPaths.begin();
  334. i != this->SearchPaths.end(); ++i)
  335. {
  336. cmSystemTools::ExpandRegistryValues(*i);
  337. cmSystemTools::GlobDirs(i->c_str(), finalPath);
  338. }
  339. this->SearchPaths.clear();
  340. // convert all paths to unix slashes
  341. for(i = finalPath.begin();
  342. i != finalPath.end(); ++i)
  343. {
  344. cmSystemTools::ConvertToUnixSlashes(*i);
  345. // copy each finalPath combined with SearchPathSuffixes
  346. // to the SearchPaths ivar
  347. for(std::vector<std::string>::iterator j = this->SearchPathSuffixes.begin();
  348. j != this->SearchPathSuffixes.end(); ++j)
  349. {
  350. std::string p = *i + std::string("/") + *j;
  351. if(cmSystemTools::FileIsDirectory(p.c_str()))
  352. {
  353. this->SearchPaths.push_back(p);
  354. }
  355. }
  356. }
  357. // now put the path without the path suffixes in the SearchPaths
  358. for(i = finalPath.begin();
  359. i != finalPath.end(); ++i)
  360. {
  361. if(cmSystemTools::FileIsDirectory(i->c_str()))
  362. {
  363. this->SearchPaths.push_back(*i);
  364. }
  365. }
  366. }
  367. void cmFindBase::PrintFindStuff()
  368. {
  369. std::cerr << "VariableName " << this->VariableName << "\n";
  370. std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  371. std::cerr << "NoSystemPath " << this->NoSystemPath << "\n";
  372. std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath << "\n";
  373. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  374. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  375. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  376. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  377. std::cerr << "Names ";
  378. for(unsigned int i =0; i < this->Names.size(); ++i)
  379. {
  380. std::cerr << this->Names[i] << " ";
  381. }
  382. std::cerr << "\n";
  383. std::cerr << "\n";
  384. std::cerr << "SearchPathSuffixes ";
  385. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  386. {
  387. std::cerr << this->SearchPathSuffixes[i] << "\n";
  388. }
  389. std::cerr << "\n";
  390. std::cerr << "SearchPaths\n";
  391. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  392. {
  393. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  394. }
  395. }
  396. bool cmFindBase::CheckForVariableInCache()
  397. {
  398. const char* cacheValue
  399. = m_Makefile->GetDefinition(this->VariableName.c_str());
  400. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  401. {
  402. return true;
  403. }
  404. if(cacheValue)
  405. {
  406. cmCacheManager::CacheIterator it =
  407. m_Makefile->GetCacheManager()->GetCacheIterator(this->VariableName.c_str());
  408. if(!it.IsAtEnd())
  409. {
  410. const char* hs = it.GetProperty("HELPSTRING");
  411. this->VariableDocumentation = hs?hs:"(none)";
  412. }
  413. }
  414. return false;
  415. }