cmFindLibraryCommand.cxx 17 KB

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