Re-organize BlueGene toolchain files
[gromacs.git] / src / gmxlib / vmddlopen.c
blobde64ae108a99ed324709686e736cc46f325371d2
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * This file is part of Gromacs Copyright (c) 1991-2008
5 * David van der Spoel, Erik Lindahl, Berk Hess, University of Groningen.
6 * Copyright (c) 2012, by the GROMACS development team, led by
7 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
8 * others, as listed in the AUTHORS file in the top-level source
9 * directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
42 /***************************************************************************
43 *cr
44 *cr (C) Copyright 1995-2009 The Board of Trustees of the
45 *cr University of Illinois
46 *cr All Rights Reserved
47 *cr
48 Developed by: Theoretical and Computational Biophysics Group
49 University of Illinois at Urbana-Champaign
50 http://www.ks.uiuc.edu/
52 Permission is hereby granted, free of charge, to any person obtaining a copy of
53 this software and associated documentation files (the Software), to deal with
54 the Software without restriction, including without limitation the rights to
55 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
56 of the Software, and to permit persons to whom the Software is furnished to
57 do so, subject to the following conditions:
59 Redistributions of source code must retain the above copyright notice,
60 this list of conditions and the following disclaimers.
62 Redistributions in binary form must reproduce the above copyright notice,
63 this list of conditions and the following disclaimers in the documentation
64 and/or other materials provided with the distribution.
66 Neither the names of Theoretical and Computational Biophysics Group,
67 University of Illinois at Urbana-Champaign, nor the names of its contributors
68 may be used to endorse or promote products derived from this Software without
69 specific prior written permission.
71 THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
72 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
73 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
74 THE CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
75 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
76 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
77 OTHER DEALINGS WITH THE SOFTWARE.
78 ***************************************************************************/
80 /***************************************************************************
81 * RCS INFORMATION:
83 * $RCSfile: vmddlopen.c,v $
84 * $Author: johns $ $Locker: $ $State: Exp $
85 * $Revision: 1.18 $ $Date: 2009/07/07 02:40:05 $
87 ***************************************************************************
88 * DESCRIPTION:
89 * Routines for loading dynamic link libraries and shared object files
90 * on various platforms, abstracting from machine dependent APIs.
92 ***************************************************************************/
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include "vmddlopen.h"
99 #ifdef HAVE_CONFIG_H
100 #include <config.h>
101 #endif
104 #ifdef GMX_USE_PLUGINS
105 #if defined(__hpux)
107 #include <dl.h>
108 #include <errno.h>
109 #include <string.h>
111 void *vmddlopen( const char *path) {
112 void *ret;
113 ret = shl_load( path, BIND_IMMEDIATE | BIND_FIRST | BIND_VERBOSE, 0);
114 return ret;
117 int vmddlclose( void *handle ) {
118 return shl_unload( (shl_t) handle );
121 void *vmddlsym( void *handle, const char *sym ) {
122 void *value=0;
124 if ( shl_findsym( (shl_t*)&handle, sym, TYPE_UNDEFINED, &value ) != 0 )
125 return 0;
126 return value;
129 const char *vmddlerror( void ) {
130 return strerror( errno );
133 #elif 0 && defined(__APPLE__)
135 * This is only needed for MacOS X version 10.3 or older
137 #include <mach-o/dyld.h>
139 void *vmddlopen( const char *path) {
140 NSObjectFileImage image;
141 NSObjectFileImageReturnCode retval;
142 NSModule module;
144 retval = NSCreateObjectFileImageFromFile(path, &image);
145 if (retval != NSObjectFileImageSuccess)
146 return NULL;
148 module = NSLinkModule(image, path,
149 NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE
150 | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
151 return module; /* module will be NULL on error */
154 int vmddlclose( void *handle ) {
155 NSModule module = (NSModule *)handle;
156 NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE);
157 return 0;
160 void *vmddlsym( void *handle, const char *symname ) {
161 char *realsymname;
162 NSModule module;
163 NSSymbol sym;
164 /* Hack around the leading underscore in the symbol name */
165 realsymname = (char *)malloc(strlen(symname)+2);
166 strcpy(realsymname, "_");
167 strcat(realsymname, symname);
168 module = (NSModule)handle;
169 sym = NSLookupSymbolInModule(module, realsymname);
170 free(realsymname);
171 if (sym)
172 return (void *)(NSAddressOfSymbol(sym));
173 return NULL;
176 const char *vmddlerror( void ) {
177 NSLinkEditErrors c;
178 int errorNumber;
179 const char *fileName;
180 const char *errorString = NULL;
181 NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
182 return errorString;
185 #elif defined(_MSC_VER)
187 #include <windows.h>
189 void *vmddlopen(const char *fname) {
190 return (void *)LoadLibrary(fname);
193 const char *vmddlerror(void) {
194 static CHAR szBuf[80];
195 DWORD dw = GetLastError();
197 sprintf(szBuf, "vmddlopen failed: GetLastError returned %u\n", dw);
198 return szBuf;
201 void *vmddlsym(void *h, const char *sym) {
202 return (void *)GetProcAddress((HINSTANCE)h, sym);
205 int vmddlclose(void *h) {
206 /* FreeLibrary returns nonzero on success */
207 return !FreeLibrary((HINSTANCE)h);
210 #else
212 /* All remaining platforms (not Windows, HP-UX, or MacOS X <= 10.3) */
213 #include <dlfcn.h>
215 void *vmddlopen(const char *fname) {
216 return dlopen(fname, RTLD_NOW);
218 const char *vmddlerror(void) {
219 return dlerror();
221 void *vmddlsym(void *h, const char *sym) {
222 return dlsym(h, sym);
224 int vmddlclose(void *h) {
225 return dlclose(h);
227 #endif
228 #else
229 void *vmddlopen(const char *fname) {
230 return NULL;
232 const char *vmddlerror(void) {
233 return NULL;
235 void *vmddlsym(void *h, const char *sym) {
236 return NULL;
238 int vmddlclose(void *h) {
239 return 0;
241 #endif