Xyce  6.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Core/src/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.3 $
40 //
41 // Revision Date : $Date: 2014/02/11 23:01:33 $
42 //
43 // Current Owner : $Author: dgbaur $
44 //-------------------------------------------------------------------------
45 
46 #include <Xyce_config.h>
47 
48 #ifdef HAVE_DLFCN_H
49 #include <dlfcn.h>
50 #endif
51 
52 #include <N_DEV_Message.h>
53 
54 #include <N_DEV_RegisterDevices.h>
59 
60 #ifdef Xyce_EXTDEV
61 #include <N_DEV_ExternDevice.h>
62 #endif
63 
64 #ifdef Xyce_NONFREE_MODELS
65 #include <N_DEV_RegisterNonFreeDevices.h>
66 #endif
67 
68 #ifdef Xyce_RAD_MODELS
69 #include <N_DEV_RegisterSandiaDevices.h>
70 #endif
71 
72 namespace Xyce {
73 namespace Device {
74 
75 void
77 {
78  static bool initialized = false;
79 
80  if (!initialized) {
81  initialized = true;
82 
87 
88 #ifdef Xyce_EXTDEV
90 #endif
91 
92 #ifdef Xyce_RAD_MODELS
93  registerSandiaDevices();
94 #endif
95 
96 #ifdef Xyce_NONFREE_MODELS
97  registerNonFreeDevices();
98 #endif
99  }
100 }
101 
102 void registerDL(const char *so_path, const char *function_key = 0);
103 
104 void
105 registerPlugin(const char *name)
106 {
107  registerDL(name);
108 }
109 
110 typedef void (*dl_register_t)();
111 
112 void registerDL(const char *so_path, const char *function_key) {
113 #ifdef HAVE_DLFCN_H
114  void *dl = dlopen(so_path, RTLD_NOW);
115  if (!dl) {
116  const char *error = dlerror();
117  Report::UserError0() << "Failed to load plugin " << error;
118  }
119  else {
120  if (function_key) {
121  std::string s = std::strlen(function_key) ? function_key : "dl_register";
122 
123  dl_register_t f = (dl_register_t) dlsym(dl, s.c_str());
124  if (!f) {
125  f = (dl_register_t) dlsym(dl, s.c_str());
126  }
127 
128  if (f) {
129  (*f)();
130  }
131  else {
132  if (std::strlen(function_key)) {
133  Report::UserError0() << "Executing dynamic library " << so_path << " function " << s << "()";
134  }
135  }
136  }
137  }
138 #endif // HAVE_DLFCN_H
139 }
140 
141 } // namespace Device
142 } // namespace Xyce