| 1 | // Gestionnaire de compilation-linker C++ - R. Ansari 10/2000
|
|---|
| 2 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
|---|
| 3 |
|
|---|
| 4 | #include "sopnamsp.h"
|
|---|
| 5 | #include "cxxcmplnk.h"
|
|---|
| 6 | #include "sspvflags.h"
|
|---|
| 7 | #include <stdlib.h>
|
|---|
| 8 | #include <iostream>
|
|---|
| 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.
|
|---|
| 15 | The present version has been adapted for different compilers and
|
|---|
| 16 | systems:
|
|---|
| 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
|
|---|
| 22 | - AIX + xlC : IBM system and C++ compiler
|
|---|
| 23 |
|
|---|
| 24 | \sa SOPHYA::PDynLinkMgr
|
|---|
| 25 |
|
|---|
| 26 | \code
|
|---|
| 27 | #include "cxxcmplnk.h"
|
|---|
| 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 |
|
|---|
| 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";
|
|---|
| 48 |
|
|---|
| 49 | /* --Methode-- */
|
|---|
| 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 | */
|
|---|
| 56 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
|
|---|
| 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-";
|
|---|
| 80 | #elif defined(Darwin)
|
|---|
| 81 | cppFlags += "-DDarwin";
|
|---|
| 82 | syscomp = "Darwin-";
|
|---|
| 83 | #endif
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 | #if defined( __GNUG__ )
|
|---|
| 87 | compCmd = "g++ ";
|
|---|
| 88 | compOptions = gcxx_opt;
|
|---|
| 89 | linkOptions = "-O -shared";
|
|---|
| 90 | syscomp += "g++/";
|
|---|
| 91 | #ifndef SO_NOFPIC
|
|---|
| 92 | compOptions += " -fPIC ";
|
|---|
| 93 | linkOptions += " -fPIC ";
|
|---|
| 94 | #endif
|
|---|
| 95 | #endif
|
|---|
| 96 | #if defined( __DECCXX )
|
|---|
| 97 | compCmd = "cxx ";
|
|---|
| 98 | compOptions = cxx_opt;
|
|---|
| 99 | linkOptions = compOptions + " -shared";
|
|---|
| 100 | syscomp += "cxx/";
|
|---|
| 101 | #endif
|
|---|
| 102 | #if defined( __KCC__ )
|
|---|
| 103 | compCmd = "KCC ";
|
|---|
| 104 | compOptions = KCC_opt;
|
|---|
| 105 | linkOptions = KCC_opt;
|
|---|
| 106 | syscomp += "KCC/";
|
|---|
| 107 | #endif
|
|---|
| 108 | #if defined( __SGICC__ )
|
|---|
| 109 | compCmd = "CC ";
|
|---|
| 110 | compOptions = SGICC_opt;
|
|---|
| 111 | linkOptions = "-shared -O -LANG:std";
|
|---|
| 112 | syscomp += "CC/";
|
|---|
| 113 | #ifdef SO_ARCH64
|
|---|
| 114 | compOptions += " -64 ";
|
|---|
| 115 | linkOptions += " -64 ";
|
|---|
| 116 | #endif
|
|---|
| 117 | #endif
|
|---|
| 118 | #if defined( __INTEL_COMPILER )
|
|---|
| 119 | compCmd = "icpc ";
|
|---|
| 120 | compOptions = icc_opt;
|
|---|
| 121 | linkOptions = icc_opt + string(" -shared");
|
|---|
| 122 | syscomp += "icc/";
|
|---|
| 123 | #endif
|
|---|
| 124 | #if defined( __IBMCPP__ )
|
|---|
| 125 | compCmd = "xlC ";
|
|---|
| 126 | compOptions = xlC_opt;
|
|---|
| 127 | linkOptions = xlC_opt + string(" -brtl -qmkshrobj ");
|
|---|
| 128 | syscomp += "xlC/";
|
|---|
| 129 | #ifdef SO_ARCH64
|
|---|
| 130 | compOptions += " -q64 ";
|
|---|
| 131 | linkOptions += " -q64 ";
|
|---|
| 132 | #endif
|
|---|
| 133 | #endif
|
|---|
| 134 |
|
|---|
| 135 | #ifndef Darwin
|
|---|
| 136 | linkCmd = compCmd;
|
|---|
| 137 | #else
|
|---|
| 138 | linkCmd = "c++ -bundle ";
|
|---|
| 139 | linkOptions = "-lSystem -lm";
|
|---|
| 140 | #endif
|
|---|
| 141 |
|
|---|
| 142 | cppFlags += " -I. ";
|
|---|
| 143 | compOptions += " -c ";
|
|---|
| 144 |
|
|---|
| 145 | bool fgenv1 = false;
|
|---|
| 146 | bool fgenv2 = false;
|
|---|
| 147 | char* varenv=NULL;
|
|---|
| 148 | varenv=getenv("SOPHYABASE");
|
|---|
| 149 | string sbaserep = "./";
|
|---|
| 150 | if (varenv) {
|
|---|
| 151 | fgenv1 = true;
|
|---|
| 152 | sbaserep = varenv;
|
|---|
| 153 | if (sbaserep[sbaserep.length()-1] != '/') sbaserep += '/';
|
|---|
| 154 | cppFlags += ( " -I" + sbaserep + "include/ ");
|
|---|
| 155 | linkOptions += " -L" + sbaserep + "slb/";
|
|---|
| 156 | }
|
|---|
| 157 | varenv=getenv("SOPHYABASEREP");
|
|---|
| 158 | if (varenv) {
|
|---|
| 159 | fgenv2 = true;
|
|---|
| 160 | string dpcbase = varenv;
|
|---|
| 161 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
|---|
| 162 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
|---|
| 163 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
|---|
| 164 | char * varenvextl=getenv("EXTLIBDIR");
|
|---|
| 165 | if (varenvextl) {
|
|---|
| 166 | string extlib = varenvextl;
|
|---|
| 167 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
|---|
| 168 | cppFlags += ( " -I" + extlib +"Include/ ");
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|
| 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;
|
|---|
| 175 |
|
|---|
| 176 | #if defined( SO_SLBALLINONE )
|
|---|
| 177 | if (fglibsophya || fglibpi) linkOptions += " -lAsophyaextPI ";
|
|---|
| 178 | /*
|
|---|
| 179 | cout << " +++ DBG/CxxCompilerLinker() - in AIX fglibpi= "
|
|---|
| 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 | }
|
|---|
| 187 | */
|
|---|
| 188 | cout << "CxxCompilerLinker()/Warning: SINGLESLB_SOPHYA_PI_EXT -> linkOptions= " << linkOptions << endl;
|
|---|
| 189 | #else
|
|---|
| 190 | if (fglibsophya) linkOptions += " -lsophya ";
|
|---|
| 191 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
|---|
| 192 | if (fglibpi) linkOptions += " -lPI ";
|
|---|
| 193 | #endif
|
|---|
| 194 | linkOptions += " -lm ";
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
|---|
| 198 | tmpDir = varenv;
|
|---|
| 199 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
|---|
| 200 | }
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | /* --Methode-- */
|
|---|
| 204 | CxxCompilerLinker::~CxxCompilerLinker()
|
|---|
| 205 | {
|
|---|
| 206 | }
|
|---|
| 207 |
|
|---|
| 208 | /* --Methode-- */
|
|---|
| 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 | */
|
|---|
| 217 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
|---|
| 218 | {
|
|---|
| 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++;
|
|---|
| 225 | q = name.rfind('.');
|
|---|
| 226 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
|---|
| 227 | else oname = tmpDir + name.substr(p) + ".o";
|
|---|
| 228 | }
|
|---|
| 229 | string cmd;
|
|---|
| 230 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
|---|
| 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-- */
|
|---|
| 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 | */
|
|---|
| 249 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
|---|
| 250 | {
|
|---|
| 251 | // const char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
|---|
| 252 | const char * soext = ".so";
|
|---|
| 253 |
|
|---|
| 254 | if (soname.length() < 1) {
|
|---|
| 255 | size_t l,p,q;
|
|---|
| 256 | l = oname.length();
|
|---|
| 257 | p = oname.rfind('/');
|
|---|
| 258 | if (p >= l) p = 0;
|
|---|
| 259 | else p++;
|
|---|
| 260 | q = oname.rfind('.');
|
|---|
| 261 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
|---|
| 262 | else soname = tmpDir + oname.substr(p) + soext;
|
|---|
| 263 | }
|
|---|
| 264 | string cmd;
|
|---|
| 265 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
|---|
| 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 |
|
|---|