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