cmFindLibraryCommand.cxx 16 KB

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