FreeFOAM The Cross-Platform CFD Toolkit
dlLibraryTable.C
Go to the documentation of this file.
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.
10 
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.
15 
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.
20 
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/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
27 #include <OpenFOAM/debug.H>
28 
29 #include <dlfcn.h>
30 
31 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
32 
34 
35 
36 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37 
39 :
40  HashTable<fileName, void*, Hash<void*> >()
41 {}
42 
43 
45 (
46  const dictionary& dict,
47  const word& libsEntry
48 )
49 {
50  open(dict, libsEntry);
51 }
52 
53 
54 // * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
55 
57 {
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.
61 }
62 
63 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
64 
65 bool Foam::dlLibraryTable::open(const fileName& functionLibName)
66 {
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);
91 
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;
118 
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  }
138 }
139 
140 
142 (
143  const dictionary& dict,
144  const word& libsEntry
145 )
146 {
147  if (dict.found(libsEntry))
148  {
149  fileNameList libNames(dict.lookup(libsEntry));
150 
151  bool allOpened = (libNames.size() > 0);
152 
153  forAll(libNames, i)
154  {
155  allOpened = dlLibraryTable::open(libNames[i]) && allOpened;
156  }
157 
158  return allOpened;
159  }
160  else
161  {
162  return false;
163  }
164 }
165 
166 
167 // ************************ vim: set sw=4 sts=4 et: ************************ //