mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
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
This commit is contained in:
committed by
Andrew Heather
parent
d9cefeff99
commit
882d7310d1
@ -47,6 +47,8 @@ License
|
|||||||
#include <io.h> // For _close
|
#include <io.h> // For _close
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
#define EXT_SO "dll"
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
|
||||||
|
|
||||||
@ -1190,14 +1192,21 @@ void* Foam::dlOpen(const fileName& libName, const bool check)
|
|||||||
<< " : dlopen of " << libName << std::endl;
|
<< " : 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));
|
||||||
|
|
||||||
fileName winLibName(libName.lessExt().ext("dll"));
|
void* handle = ::LoadLibrary(libso.c_str());
|
||||||
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)
|
if (handle)
|
||||||
{
|
{
|
||||||
libsLoaded[handle] = libName.lessExt();
|
libsLoaded[handle] = libso.lessExt();
|
||||||
}
|
}
|
||||||
else if (check)
|
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)
|
bool Foam::dlClose(void* const handle)
|
||||||
{
|
{
|
||||||
if (MSwindows::debug)
|
if (MSwindows::debug)
|
||||||
|
|||||||
@ -61,8 +61,10 @@ Description
|
|||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
|
#define EXT_SO "dylib"
|
||||||
#include <mach-o/dyld.h>
|
#include <mach-o/dyld.h>
|
||||||
#else
|
#else
|
||||||
|
#define EXT_SO "so"
|
||||||
|
|
||||||
// PGI does not have __int128_t
|
// PGI does not have __int128_t
|
||||||
#ifdef __PGIC__
|
#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)
|
void* Foam::dlOpen(const fileName& libName, const bool check)
|
||||||
{
|
{
|
||||||
|
constexpr int ldflags = (RTLD_LAZY|RTLD_GLOBAL);
|
||||||
|
|
||||||
if (POSIX::debug)
|
if (POSIX::debug)
|
||||||
{
|
{
|
||||||
std::cout
|
std::cout
|
||||||
<< "dlOpen(const fileName&)"
|
<< "dlOpen(const fileName&)"
|
||||||
<< " : dlopen of " << libName << std::endl;
|
<< " : dlopen of " << libName << std::endl;
|
||||||
}
|
}
|
||||||
void* handle = ::dlopen(libName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
|
|
||||||
|
|
||||||
#ifdef __APPLE__
|
void* handle = ::dlopen(libName.c_str(), ldflags);
|
||||||
// Re-try "libXX.so" as "libXX.dylib"
|
|
||||||
if (!handle && libName.hasExt("so"))
|
if (!handle)
|
||||||
{
|
{
|
||||||
const fileName dylibName(libName.lessExt().ext("dylib"));
|
fileName libso;
|
||||||
handle = ::dlopen(dylibName.c_str(), RTLD_LAZY|RTLD_GLOBAL);
|
|
||||||
|
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)
|
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)
|
bool Foam::dlClose(void* handle)
|
||||||
{
|
{
|
||||||
if (POSIX::debug)
|
if (POSIX::debug)
|
||||||
|
|||||||
@ -36,7 +36,6 @@ License
|
|||||||
#include "dictionary.H"
|
#include "dictionary.H"
|
||||||
#include "foamVersion.H"
|
#include "foamVersion.H"
|
||||||
|
|
||||||
#undef EXT_SO
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
#define EXT_SO ".dylib"
|
#define EXT_SO ".dylib"
|
||||||
#elif defined _WIN32
|
#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);
|
int system(const CStringList& command, const bool bg = false);
|
||||||
|
|
||||||
//- Open a shared library and return handle to library.
|
//- Open a shared library and return handle to library.
|
||||||
// Print error message if library cannot be loaded (suppress with check=true)
|
// A leading "lib" and ".so" suffix are added silently as required.
|
||||||
void* dlOpen(const fileName& lib, const bool check = true);
|
// 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
|
//- Close a dlopened library using handle. Return true if successful
|
||||||
bool dlClose(void* handle);
|
bool dlClose(void* handle);
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
system
|
system
|
||||||
{
|
{
|
||||||
type systemCall;
|
type systemCall;
|
||||||
libs ("libutilityFunctionObjects.so");
|
libs ("utilityFunctionObjects");
|
||||||
|
|
||||||
// Execute on the master process only
|
// Execute on the master process only
|
||||||
master true;
|
master true;
|
||||||
|
|||||||
Reference in New Issue
Block a user