ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
libSupport.c
Go to the documentation of this file.
1
20
21#include <stdio.h>
22#include <string.h>
23
24#include "base/abc/abc.h"
25#include "mainInt.h"
26
28
29#if defined(ABC_NO_DYNAMIC_LINKING)
30#define WIN32
31#endif
32
33#ifndef WIN32
34# include <sys/types.h>
35# include <dirent.h>
36# include <dlfcn.h>
37#endif
38
39// fix by Paddy O'Brien on Sep 22, 2009
40#ifdef __CYGWIN__
41#ifndef RTLD_LOCAL
42#define RTLD_LOCAL 0
43#endif
44#endif
45
46
47#define MAX_LIBS 256
48static void* libHandles[MAX_LIBS+1]; // will be null terminated
49
50typedef void (*lib_init_end_func) (Abc_Frame_t * pAbc);
51
53// This will find all the ABC library extensions in the current directory and load them all.
55void open_libs() {
56 int curr_lib = 0;
57
58#ifdef WIN32
59// printf("Warning: open_libs WIN32 not implemented.\n");
60#else
61 DIR* dirp;
62 struct dirent* dp;
63 char *env, *init_p, *p;
64 //int done;
65
66 env = getenv ("ABC_LIB_PATH");
67 if (env == NULL) {
68// printf("Warning: ABC_LIB_PATH not defined. Looking into the current directory.\n");
69 init_p = ABC_ALLOC( char, (2*sizeof(char)) );
70 init_p[0]='.'; init_p[1] = 0;
71 } else {
72 init_p = ABC_ALLOC( char, ((strlen(env)+1)*sizeof(char)) );
73 strcpy (init_p, env);
74 }
75
76 // Extract directories and read libraries
77 //done = 0;
78 p = init_p;
79 //while (!done) {
80 for (;;) {
81 char *endp = strchr (p,':');
82 //if (endp == NULL) done = 1; // last directory in the list
83 //else *endp = 0; // end of string
84 if (endp != NULL) *endp = 0; // end of string
85
86 dirp = opendir(p);
87 if (dirp == NULL) {
88// printf("Warning: directory in ABC_LIB_PATH does not exist (%s).\n", p);
89 continue;
90 }
91
92 while ((dp = readdir(dirp)) != NULL) {
93 if ((strncmp("libabc_", dp->d_name, 7) == 0) &&
94 (strcmp(".so", dp->d_name + strlen(dp->d_name) - 3) == 0)) {
95
96 // make sure we don't overflow the handle array
97 if (curr_lib >= MAX_LIBS) {
98 printf("Warning: maximum number of ABC libraries (%d) exceeded. Not loading %s.\n",
100 dp->d_name);
101 }
102
103 // attempt to load it
104 else {
105 char* szPrefixed = ABC_ALLOC( char, ((strlen(dp->d_name) + strlen(p) + 2) *
106 sizeof(char)) );
107 sprintf(szPrefixed, "%s/", p);
108 strcat(szPrefixed, dp->d_name);
109 libHandles[curr_lib] = dlopen(szPrefixed, RTLD_NOW | RTLD_LOCAL);
110
111 // did the load succeed?
112 if (libHandles[curr_lib] != 0) {
113 printf("Loaded ABC library: %s (Abc library extension #%d)\n", szPrefixed, curr_lib);
114 curr_lib++;
115 } else {
116 printf("Warning: failed to load ABC library %s:\n\t%s\n", szPrefixed, dlerror());
117 }
118
119 ABC_FREE(szPrefixed);
120 }
121 }
122 }
123 closedir(dirp);
124 //p = endp+1;
125 if (endp == NULL) {
126 break; // last directory in the list
127 } else {
128 p = endp+1;
129 }
130 }
131
132 ABC_FREE(init_p);
133#endif
134
135 // null terminate the list of handles
136 libHandles[curr_lib] = 0;
137}
138
140// This will close all open ABC library extensions
143#ifdef WIN32
144 printf("Warning: close_libs WIN32 not implemented.\n");
145#else
146 int i;
147 for (i = 0; libHandles[i] != 0; i++) {
148 if (dlclose(libHandles[i]) != 0) {
149 printf("Warning: failed to close library %d\n", i);
150 }
151 libHandles[i] = 0;
152 }
153#endif
154}
155
157// This will get a pointer to a function inside of an open library
159void* get_fnct_ptr(int lib_num, char* sym_name) {
160#ifdef WIN32
161 printf("Warning: get_fnct_ptr WIN32 not implemented.\n");
162 return 0;
163#else
164 return dlsym(libHandles[lib_num], sym_name);
165#endif
166}
167
169// This will call an initialization function in every open library.
172 int i;
173 lib_init_end_func init_func;
174 for (i = 0; libHandles[i] != 0; i++) {
175 init_func = (lib_init_end_func) get_fnct_ptr(i, "abc_init");
176 if (init_func == 0) {
177 printf("Warning: Failed to initialize library %d.\n", i);
178 } else {
179 (*init_func)(pAbc);
180 }
181 }
182}
183
185// This will call a shutdown function in every open library.
188 int i;
189 lib_init_end_func end_func;
190 for (i = 0; libHandles[i] != 0; i++) {
191 end_func = (lib_init_end_func) get_fnct_ptr(i, "abc_end");
192 if (end_func == 0) {
193 printf("Warning: Failed to end library %d.\n", i);
194 } else {
195 (*end_func)(pAbc);
196 }
197 }
198}
199
201{
202 open_libs();
203 call_inits(pAbc);
204}
205
207{
208 call_ends(pAbc);
209
210 // It's good practice to close our libraries at this point, but this can mess up any backtrace printed by Valgind.
211 // close_libs();
212}
213
218
#define ABC_ALLOC(type, num)
Definition abc_global.h:264
#define ABC_FREE(obj)
Definition abc_global.h:267
#define ABC_NAMESPACE_IMPL_START
#define ABC_NAMESPACE_IMPL_END
typedefABC_NAMESPACE_HEADER_START struct Abc_Frame_t_ Abc_Frame_t
INCLUDES ///.
Definition abcapis.h:38
#define DIR
Definition build.h:8
Cube * p
Definition exorList.c:222
#define MAX_LIBS
Definition libSupport.c:47
void open_libs()
Definition libSupport.c:55
void call_ends(Abc_Frame_t *pAbc)
Definition libSupport.c:187
void Libs_Init(Abc_Frame_t *pAbc)
Definition libSupport.c:200
void(* lib_init_end_func)(Abc_Frame_t *pAbc)
Definition libSupport.c:50
void close_libs()
Definition libSupport.c:142
void Libs_End(Abc_Frame_t *pAbc)
Definition libSupport.c:206
void * get_fnct_ptr(int lib_num, char *sym_name)
Definition libSupport.c:159
void call_inits(Abc_Frame_t *pAbc)
Definition libSupport.c:171
int strncmp()
int strlen()
int strcmp()
char * sprintf()
char * strcpy()
char * getenv()
char * strchr()
char * strcat()