| 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>
 | 
|---|
| 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 | /*!
 | 
|---|
| 39 |   Constructor
 | 
|---|
| 40 |   \param fglibsophya : if \c true libsophya.so is used when linking
 | 
|---|
| 41 |   \param fglibextsophya : if \c true libextsophya.so is used when linking
 | 
|---|
| 42 |   \param fglibpi : if \c true libPI.so is used when linking
 | 
|---|
| 43 | */
 | 
|---|
| 44 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
 | 
|---|
| 45 |   : verbose(false)
 | 
|---|
| 46 | {
 | 
|---|
| 47 |   
 | 
|---|
| 48 |   string syscomp = "";
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #if defined(OSF1) 
 | 
|---|
| 51 |   cppFlags += "-DOSF1" ; 
 | 
|---|
| 52 |   syscomp = "OSF1-";
 | 
|---|
| 53 | #elif defined(Linux)
 | 
|---|
| 54 |   cppFlags += "-DLinux" ;
 | 
|---|
| 55 |   syscomp = "Linux-"; 
 | 
|---|
| 56 | #elif defined(SunOS)
 | 
|---|
| 57 |   cppFlags += "-DSunOS" ;
 | 
|---|
| 58 |   syscomp = "SunOS-";  
 | 
|---|
| 59 | #elif defined(IRIX64)
 | 
|---|
| 60 |   cppFlags += "-DIRIX64" ;
 | 
|---|
| 61 |   syscomp = "IRIX64-";  
 | 
|---|
| 62 | #elif defined(AIX)
 | 
|---|
| 63 |   cppFlags += "-DAIX" ;
 | 
|---|
| 64 |   syscomp = "AIX-";  
 | 
|---|
| 65 | #elif defined(HPUX)
 | 
|---|
| 66 |   cppFlags += "-DHPUX" ;
 | 
|---|
| 67 |   syscomp = "HPUX-";  
 | 
|---|
| 68 | #elif defined(Darwin)
 | 
|---|
| 69 |   cppFlags += "-DDarwin";
 | 
|---|
| 70 |   syscomp = "Darwin-";
 | 
|---|
| 71 | #endif
 | 
|---|
| 72 | 
 | 
|---|
| 73 | 
 | 
|---|
| 74 | #if defined( __GNUG__ )
 | 
|---|
| 75 |   compCmd = "g++ ";
 | 
|---|
| 76 |   compOptions = gcxx_opt;
 | 
|---|
| 77 |   linkOptions = "-O -shared";
 | 
|---|
| 78 |   syscomp += "g++/"; 
 | 
|---|
| 79 | #elif defined( __DECCXX )
 | 
|---|
| 80 |   compCmd = "cxx ";
 | 
|---|
| 81 |   compOptions = cxx_opt;
 | 
|---|
| 82 |   linkOptions = compOptions + "-shared";
 | 
|---|
| 83 |   syscomp += "cxx/"; 
 | 
|---|
| 84 | #elif defined( __KCC__ )
 | 
|---|
| 85 |   compCmd = "KCC ";
 | 
|---|
| 86 |   compOptions = KCC_opt;
 | 
|---|
| 87 |   linkOptions = KCC_opt;
 | 
|---|
| 88 |   syscomp += "KCC/";  
 | 
|---|
| 89 | #elif defined( __SGICC__ )
 | 
|---|
| 90 |   compCmd = "CC ";
 | 
|---|
| 91 |   compOptions = SGICC_opt;
 | 
|---|
| 92 |   linkOptions = "-shared -O ";
 | 
|---|
| 93 |   syscomp += "CC/";  
 | 
|---|
| 94 | #ifdef SGI_ARCH64
 | 
|---|
| 95 |   compOptions += " -64 -DSGI_ARCH64 ";
 | 
|---|
| 96 |   linkOptions += " -64 "; 
 | 
|---|
| 97 | #endif
 | 
|---|
| 98 | #endif
 | 
|---|
| 99 | 
 | 
|---|
| 100 | #ifndef Darwin
 | 
|---|
| 101 |   linkCmd = compCmd;
 | 
|---|
| 102 | #else
 | 
|---|
| 103 |   linkCmd = "cc -bundle -flat_namespace -undefined suppress ";
 | 
|---|
| 104 |   linkOptions = "-L/usr/lib/gcc/darwin/default -lstdc++ -lcc_dynamic -lpthread ";
 | 
|---|
| 105 | #endif
 | 
|---|
| 106 | 
 | 
|---|
| 107 |   cppFlags += " -I. ";
 | 
|---|
| 108 |   compOptions += " -c ";
 | 
|---|
| 109 | 
 | 
|---|
| 110 |   string dpcbase;
 | 
|---|
| 111 |   char* varenv=NULL;
 | 
|---|
| 112 |   varenv=getenv("DPCBASEREP");
 | 
|---|
| 113 |   if (varenv) {
 | 
|---|
| 114 |     dpcbase = varenv; 
 | 
|---|
| 115 |     if (dpcbase[dpcbase.length()-1] != '/')  dpcbase += '/';
 | 
|---|
| 116 |     cppFlags += ( " -I" + dpcbase + "Include/ ");
 | 
|---|
| 117 |     linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
 | 
|---|
| 118 |     if (fglibsophya) linkOptions += " -lsophya ";
 | 
|---|
| 119 |     if (fglibextsophya) linkOptions += " -lextsophya ";
 | 
|---|
| 120 |     if (fglibpi) linkOptions += " -lPI ";
 | 
|---|
| 121 |   }
 | 
|---|
| 122 |   linkOptions += " -lm ";
 | 
|---|
| 123 | 
 | 
|---|
| 124 |   string extlib;
 | 
|---|
| 125 |   varenv=getenv("EXTLIBDIR");
 | 
|---|
| 126 |   if (varenv) {
 | 
|---|
| 127 |     extlib = varenv; 
 | 
|---|
| 128 |     if (extlib[extlib.length()-1] != '/')  extlib += '/';
 | 
|---|
| 129 |     cppFlags += ( " -I" + extlib +"Include/ ");
 | 
|---|
| 130 |   }
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   if ( (varenv=getenv("TMPDIR")) != NULL )  { 
 | 
|---|
| 133 |     tmpDir = varenv; 
 | 
|---|
| 134 |     if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
 | 
|---|
| 135 |   }
 | 
|---|
| 136 | }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /* --Methode-- */
 | 
