cmFindLibraryCommand.cxx 17 KB

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