source: Sophya/trunk/SophyaLib/SysTools/cxxcmplnk.cc@ 3577

Last change on this file since 3577 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

File size: 7.6 KB
Line 
1// Gestionnaire de compilation-linker C++ - R. Ansari 10/2000
2// LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
3
4#include "sopnamsp.h"
5#include "cxxcmplnk.h"
6#include "sspvflags.h"
7#include <iostream>
8
9/*!
10 \class SOPHYA::CxxCompilerLinker
11 \ingroup SysTools
12 This classes handles the compilation of a C++ source code and
13 building of a shared library.
14 The present version has been adapted for different compilers and
15 systems:
16 - Linux + GNU compiler (g++)
17 - Linux + Intel compiler (icc)
18 - Darwin + GNU compiler (MaxOS X 10.3 and 10.4)
19 - OSF1 + cxx (HP/Compaq/Digital OSF-Tru64 and cxx c++ compiler)
20 - IRIX64 + CC : Silicon Graphics system and C++ compiler
21 - AIX + xlC : IBM system and C++ compiler
22
23 \sa SOPHYA::PDynLinkMgr
24
25 \code
26 #include "cxxcmplnk.h"
27 CxxCompilerLinker cxx;
28 string name = "toto.cc";
29 string oname = "toto.o";
30 string soname = "toto.so";
31 int rc;
32 // compiling file
33 rc = cxx.Compile(name, oname);
34 // linking and building the shared object
35 rc = cxx.BuildSO(oname, soname);
36 \endcode
37*/
38
39static const char * gcxx_opt = "-O -fdollars-in-identifiers";
40static const char * KCC_opt =
41 "-O --exceptions --rtti --auto_instantiation --one_instantiation_per_object -D__KCC__";
42static const char * icc_opt = "-O -fpic -frtti";
43static const char * cxx_opt =
44 "-O -no_implicit_include -pthread";
45static const char * SGICC_opt = "-O -prelink -D__SGICC__ -LANG:std";
46static const char * xlC_opt = "-O -qrtti=all -qeh=v6";
47
48/* --Methode-- */
49/*!
50 Constructor
51 \param fglibsophya : if \c true libsophya.so is used when linking
52 \param fglibextsophya : if \c true libextsophya.so is used when linking
53 \param fglibpi : if \c true libPI.so is used when linking
54*/
55CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
56 : verbose(false)
57{
58
59 string syscomp = "";
60
61#if defined(OSF1)
62 cppFlags += "-DOSF1" ;
63 syscomp = "OSF1-";
64#elif defined(Linux)
65 cppFlags += "-DLinux" ;
66 syscomp = "Linux-";
67#elif defined(SunOS)
68 cppFlags += "-DSunOS" ;
69 syscomp = "SunOS-";
70#elif defined(IRIX64)
71 cppFlags += "-DIRIX64" ;
72 syscomp = "IRIX64-";
73#elif defined(AIX)
74 cppFlags += "-DAIX" ;
75 syscomp = "AIX-";
76#elif defined(HPUX)
77 cppFlags += "-DHPUX" ;
78 syscomp = "HPUX-";
79#elif defined(Darwin)
80 cppFlags += "-DDarwin";
81 syscomp = "Darwin-";
82#endif
83
84
85#if defined( __GNUG__ )
86 compCmd = "g++ ";
87 compOptions = gcxx_opt;
88 linkOptions = "-O -shared";
89 syscomp += "g++/";
90#ifndef SO_NOFPIC
91 compOptions += " -fPIC ";
92 linkOptions += " -fPIC ";
93#endif
94#endif
95#if defined( __DECCXX )
96 compCmd = "cxx ";
97 compOptions = cxx_opt;
98 linkOptions = compOptions + " -shared";
99 syscomp += "cxx/";
100#endif
101#if defined( __KCC__ )
102 compCmd = "KCC ";
103 compOptions = KCC_opt;
104 linkOptions = KCC_opt;
105 syscomp += "KCC/";
106#endif
107#if defined( __SGICC__ )
108 compCmd = "CC ";
109 compOptions = SGICC_opt;
110 linkOptions = "-shared -O -LANG:std";
111 syscomp += "CC/";
112#ifdef SO_ARCH64
113 compOptions += " -64 ";
114 linkOptions += " -64 ";
115#endif
116#endif
117#if defined( __INTEL_COMPILER )
118 compCmd = "icpc ";
119 compOptions = icc_opt;
120 linkOptions = icc_opt + string(" -shared");
121 syscomp += "icc/";
122#endif
123#if defined( __IBMCPP__ )
124 compCmd = "xlC ";
125 compOptions = xlC_opt;
126 linkOptions = xlC_opt + string(" -brtl -qmkshrobj ");
127 syscomp += "xlC/";
128#ifdef SO_ARCH64
129 compOptions += " -q64 ";
130 linkOptions += " -q64 ";
131#endif
132#endif
133
134#ifndef Darwin
135 linkCmd = compCmd;
136#else
137 linkCmd = "c++ -bundle ";
138 linkOptions = "-lSystem -lm";
139#endif
140
141 cppFlags += " -I. ";
142 compOptions += " -c ";
143
144 bool fgenv1 = false;
145 bool fgenv2 = false;
146 char* varenv=NULL;
147 varenv=getenv("SOPHYABASE");
148 string sbaserep = "./";
149 if (varenv) {
150 fgenv1 = true;
151 sbaserep = varenv;
152 if (sbaserep[sbaserep.length()-1] != '/') sbaserep += '/';
153 cppFlags += ( " -I" + sbaserep + "include/ ");
154 linkOptions += " -L" + sbaserep + "slb/";
155 }
156 varenv=getenv("SOPHYABASEREP");
157 if (varenv) {
158 fgenv2 = true;
159 string dpcbase = varenv;
160 if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
161 cppFlags += ( " -I" + dpcbase + "Include/ ");
162 linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
163 char * varenvextl=getenv("EXTLIBDIR");
164 if (varenvextl) {
165 string extlib = varenvextl;
166 if (extlib[extlib.length()-1] != '/') extlib += '/';
167 cppFlags += ( " -I" + extlib +"Include/ ");
168 }
169 }
170 if (!fgenv1 && !fgenv2)
171 cout << " CxxCompilerLinker()/Warning : SOPHYABASE not defined" << endl;
172 if (fgenv1 && fgenv2)
173 cout << " CxxCompilerLinker()/Warning : both SOPHYABASE and SOPHYABASEREP defined" << endl;
174
175#if defined( SO_SLBALLINONE )
176 if (fglibsophya || fglibpi) linkOptions += " -lAsophyaextPI ";
177 /*
178 cout << " +++ DBG/CxxCompilerLinker() - in AIX fglibpi= "
179 << ((fglibpi) ? " true " : " false ") << endl;
180 // Reza:Dec 2005 : pb avec les programmes PI sur AIX si linke avec shared lib PI
181 // (Portage sur regatta.calcul.u-psud.fr AIX 5.3 , libXm Xt X11 en .a uniquement)
182 if (fglibpi) {
183 linkOptions += " -L" + sbaserep + "lib/";
184 linkOptions += " -lPIext -lPIGcont -lPI -lXm -lXt -lX11 -lpthread ";
185 }
186 */
187 cout << "CxxCompilerLinker()/Warning: SINGLESLB_SOPHYA_PI_EXT -> linkOptions= " << linkOptions << endl;
188#else
189 if (fglibsophya) linkOptions += " -lsophya ";
190 if (fglibextsophya) linkOptions += " -lextsophya ";
191 if (fglibpi) linkOptions += " -lPI ";
192#endif
193 linkOptions += " -lm ";
194
195
196 if ( (varenv=getenv("TMPDIR")) != NULL ) {
197 tmpDir = varenv;
198 if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
199 }
200}
201
202/* --Methode-- */
203CxxCompilerLinker::~CxxCompilerLinker()
204{
205}
206
207/* --Methode-- */
208/*!
209 Compiles the file \c name using the C++ compiler driver and produces
210 the output object file \c oname. If no output name is specified,
211 a default output file name is made from the input name, with the
212 suffix .o , the in temporary directory.
213 \param name : input C++ source file
214 \param oname : output object file
215*/
216int CxxCompilerLinker::Compile(string const & name, string & oname)
217{
218 if (oname.length() < 1) {
219 size_t l,p,q;
220 l = name.length();
221 p = name.rfind('/');
222 if (p >= l) p = 0;
223 else p++;
224 q = name.rfind('.');
225 if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
226 else oname = tmpDir + name.substr(p) + ".o";
227 }
228 string cmd;
229 cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
230 if (verbose)
231 cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
232 int rc = system(cmd.c_str());
233 if (rc != 0)
234 cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
235
236 return(rc);
237}
238
239/* --Methode-- */
240/*!
241 Creates a shared library from the object file \c oname.
242 If no output name \c soname is specified,
243 a default output file name is made from the object name, with the
244 suffix .so , in the temporary directory.
245 \param oname : input object file
246 \param soname : shared library name
247*/
248int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
249{
250 // const char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
251 const char * soext = ".so";
252
253 if (soname.length() < 1) {
254 size_t l,p,q;
255 l = oname.length();
256 p = oname.rfind('/');
257 if (p >= l) p = 0;
258 else p++;
259 q = oname.rfind('.');
260 if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
261 else soname = tmpDir + oname.substr(p) + soext;
262 }
263 string cmd;
264 cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
265 if (verbose)
266 cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
267 int rc = system(cmd.c_str());
268 if (rc != 0)
269 cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
270
271 return(rc);
272}
273
Note: See TracBrowser for help on using the repository browser.