source: driver/cfa.cc@ bcb14b5

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since bcb14b5 was dfb7c96, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

temporary patch to implicitly add -m32 for 32-bit compiles

  • Property mode set to 100644
File size: 18.4 KB
Line 
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 Aug 23 15:41:55 2018
13// Update Count : 270
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#include <string.h> // strcmp
22
23#include <sys/types.h>
24#include <sys/stat.h>
25
26#include "Common/SemanticError.h"
27#include "config.h" // configure info
28
29using std::cerr;
30using std::endl;
31using std::string;
32using std::to_string;
33
34
35//#define __DEBUG_H__
36
37
38bool prefix( string arg, string pre ) {
39 return arg.substr( 0, pre.size() ) == pre;
40} // prefix
41
42enum { NumSuffixes = 2 };
43const string suffixes[NumSuffixes] = { "cfa", "hfa", };
44
45void suffix( string arg, const char * args[], int & nargs ) {
46 //std::cerr << arg << std::endl;
47 size_t dot = arg.find_last_of( "." );
48 //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
49 if ( dot == string::npos ) return;
50 string sx = arg.substr( dot + 1 );
51 for ( int i = 0; i < NumSuffixes; i += 1 ) {
52 if ( sx == suffixes[i] ) {
53 args[nargs] = "-x";
54 nargs += 1;
55 args[nargs] = "c";
56 nargs += 1;
57 return;
58 } // if
59 } // for
60} // suffix
61
62
63void shuffle( const char *args[], int S, int E, int N ) {
64 // S & E index 1 passed the end so adjust with -1
65 #ifdef __DEBUG_H__
66 cerr << "shuffle:" << S << " " << E << " " << N << endl;
67 #endif // __DEBUG_H__
68 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
69 #ifdef __DEBUG_H__
70 cerr << "\t" << j << " " << j-N << endl;
71 #endif // __DEBUG_H__
72 args[j] = args[j-N];
73 } // for
74} // shuffle
75
76static inline bool dirExists(const string & path) {
77 struct stat info;
78 if(stat( path.c_str(), &info ) != 0)
79 return false;
80 else if(info.st_mode & S_IFDIR)
81 return true;
82 else
83 return false;
84} //dirExists
85
86
87#define str(s) #s
88
89int main( int argc, char *argv[] ) {
90 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
91 string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
92
93 string installincdir( CFA_INCDIR ); // fixed location of include files
94 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed
95 string srcdriverdir ( TOP_BUILDDIR "driver"); // fixed location of cc1 and cfa-cpp commands when in tree
96
97 string heading; // banner printed at start of cfa compilation
98 string arg; // current command-line argument during command-line parsing
99 string Bprefix; // path where gcc looks for compiler command steps
100 string langstd; // language standard
101
102 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
103 string compiler_name; // name of C compiler
104
105 bool nonoptarg = false; // indicates non-option argument specified
106 bool link = true; // linking as well as compiling
107 bool verbose = false; // -v flag
108 bool quiet = false; // -quiet flag
109 bool debug = true; // -debug flag
110 bool help = false; // -help flag
111 bool CFA_flag = false; // -CFA flag
112 bool cpp_flag = false; // -E or -M flag, preprocessor only
113 bool std_flag = false; // -std= flag
114 bool noincstd_flag = false; // -no-include-stdhdr= flag
115 bool xflag = false; // user supplied -x flag
116 bool debugging __attribute(( unused )) = false; // -g flag
117 bool m32 = false; // -m32 flag
118 bool m64 = false; // -m64 flag
119 bool intree = false;
120
121 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
122 int sargs = 1; // starting location for arguments in args list
123 int nargs = sargs; // number of arguments in args list; 0 => command name
124
125 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
126 int nlibs = 0;
127
128 #ifdef __DEBUG_H__
129 cerr << "CFA:" << endl;
130 #endif // __DEBUG_H__
131
132 // process command-line arguments
133
134 for ( int i = 1; i < argc; i += 1 ) {
135 #ifdef __DEBUG_H__
136 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
137 #endif // __DEBUG_H__
138 arg = argv[i]; // convert to string value
139 #ifdef __DEBUG_H__
140 cerr << "arg:\"" << arg << "\"" << endl;
141 #endif // __DEBUG_H__
142 if ( prefix( arg, "-" ) ) {
143 // pass through arguments
144
145 if ( arg == "-Xlinker" || arg == "-o" ) {
146 args[nargs] = argv[i]; // pass the argument along
147 nargs += 1;
148 i += 1;
149 if ( i == argc ) continue; // next argument available ?
150 args[nargs] = argv[i]; // pass the argument along
151 nargs += 1;
152 } else if ( arg == "-XCFA" ) { // CFA pass through
153 i += 1;
154 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
155 nargs += 1;
156
157 // CFA specific arguments
158
159 } else if ( arg == "-CFA" ) {
160 CFA_flag = true; // strip the -CFA flag
161 link = false;
162 args[nargs] = "-E"; // replace the argument with -E
163 nargs += 1;
164 } else if ( arg == "-debug" ) {
165 debug = true; // strip the debug flag
166 } else if ( arg == "-nodebug" ) {
167 debug = false; // strip the nodebug flag
168 } else if ( arg == "-quiet" ) {
169 quiet = true; // strip the quiet flag
170 } else if ( arg == "-noquiet" ) {
171 quiet = false; // strip the noquiet flag
172 } else if ( arg == "-help" ) {
173 help = true; // strip the help flag
174 } else if ( arg == "-nohelp" ) {
175 help = false; // strip the nohelp flag
176 } else if ( arg == "-no-include-stdhdr" ) {
177 noincstd_flag = true; // strip the no-include-stdhdr flag
178 } else if ( arg == "-in-tree" ) {
179 intree = true;
180 } else if ( arg == "-compiler" ) {
181 // use the user specified compiler
182 i += 1;
183 if ( i == argc ) continue; // next argument available ?
184 compiler_path = argv[i];
185 if ( putenv( (char *)( *new string( string( "__CFA_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
186 cerr << argv[0] << " error, cannot set environment variable." << endl;
187 exit( EXIT_FAILURE );
188 } // if
189
190 // C specific arguments
191
192 } else if ( arg == "-v" ) {
193 verbose = true; // verbosity required
194 args[nargs] = argv[i]; // pass the argument along
195 nargs += 1;
196 } else if ( arg == "-g" ) {
197 debugging = true; // symbolic debugging required
198 args[nargs] = argv[i]; // pass the argument along
199 nargs += 1;
200 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
201 std_flag = true; // -std=XX provided
202 args[nargs] = argv[i]; // pass the argument along
203 nargs += 1;
204 } else if ( arg == "-x" ) {
205 xflag = true;
206 args[nargs] = argv[i]; // pass the argument along
207 nargs += 1;
208 i += 1; // advance to argument
209 args[nargs] = argv[i]; // pass the argument along
210 nargs += 1;
211 // args[nargs] = ( *new string( string("-D__GCC_X__=") + argv[i] ) ).c_str(); // add the argument for -x
212 // nargs += 1;
213 } else if ( prefix( arg, "-x" ) ) {
214 xflag = true;
215 args[nargs] = argv[i]; // pass the argument along
216 nargs += 1;
217 // args[nargs] = ( *new string( string("-D__GCC_X__=") + arg.substr(2) ) ).c_str(); // add the argument for -x
218 // nargs += 1;
219 } else if ( arg == "-w" ) {
220 args[nargs] = argv[i]; // pass the argument along
221 nargs += 1;
222 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
223 nargs += 1;
224 } else if ( prefix( arg, "-W" ) ) { // check before next tests
225 if ( arg == "-Werror" || arg == "-Wall" ) {
226 args[nargs] = argv[i]; // pass the argument along
227 nargs += 1;
228 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
229 nargs += 1;
230 } else {
231 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
232 args[nargs] = argv[i]; // conditionally pass the argument along
233 const char * warning = argv[i] + adv; // extract warning
234 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
235 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
236 } // if
237 nargs += 1;
238 } // if
239 } else if ( prefix( arg, "-B" ) ) {
240 Bprefix = arg.substr(2); // strip the -B flag
241 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
242 nargs += 1;
243 } else if ( prefix( arg, "-b" ) ) {
244 if ( arg.length() == 2 ) { // separate argument ?
245 i += 1;
246 if ( i == argc ) continue; // next argument available ?
247 arg += argv[i]; // concatenate argument
248 } // if
249 // later versions of gcc require the -b option to appear at the start of the command line
250 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
251 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
252 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
253 cerr << argv[0] << " error, cannot set environment variable." << endl;
254 exit( EXIT_FAILURE );
255 } // if
256 sargs += 1;
257 nargs += 1;
258 } else if ( prefix( arg, "-V" ) ) {
259 if ( arg.length() == 2 ) { // separate argument ?
260 i += 1;
261 if ( i == argc ) continue; // next argument available ?
262 arg += argv[i]; // concatenate argument
263 } // if
264 // later versions of gcc require the -V option to appear at the start of the command line
265 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
266 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
267 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
268 cerr << argv[0] << " error, cannot set environment variable." << endl;
269 exit( EXIT_FAILURE );
270 } // if
271 sargs += 1;
272 nargs += 1;
273 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
274 args[nargs] = argv[i]; // pass the argument along
275 nargs += 1;
276 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
277 cpp_flag = true; // cpp only
278 } // if
279 link = false; // no linkage required
280 } else if ( arg[1] == 'l' ) {
281 // if the user specifies a library, load it after user code
282 libs[nlibs] = argv[i];
283 nlibs += 1;
284 } else if ( arg == "-m32" ) {
285 m32 = true;
286 m64 = false;
287 args[nargs] = argv[i];
288 nargs += 1;
289 } else if ( arg == "-m64" ) {
290 m64 = true;
291 m32 = false;
292 args[nargs] = argv[i];
293 nargs += 1;
294 } else {
295 // concatenate any other arguments
296 args[nargs] = argv[i];
297 nargs += 1;
298 } // if
299 } else {
300 bool opt = false;
301 if ( ! xflag ) {
302 suffix( arg, args, nargs ); // check suffix
303 // args[nargs] = ( *new string( string("-D__GCC_X__=c") ) ).c_str(); // add the argument for -x
304 // nargs += 1;
305 opt = true;
306 } // if
307 // concatenate other arguments
308 args[nargs] = argv[i];
309 nargs += 1;
310 if ( opt ) {
311 args[nargs] = "-x";
312 nargs += 1;
313 args[nargs] = "none";
314 nargs += 1;
315 // args[nargs] = ( *new string( string("-D__GCC_X__=none") ) ).c_str(); // add the argument for -x
316 // nargs += 1;
317 } // if
318 nonoptarg = true;
319 xflag = false;
320 } // if
321 } // for
322
323 #ifdef __x86_64__
324 args[nargs] = "-mcx16"; // allow double-wide CAA
325 nargs += 1;
326 #endif // __x86_64__
327
328 #ifdef __DEBUG_H__
329 cerr << "args:";
330 for ( int i = 1; i < nargs; i += 1 ) {
331 cerr << " " << args[i];
332 } // for
333 cerr << endl;
334 #endif // __DEBUG_H__
335
336 if ( cpp_flag && CFA_flag ) {
337 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
338 exit( EXIT_FAILURE );
339 } // if
340
341 // add the CFA include-library paths, which allow direct access to header files without directory qualification
342 if( !intree ) {
343 args[nargs] = "-I" CFA_INCDIR;
344 nargs += 1;
345 if ( ! noincstd_flag ) { // do not use during build
346 args[nargs] = "-I" CFA_INCDIR "stdhdr";
347 nargs += 1;
348 } // if
349 args[nargs] = "-I" CFA_INCDIR "concurrency";
350 nargs += 1;
351 args[nargs] = "-I" CFA_INCDIR "containers";
352 nargs += 1;
353 } else {
354 args[nargs] = "-I" TOP_SRCDIR "libcfa/src";
355 nargs += 1;
356 if ( ! noincstd_flag ) { // do not use during build
357 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
358 nargs += 1;
359 } // if
360 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
361 nargs += 1;
362 args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
363 nargs += 1;
364 }
365
366 // add stdbool to get defines for bool/true/false
367 args[nargs] = "-imacros";
368 nargs += 1;
369 args[nargs] = "stdbool.h";
370 nargs += 1;
371
372 string libbase;
373 if( !intree ) {
374 libbase = CFA_LIBDIR;
375 } else {
376 libbase = TOP_BUILDDIR "libcfa/";
377 args[nargs] = "-D__CFA_FLAG__=-t";
378 nargs += 1;
379 }
380
381 string arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
382 if ( ! m32 && ! m64 && arch == "x86" ) { // no override and 32-bit architecture
383 args[nargs] = "-m32";
384 nargs += 1;
385 } // if
386 const char * config = debug ? "debug": "nodebug";
387 string libdir = libbase + arch + "-" + config;
388
389 if ( ! dirExists( libdir ) ) {
390 cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
391 cerr << "Was looking for " << libdir << endl;
392 libdir = libbase + arch + "-" + "nolib";
393 } // if
394
395 if ( ! dirExists( libdir ) ) {
396 cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
397 cerr << "Was looking for " << libdir << endl;
398 exit( EXIT_FAILURE );
399 } // if
400
401 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir + (intree ? "/prelude" : "")) ).c_str();
402 nargs += 1;
403
404 if ( link ) {
405 args[nargs] = "-Xlinker";
406 nargs += 1;
407 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
408 nargs += 1;
409 args[nargs] = "-Xlinker";
410 nargs += 1;
411 args[nargs] = "--undefined=__cfaabi_interpose_startup";
412 nargs += 1;
413 args[nargs] = "-Xlinker";
414 nargs += 1;
415 args[nargs] = "--undefined=__cfaabi_appready_startup";
416 nargs += 1;
417 args[nargs] = "-Xlinker";
418 nargs += 1;
419 args[nargs] = "--undefined=__cfaabi_dbg_record";
420 nargs += 1;
421
422 // include the cfa library in case it's needed
423 args[nargs] = ( *new string( string("-L" ) + libdir + (intree ? "/src" : "")) ).c_str();
424 nargs += 1;
425 args[nargs] = "-lcfa";
426 nargs += 1;
427 args[nargs] = "-lpthread";
428 nargs += 1;
429 args[nargs] = "-ldl";
430 nargs += 1;
431 args[nargs] = "-lrt";
432 nargs += 1;
433 } // if
434
435 // Add exception flags (unconditionally)
436 args[nargs] = "-fexceptions";
437 nargs += 1;
438
439 // add the correct set of flags based on the type of compile this is
440
441 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
442 nargs += 1;
443 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
444 nargs += 1;
445 args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
446 nargs += 1;
447 args[nargs] = "-D__CFA__";
448 nargs += 1;
449 args[nargs] = "-D__CFORALL__";
450 nargs += 1;
451 args[nargs] = "-D__cforall";
452 nargs += 1;
453
454 if ( cpp_flag ) {
455 args[nargs] = "-D__CPP__";
456 nargs += 1;
457 } // if
458
459 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
460 nargs += 1;
461 if ( CFA_flag ) {
462 args[sargs] = "-D__CFA_FLAG__=-N";
463 args[nargs] = "-D__CFA_PREPROCESS_";
464 nargs += 1;
465 } else {
466 args[sargs] = "-D__CFA_FLAG__=-L";
467 } // if
468 sargs += 1;
469
470 if ( debug ) {
471 heading += " (debug)";
472 args[nargs] = "-D__CFA_DEBUG__";
473 nargs += 1;
474 } else {
475 heading += " (no debug)";
476 } // if
477
478 if ( Bprefix.length() == 0 ) {
479 Bprefix = ! intree ? installlibdir : srcdriverdir;
480 if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
481 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
482 nargs += 1;
483 } // if
484
485 args[nargs] = "-Xlinker"; // used by backtrace
486 nargs += 1;
487 args[nargs] = "-export-dynamic";
488 nargs += 1;
489
490 // execute the compilation command
491
492 args[0] = compiler_path.c_str(); // set compiler command for exec
493 // find actual name of the compiler independent of the path to it
494 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
495 if ( p == -1 ) {
496 compiler_name = compiler_path;
497 } else {
498 compiler_name = *new string( compiler_path.substr( p + 1 ) );
499 } // if
500
501 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
502 args[nargs] = "-no-integrated-cpp";
503 nargs += 1;
504 args[nargs] = "-Wno-deprecated";
505 nargs += 1;
506 if ( ! std_flag ) { // default c11, if none specified
507 args[nargs] = "-std=gnu11";
508 nargs += 1;
509 } // if
510 args[nargs] = "-fgnu89-inline";
511 nargs += 1;
512 args[nargs] = "-D__int8_t_defined"; // prevent gcc type-size attributes
513 nargs += 1;
514 args[nargs] = ( *new string( string("-B") + Bprefix ) ).c_str();
515 nargs += 1;
516 args[nargs] = "-lm";
517 nargs += 1;
518 } else {
519 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
520 exit( EXIT_FAILURE );
521 } // if
522
523 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
524 args[nargs] = libs[i];
525 nargs += 1;
526 } // for
527
528 args[nargs] = NULL; // terminate with NULL
529
530 #ifdef __DEBUG_H__
531 cerr << "nargs: " << nargs << endl;
532 cerr << "args:" << endl;
533 for ( int i = 0; args[i] != NULL; i += 1 ) {
534 cerr << " \"" << args[i] << "\"" << endl;
535 } // for
536 #endif // __DEBUG_H__
537
538 if ( ! quiet ) {
539 cerr << "CFA " << "Version " << Version << heading << endl;
540
541 if ( help ) {
542 cerr <<
543 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
544 "-help\t\t\t: print this help message" << endl <<
545 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
546 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
547 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
548 "...\t\t\t: any other " << compiler_name << " flags" << endl;
549 } // if
550 } // if
551
552 if ( verbose ) {
553 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
554
555 for ( int i = 0; args[i] != NULL; i += 1 ) {
556 cerr << args[i] << " ";
557 } // for
558 cerr << endl;
559 } // if
560
561 if ( ! nonoptarg ) {
562 cerr << argv[0] << " error, no input files" << endl;
563 exit( EXIT_FAILURE );
564 } // if
565
566 // execute the command and return the result
567
568 execvp( args[0], (char *const *)args ); // should not return
569 perror( "CFA Translator error: cfa level, execvp" );
570 exit( EXIT_FAILURE );
571} // main
572
573// Local Variables: //
574// tab-width: 4 //
575// mode: c++ //
576// compile-command: "make install" //
577// End: //
Note: See TracBrowser for help on using the repository browser.