BUG: fix some bad changes in progress calc
[cmake.git] / Source / cmFindLibraryCommand.cxx
blobf5bb2d7c9edf9b7ae2aa3c90f59807613770fa01
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmFindLibraryCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008-02-11 22:00:45 $
7 Version: $Revision: 1.58 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmFindLibraryCommand.h"
18 #include "cmCacheManager.h"
20 cmFindLibraryCommand::cmFindLibraryCommand()
22 cmSystemTools::ReplaceString(this->GenericDocumentation,
23 "FIND_XXX", "find_library");
24 cmSystemTools::ReplaceString(this->GenericDocumentation,
25 "CMAKE_XXX_PATH", "CMAKE_LIBRARY_PATH");
26 cmSystemTools::ReplaceString(this->GenericDocumentation,
27 "CMAKE_XXX_MAC_PATH",
28 "CMAKE_FRAMEWORK_PATH");
29 cmSystemTools::ReplaceString(this->GenericDocumentation,
30 "CMAKE_SYSTEM_XXX_MAC_PATH",
31 "CMAKE_SYSTEM_FRAMEWORK_PATH");
32 cmSystemTools::ReplaceString(this->GenericDocumentation,
33 "XXX_SYSTEM", "LIB");
34 cmSystemTools::ReplaceString(this->GenericDocumentation,
35 "CMAKE_SYSTEM_XXX_PATH",
36 "CMAKE_SYSTEM_LIBRARY_PATH");
37 cmSystemTools::ReplaceString(this->GenericDocumentation,
38 "SEARCH_XXX_DESC", "library");
39 cmSystemTools::ReplaceString(this->GenericDocumentation,
40 "SEARCH_XXX", "library");
41 cmSystemTools::ReplaceString(this->GenericDocumentation,
42 "XXX_SUBDIR", "lib");
43 cmSystemTools::ReplaceString(this->GenericDocumentation,
44 "CMAKE_FIND_ROOT_PATH_MODE_XXX",
45 "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY");
47 this->EnvironmentPath = "LIB";
48 this->GenericDocumentation +=
49 "\n"
50 "If the library found is a framework, then VAR will be set to "
51 "the full path to the framework <fullPath>/A.framework. "
52 "When a full path to a framework is used as a library, "
53 "CMake will use a -framework A, and a -F<fullPath> to "
54 "link the framework to the target. ";
57 // cmFindLibraryCommand
58 bool cmFindLibraryCommand
59 ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
61 this->VariableDocumentation = "Path to a library.";
62 this->CMakePathName = "LIBRARY";
63 if(!this->ParseArguments(argsIn))
65 return false;
67 if(this->AlreadyInCache)
69 // If the user specifies the entry on the command line without a
70 // type we should add the type and docstring but keep the original
71 // value.
72 if(this->AlreadyInCacheWithoutMetaInfo)
74 this->Makefile->AddCacheDefinition(this->VariableName.c_str(), "",
75 this->VariableDocumentation.c_str(),
76 cmCacheManager::FILEPATH);
78 return true;
81 if(const char* abi_name =
82 this->Makefile->GetDefinition("CMAKE_INTERNAL_PLATFORM_ABI"))
84 std::string abi = abi_name;
85 if(abi.find("ELF N32") != abi.npos)
87 // Convert lib to lib32.
88 this->AddArchitecturePaths("32");
92 if(this->Makefile->GetCMakeInstance()
93 ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
95 // add special 64 bit paths if this is a 64 bit compile.
96 this->AddLib64Paths();
99 std::string library;
100 for(std::vector<std::string>::iterator i = this->Names.begin();
101 i != this->Names.end() ; ++i)
103 library = this->FindLibrary(i->c_str());
104 if(library != "")
106 this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
107 library.c_str(),
108 this->VariableDocumentation.c_str(),
109 cmCacheManager::FILEPATH);
110 return true;
113 std::string notfound = this->VariableName + "-NOTFOUND";
114 this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
115 notfound.c_str(),
116 this->VariableDocumentation.c_str(),
117 cmCacheManager::FILEPATH);
118 return true;
121 //----------------------------------------------------------------------------
122 void cmFindLibraryCommand::AddArchitecturePaths(const char* suffix)
124 std::vector<std::string> newPaths;
125 bool found = false;
126 std::string subpath = "lib";
127 subpath += suffix;
128 subpath += "/";
129 for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
130 i != this->SearchPaths.end(); ++i)
132 // Try replacing lib/ with lib<suffix>/
133 std::string s = *i;
134 cmSystemTools::ReplaceString(s, "lib/", subpath.c_str());
135 if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
137 found = true;
138 newPaths.push_back(s);
141 // Now look for lib<suffix>
142 s = *i;
143 s += suffix;
144 if(cmSystemTools::FileIsDirectory(s.c_str()))
146 found = true;
147 newPaths.push_back(s);
149 // now add the original unchanged path
150 if(cmSystemTools::FileIsDirectory(i->c_str()))
152 newPaths.push_back(*i);
156 // If any new paths were found replace the original set.
157 if(found)
159 this->SearchPaths = newPaths;
163 void cmFindLibraryCommand::AddLib64Paths()
165 if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
166 GetLanguageEnabled("C"))
168 return;
170 std::string voidsize =
171 this->Makefile->GetSafeDefinition("CMAKE_SIZEOF_VOID_P");
172 int size = atoi(voidsize.c_str());
173 if(size != 8)
175 return;
177 std::vector<std::string> path64;
178 bool found64 = false;
179 for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
180 i != this->SearchPaths.end(); ++i)
182 std::string s = *i;
183 std::string s2 = *i;
184 cmSystemTools::ReplaceString(s, "lib/", "lib64/");
185 // try to replace lib with lib64 and see if it is there,
186 // then prepend it to the path
187 if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
189 path64.push_back(s);
190 found64 = true;
192 // now just add a 64 to the path name and if it is there,
193 // add it to the path
194 s2 += "64";
195 if(cmSystemTools::FileIsDirectory(s2.c_str()))
197 found64 = true;
198 path64.push_back(s2);
200 // now add the original unchanged path
201 if(cmSystemTools::FileIsDirectory(i->c_str()))
203 path64.push_back(*i);
206 // now replace the SearchPaths with the 64 bit converted path
207 // if any 64 bit paths were discovered
208 if(found64)
210 this->SearchPaths = path64;
214 std::string cmFindLibraryCommand::FindLibrary(const char* name)
216 bool supportFrameworks = false;
217 bool onlyFrameworks = false;
218 std::string ff = this->Makefile->GetSafeDefinition("CMAKE_FIND_FRAMEWORK");
219 if(ff == "FIRST" || ff == "LAST")
221 supportFrameworks = true;
223 if(ff == "ONLY")
225 onlyFrameworks = true;
226 supportFrameworks = true;
229 const char* prefixes_list =
230 this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
231 const char* suffixes_list =
232 this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
233 std::vector<std::string> prefixes;
234 std::vector<std::string> suffixes;
235 cmSystemTools::ExpandListArgument(prefixes_list, prefixes, true);
236 cmSystemTools::ExpandListArgument(suffixes_list, suffixes, true);
238 // If the original library name provided by the user matches one of
239 // the suffixes, try it first.
240 bool tryOrig = false;
242 std::string nm = name;
243 for(std::vector<std::string>::const_iterator si = suffixes.begin();
244 !tryOrig && si != suffixes.end(); ++si)
246 std::string const& suffix = *si;
247 if(nm.length() > suffix.length() &&
248 nm.substr(nm.size()-suffix.length()) == suffix)
250 tryOrig = true;
255 // Add a trailing slash to all paths to aid the search process.
256 for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
257 i != this->SearchPaths.end(); ++i)
259 std::string& p = *i;
260 if(p.empty() || p[p.size()-1] != '/')
262 p += "/";
265 std::string tryPath;
266 for(std::vector<std::string>::const_iterator p = this->SearchPaths.begin();
267 p != this->SearchPaths.end(); ++p)
269 if(supportFrameworks)
271 tryPath = *p;
272 tryPath += name;
273 tryPath += ".framework";
274 if(cmSystemTools::FileExists(tryPath.c_str())
275 && cmSystemTools::FileIsDirectory(tryPath.c_str()))
277 tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
278 cmSystemTools::ConvertToUnixSlashes(tryPath);
279 return tryPath;
282 if(!onlyFrameworks)
284 // Try the original library name as specified by the user.
285 if(tryOrig)
287 tryPath = *p;
288 tryPath += name;
289 if(cmSystemTools::FileExists(tryPath.c_str(), true))
291 tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
292 cmSystemTools::ConvertToUnixSlashes(tryPath);
293 return tryPath;
297 // Try various library naming conventions.
298 for(std::vector<std::string>::iterator prefix = prefixes.begin();
299 prefix != prefixes.end(); ++prefix)
301 for(std::vector<std::string>::iterator suffix = suffixes.begin();
302 suffix != suffixes.end(); ++suffix)
304 tryPath = *p;
305 tryPath += *prefix;
306 tryPath += name;
307 tryPath += *suffix;
308 if(cmSystemTools::FileExists(tryPath.c_str())
309 && !cmSystemTools::FileIsDirectory(tryPath.c_str()))
311 tryPath = cmSystemTools::CollapseFullPath(tryPath.c_str());
312 cmSystemTools::ConvertToUnixSlashes(tryPath);
313 return tryPath;
319 // Couldn't find the library.
320 return "";