cmFindLibraryCommand.cxx 17 KB

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