cmFindLibraryCommand.cxx 6.6 KB

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