cmFindLibraryCommand.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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(this->Makefile->GetCMakeInstance()
  73. ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
  74. {
  75. // add special 64 bit paths if this is a 64 bit compile.
  76. this->AddLib64Paths();
  77. }
  78. std::string library;
  79. for(std::vector<std::string>::iterator i = this->Names.begin();
  80. i != this->Names.end() ; ++i)
  81. {
  82. library = this->FindLibrary(i->c_str());
  83. if(library != "")
  84. {
  85. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  86. library.c_str(),
  87. this->VariableDocumentation.c_str(),
  88. cmCacheManager::FILEPATH);
  89. return true;
  90. }
  91. }
  92. std::string notfound = this->VariableName + "-NOTFOUND";
  93. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  94. notfound.c_str(),
  95. this->VariableDocumentation.c_str(),
  96. cmCacheManager::FILEPATH);
  97. return true;
  98. }
  99. void cmFindLibraryCommand::AddLib64Paths()
  100. {
  101. if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  102. GetLanguageEnabled("C"))
  103. {
  104. return;
  105. }
  106. std::string voidsize =
  107. this->Makefile->GetRequiredDefinition("CMAKE_SIZEOF_VOID_P");
  108. int size = atoi(voidsize.c_str());
  109. if(size != 8)
  110. {
  111. return;
  112. }
  113. std::vector<std::string> path64;
  114. bool found64 = false;
  115. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  116. i != this->SearchPaths.end(); ++i)
  117. {
  118. std::string s = *i;
  119. std::string s2 = *i;
  120. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  121. // try to replace lib with lib64 and see if it is there,
  122. // then prepend it to the path
  123. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  124. {
  125. path64.push_back(s);
  126. found64 = true;
  127. }
  128. // now just add a 64 to the path name and if it is there,
  129. // add it to the path
  130. s2 += "64";
  131. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  132. {
  133. found64 = true;
  134. path64.push_back(s2);
  135. }
  136. // now add the original unchanged path
  137. if(cmSystemTools::FileIsDirectory(i->c_str()))
  138. {
  139. path64.push_back(*i);
  140. }
  141. }
  142. // now replace the SearchPaths with the 64 bit converted path
  143. // if any 64 bit paths were discovered
  144. if(found64)
  145. {
  146. this->SearchPaths = path64;
  147. }
  148. }
  149. std::string cmFindLibraryCommand::FindLibrary(const char* name)
  150. {
  151. bool supportFrameworks = false;
  152. bool onlyFrameworks = false;
  153. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  154. if(ff == "FIRST" || ff == "LAST")
  155. {
  156. supportFrameworks = true;
  157. }
  158. if(ff == "ONLY")
  159. {
  160. onlyFrameworks = true;
  161. supportFrameworks = true;
  162. }
  163. const char* prefixes_list =
  164. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  165. const char* suffixes_list =
  166. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  167. std::vector<std::string> prefixes;
  168. std::vector<std::string> suffixes;
  169. cmSystemTools::ExpandListArgument(prefixes_list, prefixes, true);
  170. cmSystemTools::ExpandListArgument(suffixes_list, suffixes, true);
  171. std::string tryPath;
  172. for(std::vector<std::string>::const_iterator p = this->SearchPaths.begin();
  173. p != this->SearchPaths.end(); ++p)
  174. {
  175. if(supportFrameworks)
  176. {
  177. tryPath = *p;
  178. tryPath += "/";
  179. tryPath += name;
  180. tryPath += ".framework";
  181. if(cmSystemTools::FileExists(tryPath.c_str())
  182. && cmSystemTools::FileIsDirectory(tryPath.c_str()))
  183. {
  184. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  185. cmSystemTools::ConvertToUnixSlashes(tryPath);
  186. return tryPath;
  187. }
  188. }
  189. if(!onlyFrameworks)
  190. {
  191. // Try various library naming conventions.
  192. for(std::vector<std::string>::iterator prefix = prefixes.begin();
  193. prefix != prefixes.end(); ++prefix)
  194. {
  195. for(std::vector<std::string>::iterator suffix = suffixes.begin();
  196. suffix != suffixes.end(); ++suffix)
  197. {
  198. tryPath = *p;
  199. tryPath += "/";
  200. tryPath += *prefix;
  201. tryPath += name;
  202. tryPath += *suffix;
  203. if(cmSystemTools::FileExists(tryPath.c_str())
  204. && !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  205. {
  206. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  207. cmSystemTools::ConvertToUnixSlashes(tryPath);
  208. return tryPath;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. // Couldn't find the library.
  215. return "";
  216. }