cmFindLibraryCommand.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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::InitialPass(std::vector<std::string> const& argsIn)
  52. {
  53. this->VariableDocumentation = "Path to a library.";
  54. this->CMakePathName = "LIBRARY";
  55. if(!this->ParseArguments(argsIn))
  56. {
  57. return false;
  58. }
  59. if(this->AlreadyInCache)
  60. {
  61. // If the user specifies the entry on the command line without a
  62. // type we should add the type and docstring but keep the original
  63. // value.
  64. if(this->AlreadyInCacheWithoutMetaInfo)
  65. {
  66. this->Makefile->AddCacheDefinition(this->VariableName.c_str(), "",
  67. this->VariableDocumentation.c_str(),
  68. cmCacheManager::FILEPATH);
  69. }
  70. return true;
  71. }
  72. if(const char* abi =
  73. this->Makefile->GetDefinition("CMAKE_INTERNAL_PLATFORM_ABI"))
  74. {
  75. if(strncmp(abi, "ELF N32", 7) ==0)
  76. {
  77. // Convert /lib to /lib32 if the architecture requests it.
  78. this->AddLib32Paths();
  79. }
  80. }
  81. if(this->Makefile->GetCMakeInstance()
  82. ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
  83. {
  84. // add special 64 bit paths if this is a 64 bit compile.
  85. this->AddLib64Paths();
  86. }
  87. std::string library;
  88. for(std::vector<std::string>::iterator i = this->Names.begin();
  89. i != this->Names.end() ; ++i)
  90. {
  91. library = this->FindLibrary(i->c_str());
  92. if(library != "")
  93. {
  94. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  95. library.c_str(),
  96. this->VariableDocumentation.c_str(),
  97. cmCacheManager::FILEPATH);
  98. return true;
  99. }
  100. }
  101. std::string notfound = this->VariableName + "-NOTFOUND";
  102. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  103. notfound.c_str(),
  104. this->VariableDocumentation.c_str(),
  105. cmCacheManager::FILEPATH);
  106. return true;
  107. }
  108. //----------------------------------------------------------------------------
  109. void cmFindLibraryCommand::AddLib32Paths()
  110. {
  111. std::vector<std::string> path32;
  112. bool found32 = false;
  113. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  114. i != this->SearchPaths.end(); ++i)
  115. {
  116. std::string s = *i;
  117. std::string s2 = *i;
  118. cmSystemTools::ReplaceString(s, "lib/", "lib32/");
  119. // try to replace lib with lib32 and see if it is there,
  120. // then prepend it to the path
  121. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  122. {
  123. path32.push_back(s);
  124. found32 = true;
  125. }
  126. // now just add a 32 to the path name and if it is there,
  127. // add it to the path
  128. s2 += "32";
  129. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  130. {
  131. found32 = true;
  132. path32.push_back(s2);
  133. }
  134. // now add the original unchanged path
  135. if(cmSystemTools::FileIsDirectory(i->c_str()))
  136. {
  137. path32.push_back(*i);
  138. }
  139. }
  140. // now replace the SearchPaths with the 32 bit converted path
  141. // if any 32 bit paths were discovered
  142. if(found32)
  143. {
  144. this->SearchPaths = path32;
  145. }
  146. }
  147. void cmFindLibraryCommand::AddLib64Paths()
  148. {
  149. if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  150. GetLanguageEnabled("C"))
  151. {
  152. return;
  153. }
  154. std::string voidsize =
  155. this->Makefile->GetRequiredDefinition("CMAKE_SIZEOF_VOID_P");
  156. int size = atoi(voidsize.c_str());
  157. if(size != 8)
  158. {
  159. return;
  160. }
  161. std::vector<std::string> path64;
  162. bool found64 = false;
  163. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  164. i != this->SearchPaths.end(); ++i)
  165. {
  166. std::string s = *i;
  167. std::string s2 = *i;
  168. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  169. // try to replace lib with lib64 and see if it is there,
  170. // then prepend it to the path
  171. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  172. {
  173. path64.push_back(s);
  174. found64 = true;
  175. }
  176. // now just add a 64 to the path name and if it is there,
  177. // add it to the path
  178. s2 += "64";
  179. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  180. {
  181. found64 = true;
  182. path64.push_back(s2);
  183. }
  184. // now add the original unchanged path
  185. if(cmSystemTools::FileIsDirectory(i->c_str()))
  186. {
  187. path64.push_back(*i);
  188. }
  189. }
  190. // now replace the SearchPaths with the 64 bit converted path
  191. // if any 64 bit paths were discovered
  192. if(found64)
  193. {
  194. this->SearchPaths = path64;
  195. }
  196. }
  197. std::string cmFindLibraryCommand::FindLibrary(const char* name)
  198. {
  199. bool supportFrameworks = false;
  200. bool onlyFrameworks = false;
  201. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  202. if(ff == "FIRST" || ff == "LAST")
  203. {
  204. supportFrameworks = true;
  205. }
  206. if(ff == "ONLY")
  207. {
  208. onlyFrameworks = true;
  209. supportFrameworks = true;
  210. }
  211. const char* prefixes_list =
  212. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  213. const char* suffixes_list =
  214. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  215. std::vector<std::string> prefixes;
  216. std::vector<std::string> suffixes;
  217. cmSystemTools::ExpandListArgument(prefixes_list, prefixes, true);
  218. cmSystemTools::ExpandListArgument(suffixes_list, suffixes, true);
  219. // Add a trailing slash to all paths to aid the search process.
  220. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  221. i != this->SearchPaths.end(); ++i)
  222. {
  223. std::string& p = *i;
  224. if(p.empty() || p[p.size()-1] != '/')
  225. {
  226. p += "/";
  227. }
  228. }
  229. std::string tryPath;
  230. for(std::vector<std::string>::const_iterator p = this->SearchPaths.begin();
  231. p != this->SearchPaths.end(); ++p)
  232. {
  233. if(supportFrameworks)
  234. {
  235. tryPath = *p;
  236. tryPath += name;
  237. tryPath += ".framework";
  238. if(cmSystemTools::FileExists(tryPath.c_str())
  239. && cmSystemTools::FileIsDirectory(tryPath.c_str()))
  240. {
  241. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  242. cmSystemTools::ConvertToUnixSlashes(tryPath);
  243. return tryPath;
  244. }
  245. }
  246. if(!onlyFrameworks)
  247. {
  248. // Try various library naming conventions.
  249. for(std::vector<std::string>::iterator prefix = prefixes.begin();
  250. prefix != prefixes.end(); ++prefix)
  251. {
  252. for(std::vector<std::string>::iterator suffix = suffixes.begin();
  253. suffix != suffixes.end(); ++suffix)
  254. {
  255. tryPath = *p;
  256. tryPath += *prefix;
  257. tryPath += name;
  258. tryPath += *suffix;
  259. if(cmSystemTools::FileExists(tryPath.c_str())
  260. && !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  261. {
  262. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  263. cmSystemTools::ConvertToUnixSlashes(tryPath);
  264. return tryPath;
  265. }
  266. }
  267. }
  268. }
  269. }
  270. // Couldn't find the library.
  271. return "";
  272. }