| 1 | // Gestionnaire de compilation-linker C++ - R. Ansari 10/2000
 | 
|---|
| 2 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA
 | 
|---|
| 3 | 
 | 
|---|
| 4 | #include "cxxcmplnk.h"
 | 
|---|
| 5 | #include <iostream.h>
 | 
|---|
| 6 | 
 | 
|---|
| 7 | /*!
 | 
|---|
| 8 |   \class SOPHYA::CxxCompilerLinker
 | 
|---|
| 9 |   \ingroup SysTools
 | 
|---|
| 10 |   This classes handles the compilation of a C++ source code and 
 | 
|---|
| 11 |   building of a shared library.
 | 
|---|
| 12 |   The present version has been adapted for different compilers:
 | 
|---|
| 13 |   g++ , Compaq/Digital cxx , SGI CC , KCC .
 | 
|---|
| 14 |   \sa SOPHYA::PDynLinkMgr
 | 
|---|
| 15 |   \code
 | 
|---|
| 16 |   #include ""cxxcmplnk.h"
 | 
|---|
| 17 |   CxxCompilerLinker cxx;
 | 
|---|
| 18 |   string name = "toto.cc";
 | 
|---|
| 19 |   string oname = "toto.o";
 | 
|---|
| 20 |   string soname = "toto.so";
 | 
|---|
| 21 |   int rc;
 | 
|---|
| 22 |   // compiling file 
 | 
|---|
| 23 |   rc = cxx.Compile(name, oname);
 | 
|---|
| 24 |   // linking and building the shared object 
 | 
|---|
| 25 |   rc = cxx.BuildSO(oname, soname);
 | 
|---|
| 26 |   \endcode
 | 
|---|
| 27 | */
 | 
|---|
| 28 | 
 | 
|---|
| 29 | static char * gcxx_opt = 
 | 
|---|
| 30 | "-O -Wall -Wpointer-arith -Wmissing-prototypes -Wsynth -fdollars-in-identifiers";
 | 
|---|
| 31 | static char * KCC_opt = 
 | 
|---|
| 32 | "-O --exceptions --rtti  --auto_instantiation --one_instantiation_per_object -D__KCC__";
 | 
|---|
| 33 | static char * cxx_opt = 
 | 
|---|
| 34 | "-O -no_implicit_include ";
 | 
|---|
| 35 | static char * SGICC_opt =  "-O -prelink -D__SGICC__ ";
 | 
|---|
| 36 | 
 | 
|---|
| 37 | /* --Methode-- */
 | 
|---|
| 38 | CxxCompilerLinker::CxxCompilerLinker()
 | 
|---|
| 39 |   : verbose(false)
 | 
|---|
| 40 | {
 | 
|---|
| 41 |   
 | 
|---|
| 42 |   string syscomp = "";
 | 
|---|
| 43 | 
 | 
|---|
| 44 | #if defined(OSF1) 
 | 
|---|
| 45 |   cppFlags += "-DOSF1" ; 
 | 
|---|
| 46 |   syscomp = "OSF1-";
 | 
|---|
| 47 | #elif defined(Linux)
 | 
|---|
| 48 |   cppFlags += "-DLinux" ;
 | 
|---|
| 49 |   syscomp = "Linux-"; 
 | 
|---|
| 50 | #elif defined(SunOS)
 | 
|---|
| 51 |   cppFlags += "-DSunOS" ;
 | 
|---|
| 52 |   syscomp = "SunOS-";  
 | 
|---|
| 53 | #elif defined(IRIX64)
 | 
|---|
| 54 |   cppFlags += "-DIRIX64" ;
 | 
|---|
| 55 |   syscomp = "IRIX64-";  
 | 
|---|
| 56 | #elif defined(AIX)
 | 
|---|
| 57 |   cppFlags += "-DAIX" ;
 | 
|---|
| 58 |   syscomp = "AIX-";  
 | 
|---|
| 59 | #elif defined(HPUX)
 | 
|---|
| 60 |   cppFlags += "-DHPUX" ;
 | 
|---|
| 61 |   syscomp = "HPUX-";  
 | 
|---|
| 62 | #endif
 | 
|---|
| 63 | 
 | 
|---|
| 64 | 
 | 
|---|
| 65 | #if defined( __GNUG__ )
 | 
|---|
| 66 |   compCmd = "g++ ";
 | 
|---|
| 67 |   compOptions = gcxx_opt;
 | 
|---|
| 68 |   linkOptions = "-O -shared";
 | 
|---|
| 69 |   syscomp += "g++/"; 
 | 
|---|
| 70 | #elif defined( __DECCXX )
 | 
|---|
| 71 |   compCmd = "cxx ";
 | 
|---|
| 72 |   compOptions = cxx_opt;
 | 
|---|
| 73 |   linkOptions = compOptions + "-shared";
 | 
|---|
| 74 |   syscomp += "cxx/"; 
 | 
|---|
| 75 | #elif defined( __KCC__ )
 | 
|---|
| 76 |   compCmd = "KCC ";
 | 
|---|
| 77 |   compOptions = KCC_opt;
 | 
|---|
| 78 |   linkOptions = KCC_opt;
 | 
|---|
| 79 |   syscomp += "KCC/";  
 | 
|---|
| 80 | #elif defined( __SGICC__ )
 | 
|---|
| 81 |   compCmd = "CC ";
 | 
|---|
| 82 |   compOptions = SGICC_opt;
 | 
|---|
| 83 |   linkOptions = "-shared -O ";
 | 
|---|
| 84 |   syscomp += "CC/";  
 | 
|---|
| 85 | #ifdef SGI_ARCH64
 | 
|---|
| 86 |   compOptions += " -64 -DSGI_ARCH64 ";
 | 
|---|
| 87 |   linkOptions += " -64 "; 
 | 
|---|
| 88 | #endif
 | 
|---|
| 89 | #endif
 | 
|---|
| 90 | 
 | 
|---|
| 91 |   cppFlags += " -I. ";
 | 
|---|
| 92 |   compOptions += " -c ";
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   string dpcbase;
 | 
|---|
| 95 |   char* varenv=NULL;
 | 
|---|
| 96 |   varenv=getenv("DPCBASEREP");
 | 
|---|
| 97 |   if (varenv) {
 | 
|---|
| 98 |     dpcbase = varenv; 
 | 
|---|
| 99 |     if (dpcbase[dpcbase.length()-1] != '/')  dpcbase += '/';
 | 
|---|
| 100 |     cppFlags += ( " -I" + dpcbase + "Include/ ");
 | 
|---|
| 101 |     linkOptions += " -L" + dpcbase + syscomp + "ShLibs/ -lextsophya -lsophya -lm "; 
 | 
|---|
| 102 |   }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   string extlib;
 | 
|---|
| 105 |   varenv=getenv("EXTLIBDIR");
 | 
|---|
| 106 |   if (varenv) {
 | 
|---|
| 107 |     extlib = varenv; 
 | 
|---|
| 108 |     if (extlib[extlib.length()-1] != '/')  extlib += '/';
 | 
|---|
| 109 |     cppFlags += ( " -I" + extlib +"Include/ ");
 | 
|---|
| 110 |   }
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   if ( (varenv=getenv("TMPDIR")) != NULL )  { 
 | 
|---|
| 113 |     tmpDir = varenv; 
 | 
|---|
| 114 |     if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
 | 
|---|
| 115 |   }
 | 
|---|
| 116 | }
 | 
