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