|---|
| 139 | CxxCompilerLinker::~CxxCompilerLinker()
 | 
|---|
| 140 | {
 | 
|---|
| 141 | }
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /* --Methode-- */
 | 
|---|
| 144 | /*!
 | 
|---|
| 145 |   Compiles the file \c name using the C++ compiler driver and produces 
 | 
|---|
| 146 |   the output object file \c oname. If no output name is specified,
 | 
|---|
| 147 |   a default output file name is made from the input name, with the 
 | 
|---|
| 148 |   suffix .o , the in temporary directory.
 | 
|---|
| 149 |   \param name : input C++ source file
 | 
|---|
| 150 |   \param oname : output object file 
 | 
|---|
| 151 | */
 | 
|---|
| 152 | int CxxCompilerLinker::Compile(string const & name, string & oname)
 | 
|---|
| 153 | {
 | 
|---|
| 154 |   if (oname.length() < 1) {
 | 
|---|
| 155 |     size_t l,p,q;
 | 
|---|
| 156 |     l = name.length();
 | 
|---|
| 157 |     p = name.rfind('/');
 | 
|---|
| 158 |     if (p >= l)  p = 0;
 | 
|---|
| 159 |     else p++;
 | 
|---|
| 160 |     q = name.rfind('.');
 | 
|---|
| 161 |     if ((q < l) && (q > p))  oname = tmpDir + name.substr(p, q-p) + ".o";
 | 
|---|
| 162 |     else oname = tmpDir + name.substr(p) + ".o";
 | 
|---|
| 163 |   }
 | 
|---|
| 164 |   string cmd;
 | 
|---|
| 165 |   cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ; 
 | 
|---|
| 166 |   if (verbose)
 | 
|---|
| 167 |     cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl; 
 | 
|---|
| 168 |   int rc = system(cmd.c_str());
 | 
|---|
| 169 |   if (rc != 0)   
 | 
|---|
| 170 |     cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl; 
 | 
|---|
| 171 |   
 | 
|---|
| 172 |   return(rc);
 | 
|---|
| 173 | }
 | 
|---|
| 174 | 
 | 
|---|
| 175 | /* --Methode-- */
 | 
|---|
| 176 | /*!
 | 
|---|
| 177 |   Creates a shared library from the object file \c oname.
 | 
|---|
| 178 |   If no output name \c soname is specified,
 | 
|---|
| 179 |   a default output file name is made from the object name, with the 
 | 
|---|
| 180 |   suffix .so , in the temporary directory.
 | 
|---|
| 181 |   \param oname : input object file 
 | 
|---|
| 182 |   \param soname : shared library name
 | 
|---|
| 183 | */
 | 
|---|
| 184 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
 | 
|---|
| 185 | {
 | 
|---|
| 186 |   //  char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
 | 
|---|
| 187 |   char * soext = ".so";
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   if (soname.length() < 1) {
 | 
|---|
| 190 |     size_t l,p,q;
 | 
|---|
| 191 |     l = oname.length();
 | 
|---|
| 192 |     p = oname.rfind('/');
 | 
|---|
| 193 |     if (p >= l)  p = 0;
 | 
|---|
| 194 |     else p++;
 | 
|---|
| 195 |     q = oname.rfind('.');
 | 
|---|
| 196 |     if ((q < l) && (q > p))  soname = tmpDir + oname.substr(p, q-p) + soext;
 | 
|---|
| 197 |     else soname = tmpDir + oname.substr(p) + soext;
 | 
|---|
| 198 |   }
 | 
|---|
| 199 |   string cmd;
 | 
|---|
| 200 |   cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ; 
 | 
|---|
| 201 |   if (verbose)
 | 
|---|
| 202 |     cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl; 
 | 
|---|
| 203 |   int rc = system(cmd.c_str());
 | 
|---|
| 204 |   if (rc != 0)   
 | 
|---|
| 205 |     cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl; 
 | 
|---|
| 206 |   
 | 
|---|
| 207 |   return(rc);
 | 
|---|
| 208 | }
 | 
|---|
| 209 | 
 | 
|---|