cmFindLibraryCommand.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. std::string voidsize =
  156. this->Makefile->GetSafeDefinition("CMAKE_SIZEOF_VOID_P");
  157. int size = atoi(voidsize.c_str());
  158. if(size != 8)
  159. {
  160. return;
  161. }
  162. std::vector<std::string> path64;
  163. bool found64 = false;
  164. for(std::vector<std::string>::iterator i = this->SearchPaths.begin();
  165. i != this->SearchPaths.end(); ++i)
  166. {
  167. std::string s = *i;
  168. std::string s2 = *i;
  169. cmSystemTools::ReplaceString(s, "lib/", "lib64/");
  170. // try to replace lib with lib64 and see if it is there,
  171. // then prepend it to the path
  172. // Note that all paths have trailing slashes.
  173. if((s != *i) && cmSystemTools::FileIsDirectory(s.c_str()))
  174. {
  175. path64.push_back(s);
  176. found64 = true;
  177. }
  178. // now just add a 64 to the path name and if it is there,
  179. // add it to the path
  180. s2 += "64/";
  181. if(cmSystemTools::FileIsDirectory(s2.c_str()))
  182. {
  183. found64 = true;
  184. path64.push_back(s2);
  185. }
  186. // now add the original unchanged path
  187. if(cmSystemTools::FileIsDirectory(i->c_str()))
  188. {
  189. path64.push_back(*i);
  190. }
  191. }
  192. // now replace the SearchPaths with the 64 bit converted path
  193. // if any 64 bit paths were discovered
  194. if(found64)
  195. {
  196. this->SearchPaths = path64;
  197. }
  198. }
  199. //----------------------------------------------------------------------------
  200. std::string cmFindLibraryCommand::FindLibrary()
  201. {
  202. std::string library;
  203. if(this->SearchFrameworkFirst || this->SearchFrameworkOnly)
  204. {
  205. library = this->FindFrameworkLibrary();
  206. }
  207. if(library.empty() && !this->SearchFrameworkOnly)
  208. {
  209. library = this->FindNormalLibrary();
  210. }
  211. if(library.empty() && this->SearchFrameworkLast)
  212. {
  213. library = this->FindFrameworkLibrary();
  214. }
  215. return library;
  216. }
  217. //----------------------------------------------------------------------------
  218. struct cmFindLibraryHelper
  219. {
  220. cmFindLibraryHelper(cmMakefile* mf);
  221. // Context information.
  222. cmMakefile* Makefile;
  223. cmGlobalGenerator* GG;
  224. // List of valid prefixes and suffixes.
  225. std::vector<std::string> Prefixes;
  226. std::vector<std::string> Suffixes;
  227. std::string PrefixRegexStr;
  228. std::string SuffixRegexStr;
  229. // Keep track of the best library file found so far.
  230. typedef std::vector<std::string>::size_type size_type;
  231. std::string BestPath;
  232. size_type BestPrefix;
  233. size_type BestSuffix;
  234. // Support for OpenBSD shared library naming: lib<name>.so.<major>.<minor>
  235. bool OpenBSD;
  236. unsigned int BestMajor;
  237. unsigned int BestMinor;
  238. // Current name under consideration.
  239. cmsys::RegularExpression NameRegex;
  240. bool TryRawName;
  241. std::string RawName;
  242. // Current full path under consideration.
  243. std::string TestPath;
  244. void RegexFromLiteral(std::string& out, std::string const& in);
  245. void RegexFromList(std::string& out, std::vector<std::string> const& in);
  246. size_type GetPrefixIndex(std::string const& prefix)
  247. {
  248. return cmsys_stl::find(this->Prefixes.begin(), this->Prefixes.end(),
  249. prefix) - this->Prefixes.begin();
  250. }
  251. size_type GetSuffixIndex(std::string const& suffix)
  252. {
  253. return cmsys_stl::find(this->Suffixes.begin(), this->Suffixes.end(),
  254. suffix) - this->Suffixes.begin();
  255. }
  256. bool HasValidSuffix(std::string const& name);
  257. void SetName(std::string const& name);
  258. bool CheckDirectory(std::string const& path);
  259. };
  260. //----------------------------------------------------------------------------
  261. cmFindLibraryHelper::cmFindLibraryHelper(cmMakefile* mf):
  262. Makefile(mf)
  263. {
  264. this->GG = this->Makefile->GetLocalGenerator()->GetGlobalGenerator();
  265. // Collect the list of library name prefixes/suffixes to try.
  266. const char* prefixes_list =
  267. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
  268. const char* suffixes_list =
  269. this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
  270. cmSystemTools::ExpandListArgument(prefixes_list, this->Prefixes, true);
  271. cmSystemTools::ExpandListArgument(suffixes_list, this->Suffixes, true);
  272. this->RegexFromList(this->PrefixRegexStr, this->Prefixes);
  273. this->RegexFromList(this->SuffixRegexStr, this->Suffixes);
  274. // Check whether to use OpenBSD-style library version comparisons.
  275. this->OpenBSD =
  276. this->Makefile->GetCMakeInstance()
  277. ->GetPropertyAsBool("FIND_LIBRARY_USE_OPENBSD_VERSIONING");
  278. this->TryRawName = false;
  279. // No library file has yet been found.
  280. this->BestPrefix = this->Prefixes.size();
  281. this->BestSuffix = this->Suffixes.size();
  282. this->BestMajor = 0;
  283. this->BestMinor = 0;
  284. }
  285. //----------------------------------------------------------------------------
  286. void cmFindLibraryHelper::RegexFromLiteral(std::string& out,
  287. std::string const& in)
  288. {
  289. for(std::string::const_iterator ci = in.begin(); ci != in.end(); ++ci)
  290. {
  291. char ch = *ci;
  292. if(ch == '[' || ch == ']' || ch == '(' || ch == ')' || ch == '\\' ||
  293. ch == '.' || ch == '*' || ch == '+' || ch == '?' || ch == '-' ||
  294. ch == '^' || ch == '$')
  295. {
  296. out += "\\";
  297. }
  298. #if defined(_WIN32) || defined(__APPLE__)
  299. out += tolower(ch);
  300. #else
  301. out += ch;
  302. #endif
  303. }
  304. }
  305. //----------------------------------------------------------------------------
  306. void cmFindLibraryHelper::RegexFromList(std::string& out,
  307. std::vector<std::string> const& in)
  308. {
  309. // Surround the list in parens so the '|' does not apply to anything
  310. // else and the result can be checked after matching.
  311. out += "(";
  312. const char* sep = "";
  313. for(std::vector<std::string>::const_iterator si = in.begin();
  314. si != in.end(); ++si)
  315. {
  316. // Separate from previous item.
  317. out += sep;
  318. sep = "|";
  319. // Append this item.
  320. this->RegexFromLiteral(out, *si);
  321. }
  322. out += ")";
  323. }
  324. //----------------------------------------------------------------------------
  325. bool cmFindLibraryHelper::HasValidSuffix(std::string const& name)
  326. {
  327. // Check if the given name ends in a valid library suffix.
  328. for(std::vector<std::string>::const_iterator si = this->Suffixes.begin();
  329. si != this->Suffixes.end(); ++si)
  330. {
  331. std::string const& suffix = *si;
  332. if(name.length() > suffix.length() &&
  333. name.substr(name.size()-suffix.length()) == suffix)
  334. {
  335. return true;
  336. }
  337. }
  338. return false;
  339. }
  340. //----------------------------------------------------------------------------
  341. void cmFindLibraryHelper::SetName(std::string const& name)
  342. {
  343. // Consider checking the raw name too.
  344. this->TryRawName = this->HasValidSuffix(name);
  345. this->RawName = name;
  346. // Build a regular expression to match library names.
  347. std::string regex = "^";
  348. regex += this->PrefixRegexStr;
  349. this->RegexFromLiteral(regex, name);
  350. regex += this->SuffixRegexStr;
  351. if(this->OpenBSD)
  352. {
  353. regex += "(\\.[0-9]+\\.[0-9]+)?";
  354. }
  355. regex += "$";
  356. this->NameRegex.compile(regex.c_str());
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmFindLibraryHelper::CheckDirectory(std::string const& path)
  360. {
  361. // If the original library name provided by the user matches one of
  362. // the suffixes, try it first. This allows users to search
  363. // specifically for a static library on some platforms (on MS tools
  364. // one cannot tell just from the library name whether it is a static
  365. // library or an import library).
  366. if(this->TryRawName)
  367. {
  368. this->TestPath = path;
  369. this->TestPath += this->RawName;
  370. if(cmSystemTools::FileExists(this->TestPath.c_str(), true))
  371. {
  372. this->BestPath =
  373. cmSystemTools::CollapseFullPath(this->TestPath.c_str());
  374. cmSystemTools::ConvertToUnixSlashes(this->BestPath);
  375. return true;
  376. }
  377. }
  378. // Search for a file matching the library name regex.
  379. std::string dir = path;
  380. cmSystemTools::ConvertToUnixSlashes(dir);
  381. std::set<cmStdString> const& files = this->GG->GetDirectoryContent(dir);
  382. for(std::set<cmStdString>::const_iterator fi = files.begin();
  383. fi != files.end(); ++fi)
  384. {
  385. std::string const& origName = *fi;
  386. #if defined(_WIN32) || defined(__APPLE__)
  387. std::string testName = cmSystemTools::LowerCase(origName);
  388. #else
  389. std::string const& testName = origName;
  390. #endif
  391. if(this->NameRegex.find(testName))
  392. {
  393. this->TestPath = path;
  394. this->TestPath += origName;
  395. if(!cmSystemTools::FileIsDirectory(this->TestPath.c_str()))
  396. {
  397. // This is a matching file. Check if it is better than the
  398. // best name found so far. Earlier prefixes are preferred,
  399. // followed by earlier suffixes. For OpenBSD, shared library
  400. // version extensions are compared.
  401. size_type prefix = this->GetPrefixIndex(this->NameRegex.match(1));
  402. size_type suffix = this->GetSuffixIndex(this->NameRegex.match(2));
  403. unsigned int major = 0;
  404. unsigned int minor = 0;
  405. if(this->OpenBSD)
  406. {
  407. sscanf(this->NameRegex.match(3).c_str(), ".%u.%u", &major, &minor);
  408. }
  409. if(this->BestPath.empty() || prefix < this->BestPrefix ||
  410. (prefix == this->BestPrefix && suffix < this->BestSuffix) ||
  411. (prefix == this->BestPrefix && suffix == this->BestSuffix &&
  412. (major > this->BestMajor ||
  413. (major == this->BestMajor && minor > this->BestMinor))))
  414. {
  415. this->BestPath = this->TestPath;
  416. this->BestPrefix = prefix;
  417. this->BestSuffix = suffix;
  418. this->BestMajor = major;
  419. this->BestMinor = minor;
  420. }
  421. }
  422. }
  423. }
  424. // Use the best candidate found in this directory, if any.
  425. return !this->BestPath.empty();
  426. }
  427. //----------------------------------------------------------------------------
  428. std::string cmFindLibraryCommand::FindNormalLibrary()
  429. {
  430. // Search the entire path for each name.
  431. cmFindLibraryHelper helper(this->Makefile);
  432. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  433. ni != this->Names.end() ; ++ni)
  434. {
  435. // Switch to searching for this name.
  436. std::string const& name = *ni;
  437. helper.SetName(name);
  438. // Search every directory.
  439. for(std::vector<std::string>::const_iterator
  440. p = this->SearchPaths.begin();
  441. p != this->SearchPaths.end(); ++p)
  442. {
  443. if(helper.CheckDirectory(*p))
  444. {
  445. return helper.BestPath;
  446. }
  447. }
  448. }
  449. // Couldn't find the library.
  450. return "";
  451. }
  452. //----------------------------------------------------------------------------
  453. std::string cmFindLibraryCommand::FindFrameworkLibrary()
  454. {
  455. // Search for a framework of each name in the entire search path.
  456. for(std::vector<std::string>::const_iterator ni = this->Names.begin();
  457. ni != this->Names.end() ; ++ni)
  458. {
  459. // Search the paths for a framework with this name.
  460. std::string fwName = *ni;
  461. fwName += ".framework";
  462. std::string fwPath = cmSystemTools::FindDirectory(fwName.c_str(),
  463. this->SearchPaths,
  464. true);
  465. if(!fwPath.empty())
  466. {
  467. return fwPath;
  468. }
  469. }
  470. // No framework found.
  471. return "";
  472. }