FIX: Static init issues with argList.C and dlLibraryTable.C
[freefoam.git] / src / OpenFOAM / db / dlLibraryTable / dlLibraryTable.C
blob107ce7cb89f1383b7613f55789b87a9a5e17d4ab
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
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
19     for more details.
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>
29 #include <dlfcn.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,
47     const word& libsEntry
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())
68     {
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"))
75         {
76             debug::controlDict().lookup("LibrarySearchPaths") >> searchPathList;
77         }
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)
83         {
84             // construct the full name
85             fileName functionLibPath;
86             if (!pathI->empty())
87                 functionLibPath = *pathI / functionLibName;
88             else
89                 functionLibPath = functionLibName;
90             functionLibPtr = dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
92 #ifdef darwin
93             if(!functionLibPtr && functionLibPath.ext()=="so") {
94                 functionLibPath=functionLibPath.lessExt()+".dylib";
95                 functionLibPtr =
96                     dlopen(functionLibPath.c_str(), RTLD_LAZY|RTLD_GLOBAL);
97             }
98 #endif
99             // if successfully loaded, stop searching and display some info
100             if (functionLibPtr)
101             {
102                 Info<< "Loaded  " << functionLibName;
103                 if (pathI->empty())
104                     Info<< " from the default search path";
105                 else
106                     Info<< " from " << functionLibPath;
107                 Info<< endl;
108                 break;
109             }
110         }
111         if (!functionLibPtr)
112         {
113             WarningIn
114             (
115                 "dlLibraryTable::open(const fileName& functionLibName)"
116             )   << "could not load " << dlerror()
117                 << endl;
119             return false;
120         }
121         else
122         {
123             if (!loadedLibraries.found(functionLibPtr))
124             {
125                 loadedLibraries.insert(functionLibPtr, functionLibName);
126                 return true;
127             }
128             else
129             {
130                 return false;
131             }
132         }
133     }
134     else
135     {
136         return false;
137     }
141 bool Foam::dlLibraryTable::open
143     const dictionary& dict,
144     const word& libsEntry
147     if (dict.found(libsEntry))
148     {
149         fileNameList libNames(dict.lookup(libsEntry));
151         bool allOpened = (libNames.size() > 0);
153         forAll(libNames, i)
154         {
155             allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
156         }
158         return allOpened;
159     }
160     else
161     {
162         return false;
163     }
167 // ************************ vim: set sw=4 sts=4 et: ************************ //