cmFindLibraryCommand.cxx 7.0 KB

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