| 1 | // Gestionnaire de compilation-linker C++ - R. Ansari 10/2000 | 
|---|
| 2 | // LAL (Orsay) / IN2P3-CNRS  DAPNIA/SPP (Saclay) / CEA | 
|---|
| 3 |  | 
|---|
| 4 | #include "sopnamsp.h" | 
|---|
| 5 | #include "cxxcmplnk.h" | 
|---|
| 6 | #include <iostream> | 
|---|
| 7 |  | 
|---|
| 8 | /*! | 
|---|
| 9 | \class SOPHYA::CxxCompilerLinker | 
|---|
| 10 | \ingroup SysTools | 
|---|
| 11 | This classes handles the compilation of a C++ source code and | 
|---|
| 12 | building of a shared library. | 
|---|
| 13 | The present version has been adapted for different compilers and | 
|---|
| 14 | systems: | 
|---|
| 15 | - Linux + GNU compiler (g++) | 
|---|
| 16 | - Linux + Intel compiler (icc) | 
|---|
| 17 | - Darwin + GNU compiler (MaxOS X 10.3 and 10.4) | 
|---|
| 18 | - OSF1 + cxx  (HP/Compaq/Digital OSF-Tru64 and cxx c++ compiler) | 
|---|
| 19 | - IRIX64 + CC : Silicon Graphics  system and C++ compiler | 
|---|
| 20 |  | 
|---|
| 21 | \sa SOPHYA::PDynLinkMgr | 
|---|
| 22 |  | 
|---|
| 23 | \code | 
|---|
| 24 | #include "cxxcmplnk.h" | 
|---|
| 25 | CxxCompilerLinker cxx; | 
|---|
| 26 | string name = "toto.cc"; | 
|---|
| 27 | string oname = "toto.o"; | 
|---|
| 28 | string soname = "toto.so"; | 
|---|
| 29 | int rc; | 
|---|
| 30 | // compiling file | 
|---|
| 31 | rc = cxx.Compile(name, oname); | 
|---|
| 32 | // linking and building the shared object | 
|---|
| 33 | rc = cxx.BuildSO(oname, soname); | 
|---|
| 34 | \endcode | 
|---|
| 35 | */ | 
|---|
| 36 |  | 
|---|
| 37 | static char * gcxx_opt = | 
|---|
| 38 | "-O -fdollars-in-identifiers"; | 
|---|
| 39 | static char * KCC_opt = | 
|---|
| 40 | "-O --exceptions --rtti  --auto_instantiation --one_instantiation_per_object -D__KCC__"; | 
|---|
| 41 | static char * icc_opt = | 
|---|
| 42 | "-O -fpic -frtti"; | 
|---|
| 43 | static char * cxx_opt = | 
|---|
| 44 | "-O -no_implicit_include -pthread"; | 
|---|
| 45 | static char * SGICC_opt =  "-O -prelink -D__SGICC__ -LANG:std"; | 
|---|
| 46 |  | 
|---|
| 47 | /* --Methode-- */ | 
|---|
| 48 | /*! | 
|---|
| 49 | Constructor | 
|---|
| 50 | \param fglibsophya : if \c true libsophya.so is used when linking | 
|---|
| 51 | \param fglibextsophya : if \c true libextsophya.so is used when linking | 
|---|
| 52 | \param fglibpi : if \c true libPI.so is used when linking | 
|---|
| 53 | */ | 
|---|
| 54 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi) | 
|---|
| 55 | : verbose(false) | 
|---|
| 56 | { | 
|---|
| 57 |  | 
|---|
| 58 | string syscomp = ""; | 
|---|
| 59 |  | 
|---|
| 60 | #if defined(OSF1) | 
|---|
| 61 | cppFlags += "-DOSF1" ; | 
|---|
| 62 | syscomp = "OSF1-"; | 
|---|
| 63 | #elif defined(Linux) | 
|---|
| 64 | cppFlags += "-DLinux" ; | 
|---|
| 65 | syscomp = "Linux-"; | 
|---|
| 66 | #elif defined(SunOS) | 
|---|
| 67 | cppFlags += "-DSunOS" ; | 
|---|
| 68 | syscomp = "SunOS-"; | 
|---|
| 69 | #elif defined(IRIX64) | 
|---|
| 70 | cppFlags += "-DIRIX64" ; | 
|---|
| 71 | syscomp = "IRIX64-"; | 
|---|
| 72 | #elif defined(AIX) | 
|---|
| 73 | cppFlags += "-DAIX" ; | 
|---|
| 74 | syscomp = "AIX-"; | 
|---|
| 75 | #elif defined(HPUX) | 
|---|
| 76 | cppFlags += "-DHPUX" ; | 
|---|
| 77 | syscomp = "HPUX-"; | 
|---|
| 78 | #elif defined(Darwin) | 
|---|
| 79 | cppFlags += "-DDarwin"; | 
|---|
| 80 | syscomp = "Darwin-"; | 
|---|
| 81 | #endif | 
|---|
| 82 |  | 
|---|
| 83 |  | 
|---|
| 84 | #if defined( __GNUG__ ) | 
|---|
| 85 | compCmd = "g++ "; | 
|---|
| 86 | compOptions = gcxx_opt; | 
|---|
| 87 | linkOptions = "-O -shared"; | 
|---|
| 88 | syscomp += "g++/"; | 
|---|
| 89 | #endif | 
|---|
| 90 | #if defined( __DECCXX ) | 
|---|
| 91 | compCmd = "cxx "; | 
|---|
| 92 | compOptions = cxx_opt; | 
|---|
| 93 | linkOptions = compOptions + " -shared"; | 
|---|
| 94 | syscomp += "cxx/"; | 
|---|
| 95 | #endif | 
|---|
| 96 | #if defined( __KCC__ ) | 
|---|
| 97 | compCmd = "KCC "; | 
|---|
| 98 | compOptions = KCC_opt; | 
|---|
| 99 | linkOptions = KCC_opt; | 
|---|
| 100 | syscomp += "KCC/"; | 
|---|
| 101 | #endif | 
|---|
| 102 | #if defined( __SGICC__ ) | 
|---|
| 103 | compCmd = "CC "; | 
|---|
| 104 | compOptions = SGICC_opt; | 
|---|
| 105 | linkOptions = "-shared -O -LANG:std"; | 
|---|
| 106 | syscomp += "CC/"; | 
|---|
| 107 | #ifdef SGI_ARCH64 | 
|---|
| 108 | compOptions += " -64 -DSGI_ARCH64 "; | 
|---|
| 109 | linkOptions += " -64 "; | 
|---|
| 110 | #endif | 
|---|
| 111 | #endif | 
|---|
| 112 | #if defined( __INTEL_COMPILER ) | 
|---|
| 113 | compCmd = "icpc "; | 
|---|
| 114 | compOptions = icc_opt; | 
|---|
| 115 | linkOptions = icc_opt + string(" -shared"); | 
|---|
| 116 | syscomp += "icc/"; | 
|---|
| 117 | #endif | 
|---|
| 118 |  | 
|---|
| 119 | #ifndef Darwin | 
|---|
| 120 | linkCmd = compCmd; | 
|---|
| 121 | #else | 
|---|
| 122 | linkCmd = "c++ -bundle "; | 
|---|
| 123 | linkOptions = "-lSystem -lm"; | 
|---|
| 124 | #endif | 
|---|
| 125 |  | 
|---|
| 126 | cppFlags += " -I. "; | 
|---|
| 127 | compOptions += " -c "; | 
|---|
| 128 |  | 
|---|
| 129 | bool fgenv1 = false; | 
|---|
| 130 | bool fgenv2 = false; | 
|---|
| 131 | char* varenv=NULL; | 
|---|
| 132 | varenv=getenv("SOPHYABASE"); | 
|---|
| 133 | if (varenv) { | 
|---|
| 134 | fgenv1 = true; | 
|---|
| 135 | string sbaserep = varenv; | 
|---|
| 136 | if (sbaserep[sbaserep.length()-1] != '/')  sbaserep += '/'; | 
|---|
| 137 | cppFlags += ( " -I" + sbaserep + "include/ "); | 
|---|
| 138 | linkOptions += " -L" + sbaserep + "slb/"; | 
|---|
| 139 | } | 
|---|
| 140 | varenv=getenv("SOPHYABASEREP"); | 
|---|
| 141 | if (varenv) { | 
|---|
| 142 | fgenv2 = true; | 
|---|
| 143 | string dpcbase = varenv; | 
|---|
| 144 | if (dpcbase[dpcbase.length()-1] != '/')  dpcbase += '/'; | 
|---|
| 145 | cppFlags += ( " -I" + dpcbase + "Include/ "); | 
|---|
| 146 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/"; | 
|---|
| 147 | char * varenvextl=getenv("EXTLIBDIR"); | 
|---|
| 148 | if (varenvextl) { | 
|---|
| 149 | string extlib = varenvextl; | 
|---|
| 150 | if (extlib[extlib.length()-1] != '/')  extlib += '/'; | 
|---|
| 151 | cppFlags += ( " -I" + extlib +"Include/ "); | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 | if (!fgenv1 && !fgenv2) | 
|---|
| 155 | cout << " CxxCompilerLinker()/Warning : SOPHYABASE not defined" << endl; | 
|---|
| 156 | if (fgenv1 && fgenv2) | 
|---|
| 157 | cout << " CxxCompilerLinker()/Warning : both SOPHYABASE and SOPHYABASEREP defined" << endl; | 
|---|
| 158 |  | 
|---|
| 159 | if (fglibsophya) linkOptions += " -lsophya "; | 
|---|
| 160 | if (fglibextsophya) linkOptions += " -lextsophya "; | 
|---|
| 161 | if (fglibpi) linkOptions += " -lPI "; | 
|---|
| 162 | linkOptions += " -lm "; | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 | if ( (varenv=getenv("TMPDIR")) != NULL )  { | 
|---|
| 166 | tmpDir = varenv; | 
|---|
| 167 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/'; | 
|---|
| 168 | } | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | /* --Methode-- */ | 
|---|
| 172 | CxxCompilerLinker::~CxxCompilerLinker() | 
|---|
| 173 | { | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | /* --Methode-- */ | 
|---|
| 177 | /*! | 
|---|
| 178 | Compiles the file \c name using the C++ compiler driver and produces | 
|---|
| 179 | the output object file \c oname. If no output name is specified, | 
|---|
| 180 | a default output file name is made from the input name, with the | 
|---|
| 181 | suffix .o , the in temporary directory. | 
|---|
| 182 | \param name : input C++ source file | 
|---|
| 183 | \param oname : output object file | 
|---|
| 184 | */ | 
|---|
| 185 | int CxxCompilerLinker::Compile(string const & name, string & oname) | 
|---|
| 186 | { | 
|---|
| 187 | if (oname.length() < 1) { | 
|---|
| 188 | size_t l,p,q; | 
|---|
| 189 | l = name.length(); | 
|---|
| 190 | p = name.rfind('/'); | 
|---|
| 191 | if (p >= l)  p = 0; | 
|---|
| 192 | else p++; | 
|---|
| 193 | q = name.rfind('.'); | 
|---|
| 194 | if ((q < l) && (q > p))  oname = tmpDir + name.substr(p, q-p) + ".o"; | 
|---|
| 195 | else oname = tmpDir + name.substr(p) + ".o"; | 
|---|
| 196 | } | 
|---|
| 197 | string cmd; | 
|---|
| 198 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ; | 
|---|
| 199 | if (verbose) | 
|---|
| 200 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl; | 
|---|
| 201 | int rc = system(cmd.c_str()); | 
|---|
| 202 | if (rc != 0) | 
|---|
| 203 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 204 |  | 
|---|
| 205 | return(rc); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | /* --Methode-- */ | 
|---|
| 209 | /*! | 
|---|
| 210 | Creates a shared library from the object file \c oname. | 
|---|
| 211 | If no output name \c soname is specified, | 
|---|
| 212 | a default output file name is made from the object name, with the | 
|---|
| 213 | suffix .so , in the temporary directory. | 
|---|
| 214 | \param oname : input object file | 
|---|
| 215 | \param soname : shared library name | 
|---|
| 216 | */ | 
|---|
| 217 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname) | 
|---|
| 218 | { | 
|---|
| 219 | //  char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002 | 
|---|
| 220 | char * soext = ".so"; | 
|---|
| 221 |  | 
|---|
| 222 | if (soname.length() < 1) { | 
|---|
| 223 | size_t l,p,q; | 
|---|
| 224 | l = oname.length(); | 
|---|
| 225 | p = oname.rfind('/'); | 
|---|
| 226 | if (p >= l)  p = 0; | 
|---|
| 227 | else p++; | 
|---|
| 228 | q = oname.rfind('.'); | 
|---|
| 229 | if ((q < l) && (q > p))  soname = tmpDir + oname.substr(p, q-p) + soext; | 
|---|
| 230 | else soname = tmpDir + oname.substr(p) + soext; | 
|---|
| 231 | } | 
|---|
| 232 | string cmd; | 
|---|
| 233 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ; | 
|---|
| 234 | if (verbose) | 
|---|
| 235 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl; | 
|---|
| 236 | int rc = system(cmd.c_str()); | 
|---|
| 237 | if (rc != 0) | 
|---|
| 238 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 239 |  | 
|---|
| 240 | return(rc); | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|