[1275] | 1 | // Gestionnaire de compilation-linker C++ - R. Ansari 10/2000
|
---|
| 2 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
| 3 |
|
---|
[2615] | 4 | #include "sopnamsp.h"
|
---|
[1275] | 5 | #include "cxxcmplnk.h"
|
---|
[2322] | 6 | #include <iostream>
|
---|
[1275] | 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.
|
---|
[2598] | 13 | The present version has been adapted for different compilers and
|
---|
| 14 | systems:
|
---|
[2796] | 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
|
---|
[2598] | 20 |
|
---|
[1275] | 21 | \sa SOPHYA::PDynLinkMgr
|
---|
[2598] | 22 |
|
---|
[1275] | 23 | \code
|
---|
[2269] | 24 | #include "cxxcmplnk.h"
|
---|
[1275] | 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 =
|
---|
[2727] | 38 | "-O -fdollars-in-identifiers";
|
---|
[1275] | 39 | static char * KCC_opt =
|
---|
| 40 | "-O --exceptions --rtti --auto_instantiation --one_instantiation_per_object -D__KCC__";
|
---|
[2796] | 41 | static char * icc_opt =
|
---|
| 42 | "-O -fpic -frtti";
|
---|
[1275] | 43 | static char * cxx_opt =
|
---|
[2796] | 44 | "-O -no_implicit_include -pthread";
|
---|
[2443] | 45 | static char * SGICC_opt = "-O -prelink -D__SGICC__ -LANG:std";
|
---|
[2867] | 46 | static char * xlC_opt =
|
---|
| 47 | "-O -qrtti=all -qeh=v6";
|
---|
[1275] | 48 |
|
---|
| 49 | /* --Methode-- */
|
---|
[2212] | 50 | /*!
|
---|
| 51 | Constructor
|
---|
| 52 | \param fglibsophya : if \c true libsophya.so is used when linking
|
---|
| 53 | \param fglibextsophya : if \c true libextsophya.so is used when linking
|
---|
| 54 | \param fglibpi : if \c true libPI.so is used when linking
|
---|
| 55 | */
|
---|
[1900] | 56 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
|
---|
[1275] | 57 | : verbose(false)
|
---|
| 58 | {
|
---|
| 59 |
|
---|
| 60 | string syscomp = "";
|
---|
| 61 |
|
---|
| 62 | #if defined(OSF1)
|
---|
| 63 | cppFlags += "-DOSF1" ;
|
---|
| 64 | syscomp = "OSF1-";
|
---|
| 65 | #elif defined(Linux)
|
---|
| 66 | cppFlags += "-DLinux" ;
|
---|
| 67 | syscomp = "Linux-";
|
---|
| 68 | #elif defined(SunOS)
|
---|
| 69 | cppFlags += "-DSunOS" ;
|
---|
| 70 | syscomp = "SunOS-";
|
---|
| 71 | #elif defined(IRIX64)
|
---|
| 72 | cppFlags += "-DIRIX64" ;
|
---|
| 73 | syscomp = "IRIX64-";
|
---|
| 74 | #elif defined(AIX)
|
---|
| 75 | cppFlags += "-DAIX" ;
|
---|
| 76 | syscomp = "AIX-";
|
---|
| 77 | #elif defined(HPUX)
|
---|
| 78 | cppFlags += "-DHPUX" ;
|
---|
| 79 | syscomp = "HPUX-";
|
---|
[1797] | 80 | #elif defined(Darwin)
|
---|
| 81 | cppFlags += "-DDarwin";
|
---|
| 82 | syscomp = "Darwin-";
|
---|
[1275] | 83 | #endif
|
---|
| 84 |
|
---|
| 85 |
|
---|
| 86 | #if defined( __GNUG__ )
|
---|
| 87 | compCmd = "g++ ";
|
---|
| 88 | compOptions = gcxx_opt;
|
---|
| 89 | linkOptions = "-O -shared";
|
---|
| 90 | syscomp += "g++/";
|
---|
[2796] | 91 | #endif
|
---|
| 92 | #if defined( __DECCXX )
|
---|
[1275] | 93 | compCmd = "cxx ";
|
---|
| 94 | compOptions = cxx_opt;
|
---|
[2796] | 95 | linkOptions = compOptions + " -shared";
|
---|
[1275] | 96 | syscomp += "cxx/";
|
---|
[2796] | 97 | #endif
|
---|
| 98 | #if defined( __KCC__ )
|
---|
[1275] | 99 | compCmd = "KCC ";
|
---|
| 100 | compOptions = KCC_opt;
|
---|
| 101 | linkOptions = KCC_opt;
|
---|
| 102 | syscomp += "KCC/";
|
---|
[2796] | 103 | #endif
|
---|
| 104 | #if defined( __SGICC__ )
|
---|
[1275] | 105 | compCmd = "CC ";
|
---|
| 106 | compOptions = SGICC_opt;
|
---|
[2443] | 107 | linkOptions = "-shared -O -LANG:std";
|
---|
[1275] | 108 | syscomp += "CC/";
|
---|
| 109 | #ifdef SGI_ARCH64
|
---|
| 110 | compOptions += " -64 -DSGI_ARCH64 ";
|
---|
| 111 | linkOptions += " -64 ";
|
---|
| 112 | #endif
|
---|
| 113 | #endif
|
---|
[2796] | 114 | #if defined( __INTEL_COMPILER )
|
---|
| 115 | compCmd = "icpc ";
|
---|
| 116 | compOptions = icc_opt;
|
---|
[2802] | 117 | linkOptions = icc_opt + string(" -shared");
|
---|
[2796] | 118 | syscomp += "icc/";
|
---|
| 119 | #endif
|
---|
[2867] | 120 | #if defined( __IBMCPP__ )
|
---|
| 121 | compCmd = "xlC ";
|
---|
| 122 | compOptions = xlC_opt;
|
---|
| 123 | linkOptions = xlC_opt + string(" -brtl -qmkshrobj ");
|
---|
| 124 | syscomp += "xlC/";
|
---|
| 125 | #endif
|
---|
[1275] | 126 |
|
---|
[1900] | 127 | #ifndef Darwin
|
---|
| 128 | linkCmd = compCmd;
|
---|
| 129 | #else
|
---|
[2496] | 130 | linkCmd = "c++ -bundle ";
|
---|
| 131 | linkOptions = "-lSystem -lm";
|
---|
[1900] | 132 | #endif
|
---|
| 133 |
|
---|
[1275] | 134 | cppFlags += " -I. ";
|
---|
| 135 | compOptions += " -c ";
|
---|
| 136 |
|
---|
[2727] | 137 | bool fgenv1 = false;
|
---|
| 138 | bool fgenv2 = false;
|
---|
[1275] | 139 | char* varenv=NULL;
|
---|
[2727] | 140 | varenv=getenv("SOPHYABASE");
|
---|
[2867] | 141 | string sbaserep = "./";
|
---|
[2727] | 142 | if (varenv) {
|
---|
| 143 | fgenv1 = true;
|
---|
[2867] | 144 | sbaserep = varenv;
|
---|
[2727] | 145 | if (sbaserep[sbaserep.length()-1] != '/') sbaserep += '/';
|
---|
| 146 | cppFlags += ( " -I" + sbaserep + "include/ ");
|
---|
| 147 | linkOptions += " -L" + sbaserep + "slb/";
|
---|
| 148 | }
|
---|
[2437] | 149 | varenv=getenv("SOPHYABASEREP");
|
---|
[1275] | 150 | if (varenv) {
|
---|
[2727] | 151 | fgenv2 = true;
|
---|
| 152 | string dpcbase = varenv;
|
---|
[1275] | 153 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
| 154 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
[1900] | 155 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
[2727] | 156 | char * varenvextl=getenv("EXTLIBDIR");
|
---|
| 157 | if (varenvextl) {
|
---|
| 158 | string extlib = varenvextl;
|
---|
[1275] | 159 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
| 160 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
[2727] | 161 | }
|
---|
[1275] | 162 | }
|
---|
[2727] | 163 | if (!fgenv1 && !fgenv2)
|
---|
| 164 | cout << " CxxCompilerLinker()/Warning : SOPHYABASE not defined" << endl;
|
---|
| 165 | if (fgenv1 && fgenv2)
|
---|
| 166 | cout << " CxxCompilerLinker()/Warning : both SOPHYABASE and SOPHYABASEREP defined" << endl;
|
---|
[1275] | 167 |
|
---|
[2727] | 168 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
| 169 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
[2867] | 170 | #if defined( AIX )
|
---|
| 171 | cout << " ++++==++ DBG/CxxCompilerLinker() - in AIX fglibpi= "
|
---|
| 172 | << ((fglibpi) ? " true " : " false ") << endl;
|
---|
| 173 | // Reza:Dec 2005 : pb avec les programmes PI sur AIX si linke avec shared lib PI
|
---|
| 174 | // (Portage sur regatta.calcul.u-psud.fr AIX 5.3 , libXm Xt X11 en .a uniquement)
|
---|
| 175 | if (fglibpi) {
|
---|
| 176 | linkOptions += " -L" + sbaserep + "lib/";
|
---|
| 177 | linkOptions += " -lPIext -lPIGcont -lPI -lXm -lXt -lX11 -lpthread ";
|
---|
| 178 | }
|
---|
| 179 | #else
|
---|
[2727] | 180 | if (fglibpi) linkOptions += " -lPI ";
|
---|
[2867] | 181 | #endif
|
---|
[2727] | 182 | linkOptions += " -lm ";
|
---|
| 183 |
|
---|
| 184 |
|
---|
[1277] | 185 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
| 186 | tmpDir = varenv;
|
---|
| 187 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
| 188 | }
|
---|
[1275] | 189 | }
|
---|
| 190 |
|
---|
| 191 | /* --Methode-- */
|
---|
| 192 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
| 193 | {
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /* --Methode-- */
|
---|
[2212] | 197 | /*!
|
---|
| 198 | Compiles the file \c name using the C++ compiler driver and produces
|
---|
| 199 | the output object file \c oname. If no output name is specified,
|
---|
| 200 | a default output file name is made from the input name, with the
|
---|
| 201 | suffix .o , the in temporary directory.
|
---|
| 202 | \param name : input C++ source file
|
---|
| 203 | \param oname : output object file
|
---|
| 204 | */
|
---|
[1277] | 205 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
[1275] | 206 | {
|
---|
[1277] | 207 | if (oname.length() < 1) {
|
---|
| 208 | size_t l,p,q;
|
---|
| 209 | l = name.length();
|
---|
| 210 | p = name.rfind('/');
|
---|
| 211 | if (p >= l) p = 0;
|
---|
| 212 | else p++;
|
---|
[2183] | 213 | q = name.rfind('.');
|
---|
[2190] | 214 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
[1277] | 215 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
[1275] | 216 | }
|
---|
| 217 | string cmd;
|
---|
[1277] | 218 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
[1275] | 219 | if (verbose)
|
---|
| 220 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
| 221 | int rc = system(cmd.c_str());
|
---|
| 222 | if (rc != 0)
|
---|
| 223 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 224 |
|
---|
| 225 | return(rc);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | /* --Methode-- */
|
---|
[2212] | 229 | /*!
|
---|
| 230 | Creates a shared library from the object file \c oname.
|
---|
| 231 | If no output name \c soname is specified,
|
---|
| 232 | a default output file name is made from the object name, with the
|
---|
| 233 | suffix .so , in the temporary directory.
|
---|
| 234 | \param oname : input object file
|
---|
| 235 | \param soname : shared library name
|
---|
| 236 | */
|
---|
[1277] | 237 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
[1275] | 238 | {
|
---|
[1900] | 239 | // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
| 240 | char * soext = ".so";
|
---|
| 241 |
|
---|
[1275] | 242 | if (soname.length() < 1) {
|
---|
[1277] | 243 | size_t l,p,q;
|
---|
| 244 | l = oname.length();
|
---|
| 245 | p = oname.rfind('/');
|
---|
| 246 | if (p >= l) p = 0;
|
---|
| 247 | else p++;
|
---|
[2183] | 248 | q = oname.rfind('.');
|
---|
[2190] | 249 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
[1900] | 250 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
[1275] | 251 | }
|
---|
| 252 | string cmd;
|
---|
[1900] | 253 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
[1275] | 254 | if (verbose)
|
---|
| 255 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
| 256 | int rc = system(cmd.c_str());
|
---|
| 257 | if (rc != 0)
|
---|
| 258 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 259 |
|
---|
| 260 | return(rc);
|
---|
| 261 | }
|
---|
| 262 |
|
---|