cmFindLibraryCommand.cxx 17 KB

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