Add -D to iverilog-vpi and update documentation.
[iverilog.git] / cadpli / ivl_dlfcn.h
blob9bfab82842a7beec4d54441b233a085b69167c66
1 #ifndef __ivl_dlfcn_H
2 #define __ivl_dlfcn_H
3 /*
4 * Copyright (c) 2001 Stephen Williams (steve@icarus.com)
6 * This source code is free software; you can redistribute it
7 * and/or modify it in source code form under the terms of the GNU
8 * General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 #ifdef HAVE_CVS_IDENT
22 #ident "$Id: ivl_dlfcn.h,v 1.3 2004/10/04 01:10:56 steve Exp $"
23 #endif
25 #if defined(__MINGW32__)
26 # include <windows.h>
27 # include <stdio.h>
28 typedef void * ivl_dll_t;
29 #elif defined(HAVE_DLFCN_H)
30 # include <dlfcn.h>
31 typedef void* ivl_dll_t;
32 #elif defined(HAVE_DL_H)
33 # include <dl.h>
34 typedef shl_t ivl_dll_t;
35 #endif
37 #if defined(__MINGW32__)
38 static inline ivl_dll_t ivl_dlopen(const char *name)
39 { return (void *)LoadLibrary(name); }
41 static inline void *ivl_dlsym(ivl_dll_t dll, const char *nm)
42 { return (void *)GetProcAddress((HINSTANCE)dll,nm);}
44 static inline void ivl_dlclose(ivl_dll_t dll)
45 { (void)FreeLibrary((HINSTANCE)dll);}
47 static inline const char *dlerror(void)
49 static char msg[256];
50 unsigned long err = GetLastError();
51 FormatMessage(
52 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
53 NULL,
54 err,
55 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
56 (LPTSTR) &msg,
57 sizeof(msg) - 1,
58 NULL
60 return msg;
63 #elif defined(HAVE_DLFCN_H)
64 static inline ivl_dll_t ivl_dlopen(const char*name)
65 { return dlopen(name,RTLD_LAZY); }
67 static inline void* ivl_dlsym(ivl_dll_t dll, const char*nm)
69 void*sym = dlsym(dll, nm);
70 /* Not found? try without the leading _ */
71 if (sym == 0 && nm[0] == '_')
72 sym = dlsym(dll, nm+1);
73 return sym;
76 static inline void ivl_dlclose(ivl_dll_t dll)
77 { dlclose(dll); }
79 #elif defined(HAVE_DL_H)
80 static inline ivl_dll_t ivl_dlopen(const char*name)
81 { return shl_load(name, BIND_IMMEDIATE, 0); }
83 static inline void* ivl_dlsym(ivl_dll_t dll, const char*nm)
85 void*sym;
86 int rc = shl_findsym(&dll, nm, TYPE_PROCEDURE, &sym);
87 return (rc == 0) ? sym : 0;
90 static inline void ivl_dlclose(ivl_dll_t dll)
91 { shl_unload(dll); }
93 static inline const char*dlerror(void)
94 { return strerror( errno ); }
95 #endif
98 * $Log: ivl_dlfcn.h,v $
99 * Revision 1.3 2004/10/04 01:10:56 steve
100 * Clean up spurious trailing white space.
102 * Revision 1.2 2003/12/12 05:43:08 steve
103 * Some systems dlsym requires leading _ or not on whim.
105 * Revision 1.1 2003/02/17 00:01:25 steve
106 * Use a variant of ivl_dlfcn to do dynamic loading
107 * from within the cadpli module.
109 * Change the +cadpli flag to -cadpli, to keep the
110 * plusargs namespace clear.
113 #endif