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

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

petit ajout dans la doc (commentaires) de cxxcmplnk.cc, Reza 06/07/2006

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