Commit 882d7310 authored by Mark Olesen's avatar Mark Olesen Committed by Andrew Heather
Browse files

ENH: silently add "lib" prefix and ".so" suffix for dlOpen'ed libraries

- reduces some dictionary clutter and probably looks less confusing
  than having an ending that may not correspond to the current OS.

  Eg, "fvOptions" instead of "libfvOptions.so", "libfvOptions.dylib" ...

- convenience dlOpen method for multiple files
parent d9cefeff
......@@ -47,6 +47,8 @@ License
#include <io.h> // For _close
#include <windows.h>
#define EXT_SO "dll"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
......@@ -1190,14 +1192,21 @@ void* Foam::dlOpen(const fileName& libName, const bool check)
<< " : dlopen of " << libName << std::endl;
}
// Map "libXX.so" and "libXX" to "libXX.dll"
// Always remap "libXX.so" and "libXX" to "libXX.dll"
fileName libso(libName.lessExt().ext(EXT_SO));
void* handle = ::LoadLibrary(libso.c_str());
fileName winLibName(libName.lessExt().ext("dll"));
void* handle = ::LoadLibrary(winLibName.c_str());
if (!handle && !libso.startsWith("lib"))
{
// Try with 'lib' prefix
libso = "lib" + libso;
handle = ::LoadLibrary(libso.c_str());
}
if (handle)
{
libsLoaded[handle] = libName.lessExt();
libsLoaded[handle] = libso.lessExt();
}
else if (check)
{
......@@ -1218,6 +1227,26 @@ void* Foam::dlOpen(const fileName& libName, const bool check)
}
Foam::label Foam::dlOpen
(
std::initializer_list<fileName> libNames,
const bool check
)
{
label nLoaded = 0;
for (const fileName& libName : libNames)
{
if (Foam::dlOpen(libName, check))
{
++nLoaded;
}
}
return nLoaded;
}
bool Foam::dlClose(void* const handle)
{
if (MSwindows::debug)
......
......@@ -61,8 +61,10 @@ Description
#include <dlfcn.h>
#ifdef __APPLE__
#define EXT_SO "dylib"
#include <mach-o/dyld.h>
#else
#define EXT_SO "so"
// PGI does not have __int128_t
#ifdef __PGIC__
......@@ -1653,22 +1655,40 @@ int Foam::system(const Foam::UList<Foam::string>& command, const bool bg)
void* Foam::dlOpen(const fileName& libName, const bool check)
{
constexpr int ldflags = (RTLD_LAZY|RTLD_GLOBAL);
if (POSIX::debug)
{
std::cout
<< "dlOpen(const fileName&)"
<< " : dlopen of " << libName << std::endl;
}
void* handle = ::dlopen(libName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
#ifdef __APPLE__
// Re-try "libXX.so" as "libXX.dylib"
if (!handle && libName.hasExt("so"))
void* handle = ::dlopen(libName.c_str(), ldflags);
if (!handle)
{
const fileName dylibName(libName.lessExt().ext("dylib"));
handle = ::dlopen(dylibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
fileName libso;
if (!libName.startsWith("lib"))
{
// Try with 'lib' prefix
libso = "lib" + libName;
handle = ::dlopen(libso.c_str(), ldflags);
}
else
{
libso = libName;
}
// With canonical library extension ("so" or "dylib"), which remaps
// "libXX" to "libXX.so" as well as "libXX.so" -> "libXX.dylib"
if (!handle && !libso.hasExt(EXT_SO))
{
libso = libso.lessExt().ext(EXT_SO);
handle = ::dlopen(libso.c_str(), ldflags);
}
}
#endif
if (!handle && check)
{
......@@ -1689,6 +1709,26 @@ void* Foam::dlOpen(const fileName& libName, const bool check)
}
Foam::label Foam::dlOpen
(
std::initializer_list<fileName> libNames,
const bool check
)
{
label nLoaded = 0;
for (const fileName& libName : libNames)
{
if (Foam::dlOpen(libName, check))
{
++nLoaded;
}
}
return nLoaded;
}
bool Foam::dlClose(void* handle)
{
if (POSIX::debug)
......
......@@ -36,7 +36,6 @@ License
#include "dictionary.H"
#include "foamVersion.H"
#undef EXT_SO
#ifdef __APPLE__
#define EXT_SO ".dylib"
#elif defined _WIN32
......
......@@ -253,8 +253,14 @@ int system(const UList<string>& command, const bool bg = false);
int system(const CStringList& command, const bool bg = false);
//- Open a shared library and return handle to library.
// Print error message if library cannot be loaded (suppress with check=true)
void* dlOpen(const fileName& lib, const bool check = true);
// A leading "lib" and ".so" suffix are added silently as required.
// Prints warning if a library cannot be loaded (suppress with check=false)
void* dlOpen(const fileName& libName, const bool check=true);
//- Open shared libraries and return number of libraries loaded.
// A leading "lib" and ".so" suffix are added silently as required.
// Prints warning if a library cannot be loaded (suppress with check=false)
label dlOpen(std::initializer_list<fileName> libNames, const bool check=true);
//- Close a dlopened library using handle. Return true if successful
bool dlClose(void* handle);
......
......@@ -3,7 +3,7 @@
system
{
type systemCall;
libs ("libutilityFunctionObjects.so");
libs ("utilityFunctionObjects");
// Execute on the master process only
master true;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment