[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"
|
---|
[3014] | 6 | #include "sspvflags.h"
|
---|
[3619] | 7 | #include <stdlib.h>
|
---|
[2322] | 8 | #include <iostream>
|
---|
[1275] | 9 |
|
---|
| 10 | /*!
|
---|
| 11 | \class SOPHYA::CxxCompilerLinker
|
---|
| 12 | \ingroup SysTools
|
---|
| 13 | This classes handles the compilation of a C++ source code and
|
---|
| 14 | building of a shared library.
|
---|
[2598] | 15 | The present version has been adapted for different compilers and
|
---|
| 16 | systems:
|
---|
[2796] | 17 | - Linux + GNU compiler (g++)
|
---|
| 18 | - Linux + Intel compiler (icc)
|
---|
| 19 | - Darwin + GNU compiler (MaxOS X 10.3 and 10.4)
|
---|
| 20 | - OSF1 + cxx (HP/Compaq/Digital OSF-Tru64 and cxx c++ compiler)
|
---|
| 21 | - IRIX64 + CC : Silicon Graphics system and C++ compiler
|
---|
[3010] | 22 | - AIX + xlC : IBM system and C++ compiler
|
---|
[2598] | 23 |
|
---|
[1275] | 24 | \sa SOPHYA::PDynLinkMgr
|
---|
[2598] | 25 |
|
---|
[1275] | 26 | \code
|
---|
[2269] | 27 | #include "cxxcmplnk.h"
|
---|
[1275] | 28 | CxxCompilerLinker cxx;
|
---|
| 29 | string name = "toto.cc";
|
---|
| 30 | string oname = "toto.o";
|
---|
| 31 | string soname = "toto.so";
|
---|
| 32 | int rc;
|
---|
| 33 | // compiling file
|
---|
| 34 | rc = cxx.Compile(name, oname);
|
---|
| 35 | // linking and building the shared object
|
---|
| 36 | rc = cxx.BuildSO(oname, soname);
|
---|
| 37 | \endcode
|
---|
| 38 | */
|
---|
| 39 |
|
---|
[3572] | 40 | static const char * gcxx_opt = "-O -fdollars-in-identifiers";
|
---|
| 41 | static const char * KCC_opt =
|
---|
| 42 | "-O --exceptions --rtti --auto_instantiation --one_instantiation_per_object -D__KCC__";
|
---|
| 43 | static const char * icc_opt = "-O -fpic -frtti";
|
---|
| 44 | static const char * cxx_opt =
|
---|
| 45 | "-O -no_implicit_include -pthread";
|
---|
| 46 | static const char * SGICC_opt = "-O -prelink -D__SGICC__ -LANG:std";
|
---|
| 47 | static const char * xlC_opt = "-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++/";
|
---|
[3204] | 91 | #ifndef SO_NOFPIC
|
---|
| 92 | compOptions += " -fPIC ";
|
---|
| 93 | linkOptions += " -fPIC ";
|
---|
[2796] | 94 | #endif
|
---|
[3204] | 95 | #endif
|
---|
[2796] | 96 | #if defined( __DECCXX )
|
---|
[1275] | 97 | compCmd = "cxx ";
|
---|
| 98 | compOptions = cxx_opt;
|
---|
[2796] | 99 | linkOptions = compOptions + " -shared";
|
---|
[1275] | 100 | syscomp += "cxx/";
|
---|
[2796] | 101 | #endif
|
---|
| 102 | #if defined( __KCC__ )
|
---|
[1275] | 103 | compCmd = "KCC ";
|
---|
| 104 | compOptions = KCC_opt;
|
---|
| 105 | linkOptions = KCC_opt;
|
---|
| 106 | syscomp += "KCC/";
|
---|
[2796] | 107 | #endif
|
---|
| 108 | #if defined( __SGICC__ )
|
---|
[1275] | 109 | compCmd = "CC ";
|
---|
| 110 | compOptions = SGICC_opt;
|
---|
[2443] | 111 | linkOptions = "-shared -O -LANG:std";
|
---|
[1275] | 112 | syscomp += "CC/";
|
---|
[3204] | 113 | #ifdef SO_ARCH64
|
---|
| 114 | compOptions += " -64 ";
|
---|
[1275] | 115 | linkOptions += " -64 ";
|
---|
| 116 | #endif
|
---|
| 117 | #endif
|
---|
[2796] | 118 | #if defined( __INTEL_COMPILER )
|
---|
| 119 | compCmd = "icpc ";
|
---|
| 120 | compOptions = icc_opt;
|
---|
[2802] | 121 | linkOptions = icc_opt + string(" -shared");
|
---|
[2796] | 122 | syscomp += "icc/";
|
---|
| 123 | #endif
|
---|
[2867] | 124 | #if defined( __IBMCPP__ )
|
---|
| 125 | compCmd = "xlC ";
|
---|
| 126 | compOptions = xlC_opt;
|
---|
| 127 | linkOptions = xlC_opt + string(" -brtl -qmkshrobj ");
|
---|
| 128 | syscomp += "xlC/";
|
---|
[3204] | 129 | #ifdef SO_ARCH64
|
---|
| 130 | compOptions += " -q64 ";
|
---|
| 131 | linkOptions += " -q64 ";
|
---|
[2867] | 132 | #endif
|
---|
[3204] | 133 | #endif
|
---|
[1275] | 134 |
|
---|
[1900] | 135 | #ifndef Darwin
|
---|
| 136 | linkCmd = compCmd;
|
---|
| 137 | #else
|
---|
[2496] | 138 | linkCmd = "c++ -bundle ";
|
---|
| 139 | linkOptions = "-lSystem -lm";
|
---|
[1900] | 140 | #endif
|
---|
| 141 |
|
---|
[1275] | 142 | cppFlags += " -I. ";
|
---|
| 143 | compOptions += " -c ";
|
---|
| 144 |
|
---|
[2727] | 145 | bool fgenv1 = false;
|
---|
| 146 | bool fgenv2 = false;
|
---|
[1275] | 147 | char* varenv=NULL;
|
---|
[2727] | 148 | varenv=getenv("SOPHYABASE");
|
---|
[2867] | 149 | string sbaserep = "./";
|
---|
[2727] | 150 | if (varenv) {
|
---|
| 151 | fgenv1 = true;
|
---|
[2867] | 152 | sbaserep = varenv;
|
---|
[2727] | 153 | if (sbaserep[sbaserep.length()-1] != '/') sbaserep += '/';
|
---|
| 154 | cppFlags += ( " -I" + sbaserep + "include/ ");
|
---|
| 155 | linkOptions += " -L" + sbaserep + "slb/";
|
---|
| 156 | }
|
---|
[2437] | 157 | varenv=getenv("SOPHYABASEREP");
|
---|
[1275] | 158 | if (varenv) {
|
---|
[2727] | 159 | fgenv2 = true;
|
---|
| 160 | string dpcbase = varenv;
|
---|
[1275] | 161 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
| 162 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
[1900] | 163 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
[2727] | 164 | char * varenvextl=getenv("EXTLIBDIR");
|
---|
| 165 | if (varenvextl) {
|
---|
| 166 | string extlib = varenvextl;
|
---|
[1275] | 167 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
| 168 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
[2727] | 169 | }
|
---|
[1275] | 170 | }
|
---|
[2727] | 171 | if (!fgenv1 && !fgenv2)
|
---|
| 172 | cout << " CxxCompilerLinker()/Warning : SOPHYABASE not defined" << endl;
|
---|
| 173 | if (fgenv1 && fgenv2)
|
---|
| 174 | cout << " CxxCompilerLinker()/Warning : both SOPHYABASE and SOPHYABASEREP defined" << endl;
|
---|
[1275] | 175 |
|
---|
[3211] | 176 | #if defined( SO_SLBALLINONE )
|
---|
[3014] | 177 | if (fglibsophya || fglibpi) linkOptions += " -lAsophyaextPI ";
|
---|
| 178 | /*
|
---|
| 179 | cout << " +++ DBG/CxxCompilerLinker() - in AIX fglibpi= "
|
---|
[2867] | 180 | << ((fglibpi) ? " true " : " false ") << endl;
|
---|
| 181 | // Reza:Dec 2005 : pb avec les programmes PI sur AIX si linke avec shared lib PI
|
---|
| 182 | // (Portage sur regatta.calcul.u-psud.fr AIX 5.3 , libXm Xt X11 en .a uniquement)
|
---|
| 183 | if (fglibpi) {
|
---|
| 184 | linkOptions += " -L" + sbaserep + "lib/";
|
---|
| 185 | linkOptions += " -lPIext -lPIGcont -lPI -lXm -lXt -lX11 -lpthread ";
|
---|
| 186 | }
|
---|
[3014] | 187 | */
|
---|
| 188 | cout << "CxxCompilerLinker()/Warning: SINGLESLB_SOPHYA_PI_EXT -> linkOptions= " << linkOptions << endl;
|
---|
[2867] | 189 | #else
|
---|
[3014] | 190 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
| 191 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
[2727] | 192 | if (fglibpi) linkOptions += " -lPI ";
|
---|
[2867] | 193 | #endif
|
---|
[2727] | 194 | linkOptions += " -lm ";
|
---|
| 195 |
|
---|
| 196 |
|
---|
[1277] | 197 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
| 198 | tmpDir = varenv;
|
---|
| 199 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
| 200 | }
|
---|
[1275] | 201 | }
|
---|
| 202 |
|
---|
| 203 | /* --Methode-- */
|
---|
| 204 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
| 205 | {
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /* --Methode-- */
|
---|
[2212] | 209 | /*!
|
---|
| 210 | Compiles the file \c name using the C++ compiler driver and produces
|
---|
| 211 | the output object file \c oname. If no output name is specified,
|
---|
| 212 | a default output file name is made from the input name, with the
|
---|
| 213 | suffix .o , the in temporary directory.
|
---|
| 214 | \param name : input C++ source file
|
---|
| 215 | \param oname : output object file
|
---|
| 216 | */
|
---|
[1277] | 217 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
[1275] | 218 | {
|
---|
[1277] | 219 | if (oname.length() < 1) {
|
---|
| 220 | size_t l,p,q;
|
---|
| 221 | l = name.length();
|
---|
| 222 | p = name.rfind('/');
|
---|
| 223 | if (p >= l) p = 0;
|
---|
| 224 | else p++;
|
---|
[2183] | 225 | q = name.rfind('.');
|
---|
[2190] | 226 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
[1277] | 227 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
[1275] | 228 | }
|
---|
| 229 | string cmd;
|
---|
[1277] | 230 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
[1275] | 231 | if (verbose)
|
---|
| 232 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
| 233 | int rc = system(cmd.c_str());
|
---|
| 234 | if (rc != 0)
|
---|
| 235 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 236 |
|
---|
| 237 | return(rc);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | /* --Methode-- */
|
---|
[2212] | 241 | /*!
|
---|
| 242 | Creates a shared library from the object file \c oname.
|
---|
| 243 | If no output name \c soname is specified,
|
---|
| 244 | a default output file name is made from the object name, with the
|
---|
| 245 | suffix .so , in the temporary directory.
|
---|
| 246 | \param oname : input object file
|
---|
| 247 | \param soname : shared library name
|
---|
| 248 | */
|
---|
[1277] | 249 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
[1275] | 250 | {
|
---|
[3572] | 251 | // const char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
| 252 | const char * soext = ".so";
|
---|
[1900] | 253 |
|
---|
[1275] | 254 | if (soname.length() < 1) {
|
---|
[1277] | 255 | size_t l,p,q;
|
---|
| 256 | l = oname.length();
|
---|
| 257 | p = oname.rfind('/');
|
---|
| 258 | if (p >= l) p = 0;
|
---|
| 259 | else p++;
|
---|
[2183] | 260 | q = oname.rfind('.');
|
---|
[2190] | 261 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
[1900] | 262 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
[1275] | 263 | }
|
---|
| 264 | string cmd;
|
---|
[1900] | 265 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
[1275] | 266 | if (verbose)
|
---|
| 267 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
| 268 | int rc = system(cmd.c_str());
|
---|
| 269 | if (rc != 0)
|
---|
| 270 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 271 |
|
---|
| 272 | return(rc);
|
---|
| 273 | }
|
---|
| 274 |
|
---|