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