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

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

File size: 5.8 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-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
37static char * gcxx_opt =
38"-O -Wall -Wpointer-arith -Wmissing-prototypes -Wsynth -fdollars-in-identifiers";
39static char * KCC_opt =
40"-O --exceptions --rtti --auto_instantiation --one_instantiation_per_object -D__KCC__";
41static char * cxx_opt =
42"-O -no_implicit_include ";
43static 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*/
52CxxCompilerLinker::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 string dpcbase;
119 char* varenv=NULL;
120 varenv=getenv("SOPHYABASEREP");
121 if (varenv) {
122 dpcbase = varenv;
123 if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
124 cppFlags += ( " -I" + dpcbase + "Include/ ");
125 linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
126 if (fglibsophya) linkOptions += " -lsophya ";
127 if (fglibextsophya) linkOptions += " -lextsophya ";
128 if (fglibpi) linkOptions += " -lPI ";
129 }
130 linkOptions += " -lm ";
131
132 string extlib;
133 varenv=getenv("EXTLIBDIR");
134 if (varenv) {
135 extlib = varenv;
136 if (extlib[extlib.length()-1] != '/') extlib += '/';
137 cppFlags += ( " -I" + extlib +"Include/ ");
138 }
139
140 if ( (varenv=getenv("TMPDIR")) != NULL ) {
141 tmpDir = varenv;
142 if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
143 }
144}
145
146/* --Methode-- */
147CxxCompilerLinker::~CxxCompilerLinker()
148{
149}
150
151/* --Methode-- */
152/*!
153 Compiles the file \c name using the C++ compiler driver and produces
154 the output object file \c oname. If no output name is specified,
155 a default output file name is made from the input name, with the
156 suffix .o , the in temporary directory.
157 \param name : input C++ source file
158 \param oname : output object file
159*/
160int CxxCompilerLinker::Compile(string const & name, string & oname)
161{
162 if (oname.length() < 1) {
163 size_t l,p,q;
164 l = name.length();
165 p = name.rfind('/');
166 if (p >= l) p = 0;
167 else p++;
168 q = name.rfind('.');
169 if ((q < l) && (q > p)) oname = tmpDir + name.substr(p, q-p) + ".o";
170 else oname = tmpDir + name.substr(p) + ".o";
171 }
172 string cmd;
173 cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
174 if (verbose)
175 cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
176 int rc = system(cmd.c_str());
177 if (rc != 0)
178 cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
179
180 return(rc);
181}
182
183/* --Methode-- */
184/*!
185 Creates a shared library from the object file \c oname.
186 If no output name \c soname is specified,
187 a default output file name is made from the object name, with the
188 suffix .so , in the temporary directory.
189 \param oname : input object file
190 \param soname : shared library name
191*/
192int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
193{
194 // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
195 char * soext = ".so";
196
197 if (soname.length() < 1) {
198 size_t l,p,q;
199 l = oname.length();
200 p = oname.rfind('/');
201 if (p >= l) p = 0;
202 else p++;
203 q = oname.rfind('.');
204 if ((q < l) && (q > p)) soname = tmpDir + oname.substr(p, q-p) + soext;
205 else soname = tmpDir + oname.substr(p) + soext;
206 }
207 string cmd;
208 cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
209 if (verbose)
210 cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
211 int rc = system(cmd.c_str());
212 if (rc != 0)
213 cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
214
215 return(rc);
216}
217
Note: See TracBrowser for help on using the repository browser.