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

Last change on this file since 895 was 895, checked in by ansari, 25 years ago

Documentation de fichiers - Reza 12/4/2000

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