Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
N_DEV_RegisterDevices.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // Copyright Notice
3 //
4 // Copyright 2002 Sandia Corporation. Under the terms
5 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
6 // Government retains certain rights in this software.
7 //
8 // Xyce(TM) Parallel Electrical Simulator
9 // Copyright (C) 2002-2011 Sandia Corporation
10 //
11 // This program is free software: you can redistribute it and/or modify
12 // it 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 // This program 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
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 //-----------------------------------------------------------------------------
24 
25 //-------------------------------------------------------------------------
26 // Filename : $RCSfile: N_DEV_RegisterDevices.C,v $
27 //
28 // Purpose :
29 //
30 // Special Notes :
31 //
32 // Creator : David Baur
33 //
34 // Creation Date : 3/15/2013
35 //
36 // Revision Information:
37 // ---------------------
38 //
39 // Revision Number: $Revision: 1.4 $
40 //
41 // Revision Date : $Date: 2014/04/10 20:49:40 $
42 //
43 // Current Owner : $Author: dgbaur $
44 //-------------------------------------------------------------------------
45 
46 #include <Xyce_config.h>
47 
48 #include <cstring>
49 
50 #ifdef HAVE_DLFCN_H
51 #include <dlfcn.h>
52 #endif
53 
54 #include <N_DEV_Message.h>
55 
56 #include <N_DEV_RegisterDevices.h>
60 #include <N_DEV_RegisterExternalDevices.h>
62 
63 #ifdef Xyce_NONFREE_MODELS
64 #include <N_DEV_RegisterNonFreeDevices.h>
65 #endif
66 
67 #ifdef Xyce_RAD_MODELS
68 #include <N_DEV_RegisterSandiaDevices.h>
69 #endif
70 
71 namespace Xyce {
72 namespace Device {
73 
74 void
76 {
77  static bool initialized = false;
78 
79  if (!initialized) {
80  initialized = true;
81 
86  registerExternalDevices();
87 
88 #ifdef Xyce_RAD_MODELS
89  registerSandiaDevices();
90 #endif
91 
92 #ifdef Xyce_NONFREE_MODELS
93  registerNonFreeDevices();
94 #endif
95  }
96 }
97 
98 void registerDL(const char *so_path, const char *function_key = 0);
99 
100 void
101 registerPlugin(const char *name)
102 {
103  registerDL(name);
104 }
105 
106 typedef void (*dl_register_t)();
107 
108 void registerDL(const char *so_path, const char *function_key) {
109 #ifdef HAVE_DLFCN_H
110  void *dl = dlopen(so_path, RTLD_NOW);
111  if (!dl) {
112  const char *error = dlerror();
113  Report::UserError0() << "Failed to load plugin " << error;
114  }
115  else {
116  if (function_key) {
117  std::string s = std::strlen(function_key) ? function_key : "dl_register";
118 
119  dl_register_t f = (dl_register_t) dlsym(dl, s.c_str());
120  if (!f) {
121  f = (dl_register_t) dlsym(dl, s.c_str());
122  }
123 
124  if (f) {
125  (*f)();
126  }
127  else {
128  if (std::strlen(function_key)) {
129  Report::UserError0() << "Executing dynamic library " << so_path << " function " << s << "()";
130  }
131  }
132  }
133  }
134 #endif // HAVE_DLFCN_H
135 }
136 
137 } // namespace Device
138 } // namespace Xyce