cmFindLibraryCommand.cxx 9.5 KB

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