1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFindLibraryCommand.h"
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
#include <utility>
#include "cmsys/RegularExpression.hxx"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmProperty.h"
#include "cmState.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
cmFindLibraryCommand::cmFindLibraryCommand(cmExecutionStatus& status)
: cmFindBase("find_library", status)
{
this->EnvironmentPath = "LIB";
this->NamesPerDirAllowed = true;
this->VariableDocumentation = "Path to a library.";
this->VariableType = cmStateEnums::FILEPATH;
}
// cmFindLibraryCommand
bool cmFindLibraryCommand::InitialPass(std::vector<std::string> const& argsIn)
{
this->DebugMode = this->ComputeIfDebugModeWanted();
this->CMakePathName = "LIBRARY";
if (!this->ParseArguments(argsIn)) {
return false;
}
if (this->AlreadyDefined) {
this->NormalizeFindResult();
return true;
}
// add custom lib<qual> paths instead of using fixed lib32, lib64 or
// libx32
if (cmProp customLib = this->Makefile->GetDefinition(
"CMAKE_FIND_LIBRARY_CUSTOM_LIB_SUFFIX")) {
this->AddArchitecturePaths(customLib->c_str());
}
// add special 32 bit paths if this is a 32 bit compile.
else if (this->Makefile->PlatformIs32Bit() &&
this->Makefile->GetState()->GetGlobalPropertyAsBool(
"FIND_LIBRARY_USE_LIB32_PATHS")) {
this->AddArchitecturePaths("32");
}
// add special 64 bit paths if this is a 64 bit compile.
else if (this->Makefile->PlatformIs64Bit() &&
this->Makefile->GetState()->GetGlobalPropertyAsBool(
"FIND_LIBRARY_USE_LIB64_PATHS")) {
this->AddArchitecturePaths("64");
}
// add special 32 bit paths if this is an x32 compile.
else if (this->Makefile->PlatformIsx32() &&
this->Makefile->GetState()->GetGlobalPropertyAsBool(
"FIND_LIBRARY_USE_LIBX32_PATHS")) {
this->AddArchitecturePaths("x32");
}
std::string const library = this->FindLibrary();
this->StoreFindResult(library);
return true;
}
void cmFindLibraryCommand::AddArchitecturePaths(const char* suffix)
{
std::vector<std::string> original;
original.swap(this->SearchPaths);
for (std::string const& o : original) {
this->AddArchitecturePath(o, 0, suffix);
if (this->DebugMode) {
std::string msg = cmStrCat(
"find_library(", this->VariableName, ") removed original suffix ", o,
" from PATH_SUFFIXES while adding architecture paths for suffix '",
suffix, "'");
this->DebugMessage(msg);
}
}
}
static bool cmLibDirsLinked(std::string const& l, std::string const& r)
{
// Compare the real paths of the two directories.
// Since our caller only changed the trailing component of each
// directory, the real paths can be the same only if at least one of
// the trailing components is a symlink. Use this as an optimization
// to avoid excessive realpath calls.
return (cmSystemTools::FileIsSymlink(l) ||
cmSystemTools::FileIsSymlink(r)) &&
cmSystemTools::GetRealPath(l) == cmSystemTools::GetRealPath(r);
}
void cmFindLibraryCommand::AddArchitecturePath(
std::string const& dir, std::string::size_type start_pos, const char* suffix,
bool fresh)
{
std::string::size_type pos = dir.find("lib/", start_pos);
if (pos != std::string::npos) {
// Check for "lib".
std::string lib = dir.substr(0, pos + 3);
bool use_lib = cmSystemTools::FileIsDirectory(lib);
// Check for "lib<suffix>" and use it first.
std::string libX = lib + suffix;
bool use_libX = cmSystemTools::FileIsDirectory(libX);
// Avoid copies of the same directory due to symlinks.
if (use_libX && use_lib && cmLibDirsLinked(libX, lib)) {
use_libX = false;
}
if (use_libX) {
libX += dir.substr(pos + 3);
std::string::size_type libX_pos = pos + 3 + strlen(suffix) + 1;
this->AddArchitecturePath(libX, libX_pos, suffix);
}
if (use_lib) {
this->AddArchitecturePath(dir, pos + 3 + 1, suffix, false);
}
}
if (fresh) {
// Check for the original unchanged path.
bool use_dir = cmSystemTools::FileIsDirectory(dir);
// Check for <dir><suffix>/ and use it first.
std::string dirX = dir + suffix;
bool use_dirX = cmSystemTools::FileIsDirectory(dirX);
// Avoid copies of the same directory due to symlinks.
if (use_dirX && use_dir && cmLibDirsLinked(dirX, dir)) {
use_dirX = false;
}
if (use_dirX) {
dirX += "/";
if (this->DebugMode) {
std::string msg = cmStrCat(
"find_library(", this->VariableName, ") added replacement path ",
dirX, " to PATH_SUFFIXES for architecture suffix '", suffix, "'");
this->DebugMessage(msg);
}
this->SearchPaths.push_back(std::move(dirX));
}
if (use_dir) {
this->SearchPaths.push_back(dir);
if (this->DebugMode) {
std::string msg = cmStrCat(
"find_library(", this->VariableName, ") added replacement path ",
dir, " to PATH_SUFFIXES for architecture suffix '", suffix, "'");
this->DebugMessage(msg);
}
}
}
}
std::string cmFindLibraryCommand::FindLibrary()
{
std::string library;
if (this->SearchFrameworkFirst || this->SearchFrameworkOnly) {
library = this->FindFrameworkLibrary();
}
if (library.empty() && !this->SearchFrameworkOnly) {
library = this->FindNormalLibrary();
}
if (library.empty() && this->SearchFrameworkLast) {
library = this->FindFrameworkLibrary();
}
return library;
}
struct cmFindLibraryHelper
{
cmFindLibraryHelper(std::string debugName, cmMakefile* mf,
cmFindBase const* findBase);
// Context information.
cmMakefile* Makefile;
cmGlobalGenerator* GG;
// List of valid prefixes and suffixes.
std::vector<std::string> Prefixes;
std::vector<std::string> Suffixes;
std::string PrefixRegexStr;
std::string SuffixRegexStr;
// Keep track of the best library file found so far.
using size_type = std::vector<std::string>::size_type;
std::string BestPath;
// Support for OpenBSD shared library naming: lib<name>.so.<major>.<minor>
bool OpenBSD;
bool DebugMode;
// Current names under consideration.
struct Name
{
bool TryRaw = false;
std::string Raw;
cmsys::RegularExpression Regex;
};
std::vector<Name> Names;
// Current full path under consideration.
std::string TestPath;
void RegexFromLiteral(std::string& out, std::string const& in);
void RegexFromList(std::string& out, std::vector<std::string> const& in);
size_type GetPrefixIndex(std::string const& prefix)
{
return std::find(this->Prefixes.begin(), this->Prefixes.end(), prefix) -
this->Prefixes.begin();
}
size_type GetSuffixIndex(std::string const& suffix)
{
return std::find(this->Suffixes.begin(), this->Suffixes.end(), suffix) -
this->Suffixes.begin();
}
bool HasValidSuffix(std::string const& name);
void AddName(std::string const& name);
void SetName(std::string const& name);
bool CheckDirectory(std::string const& path);
bool CheckDirectoryForName(std::string const& path, Name& name);
cmFindBaseDebugState DebugSearches;
void DebugLibraryFailed(std::string const& name, std::string const& path)
{
if (this->DebugMode) {
auto regexName =
cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr);
this->DebugSearches.FailedAt(path, regexName);
}
};
void DebugLibraryFound(std::string const& name, std::string const& path)
{
if (this->DebugMode) {
auto regexName =
cmStrCat(this->PrefixRegexStr, name, this->SuffixRegexStr);
this->DebugSearches.FoundAt(path, regexName);
}
};
};
cmFindLibraryHelper::cmFindLibraryHelper(std::string debugName, cmMakefile* mf,
cmFindBase const* base)
: Makefile(mf)
, DebugMode(base->DebugModeEnabled())
, DebugSearches(std::move(debugName), base)
{
this->GG = this->Makefile->GetGlobalGenerator();
// Collect the list of library name prefixes/suffixes to try.
std::string const& prefixes_list =
this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_PREFIXES");
std::string const& suffixes_list =
this->Makefile->GetRequiredDefinition("CMAKE_FIND_LIBRARY_SUFFIXES");
cmExpandList(prefixes_list, this->Prefixes, true);
cmExpandList(suffixes_list, this->Suffixes, true);
this->RegexFromList(this->PrefixRegexStr, this->Prefixes);
this->RegexFromList(this->SuffixRegexStr, this->Suffixes);
// Check whether to use OpenBSD-style library version comparisons.
this->OpenBSD = this->Makefile->GetState()->GetGlobalPropertyAsBool(
"FIND_LIBRARY_USE_OPENBSD_VERSIONING");
}
void cmFindLibraryHelper::RegexFromLiteral(std::string& out,
std::string const& in)
{
for (char ch : in) {
if (ch == '[' || ch == ']' || ch == '(' || ch == ')' || ch == '\\' ||
ch == '.' || ch == '*' || ch == '+' || ch == '?' || ch == '-' ||
ch == '^' || ch == '$') {
out += "\\";
}
#if defined(_WIN32) || defined(__APPLE__)
out += static_cast<char>(tolower(ch));
#else
out += ch;
#endif
}
}
void cmFindLibraryHelper::RegexFromList(std::string& out,
std::vector<std::string> const& in)
{
// Surround the list in parens so the '|' does not apply to anything
// else and the result can be checked after matching.
out += "(";
const char* sep = "";
for (std::string const& s : in) {
// Separate from previous item.
out += sep;
sep = "|";
// Append this item.
this->RegexFromLiteral(out, s);
}
out += ")";
}
bool cmFindLibraryHelper::HasValidSuffix(std::string const& name)
{
for (std::string suffix : this->Suffixes) {
if (name.length() <= suffix.length()) {
continue;
}
// Check if the given name ends in a valid library suffix.
if (name.substr(name.size() - suffix.length()) == suffix) {
return true;
}
// Check if a valid library suffix is somewhere in the name,
// this may happen e.g. for versioned shared libraries: libfoo.so.2
suffix += ".";
if (name.find(suffix) != std::string::npos) {
return true;
}
}
return false;
}
void cmFindLibraryHelper::AddName(std::string const& name)
{
Name entry;
// Consider checking the raw name too.
entry.TryRaw = this->HasValidSuffix(name);
entry.Raw = name;
// Build a regular expression to match library names.
std::string regex = cmStrCat('^', this->PrefixRegexStr);
this->RegexFromLiteral(regex, name);
regex += this->SuffixRegexStr;
if (this->OpenBSD) {
regex += "(\\.[0-9]+\\.[0-9]+)?";
}
regex += "$";
entry.Regex.compile(regex);
this->Names.push_back(std::move(entry));
}
void cmFindLibraryHelper::SetName(std::string const& name)
{
this->Names.clear();
this->AddName(name);
}
bool cmFindLibraryHelper::CheckDirectory(std::string const& path)
{
for (Name& i : this->Names) {
if (this->CheckDirectoryForName(path, i)) {
return true;
}
}
return false;
}
bool cmFindLibraryHelper::CheckDirectoryForName(std::string const& path,
Name& name)
{
// If the original library name provided by the user matches one of
// the suffixes, try it first. This allows users to search
// specifically for a static library on some platforms (on MS tools
// one cannot tell just from the library name whether it is a static
// library or an import library).
if (name.TryRaw) {
this->TestPath = cmStrCat(path, name.Raw);
const bool exists = cmSystemTools::FileExists(this->TestPath, true);
if (!exists) {
this->DebugLibraryFailed(name.Raw, path);
} else {
this->DebugLibraryFound(name.Raw, path);
this->BestPath = cmSystemTools::CollapseFullPath(this->TestPath);
cmSystemTools::ConvertToUnixSlashes(this->BestPath);
return true;
}
}
// No library file has yet been found.
size_type bestPrefix = this->Prefixes.size();
size_type bestSuffix = this->Suffixes.size();
unsigned int bestMajor = 0;
unsigned int bestMinor = 0;
// Search for a file matching the library name regex.
std::string dir = path;
cmSystemTools::ConvertToUnixSlashes(dir);
std::set<std::string> const& files = this->GG->GetDirectoryContent(dir);
for (std::string const& origName : files) {
#if defined(_WIN32) || defined(__APPLE__)
std::string testName = cmSystemTools::LowerCase(origName);
#else
std::string const& testName = origName;
#endif
if (name.Regex.find(testName)) {
this->TestPath = cmStrCat(path, origName);
// Make sure the path is readable and is not a directory.
if (cmSystemTools::FileExists(this->TestPath, true)) {
this->DebugLibraryFound(name.Raw, dir);
// This is a matching file. Check if it is better than the
// best name found so far. Earlier prefixes are preferred,
// followed by earlier suffixes. For OpenBSD, shared library
// version extensions are compared.
size_type prefix = this->GetPrefixIndex(name.Regex.match(1));
size_type suffix = this->GetSuffixIndex(name.Regex.match(2));
unsigned int major = 0;
unsigned int minor = 0;
if (this->OpenBSD) {
sscanf(name.Regex.match(3).c_str(), ".%u.%u", &major, &minor);
}
if (this->BestPath.empty() || prefix < bestPrefix ||
(prefix == bestPrefix && suffix < bestSuffix) ||
(prefix == bestPrefix && suffix == bestSuffix &&
(major > bestMajor ||
(major == bestMajor && minor > bestMinor)))) {
this->BestPath = this->TestPath;
bestPrefix = prefix;
bestSuffix = suffix;
bestMajor = major;
bestMinor = minor;
}
}
}
}
if (this->BestPath.empty()) {
this->DebugLibraryFailed(name.Raw, dir);
} else {
this->DebugLibraryFound(name.Raw, this->BestPath);
}
// Use the best candidate found in this directory, if any.
return !this->BestPath.empty();
}
std::string cmFindLibraryCommand::FindNormalLibrary()
{
if (this->NamesPerDir) {
return this->FindNormalLibraryNamesPerDir();
}
return this->FindNormalLibraryDirsPerName();
}
std::string cmFindLibraryCommand::FindNormalLibraryNamesPerDir()
{
// Search for all names in each directory.
cmFindLibraryHelper helper(this->FindCommandName, this->Makefile, this);
for (std::string const& n : this->Names) {
helper.AddName(n);
}
// Search every directory.
for (std::string const& sp : this->SearchPaths) {
if (helper.CheckDirectory(sp)) {
return helper.BestPath;
}
}
// Couldn't find the library.
return "";
}
std::string cmFindLibraryCommand::FindNormalLibraryDirsPerName()
{
// Search the entire path for each name.
cmFindLibraryHelper helper(this->FindCommandName, this->Makefile, this);
for (std::string const& n : this->Names) {
// Switch to searching for this name.
helper.SetName(n);
// Search every directory.
for (std::string const& sp : this->SearchPaths) {
if (helper.CheckDirectory(sp)) {
return helper.BestPath;
}
}
}
// Couldn't find the library.
return "";
}
std::string cmFindLibraryCommand::FindFrameworkLibrary()
{
if (this->NamesPerDir) {
return this->FindFrameworkLibraryNamesPerDir();
}
return this->FindFrameworkLibraryDirsPerName();
}
std::string cmFindLibraryCommand::FindFrameworkLibraryNamesPerDir()
{
std::string fwPath;
// Search for all names in each search path.
for (std::string const& d : this->SearchPaths) {
for (std::string const& n : this->Names) {
fwPath = cmStrCat(d, n, ".framework");
if (cmSystemTools::FileIsDirectory(fwPath)) {
return cmSystemTools::CollapseFullPath(fwPath);
}
}
}
// No framework found.
return "";
}
std::string cmFindLibraryCommand::FindFrameworkLibraryDirsPerName()
{
std::string fwPath;
// Search for each name in all search paths.
for (std::string const& n : this->Names) {
for (std::string const& d : this->SearchPaths) {
fwPath = cmStrCat(d, n, ".framework");
if (cmSystemTools::FileIsDirectory(fwPath)) {
return cmSystemTools::CollapseFullPath(fwPath);
}
}
}
// No framework found.
return "";
}
bool cmFindLibrary(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
return cmFindLibraryCommand(status).InitialPass(args);
}
|