cmFindLibraryCommand.cxx 14 KB

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