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