Determine location of libc dynamically in wxDynamicLibrary test

Don't hard code the path to it, even different Linux versions use
different paths, e.g. /lib/x86_64-linux-gnu under Debian and /lib64
under Fedora, so just try all the possibilities until we find something.
This commit is contained in:
Vadim Zeitlin 2022-08-04 01:07:35 +02:00
parent f27acce6cd
commit 512b8033fe

View file

@ -28,42 +28,67 @@ TEST_CASE("DynamicLibrary::Load", "[dynlib]")
#if defined(__WINDOWS__) #if defined(__WINDOWS__)
static const char* const LIB_NAME = "kernel32.dll"; static const char* const LIB_NAME = "kernel32.dll";
static const char* const FUNC_NAME = "lstrlenA"; static const char* const FUNC_NAME = "lstrlenA";
#else // !__WINDOWS__ #elif defined(__DARWIN__)
#if defined(__DARWIN__)
static const char* const LIB_NAME = "/usr/lib/libc.dylib";
#elif defined(__LINUX__)
#ifdef __x86_64__
static const char* const LIB_NAME = "/lib/x86_64-linux-gnu/libc.so.6";
#else
static const char* const LIB_NAME = "/lib/libc.so.6";
#endif
#elif defined(__FreeBSD__)
static const char* const LIB_NAME = "/lib/libc.so.7";
#else
static const char* const LIB_NAME = "/unknown/libc/location";
#endif
static const char* const FUNC_NAME = "strlen";
// Under macOS 12+ we can actually load the libc dylib even though the // Under macOS 12+ we can actually load the libc dylib even though the
// corresponding file doesn't exist on disk, so skip this check there. // corresponding file doesn't exist on disk, so we have to handle it
#ifndef __DARWIN__ // differently.
if ( !wxFileName::Exists(LIB_NAME) ) static const char* const LIB_NAME = "/usr/lib/libc.dylib";
static const char* const FUNC_NAME = "strlen";
#else // other Unix
static const char* const candidateDirs[] =
{ {
WARN("Shared library \"" << LIB_NAME << "\" doesn't exist, " "/lib/x86_64-linux-gnu",
"skipping DynamicLibraryTestCase::Load() test."); "/lib",
"/lib64",
"/usr/lib",
};
wxArrayString paths; static const char* const candidateVersions[] = { "6", "7", };
wxDir::GetAllFiles("/lib", &paths, "libc.*", wxDIR_FILES);
wxDir::GetAllFiles("/usr/lib", &paths, "libc.*", wxDIR_FILES); wxString LIB_NAME;
if ( !paths.empty() ) wxArrayString allMatches;
for ( size_t n = 0; n < WXSIZEOF(candidateDirs); ++n )
{
const wxString dir(candidateDirs[n]);
if ( !wxDir::Exists(dir) )
continue;
for ( size_t m = 0; m < WXSIZEOF(candidateVersions); ++m )
{ {
WARN("Possible candidates:\n" << wxJoin(paths, '\n')); const wxString candidate = wxString::Format
(
"%s/libc.so.%s",
dir, candidateVersions[m]
);
if ( wxFileName::Exists(candidate) )
{
LIB_NAME = candidate;
break;
}
}
if ( !LIB_NAME.empty() )
break;
wxDir::GetAllFiles(dir, &allMatches, "libc.*", wxDIR_FILES);
}
if ( LIB_NAME.empty() )
{
WARN("Couldn't find libc.so, skipping DynamicLibrary::Load() test.");
if ( !allMatches.empty() )
{
WARN("Possible candidates:\n" << wxJoin(allMatches, '\n'));
} }
return; return;
} }
#endif // !__DARWIN__
#endif // __WINDOWS__/!__WINDOWS__ static const char* const FUNC_NAME = "strlen";
#endif // OS
wxDynamicLibrary lib(LIB_NAME); wxDynamicLibrary lib(LIB_NAME);
REQUIRE( lib.IsLoaded() ); REQUIRE( lib.IsLoaded() );