cmFindLibraryCommand.cxx 16 KB

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