abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
libSupport.c
Go to the documentation of this file.
1 /**CFile****************************************************************
2 
3  FileName [libSupport.c]
4 
5  SystemName [ABC: Logic synthesis and verification system.]
6 
7  PackageName [The main package.]
8 
9  Synopsis [Support for external libaries.]
10 
11  Author [Mike Case]
12 
13  Affiliation [UC Berkeley]
14 
15  Date [Ver. 1.0. Started - June 20, 2005.]
16 
17  Revision [$Id: libSupport.c,v 1.1 2005/09/06 19:59:51 casem Exp $]
18 
19 ***********************************************************************/
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
48 static void* libHandles[MAX_LIBS+1]; // will be null terminated
49 
50 typedef void (*lib_init_end_func) (Abc_Frame_t * pAbc);
51 
52 ////////////////////////////////////////////////////////////////////////////////////////////////////
53 // This will find all the ABC library extensions in the current directory and load them all.
54 ////////////////////////////////////////////////////////////////////////////////////////////////////
55 void 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  char *endp = strchr (p,':');
81  if (endp == NULL) done = 1; // last directory in the list
82  else *endp = 0; // end of string
83 
84  dirp = opendir(p);
85  if (dirp == NULL) {
86 // printf("Warning: directory in ABC_LIB_PATH does not exist (%s).\n", p);
87  continue;
88  }
89 
90  while ((dp = readdir(dirp)) != NULL) {
91  if ((strncmp("libabc_", dp->d_name, 7) == 0) &&
92  (strcmp(".so", dp->d_name + strlen(dp->d_name) - 3) == 0)) {
93 
94  // make sure we don't overflow the handle array
95  if (curr_lib >= MAX_LIBS) {
96  printf("Warning: maximum number of ABC libraries (%d) exceeded. Not loading %s.\n",
97  MAX_LIBS,
98  dp->d_name);
99  }
100 
101  // attempt to load it
102  else {
103  char* szPrefixed = ABC_ALLOC( char, ((strlen(dp->d_name) + strlen(p) + 2) *
104  sizeof(char)) );
105  sprintf(szPrefixed, "%s/", p);
106  strcat(szPrefixed, dp->d_name);
107  libHandles[curr_lib] = dlopen(szPrefixed, RTLD_NOW | RTLD_LOCAL);
108 
109  // did the load succeed?
110  if (libHandles[curr_lib] != 0) {
111  printf("Loaded ABC library: %s (Abc library extension #%d)\n", szPrefixed, curr_lib);
112  curr_lib++;
113  } else {
114  printf("Warning: failed to load ABC library %s:\n\t%s\n", szPrefixed, dlerror());
115  }
116 
117  ABC_FREE(szPrefixed);
118  }
119  }
120  }
121  closedir(dirp);
122  p = endp+1;
123  }
124 
125  ABC_FREE(init_p);
126 #endif
127 
128  // null terminate the list of handles
129  libHandles[curr_lib] = 0;
130 }
131 
132 ////////////////////////////////////////////////////////////////////////////////////////////////////
133 // This will close all open ABC library extensions
134 ////////////////////////////////////////////////////////////////////////////////////////////////////
135 void close_libs() {
136 #ifdef WIN32
137  printf("Warning: close_libs WIN32 not implemented.\n");
138 #else
139  int i;
140  for (i = 0; libHandles[i] != 0; i++) {
141  if (dlclose(libHandles[i]) != 0) {
142  printf("Warning: failed to close library %d\n", i);
143  }
144  libHandles[i] = 0;
145  }
146 #endif
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////////////////////////
150 // This will get a pointer to a function inside of an open library
151 ////////////////////////////////////////////////////////////////////////////////////////////////////
152 void* get_fnct_ptr(int lib_num, char* sym_name) {
153 #ifdef WIN32
154  printf("Warning: get_fnct_ptr WIN32 not implemented.\n");
155  return 0;
156 #else
157  return dlsym(libHandles[lib_num], sym_name);
158 #endif
159 }
160 
161 ////////////////////////////////////////////////////////////////////////////////////////////////////
162 // This will call an initialization function in every open library.
163 ////////////////////////////////////////////////////////////////////////////////////////////////////
164 void call_inits(Abc_Frame_t* pAbc) {
165  int i;
166  lib_init_end_func init_func;
167  for (i = 0; libHandles[i] != 0; i++) {
168  init_func = (lib_init_end_func) get_fnct_ptr(i, "abc_init");
169  if (init_func == 0) {
170  printf("Warning: Failed to initialize library %d.\n", i);
171  } else {
172  (*init_func)(pAbc);
173  }
174  }
175 }
176 
177 ////////////////////////////////////////////////////////////////////////////////////////////////////
178 // This will call a shutdown function in every open library.
179 ////////////////////////////////////////////////////////////////////////////////////////////////////
180 void call_ends(Abc_Frame_t* pAbc) {
181  int i;
182  lib_init_end_func end_func;
183  for (i = 0; libHandles[i] != 0; i++) {
184  end_func = (lib_init_end_func) get_fnct_ptr(i, "abc_end");
185  if (end_func == 0) {
186  printf("Warning: Failed to end library %d.\n", i);
187  } else {
188  (*end_func)(pAbc);
189  }
190  }
191 }
192 
193 void Libs_Init(Abc_Frame_t * pAbc)
194 {
195  open_libs();
196  call_inits(pAbc);
197 }
198 
199 void Libs_End(Abc_Frame_t * pAbc)
200 {
201  call_ends(pAbc);
202 
203  // It's good practice to close our libraries at this point, but this can mess up any backtrace printed by Valgind.
204  // close_libs();
205 }
206 
207 ////////////////////////////////////////////////////////////////////////
208 /// END OF FILE ///
209 ////////////////////////////////////////////////////////////////////////
211 
static Llb_Mgr_t * p
Definition: llb3Image.c:950
void open_libs()
Definition: libSupport.c:55
void Libs_End(Abc_Frame_t *pAbc)
Definition: libSupport.c:199
#define ABC_ALLOC(type, num)
Definition: abc_global.h:229
void call_ends(Abc_Frame_t *pAbc)
Definition: libSupport.c:180
void Libs_Init(Abc_Frame_t *pAbc)
Definition: libSupport.c:193
int strcmp()
typedefABC_NAMESPACE_HEADER_START struct Abc_Frame_t_ Abc_Frame_t
INCLUDES ///.
void call_inits(Abc_Frame_t *pAbc)
Definition: libSupport.c:164
void close_libs()
Definition: libSupport.c:135
char * strchr()
char * getenv()
#define ABC_NAMESPACE_IMPL_END
Definition: abc_global.h:108
char * sprintf()
void * get_fnct_ptr(int lib_num, char *sym_name)
Definition: libSupport.c:152
char * strcpy()
#define ABC_NAMESPACE_IMPL_START
Definition: abc_global.h:107
#define MAX_LIBS
Definition: libSupport.c:47
#define ABC_FREE(obj)
Definition: abc_global.h:232
void(* lib_init_end_func)(Abc_Frame_t *pAbc)
Definition: libSupport.c:50
char * strcat()
int strncmp()
int strlen()
static void * libHandles[MAX_LIBS+1]
Definition: libSupport.c:48