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