cmFindLibraryCommand.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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;
  90. for(std::vector<std::string>::iterator i = this->Names.begin();
  91. i != this->Names.end() ; ++i)
  92. {
  93. library = this->FindLibrary(i->c_str());
  94. if(library != "")
  95. {
  96. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  97. library.c_str(),
  98. this->VariableDocumentation.c_str(),
  99. cmCacheManager::FILEPATH);
  100. return true;
  101. }
  102. }
  103. std::string notfound = this->VariableName + "-NOTFOUND";
  104. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  105. notfound.c_str(),
  106. this->VariableDocumentation.c_str(),
  107. cmCacheManager::FILEPATH);
  108. return true;
  109. }
  110. //----------------------------------------------------------------------------
  111. void cmFindLibraryCommand::AddArchitecturePaths(const char* suffix)
  112. {
  113. std::vector<std::string> newPaths;
  114. bool found = false;
  115. std::string subpath = "lib";
  116. subpath += suffix;
  117. subpath += "/";
  118. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  119. i != this->SearchPaths.end(); ++i)
  120. {
  121. // Try replacing lib/ with lib<suffix>/
  122. std::string s = *i;
  123. cmSystemTools::ReplaceString(s, "lib/", subpath.c_str());
  124. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  125. {
  126. found = true;
  127. newPaths.push_back(s);
  128. }
  129. // Now look for lib<suffix>
  130. s = *i;
  131. s += suffix;
  132. if(cmSystemTools::FileIsDirectory(s.c_str()))
  133. {
  134. found = true;
  135. newPaths.push_back(s);
  136. }
  137. // now add the original unchanged path
  138. if(cmSystemTools::FileIsDirectory(i->c_str()))
  139. {
  140. newPaths.push_back(*i);
  141. }
  142. }
  143. // If any new paths were found replace the original set.
  144. if(found)
  145. {
  146. this->SearchPaths = newPaths;
  147. }
  148. }
  149. void cmFindLibraryCommand::AddLib64Paths()
  150. {
  151. if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  152. GetLanguageEnabled("C"))
  153. {
  154. return;
  155. }
  156. std::string voidsize =
  157. this->Makefile->GetRequiredDefinition("CMAKE_SIZEOF_VOID_P");
  158. int size = atoi(voidsize.c_str());
  159. if(size != 8)
  160. {
  161. return;
  162. }
  163. std::vector<std::string> path64;
  164. bool found64 = false;
  165. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  166. i != this->SearchPaths.end(); ++i)
  167. {
  168. std::string s = *i;
  169. std::string s2 = *i;
  170. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  171. // try to replace lib with lib64 and see if it is there,
  172. // then prepend it to the path
  173. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  174. {
  175. path64.push_back(s);
  176. found64 = true;
  177. }
  178. // now just add a 64 to the path name and if it is there,
  179. // add it to the path
  180. s2 += "64";
  181. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  182. {
  183. found64 = true;
  184. path64.push_back(s2);
  185. }
  186. // now add the original unchanged path
  187. if(cmSystemTools::FileIsDirectory(i->c_str()))
  188. {
  189. path64.push_back(*i);
  190. }
  191. }
  192. // now replace the SearchPaths with the 64 bit converted path
  193. // if any 64 bit paths were discovered
  194. if(found64)
  195. {
  196. this->SearchPaths = path64;
  197. }
  198. }
  199. std::string cmFindLibraryCommand::FindLibrary(const char* name)
  200. {
  201. bool supportFrameworks = false;
  202. bool onlyFrameworks = false;
  203. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  204. if(ff == "FIRST" || ff == "LAST")
  205. {
  206. supportFrameworks = true;
  207. }
  208. if(ff == "ONLY")
  209. {
  210. onlyFrameworks = true;
  211. supportFrameworks = true;
  212. }
  213. const char* prefixes_list =
  214. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  215. const char* suffixes_list =
  216. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  217. std::vector<std::string> prefixes;
  218. std::vector<std::string> suffixes;
  219. cmSystemTools::ExpandListArgument(prefixes_list, prefixes, true);
  220. cmSystemTools::ExpandListArgument(suffixes_list, suffixes, true);
  221. // Add a trailing slash to all paths to aid the search process.
  222. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  223. i != this->SearchPaths.end(); ++i)
  224. {
  225. std::string& p = *i;
  226. if(p.empty() || p[p.size()-1] != '/')
  227. {
  228. p += "/";
  229. }
  230. }
  231. std::string tryPath;
  232. for(std::vector<std::string>::const_iterator p = this->SearchPaths.begin();
  233. p != this->SearchPaths.end(); ++p)
  234. {
  235. if(supportFrameworks)
  236. {
  237. tryPath = *p;
  238. tryPath += name;
  239. tryPath += ".framework";
  240. if(cmSystemTools::FileExists(tryPath.c_str())
  241. && cmSystemTools::FileIsDirectory(tryPath.c_str()))
  242. {
  243. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  244. cmSystemTools::ConvertToUnixSlashes(tryPath);
  245. return tryPath;
  246. }
  247. }
  248. if(!onlyFrameworks)
  249. {
  250. // Try various library naming conventions.
  251. for(std::vector<std::string>::iterator prefix = prefixes.begin();
  252. prefix != prefixes.end(); ++prefix)
  253. {
  254. for(std::vector<std::string>::iterator suffix = suffixes.begin();
  255. suffix != suffixes.end(); ++suffix)
  256. {
  257. tryPath = *p;
  258. tryPath += *prefix;
  259. tryPath += name;
  260. tryPath += *suffix;
  261. if(cmSystemTools::FileExists(tryPath.c_str())
  262. && !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  263. {
  264. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  265. cmSystemTools::ConvertToUnixSlashes(tryPath);
  266. return tryPath;
  267. }
  268. }
  269. }
  270. }
  271. }
  272. // Couldn't find the library.
  273. return "";
  274. }