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