[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++/";
|
---|
[2796] | 93 | #endif
|
---|
| 94 | #if defined( __DECCXX )
|
---|
[1275] | 95 | compCmd = "cxx ";
|
---|
| 96 | compOptions = cxx_opt;
|
---|
[2796] | 97 | linkOptions = compOptions + " -shared";
|
---|
[1275] | 98 | syscomp += "cxx/";
|
---|
[2796] | 99 | #endif
|
---|
| 100 | #if defined( __KCC__ )
|
---|
[1275] | 101 | compCmd = "KCC ";
|
---|
| 102 | compOptions = KCC_opt;
|
---|
| 103 | linkOptions = KCC_opt;
|
---|
| 104 | syscomp += "KCC/";
|
---|
[2796] | 105 | #endif
|
---|
| 106 | #if defined( __SGICC__ )
|
---|
[1275] | 107 | compCmd = "CC ";
|
---|
| 108 | compOptions = SGICC_opt;
|
---|
[2443] | 109 | linkOptions = "-shared -O -LANG:std";
|
---|
[1275] | 110 | syscomp += "CC/";
|
---|
| 111 | #ifdef SGI_ARCH64
|
---|
| 112 | compOptions += " -64 -DSGI_ARCH64 ";
|
---|
| 113 | linkOptions += " -64 ";
|
---|
| 114 | #endif
|
---|
| 115 | #endif
|
---|
[2796] | 116 | #if defined( __INTEL_COMPILER )
|
---|
| 117 | compCmd = "icpc ";
|
---|
| 118 | compOptions = icc_opt;
|
---|
[2802] | 119 | linkOptions = icc_opt + string(" -shared");
|
---|
[2796] | 120 | syscomp += "icc/";
|
---|
| 121 | #endif
|
---|
[2867] | 122 | #if defined( __IBMCPP__ )
|
---|
| 123 | compCmd = "xlC ";
|
---|
| 124 | compOptions = xlC_opt;
|
---|
| 125 | linkOptions = xlC_opt + string(" -brtl -qmkshrobj ");
|
---|
| 126 | syscomp += "xlC/";
|
---|
| 127 | #endif
|
---|
[1275] | 128 |
|
---|
[1900] | 129 | #ifndef Darwin
|
---|
| 130 | linkCmd = compCmd;
|
---|
| 131 | #else
|
---|
[2496] | 132 | linkCmd = "c++ -bundle ";
|
---|
| 133 | linkOptions = "-lSystem -lm";
|
---|
[1900] | 134 | #endif
|
---|
| 135 |
|
---|
[1275] | 136 | cppFlags += " -I. ";
|
---|
| 137 | compOptions += " -c ";
|
---|
| 138 |
|
---|
[2727] | 139 | bool fgenv1 = false;
|
---|
| 140 | bool fgenv2 = false;
|
---|
[1275] | 141 | char* varenv=NULL;
|
---|
[2727] | 142 | varenv=getenv("SOPHYABASE");
|
---|
[2867] | 143 | string sbaserep = "./";
|
---|
[2727] | 144 | if (varenv) {
|
---|
| 145 | fgenv1 = true;
|
---|
[2867] | 146 | sbaserep = varenv;
|
---|
[2727] | 147 | if (sbaserep[sbaserep.length()-1] != '/') sbaserep += '/';
|
---|
| 148 | cppFlags += ( " -I" + sbaserep + "include/ ");
|
---|
| 149 | linkOptions += " -L" + sbaserep + "slb/";
|
---|
| 150 | }
|
---|
[2437] | 151 | varenv=getenv("SOPHYABASEREP");
|
---|
[1275] | 152 | if (varenv) {
|
---|
[2727] | 153 | fgenv2 = true;
|
---|
| 154 | string dpcbase = varenv;
|
---|
[1275] | 155 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
| 156 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
[1900] | 157 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
[2727] | 158 | char * varenvextl=getenv("EXTLIBDIR");
|
---|
| 159 | if (varenvextl) {
|
---|
| 160 | string extlib = varenvextl;
|
---|
[1275] | 161 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
| 162 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
[2727] | 163 | }
|
---|
[1275] | 164 | }
|
---|
[2727] | 165 | if (!fgenv1 && !fgenv2)
|
---|
| 166 | cout << " CxxCompilerLinker()/Warning : SOPHYABASE not defined" << endl;
|
---|
| 167 | if (fgenv1 && fgenv2)
|
---|
| 168 | cout << " CxxCompilerLinker()/Warning : both SOPHYABASE and SOPHYABASEREP defined" << endl;
|
---|
[1275] | 169 |
|
---|
[3014] | 170 | #if defined( SINGLESLB_SOPHYA_PI_EXT )
|
---|
| 171 | if (fglibsophya || fglibpi) linkOptions += " -lAsophyaextPI ";
|
---|
| 172 | /*
|
---|
| 173 | cout << " +++ DBG/CxxCompilerLinker() - in AIX fglibpi= "
|
---|
[2867] | 174 | << ((fglibpi) ? " true " : " false ") << endl;
|
---|
| 175 | // Reza:Dec 2005 : pb avec les programmes PI sur AIX si linke avec shared lib PI
|
---|
| 176 | // (Portage sur regatta.calcul.u-psud.fr AIX 5.3 , libXm Xt X11 en .a uniquement)
|
---|
| 177 | if (fglibpi) {
|
---|
| 178 | linkOptions += " -L" + sbaserep + "lib/";
|
---|
| 179 | linkOptions += " -lPIext -lPIGcont -lPI -lXm -lXt -lX11 -lpthread ";
|
---|
| 180 | }
|
---|
[3014] | 181 | */
|
---|
| 182 | cout << "CxxCompilerLinker()/Warning: SINGLESLB_SOPHYA_PI_EXT -> linkOptions= " << linkOptions << endl;
|
---|
[2867] | 183 | #else
|
---|
[3014] | 184 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
| 185 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
[2727] | 186 | if (fglibpi) linkOptions += " -lPI ";
|
---|
[2867] | 187 | #endif
|
---|
[2727] | 188 | linkOptions += " -lm ";
|
---|
| 189 |
|
---|
| 190 |
|
---|
[1277] | 191 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
| 192 | tmpDir = varenv;
|
---|
| 193 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
| 194 | }
|
---|
[1275] | 195 | }
|
---|
| 196 |
|
---|
| 197 | /* --Methode-- */
|
---|
| 198 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
| 199 | {
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | /* --Methode-- */
|
---|
[2212] | 203 | /*!
|
---|
| 204 | Compiles the file \c name using the C++ compiler driver and produces
|
---|
| 205 | the output object file \c oname. If no output name is specified,
|
---|
| 206 | a default output file name is made from the input name, with the
|
---|
| 207 | suffix .o , the in temporary directory.
|
---|
| 208 | \param name : input C++ source file
|
---|
| 209 | \param oname : output object file
|
---|
| 210 | */
|
---|
[1277] | 211 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
[1275] | 212 | {
|
---|
[1277] | 213 | if (oname.length() < 1) {
|
---|
| 214 | size_t l,p,q;
|
---|
| 215 | l = name.length();
|
---|
| 216 | p = name.rfind('/');
|
---|
| 217 | if (p >= l) p = 0;
|
---|
| 218 | else p++;
|
---|
[2183] | 219 | q = name.rfind('.');
|
---|
[2190] | 220 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
[1277] | 221 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
[1275] | 222 | }
|
---|
| 223 | string cmd;
|
---|
[1277] | 224 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
[1275] | 225 | if (verbose)
|
---|
| 226 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
| 227 | int rc = system(cmd.c_str());
|
---|
| 228 | if (rc != 0)
|
---|
| 229 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 230 |
|
---|
| 231 | return(rc);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | /* --Methode-- */
|
---|
[2212] | 235 | /*!
|
---|
| 236 | Creates a shared library from the object file \c oname.
|
---|
| 237 | If no output name \c soname is specified,
|
---|
| 238 | a default output file name is made from the object name, with the
|
---|
| 239 | suffix .so , in the temporary directory.
|
---|
| 240 | \param oname : input object file
|
---|
| 241 | \param soname : shared library name
|
---|
| 242 | */
|
---|
[1277] | 243 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
[1275] | 244 | {
|
---|
[1900] | 245 | // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
| 246 | char * soext = ".so";
|
---|
| 247 |
|
---|
[1275] | 248 | if (soname.length() < 1) {
|
---|
[1277] | 249 | size_t l,p,q;
|
---|
| 250 | l = oname.length();
|
---|
| 251 | p = oname.rfind('/');
|
---|
| 252 | if (p >= l) p = 0;
|
---|
| 253 | else p++;
|
---|
[2183] | 254 | q = oname.rfind('.');
|
---|
[2190] | 255 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
[1900] | 256 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
[1275] | 257 | }
|
---|
| 258 | string cmd;
|
---|
[1900] | 259 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
[1275] | 260 | if (verbose)
|
---|
| 261 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
| 262 | int rc = system(cmd.c_str());
|
---|
| 263 | if (rc != 0)
|
---|
| 264 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 265 |
|
---|
| 266 | return(rc);
|
---|
| 267 | }
|
---|
| 268 |
|
---|