cmFindLibraryCommand.cxx 15 KB

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