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