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