cmFindBase.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 foundName = false;
  132. bool doingNames = false;
  133. bool foundPaths = false;
  134. bool doingPaths = false;
  135. bool doingPathSuf = false;
  136. bool newStyle = false;
  137. for (unsigned int j = 1; j < args.size(); ++j)
  138. {
  139. if(args[j] == "NAMES")
  140. {
  141. doingNames = true;
  142. newStyle = true;
  143. doingPathSuf = false;
  144. doingPaths = false;
  145. }
  146. else if (args[j] == "PATHS")
  147. {
  148. doingPaths = true;
  149. newStyle = true;
  150. doingNames = false;
  151. doingPathSuf = false;
  152. }
  153. else if (args[j] == "PATH_SUFFIXES")
  154. {
  155. doingPathSuf = true;
  156. newStyle = true;
  157. doingNames = false;
  158. doingPaths = false;
  159. }
  160. else if (args[j] == "NO_SYSTEM_PATH")
  161. {
  162. this->NoSystemPath = true;
  163. }
  164. else if (args[j] == "NO_CMAKE_PATH")
  165. {
  166. this->NoCMakePath = true;
  167. }
  168. else if (args[j] == "NO_CMAKE_ENVIRONMENT_PATH")
  169. {
  170. this->NoCMakeEnvironmentPath = true;
  171. }
  172. else if (args[j] == "NO_CMAKE_SYSTEM_PATH")
  173. {
  174. this->NoCMakeSystemPath = true;
  175. }
  176. else
  177. {
  178. if(doingNames)
  179. {
  180. this->Names.push_back(args[j]);
  181. }
  182. else if(doingPaths)
  183. {
  184. userPaths.push_back(args[j]);
  185. }
  186. else if(doingPathSuf)
  187. {
  188. this->SearchPathSuffixes.push_back(args[j]);
  189. }
  190. }
  191. }
  192. if(this->VariableDocumentation.size() == 0)
  193. {
  194. this->VariableDocumentation = "Whare can ";
  195. if(this->Names.size() == 0)
  196. {
  197. this->VariableDocumentation += "the (unknown) library be found";
  198. }
  199. else if(this->Names.size() == 1)
  200. {
  201. this->VariableDocumentation += "the " + this->Names[0] + " library be found";
  202. }
  203. else
  204. {
  205. this->VariableDocumentation += "one of the " + this->Names[0];
  206. for (unsigned int j = 1; j < this->Names.size() - 1; ++j)
  207. {
  208. this->VariableDocumentation += ", " + this->Names[j];
  209. }
  210. this->VariableDocumentation += " or " + this->Names[this->Names.size() - 1] + " libraries be found";
  211. }
  212. }
  213. // look for old style
  214. // FIND_*(VAR name path1 path2 ...)
  215. if(!newStyle)
  216. {
  217. this->Names.push_back(args[1]);
  218. for(unsigned int j = 2; j < args.size(); ++j)
  219. {
  220. userPaths.push_back(args[j]);
  221. }
  222. }
  223. this->ExpandPaths(userPaths);
  224. return true;
  225. }
  226. void cmFindBase::ExpandPaths(std::vector<std::string> userPaths)
  227. {
  228. // Add CMAKE_*_PATH environment variables
  229. if(!this->NoCMakeEnvironmentPath)
  230. {
  231. this->AddEnvironmentVairables();
  232. }
  233. // Add CMake varibles of the same name as the previous environment
  234. // varibles CMAKE_*_PATH to be used most of the time with -D
  235. // command line options
  236. if(!this->NoCMakePath)
  237. {
  238. this->AddCMakeVairables();
  239. }
  240. // add System environment PATH and (LIB or INCLUDE)
  241. if(!this->NoSystemPath)
  242. {
  243. this->AddSystemEnvironmentVairables();
  244. }
  245. // Add CMAKE_SYSTEM_*_PATH variables which are defined in platform files
  246. if(!this->NoCMakeSystemPath)
  247. {
  248. this->AddCMakeSystemVariables();
  249. }
  250. // add the paths specified in the FIND_* call
  251. for(unsigned int i =0; i < userPaths.size(); ++i)
  252. {
  253. this->SearchPaths.push_back(userPaths[i]);
  254. }
  255. // clean things up
  256. this->ExpandRegistryAndCleanPath();
  257. }
  258. void cmFindBase::AddEnvironmentVairables()
  259. {
  260. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  261. {
  262. cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
  263. }
  264. std::string var = "CMAKE_";
  265. var += this->CMakePathName;
  266. var += "_PATH";
  267. cmSystemTools::GetPath(this->SearchPaths, var.c_str());
  268. if(this->SearchFrameworkLast)
  269. {
  270. cmSystemTools::GetPath(this->SearchPaths, "CMAKE_FRAMEWORK_PATH");
  271. }
  272. }
  273. void cmFindBase::AddCMakeVairables()
  274. {
  275. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  276. {
  277. if(const char* path = m_Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
  278. {
  279. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  280. }
  281. }
  282. std::string var = "CMAKE_";
  283. var += this->CMakePathName;
  284. var += "_PATH";
  285. if(const char* path = m_Makefile->GetDefinition(var.c_str()))
  286. {
  287. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  288. }
  289. if(this->SearchFrameworkLast)
  290. {
  291. if(const char* path = m_Makefile->GetDefinition("CMAKE_FRAMEWORK_PATH"))
  292. {
  293. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  294. }
  295. }
  296. }
  297. void cmFindBase::AddSystemEnvironmentVairables()
  298. {
  299. // Add LIB or INCLUDE
  300. if(this->EnvironmentPath.size())
  301. {
  302. cmSystemTools::GetPath(this->SearchPaths, this->EnvironmentPath.c_str());
  303. }
  304. // Add PATH
  305. cmSystemTools::GetPath(this->SearchPaths);
  306. }
  307. void cmFindBase::AddCMakeSystemVariables()
  308. {
  309. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  310. {
  311. if(const char* path = m_Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
  312. {
  313. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  314. }
  315. }
  316. std::string var = "CMAKE_SYSTEM_";
  317. var += this->CMakePathName;
  318. var += "_PATH";
  319. if(const char* path = m_Makefile->GetDefinition(var.c_str()))
  320. {
  321. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  322. }
  323. if(this->SearchFrameworkLast)
  324. {
  325. if(const char* path = m_Makefile->GetDefinition("CMAKE_SYSTEM_FRAMEWORK_PATH"))
  326. {
  327. cmSystemTools::ExpandListArgument(path, this->SearchPaths);
  328. }
  329. }
  330. }
  331. void cmFindBase::ExpandRegistryAndCleanPath()
  332. {
  333. std::vector<std::string> finalPath;
  334. std::vector<std::string>::iterator i;
  335. for(i = this->SearchPaths.begin();
  336. i != this->SearchPaths.end(); ++i)
  337. {
  338. cmSystemTools::ExpandRegistryValues(*i);
  339. cmSystemTools::GlobDirs(i->c_str(), finalPath);
  340. }
  341. this->SearchPaths.clear();
  342. // convert all paths to unix slashes
  343. for(i = finalPath.begin();
  344. i != finalPath.end(); ++i)
  345. {
  346. cmSystemTools::ConvertToUnixSlashes(*i);
  347. // copy each finalPath combined with SearchPathSuffixes
  348. // to the SearchPaths ivar
  349. for(std::vector<std::string>::iterator j = this->SearchPathSuffixes.begin();
  350. j != this->SearchPathSuffixes.end(); ++j)
  351. {
  352. std::string p = *i + std::string("/") + *j;
  353. if(cmSystemTools::FileIsDirectory(p.c_str()))
  354. {
  355. this->SearchPaths.push_back(p);
  356. }
  357. }
  358. }
  359. // now put the path without the path suffixes in the SearchPaths
  360. for(i = finalPath.begin();
  361. i != finalPath.end(); ++i)
  362. {
  363. if(cmSystemTools::FileIsDirectory(i->c_str()))
  364. {
  365. this->SearchPaths.push_back(*i);
  366. }
  367. }
  368. }
  369. void cmFindBase::PrintFindStuff()
  370. {
  371. std::cerr << "VariableName " << this->VariableName << "\n";
  372. std::cerr << "VariableDocumentation " << this->VariableDocumentation << "\n";
  373. std::cerr << "NoSystemPath " << this->NoSystemPath << "\n";
  374. std::cerr << "NoCMakeEnvironmentPath " << this->NoCMakeEnvironmentPath << "\n";
  375. std::cerr << "NoCMakePath " << this->NoCMakePath << "\n";
  376. std::cerr << "NoCMakeSystemPath " << this->NoCMakeSystemPath << "\n";
  377. std::cerr << "EnvironmentPath " << this->EnvironmentPath << "\n";
  378. std::cerr << "CMakePathName " << this->CMakePathName << "\n";
  379. std::cerr << "Names ";
  380. for(unsigned int i =0; i < this->Names.size(); ++i)
  381. {
  382. std::cerr << this->Names[i] << " ";
  383. }
  384. std::cerr << "\n";
  385. std::cerr << "\n";
  386. std::cerr << "SearchPathSuffixes ";
  387. for(unsigned int i =0; i < this->SearchPathSuffixes.size(); ++i)
  388. {
  389. std::cerr << this->SearchPathSuffixes[i] << "\n";
  390. }
  391. std::cerr << "\n";
  392. std::cerr << "SearchPaths\n";
  393. for(unsigned int i =0; i < this->SearchPaths.size(); ++i)
  394. {
  395. std::cerr << "[" << this->SearchPaths[i] << "]\n";
  396. }
  397. }
  398. bool cmFindBase::CheckForVariableInCache()
  399. {
  400. const char* cacheValue
  401. = m_Makefile->GetDefinition(this->VariableName.c_str());
  402. if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
  403. {
  404. return true;
  405. }
  406. if(cacheValue)
  407. {
  408. cmCacheManager::CacheIterator it =
  409. m_Makefile->GetCacheManager()->GetCacheIterator(this->VariableName.c_str());
  410. if(!it.IsAtEnd())
  411. {
  412. const char* hs = it.GetProperty("HELPSTRING");
  413. this->VariableDocumentation = hs?hs:"(none)";
  414. }
  415. }
  416. return false;
  417. }