cmFindLibraryCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 "cmFindLibraryCommand.h"
  14. #include "cmCacheManager.h"
  15. cmFindLibraryCommand::cmFindLibraryCommand()
  16. {
  17. cmSystemTools::ReplaceString(this->GenericDocumentation,
  18. "FIND_XXX", "find_library");
  19. cmSystemTools::ReplaceString(this->GenericDocumentation,
  20. "CMAKE_XXX_PATH", "CMAKE_LIBRARY_PATH");
  21. cmSystemTools::ReplaceString(this->GenericDocumentation,
  22. "CMAKE_XXX_MAC_PATH",
  23. "CMAKE_FRAMEWORK_PATH");
  24. cmSystemTools::ReplaceString(this->GenericDocumentation,
  25. "CMAKE_SYSTEM_XXX_MAC_PATH",
  26. "CMAKE_SYSTEM_FRAMEWORK_PATH");
  27. cmSystemTools::ReplaceString(this->GenericDocumentation,
  28. "XXX_SYSTEM", "LIB");
  29. cmSystemTools::ReplaceString(this->GenericDocumentation,
  30. "CMAKE_SYSTEM_XXX_PATH",
  31. "CMAKE_SYSTEM_LIBRARY_PATH");
  32. cmSystemTools::ReplaceString(this->GenericDocumentation,
  33. "SEARCH_XXX_DESC", "library");
  34. cmSystemTools::ReplaceString(this->GenericDocumentation,
  35. "SEARCH_XXX", "library");
  36. cmSystemTools::ReplaceString(this->GenericDocumentation,
  37. "XXX_SUBDIR", "lib");
  38. cmSystemTools::ReplaceString(this->GenericDocumentation,
  39. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  40. "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY");
  41. this->EnvironmentPath = "LIB";
  42. this->GenericDocumentation +=
  43. "\n"
  44. "If the library found is a framework, then VAR will be set to "
  45. "the full path to the framework <fullPath>/A.framework. "
  46. "When a full path to a framework is used as a library, "
  47. "CMake will use a -framework A, and a -F<fullPath> to "
  48. "link the framework to the target. ";
  49. }
  50. // cmFindLibraryCommand
  51. bool cmFindLibraryCommand
  52. ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
  53. {
  54. this->VariableDocumentation = "Path to a library.";
  55. this->CMakePathName = "LIBRARY";
  56. if(!this->ParseArguments(argsIn))
  57. {
  58. return false;
  59. }
  60. if(this->AlreadyInCache)
  61. {
  62. // If the user specifies the entry on the command line without a
  63. // type we should add the type and docstring but keep the original
  64. // value.
  65. if(this->AlreadyInCacheWithoutMetaInfo)
  66. {
  67. this->Makefile->AddCacheDefinition(this->VariableName.c_str(), "",
  68. this->VariableDocumentation.c_str(),
  69. cmCacheManager::FILEPATH);
  70. }
  71. return true;
  72. }
  73. if(const char* abi_name =
  74. this->Makefile->GetDefinition("CMAKE_INTERNAL_PLATFORM_ABI"))
  75. {
  76. std::string abi = abi_name;
  77. if(abi.find("ELF N32") != abi.npos)
  78. {
  79. // Convert lib to lib32.
  80. this->AddArchitecturePaths("32");
  81. }
  82. }
  83. if(this->Makefile->GetCMakeInstance()
  84. ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
  85. {
  86. // add special 64 bit paths if this is a 64 bit compile.
  87. this->AddLib64Paths();
  88. }
  89. std::string library = this->FindLibrary();
  90. if(library != "")
  91. {
  92. // Save the value in the cache
  93. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  94. library.c_str(),
  95. this->VariableDocumentation.c_str(),
  96. cmCacheManager::FILEPATH);
  97. return true;
  98. }
  99. std::string notfound = this->VariableName + "-NOTFOUND";
  100. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  101. notfound.c_str(),
  102. this->VariableDocumentation.c_str(),
  103. cmCacheManager::FILEPATH);
  104. return true;
  105. }
  106. //----------------------------------------------------------------------------
  107. void cmFindLibraryCommand::AddArchitecturePaths(const char* suffix)
  108. {
  109. std::vector<std::string> newPaths;
  110. bool found = false;
  111. std::string subpath = "lib";
  112. subpath += suffix;
  113. subpath += "/";
  114. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  115. i != this->SearchPaths.end(); ++i)
  116. {
  117. // Try replacing lib/ with lib<suffix>/
  118. std::string s = *i;
  119. cmSystemTools::ReplaceString(s, "lib/", subpath.c_str());
  120. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  121. {
  122. found = true;
  123. newPaths.push_back(s);
  124. }
  125. // Now look for lib<suffix>
  126. s = *i;
  127. s += suffix;
  128. if(cmSystemTools::FileIsDirectory(s.c_str()))
  129. {
  130. found = true;
  131. newPaths.push_back(s);
  132. }
  133. // now add the original unchanged path
  134. if(cmSystemTools::FileIsDirectory(i->c_str()))
  135. {
  136. newPaths.push_back(*i);
  137. }
  138. }
  139. // If any new paths were found replace the original set.
  140. if(found)
  141. {
  142. this->SearchPaths = newPaths;
  143. }
  144. }
  145. void cmFindLibraryCommand::AddLib64Paths()
  146. {
  147. if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  148. GetLanguageEnabled("C"))
  149. {
  150. return;
  151. }
  152. std::string voidsize =
  153. this->Makefile->GetSafeDefinition("CMAKE_SIZEOF_VOID_P");
  154. int size = atoi(voidsize.c_str());
  155. if(size != 8)
  156. {
  157. return;
  158. }
  159. std::vector<std::string> path64;
  160. bool found64 = false;
  161. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  162. i != this->SearchPaths.end(); ++i)
  163. {
  164. std::string s = *i;
  165. std::string s2 = *i;
  166. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  167. // try to replace lib with lib64 and see if it is there,
  168. // then prepend it to the path
  169. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  170. {
  171. path64.push_back(s);
  172. found64 = true;
  173. }
  174. // now just add a 64 to the path name and if it is there,
  175. // add it to the path
  176. s2 += "64";
  177. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  178. {
  179. found64 = true;
  180. path64.push_back(s2);
  181. }
  182. // now add the original unchanged path
  183. if(cmSystemTools::FileIsDirectory(i->c_str()))
  184. {
  185. path64.push_back(*i);
  186. }
  187. }
  188. // now replace the SearchPaths with the 64 bit converted path
  189. // if any 64 bit paths were discovered
  190. if(found64)
  191. {
  192. this->SearchPaths = path64;
  193. }
  194. }
  195. //----------------------------------------------------------------------------
  196. std::string cmFindLibraryCommand::FindLibrary()
  197. {
  198. std::string library;
  199. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  200. {
  201. library = this->FindFrameworkLibrary();
  202. }
  203. if(library.empty() && !this->SearchFrameworkOnly)
  204. {
  205. library = this->FindNormalLibrary();
  206. }
  207. if(library.empty() && this->SearchFrameworkLast)
  208. {
  209. library = this->FindFrameworkLibrary();
  210. }
  211. return library;
  212. }
  213. //----------------------------------------------------------------------------
  214. std::string cmFindLibraryCommand::FindNormalLibrary()
  215. {
  216. // Collect the list of library name prefixes/suffixes to try.
  217. const char* prefixes_list =
  218. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  219. const char* suffixes_list =
  220. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  221. std::vector<std::string> prefixes;
  222. std::vector<std::string> suffixes;
  223. cmSystemTools::ExpandListArgument(prefixes_list, prefixes, true);
  224. cmSystemTools::ExpandListArgument(suffixes_list, suffixes, true);
  225. // Search the entire path for each name.
  226. std::string tryPath;
  227. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  228. ni != this->Names.end() ; ++ni)
  229. {
  230. // If the original library name provided by the user matches one of
  231. // the suffixes, try it first.
  232. bool tryOrig = false;
  233. std::string const& name = *ni;
  234. for(std::vector<std::string>::const_iterator si = suffixes.begin();
  235. !tryOrig && si != suffixes.end(); ++si)
  236. {
  237. std::string const& suffix = *si;
  238. if(name.length() > suffix.length() &&
  239. name.substr(name.size()-suffix.length()) == suffix)
  240. {
  241. tryOrig = true;
  242. }
  243. }
  244. for(std::vector<std::string>::const_iterator
  245. p = this->SearchPaths.begin();
  246. p != this->SearchPaths.end(); ++p)
  247. {
  248. // Try the original library name as specified by the user.
  249. if(tryOrig)
  250. {
  251. tryPath = *p;
  252. tryPath += name;
  253. if(cmSystemTools::FileExists(tryPath.c_str(), true))
  254. {
  255. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  256. cmSystemTools::ConvertToUnixSlashes(tryPath);
  257. return tryPath;
  258. }
  259. }
  260. // Try various library naming conventions.
  261. for(std::vector<std::string>::iterator prefix = prefixes.begin();
  262. prefix != prefixes.end(); ++prefix)
  263. {
  264. for(std::vector<std::string>::iterator suffix = suffixes.begin();
  265. suffix != suffixes.end(); ++suffix)
  266. {
  267. tryPath = *p;
  268. tryPath += *prefix;
  269. tryPath += name;
  270. tryPath += *suffix;
  271. if(cmSystemTools::FileExists(tryPath.c_str())
  272. && !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  273. {
  274. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  275. cmSystemTools::ConvertToUnixSlashes(tryPath);
  276. return tryPath;
  277. }
  278. }
  279. }
  280. }
  281. }
  282. // Couldn't find the library.
  283. return "";
  284. }
  285. //----------------------------------------------------------------------------
  286. std::string cmFindLibraryCommand::FindFrameworkLibrary()
  287. {
  288. // Search for a framework of each name in the entire search path.
  289. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  290. ni != this->Names.end() ; ++ni)
  291. {
  292. // Search the paths for a framework with this name.
  293. std::string fwName = *ni;
  294. fwName += ".framework";
  295. std::string fwPath = cmSystemTools::FindDirectory(fwName.c_str(),
  296. this->SearchPaths,
  297. true);
  298. if(!fwPath.empty())
  299. {
  300. return fwPath;
  301. }
  302. }
  303. // No framework found.
  304. return "";
  305. }