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-g++
|
---|
16 | - Linux-KCC
|
---|
17 | - MacOS X/Darwin (Apple OS X 10.2 and 10.3)
|
---|
18 | - OSF-cxx : (HP/Compaq/Digital OSF-Tru64 and cxx c++ compiler)
|
---|
19 | - SGI-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 * cxx_opt =
|
---|
42 | "-O -no_implicit_include ";
|
---|
43 | static char * SGICC_opt = "-O -prelink -D__SGICC__ -LANG:std";
|
---|
44 |
|
---|
45 | /* --Methode-- */
|
---|
46 | /*!
|
---|
47 | Constructor
|
---|
48 | \param fglibsophya : if \c true libsophya.so is used when linking
|
---|
49 | \param fglibextsophya : if \c true libextsophya.so is used when linking
|
---|
50 | \param fglibpi : if \c true libPI.so is used when linking
|
---|
51 | */
|
---|
52 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
|
---|
53 | : verbose(false)
|
---|
54 | {
|
---|
55 |
|
---|
56 | string syscomp = "";
|
---|
57 |
|
---|
58 | #if defined(OSF1)
|
---|
59 | cppFlags += "-DOSF1" ;
|
---|
60 | syscomp = "OSF1-";
|
---|
61 | #elif defined(Linux)
|
---|
62 | cppFlags += "-DLinux" ;
|
---|
63 | syscomp = "Linux-";
|
---|
64 | #elif defined(SunOS)
|
---|
65 | cppFlags += "-DSunOS" ;
|
---|
66 | syscomp = "SunOS-";
|
---|
67 | #elif defined(IRIX64)
|
---|
68 | cppFlags += "-DIRIX64" ;
|
---|
69 | syscomp = "IRIX64-";
|
---|
70 | #elif defined(AIX)
|
---|
71 | cppFlags += "-DAIX" ;
|
---|
72 | syscomp = "AIX-";
|
---|
73 | #elif defined(HPUX)
|
---|
74 | cppFlags += "-DHPUX" ;
|
---|
75 | syscomp = "HPUX-";
|
---|
76 | #elif defined(Darwin)
|
---|
77 | cppFlags += "-DDarwin";
|
---|
78 | syscomp = "Darwin-";
|
---|
79 | #endif
|
---|
80 |
|
---|
81 |
|
---|
82 | #if defined( __GNUG__ )
|
---|
83 | compCmd = "g++ ";
|
---|
84 | compOptions = gcxx_opt;
|
---|
85 | linkOptions = "-O -shared";
|
---|
86 | syscomp += "g++/";
|
---|
87 | #elif defined( __DECCXX )
|
---|
88 | compCmd = "cxx ";
|
---|
89 | compOptions = cxx_opt;
|
---|
90 | linkOptions = compOptions + "-shared";
|
---|
91 | syscomp += "cxx/";
|
---|
92 | #elif defined( __KCC__ )
|
---|
93 | compCmd = "KCC ";
|
---|
94 | compOptions = KCC_opt;
|
---|
95 | linkOptions = KCC_opt;
|
---|
96 | syscomp += "KCC/";
|
---|
97 | #elif defined( __SGICC__ )
|
---|
98 | compCmd = "CC ";
|
---|
99 | compOptions = SGICC_opt;
|
---|
100 | linkOptions = "-shared -O -LANG:std";
|
---|
101 | syscomp += "CC/";
|
---|
102 | #ifdef SGI_ARCH64
|
---|
103 | compOptions += " -64 -DSGI_ARCH64 ";
|
---|
104 | linkOptions += " -64 ";
|
---|
105 | #endif
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | #ifndef Darwin
|
---|
109 | linkCmd = compCmd;
|
---|
110 | #else
|
---|
111 | linkCmd = "c++ -bundle ";
|
---|
112 | linkOptions = "-lSystem -lm";
|
---|
113 | #endif
|
---|
114 |
|
---|
115 | cppFlags += " -I. ";
|
---|
116 | compOptions += " -c ";
|
---|
117 |
|
---|
118 | bool fgenv1 = false;
|
---|
119 | bool fgenv2 = false;
|
---|
120 | char* varenv=NULL;
|
---|
121 | varenv=getenv("SOPHYABASE");
|
---|
122 | if (varenv) {
|
---|
123 | fgenv1 = true;
|
---|
124 | string sbaserep = varenv;
|
---|
125 | if (sbaserep[sbaserep.length()-1] != '/') sbaserep += '/';
|
---|
126 | cppFlags += ( " -I" + sbaserep + "include/ ");
|
---|
127 | linkOptions += " -L" + sbaserep + "slb/";
|
---|
128 | }
|
---|
129 | varenv=getenv("SOPHYABASEREP");
|
---|
130 | if (varenv) {
|
---|
131 | fgenv2 = true;
|
---|
132 | string dpcbase = varenv;
|
---|
133 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
134 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
135 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
136 | char * varenvextl=getenv("EXTLIBDIR");
|
---|
137 | if (varenvextl) {
|
---|
138 | string extlib = varenvextl;
|
---|
139 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
140 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
141 | }
|
---|
142 | }
|
---|
143 | if (!fgenv1 && !fgenv2)
|
---|
144 | cout << " CxxCompilerLinker()/Warning : SOPHYABASE not defined" << endl;
|
---|
145 | if (fgenv1 && fgenv2)
|
---|
146 | cout << " CxxCompilerLinker()/Warning : both SOPHYABASE and SOPHYABASEREP defined" << endl;
|
---|
147 |
|
---|
148 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
149 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
150 | if (fglibpi) linkOptions += " -lPI ";
|
---|
151 | linkOptions += " -lm ";
|
---|
152 |
|
---|
153 |
|
---|
154 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
155 | tmpDir = varenv;
|
---|
156 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /* --Methode-- */
|
---|
161 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
162 | {
|
---|
163 | }
|
---|
164 |
|
---|
165 | /* --Methode-- */
|
---|
166 | /*!
|
---|
167 | Compiles the file \c name using the C++ compiler driver and produces
|
---|
168 | the output object file \c oname. If no output name is specified,
|
---|
169 | a default output file name is made from the input name, with the
|
---|
170 | suffix .o , the in temporary directory.
|
---|
171 | \param name : input C++ source file
|
---|
172 | \param oname : output object file
|
---|
173 | */
|
---|
174 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
175 | {
|
---|
176 | if (oname.length() < 1) {
|
---|
177 | size_t l,p,q;
|
---|
178 | l = name.length();
|
---|
179 | p = name.rfind('/');
|
---|
180 | if (p >= l) p = 0;
|
---|
181 | else p++;
|
---|
182 | q = name.rfind('.');
|
---|
183 | if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
184 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
185 | }
|
---|
186 | string cmd;
|
---|
187 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
188 | if (verbose)
|
---|
189 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
190 | int rc = system(cmd.c_str());
|
---|
191 | if (rc != 0)
|
---|
192 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
193 |
|
---|
194 | return(rc);
|
---|
195 | }
|
---|
196 |
|
---|
197 | /* --Methode-- */
|
---|
198 | /*!
|
---|
199 | Creates a shared library from the object file \c oname.
|
---|
200 | If no output name \c soname is specified,
|
---|
201 | a default output file name is made from the object name, with the
|
---|
202 | suffix .so , in the temporary directory.
|
---|
203 | \param oname : input object file
|
---|
204 | \param soname : shared library name
|
---|
205 | */
|
---|
206 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
207 | {
|
---|
208 | // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
209 | char * soext = ".so";
|
---|
210 |
|
---|
211 | if (soname.length() < 1) {
|
---|
212 | size_t l,p,q;
|
---|
213 | l = oname.length();
|
---|
214 | p = oname.rfind('/');
|
---|
215 | if (p >= l) p = 0;
|
---|
216 | else p++;
|
---|
217 | q = oname.rfind('.');
|
---|
218 | if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
219 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
220 | }
|
---|
221 | string cmd;
|
---|
222 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
223 | if (verbose)
|
---|
224 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
225 | int rc = system(cmd.c_str());
|
---|
226 | if (rc != 0)
|
---|
227 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
228 |
|
---|
229 | return(rc);
|
---|
230 | }
|
---|
231 |
|
---|