cmFindLibraryCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #include <cmsys/Directory.hxx>
  16. #include <cmsys/stl/algorithm>
  17. cmFindLibraryCommand::cmFindLibraryCommand()
  18. {
  19. cmSystemTools::ReplaceString(this->GenericDocumentation,
  20. "FIND_XXX", "find_library");
  21. cmSystemTools::ReplaceString(this->GenericDocumentation,
  22. "CMAKE_XXX_PATH", "CMAKE_LIBRARY_PATH");
  23. cmSystemTools::ReplaceString(this->GenericDocumentation,
  24. "CMAKE_XXX_MAC_PATH",
  25. "CMAKE_FRAMEWORK_PATH");
  26. cmSystemTools::ReplaceString(this->GenericDocumentation,
  27. "CMAKE_SYSTEM_XXX_MAC_PATH",
  28. "CMAKE_SYSTEM_FRAMEWORK_PATH");
  29. cmSystemTools::ReplaceString(this->GenericDocumentation,
  30. "XXX_SYSTEM", "LIB");
  31. cmSystemTools::ReplaceString(this->GenericDocumentation,
  32. "CMAKE_SYSTEM_XXX_PATH",
  33. "CMAKE_SYSTEM_LIBRARY_PATH");
  34. cmSystemTools::ReplaceString(this->GenericDocumentation,
  35. "SEARCH_XXX_DESC", "library");
  36. cmSystemTools::ReplaceString(this->GenericDocumentation,
  37. "SEARCH_XXX", "library");
  38. cmSystemTools::ReplaceString(this->GenericDocumentation,
  39. "XXX_SUBDIR", "lib");
  40. cmSystemTools::ReplaceString(this->GenericDocumentation,
  41. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  42. "CMAKE_FIND_ROOT_PATH_MODE_LIBRARY");
  43. this->EnvironmentPath = "LIB";
  44. this->GenericDocumentation +=
  45. "\n"
  46. "If the library found is a framework, then VAR will be set to "
  47. "the full path to the framework <fullPath>/A.framework. "
  48. "When a full path to a framework is used as a library, "
  49. "CMake will use a -framework A, and a -F<fullPath> to "
  50. "link the framework to the target. ";
  51. }
  52. // cmFindLibraryCommand
  53. bool cmFindLibraryCommand
  54. ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
  55. {
  56. this->VariableDocumentation = "Path to a library.";
  57. this->CMakePathName = "LIBRARY";
  58. if(!this->ParseArguments(argsIn))
  59. {
  60. return false;
  61. }
  62. if(this->AlreadyInCache)
  63. {
  64. // If the user specifies the entry on the command line without a
  65. // type we should add the type and docstring but keep the original
  66. // value.
  67. if(this->AlreadyInCacheWithoutMetaInfo)
  68. {
  69. this->Makefile->AddCacheDefinition(this->VariableName.c_str(), "",
  70. this->VariableDocumentation.c_str(),
  71. cmCacheManager::FILEPATH);
  72. }
  73. return true;
  74. }
  75. if(const char* abi_name =
  76. this->Makefile->GetDefinition("CMAKE_INTERNAL_PLATFORM_ABI"))
  77. {
  78. std::string abi = abi_name;
  79. if(abi.find("ELF N32") != abi.npos)
  80. {
  81. // Convert lib to lib32.
  82. this->AddArchitecturePaths("32");
  83. }
  84. }
  85. if(this->Makefile->GetCMakeInstance()
  86. ->GetPropertyAsBool("FIND_LIBRARY_USE_LIB64_PATHS"))
  87. {
  88. // add special 64 bit paths if this is a 64 bit compile.
  89. this->AddLib64Paths();
  90. }
  91. std::string library = this->FindLibrary();
  92. if(library != "")
  93. {
  94. // Save the value in the cache
  95. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  96. library.c_str(),
  97. this->VariableDocumentation.c_str(),
  98. cmCacheManager::FILEPATH);
  99. return true;
  100. }
  101. std::string notfound = this->VariableName + "-NOTFOUND";
  102. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  103. notfound.c_str(),
  104. this->VariableDocumentation.c_str(),
  105. cmCacheManager::FILEPATH);
  106. return true;
  107. }
  108. //----------------------------------------------------------------------------
  109. void cmFindLibraryCommand::AddArchitecturePaths(const char* suffix)
  110. {
  111. std::vector<std::string> newPaths;
  112. bool found = false;
  113. std::string subpath = "lib";
  114. subpath += suffix;
  115. subpath += "/";
  116. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  117. i != this->SearchPaths.end(); ++i)
  118. {
  119. // Try replacing lib/ with lib<suffix>/
  120. std::string s = *i;
  121. cmSystemTools::ReplaceString(s, "lib/", subpath.c_str());
  122. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  123. {
  124. found = true;
  125. newPaths.push_back(s);
  126. }
  127. // Now look for lib<suffix>
  128. s = *i;
  129. s += suffix;
  130. if(cmSystemTools::FileIsDirectory(s.c_str()))
  131. {
  132. found = true;
  133. newPaths.push_back(s);
  134. }
  135. // now add the original unchanged path
  136. if(cmSystemTools::FileIsDirectory(i->c_str()))
  137. {
  138. newPaths.push_back(*i);
  139. }
  140. }
  141. // If any new paths were found replace the original set.
  142. if(found)
  143. {
  144. this->SearchPaths = newPaths;
  145. }
  146. }
  147. void cmFindLibraryCommand::AddLib64Paths()
  148. {
  149. if(!this->Makefile->GetLocalGenerator()->GetGlobalGenerator()->
  150. GetLanguageEnabled("C"))
  151. {
  152. return;
  153. }
  154. std::string voidsize =
  155. this->Makefile->GetSafeDefinition("CMAKE_SIZEOF_VOID_P");
  156. int size = atoi(voidsize.c_str());
  157. if(size != 8)
  158. {
  159. return;
  160. }
  161. std::vector<std::string> path64;
  162. bool found64 = false;
  163. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  164. i != this->SearchPaths.end(); ++i)
  165. {
  166. std::string s = *i;
  167. std::string s2 = *i;
  168. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  169. // try to replace lib with lib64 and see if it is there,
  170. // then prepend it to the path
  171. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  172. {
  173. path64.push_back(s);
  174. found64 = true;
  175. }
  176. // now just add a 64 to the path name and if it is there,
  177. // add it to the path
  178. s2 += "64";
  179. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  180. {
  181. found64 = true;
  182. path64.push_back(s2);
  183. }
  184. // now add the original unchanged path
  185. if(cmSystemTools::FileIsDirectory(i->c_str()))
  186. {
  187. path64.push_back(*i);
  188. }
  189. }
  190. // now replace the SearchPaths with the 64 bit converted path
  191. // if any 64 bit paths were discovered
  192. if(found64)
  193. {
  194. this->SearchPaths = path64;
  195. }
  196. }
  197. //----------------------------------------------------------------------------
  198. std::string cmFindLibraryCommand::FindLibrary()
  199. {
  200. std::string library;
  201. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  202. {
  203. library = this->FindFrameworkLibrary();
  204. }
  205. if(library.empty() && !this->SearchFrameworkOnly)
  206. {
  207. library = this->FindNormalLibrary();
  208. }
  209. if(library.empty() && this->SearchFrameworkLast)
  210. {
  211. library = this->FindFrameworkLibrary();
  212. }
  213. return library;
  214. }
  215. //----------------------------------------------------------------------------
  216. struct cmFindLibraryHelper
  217. {
  218. cmFindLibraryHelper(cmMakefile* mf);
  219. // Context information.
  220. cmMakefile* Makefile;
  221. cmGlobalGenerator* GG;
  222. // List of valid prefixes and suffixes.
  223. std::vector<std::string> Prefixes;
  224. std::vector<std::string> Suffixes;
  225. std::string PrefixRegexStr;
  226. std::string SuffixRegexStr;
  227. // Keep track of the best library file found so far.
  228. typedef std::vector<std::string>::size_type size_type;
  229. std::string BestPath;
  230. size_type BestPrefix;
  231. size_type BestSuffix;
  232. // Support for OpenBSD shared library naming: lib<name>.so.<major>.<minor>
  233. bool OpenBSD;
  234. unsigned int BestMajor;
  235. unsigned int BestMinor;
  236. // Current name under consideration.
  237. cmsys::RegularExpression NameRegex;
  238. bool TryRawName;
  239. std::string RawName;
  240. // Current full path under consideration.
  241. std::string TestPath;
  242. void RegexFromLiteral(std::string& out, std::string const& in);
  243. void RegexFromList(std::string& out, std::vector<std::string> const& in);
  244. size_type GetPrefixIndex(std::string const& prefix)
  245. {
  246. return cmsys_stl::find(this->Prefixes.begin(), this->Prefixes.end(),
  247. prefix) - this->Prefixes.begin();
  248. }
  249. size_type GetSuffixIndex(std::string const& suffix)
  250. {
  251. return cmsys_stl::find(this->Suffixes.begin(), this->Suffixes.end(),
  252. suffix) - this->Suffixes.begin();
  253. }
  254. bool HasValidSuffix(std::string const& name);
  255. void SetName(std::string const& name);
  256. bool CheckDirectory(std::string const& path);
  257. };
  258. //----------------------------------------------------------------------------
  259. cmFindLibraryHelper::cmFindLibraryHelper(cmMakefile* mf):
  260. Makefile(mf)
  261. {
  262. this->GG = this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  263. // Collect the list of library name prefixes/suffixes to try.
  264. const char* prefixes_list =
  265. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  266. const char* suffixes_list =
  267. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  268. cmSystemTools::ExpandListArgument(prefixes_list, this->Prefixes, true);
  269. cmSystemTools::ExpandListArgument(suffixes_list, this->Suffixes, true);
  270. this->RegexFromList(this->PrefixRegexStr, this->Prefixes);
  271. this->RegexFromList(this->SuffixRegexStr, this->Suffixes);
  272. // Check whether to use OpenBSD-style library version comparisons.
  273. this->OpenBSD =
  274. this->Makefile->GetCMakeInstance()
  275. ->GetPropertyAsBool("FIND_LIBRARY_USE_OPENBSD_VERSIONING");
  276. this->TryRawName = false;
  277. // No library file has yet been found.
  278. this->BestPrefix = this->Prefixes.size();
  279. this->BestSuffix = this->Suffixes.size();
  280. this->BestMajor = 0;
  281. this->BestMinor = 0;
  282. }
  283. //----------------------------------------------------------------------------
  284. void cmFindLibraryHelper::RegexFromLiteral(std::string& out,
  285. std::string const& in)
  286. {
  287. for(std::string::const_iterator ci = in.begin(); ci != in.end(); ++ci)
  288. {
  289. char ch = *ci;
  290. if(ch == '[' || ch == ']' || ch == '(' || ch == ')' || ch == '\\' ||
  291. ch == '.' || ch == '*' || ch == '+' || ch == '?' || ch == '-' ||
  292. ch == '^' || ch == '$')
  293. {
  294. out += "\\";
  295. }
  296. #if defined(_WIN32) || defined(__APPLE__)
  297. out += tolower(ch);
  298. #else
  299. out += ch;
  300. #endif
  301. }
  302. }
  303. //----------------------------------------------------------------------------
  304. void cmFindLibraryHelper::RegexFromList(std::string& out,
  305. std::vector<std::string> const& in)
  306. {
  307. // Surround the list in parens so the '|' does not apply to anything
  308. // else and the result can be checked after matching.
  309. out += "(";
  310. const char* sep = "";
  311. for(std::vector<std::string>::const_iterator si = in.begin();
  312. si != in.end(); ++si)
  313. {
  314. // Separate from previous item.
  315. out += sep;
  316. sep = "|";
  317. // Append this item.
  318. this->RegexFromLiteral(out, *si);
  319. }
  320. out += ")";
  321. }
  322. //----------------------------------------------------------------------------
  323. bool cmFindLibraryHelper::HasValidSuffix(std::string const& name)
  324. {
  325. // Check if the given name ends in a valid library suffix.
  326. for(std::vector<std::string>::const_iterator si = this->Suffixes.begin();
  327. si != this->Suffixes.end(); ++si)
  328. {
  329. std::string const& suffix = *si;
  330. if(name.length() > suffix.length() &&
  331. name.substr(name.size()-suffix.length()) == suffix)
  332. {
  333. return true;
  334. }
  335. }
  336. return false;
  337. }
  338. //----------------------------------------------------------------------------
  339. void cmFindLibraryHelper::SetName(std::string const& name)
  340. {
  341. // Consider checking the raw name too.
  342. this->TryRawName = this->HasValidSuffix(name);
  343. this->RawName = name;
  344. // Build a regular expression to match library names.
  345. std::string regex = "^";
  346. regex += this->PrefixRegexStr;
  347. this->RegexFromLiteral(regex, name);
  348. regex += this->SuffixRegexStr;
  349. if(this->OpenBSD)
  350. {
  351. regex += "(\\.[0-9]+\\.[0-9]+)?";
  352. }
  353. regex += "$";
  354. this->NameRegex.compile(regex.c_str());
  355. }
  356. //----------------------------------------------------------------------------
  357. bool cmFindLibraryHelper::CheckDirectory(std::string const& path)
  358. {
  359. // If the original library name provided by the user matches one of
  360. // the suffixes, try it first. This allows users to search
  361. // specifically for a static library on some platforms (on MS tools
  362. // one cannot tell just from the library name whether it is a static
  363. // library or an import library).
  364. if(this->TryRawName)
  365. {
  366. this->TestPath = path;
  367. this->TestPath += this->RawName;
  368. if(cmSystemTools::FileExists(this->TestPath.c_str(), true))
  369. {
  370. this->BestPath =
  371. cmSystemTools::CollapseFullPath(this->TestPath.c_str());
  372. cmSystemTools::ConvertToUnixSlashes(this->BestPath);
  373. return true;
  374. }
  375. }
  376. // Search for a file matching the library name regex.
  377. std::string dir = path;
  378. cmSystemTools::ConvertToUnixSlashes(dir);
  379. std::set<cmStdString> const& files = this->GG->GetDirectoryContent(dir);
  380. for(std::set<cmStdString>::const_iterator fi = files.begin();
  381. fi != files.end(); ++fi)
  382. {
  383. std::string const& origName = *fi;
  384. #if defined(_WIN32) || defined(__APPLE__)
  385. std::string testName = cmSystemTools::LowerCase(origName);
  386. #else
  387. std::string const& testName = origName;
  388. #endif
  389. if(this->NameRegex.find(testName))
  390. {
  391. this->TestPath = path;
  392. this->TestPath += origName;
  393. if(!cmSystemTools::FileIsDirectory(this->TestPath.c_str()))
  394. {
  395. // This is a matching file. Check if it is better than the
  396. // best name found so far. Earlier prefixes are preferred,
  397. // followed by earlier suffixes. For OpenBSD, shared library
  398. // version extensions are compared.
  399. size_type prefix = this->GetPrefixIndex(this->NameRegex.match(1));
  400. size_type suffix = this->GetSuffixIndex(this->NameRegex.match(2));
  401. unsigned int major = 0;
  402. unsigned int minor = 0;
  403. if(this->OpenBSD)
  404. {
  405. sscanf(this->NameRegex.match(3).c_str(), ".%u.%u", &major, &minor);
  406. }
  407. if(this->BestPath.empty() || prefix < this->BestPrefix ||
  408. (prefix == this->BestPrefix && suffix < this->BestSuffix) ||
  409. (prefix == this->BestPrefix && suffix == this->BestSuffix &&
  410. (major > this->BestMajor ||
  411. (major == this->BestMajor && minor > this->BestMinor))))
  412. {
  413. this->BestPath = this->TestPath;
  414. this->BestPrefix = prefix;
  415. this->BestSuffix = suffix;
  416. this->BestMajor = major;
  417. this->BestMinor = minor;
  418. }
  419. }
  420. }
  421. }
  422. // Use the best candidate found in this directory, if any.
  423. return !this->BestPath.empty();
  424. }
  425. //----------------------------------------------------------------------------
  426. std::string cmFindLibraryCommand::FindNormalLibrary()
  427. {
  428. // Search the entire path for each name.
  429. cmFindLibraryHelper helper(this->Makefile);
  430. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  431. ni != this->Names.end() ; ++ni)
  432. {
  433. // Switch to searching for this name.
  434. std::string const& name = *ni;
  435. helper.SetName(name);
  436. // Search every directory.
  437. for(std::vector<std::string>::const_iterator
  438. p = this->SearchPaths.begin();
  439. p != this->SearchPaths.end(); ++p)
  440. {
  441. if(helper.CheckDirectory(*p))
  442. {
  443. return helper.BestPath;
  444. }
  445. }
  446. }
  447. // Couldn't find the library.
  448. return "";
  449. }
  450. //----------------------------------------------------------------------------
  451. std::string cmFindLibraryCommand::FindFrameworkLibrary()
  452. {
  453. // Search for a framework of each name in the entire search path.
  454. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  455. ni != this->Names.end() ; ++ni)
  456. {
  457. // Search the paths for a framework with this name.
  458. std::string fwName = *ni;
  459. fwName += ".framework";
  460. std::string fwPath = cmSystemTools::FindDirectory(fwName.c_str(),
  461. this->SearchPaths,
  462. true);
  463. if(!fwPath.empty())
  464. {
  465. return fwPath;
  466. }
  467. }
  468. // No framework found.
  469. return "";
  470. }