[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"
|
---|
| 5 | #include <iostream.h>
|
---|
| 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.
|
---|
| 12 | The present version has been adapted for different compilers:
|
---|
| 13 | g++ , Compaq/Digital cxx , SGI CC , KCC .
|
---|
| 14 | \sa SOPHYA::PDynLinkMgr
|
---|
| 15 | \code
|
---|
| 16 | #include ""cxxcmplnk.h"
|
---|
| 17 | CxxCompilerLinker cxx;
|
---|
| 18 | string name = "toto.cc";
|
---|
| 19 | string oname = "toto.o";
|
---|
| 20 | string soname = "toto.so";
|
---|
| 21 | int rc;
|
---|
| 22 | // compiling file
|
---|
| 23 | rc = cxx.Compile(name, oname);
|
---|
| 24 | // linking and building the shared object
|
---|
| 25 | rc = cxx.BuildSO(oname, soname);
|
---|
| 26 | \endcode
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | static char * gcxx_opt =
|
---|
| 30 | "-O -Wall -Wpointer-arith -Wmissing-prototypes -Wsynth -fdollars-in-identifiers";
|
---|
| 31 | static char * KCC_opt =
|
---|
| 32 | "-O --exceptions --rtti --auto_instantiation --one_instantiation_per_object -D__KCC__";
|
---|
| 33 | static char * cxx_opt =
|
---|
| 34 | "-O -no_implicit_include ";
|
---|
| 35 | static char * SGICC_opt = "-O -prelink -D__SGICC__ ";
|
---|
| 36 |
|
---|
| 37 | /* --Methode-- */
|
---|
[1900] | 38 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
|
---|
[1275] | 39 | : verbose(false)
|
---|
| 40 | {
|
---|
| 41 |
|
---|
| 42 | string syscomp = "";
|
---|
| 43 |
|
---|
| 44 | #if defined(OSF1)
|
---|
| 45 | cppFlags += "-DOSF1" ;
|
---|
| 46 | syscomp = "OSF1-";
|
---|
| 47 | #elif defined(Linux)
|
---|
| 48 | cppFlags += "-DLinux" ;
|
---|
| 49 | syscomp = "Linux-";
|
---|
| 50 | #elif defined(SunOS)
|
---|
| 51 | cppFlags += "-DSunOS" ;
|
---|
| 52 | syscomp = "SunOS-";
|
---|
| 53 | #elif defined(IRIX64)
|
---|
| 54 | cppFlags += "-DIRIX64" ;
|
---|
| 55 | syscomp = "IRIX64-";
|
---|
| 56 | #elif defined(AIX)
|
---|
| 57 | cppFlags += "-DAIX" ;
|
---|
| 58 | syscomp = "AIX-";
|
---|
| 59 | #elif defined(HPUX)
|
---|
| 60 | cppFlags += "-DHPUX" ;
|
---|
| 61 | syscomp = "HPUX-";
|
---|
[1797] | 62 | #elif defined(Darwin)
|
---|
| 63 | cppFlags += "-DDarwin";
|
---|
| 64 | syscomp = "Darwin-";
|
---|
[1275] | 65 | #endif
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | #if defined( __GNUG__ )
|
---|
| 69 | compCmd = "g++ ";
|
---|
| 70 | compOptions = gcxx_opt;
|
---|
| 71 | linkOptions = "-O -shared";
|
---|
| 72 | syscomp += "g++/";
|
---|
| 73 | #elif defined( __DECCXX )
|
---|
| 74 | compCmd = "cxx ";
|
---|
| 75 | compOptions = cxx_opt;
|
---|
[1279] | 76 | linkOptions = compOptions + "-shared";
|
---|
[1275] | 77 | syscomp += "cxx/";
|
---|
| 78 | #elif defined( __KCC__ )
|
---|
| 79 | compCmd = "KCC ";
|
---|
| 80 | compOptions = KCC_opt;
|
---|
| 81 | linkOptions = KCC_opt;
|
---|
| 82 | syscomp += "KCC/";
|
---|
| 83 | #elif defined( __SGICC__ )
|
---|
| 84 | compCmd = "CC ";
|
---|
| 85 | compOptions = SGICC_opt;
|
---|
[1284] | 86 | linkOptions = "-shared -O ";
|
---|
[1275] | 87 | syscomp += "CC/";
|
---|
| 88 | #ifdef SGI_ARCH64
|
---|
| 89 | compOptions += " -64 -DSGI_ARCH64 ";
|
---|
| 90 | linkOptions += " -64 ";
|
---|
| 91 | #endif
|
---|
| 92 | #endif
|
---|
| 93 |
|
---|
[1900] | 94 | #ifndef Darwin
|
---|
| 95 | linkCmd = compCmd;
|
---|
| 96 | #else
|
---|
| 97 | linkCmd = "cc -bundle -flat_namespace -undefined suppress ";
|
---|
[2124] | 98 | linkOptions = "-L/usr/lib/gcc/darwin/default -lstdc++ -lcc_dynamic -lpthread ";
|
---|
[1900] | 99 | #endif
|
---|
| 100 |
|
---|
[1275] | 101 | cppFlags += " -I. ";
|
---|
| 102 | compOptions += " -c ";
|
---|
| 103 |
|
---|
| 104 | string dpcbase;
|
---|
| 105 | char* varenv=NULL;
|
---|
| 106 | varenv=getenv("DPCBASEREP");
|
---|
| 107 | if (varenv) {
|
---|
| 108 | dpcbase = varenv;
|
---|
| 109 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
| 110 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
[1900] | 111 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
| 112 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
| 113 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
| 114 | if (fglibpi) linkOptions += " -lPI ";
|
---|
[1275] | 115 | }
|
---|
[2124] | 116 | linkOptions += " -lm ";
|
---|
[1275] | 117 |
|
---|
| 118 | string extlib;
|
---|
| 119 | varenv=getenv("EXTLIBDIR");
|
---|
| 120 | if (varenv) {
|
---|
| 121 | extlib = varenv;
|
---|
| 122 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
| 123 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[1277] | 126 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
| 127 | tmpDir = varenv;
|
---|
| 128 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
| 129 | }
|
---|
[1275] | 130 | }
|
---|
| 131 |
|
---|
| 132 | /* --Methode-- */
|
---|
| 133 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
| 134 | {
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | /* --Methode-- */
|
---|
[1277] | 138 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
[1275] | 139 | {
|
---|
[1277] | 140 | if (oname.length() < 1) {
|
---|
| 141 | size_t l,p,q;
|
---|
| 142 | l = name.length();
|
---|
| 143 | p = name.rfind('/');
|
---|
| 144 | if (p >= l) p = 0;
|
---|
| 145 | else p++;
|
---|
[2183] | 146 | q = name.rfind('.');
|
---|
[2190] | 147 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
[1277] | 148 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
[1275] | 149 | }
|
---|
| 150 | string cmd;
|
---|
[1277] | 151 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
[1275] | 152 | if (verbose)
|
---|
| 153 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
| 154 | int rc = system(cmd.c_str());
|
---|
| 155 | if (rc != 0)
|
---|
| 156 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 157 |
|
---|
| 158 | return(rc);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /* --Methode-- */
|
---|
[1277] | 162 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
[1275] | 163 | {
|
---|
[1900] | 164 | // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
| 165 | char * soext = ".so";
|
---|
| 166 |
|
---|
[1275] | 167 | if (soname.length() < 1) {
|
---|
[1277] | 168 | size_t l,p,q;
|
---|
| 169 | l = oname.length();
|
---|
| 170 | p = oname.rfind('/');
|
---|
| 171 | if (p >= l) p = 0;
|
---|
| 172 | else p++;
|
---|
[2183] | 173 | q = oname.rfind('.');
|
---|
[2190] | 174 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
[1900] | 175 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
[1275] | 176 | }
|
---|
| 177 | string cmd;
|
---|
[1900] | 178 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
[1275] | 179 | if (verbose)
|
---|
| 180 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
| 181 | int rc = system(cmd.c_str());
|
---|
| 182 | if (rc != 0)
|
---|
| 183 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
| 184 |
|
---|
| 185 | return(rc);
|
---|
| 186 | }
|
---|
| 187 |
|
---|