cmFindLibraryCommand.cxx 17 KB

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