cmFindLibraryCommand.cxx 16 KB

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