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()
|
---|
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 | cppFlags += " -I. ";
|
---|
95 | compOptions += " -c ";
|
---|
96 |
|
---|
97 | string dpcbase;
|
---|
98 | char* varenv=NULL;
|
---|
99 | varenv=getenv("DPCBASEREP");
|
---|
100 | if (varenv) {
|
---|
101 | dpcbase = varenv;
|
---|
102 | if (dpcbase[dpcbase.length()-1] != '/') dpcbase += '/';
|
---|
103 | cppFlags += ( " -I" + dpcbase + "Include/ ");
|
---|
104 | linkOptions += " -L" + dpcbase + syscomp + "ShLibs/ -lextsophya -lsophya -lm ";
|
---|
105 | }
|
---|
106 |
|
---|
107 | string extlib;
|
---|
108 | varenv=getenv("EXTLIBDIR");
|
---|
109 | if (varenv) {
|
---|
110 | extlib = varenv;
|
---|
111 | if (extlib[extlib.length()-1] != '/') extlib += '/';
|
---|
112 | cppFlags += ( " -I" + extlib +"Include/ ");
|
---|
113 | }
|
---|
114 |
|
---|
115 | if ( (varenv=getenv("TMPDIR")) != NULL ) {
|
---|
116 | tmpDir = varenv;
|
---|
117 | if (tmpDir[tmpDir.length()-1] != '/') tmpDir += '/';
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | /* --Methode-- */
|
---|
122 | CxxCompilerLinker::~CxxCompilerLinker()
|
---|
123 | {
|
---|
124 | }
|
---|
125 |
|
---|
126 | /* --Methode-- */
|
---|
127 | int CxxCompilerLinker::Compile(string const & name, string & oname)
|
---|
128 | {
|
---|
129 | if (oname.length() < 1) {
|
---|
130 | size_t l,p,q;
|
---|
131 | l = name.length();
|
---|
132 | p = name.rfind('/');
|
---|
133 | if (p >= l) p = 0;
|
---|
134 | else p++;
|
---|
135 | q = name.find('.');
|
---|
136 | if (q < l) oname = tmpDir + name.substr(p, q-p) + ".o";
|
---|
137 | else oname = tmpDir + name.substr(p) + ".o";
|
---|
138 | }
|
---|
139 | string cmd;
|
---|
140 | cmd = compCmd + cppFlags + compOptions + "-o " + oname + " " + name ;
|
---|
141 | if (verbose)
|
---|
142 | cout << "CxxCompilerLinker::Compile() - Executing \n" << cmd << endl;
|
---|
143 | int rc = system(cmd.c_str());
|
---|
144 | if (rc != 0)
|
---|
145 | cerr << "CxxCompilerLinker::Compile() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
146 |
|
---|
147 | return(rc);
|
---|
148 | }
|
---|
149 |
|
---|
150 | /* --Methode-- */
|
---|
151 | int CxxCompilerLinker::BuildSO(string const & oname, string & soname)
|
---|
152 | {
|
---|
153 | if (soname.length() < 1) {
|
---|
154 | size_t l,p,q;
|
---|
155 | l = oname.length();
|
---|
156 | p = oname.rfind('/');
|
---|
157 | if (p >= l) p = 0;
|
---|
158 | else p++;
|
---|
159 | q = oname.find('.');
|
---|
160 | if (q < l) soname = tmpDir + oname.substr(p, q-p) + ".so";
|
---|
161 | else soname = tmpDir + oname.substr(p) + ".so";
|
---|
162 | }
|
---|
163 | string cmd;
|
---|
164 | cmd = compCmd + linkOptions + "-o " + soname + " " + oname ;
|
---|
165 | if (verbose)
|
---|
166 | cout << "CxxCompilerLinker::BuildSO() - Executing \n" << cmd << endl;
|
---|
167 | int rc = system(cmd.c_str());
|
---|
168 | if (rc != 0)
|
---|
169 | cerr << "CxxCompilerLinker::BuildSO() Error Rc(" << cmd <<")= "<< rc << endl;
|
---|
170 |
|
---|
171 | return(rc);
|
---|
172 | }
|
---|
173 |
|
---|