1 | // Gestionnaire de compilation-linker C++ - R. Ansari 10/2000
|
---|
2 | // LAL (Orsay) / IN2P3-CNRS DAPNIA/SPP (Saclay) / CEA
|
---|
3 |
|
---|
4 | #include "cxxcmplnk.h"
|
---|
5 | #include <iostream.h>
|
---|
6 |
|
---|
7 | /*!
|
---|
8 | \class SOPHYA::CxxCompilerLinker
|
---|
9 | \ingroup SysTools
|
---|
10 | This classes handles the compilation of a C++ source code and
|
---|
11 | building of a shared library.
|
---|
12 | The present version has been adapted for different compilers:
|
---|
13 | g++ , Compaq/Digital cxx , SGI CC , KCC .
|
---|
14 | \sa SOPHYA::PDynLinkMgr
|
---|
15 | \code
|
---|
16 | #include ""cxxcmplnk.h"
|
---|
17 | CxxCompilerLinker cxx;
|
---|
18 | string name = "toto.cc";
|
---|
19 | string oname = "toto.o";
|
---|
20 | string soname = "toto.so";
|
---|
21 | int rc;
|
---|
22 | // compiling file
|
---|
23 | rc = cxx.Compile(name, oname);
|
---|
24 | // linking and building the shared object
|
---|
25 | rc = cxx.BuildSO(oname, soname);
|
---|
26 | \endcode
|
---|
27 | */
|
---|
28 |
|
---|
29 | static char * gcxx_opt =
|
---|
30 | "-O -Wall -Wpointer-arith -Wmissing-prototypes -Wsynth -fdollars-in-identifiers";
|
---|
31 | static char * KCC_opt =
|
---|
32 | "-O --exceptions --rtti --auto_instantiation --one_instantiation_per_object -D__KCC__";
|
---|
33 | static char * cxx_opt =
|
---|
34 | "-O -no_implicit_include ";
|
---|
35 | static char * SGICC_opt = "-O -prelink -D__SGICC__ ";
|
---|
36 |
|
---|
37 | /* --Methode-- */
|
---|
38 | CxxCompilerLinker::CxxCompilerLinker(bool fglibsophya, bool fglibextsophya, bool fglibpi)
|
---|
39 | : verbose(false)
|
---|
40 | {
|
---|
41 |
|
---|
42 | string syscomp = "";
|
---|
43 |
|
---|
44 | #if defined(OSF1)
|
---|
45 | cppFlags += "-DOSF1" ;
|
---|
46 | syscomp = "OSF1-";
|
---|
47 | #elif defined(Linux)
|
---|
48 | cppFlags += "-DLinux" ;
|
---|
49 | syscomp = "Linux-";
|
---|
50 | #elif defined(SunOS)
|
---|
51 | cppFlags += "-DSunOS" ;
|
---|
52 | syscomp = "SunOS-";
|
---|
53 | #elif defined(IRIX64)
|
---|
54 | cppFlags += "-DIRIX64" ;
|
---|
55 | syscomp = "IRIX64-";
|
---|
56 | #elif defined(AIX)
|
---|
57 | cppFlags += "-DAIX" ;
|
---|
58 | syscomp = "AIX-";
|
---|
59 | #elif defined(HPUX)
|
---|
60 | cppFlags += "-DHPUX" ;
|
---|
61 | syscomp = "HPUX-";
|
---|
62 | #elif defined(Darwin)
|
---|
63 | cppFlags += "-DDarwin";
|
---|
64 | syscomp = "Darwin-";
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 | #if defined( __GNUG__ )
|
---|
69 | compCmd = "g++ ";
|
---|
70 | compOptions = gcxx_opt;
|
---|
71 | linkOptions = "-O -shared";
|
---|
72 | syscomp += "g++/";
|
---|
73 | #elif defined( __DECCXX )
|
---|
74 | compCmd = "cxx ";
|
---|
75 | compOptions = cxx_opt;
|
---|
76 | linkOptions = compOptions + "-shared";
|
---|
77 | syscomp += "cxx/";
|
---|
78 | #elif defined( __KCC__ )
|
---|
79 | compCmd = "KCC ";
|
---|
80 | compOptions = KCC_opt;
|
---|
81 | linkOptions = KCC_opt;
|
---|
82 | syscomp += "KCC/";
|
---|
83 | #elif defined( __SGICC__ )
|
---|
84 | compCmd = "CC ";
|
---|
85 | compOptions = SGICC_opt;
|
---|
86 | linkOptions = "-shared -O ";
|
---|
87 | syscomp += "CC/";
|
---|
88 | #ifdef SGI_ARCH64
|
---|
89 | compOptions += " -64 -DSGI_ARCH64 ";
|
---|
90 | linkOptions += " -64 ";
|
---|
91 | #endif
|
---|
92 | #endif
|
---|
93 |
|
---|
94 | #ifndef Darwin
|
---|
95 | linkCmd = compCmd;
|
---|
96 | #else
|
---|
97 | linkCmd = "cc -bundle -flat_namespace -undefined suppress ";
|
---|
98 | linkOptions = "-L/usr/lib/gcc/darwin/default -lstdc++ -lcc_dynamic -lpthread ";
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | cppFlags += " -I. ";
|
---|
102 | compOptions += " -c ";
|
---|
103 |
|
---|
104 | string dpcbase;
|
---|
105 | char* varenv=NULL;
|
---|
106 | varenv=getenv("DPCBASEREP");
|
---|
107 | if (varenv) {
|
---|
108 | dpcbase = varenv;
|
---|
109 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
110 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
111 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/";
|
---|
112 | if (fglibsophya) linkOptions += " -lsophya ";
|
---|
113 | if (fglibextsophya) linkOptions += " -lextsophya ";
|
---|
114 | if (fglibpi) linkOptions += " -lPI ";
|
---|
115 | }
|
---|
116 | linkOptions += " -lm ";
|
---|
117 |
|
---|
118 | string extlib;
|
---|
119 | varenv=getenv("EXTLIBDIR");
|
---|
120 | if (varenv) {
|
---|
121 | extlib = varenv;
|
---|
122 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
123 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
124 | }
|
---|
125 |
|
---|
126 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
127 | tmpDir = varenv;
|
---|
128 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* --Methode-- */
|
---|
133 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
134 | {
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* --Methode-- */
|
---|
138 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
139 | {
|
---|
140 | if (oname.length() < 1) {
|
---|
141 | size_t l,p,q;
|
---|
142 | l = name.length();
|
---|
143 | p = name.rfind('/');
|
---|
144 | if (p >= l) p = 0;
|
---|
145 | else p++;
|
---|
146 | q = name.find('.');
|
---|
147 | if (q < l) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
148 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
149 | }
|
---|
150 | string cmd;
|
---|
151 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
152 | if (verbose)
|
---|
153 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
154 | int rc = system(cmd.c_str());
|
---|
155 | if (rc != 0)
|
---|
156 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
157 |
|
---|
158 | return(rc);
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* --Methode-- */
|
---|
162 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
163 | {
|
---|
164 | // char * soext = ".dylib"; if defined(Darwin) - pas necessaire Reza 02/2002
|
---|
165 | char * soext = ".so";
|
---|
166 |
|
---|
167 | if (soname.length() < 1) {
|
---|
168 | size_t l,p,q;
|
---|
169 | l = oname.length();
|
---|
170 | p = oname.rfind('/');
|
---|
171 | if (p >= l) p = 0;
|
---|
172 | else p++;
|
---|
173 | q = oname.find('.');
|
---|
174 | if (q < l) soname = tmpDir + oname.substr(p, q-p) + soext;
|
---|
175 | else soname = tmpDir + oname.substr(p) + soext;
|
---|
176 | }
|
---|
177 | string cmd;
|
---|
178 | cmd = linkCmd + " " + oname + " " + linkOptions + " -o " + soname + " " ;
|
---|
179 | if (verbose)
|
---|
180 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
181 | int rc = system(cmd.c_str());
|
---|
182 | if (rc != 0)
|
---|
183 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
184 |
|
---|
185 | return(rc);
|
---|
186 | }
|
---|
187 |
|
---|