1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
9 This file is part of OpenFOAM.
11 OpenFOAM is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
16 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License
22 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include <OpenFOAM/dlLibraryTable.H>
27 #include <OpenFOAM/debug.H>
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
33 Foam::dlLibraryTable Foam::dlLibraryTable::loadedLibraries;
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
38 Foam::dlLibraryTable::dlLibraryTable()
40 HashTable<fileName, void*, Hash<void*> >()
44 Foam::dlLibraryTable::readDlLibrary::readDlLibrary
46 const dictionary& dict,
50 open(dict, libsEntry);
54 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
56 Foam::dlLibraryTable::~dlLibraryTable()
58 // Don't bother with calling dlclose(), the OS does so anyways and it can
59 // even cause problems if the generated finalization code decides to unload
60 // the loaded library before calling this dtor.
63 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
65 bool Foam::dlLibraryTable::open(const fileName& functionLibName)
67 if (functionLibName.size())
69 void* functionLibPtr = NULL;
70 // set up the list of paths to search the library in
71 fileNameList searchPathList;
72 // add the paths listed in controlDict::LibrarySearchPaths to the list
73 // if the library name is not absolute
74 if (functionLibName[0] != '/' && debug::controlDict().found("LibrarySearchPaths"))
76 debug::controlDict().lookup("LibrarySearchPaths") >> searchPathList;
78 // in any case, we want to also try the default search paths
79 // (i.e. LD_LIBRARY_PATH etc.) or, if functionLibName is an
80 // absolute path, that one. So we append an empty string.
81 searchPathList.setSize(searchPathList.size()+1,fileName());
82 forAllConstIter(fileNameList, searchPathList, pathI)
84 // construct the full name
85 fileName functionLibPath;
87 functionLibPath = *pathI / functionLibName;
89 functionLibPath = functionLibName;
90 functionLibPtr = dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
93 if(!functionLibPtr && functionLibPath.ext()=="so") {
94 functionLibPath=functionLibPath.lessExt()+".dylib";
96 dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
99 // if successfully loaded, stop searching and display some info
102 Info<< "Loaded " << functionLibName;
104 Info<< " from the default search path";
106 Info<< " from " << functionLibPath;
115 "dlLibraryTable::open(const fileName& functionLibName)"
116 ) << "could not load " << dlerror()
123 if (!loadedLibraries.found(functionLibPtr))
125 loadedLibraries.insert(functionLibPtr, functionLibName);
141 bool Foam::dlLibraryTable::open
143 const dictionary& dict,
144 const word& libsEntry
147 if (dict.found(libsEntry))
149 fileNameList libNames(dict.lookup(libsEntry));
151 bool allOpened = (libNames.size() > 0);
155 allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
167 // ************************ vim: set sw=4 sts=4 et: ************************ //