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