|---|
| 117 | 
 | 
|---|
| 118 | /* --Methode-- */
 | 
|---|
| 119 | CxxCompilerLinker::~CxxCompilerLinker()
 | 
|---|
| 120 | {
 | 
|---|
| 121 | }
 | 
|---|
| 122 | 
 | 
|---|
| 123 | /* --Methode-- */
 | 
|---|
| 124 | int CxxCompilerLinker::Compile(string const & name, string & oname)
 | 
|---|
| 125 | {
 | 
|---|
| 126 |   if (oname.length() < 1) {
 | 
|---|
| 127 |     size_t l,p,q;
 | 
|---|
| 128 |     l = name.length();
 | 
|---|
| 129 |     p = name.rfind('/');
 | 
|---|
| 130 |     if (p >= l)  p = 0;
 | 
|---|
| 131 |     else p++;
 | 
|---|
| 132 |     q = name.find('.');
 | 
|---|
| 133 |     if (q < l)  oname = tmpDir + name.substr(p, q-p) + ".o";
 | 
|---|
| 134 |     else oname = tmpDir + name.substr(p) + ".o";
 | 
|---|
| 135 |   }
 | 
|---|
| 136 |   string cmd;
 | 
|---|
| 137 |   cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ; 
 | 
|---|
| 138 |   if (verbose)
 | 
|---|
| 139 |     cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl; 
 | 
|---|
| 140 |   int rc = system(cmd.c_str());
 | 
|---|
| 141 |   if (rc != 0)   
 | 
|---|
| 142 |     cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl; 
 | 
|---|
| 143 |   
 | 
|---|
| 144 |   return(rc);
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | /* --Methode-- */
 | 
|---|
| 148 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
 | 
|---|
| 149 | {
 | 
|---|
| 150 |   if (soname.length() < 1) {
 | 
|---|
| 151 |     size_t l,p,q;
 | 
|---|
| 152 |     l = oname.length();
 | 
|---|
| 153 |     p = oname.rfind('/');
 | 
|---|
| 154 |     if (p >= l)  p = 0;
 | 
|---|
| 155 |     else p++;
 | 
|---|
| 156 |     q = oname.find('.');
 | 
|---|
| 157 |     if (q < l)  soname = tmpDir + oname.substr(p, q-p) + ".so";
 | 
|---|
| 158 |     else soname = tmpDir + oname.substr(p) + ".so";
 | 
|---|
| 159 |   }
 | 
|---|
| 160 |   string cmd;
 | 
|---|
| 161 |   cmd = compCmd + linkOptions + "-o " + soname + " " + oname ; 
 | 
|---|
| 162 |   if (verbose)
 | 
|---|
| 163 |     cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl; 
 | 
|---|
| 164 |   int rc = system(cmd.c_str());
 | 
|---|
| 165 |   if (rc != 0)   
 | 
|---|
| 166 |     cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl; 
 | 
|---|
| 167 |   
 | 
|---|
| 168 |   return(rc);
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|