| 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-- */ | 
|---|
| 38 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi) | 
|---|
| 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-"; | 
|---|
| 62 | #elif defined(Darwin) | 
|---|
| 63 | cppFlags += "-DDarwin"; | 
|---|
| 64 | syscomp = "Darwin-"; | 
|---|
| 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; | 
|---|
| 76 | linkOptions = compOptions + "-shared"; | 
|---|
| 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; | 
|---|
| 86 | linkOptions = "-shared -O "; | 
|---|
| 87 | syscomp += "CC/"; | 
|---|
| 88 | #ifdef SGI_ARCH64 | 
|---|
| 89 | compOptions += " -64 -DSGI_ARCH64 "; | 
|---|
| 90 | linkOptions += " -64 "; | 
|---|
| 91 | #endif | 
|---|
| 92 | #endif | 
|---|
| 93 |  | 
|---|
| 94 | #ifndef Darwin | 
|---|
| 95 | linkCmd = compCmd; | 
|---|
| 96 | #else | 
|---|
| 97 | linkCmd = "cc -bundle -flat_namespace -undefined suppress "; | 
|---|
| 98 | linkOptions = "-L/usr/lib/gcc/darwin/default -lstdc++ -lcc_dynamic -lpthread -lm"; | 
|---|
| 99 | #endif | 
|---|
| 100 |  | 
|---|
| 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/ "); | 
|---|
| 111 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/"; | 
|---|
| 112 | if (fglibsophya) linkOptions += " -lsophya "; | 
|---|
| 113 | if (fglibextsophya) linkOptions += " -lextsophya "; | 
|---|
| 114 | if (fglibpi) linkOptions += " -lPI "; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | string extlib; | 
|---|
| 118 | varenv=getenv("EXTLIBDIR"); | 
|---|
| 119 | if (varenv) { | 
|---|
| 120 | extlib = varenv; | 
|---|
| 121 | if (extlib[extlib.length()-1] != '/')  extlib += '/'; | 
|---|
| 122 | cppFlags += ( " -I" + extlib +"Include/ "); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | if ( (varenv=getenv("TMPDIR")) != NULL )  { | 
|---|
| 126 | tmpDir = varenv; | 
|---|
| 127 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/'; | 
|---|
| 128 | } | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | /* --Methode-- */ | 
|---|
| 132 | CxxCompilerLinker::~CxxCompilerLinker() | 
|---|
| 133 | { | 
|---|
| 134 | } | 
|---|
| 135 |  | 
|---|
| 136 | /* --Methode-- */ | 
|---|
| 137 | int CxxCompilerLinker::Compile(string const & name, string & oname) | 
|---|
| 138 | { | 
|---|
| 139 | if (oname.length() < 1) { | 
|---|
| 140 | size_t l,p,q; | 
|---|
| 141 | l = name.length(); | 
|---|
| 142 | p = name.rfind('/'); | 
|---|
| 143 | if (p >= l)  p = 0; | 
|---|
| 144 | else p++; | 
|---|
| 145 | q = name.find('.'); | 
|---|
| 146 | if (q < l)  oname = tmpDir + name.substr(p, q-p) + ".o"; | 
|---|
| 147 | else oname = tmpDir + name.substr(p) + ".o"; | 
|---|
| 148 | } | 
|---|
| 149 | string cmd; | 
|---|
| 150 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ; | 
|---|
| 151 | if (verbose) | 
|---|
| 152 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl; | 
|---|
| 153 | int rc = system(cmd.c_str()); | 
|---|
| 154 | if (rc != 0) | 
|---|
| 155 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 156 |  | 
|---|
| 157 | return(rc); | 
|---|
| 158 | } | 
|---|
| 159 |  | 
|---|
| 160 | /* --Methode-- */ | 
|---|
| 161 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname) | 
|---|
| 162 | { | 
|---|
| 163 | //  char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002 | 
|---|
| 164 | char * soext = ".so"; | 
|---|
| 165 |  | 
|---|
| 166 | if (soname.length() < 1) { | 
|---|
| 167 | size_t l,p,q; | 
|---|
| 168 | l = oname.length(); | 
|---|
| 169 | p = oname.rfind('/'); | 
|---|
| 170 | if (p >= l)  p = 0; | 
|---|
| 171 | else p++; | 
|---|
| 172 | q = oname.find('.'); | 
|---|
| 173 | if (q < l)  soname = tmpDir + oname.substr(p, q-p) + soext; | 
|---|
| 174 | else soname = tmpDir + oname.substr(p) + soext; | 
|---|
| 175 | } | 
|---|
| 176 | string cmd; | 
|---|
| 177 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ; | 
|---|
| 178 | if (verbose) | 
|---|
| 179 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl; | 
|---|
| 180 | int rc = system(cmd.c_str()); | 
|---|
| 181 | if (rc != 0) | 
|---|
| 182 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl; | 
|---|
| 183 |  | 
|---|
| 184 | return(rc); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|