[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 =
|
---|
| 38 | "-O -Wall -Wpointer-arith -Wmissing-prototypes -Wsynth -fdollars-in-identifiers";
|
---|
| 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 |
|
---|
| 118 | string dpcbase;
|
---|
| 119 | char* varenv=NULL;
|
---|
[2437] | 120 | varenv=getenv("SOPHYABASEREP");
|
---|
[1275] | 121 | if (varenv) {
|
---|
| 122 | dpcbase = varenv;
|
---|
| 123 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
| 124 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
[1900] | 125 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
| 126 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
| 127 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
| 128 | if (fglibpi) linkOptions += " -lPI ";
|
---|
[1275] | 129 | }
|
---|
[2124] | 130 | linkOptions += " -lm ";
|
---|
[1275] | 131 |
|
---|
| 132 | string extlib;
|
---|
| 133 | varenv=getenv("EXTLIBDIR");
|
---|
| 134 | if (varenv) {
|
---|
| 135 | extlib = varenv;
|
---|
| 136 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
| 137 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[1277] | 140 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
| 141 | tmpDir = varenv;
|
---|
| 142 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
| 143 | }
|
---|
[1275] | 144 | }
|
---|
| 145 |
|
---|
| 146 | /* --Methode-- */
|
---|
| 147 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
| 148 | {
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | /* --Methode-- */
|
---|
[2212] | 152 | /*!
|
---|
| 153 | Compiles the file \c name using the C++ compiler driver and produces
|
---|
| 154 | the output object file \c oname. If no output name is specified,
|
---|
| 155 | a default output file name is made from the input name, with the
|
---|
| 156 | suffix .o , the in temporary directory.
|
---|
| 157 | \param name : input C++ source file
|
---|
| 158 | \param oname : output object file
|
---|
| 159 | */
|
---|
[1277] | 160 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
[1275] | 161 | {
|
---|
[1277] | 162 | if (oname.length() < 1) {
|
---|
| 163 | size_t l,p,q;
|
---|
| 164 | l = name.length();
|
---|
| 165 | p = name.rfind('/');
|
---|
| 166 | if (p >= l) p = 0;
|
---|
| 167 | else p++;
|
---|
[2183] | 168 | q = name.rfind('.');
|
---|
[2190] | 169 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
[1277] | 170 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
[1275] | 171 | }
|
---|
| 172 | string cmd;
|
---|
[1277] | 173 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
[1275] | 174 | if (verbose)
|
---|
| 175 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
| 176 | int rc = system(cmd.c_str());
|
---|
| 177 | if (rc != 0)
|
---|
| 178 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 179 |
|
---|
| 180 | return(rc);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | /* --Methode-- */
|
---|
[2212] | 184 | /*!
|
---|
| 185 | Creates a shared library from the object file \c oname.
|
---|
| 186 | If no output name \c soname is specified,
|
---|
| 187 | a default output file name is made from the object name, with the
|
---|
| 188 | suffix .so , in the temporary directory.
|
---|
| 189 | \param oname : input object file
|
---|
| 190 | \param soname : shared library name
|
---|
| 191 | */
|
---|
[1277] | 192 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
[1275] | 193 | {
|
---|
[1900] | 194 | // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
| 195 | char * soext = ".so";
|
---|
| 196 |
|
---|
[1275] | 197 | if (soname.length() < 1) {
|
---|
[1277] | 198 | size_t l,p,q;
|
---|
| 199 | l = oname.length();
|
---|
| 200 | p = oname.rfind('/');
|
---|
| 201 | if (p >= l) p = 0;
|
---|
| 202 | else p++;
|
---|
[2183] | 203 | q = oname.rfind('.');
|
---|
[2190] | 204 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
[1900] | 205 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
[1275] | 206 | }
|
---|
| 207 | string cmd;
|
---|
[1900] | 208 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
[1275] | 209 | if (verbose)
|
---|
| 210 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
| 211 | int rc = system(cmd.c_str());
|
---|
| 212 | if (rc != 0)
|
---|
| 213 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 214 |
|
---|
| 215 | return(rc);
|
---|
| 216 | }
|
---|
| 217 |
|
---|