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