1 | // -*- Mode: C++ -*- |
---|
2 | // |
---|
3 | // CForall Version 1.0, Copyright (C) Peter A. Buhr 2002 |
---|
4 | // |
---|
5 | // cfa.cc -- |
---|
6 | // |
---|
7 | // Author : Peter A. Buhr |
---|
8 | // Created On : Tue Aug 20 13:44:49 2002 |
---|
9 | // Last Modified By : Peter A. Buhr |
---|
10 | // Last Modified On : Fri Oct 17 18:03:15 2014 |
---|
11 | // Update Count : 95 |
---|
12 | // |
---|
13 | |
---|
14 | #include <iostream> |
---|
15 | #include <cstdio> // perror |
---|
16 | #include <cstdlib> // putenv, exit |
---|
17 | #include <unistd.h> // execvp |
---|
18 | #include <string> // STL version |
---|
19 | |
---|
20 | #include "config.h" // configure info |
---|
21 | |
---|
22 | using std::cerr; |
---|
23 | using std::endl; |
---|
24 | using std::string; |
---|
25 | |
---|
26 | |
---|
27 | //#define __DEBUG_H__ |
---|
28 | |
---|
29 | |
---|
30 | bool prefix( string arg, string pre ) { |
---|
31 | return arg.substr( 0, pre.size() ) == pre; |
---|
32 | } // prefix |
---|
33 | |
---|
34 | |
---|
35 | void shuffle( const char *args[], int S, int E, int N ) { |
---|
36 | // S & E index 1 passed the end so adjust with -1 |
---|
37 | #ifdef __DEBUG_H__ |
---|
38 | cerr << "shuffle:" << S << " " << E << " " << N << endl; |
---|
39 | #endif // __DEBUG_H__ |
---|
40 | for ( int j = E-1 + N; j > S-1 + N; j -=1 ) { |
---|
41 | #ifdef __DEBUG_H__ |
---|
42 | cerr << "\t" << j << " " << j-N << endl; |
---|
43 | #endif // __DEBUG_H__ |
---|
44 | args[j] = args[j-N]; |
---|
45 | } // for |
---|
46 | } // shuffle |
---|
47 | |
---|
48 | |
---|
49 | int main( int argc, char *argv[] ) { |
---|
50 | string Version( VERSION ); // current version number from CONFIG |
---|
51 | string Major( "0" ), Minor( "0" ), Patch( "0" ); // default version numbers |
---|
52 | int posn1 = Version.find( "." ); // find the divider between major and minor version numbers |
---|
53 | if ( posn1 == -1 ) { // not there ? |
---|
54 | Major = Version; |
---|
55 | } else { |
---|
56 | Major = Version.substr( 0, posn1 ); |
---|
57 | int posn2 = Version.find( ".", posn1 + 1 ); // find the divider between minor and patch numbers |
---|
58 | if ( posn2 == -1 ) { // not there ? |
---|
59 | Minor = Version.substr( posn1 ); |
---|
60 | } else { |
---|
61 | Minor = Version.substr( posn1 + 1, posn2 - posn1 - 1 ); |
---|
62 | Patch = Version.substr( posn2 + 1 ); |
---|
63 | } // if |
---|
64 | } // if |
---|
65 | |
---|
66 | string installlibdir( CFA_LIBDIR ); // fixed location of the cpp and cfa-cpp commands |
---|
67 | |
---|
68 | string heading; // banner printed at start of cfa compilation |
---|
69 | string arg; // current command-line argument during command-line parsing |
---|
70 | string Bprefix; // path where gcc looks for compiler command steps |
---|
71 | string langstd; // language standard |
---|
72 | |
---|
73 | string compiler_path( GCC_PATH ); // path/name of C compiler |
---|
74 | string compiler_name; // name of C compiler |
---|
75 | |
---|
76 | bool nonoptarg = false; // indicates non-option argument specified |
---|
77 | bool link = true; // linking as well as compiling |
---|
78 | bool verbose = false; // -v flag |
---|
79 | bool quiet = false; // -quiet flag |
---|
80 | bool debug = true; // -debug flag |
---|
81 | bool help = false; // -help flag |
---|
82 | bool CFA_flag = false; // -CFA flag |
---|
83 | bool cpp_flag = false; // -E or -M flag, preprocessor only |
---|
84 | bool debugging = false; // -g flag |
---|
85 | |
---|
86 | const char *args[argc + 100]; // cfa command line values, plus some space for additional flags |
---|
87 | int sargs = 1; // starting location for arguments in args list |
---|
88 | int nargs = sargs; // number of arguments in args list; 0 => command name |
---|
89 | |
---|
90 | const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags |
---|
91 | int nlibs = 0; |
---|
92 | |
---|
93 | #ifdef __DEBUG_H__ |
---|
94 | cerr << "CFA:" << endl; |
---|
95 | #endif // __DEBUG_H__ |
---|
96 | |
---|
97 | // process command-line arguments |
---|
98 | |
---|
99 | for ( int i = 1; i < argc; i += 1 ) { |
---|
100 | #ifdef __DEBUG_H__ |
---|
101 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
102 | #endif // __DEBUG_H__ |
---|
103 | arg = argv[i]; // convert to string value |
---|
104 | #ifdef __DEBUG_H__ |
---|
105 | cerr << "arg:\"" << arg << "\"" << endl; |
---|
106 | #endif // __DEBUG_H__ |
---|
107 | if ( prefix( arg, "-" ) ) { |
---|
108 | // pass through arguments |
---|
109 | |
---|
110 | if ( arg == "-Xlinker" || arg == "-o" ) { |
---|
111 | args[nargs] = argv[i]; // pass the argument along |
---|
112 | nargs += 1; |
---|
113 | i += 1; |
---|
114 | if ( i == argc ) continue; // next argument available ? |
---|
115 | args[nargs] = argv[i]; // pass the argument along |
---|
116 | nargs += 1; |
---|
117 | } else if ( arg == "-XCFA" ) { // CFA pass through |
---|
118 | i += 1; |
---|
119 | args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str(); |
---|
120 | nargs += 1; |
---|
121 | |
---|
122 | // CFA specific arguments |
---|
123 | |
---|
124 | } else if ( arg == "-CFA" ) { |
---|
125 | CFA_flag = true; // strip the -CFA flag |
---|
126 | link = false; |
---|
127 | args[nargs] = "-E"; // replace the argument with -E |
---|
128 | nargs += 1; |
---|
129 | } else if ( arg == "-debug" ) { |
---|
130 | debug = true; // strip the debug flag |
---|
131 | } else if ( arg == "-nodebug" ) { |
---|
132 | debug = false; // strip the nodebug flag |
---|
133 | } else if ( arg == "-quiet" ) { |
---|
134 | quiet = true; // strip the quiet flag |
---|
135 | } else if ( arg == "-noquiet" ) { |
---|
136 | quiet = false; // strip the noquiet flag |
---|
137 | } else if ( arg == "-help" ) { |
---|
138 | help = true; // strip the help flag |
---|
139 | } else if ( arg == "-nohelp" ) { |
---|
140 | help = false; // strip the nohelp flag |
---|
141 | |
---|
142 | // C++ specific arguments |
---|
143 | |
---|
144 | } else if ( arg == "-v" ) { |
---|
145 | verbose = true; // verbosity required |
---|
146 | args[nargs] = argv[i]; // pass the argument along |
---|
147 | nargs += 1; |
---|
148 | } else if ( arg == "-g" ) { |
---|
149 | debugging = true; // symbolic debugging required |
---|
150 | args[nargs] = argv[i]; // pass the argument along |
---|
151 | nargs += 1; |
---|
152 | } else if ( prefix( arg, "-B" ) ) { |
---|
153 | Bprefix = arg.substr(2); // strip the -B flag |
---|
154 | args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); |
---|
155 | nargs += 1; |
---|
156 | } else if ( prefix( arg, "-b" ) ) { |
---|
157 | if ( arg.length() == 2 ) { // separate argument ? |
---|
158 | i += 1; |
---|
159 | if ( i == argc ) continue; // next argument available ? |
---|
160 | arg += argv[i]; // concatenate argument |
---|
161 | } // if |
---|
162 | // later versions of gcc require the -b option to appear at the start of the command line |
---|
163 | shuffle( args, sargs, nargs, 1 ); // make room at front of argument list |
---|
164 | args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along |
---|
165 | if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) { |
---|
166 | cerr << argv[0] << " error, cannot set environment variable." << endl; |
---|
167 | exit( EXIT_FAILURE ); |
---|
168 | } // if |
---|
169 | sargs += 1; |
---|
170 | nargs += 1; |
---|
171 | } else if ( prefix( arg, "-V" ) ) { |
---|
172 | if ( arg.length() == 2 ) { // separate argument ? |
---|
173 | i += 1; |
---|
174 | if ( i == argc ) continue; // next argument available ? |
---|
175 | arg += argv[i]; // concatenate argument |
---|
176 | } // if |
---|
177 | // later versions of gcc require the -V option to appear at the start of the command line |
---|
178 | shuffle( args, sargs, nargs, 1 ); // make room at front of argument list |
---|
179 | args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along |
---|
180 | if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) { |
---|
181 | cerr << argv[0] << " error, cannot set environment variable." << endl; |
---|
182 | exit( EXIT_FAILURE ); |
---|
183 | } // if |
---|
184 | sargs += 1; |
---|
185 | nargs += 1; |
---|
186 | } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) { |
---|
187 | args[nargs] = argv[i]; // pass the argument along |
---|
188 | nargs += 1; |
---|
189 | if ( arg == "-E" || arg == "-M" || arg == "-MM" ) { |
---|
190 | cpp_flag = true; // cpp only |
---|
191 | } // if |
---|
192 | link = false; // no linkage required |
---|
193 | } else if ( arg[1] == 'l' ) { |
---|
194 | // if the user specifies a library, load it after user code |
---|
195 | libs[nlibs] = argv[i]; |
---|
196 | nlibs += 1; |
---|
197 | } else { |
---|
198 | // concatenate any other arguments |
---|
199 | args[nargs] = argv[i]; |
---|
200 | nargs += 1; |
---|
201 | } // if |
---|
202 | } else { |
---|
203 | // concatenate other arguments |
---|
204 | args[nargs] = argv[i]; |
---|
205 | nargs += 1; |
---|
206 | nonoptarg = true; |
---|
207 | } // if |
---|
208 | } // for |
---|
209 | |
---|
210 | #ifdef __DEBUG_H__ |
---|
211 | cerr << "args:"; |
---|
212 | for ( int i = 1; i < nargs; i += 1 ) { |
---|
213 | cerr << " " << args[i]; |
---|
214 | } // for |
---|
215 | cerr << endl; |
---|
216 | #endif // __DEBUG_H__ |
---|
217 | |
---|
218 | if ( cpp_flag && CFA_flag ) { |
---|
219 | cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl; |
---|
220 | exit( EXIT_FAILURE ); |
---|
221 | } // if |
---|
222 | |
---|
223 | string d; |
---|
224 | if ( debug ) { |
---|
225 | d = "-d"; |
---|
226 | } // if |
---|
227 | |
---|
228 | if ( link ) { |
---|
229 | // include the cfa library in case it's needed |
---|
230 | args[nargs] = "-L" CFA_LIBDIR; |
---|
231 | nargs += 1; |
---|
232 | args[nargs] = "-lcfa"; |
---|
233 | nargs += 1; |
---|
234 | } // if |
---|
235 | |
---|
236 | // add the correct set of flags based on the type of compile this is |
---|
237 | |
---|
238 | args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str(); |
---|
239 | nargs += 1; |
---|
240 | args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str(); |
---|
241 | nargs += 1; |
---|
242 | |
---|
243 | if ( cpp_flag ) { |
---|
244 | args[nargs] = "-D__CPP__"; |
---|
245 | nargs += 1; |
---|
246 | } // if |
---|
247 | |
---|
248 | if ( CFA_flag ) { |
---|
249 | args[nargs] = "-D__CFA__"; |
---|
250 | nargs += 1; |
---|
251 | } // if |
---|
252 | |
---|
253 | if ( debug ) { |
---|
254 | heading += " (debug)"; |
---|
255 | args[nargs] = "-D__CFA_DEBUG__"; |
---|
256 | nargs += 1; |
---|
257 | } else { |
---|
258 | heading += " (no debug)"; |
---|
259 | } // if |
---|
260 | |
---|
261 | if ( Bprefix.length() == 0 ) { |
---|
262 | Bprefix = installlibdir; |
---|
263 | args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); |
---|
264 | nargs += 1; |
---|
265 | } // if |
---|
266 | |
---|
267 | // execute the compilation command |
---|
268 | |
---|
269 | args[0] = compiler_path.c_str(); // set compiler command for exec |
---|
270 | // find actual name of the compiler independent of the path to it |
---|
271 | int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/' |
---|
272 | if ( p == -1 ) { |
---|
273 | compiler_name = compiler_path; |
---|
274 | } else { |
---|
275 | compiler_name = *new string( compiler_path.substr( p + 1 ) ); |
---|
276 | } // if |
---|
277 | |
---|
278 | if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name |
---|
279 | args[nargs] = "-no-integrated-cpp"; |
---|
280 | nargs += 1; |
---|
281 | args[nargs] = "-Wno-deprecated"; |
---|
282 | nargs += 1; |
---|
283 | args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str(); |
---|
284 | nargs += 1; |
---|
285 | } else { |
---|
286 | cerr << argv[0] << " error, compiler " << compiler_name << " not supported." << endl; |
---|
287 | exit( EXIT_FAILURE ); |
---|
288 | } // if |
---|
289 | |
---|
290 | for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries |
---|
291 | args[nargs] = libs[i]; |
---|
292 | nargs += 1; |
---|
293 | } // for |
---|
294 | |
---|
295 | args[nargs] = NULL; // terminate with NULL |
---|
296 | |
---|
297 | #ifdef __DEBUG_H__ |
---|
298 | cerr << "nargs: " << nargs << endl; |
---|
299 | cerr << "args:" << endl; |
---|
300 | for ( int i = 0; args[i] != NULL; i += 1 ) { |
---|
301 | cerr << " \"" << args[i] << "\"" << endl; |
---|
302 | } // for |
---|
303 | #endif // __DEBUG_H__ |
---|
304 | |
---|
305 | if ( ! quiet ) { |
---|
306 | cerr << "CFA " << "Version " << Version << heading << endl; |
---|
307 | |
---|
308 | if ( help ) { |
---|
309 | cerr << |
---|
310 | "-debug\t\t\t: use cfa runtime with debug checking" << endl << |
---|
311 | "-help\t\t\t: print this help message" << endl << |
---|
312 | "-quiet\t\t\t: print no messages from the cfa command" << endl << |
---|
313 | "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl << |
---|
314 | "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl << |
---|
315 | "...\t\t\t: any other " << compiler_name << " flags" << endl; |
---|
316 | } // if |
---|
317 | } // if |
---|
318 | |
---|
319 | if ( verbose ) { |
---|
320 | if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc |
---|
321 | |
---|
322 | for ( int i = 0; args[i] != NULL; i += 1 ) { |
---|
323 | cerr << args[i] << " "; |
---|
324 | } // for |
---|
325 | cerr << endl; |
---|
326 | } // if |
---|
327 | |
---|
328 | if ( ! nonoptarg ) { |
---|
329 | cerr << argv[0] << " error, no input files" << endl; |
---|
330 | exit( EXIT_FAILURE ); |
---|
331 | } // if |
---|
332 | |
---|
333 | // execute the command and return the result |
---|
334 | |
---|
335 | execvp( args[0], (char *const *)args ); // should not return |
---|
336 | perror( "CFA Translator error: cfa level, execvp" ); |
---|
337 | exit( EXIT_FAILURE ); |
---|
338 | } // main |
---|
339 | |
---|
340 | // Local Variables: // |
---|
341 | // compile-command: "make install" // |
---|
342 | // End: // |
---|