cmFindLibraryCommand.cxx 15 KB

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