cmFindLibraryCommand.cxx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. "XXX_SYSTEM", "LIB");
  23. cmSystemTools::ReplaceString(this->GenericDocumentation,
  24. "CMAKE_SYSTEM_XXX_PATH",
  25. "CMAKE_SYSTEM_LIBRARY_PATH");
  26. cmSystemTools::ReplaceString(this->GenericDocumentation,
  27. "SEARCH_XXX_DESC", "library");
  28. cmSystemTools::ReplaceString(this->GenericDocumentation,
  29. "SEARCH_XXX", "library");
  30. cmSystemTools::ReplaceString(this->GenericDocumentation,
  31. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  32. "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY");
  33. this->GenericDocumentation +=
  34. "\n"
  35. "If the library found is a framework, then VAR will be set to "
  36. "the full path to the framework <fullPath>/A.framework. "
  37. "When a full path to a framework is used as a library, "
  38. "CMake will use a -framework A, and a -F<fullPath> to "
  39. "link the framework to the target. ";
  40. }
  41. // cmFindLibraryCommand
  42. bool cmFindLibraryCommand::InitialPass(std::vector<std::string> const& argsIn)
  43. {
  44. this->VariableDocumentation = "Path to a library.";
  45. this->CMakePathName = "LIBRARY";
  46. if(!this->ParseArguments(argsIn))
  47. {
  48. return false;
  49. }
  50. if(this->AlreadyInCache)
  51. {
  52. // If the user specifies the entry on the command line without a
  53. // type we should add the type and docstring but keep the original
  54. // value.
  55. if(this->AlreadyInCacheWithoutMetaInfo)
  56. {
  57. this->Makefile->AddCacheDefinition(this->VariableName.c_str(), "",
  58. this->VariableDocumentation.c_str(),
  59. cmCacheManager::FILEPATH);
  60. }
  61. return true;
  62. }
  63. if(this->Makefile->GetCMakeInstance()
  64. ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
  65. {
  66. // add special 64 bit paths if this is a 64 bit compile.
  67. this->AddLib64Paths();
  68. }
  69. std::string library;
  70. for(std::vector<std::string>::iterator i = this->Names.begin();
  71. i != this->Names.end() ; ++i)
  72. {
  73. library = this->FindLibrary(i->c_str());
  74. if(library != "")
  75. {
  76. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  77. library.c_str(),
  78. this->VariableDocumentation.c_str(),
  79. cmCacheManager::FILEPATH);
  80. return true;
  81. }
  82. }
  83. std::string notfound = this->VariableName + "-NOTFOUND";
  84. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  85. notfound.c_str(),
  86. this->VariableDocumentation.c_str(),
  87. cmCacheManager::FILEPATH);
  88. return true;
  89. }
  90. void cmFindLibraryCommand::AddLib64Paths()
  91. {
  92. if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  93. GetLanguageEnabled("C"))
  94. {
  95. return;
  96. }
  97. std::string voidsize =
  98. this->Makefile->GetRequiredDefinition("CMAKE_SIZEOF_VOID_P");
  99. int size = atoi(voidsize.c_str());
  100. std::vector<std::string> path64;
  101. if(size != 8)
  102. {
  103. return;
  104. }
  105. bool found64 = false;
  106. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  107. i != this->SearchPaths.end(); ++i)
  108. {
  109. std::string s = *i;
  110. std::string s2 = *i;
  111. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  112. // try to replace lib with lib64 and see if it is there,
  113. // then prepend it to the path
  114. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  115. {
  116. path64.push_back(s);
  117. found64 = true;
  118. }
  119. // now just add a 64 to the path name and if it is there,
  120. // add it to the path
  121. s2 += "64";
  122. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  123. {
  124. found64 = true;
  125. path64.push_back(s2);
  126. }
  127. // now add the original unchanged path
  128. if(cmSystemTools::FileIsDirectory(i->c_str()))
  129. {
  130. path64.push_back(*i);
  131. }
  132. }
  133. // now replace the SearchPaths with the 64 bit converted path
  134. // if any 64 bit paths were discovered
  135. if(found64)
  136. {
  137. this->SearchPaths = path64;
  138. }
  139. }
  140. std::string cmFindLibraryCommand::FindLibrary(const char* name)
  141. {
  142. bool supportFrameworks = false;
  143. bool onlyFrameworks = false;
  144. std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
  145. if(ff == "FIRST" || ff == "LAST")
  146. {
  147. supportFrameworks = true;
  148. }
  149. if(ff == "ONLY")
  150. {
  151. onlyFrameworks = true;
  152. supportFrameworks = true;
  153. }
  154. const char* prefixes_list =
  155. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  156. const char* suffixes_list =
  157. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  158. std::vector<std::string> prefixes;
  159. std::vector<std::string> suffixes;
  160. cmSystemTools::ExpandListArgument(prefixes_list, prefixes, true);
  161. cmSystemTools::ExpandListArgument(suffixes_list, suffixes, true);
  162. std::string tryPath;
  163. for(std::vector<std::string>::const_iterator p = this->SearchPaths.begin();
  164. p != this->SearchPaths.end(); ++p)
  165. {
  166. if(supportFrameworks)
  167. {
  168. tryPath = *p;
  169. tryPath += "/";
  170. tryPath += name;
  171. tryPath += ".framework";
  172. if(cmSystemTools::FileExists(tryPath.c_str())
  173. && cmSystemTools::FileIsDirectory(tryPath.c_str()))
  174. {
  175. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  176. cmSystemTools::ConvertToUnixSlashes(tryPath);
  177. return tryPath;
  178. }
  179. }
  180. if(!onlyFrameworks)
  181. {
  182. // Try various library naming conventions.
  183. for(std::vector<std::string>::iterator prefix = prefixes.begin();
  184. prefix != prefixes.end(); ++prefix)
  185. {
  186. for(std::vector<std::string>::iterator suffix = suffixes.begin();
  187. suffix != suffixes.end(); ++suffix)
  188. {
  189. tryPath = *p;
  190. tryPath += "/";
  191. tryPath += *prefix;
  192. tryPath += name;
  193. tryPath += *suffix;
  194. if(cmSystemTools::FileExists(tryPath.c_str())
  195. && !cmSystemTools::FileIsDirectory(tryPath.c_str()))
  196. {
  197. tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
  198. cmSystemTools::ConvertToUnixSlashes(tryPath);
  199. return tryPath;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. // Couldn't find the library.
  206. return "";
  207. }