You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
7.4 KiB

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