[219] | 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
|
---|
| 13 | static const char* sofext = ".so";
|
---|
| 14 | static const char* sofext_HPUX = ".sl";
|
---|
| 15 |
|
---|
| 16 | // Variables et methodes static
|
---|
| 17 | int PDynLinkMgr::numSO = 0;
|
---|
| 18 | string* PDynLinkMgr::tmpDir = NULL;
|
---|
| 19 |
|
---|
[895] | 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 |
|
---|
[219] | 27 | /* --Methode-Static-- */
|
---|
[895] | 28 | /*! Sets the path for a temporary space where shared libraries are copied.
|
---|
| 29 | The path is appended to \b LD_LIBRARY_PATH
|
---|
| 30 | */
|
---|
[219] | 31 | void PDynLinkMgr::SetTmpDir(string const & path)
|
---|
| 32 | {
|
---|
| 33 | if ( (path.length() > 0) && (path[path.length()] != '/') ) GetTmpDir() = path + '/';
|
---|
| 34 | else GetTmpDir() = path;
|
---|
[480] | 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
|
---|
[219] | 71 | return;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /* --Methode-Static-- */
|
---|
[895] | 75 | /*! Returns the temporary space path */
|
---|
[219] | 76 | string& PDynLinkMgr::GetTmpDir()
|
---|
| 77 | {
|
---|
| 78 | if (tmpDir == NULL) {
|
---|
| 79 | tmpDir = new string("");
|
---|
| 80 | char* varenv;
|
---|
[895] | 81 | if ( (varenv=getenv("SOPHYA_TMP")) != NULL ) *tmpDir = varenv;
|
---|
[219] | 82 | else if ( (varenv=getenv("TMPDIR")) != NULL ) *tmpDir = varenv;
|
---|
| 83 | }
|
---|
| 84 | return(*tmpDir);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /* --Methode-Static-- */
|
---|
[895] | 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 | */
|
---|
[219] | 94 | PDynLinkMgr* PDynLinkMgr::BuildFromCFile(string const & fname)
|
---|
| 95 | {
|
---|
| 96 | size_t l = fname.length();
|
---|
| 97 | if (l < 1) return(NULL);
|
---|
| 98 | string fnameobj = GetTmpDir()+"tmp_pdl.o";
|
---|
| 99 |
|
---|
| 100 | string cmd;
|
---|
| 101 | int rc;
|
---|
| 102 |
|
---|
| 103 | // Compilation du fichier
|
---|
[480] | 104 | #ifndef __mac__
|
---|
[219] | 105 | cmd = "cc -c -o " + fnameobj + " " + fname;
|
---|
[480] | 106 | #else
|
---|
| 107 | cmd = "Il faut compiler !!!" + fnameobj + " " + fname;
|
---|
[219] | 108 | #endif
|
---|
| 109 | rc = system(cmd.c_str());
|
---|
| 110 | if (rc != 0) {
|
---|
| 111 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 112 | return(NULL);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | char buff[32];
|
---|
| 116 | numSO++;
|
---|
| 117 | #ifndef HPUX
|
---|
| 118 | sprintf(buff,"pdlmgr%d%s", numSO,sofext);
|
---|
| 119 | #endif
|
---|
| 120 | string fnameso = GetTmpDir()+buff;
|
---|
| 121 |
|
---|
| 122 | // Creation du shared-lib
|
---|
| 123 | #if defined(OSF1)
|
---|
| 124 | cmd = "ld -shared -o " + fnameso + " -all " + fnameobj + " -none -lm -lc";
|
---|
[480] | 125 | #elif defined(Linux)
|
---|
[219] | 126 | cmd = "ld -shared -o " + fnameso + " " + fnameobj + " -lm -lc";
|
---|
[480] | 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";
|
---|
[219] | 137 | #endif
|
---|
| 138 | rc = system(cmd.c_str());
|
---|
| 139 | if (rc != 0) {
|
---|
| 140 | cerr << "PDynLinkMgr::BuildFromCFile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 141 | return(NULL);
|
---|
| 142 | }
|
---|
[296] | 143 | PDynLinkMgr* rdyn = new PDynLinkMgr(fnameso, false);
|
---|
| 144 | rdyn->copy = true;
|
---|
| 145 | return(rdyn);
|
---|
[219] | 146 |
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | /* --Methode-- */
|
---|
[895] | 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 | */
|
---|
[219] | 155 | PDynLinkMgr::PDynLinkMgr(string& soname, bool cp)
|
---|
| 156 | {
|
---|
| 157 | dlhandle = NULL;
|
---|
| 158 | soName = "";
|
---|
| 159 |
|
---|
| 160 | if (soname.find_last_of(".") > soname.length())
|
---|
| 161 | #ifdef HPUX
|
---|
| 162 | soname += sofext_HPUX;
|
---|
| 163 | #else
|
---|
| 164 | soname += sofext;
|
---|
| 165 | #endif
|
---|
| 166 |
|
---|
| 167 | string fnameso;
|
---|
| 168 | if (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 | }
|
---|
| 184 | else fnameso = soname;
|
---|
| 185 | copy = cp;
|
---|
| 186 | soName = fnameso;
|
---|
| 187 |
|
---|
[480] | 188 | #if defined(HPUX)
|
---|
| 189 | dlhandle = NULL;
|
---|
| 190 | cerr << "PDynLinkMgr::PDynLinkMgr() Not yet available on HP-UX " << endl;
|
---|
| 191 | return;
|
---|
| 192 | #else
|
---|
[219] | 193 | dlhandle = dlopen(fnameso.c_str(), RTLD_NOW);
|
---|
| 194 | if (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-- */
|
---|
[895] | 205 | /*! Destructor. Closes the shared library. Removes the file if it had been
|
---|
| 206 | copied in the temporary space, or generated by \b BuildFromCFile */
|
---|
[219] | 207 | PDynLinkMgr::~PDynLinkMgr()
|
---|
| 208 | {
|
---|
[480] | 209 | #if defined(HPUX)
|
---|
| 210 | cerr << "PDynLinkMgr::~PDynLinkMgr() Not yet available on HP-UX " << endl;
|
---|
| 211 | return;
|
---|
| 212 | #else
|
---|
[219] | 213 | if (dlhandle) dlclose(dlhandle); dlhandle = NULL;
|
---|
| 214 | if (copy) {
|
---|
[296] | 215 | string cmd = "rm -f " + soName;
|
---|
[219] | 216 | system(cmd.c_str());
|
---|
| 217 | }
|
---|
[480] | 218 | #endif
|
---|
[219] | 219 | }
|
---|
| 220 |
|
---|
| 221 | /* --Methode-- */
|
---|
[895] | 222 | /*! Returns a handle to the function named \b funcname.
|
---|
| 223 | Returns the NULL pointer in case of error */
|
---|
[219] | 224 | DlFunction PDynLinkMgr::GetFunction(string const & funcname)
|
---|
| 225 | {
|
---|
| 226 | DlFunction f = NULL;
|
---|
[480] | 227 | #if defined(HPUX)
|
---|
| 228 | cerr << "PDynLinkMgr::GetFunction() Not yet available on HP-UX " << endl;
|
---|
| 229 | return f;
|
---|
| 230 | #else
|
---|
[219] | 231 | if (dlhandle != NULL)
|
---|
| 232 | f = (DlFunction)dlsym(dlhandle, funcname.c_str());
|
---|
[480] | 233 | if (f == NULL) cerr << "PDynLinkMgr::GetFunction(): Error linking " << funcname << endl;
|
---|
| 234 | return(f);
|
---|
[219] | 235 | #endif
|
---|
| 236 | }
|
---|
| 237 |
|
---|