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

Last change on this file since 3014 was 3014, checked in by ansari, 19 years ago

Prise en compte du flag de compil SINGLE_SLB ds CxxCompilerLinker et MAJ makefile pour sspvflags.h , Reza 11/7/2006

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