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