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