source: Sophya/trunk/SophyaLib/SysTools/pdlmgr.cc@ 1783

Last change on this file since 1783 was 1783, checked in by aubourg, 24 years ago

pour compilation darwin (MacOS X 10.1.1)

File size: 7.0 KB
Line 
1//
2// Gestionnaire de lien dynamique - R. Ansari 12/98
3// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
4//
5#include <machdefs.h>
6#ifndef OS_MACOSX
7// EA : gerer dylib macosx un jour...
8
9#include "pdlmgr.h"
10#include <stdio.h>
11#include <stdlib.h>
12
13#include <iostream.h>
14
15
16// Extension de noms de fichiers Shared libs
17static const char* sofext = ".so";
18static const char* sofext_HPUX = ".sl";
19
20// Variables et methodes static
21int PDynLinkMgr::numSO = 0;
22string* PDynLinkMgr::tmpDir = NULL;
23
24/*!
25 \class SOPHYA::PDynLinkMgr
26 \ingroup SysTools
27 This classes handles the run-time operations related to using shared
28 libraries. The present version has been adapted for different Unix
29 flavours (Linux, Compaq/Digital Unix, SGI IRIX, IBM AIX, Sun Solaris).
30 The example here shows the linking of shared library named "mylib.so"
31 containing a function \c double \c myfunction(double x).
32 \code
33 #include "pdlmgr.h"
34 typedef double (* AFunctionOfX) (double x);
35 {
36 // ...
37 string soname = "mylib.so";
38 string funcname = "myfunction";
39 PDynLinkMgr dyl(son);
40 AFunctionOfX f = (AFunctionOfX)dyl.GetFunction(funcname);
41 double x = 3.1425;
42 if (f != NULL)
43 cout << " X= " << x << " myfunction(x)=" << f(x) << endl;
44 // ...
45 }
46 \endcode
47*/
48
49/* --Methode-Static-- */
50/*! Sets the path for a temporary space where shared libraries are copied.
51 The path is appended to \b LD_LIBRARY_PATH
52*/
53void PDynLinkMgr::SetTmpDir(string const & path)
54{
55if ( (path.length() > 0) && (path[path.length()-1] != '/') ) GetTmpDir() = path + '/';
56else GetTmpDir() = path;
57#if defined(OSF1) || defined(Linux) || defined(SunOS) || defined(IRIX64)
58 char* varenv=NULL;
59#if !defined(IRIX64)
60 string cmd = "LD_LIBRARY_PATH=";
61 varenv=getenv("LD_LIBRARY_PATH");
62#else
63#ifdef SGI_ARCH64
64 string cmd = "LD_LIBRARYN32_PATH=";
65 varenv=getenv("LD_LIBRARYN32_PATH");
66#else
67 string cmd = "LD_LIBRARYN64_PATH=";
68 varenv=getenv("LD_LIBRARYN64_PATH");
69#endif
70#endif
71
72 if (varenv == NULL) {
73 cmd += '.';
74 if (path.length() > 0) cmd += ':' + path;
75 }
76 else {
77 if (varenv[0] != '.') cmd += ".:";
78 if (path.length() > 0) cmd += path + ':';
79 cmd += varenv;
80 putenv(const_cast<char *>(cmd.c_str()));
81 }
82#elif defined(AIX)
83 string cmd = "LIBPATH=";
84 char* varenv=NULL;
85 varenv=getenv("LIBPATH");
86 if (varenv == NULL) {
87 cmd += '.';
88 if (path.length() > 0) cmd += ':' + path;
89 cmd += ":/usr/lib:/lib";
90 }
91 else {
92 if (varenv[0] != '.') cmd += ".:";
93 if (path.length() > 0) cmd += path + ':';
94 cmd += varenv;
95 putenv(const_cast<char *>(cmd.c_str()));
96 }
97
98#endif
99return;
100}
101
102/* --Methode-Static-- */
103/*! Returns the temporary space path */
104string& PDynLinkMgr::GetTmpDir()
105{
106if (tmpDir == NULL) {
107 tmpDir = new string("");
108 char* varenv;
109 if ( (varenv=getenv("TMPDIR")) != NULL ) {
110 *tmpDir = varenv;
111 if ((*tmpDir)[tmpDir->length()-1] != '/') (*tmpDir) += '/';
112 }
113}
114return(*tmpDir);
115}
116
117/* --Methode-Static-- */
118/*! Compiles the C source file named \b fname and creates the
119 corresponding shared library linking against the standard
120 C library (-lc) and the math library (-lm).
121 Returns a pointer to the created PDynLinkMgr object (by new).
122 Returns the NULL pointer in case of errors.
123*/
124PDynLinkMgr* PDynLinkMgr::BuildFromCFile(string const & fname)
125{
126size_t l = fname.length();
127if (l < 1) return(NULL);
128string fnameobj = GetTmpDir()+"tmp_pdl.o";
129
130string cmd;
131int rc;
132
133// Compilation du fichier
134#ifndef __mac__
135#ifdef SGI_ARCH64
136cmd = "cc -64 -c -o " + fnameobj + " " + fname;
137#else
138cmd = "cc -c -o " + fnameobj + " " + fname;
139#endif
140#else
141cmd = "Il faut compiler !!!" + fnameobj + " " + fname;
142#endif
143rc = system(cmd.c_str());
144if (rc != 0) {
145 cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
146 return(NULL);
147 }
148
149char buff[32];
150numSO++;
151#ifndef HPUX
152sprintf(buff,"pdlmgr%d%s", numSO,sofext);
153#endif
154string fnameso = GetTmpDir()+buff;
155
156// Creation du shared-lib
157#if defined(OSF1)
158 cmd = "ld -shared -o " + fnameso + " -all " + fnameobj + " -none -lm -lc";
159#elif defined(Linux)
160 cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
161#elif defined(SunOS)
162 cmd = "ld -G -o " + fnameso + " " + fnameobj + " -lm -lc";
163#elif defined(IRIX64)
164#ifdef SGI_ARCH64
165 cmd = "ld -64 -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
166#else
167 cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
168#endif
169#elif defined(AIX)
170 cmd = "ld -G -bnogc -bexpall -bM:1L -o " + fnameso + " " + fnameobj;
171#elif defined(HPUX)
172 cmd = "ld -b -o " + fnameso + " " + fnameobj + " -lm -lc";
173#else
174 cmd = "ld -o " + fnameso + " " + fnameobj + " -lm -lc";
175#endif
176rc = system(cmd.c_str());
177if (rc != 0) {
178 cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
179 return(NULL);
180 }
181PDynLinkMgr* rdyn = new PDynLinkMgr(fnameso, false);
182rdyn->copy = true;
183return(rdyn);
184
185}
186
187/* --Methode-- */
188/*! The constructor.
189 \param soname : Name of the shared library. ".so" is appended
190 to the name if no dot "." is found in the name.
191 \param cp : if true, copies the shared library in the temporary space.
192*/
193PDynLinkMgr::PDynLinkMgr(string& soname, bool cp)
194{
195dlhandle = NULL;
196soName = "";
197
198if (soname.find_last_of(".") > soname.length())
199#ifdef HPUX
200 soname += sofext_HPUX;
201#else
202 soname += sofext;
203#endif
204
205string fnameso;
206if (cp) {
207 numSO++;
208 char buff[32];
209#ifndef HPUX
210 sprintf(buff,"pdlmgr%d%s", numSO,sofext);
211#else
212 sprintf(buff,"pdlmgr%d%s", numSO,sofext_HPUX);
213#endif
214 fnameso = GetTmpDir()+buff;
215 string cmd = "cp " + soname + " " + fnameso;
216 int rc = system(cmd.c_str());
217 if (rc != 0) {
218 cerr << "PDynLinkMgr::PDynLinkMgr() Error Rc(" << cmd <<")= "<< rc << endl;
219 return;
220 }
221 }
222else fnameso = soname;
223copy = cp;
224soName = fnameso;
225
226#if defined(HPUX)
227dlhandle = NULL;
228cerr << "PDynLinkMgr::PDynLinkMgr() Not yet available on HP-UX " << endl;
229return;
230#else
231dlhandle = dlopen(fnameso.c_str(), RTLD_NOW);
232if (dlhandle == NULL) {
233 cerr << "PDynLinkMgr::PDynLinkMgr(): Error opening SO " << fnameso
234 << " (" << soname << ")" << endl;
235 string sn = dlerror();
236 cerr << "Loader Error (dlerror()) :" << sn << endl;
237 return;
238 }
239#endif
240}
241
242/* --Methode-- */
243/*! Destructor. Closes the shared library. Removes the file if it had been
244 copied in the temporary space, or generated by \b BuildFromCFile */
245PDynLinkMgr::~PDynLinkMgr()
246{
247#if defined(HPUX)
248cerr << "PDynLinkMgr::~PDynLinkMgr() Not yet available on HP-UX " << endl;
249return;
250#else
251if (dlhandle) dlclose(dlhandle); dlhandle = NULL;
252if (copy) {
253 string cmd = "rm -f " + soName;
254 system(cmd.c_str());
255 }
256#endif
257}
258
259/* --Methode-- */
260/*! Returns a handle to the function named \b funcname.
261 Returns the NULL pointer in case of error */
262DlFunction PDynLinkMgr::GetFunction(string const & funcname)
263{
264DlFunction f = NULL;
265#if defined(HPUX)
266cerr << "PDynLinkMgr::GetFunction() Not yet available on HP-UX " << endl;
267return f;
268#else
269if (dlhandle != NULL)
270 f = (DlFunction)dlsym(dlhandle, funcname.c_str());
271if (f == NULL) cerr << "PDynLinkMgr::GetFunction(): Error linking " << funcname << endl;
272return(f);
273#endif
274}
275
276#endif
Note: See TracBrowser for help on using the repository browser.