cmFindLibraryCommand.cxx 6.5 KB

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